There's really nothing wrong with the code; you have followed the example from the documentation for FFT and the following lines
P_amp2=abs(Y/L);
P_amp(:,jj) = P_amp2(1:L/2+1);
P_amp(2:end-1,jj) = 2*P_amp(2:end-1,jj);
f=Fs*(0:(L/2))/L;
return the positive frequency half of the estimated PSD and normalized it to match total power in the double-sided PSD (P_amp2) by the factor of 2 applied to all frequencies except DC and Fmax which are not replicated in the two-sided spectrum.
The frequency vector f is for the one-sided PSD estimate.
If you really want the two-sided PSD, then don't take the positive half elements in P_amp; just use P_amp2 and the corresponding double-sided frequency vector. At that point, for convenience since apparently wouldn't need them, could rename P_amp2 to P_amp and fs to f.
BTW, the FFT and other MATLAB routines are vectorized, you can compute all the results using the original array and dispense with the for loop...
nn_ss=data(1:1800,1:88);
Ts = mean(diff(time));
Fs=500;
Fn=Fs/2;
L=length(time);
Y=fft(nn_ss);
fts=Y/L;
P_amp=abs(Y/L);
phase=angle(Y).*180/pi;
ly=length(Y);
fs=(-ly/2:ly/2-1)/ly*Fs;
The result will be NxM PSD (2-sided) by column same as if had used for...end loop
Best Answer