Documentation Center

  • Trials
  • 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)... .

Moving Average Filter

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:

$$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).$$

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.

Discrete Filter

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

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

results in 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).$$

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 $z^{-1}$ and to order the numerator and denominator terms in ascending powers of $z^{-1}$ .

Consider the transfer function:

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

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 $z^{-1}$ to represent $1+0.2z^{-1}$ .

a = [1 0.2];

Enter the coefficients of the numerator to represent $2+3z^{-1}$ .

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?