Filters are data processing
techniques that can smooth out high-frequency fluctuations in data
or remove periodic trends of a specific frequency from data. In MATLAB^{®},
the `filter`

function filters
a vector of data *x* according to the following difference
equation, which describes a tapped delay-line filter.

$$\begin{array}{l}a(1)y(n)=b(1)x(n)+b(2)x(n-1)+\dots +b({N}_{b})x(n-{N}_{b}+1)\\ \text{}-a(2)y(n-1)-\dots -a({N}_{a})y(n-{N}_{a}+1)\end{array}$$

In this equation, *a* and *b* are
vectors of coefficients of the filter, *N _{a}* is
the feedback filter order, and

The `filter`

function uses specified coefficient
vectors *a* and *b* to filter the
input data *x*. For more information on difference
equations describing filters, see [1].

The `filter`

function is one way to implement a moving-average filter, which is a common data smoothing technique.

The following difference equation describes a filter that averages time-dependent data with respect to the current hour and the three previous hours of data.

$$y(n)=\frac{1}{4}x(n)+\frac{1}{4}x(n-1)+\frac{1}{4}x(n-2)+\frac{1}{4}x(n-3)$$

Import data that describes traffic flow over time, and assign the first column of vehicle counts to the vector `x`

.

```
load count.dat
x = count(:,1);
```

Create the filter coefficient vectors.

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

Compute the 4-hour moving average of the data, and plot both the original data and the filtered data.

y = filter(b,a,x); t = 1:length(x); plot(t,x,'--',t,y,'-') legend('Original Data','Filtered Data')

This example shows how to modify the amplitude of a vector of data by applying a transfer function.

In digital signal processing, filters are often represented by a transfer function. The Z-transform of the difference equation

$$\begin{array}{rcl}a(1)y(n)& =& b(1)x(n)+b(2)x(n-1)+...+b({N}_{b})x(n-{N}_{b}+1)\\ & & -a(2)y(n-1)-...-a({N}_{a})y(n-{N}_{a}+1)\end{array}$$

is the following transfer function.

$$Y(z)=H({z}^{-1})X(z)=\frac{b(1)+b(2){z}^{-1}+...+b({N}_{b}){z}^{-{N}_{b}+1}}{a(1)+a(2){z}^{-1}+...+a({N}_{a}){z}^{-{N}_{a}+1}}X(z)$$

Use the transfer function

$$H({z}^{-1})=\frac{b({z}^{-1})}{a({z}^{-1})}=\frac{2+3{z}^{-1}}{1+0.2{z}^{-1}}$$

to modify the amplitude of the data in `count.dat`

.

Load the data and assign the first column to the vector `x`

.

```
load count.dat
x = count(:,1);
```

Create the filter coefficient vectors according to the transfer function $$H({z}^{-1})$$.

a = [1 0.2]; b = [2 3];

Compute the filtered data, and plot both the original data and the filtered data. This filter primarily modifies the amplitude of the original data.

y = filter(b,a,x); t = 1:length(x); plot(t,x,'--',t,y,'-') legend('Original Data','Filtered Data')

[1] Oppenheim, Alan V., Ronald W. Schafer,
and John R. Buck. *Discrete-Time Signal Processing*.
Upper Saddle River, NJ: Prentice-Hall, 1999.

`conv`

| `filter`

| `filter2`

| `movmean`

| `smoothdata`