function MovingAverageDemo
% Lesson 5, VMI
% James C. Squire, 9/2004

% sensor signal
N = 20;
n=-N:N;
s=[zeros(1,N) 1 ones(1,N)];
N = length(n);
subplot(3,1,1)
stem(n,s)
title('Projectile sensor s[n] without noise')
pause

% after added noise
rand('state',7)
d = 2*(rand(size(s))-0.5);
r = d+s;
subplot(3,1,2)
stem(n,r)
title('Projectile sensor r[n] with noise')
pause

% after filtering length M filter
M = 7;
p = zeros(size(r));
for i=1:M-1
    p(i) = sum(r(1:i))/i;
end
for i=M:N
    p(i) = sum(r(i-M+1:i))/M;
end
subplot(3,1,3)
stem(n,p)
title('p[n] after processing with MA filter')

figure(gcf)