I am trying to implement an algorithm in python, but I am not sure when I should use fftshift(fft(fftshift(x)))
and when only fft(x)
(from numpy). Is there a rule of thumb based on the shape of input data?
I am using fftshift
instead of ifftshift
due to the even number of values in the vector x
.

- 3,942
- 5
- 45
- 81
-
4Don't use `fftshift` instead of `ifftshift` because you have an even number of values. Use the right function in the right location. This prevents misunderstands and errors later on. The correct syntax is `fftshift(fft(ifftshift(x)))`. – Cris Luengo Apr 17 '18 at 15:35
-
See [this answer](https://stackoverflow.com/a/76376555/774575) for how re-ordering functions `fftshift` and `ifftshift` work, and when to use one or the other. – mins May 31 '23 at 19:05
2 Answers
It really just depends on what you want. The DFT (and hence the FFT) is periodic in the frequency domain with period equal to 2pi.
The fft()
function will return the approximation of the DFT with omega (radians/s) from 0
to pi
(i.e. 0
to fs
, where fs
is the sampling frequency). All fftshift()
does is swap the output vector of the fft()
right down the middle. So the output of fftshift(fft())
is now from -pi/2
to pi/2
.
Usually, people like to plot a good approximation of the DTFT (or maybe even the CTFT) using the FFT, so they zero-pad the input with a huge amount of zeros (the function fft()
does this on it's own) and then they use the fftshift()
function to plot between -pi
and pi
.
In other words, use fftshift(fft())
for plotting, and fft()
for the math!

- 1,768
- 2
- 15
- 22
-
-
-
In the question itself. "I am trying to implement an algorithm in python, but I am not sure when I should use `fftshift(fft(fftshift(x)))`" there is a fftshift in the first and there is fftshift just before doing fft. Why do we have to do fftshift before doing fft? – Creator Jan 20 '21 at 20:03
-
-
Could anyone please clarify the effect of using two fftshift() as in the question? Or fftshift(fft(ifftshift(x))) as suggested by @Cris Luengo in comment on the question, as this answer just explains the effect of one fftshift(). – psj Feb 03 '21 at 06:40
fft(fftshift(x)) rotates the input vector so the the phase of the complex FFT result is relative to the center of the original data window. If the input waveform is not exactly integer periodic in the FFT width, phase relative to the center of the original window of data may make more sense than the phase relative to some averaging between the discontinuous beginning and end. fft(fftshift(x)) also has the property that the imaginary component of a result will always be positive for a positive zero crossing at the center of the window of any antisymmetric waveform component.
fftshift(fft(y)) rotates the FFT results so that the DC bin is in the center of the result, halfway between -Fs/2 and Fs/2, which is a common spectrum display format.

- 70,107
- 14
- 90
- 153
-
5This is only true for even-sized `x`. Use `ifftshift` to shift the origin form the center to the left-most bin in preparation for the FFT algorithm: `fft(ifftshift(x))`. – Cris Luengo Apr 17 '18 at 15:33