Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

## Filtering Data

### Introduction

Various MATLAB® IEEE® functions help you work with difference equations and filters to shape the variations in the raw data. These functions operate on both vectors and matrices. Filter data to smooth out high-frequency fluctuations or remove periodic trends of a specific frequency.

A vector input represents a single, sampled data signal (or sequence). For a matrix input, each signal corresponds to a column in the matrix and each data sample is a row.

### Filter Function

The function

```y = filter(b,a,x)
```

creates filtered data y by processing the data in vector x with the filter described by vectors a and b.

The filter function is a general tapped delay-line filter, described by the difference equation

Here, n is the index of the current sample, is the order of the polynomial described by vector a, and is the order of the polynomial described by vector b. The output y(n) is a linear combination of current and previous inputs, x(n) x(n – 1)..., and previous outputs, y(n – 1) y(n – 2)... .

### Example: Moving Average Filter

Smooth the data in count.dat using a moving-average filter to see the average traffic flow over a 4-hour window (covering the current hour and the previous 3 hours). This is represented by the following difference equation:

The corresponding vectors are

```a = 1;
b = [1/4 1/4 1/4 1/4];
```

For information on loading the data in this example, see Loading the count.dat Data

1. Extract the first column of count and assign it to the vector x:

```x = count(:,1);
```
2. Calculate the 4-hour moving average of the data:

```y = filter(b,a,x);
```
3. The filtered data, represented by the solid line in the plot, is the 4-hour moving average of the count data. The original data is represented by the dashed line.

Plot of Original and Smoothed Data

### Example: Discrete Filter

You use the discrete filter to shape the data by applying a transfer function to the input signal.

Depending on your objectives, the transfer function you choose might alter both the amplitude and the phase of the variations in the data at different frequencies to produce either a smoother or a rougher output.

Taking the z-transform of the following difference equation

results in the following transfer function:

Here Y(z) is the z-transform of the filtered output y(n). The coefficients b and a are unchanged by the z-transform.

In digital signal processing (DSP), it is customary to write transfer functions as rational expressions in and to order the numerator and denominator terms in ascending powers of .

Consider the following transfer function:

To apply this transfer function to the data in count.dat:

1. Load the matrix count into the workspace:

```load count.dat;
```
2. Extract the first column and assign it to x:

```x = count(:,1);
```
3. Enter the coefficients of the denominator ordered in ascending powers of to represent :

```a = [1 0.2];
```
4. Enter the coefficients of the numerator to represent :

```b = [2 3];
```
5. Call the filter function:

```y = filter(b,a,x);
```
6. Compare the original data and the shaped data with an overlaid plot of the two curves:

```t = 1:length(x);
plot(t,x,'-.',t,y,'-'), grid on
legend('Original Data','Shaped Data',2)
```

The plot shows this filter primarily modifies the amplitude of the original data.

Plot of Original and Shaped Data

Was this topic helpful?