% prj_1.m Project #1 to observe the effect "simple moving average" % on a noisy sequence, defined as % y(n) = (1/M)*sum[r(n-m), m=0,...,M-1], n=0,...,N-1 % where r(n)=x(r)+no(n)=signal + noise % % we encode the dsp statement into matlabese % by (i) setting n=nr-1 and (ii) m=mr-1 % the moving average now reads % yr(nr) = (1/M)*sum[rr(nr-mr+1), mr=1,...,M], nr=1,...,N % ***** NOTICE THE '+1' WHICH APPEARS IN THE ARGUMENT OF xr( ) !! **************** % we can now implement this form in matlab % we define x for more clarity % this is encoded, then, into xr(nr)=1, nr in [1,16] and zero otherwise % clear,clc,clf N=32; % total length of sequences L=12; % width of simple pulse x: xr=[zeros(1,8),ones(1,L),zeros(1,N-L-8)]; % signal var=0.1; % noise variance no=randn(1,N); % noise sequence rr=xr+var*no; % noisy signal M=4; % length of moving average % perform moving average for nr=1:N yr(nr)=0.0; for mr=1:M % accumulate average if nr-mr+1>=1 & nr-mr+1<=N yr(nr)=yr(nr)+rr(nr-mr+1)/M; end end end % we can now DECODE the matlab representations using the decoded index n n=(0:N-1); plot(n,xr,'c+',n,rr,'o',n,yr,'r*') axis([0 N-1 -0.1 1.1]) xlabel('discrete time ') title('a noisy moving average') text(25,1,'signal','color','c') text(25,0.75,'noisy signal','color','y') text(25,0.5,'smoothed signal','color','r')