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.

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

$$\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}$$

Here, *n* is the index of the current sample,$${N}_{a}$$ is the order of the polynomial
described by vector *a*, and $${N}_{b}$$ 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)... .

This example shows how to 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:

Create the corresponding vectors.

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

Import the data from `count.dat`

using the `load`

function.

```
load count.dat
```

Loading this data creates a 24-by-3 matrix called `count`

in the MATLAB® workspace.

Extract the first column of `count`

and assign it to the vector `x`

.

x = count(:,1);

Calculate the 4-hour moving average of the data.

y = filter(b,a,x);

Plot the original data and the filtered data.

figure t = 1:length(x); plot(t,x,'--',t,y,'-'),grid on legend('Original Data','Smoothed Data',2) title('Plot of Original and Smoothed Data')

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.

This example shows how to use the discrete filter to shape data by applying a transfer function to an 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 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 transfer function:

The following code defines and applies this transfer function to the data in `count.dat`

.

Load the matrix count into the workspace.

```
load count.dat
```

Extract the first column and assign it to `x`

.

x = count(:,1);

Enter the coefficients of the denominator ordered in ascending powers of to represent .

a = [1 0.2];

Enter the coefficients of the numerator to represent .

b = [2 3];

Call the filter function.

y = filter(b,a,x);

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) title('Plot of Original and Shaped Data')

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

Was this topic helpful?