# Question based on scaling the FFT for obtaining PSD

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)'

1. 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 does 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?)

2. 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.

3. 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 till end?

The exact location (index) of the Nyquist frequency in your frequency vector depends on the length of the input signal. If the length is odd the Nyquist frequency is not included; if the length is even it is included. Consequently, and assuming the signal is real, either the first $$\frac{N}{2}$$ or $$\frac{N}{2} + 1$$ samples are needed to fully describe your signal in the frequency domain. Note that, like the first sample describing the DC value, the sample describing the Nyquist value only occurs once; it should therefore not be multiplied by two when calculating the PSD of a real signal.
• Does that mean if the signal length is ODD (which means NF is not included), I can scale everything till end? or will it be again till end-1 as in even case? – MaxFrost Oct 21 '18 at 10:46