
6.5 COMPARING FUNCTIONS FOR FILTERING DATA SERIES 169
6 SIGNAL PROCESSING
y1 = y1(2:101,1);
A more general way to correct the phase shi s of conv is
y1 = y1(1+(m1-1)/2:end-(m1-1)/2,1);
which, of course, only works for an odd number of lter weights. We can
then plot both input and output signals for comparison, using
legend to
display a legend for the plot.
plot(t,x1,'b-',t,y1,'r-')
legend('x1(t)','y1(t)')
is plot illustrates the e ect that the running mean has the original input
series. e output
y1 is signi cantly smoother than the input signal x1. If
we increase the length of the lter, we obtain an even smoother signal out-
put.
b2 = [1 1 1 1 1]/5;
m2 = length(b2);
y2 = conv(b2,x1);
y2 = y2(1+(m2-1)/2:end-(m2-1)/2,1);
plot(t,x1,'b-',t,y1,'r-',t,y2,'g-')
legend('x1(t)','y1(t)','y2(t)')
e next section provides a more general description of lters.
6.5 Comparing Functions for Filtering Data Series
e lters described in the previous section were very simple examples of
nonrecursive filters, in which the lter output y(t) depends only on the l-
ter input x(t) and the lter weights b
k
. Prior to introducing a more general
description of linear time-invariant lters, we replace the function
conv
by
filter, which can also be used for recursive filters. In this case, the
output y(t
n
) depends not only on the lter input x(t), but also on previous
elements of the output y(t
n–1
), y(t
n–2
), y(t
n–3
) and so on (Section 6.6). We
will rst use
conv for nonrecursive lters in order to compare the results of
conv and filter.
clear
t = (1:100)';
randn('seed',0);
x3 = randn(100,1);