4
$\begingroup$

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. What happens if I choose the length of signal L > NFFT? and what's about choosing L different form NFFT?

  2. In the following code, 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 (I mean when I change L and NFFT,) then the values of amplitudes were different too. How can I get the exact value of amplitude of input signal?

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.
$\endgroup$

3 Answers 3

7
$\begingroup$
  1. What happens if I choose the length of signal L > NFFT? and what's about choosing L different form NFFT?

Did you read the documentation? http://www.mathworks.com/help/techdoc/ref/fft.html

Y = fft(X,n) returns the n-point DFT. fft(X) is equivalent to fft(X, n) where n is the size of X in the first nonsingleton dimension. If the length of X is less than n, X is padded with trailing zeros to length n. If the length of X is greater than n, the sequence X is truncated. When X is a matrix, the length of the columns are adjusted in the same manner.

So it just ignores the signal after n samples.

2 In the following code, I'm not sure if I used window correctly.

Yep. You just multiply the window by the signal:

enter image description here

But when I use window (hanning in the following code), I can't get the exact values of amplitudes?

Yes, It attenuates anything at the ends of the window, so the amplitude varies depending on when the window hits the signal. If there's a huge peak in the signal, but it happens at the end of the window, it gets multiplied by 0. If it happens in the middle of the window, it gets multiplied by 1.

For signals that are stationary across the window, then it doesn't matter when the window is applied to the signal, right? So you can just multiply by a constant (2 for a Hann window, I think, because the area under the window is 0.5) to get the true amplitude.

Also, if you use overlapped windows and certain windows (constant-overlap-add (COLA) constraint), I believe you can combine them to get the true amplitude for any waveform, even non-stationary ones. https://ccrma.stanford.edu/~jos/sasp/Overlap_Add_Decomposition.html

3 When L and NFFT get different values (I mean when I change L and NFFT,) then the values of amplitudes were different too. How can I get the exact value of amplitude of input signal?

The signal is stationary, so it shouldn't really matter how much of it you measure. To get the true amplitude you probably need to divide by N, depending on matlab's implementation. https://gist.github.com/236567

$\endgroup$
1
  • $\begingroup$ The DFT actually calculates the energy spectrum. To get the amplitudes in Matlab scale the result of fft() by 2/NFFT and plot the magnitude. $\endgroup$
    – Deve
    Sep 3, 2012 at 14:22
6
$\begingroup$

When L>NFFT, the signal will be cropped before FFT; when NFFT>L, the signal will be zero padded before FFT.

In your case, the window is used to surpress noise, and it will changes the spectrum of the signal so you cannot get the 'exact' amplitude spectrum. Actually, exact spectra can only be computed from infinte samples. Since the signal always has finite length, you can never get the exact spectrum. It's called spectral leakage.

$\endgroup$
4
$\begingroup$

How can I get the exact value of amplitude of input signal?

If your signal is stationary and perfectly periodic, then you can get the amplitude by using a data and FFT length that are exactly an integer multiple of the signal's period, and using no window function.

A non-rectangular window function and an FFT length longer than the data length are useful if you don't know the period or S/N ratio, and want only an estimate of the spectra rather than any "exact" values. An FFT length longer than the data length also helps interpolate a "smoother looking" spectrum from a given number of data samples, potential providing a higher resolution frequency of isolated spectral peaks well above the noise floor.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.