next up previous
Next: Fourier Transforms Up: Data Analysis Previous: Curve Fitting (Regression)


Difference Equations & Filters

Matlab allows us to implement filters often on data points in vectors form. In this example I've shown how we could implement a moving average filter for a sample dataset (indicating traffic conditions at various times at 3 locations). The data itself is represented in the following format: each row of data corresponds to a time with the 3 columns indicating traffic at the 3 locations.

% File: count.dat
    11    11     9
     7    13    11
     ...
    10     9     7

% Hints: use ; at end of line to prevent debug'ish output from being printed
                                                                                                                       
load count.dat
a = 1
b = [1/4 1/4 1/4 1/4]        % vector for 4 hour moving average
% -- essentially a fourth of the current and past 3 inputs
      
x = count(:,1)
y = filter(b,a,x)            % Does the averaging
      
t = 1:length(x)
plot(t,x,'-.',t,y,'-'), grid on
legend('Original Data','Smoothed Data',2)

The filter command can be thought of as an implementation of the difference equation the output y(n) of which is a linear combination of current and previous inputs, x(n) x(n-1) ..., and previous outputs, y(n-1) y(n-2) ...

\begin{displaymath}a(1)y(n) = (b(1)x(n) + b(2)x(n-1) + ... + b(nb)x(n-nb+1)) - (a(2)y(n-1) - ... - a(na)y(n-na+1))\end{displaymath}

The filter structure shown above is the general tapped delay-line filter described by the difference equation below, where n is the index of the current sample, na is the order of the polynomial described by vector a and nb is the order of the polynomial described by vector b.In our example, vectors `a' and `b' correspond to values `1' and [1/4, 1/4, 1/4, 1/4]. So we get:

\begin{displaymath}y(n) = \frac{1}{4}\left(x(n) + x(n-1) + x(n-2)) + x(n-3)\right) \end{displaymath}

The corresponding bit of code in Matlab becomes: filter(b, a, x). A plot shows how the filter smooths out the data over a 4-hour period.

a =
     1

b =
    0.2500    0.2500    0.2500    0.2500

x =
    11
     7
    14
    11
    43
    38
    61
    75
    38
    28
    12
    18
    18
    17
    19
    32
    42
    57
    44
   114
    35
    11
    13
    10

y =
    2.7500
    4.5000
    8.0000
   10.7500
   18.7500
   26.5000
   38.2500
   54.2500
   53.0000
   50.5000
   38.2500
   24.0000
   19.0000
   16.2500
   18.0000
   21.5000
   27.5000
   37.5000
   43.7500
   64.2500
   62.5000
   51.0000
   43.2500
   17.2500

t =
  Columns 1 through 12 
     1     2     3     4     5     6     7     8     9    10    11    12
  Columns 13 through 24 
    13    14    15    16    17    18    19    20    21    22    23    24
Figure 6: Effect of 4-Hour Moving Average Filter
\begin{figure}\epsfig{file=src/filters.eps, width=13cm}
\end{figure}


next up previous
Next: Fourier Transforms Up: Data Analysis Previous: Curve Fitting (Regression)
Arvind Gopu 2006-03-24