Friday, December 4, 2009

Yule Walker Method Versus Welch Method

In this section I compare ARYule with Pwelch for a short data set. The emphasis is on the superior performance of the Yule Method for the short data set.

The figure shows a comparison between the YULE method of PSD Estimation and Welches method for the same amount of data. The code is shown below. Comments please. I can write a lot about this but it is self explanatory. DO you think?



ARYule Figure:
b=1;a=[1 -0.9 .5];
[H,w]=freqz(b,a);

for i=1:1
N=2^7;n=0:N-1;w=randn(size(n));

x=filter(b,a,w);

%ARYuleRGH1;
A=aryule(x,2);

[H1,w]=freqz(b0,A);
subplot(1,2,1)
plot(w,10*log10(abs(H)))
hold on
plot(w,10*log10(abs(H1)),'r')
subplot(1,2,2)
th=0:.05:2*pi;plot(cos(th),sin(th));hold on;
plot(roots(a),'x')
plot(roots(A),'xr')
drawnow
end

subplot(1,2,1);plot(w,10*log10(abs(H)),'LineWidth',3)
title('Yule-Walker Method: N=2^7,M=2^6, no overlap')
hold off
subplot(1,2,2);plot(roots(a),'x','LineWidth',3)
title('POLE plot')
hold off




PWELCH Figure

b=1;a=[1 -0.9 .5];
[H,w]=freqz(b,a);
for i=1:1
N=2^7;n=0:N-1;v=randn(size(n));M=2^6;
x=filter(b,a,v);
[Px,F]=pwelch(x,rectwin(M),0,M,1);
plot(F,10*log10(Px/2),'r')
hold on
drawnow
end
plot(w/2/pi,10*log10(abs(H).^2),'LineWidth',3)
title('welch method: N=2^7,M=2^6, no overlap')
hold off

Sunday, November 29, 2009

Implementation of ARYule


The graphs show an expt testing  ARYule 100 times on a 2-pole system with 2^8 data points:

>> for i=1:100
AAaryule
drawnow
end
>>

% Aryule method as implemented in AA Book(ref)

N=2^7;n=0:N-1;g=randn(size(n));%3.5*(rand(size(n))-0.5);
%b=1;a=[1 -.9 .3];
b=1;a=conv([1 -.9 .8],[1 .3 .8]);
x=filter(b,a,g);

maxlag=15;
p=4;
[rx,lags]=xcorr(x,x,maxlag,'biased');
rx=rx(maxlag+1:maxlag+1+p);
R=toeplitz(rx);
R1=inv(R);
b2=1/R1(1,1);b0=sqrt(b2);            %find b(0) squared
a1=b2*R1(2:p+1,1);                      %fin AR coefficients a(1) - a(5)

% Obtain a coeffs using 'inner' matrix
RR=R(1:p,1:p);
a2=inv(RR)*-rx(2:p+1)';
[a1(1:p-1) a2(1:p-1)];
[H,w]=freqz(b,a);[H1,w]=freqz(b,[1 a1']);
figure(1)
plot(w,20*log10(abs(H)),w,20*log10(abs(H1)));hold on
axis([0 pi -20 20]);

figure(2)
th=0:.1:2*pi;plot(cos(th),sin(th));hold on
plot(roots([1 a1']),'xr')
%zplane(1,[1 a1']);hold on


Saturday, November 28, 2009

Negative Frequency

Search for discussion on negative frequency came up with this page
Negative Frequency

Tuesday, November 24, 2009

Signal Processing Toolbox - Measuring the Power of Deterministic Periodic Signals Demo

Signal Processing Toolbox 6.12

Measuring the Power of Deterministic Periodic Signals

Richard says:

'This page is an interesting and important in understanding the concept of power measurement / estimation of discrete time signals in the frequency domain.'



This demonstration will focus on power signals, specifically deterministic periodic signals. Although continuous in time, periodic deterministic signals produce discrete power spectrums. For this reason we will use mean-square (power) to measure the signal's power at a specific frequency.
We will provide two examples of how to measure a signal's average power. The examples will use sine waves and assume a load impedance of 1 Ohm.

http://www.mathworks.com/products/signal/demos.html?file=/products/demos/shipping/signal/deterministicsignalpower.html

Monday, November 23, 2009

Use of TFESTIMATE to plot the frequency response of a system from its I/O data


Input and output data with frequency response estimate using tfestimate the implementation of pwelch for cross power spectum. Zooming in on both is interesting:

The m-file:

N=2^16;n=0:N-1;x=randn(size(n));
b=[0 1];a=[1 -1.8 .9];
y=filter(b,a,x);
subplot(1,2,1)
plot(n,y,n,x)
title('Input and output data; N=2^1^6;')
legend('output, y','input, x')
[H,w]=freqz(b,a);
[Pxyh,F]=tfestimate(x,y,hamming(N),0,N,1);
[Pxyr,F]=tfestimate(x,y,rectwin(N),0,N,1);
subplot(1,2,2)
plot(F,abs(Pxyr),F,abs(Pxyh),w/2/pi,abs(H))
title('Frequency response and estimates using tfestimate')
legend('tfe:Rectwin','tfe:Hamming','Freq Resp of filter')

Monday, November 16, 2009

Non Parametric PSD continued. Correction from previous

The power spectrum now matches the frequency response of the filter.
plot(f,10*log10(Py/2),'g','LineWidth',3)

I divided Py by 2 before plotting.

Demonstration of Non-parametric Power Spectrum Estimation


The graph shows spectrum of data passed through a filter calculated using pwelch MATLAB command to evaluate the periodogram by welches method.


%Design nice filter
Wp = [.2 .7], Ws = [.1 .8];Rp=1,Rs=30;
[N,Wn]=ellipord(Wp,Ws,Rp,Rs);
[b,a]=ellip(N,Rp,Rs,Wn);
N=2^12;n=0:N-1;

% generate and filter data
%x=rand(size(n))*2-1;
x=randn(size(n));
y=filter(b,a,x);

%Periodogram
[Px,f] = pwelch(x,rectwin(N),0,N,1);
[Py,f] = pwelch(y,rectwin(N),0,N,1);
plot(f,10*log10(Px),f,10*log10(Py))
hold on
title('Periodogram rectangular window');xlabel('frequency cycles per sample');ylabel('P')

% Bartlett Rectangular window
[Px,f] = pwelch(x,rectwin(N/16),0,N/16,1);
[Py,f] = pwelch(y,rectwin(N/16),0,N/16,1);
plot(f,10*log10(Py),'g','LineWidth',3)

% Bartlett Hamming window
[Px,f] = pwelch(x,hamming(N/16),0,N/16,1);
[Py,f] = pwelch(y,hamming(N/16),0,N/16,1);
plot(f,10*log10(Py),'c','LineWidth',2)
legend('x:periodogram','y:periodogram','y:Bartlett,rect','y:Bartlett,hamming')
xlabel('frequency cycles per sample');ylabel('P')
[H,w]=freqz(b,a);plot(w/2/pi,20*log10(abs(H)),'r')
axis([0 .5 -80 20]);grid










Friday, November 6, 2009

Correlation using Humusoft

Finally got down to using the new analog boards in room 14. I am focussing on block oriented signal processing using the buffer and frame in/out blocks. They are working quite happily at 10kHz sampling and work well at 20 kHz. I have both i/p and o/p going but still have to work out the exact buffering organisation of the boards. I am readyb to get the cross correlation functions going again. I'll post the code tomorrow.

Monday, November 2, 2009

Cross correlation continued....

I have included a better description of the figure.
The xcorr function is used 'unbiased' as I am simply showing that the cross correlation rxw is not equal to rwx.


% m-file showing that rxw /= (not equal to) rwx
% rxw(m)=0 for m<0,>= 0;
% rwx(m)=0 for m>0, =h(-m) m<= 0;
% This is important when deriving the Yule-Walker equations for an AR
% process
N=2^16;n=0:N-1;w=randn(size(n));
b=[0 1];a=[1 -1.8 .9];
x=filter(b,a,w);
M=150;m=-M:M;rxw=xcorr(x,w,M);rwx=xcorr(w,x,M);
subplot(2,1,1);plot(m,rxw,'.');
title('Crosscorrelation rxw');xlabel('m');ylabel('rxw');grid
subplot(2,1,2);plot(m,rwx,'.');xlabel('m');ylabel('rwx');grid

Trying to do Yule walker from Proakis&Manolakis


% m-file showing that rxw /= rwx
% rxw(m)=0 for m<0,>= 0;
% rwx(m)=0 for m>0, =h(-m) m<= 0;
% This is important when deriving the Yule-Walker equations for an AR
% process
N=2^10;n=0:N-1;w=randn(size(n));
b=1;a=[1 -1 .8];
x=filter(b,a,w);
M=40;m=-M:M;rxw=xcorr(x,w,M);rwx=xcorr(w,x,M);
subplot(2,1,1);plot(m,rxw);subplot(2,1,2);plot(m,rwx)



Friday, October 30, 2009

MATLAB history on Proakis & Man Ch 14

%-- 30/10/09 11:35 --%
N=2^14;n=0:N-1;w=randn(size(n))*3+2;
mean(w)
var(w)
b=[1 -2 1];
x=filter(b,1,w;
x=filter(b,1,w);
mean(x)
var(x)
help xcorr
rxx=xcorr(w,4)
rxx=xcorr(w,4,'biased')
rxx=xcorr((w-mean(w)),4,'biased')
rxx=xcorr(x,4,'biased')
rxx=xcorr(x,8,'biased')
stem(rxx)
M=2^7;n=0:M-1;x=sin(2*pi*.1*n);
clear fax
X=fft(x,M*8);
N=length(X)
stem(fax,abs(X))
stem(fax,20*log10(abs(X)))
plot(fax,20*log10(abs(X)))
grid
ginput(2)
mea=ginput(2)
mea(2)-meaa(1)
mea(2)-mea(1)
M=100;n=0:M-1;x=sin(2*pi*.1*n);
X=fft(x,M*8);
N=length(X)
plot(fax,20*log10(abs(X)))
grid
mea=ginput(2)
mea(2,1)-mea(1,1)
M=90;n=0:M-1;x=sin(2*pi*.1*n);
X=fft(x,M*8);
X=fft(x,M*16);
plot(fax,20*log10(abs(X)))
N=length(X)
plot(fax,20*log10(abs(X)))
grid
mea=ginput(2)
mea(2,1)-mea(1,1)
[H,w]=freqz(b,a,512,Fs);
semilogx(w,20*log10(abs(H)))
Fs=1000;
10*log10(3/Pn)
Q=2/2^6;Pn=Q^2/12
10*log10(3/Pn)