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 k, but in the MATLAB documentation of fftshift
it's only written that this command
rearranges the outputs of
fft
,fft2
, andfftn
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?
If you read onwards in the documentation on
fftshift
: "It is useful for visualizing a Fourier transform with the zero-frequency component in the middle of the spectrum."The shift is not necessary to perform the fft, but it is handy to visualise the Fourier transform. Whether to use
fftshift
or not is thus dependent upon whether you want to visualise your transform or not.The matlab
fft
only computes half of the frequency spectrum (positive frequencies and the null 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:0 1 2 3
... (freq bins > 0) ...Fs/2 -Fs/2
... (freq bins < 0) ...-3 -2 -1
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.So, to answer your question, no you don't need to use
fftshift
after of before a call tofft
orifft
. But if you used afftshift
on your vector, you should undo it by applying anifftshift
orfftshift
(I think both calls are equivalent).The simple answer is call to
fftshift
not needed before a call to fft.fftshift
does not influence the calculation of Fast fourier transform. fftshift rearranges values inside a matrix. For eg:Note that
ifftshift
is different thanfftshift
because it shifts the negative back to the positive. Assume a simple 3 bin unit impulse in frequency domain beforefftshift
fftshift
givesyou can see the discontinuity in phase. If you perform
ifftshift
, negative freq is shifted back to positive:whereas,
fftshift
again gives:one can see the phase monotonicity is not the same, (aka,
fftshift
-ifftshift
case gives [decrease, increase] andfftshift
-fftshift
case gives [increase, decrease] in phase).