Time Domain Representation of Continuous and Discrete Signals 77
plot(n,y);title(‘Noise vs. n’);hold on;
stem(n,noise);hold on;plot(n,y,n,noise);
ylabel(‘Amplitude’);xlabel(‘time index n’);
axis([0 20 -1.3 1.3]);
subplot(3,1,3);
signoi =signal+noise;
stem(n,signoi);title(‘[Signal + Noise] vs. n’);hold on;
plot(n,signoi);ylabel(‘Amplitude[ signal+noise]’);
xlabel(‘ time index n’)
figure(2)
subplot(3,1,1)
N = [.5 .5];
D = 1;
movave2 = filter(N,D,signoi);
plot(n,sig n al,n,sig noi,’s--’,n,m ovave2,’o-.’);
legend(‘signal’,’signal+noise’,’moving ave/2term’)
title(‘Various moving averages’);ylabel(‘magnitude’)
subplot(3,1,2)
N=[.33 .33 .33];
D=1;
movave3=filter(N,D,signoi);
plot(n,sig n al,n,sig noi,’s--’,n,m ovave3,’o-.’);yla bel(‘m ag nitude’)
legend(‘signal’,’signal+noise’,’moving ave/3term’)
subplot(3,1,3)
N = [.25 .25 .25 .25];
D = 1;
movave4 = filter(N,D,signoi);
plot(n,signal,n,signoi,’s--’,n,movave4,’o-.’);
legend(‘signal’,’signal+noise’,’moving ave/4term’)
ylabel(‘magnitude’);xlabel(‘ time index n’)
figure(3)
plot(n,signal,n,signoi,’ks--’,n,movave2,’ko-.’);
legend(‘signal’,’signal+noise’,’moving ave/2term’);
title(‘Best approximation using moving averages’);
ylabel(‘magnitude’);xlabel(‘ time index n’)
figure(4)
err2 = signal-movave2;
stem(n,err2);hold on;plot(n,y,n,err2);
title(‘error=[mov.ave/2terms ] vs t’);
ylabel(‘error’);xlabel(‘ time index n’)
error1= sum(abs(signal-signoi)/21);
error2 = sum(abs(signal-movave2)/21);
error3 = sum(abs(signal-movave3)/21);
error4 = sum(abs(signal-movave4)/21);
disp(‘**************************************************’)
disp(‘****************ANALYSIS OF ERROR****************’)
disp(‘**************************************************’)
disp(‘ no ave 2 term ave 3 term ave 4 term ave ‘);
disp([error1 error2 error3 error4])
disp(‘**************************************************’)
disp(‘**************************************************’)
Back in the command window the script fi le averages is executed three times, and the
results are shown as follows and in the plots of Figures 1.63 through 1.66.