Вы находитесь на странице: 1из 9

clear all clc close all v=randn(1000,1); % produces a white noise with zeo mean and unit variance

% choose the no. of samples large enough so that var is % close to 1

% Part a a1=0; a2=-0.81; u=zeros(24,1); u(1:2)=v(1:2,:); % sets u[1]=v[1] and u[2]=v[2], since u[-1] and u[0] are zero, % note that we have u[n] for n=1,2,...,24 for i=3:24 u(i,:)=a2*u(i-2,:)+v(i,:); end figure plot(v(1:24),'r-*') hold on plot(u,'b-o') legend('white noise v[n]','sequence u[n]') title('AR random process time sequences') ylabel('saample value') xlabel('sample number')

%% part b r_u_hat=zeros(24,1);% initiate the estimated autocorrelation vector for m=0:23 ind=m+1; s=0; for n=1:24 k=n-m; if k>0 s=s+u(n)*conj(u(n-m)); end end r_u_hat(ind)=s/24; end % [r_u_true, lags] = xcorr(u,'coeff') ; %true autocorrelation % note that the first 23 elemets of r_u_true % are for lags equal to -23 to -1 and the % last 24 elemets correspond to lags of 0 to % 24 samples, so we are interested in the % last 24 elemets only % could also use 'autocorr'

% r_u_true= r_u_true(24:end) [r_u_true,Lags,Bounds] = autocorr(u,23)% true autocorrelation figure plot(r_u_hat,'k-*') hold on plot(r_u_true,'b-o') legend('estimated autocorrelation','true atocorrelation') title('Not normalized estimated and Normalized true autocorrelations') ylabel('autocorrelation') xlabel('sample number')

%% part c [Su_estimated,w]=freqz(r_u_hat); figure plot(abs(Su_estimated)) title('estimated PSD(Fourier Transform of estimated autocorrelation)') ylabel('DTFT Coefficient') xlabel('frequency index')

%% part d ar_coeffs = aryule(r_u_hat,2) % in command window I got % ar_coeffs = % 1.0000 0.0657 a1=ar_coeffs(2); a2=ar_coeffs(3); b0=1;

0.7099

%% part e and f [Su,w]=freqz(r_u_true); % true PSD figure plot(w,abs(Su)) hold on S_u_hat=freqz([b0],[1 a1 a2],w) plot(w,abs(S_u_hat),'r') legend('True PSD','estimated PSD') title('estimated and true PSDs') ylabel('PSD') xlabel('frequency index')

%% Using normalized estimated autocorrelation r_u_hat_normalized=r_u_hat/r_u_hat(1); figure plot(r_u_hat_normalized,'k-*') hold on plot(r_u_true,'b-o') legend('estimated autocorrelation','true atocorrelation') title('Normalized estimated and Normalized true autocorrelations') ylabel('autocorrelation') xlabel('sample number')

% part d ar_coeffs_n = aryule(r_u_hat_normalized,2) % in command window I got % ar_coeffs = % 1.0000 0.0657 0.7099 a1_n=ar_coeffs_n(2); a2_n=ar_coeffs_n(3); b0=1;

% part e and f figure plot(w,abs(Su)) hold on S_u_hat_n=freqz([b0],[1 a1_n a2_n],w) plot(w,abs(S_u_hat_n),'r') legend('True PSD','estimated PSD') title('estimated and true PSDs, using normalized autocorrelations') ylabel('PSD') xlabel('frequency index')

Вам также может понравиться