Date: Jan 13, 2010 12:37 AM
Author: Greg Heath
Subject: Re: MATLABs FFT and fftshifting the input
On Jan 11, 3:20 pm, robert bristow-johnson <r...@audioimagination.com>
> On Jan 11, 2:11 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> > On Jan 11, 10:25 am, "orthocto" <ortho...@gmail.com> wrote:
> > > I've tried looking around for an answer to these questions
> > > ... and there are a few threads. Yet, I am still confused.
> > > My questions of this thread are:
> > > A. Why should one do fft( fftshift(x) )
> > I do not recommend it ... it leads to confusion.
> > To avoid confusion only use fftshift on the spectrum.
> another perpective.
> > If you have to shift in the time domain, use ifftshift.
> i didn't even know that existed. what does ifftshift() do
> that is different that fftshift()?
According to the MATLAB documentation:
Y = fftshift(X) rearranges the outputs of fft, fft2, and
fftn by moving the zero-frequency component to the center
of the array. 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.
ifftshift(X) undoes the results of fftshift
In particular, there is no mention of shifting in the
So, the MATLAB convention is
1. Define x(t) over [0,T-dt]
2. Obtain X(f) = fft(x) defined over [0,Fs-df]
3. Obtain Xb = fftshift(X) defined over
a. df*[ -(N-1)/2 : (N-1)/2 ] for N odd
b. df*[ -N/2 : N/2 - 1 ] for N even
4. If X is no longer available, recover X = ifftshift(Xb)
5. Obtain x = ifft(X)
Notice that both fftshift and ifftshift are used on the
spectrum. However,steps 4 and 5 can be replaced by
6. Obtain xb = ifft(Xb) defined over
a. dt*[ -(N-1)/2 : (N-1)/2 ] for N odd
b. dt*[ -N/2 : N/2 - 1 ] for N even
7. Recover x = ifftshift(xb)
Therefore, fftshift is only used on the spectrum
and the time domain shift is obtained using ifftshift.
Consequently, to lessen the confusion, my advice to the
OP, and newbies in general, is "Use ifftshift in the
time domain to convert a "zero centered" waveform to one
that starts at t = 0 and conforms to the fft assumption.
Because of all the symmetry properties available there
are many different ways to approach the problem (e.g.,
fft(xb)).However, for giving advice to newbies who,
like the OP, will be flumoxed by the MATLAB documentation,
I think the above approach is the least confusing.
So, when starting with xb instead of x, my advice is to
use step 7 first.
> > However, beware that the function is not shifted, it is
> > reflected about point ceil((N+1)/2) which then becomes
> > the first point corresponding to t = 0 when using fft.
> okay. why would i use that?
Because the fft assumes the first point is t = 0.
> > Define
> > dt = 1/Fs, T = N*dt, df = 1/T
> > fft assumes
> > 1. x is defined over the nonnegative time interval
> > t = dt*(0:N-1) ; = 0:dt:T-dt ; = linspace(0,T-dt,N);
> > 2. x is periodic with period T = N*dt outside that interval.
> > If X = fft(x), then ifft(X) will automatically have
> > that property.
> > 3. f is defined over the nonnegative frequency interval
> > f = df*(0:N-1) = 0:df:Fs-df = linespace(0,Fs-df,N);
> > 4. X = fft(x) is automatically periodic with period
> > Fs = N*df outside that interval.
> > If either t or f are not defined that way, shifting of x
> > using ifftshift or X using fftshift should be considered.
> > Typically, x is defined over nonnegative t as specified
> > above. Consequently X = fft(x) will be defined over
> > nonegative f.
> if you put 1 & 2 together, then you *can* understand a manner
> that x(t) is defined for -N/2*T <= t < N/2*T. the portion of
> x(t) that is 0 <= t < N/2*T goes where it usually does and the
> portion of x(t) that is -N/2*T <= t < 0 goes into the second
> half. so that suggest a use for fftshift()( or you can do it
> explicitly in MATLAB without fftshift() ).
You can. However, as explained above, I think it is less
confusing to the inexperienced user to use fftshift in the
fft (frequency) domain and ifftshift in the ifft (time) domain.
How to do otherwise is more easily understood by you and me.
However, my advice to a newbie, like the OP, is to stick as
close as possible to the MATLAB documentation which,
unfortunately, is sorely lacking.
> so, imagine you have two pulses in the time domain. doesn't matter
> what kind of pulse, could be a rectangular pulse, or a gaussian pulse,
> or a piece of Hann-windowed data. one pulse is centered around t=0
> and the other pulse is centered around t=N/2*T. as the OP noticed,
> the magnitude will be the same, but what will happen is that the
> delayed pulse, the phase response will have +/- pi added to every odd
> numbered sample in the frequency domain.
> but since MATLAB is stupid and even though for two decades, *someone*
> in the know had been telling them that they should extend the language
> to accommodate zero and negative indices, they have never done it. so
> if you sample a piece of time-domain data and you window it and put it
> into a MATLAB array, it will go into that array centered at N/2*T and
> when you FFT the array you will see that annoying phase response. but
> if you fftshift() the time domain data going in, the phase response
> will look like one might expect of a windowed pulse (centered at 0).
> that, as far as i can tell, is what fftshift() is for.
No, as the documentation states, fftshift is for shifting ffts.
It is less confusing to use ifftshift to shift iffts or original
I hope my reasoning is clear.