Do I need to call fftshift before calling fft or ifft?

user4861528 picture user4861528 · Aug 23, 2015 · Viewed 15.3k times · Source

In the book "Computational Fourier Optics, A Matlab Tutorial" by David Voelz, it is written that a call to fftshift is needed before a call to fft or ifft, but in the MATLAB documentation of fftshift it's only written that this command

rearranges the outputs of fft, fft2, and fftn by moving the zero-frequency component to the center of the array.

There is no mention in documentation that this command should be called before the call to fft, and I saw some examples that call fft without a call to fftshift beforehand.

My question is: Whether or not fftshift needed to be called before a call to fft or ifft?

If fftshift doesn't need to be called before a call to fft, and only after a call to fft, then when should we use (if at all) the command ifftshift with relation to a calculation of the fft of a data set?

Answer

Tikoloche picture Tikoloche · Aug 24, 2015

The matlab fft only computes half of the frequency spectrum (positive frequencies and the zero frequency if your number of samples is odd) in order to save computation time. Then, the second half of the frequency spectrum (which is the complex conjugate of the first half) is just added at the end of this vector.

So what you get after a fft is the following vector:

 0 1 2 3 ... Fs/2   -Fs/2 ... -3 -2 -1
<----------------> <------------------>
  positive freq        negative freq

where Fs is the frequency sample.

Now, what fftshift does is just shifting the negative frequency bins (2nd part of the frequency spectrum) at the beginning of your vector so that you can display a nice frequency spectrum starting at -Fs/2 and ending at +Fs/2. The shifted vector becomes:

 -Fs/2 ... -3 -2 -1   0 1 2 3 ... Fs/2
<------------------> <---------------->
    negative freq      positive freq

So, to answer your question, no you don't need to use fftshift after or before a call to fft or ifft. But if you used a fftshift on your vector, you should undo it by applying an ifftshift or fftshift. (I think both calls are equivalent.)