# MATLABs FFT and fftshifting the input

Started by January 11, 2010
```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) )
B. What assumptions does the MATLAB fft(.) routine have?
B.1. Does it expect the DC bin to be the first element of the input
signal?
B.2. What other assumptions... ?

Here's my understanding (or lack of)...

1. TAKING THE FFT
The following command gives me the spectrum with the DC bin as the first
element:
X = fft(x);

Now, the first half contains the positive frequencies [f=0,f=Fs/2]. The
second half contains the negative frequencies [f=-Fs/2, f=0).

2. FFTSHIFT THE OUTPUT
Now, if I take the fftshift - this is nothing more than a 'special'
circshift; it swaps the two halfs; its puts DC in the center. The vector
contains freq components that span [f=-Fs/2, f=Fs/2].

X_out_shft = fftshift( fft(x) );

I can understand why I might want to do this...

3. FFTSHIFT THE INPUT
I often see that the following is performed. But I do not understand,
*why* I would want to do this:

X_in_shft = fftshift( fft( fftshift(x) ) );

Upon inspection, I see that shifting the input does not modify the
magnitude of X_in_shft, but it does modify the phase. So, why is this
done?

4. ASSUMPTIONS
Finally, what does MATLAB's fft routine expect? Should I also be plugging
in an input vector with DC as the first element?

```
```orthocto wrote:
> I've tried looking around for an answer to these questions... and there are
> a few threads. Yet, I am still confused.

Matlab has a fine help system. Have you tried it?

Jerry
--
Engineering is the art of making what you want from things you can get.
&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;&#2013266095;
```
```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.
If you have to shift in the time domain, use ifftshift.
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.

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.

To obtain a representation of the spectrum over a
bipolar frequency interval use

fb  = f - df*ceil((N-1)/2);
= df*[ -ceil((N-1)/2) : floor((N-1)/2)];
= df*[ -(N-1)/2       : (N-1)/2 ];     %N odd
= df*[     -N/2       :  N/2-1  ];     %N even
Xb  = fftshift(fft(x));
x   = ifftshift(ifft(Xb));

However, if x is not defined over nonegative t, as
specified, but is even or odd about zero, then you
can use the ifftshift function to straighten things
out.

I don't do it and don't encourage it because the
point ceil((N+1)/2) has to be placed exactly at the
the point of symmetry. That tends to confuse the
inexperienced.

For example,

ifftshift([-3:3]) = [0 1 2 3 -3 -2 -1]
ifftshift([-4:3]) = [0 1 2 3 -4 -3 -2 -1]
However
ifftshift([-3:4]) = [1 2 3 -4 -3 -2 -1 0]  % INCORRECT!

My solution, which is always valid (i.e., x doesn't
have to be even or odd) is just to redefine the t axis
so that the given waveform starts at t = 0 instead of
t = -|t0|. Then the transform is multiplied by
exp(i*2*pi*f*|t0|) only resulting in a phase shift that
is linearly increasing in frequency.

> B. What assumptions does the MATLAB fft(.) routine have?

See above.

> B.1. Does it expect the DC bin to be the first element of
> the input signal?

No, see below.

> B.2. What other assumptions... ?

See above.

> Here's my understanding (or lack of)...
>
> 1. TAKING THE FFT
> The following command gives me the spectrum with the DC bin
>  as the first element: X = fft(x);
>
> Now, the first half contains the positive frequencies [f=0,f=Fs/2].
> The second half contains the negative frequencies [f=-Fs/2, f=0).

No. See the definition of fb above.

The Nyquist frequency Fs/2 is not in the spectrum when N is odd.
The Nyquist freqency is the first negative freqency when N is even.

N odd
Negative frequencies    [ -(Fs-df)/2 :     -df    ]
Nonnegative frequencies [     0      :  (Fs-df)/2 ]

N even
Negative frequencies    [ -Fs/2 :  -df    ]
Nonnegative frequencies [    0  : Fs/2-df ]

> 2. FFTSHIFT THE OUTPUT
> Now, if I take the fftshift - this is nothing more than a 'special'
> circshift; it swaps the two halfs; its puts DC in the center. The
> vector contains freq components that span [f=-Fs/2, f=Fs/2].

No, see above.

> X_out_shft = fftshift( fft(x) );
>
> I can understand why I might want to do this...

To see the symmetric bipolar spectrum when x is real.

> 3. FFTSHIFT THE INPUT
> I often see that the following is performed. But I do not
> understand, *why* I would want to do this:
>
> X_in_shft = fftshift( fft( fftshift(x) ) );

fftshift( fft( ifftshift(x) ) );

when x is defined "symmetrically" about t = 0.

> Upon inspection, I see that shifting the input does not modify the
> magnitude of X_in_shft, but it does modify the phase. So, why is
> this done?

Again, it is not a shift, it is a reflection about the "center"
point keeping in mind the periodicity with period N.

> 4. ASSUMPTIONS
> Finally, what does MATLAB's fft routine expect? Should I also be
> plugging in an input vector with DC as the first element?

That makes no sense. Just plug in the waveform. if it doesn't
start at t = 0 then you have to reread this post and reply
if you have more questions.

Hope this helps.

Greg

```
```On Jan 11, 10:30&#2013266080;am, Jerry Avins <j...@ieee.org> wrote:
> orthocto wrote:
> > I've tried looking around for an answer to these questions... and there are
> > a few threads. Yet, I am still confused.
>
> Matlab has a fine help system. Have you tried it?

The documentation on the fft is piss-poor.

Greg
```
```On Jan 11, 2:11&#2013266080;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()?

> 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?

> Define
>
> dt = 1/Fs, T = N*dt, df = 1/T
>
> fft assumes
>
> 1. x is defined over the nonnegative time interval
> &#2013266080; &#2013266080; &#2013266080;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.
> &#2013266080; &#2013266080; If X = fft(x), then ifft(X) &#2013266080;will automatically have
> &#2013266080; &#2013266080; that property.
> 3. f is defined over the nonnegative frequency interval
> &#2013266080; &#2013266080; f = df*(0:N-1) = 0:df:Fs-df = linespace(0,Fs-df,N);
> 4. X = fft(x) is automatically periodic with period
> &#2013266080; &#2013266080;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 &#2013266080;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
()).

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.

r b-j

```
```On Jan 11, 3:20 pm, robert bristow-johnson <r...@audioimagination.com>
wrote:
> 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.

and

ifftshift(X) undoes the results of fftshift

In particular, there is no mention of shifting in the
time domain.

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
time functions.

I hope my reasoning is clear.

Greg
```
```On Jan 11, 7:25=A0am, "orthocto" <ortho...@gmail.com> wrote:
> I've tried looking around for an answer to these questions... and there a=
re
> a few threads. Yet, I am still confused.
>
> My questions of this thread are:
> A. Why should one do fft( fftshift(x) )
> B. What assumptions does the MATLAB fft(.) routine have?
> B.1. Does it expect the DC bin to be the first element of the input
> signal?
> B.2. What other assumptions... ?
>
> Here's my understanding (or lack of)...
>
> 1. TAKING THE FFT
> The following command gives me the spectrum with the DC bin as the first
> element:
> X =3D fft(x); =A0
>
> Now, the first half contains the positive frequencies [f=3D0,f=3DFs/2]. T=
he
> second half contains the negative frequencies [f=3D-Fs/2, f=3D0).
>
> 2. FFTSHIFT THE OUTPUT
> Now, if I take the fftshift - this is nothing more than a 'special'
> circshift; it swaps the two halfs; its puts DC in the center. The vector
> contains freq components that span [f=3D-Fs/2, f=3DFs/2].
>
> X_out_shft =3D fftshift( fft(x) );
>
> I can understand why I might want to do this...
>
> 3. FFTSHIFT THE INPUT
> I often see that the following is performed. But I do not understand,
> *why* I would want to do this:
>
> X_in_shft =3D fftshift( fft( fftshift(x) ) );
>
> Upon inspection, I see that shifting the input does not modify the
> magnitude of X_in_shft, but it does modify the phase. So, why is this
> done?

The standard FFT implementation produces a result whose phase is
referenced to the sides of FFT aperture (or inherent rectangular
window).  For any signal components which are presumed not to be
periodic in this aperture, the phase of these components at the
two edges of the aperture won't match each other, and thus won't
be directly related to the phase of the FFT result.  However if
you rotate the your data by half the aperture, the center of your
data will now be at the edge of the FFT, and thus the FFT phase
result will relative to the center of your pre-shifted data,
where any sinusoidal components (periodic or not in the FT
window) will be more presumably be continuous, as sampled by the
discrete FT.

The equivalent alternative would be to adjust the phase of every
other bin of the FFT result.

Having the FFT phase result match the data is a bit useful if, in
the frequency domain, you want to interpolate, analyze, or modify
the phase of a not-necessarily-periodic signal (common in real
world sampling), and be able to visualize what you are doing.

IMHO. YMMV.
--
rhn A.T nicholson d.0.t C-o-M
http://www.nicholson.com/rhn/dsp.html
```
```On Jan 13, 11:22&#2013266080;am, "Ron N." <rhnlo...@yahoo.com> wrote:
> On Jan 11, 7:25&#2013266080;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) )
> > B. What assumptions does the MATLAB fft(.) routine have?
> > B.1. Does it expect the DC bin to be the first element of the input
> > signal?
> > B.2. What other assumptions... ?
>
> > Here's my understanding (or lack of)...
>
> > 1. TAKING THE FFT
> > The following command gives me the spectrum with the DC bin as the first
> > element:
> > X = fft(x); &#2013266080;
>
> > Now, the first half contains the positive frequencies [f=0,f=Fs/2]. The
> > second half contains the negative frequencies [f=-Fs/2, f=0).
>
> > 2. FFTSHIFT THE OUTPUT
> > Now, if I take the fftshift - this is nothing more than a 'special'
> > circshift; it swaps the two halfs; its puts DC in the center. The vector
> > contains freq components that span [f=-Fs/2, f=Fs/2].
>
> > X_out_shft = fftshift( fft(x) );
>
> > I can understand why I might want to do this...
>
> > 3. FFTSHIFT THE INPUT
> > I often see that the following is performed. But I do not understand,
> > *why* I would want to do this:
>
> > X_in_shft = fftshift( fft( fftshift(x) ) );
>
> > Upon inspection, I see that shifting the input does not modify the
> > magnitude of X_in_shft, but it does modify the phase. So, why is this
> > done?
>
> The standard FFT implementation produces a result whose phase is
> referenced to the sides of FFT aperture (or inherent rectangular
> window). &#2013266080;For any signal components which are presumed not to be
> periodic in this aperture, the phase of these components at the
> two edges of the aperture won't match each other, and thus won't
> be directly related to the phase of the FFT result. &#2013266080;However if
> you rotate the your data by half the aperture, the center of your
> data will now be at the edge of the FFT, and thus the FFT phase
> result will relative to the center of your pre-shifted data,
> where any sinusoidal components (periodic or not in the FT
> window) will be more presumably be continuous, as sampled by the
> discrete FT.
>
> The equivalent alternative would be to adjust the phase of every
> other bin of the FFT result.

i think Greg (Heath, not Greg B) says that you're (or we're) wrong,
Ron.

> Having the FFT phase result match the data is a bit useful if, in
> the frequency domain, you want to interpolate, analyze, or modify
> the phase of a not-necessarily-periodic signal (common in real
> world sampling), and be able to visualize what you are doing.
>
> IMHO. YMMV.

r b-j

```
```On Jan 14, 3:57 am, robert bristow-johnson <r...@audioimagination.com>
wrote:
> On Jan 13, 11:22 am, "Ron N." <rhnlo...@yahoo.com> wrote:
> > On Jan 11, 7: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) )
> > > B. What assumptions does the MATLAB fft(.) routine have?
> > > B.1. Does it expect the DC bin to be the first element of the input
> > > signal?
> > > B.2. What other assumptions... ?
>
> > > Here's my understanding (or lack of)...
>
> > > 1. TAKING THE FFT
> > > The following command gives me the spectrum with the DC bin as the first
> > > element:
> > > X = fft(x);
>
> > > Now, the first half contains the positive frequencies [f=0,f=Fs/2]. The
> > > second half contains the negative frequencies [f=-Fs/2, f=0).
>
> > > 2. FFTSHIFT THE OUTPUT
> > > Now, if I take the fftshift - this is nothing more than a 'special'
> > > circshift; it swaps the two halfs; its puts DC in the center. The vector
> > > contains freq components that span [f=-Fs/2, f=Fs/2].
>
> > > X_out_shft = fftshift( fft(x) );
>
> > > I can understand why I might want to do this...
>
> > > 3. FFTSHIFT THE INPUT
> > > I often see that the following is performed. But I do not understand,
> > > *why* I would want to do this:
>
> > > X_in_shft = fftshift( fft( fftshift(x) ) );
>
> > > Upon inspection, I see that shifting the input does not modify the
> > > magnitude of X_in_shft, but it does modify the phase. So, why is this
> > > done?
>
> > The standard FFT implementation produces a result whose phase is
> > referenced to the sides of FFT aperture (or inherent rectangular
> > window).  For any signal components which are presumed not to be
> > periodic in this aperture, the phase of these components at the
> > two edges of the aperture won't match each other, and thus won't
> > be directly related to the phase of the FFT result.  However if
> > you rotate the your data by half the aperture, the center of your
> > data will now be at the edge of the FFT, and thus the FFT phase
> > result will relative to the center of your pre-shifted data,
> > where any sinusoidal components (periodic or not in the FT
> > window) will be more presumably be continuous, as sampled by the
> > discrete FT.
>
> > The equivalent alternative would be to adjust the phase of every
> > other bin of the FFT result.
>
> i think Greg (Heath, not Greg B) says that you're (or we're) wrong,
> Ron.

No I don't. I say that the MALAB fft/ifft documentation is confusing
and recommend that a good way for a newbie to keep things straight is
to use the sequence fft,fftshift,ifft,ifftshift when phase is to be
referenced to the left side of the the time window. This results in
fftshift being used on the fft in the spectral domain and ifftshift
being used on ifft in the time domain.

When phase is to be referenced to any other time in the window,
my recommendation is to simply multiply X(f) by the factor
exp(-i*2*pi*f*t0).

When the time function is real and symmetric or antisymmetric about
the "middle" of the window, the method of always using fftshift in
the time domain is not clear to me. It seems to me that the
preferred approach would be to use ifftshift in the time domain.

I will take a look into this. However, before doing so let
me note that

1. fftshift and ifftshift are inverses
2. fftshift = ifftshift when N is even

Most applications use N = 2^M. Therefore, many users are
neither aware of the existence of ifftshift nor aware that fftshift
is not it's own inverse when N is odd.

Hope this helps.

Greg
```
```On Jan 18, 7:08&#2013266080;am, Greg Heath <he...@alumni.brown.edu> wrote:
> On Jan 14, 3:57 am, robert bristow-johnson <r...@audioimagination.com>
> wrote:
> > On Jan 13, 11:22 am, "Ron N." <rhnlo...@yahoo.com> wrote:
> > > On Jan 11, 7: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) )
> > > > B. What assumptions does the MATLAB fft(.) routine have?
> > > > B.1. Does it expect the DC bin to be the first element of the input
> > > > signal?
> > > > B.2. What other assumptions... ?
>
> > > > Here's my understanding (or lack of)...
>
> > > > 1. TAKING THE FFT
> > > > The following command gives me the spectrum with the DC bin as the first
> > > > element:
> > > > X = fft(x);
>
> > > > Now, the first half contains the positive frequencies [f=0,f=Fs/2]. The
> > > > second half contains the negative frequencies [f=-Fs/2, f=0).
>
> > > > 2. FFTSHIFT THE OUTPUT
> > > > Now, if I take the fftshift - this is nothing more than a 'special'
> > > > circshift; it swaps the two halfs; its puts DC in the center. The vector
> > > > contains freq components that span [f=-Fs/2, f=Fs/2].
>
> > > > X_out_shft = fftshift( fft(x) );
>
> > > > I can understand why I might want to do this...
>
> > > > 3. FFTSHIFT THE INPUT
> > > > I often see that the following is performed. But I do not understand,
> > > > *why* I would want to do this:
>
> > > > X_in_shft = fftshift( fft( fftshift(x) ) );
>
> > > > Upon inspection, I see that shifting the input does not modify the
> > > > magnitude of X_in_shft, but it does modify the phase. So, why is this
> > > > done?
>
> > > The standard FFT implementation produces a result whose phase is
> > > referenced to the sides of FFT aperture (or inherent rectangular
> > > window). &#2013266080;For any signal components which are presumed not to be
> > > periodic in this aperture, the phase of these components at the
> > > two edges of the aperture won't match each other, and thus won't
> > > be directly related to the phase of the FFT result. &#2013266080;However if
> > > you rotate the your data by half the aperture, the center of your
> > > data will now be at the edge of the FFT, and thus the FFT phase
> > > result will relative to the center of your pre-shifted data,
> > > where any sinusoidal components (periodic or not in the FT
> > > window) will be more presumably be continuous, as sampled by the
> > > discrete FT.
>
> > > The equivalent alternative would be to adjust the phase of every
> > > other bin of the FFT result.
>
> > i think Greg (Heath, not Greg B) says that you're (or we're) wrong,
> > Ron.
>
> No I don't. I say that the MALAB fft/ifft documentation is confusing
> and recommend that a good way for a newbie to keep things straight is
> to use the sequence fft,fftshift,ifft,ifftshift when phase is to be
> referenced to the left side of the the time window. This results in
> fftshift being used on the fft in the spectral domain and ifftshift
> being used on ifft in the time domain.
>
> When phase is to be referenced to any other time in the window,
> my recommendation is to simply multiply X(f) by the factor
> exp(-i*2*pi*f*t0).
>
> When the time function is real and symmetric or antisymmetric about
> the "middle" of the window, the method of always using fftshift in
> the time domain is not clear to me. It seems to me that the
> preferred approach would be to use ifftshift in the time domain.
>
> I will take a look into this. However, before doing so let
> me note that
>
> 1. fftshift and ifftshift are inverses
> 2. fftshift = ifftshift when N is even
>
> Most applications use N = 2^M. Therefore, many users are
> neither aware of the existence of ifftshift nor aware that fftshift
> is not it's own inverse when N is odd.

The MATLAB code below shows that ifftshift, and not fftshift shifts
the t = 0 symmetry point maximum in the middle of the window to
the left hand border. Whereas fftshift shifts it to the right hand
border.

More simply,

1. fftshift shifts and reflects the positive time points to the left
hand
side of the window causing the original middle point at t=0
to become the right most point.
whereas
2. ifftshift shifts and reflects the negative time components to the
right hand side of the window causing the original middle point
at t=0 to become the left most point.

Hope this helps.

Greg

N = 15, T = 5, dt = T/N       % 0.33333

t = dt*[-(N-1)/2 : (N-1)/2 ];  % [-(T-dt)/2 : dt : (T-dt)/2]
minmax(t)                         % [ -2.3333  2.3333]

z1 = exp(-t.^2);
[z1max i1max] = max(z1)       % [ 1 8]  Max in the middle
z2 = ifftshift(z1);
[z2max i2max] = max(z2)       % [1 1 ] Max on the left
z3 = fftshift(z1);
[z3max i3max] = max(z3)       % [1 15] Max on the right

```