any one help me that i find the psd of the random signal having lenght 1000,i write the code but get some error in the result not find correct result

close all;clear all;clc;
n=1000;
m=50;
x=0;
%%%%%%%%random signal
sig=[rand(1,n)-(1/2)];
sig1=zeros(1,m+1);
for i=1:m+1
for j=1:(n-i)+1
x=x+(sig(j).*sig(j+i-1) );
end
x=x/(n-m);
sig1(i)=x;
end
out=autocorr(sig,m+1);%using built in function of autocorrelation
figure(1);
subplot(2,1,1);
plot(sig1)
subplot(2,1,2);
plot(out);
%%%power-s-density-code%%%
f=50;
z=0;
psd1=zeros(1,m+1);
for q=1:m+1
for w=1:m+1
z=z+(sig1(w).*exp((-i*2*pi*q*w)/((2*m)+1)));
end
z=z/(n-m);
psd1(q)=z;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%find psd by fft shift
final=fftshift(abs(out));
%%%%%%%%%%%%%%%%%%
figure(2);
subplot(2,1,1);
plot(psd1)
subplot(2,1,2);
plot(final)

1 Comment

It would be really helpful if you could use the "code" button to format your MATLAB code into a more readable format.

Sign in to comment.

Answers (1)

Hi, I agree with cyclist's comment. I see that you are using autocorr() from the Econometrics Toolbox.
Do you have the Signal Processing Toolbox? You can use spectrum.periodogram to obtain the PSD. I assume in these examples that the sampling rate is 1. Change it accordingly.
fs = 1;
x = randn(1e3,1);
psdest = psd(spectrum.periodogram,x,'NFFT',length(x),'Fs',fs);
plot(psdest);
If you do not have the Signal Processing Toolbox, you can use fft()
figure;
xdft = fft(x);
xdft = (fs/length(x)).*abs(xdft(1:length(x)/2+1)).^2;
xdft(2:end-1) = 2*xdft(2:end-1);
F = 0:fs/length(x):1/2;
plot(F,10*log10(xdft)); grid on;
Wayne

Tags

Asked:

on 25 Sep 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!