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
.
It is useful for visualizing a Fourier transform with the zero-frequency component in the middle of the spectrum. For vectors, fftshift(X) swaps the left and right halves of X . For matrices, fftshift(X) swaps quadrants one and three of X with quadrants two and four.
fft computes the discrete Fourier transform and by definition the output is complex. fftshift doesn't compute anything except swaping the position of the samples, so if your input is real, you get real output.
fft. fftshift. Shift the zero-frequency component to the center of the spectrum.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With