MATLAB: Could you please let me know how to write code to draw the power spectral density (PSD) vs frequency for the data? Thanks.

fftMATLAB and Simulink Student Suite

Time(s) Amplitude 0.707 0.707 1.401 0.694 2.104 0.703 2.803 0.699 3.497 0.694 4.183 0.686 4.869 0.686 5.575 0.706 6.288 0.713 7.007 0.719 7.711 0.704 8.401 0.69 9.098 0.697 9.8 0.702 10.505 0.705 11.192 0.687 11.876 0.684

Best Answer

Hi Ray,
Your data are:
amplitude=[0.707 0.694 0.703 0.699 0.694 0.686 0.686 0.706 0.713 0.719 0.704 0.69 0.697 0.702 0.705 0.687 0.684];
t=[0.707 1.401 2.104 2.803 3.497 4.183 4.869 5.575 6.288 7.007 7.711 8.401 9.098 9.8 10.505 11.192 11.876];
The vector t values are non-uniformly spaced in time. I see two options how to calculate PSD:
1) You can use the Matlab function 'interp1' to interpolate data on uniform time grid and then apply the function 'fft' or any other Matlab function that calculates PSD. Code example:
T_mean=(t(length(t))-t(1))/(length(t)-1); %Calculate mean sampling period
amplitude_i=interp1(t,amplitude,t(1):T_mean:t(length(t))); %Interpolate data on uniform time grid
Psd_i=abs(fft(amplitude_i)).^2/(length(t)*T_mean); %Calculate PSD of interpolated data
semilogy(Psd_i); %Plot PSD on log scale as PSD has a large peak on zero frequency
2) Another option is to use the function 'nedft' downloadable on fileexchange:
https://se.mathworks.com/matlabcentral/fileexchange/11020-extended-dft
This function allows you to calculate the PSD directly from the non-uniform data (amplitude and t vectors) without interpolating them to uniform grid. Let me known if you are interested in applying it.