Summary: Average stock price as an example of discrete-time filtering.
Suppose we want to average daily stock prices taken over last
year to yield a running weekly average (average over five
trading sessions). The filter we want is a length-5 averager
(as shown in the unit-sample response), and the input's
duration is 253 (365 calendar days minus weekend days and
holidays). The output duration will be
![]() |
Figure 1 shows the input and the filtered output. The MATLAB programs that compute the filtered output in the time and frequency domains are
Time Domain
h = [1 1 1 1 1]/5;
y = filter(h,1,[djia zeros(1,4)]);
Frequency Domain
h = [1 1 1 1 1]/5;
DJIA = fft(djia, 512);
H = fft(h, 512);
Y = H.*X;
y = ifft(Y);
filter program has the "feature"
that the length of its output equals the length of its
input. To force it to produce a signal having the proper
length, the program zero-pads the input
appropriately.
fft function automatically
zero-pads its input if the specified transform length (its
second argument) exceeds the signal's length. The frequency
domain result will have a small imaginary component —
largest value is
An interesting signal processing aspect of this example is demonstrated at the beginning and end of the output. The ramping up and down that occurs can be traced to assuming the input is zero before it begins and after it ends. The filter "sees" these initial and final values as the difference equation passes over the input. These artifacts can be handled in two ways: we can just ignore the edge effects or the data from previous and succeeding years' last and first week, respectively, can be placed at the ends.