DSPRelated.com
Forums

MATLAB code for Power Spectral Density

Started by ngeva0 December 9, 2005
>ngeva0 wrote: > >> ... Can you explain what does the "2 volts >> peak to peak" mean? I don't get that. Thank you. > >Consider the function y = A*sin(wt). the positive peak value is +A and >is easy to see on an oscilloscope. The value for y = zero is hard to >estimate accurately, but the negative peak, -A, is easy to see. We >measure the waveform as having a peak-to-peak value of 2*A and often >describe it that way. For any function, peak to peak is the entire range
>that it needs. > >Jerry >-- >Engineering is the art of making what you want from things you can get. >����������������������������������������������������������������������� >
Thank you.
Hi,

If you're looking to learn how to compute accurate spectral estimates, a 
particular favorite text around here is Stoica  & Moses (Intro to Spectral 
Analysis).

If you want to use preprogrammed software for this, I encourage you to use 
two methods in the Signal Processing Toolbox (if you have it).  Type "help 
spectrum/psd" and "help spectrum/msspectrum" to learn how to compute PSD and 
mean-square spectrum estimates.  These are two well-defined estimates that 
are useful in practice.  Quick examples taken from the methods cited above 
to get you started:

PSD EXAMPLE (if you're looking for a spectral density on, say, a noisy comms 
signal or a general continuous spectral distribution):

     %  Spectral analysis of a signal that contains a 200Hz cosine plus 
noise.
       Fs = 1000;   t = 0:1/Fs:.296;
       x = cos(2*pi*t*200)+randn(size(t));
       h = spectrum.welch;                  % Instantiate a welch object.
       psd(h,x,'Fs',Fs);                    % Plot the one-sided PSD.

MSS EXAMPLE (if you have distinct sines, e.g. discrete spectra, and want 
amplitude levels - sounds like your example):

       % In this example we will measure the power of a deterministic
       % power signal which has a frequency component at 200Hz. We'll use a
       % signal with a peak amplitude of 3 volts therefore, the theoretical
       % power at 200Hz should be 3^2/2 volts^2 (watts) or 6.53dB.
       Fs = 1000;   t = 0:1/Fs:.2;
       x = 3*cos(2*pi*t*200);
       h = spectrum.welch;           % Instantiate a welch object.

       % Plot the one-sided Mean-square spectrum.
       h.FFTLength = 'UserDefined';
       msspectrum(h,x,'Fs',Fs,'NFFT',2^14);

Regards,
--Don


"ngeva0" <ngeva0@hotmail.com> wrote in message 
news:ne-dnR4SaOiNswfenZ2dnUVZ_tqdnZ2d@giganews.com...
> Can anyone please check if my code is correct? SINE512 is just a file > name. > This file has 512 data points and it's a sine wave. Thank you!! > > %Sampling frequency > Fs=50000; > > %# of samples in the data > datasize=size(SINE512); > numsample=datasize(1); > > %Windowing > H=hann(numsample); > W=H.*(SINE512(:,2)); > > %Fourier Transform > FFTX=fft(W,numsample); > > %Power: magnitude^2 > X=FFTX(1:floor(numsample/2)).*conj(FFTX(1:floor(numsample/2))); > > %Bandwidth > BW=1.5*Fs*numsample; > > %PSD=magnitude^2/bandwidth > PSD=X/BW; > > %Computing the corresponding frequency values > Omega=Fs*(0:numsample-1)/numsample; > Omega=Omega(1:floor(numsample/2)); > > %Plot PSD > H=plot(Omega,PSD); > set(H,'Color','BLACK'); >