| MATLAB® | ![]() |
| On this page… |
|---|
A variety of 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. You can 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
![]()
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)... .
You can 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];
Extract the first column of count and assign it to the vector x:
x = count(:,1);
The 4-hour moving average of the data is calculated by
y = filter(b,a,x);
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

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:
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)
As you can see from the plot, this filter primarily modifies the amplitude of the original data.
Plot of Original and Shaped Data

![]() | Inconsistent Data | Detrending Data | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |