I am trying to calculate the FFT of a pressure signal with the correct amplitude, as I want to calculate the resulting sound pressure level from the FFT.
Basically, I can calculate the FFT amplitude correctly when I don't use a window, but I want to use a Hanning window and I do not know how to normalize for the window power. In the code below, from this post https://www.mathworks.com/matlabcentral/answers/33653-psd-estimation-fft-vs-welch, the FFT is normalized by window power by taking FFT/(window'*window). However, the FFT is never normalized by dividing by the length Nx of the signal. But still, the correct PSD is found. From what I can understand, the result from pwelch should also be scaled before it can be used to obtain the correct sound pressure levels.
Fs = 1024;
t = (0:1/Fs:1-1/Fs).';
x = sin(2*pi*t*200);
Nx = length(x);
w = hanning(Nx);
xw = x.*w;
nfft = Nx;
X = fft(xw,nfft);
mx = abs(X).^2;
mx = mx/(w'*w);
NumUniquePts = nfft/2+1;
mx = mx(1:NumUniquePts);
mx(2:end-1) = mx(2:end-1)*2;
Pxx1 = mx/Fs;
Fx1 = (0:NumUniquePts-1)*Fs/nfft;
[Pxx2,Fx2] = pwelch(x,w,0,nfft,Fs);
plot(Fx1,10*log10(Pxx1),Fx2,10*log10(Pxx2),'r:');
legend('PSD via FFT','PSD via pwelch')
If someone can please clarify exactly how to calculate a windowed FFT of which the amplitude is correctly scaled for signal length and window power, it would be greatly appreciated. Matlab's documentation is not very clear on the subject.