I want to know the difference between these following commands (matlab) and why I get two different outputs?


fft(signal, nfft);

I found that the output arrays are not the same, but I don't know why! how it works with the signal of any length without padding the original signal with zeros!

  • signal length is't a power of 2
  • nfft is the next power of 2
  • $\begingroup$ Nice question. There at least two reasons I can think of. Firstly, FFT routines are highly optimized for sequences that are power of 2. There are many other "optimal lengths" such as primes, etc, but this is generally the case. On the other hand, fft can still be calculated for sequence lengths that are not powers of two, but then calculations are not as fast, as well as truncation errors can manifest themselves. $\endgroup$
    – jojek
    Apr 30, 2016 at 21:16
  • $\begingroup$ Is this Matlab code? please be more clear on what exact difference you observed ? And, if your second line means taking an N-point FFT with N = "nfft" in your code, and if the signal has a different length than "nfft", then how do you compare the two FFTs of different lengths ? $\endgroup$
    – Fat32
    Apr 30, 2016 at 21:26
  • $\begingroup$ A matlab code, and nFFT is the next power of 2 from the length of the original signal, for example if the original length is 60, then nFFT=64. $\endgroup$ Apr 30, 2016 at 21:32
  • $\begingroup$ @KamalMoussa ok see my answer please. $\endgroup$
    – Fat32
    Apr 30, 2016 at 21:37

1 Answer 1


Assuming a signal x[n] of length L, The Matlab function fft(x) computes an L-point DFT of the signal, whereas fft(x,N) computes an N point DFT of x[n]. For certain reasons N must be greater or equal to L, otherwise a wrong (aliased) DFT result would be computed, so that one cannot reconstruct x[n] back from such a DFT via inverse DFT.

The two calls would result in two DFTs which are of different lengths and which ,in general, take different values. This is because they are in fact L and N samples of DTFT $X(e^{j\omega})$ of the signal x[n].

  • $\begingroup$ I agree, but what I need is to know what actually happened to the signal samples when I change to the number of points to be computed to N. will the signal be padded by zeros from (Lth point to Nth point) or just the signal will be sampled again by N ? $\endgroup$ Apr 30, 2016 at 21:50
  • $\begingroup$ @KamalMoussa Matlab pads zeros to the end of x[n] if fft(x,N) is called with N larger than signal length. You can check this by observing that fft ( [ x zeros(1,N-L)] ) produces the same output as " fft(x, N)" does $\endgroup$
    – Fat32
    Apr 30, 2016 at 21:56
  • $\begingroup$ Good, that's what I'm doing to implement FFT algorithm which works only when N is a power of 2. So, if the signal length isn't a power of 2, the algorithm will pad the remain points by zeros to the next power of 2. But I want to return the original length of signal ( L in our example), can I do it? Is this the actual DFT of the original signal? Sorry if my questions seems stupid! $\endgroup$ Apr 30, 2016 at 22:03
  • $\begingroup$ Nice. In my second paragraph I describe why fft(x) and fft(x,N) will "be" different due to they are getting different samples from DTFT $X(e^{j\omega})$ . Now if you want to return L-point (L:signal length) DFT for an arbitrary L, you should either use more complex radix-M decompostions or you may "resample" N point FFT into L point... but I'm not sure if it works $\endgroup$
    – Fat32
    Apr 30, 2016 at 22:19
  • $\begingroup$ One final note: as you know FFT is for computational performance. And therefore in almost every practical applications length N is taken to be a power of 2 for the fastest and simplest implementations without a loss of generality. However if for some reasons, mainly academic, must you compute irregular length of FFTs then you must accept some little loss of performance which is not the point there... $\endgroup$
    – Fat32
    Apr 30, 2016 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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