BillHung.Net


powered by FreeFind     sms

EE 20N Fall 04 HW3 Moving Average

%%The following Matlab function can be defined in a file called movingAverage.

m:

 

function y = movingAverage(x)

for n=1:3

y(i) = sum(x(1:n))/4;

end;

N = 4:length(x);

y(N) = (x(N-3) + x(N-2) + x(N-1) + x(N))/4;

 

%%Notice the way this is written. The for loop calculates the first four samples.

The last line calculates all the remaining samples, exploiting the fact that

Matlab automatically vectorizes. This yields a very compact definition. An

even more compact definition is

 

function y = movingAverage(x)

z = [zeros(1,3), x];

N = 1:length(x);

y(N) = ( z(N) + z(N+1) + z(N+2) + z(N+3))/4;

 

%%Here, we prepend 3 zeros in front of the argument vector, and then compute

the output vector by adding five neighboring values of the expanded vector.

(a) The unit step at 10 can be generated compactly with the commands:

 

N = 1:20;

x = N>=10;

 

%%2 The second command assigns to each element of the vector x the result

of comparing the corresponding value of t to 10. To check this, plot x

with the command:

stem(x)