%% Moving-Average Filter of Traffic Data
% 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')

% Copyright 2012 The MathWorks, Inc.