Following is the code given in MATLAB's site to estimate PSD using FFT.
Fs = 1000;
t = 0:1/Fs:1-1/Fs;
x = cos(2*pi*100*t) + randn(size(t));
N = length(x);
xdft = fft(x);
xdft = xdft(1:N/2+1);
psdx = (1/(Fs*N)) * abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
freq = 0:Fs/length(x):Fs/2;
plot(freq,10*log10(psdx))
grid on
title('Periodogram Using FFT')
xlabel('Frequency (Hz)')
ylabel('Power/Frequency (dB/Hz)'
I know I might lack a proper understanding of the fundamentals, but can anyone explain in order to extract the first half of
xdft
why indicesdoes the index run from $1$ to $N/2 + 1$ and not $1$ to $N/2$? (I suppose the Nyquist frequency lies at $i = N/2$, am I right?)Some sources mention that the square of the magnitude should be scaled by $\frac{1}{N}$, while here it is scaled by $\frac{1}{Fs*N}$. I am unable to figure out which of these two should be used.
I understand that the DC value should be left untouched when converting to single-sided spectrum. But why is the scaling by $2$ performed only till
end-1
and not tillend
?