% 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) ...
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:
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