DSPRelated.com
Forums

Wrong results in periodogram code in Matlab

Started by wobbler28 May 10, 2015
Hi, I am learning about spectral analysis, now I want to solve a
periodogram problem in Matlab but I'm getting wrong results, don't know
why.

I'm working with an AR random process:

x(n)=e(n)+(summation from k=1 to N of) (α_k* x(n−k))

e(n) is a white noise signal  and α_k are five given coefficients

I plotted the power spectrum in Matlab for ω from 0 to π.

Then I generated the realizations of x(n) with 10000 samples

Now I'm trying to get the estimate of the spectrum from the generated data
using the periodogram formula

Sxx=(1/K)*∣(summation from k=0 to K-1 of) (x[k]*e^(−jωk))∣^2


with K=256 the number of first values of x(n). When I run my code what I
get is a vector whose element are all the same constant value, so I'm
getting a straight line in my plots. Not sure what is going wrong.

I would appreciate if you can give a look to my code and advise on what I
am doing wrong. This is the section of the code I'm using to find the
estimates

[CODE]

var_e=0.01;                                         % noise variance
b_1=1.4261;                                        % coefficients
b_2=-.7634;
b_3=-.9002;
b_4=1.2548;
b_5=-.5707;    
K=256;
wn=rand(1,10000)-0.5;                      % white noise
samp=1:1:1e4;
T=10000;
x=zeros(1,length(samp));
                                                              % here I
generate the expectations

for n=6:T+5
    x(n)=b_1*x(n-1)+b_2*x(n-2)+b_3*x(n-3)+b_4*x(n-4)+b_5*x(n-5)+wn(n-5);
end
x=x(6:end);
figure
plot(samp,x)

% here I'm looking for the periodogram. It's the section giving wrong
results

    for i=1:length(w)                                % w is the frequency,
has 1000 
        for k=1:K
        per2(i)=sum(x(k)*exp(-j*k*i));      % sum of exponentials
        pe2(i)=per2(i)*conj(per2(i));         % squared
        end
    end


[CODE]

Thanks in advance for your help,
Alif Hamnan


---------------------------------------
Posted through http://www.DSPRelated.com
   

The following line has at least 2 defects

 per2(i)=sum(x(k)*exp(-j*k*i));     

Hints 

Is i the same as w?
How many items are being summed inside your sum statement ?


Bob
>The following line has at least 2 defects > > per2(i)=sum(x(k)*exp(-j*k*i)); > >Hints > >Is i the same as w? >How many items are being summed inside your sum statement ? > > >Bob
Hi, thanks for your answer. I get the first hint, should use w(i) instead of i. I changed to per2(i)=sum(x(k)*exp(-j*k*w(i))); But continue getting the same error: all the results are equal. I don't really get the second hint, I feel like just rewriting the summation formula. As I understand it, if I have my frequency w as a vector from zero to pi with 1024 elements and want to estimate the original power spectrum taking K=256 samples from the vector x (which has 10000 elements), aftr applying the periodogram formula I should get a vector with 1024 elements, so I can compare between the original and the estimated spectra.Am I ok? or I should get a vector with K-1 elements instead? How can I then make a comparison between the calculated an the estimated? Regards, Alif --------------------------------------- Posted through http://www.DSPRelated.com
I got it!

Thanks very much for your help Bob, the loop:


for k=0:K-1
    per1=x(k+1)*exp(-1j*k.*w)+per1;
end
pdg1=((abs(per1)).^2)./K;
plot(w,pdg1)



Best Regards,
Alif
---------------------------------------
Posted through http://www.DSPRelated.com