5
votes

I want to ask some questions related to the last question of mine so I don't want to post in another thread. My question contains a code, I therefore can't post it as a comment. So I have to edit my old question into a new one. Please take a look and help. Thank you.

I'm new to FFT and DSP and I want to ask you some questions about calculating FFT in Matlab. The following code is from Matlab help, I just removed the noise.

  1. Can I choose the length of signal L different from NFFT?

  2. I'm not sure if I used window correctly. But when I use window (hanning in the following code), I can't get the exact values of amplitudes?

  3. When L and NFFT get different values, then the values of amplitudes were different too. How can I get the exact value of amplitude of input signal? (in the following code, I used a already known signal to check if the code work correctly. But in case, I got the signal from a sensor and I dont know ahead its amplitude, how can I check?)

I thank you very much and look forward to hearing from you :)

Fs = 1000;                    % Sampling frequency
T = 1/Fs;                     % Sample time
L = 512;                     % Length of signal
NFFT=1024;                   % number of fft points
t = (0:L-1)*T;                % Time vector
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);    input signal
X = fft(hann(L).*x', NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(X(1:NFFT/2+1)))     % Plot single-sided amplitude spectrum.
2

2 Answers

2
votes

L is the number of samples in your input signal. If L < NFFT then the difference is zero-padded.

I would recommend you do some reading on the effect of zero-padding on FFTs. Typically it is best to use L = NFFT as this will give you the best representation of your data.

An excepted answer on the use of zero-padding and FFTs is given here: https://dsp.stackexchange.com/questions/741/why-should-i-zero-pad-a-signal-before-taking-the-fourier-transform

In your experiment you are seeing different amplitudes because you will have different amount of spectral leakage with each different L.

1
votes

You need to apply a window function prior to the FFT to get consistent results with frequency components that have non-integral number of periods within your sampling window.

You might also want to consider using periodogram instead of using the FFT directly - it takes care of window functions and a lot of the other housekeeping for you.