Code covered by the BSD License

4.33333
4.3 | 6 ratings Rate this file 49 Downloads (last 30 days) File Size: 102 KB File ID: #25473 Version: 1.0

# Why use fftshift(fft(fftshift(x))) in Matlab instead of fft(x)?

by

### Kan Wu (view profile)

Explain why we use fftshift(fft(fftshift(x))) in Matlab instead of fft(x)

File Information
Description

Explain why we use fftshift(fft(fftshift(x))) in Matlab instead of fft(x). An example is given. The example and Matlab codes are partially copied from Daniele Disco ‘s work in "A guide to the Fast Fourier Transform, 2nd Edition".

Acknowledgements

A Guide To The Fft 2nd Edition Plus inspired this file.

MATLAB release MATLAB 7.6 (R2008a)
10 Mar 2015 Hongyu Li

### Hongyu Li (view profile)

Thank you very much,Kan Wu!

16 Jun 2014 Junqiu Qi

### Junqiu Qi (view profile)

03 Jan 2014 Kan Wu

### Kan Wu (view profile)

Hi Yuji,

ifftshift is to correct the bug in fftshift when dealing with the sequence with odd length (that is the number of elements in the sequence is odd). So it is safe to always use: fftshift(fft(ifftshift(sig))) or fftshift(ifft(ifftshift(spectrum))).

Comment only
13 May 2013 Yuji Zhang

### Yuji Zhang (view profile)

hi Kan,Shalin,

Thanks for the code and the discussion - nice and helpful.

A book I'm reading says it should be
fftshift(fft(fftshift(sig)))
and
ifftshift(ifft(ifftshift(spectrum)))

I'm confused. Could you explain where to use ifftshift when N=odd?

Thank you!
(The book is "Numerical Simulation of Optical Wave Propagation with examples in Matlab")

23 May 2010 zhang lanqing

### zhang lanqing (view profile)

thanl you very much !

Comment only
18 May 2010 Shalin Mehta

### Shalin Mehta (view profile)

You're welcome. ifftshift is the same as fftshift for even length sequence, but different for odd length. So if one uses fftshift(fft(ifftshift(...))) things work well. By the way, scaling by dt and df to correct for scaling introduced by FFT algorithm is neat trick.

Comment only
16 May 2010 Kan Wu

### Kan Wu (view profile)

To Mehta, yes you r right. ifftshift is used for sequence with odd length. Thank you for the correction!

Comment only
14 May 2010 Shalin Mehta

### Shalin Mehta (view profile)

There is a problem with above recipe. It fails when you have sequence of odd length. Correct recipe is:
fftshift(fft(ifftshift(sig))) or fftshift(ifft(ifftshift(spectrum))).

A description of this can be found on my submission on fftshift, ifftshift.

To observe that above is true, run the following code with fftshift and ifftshift on inner call for computing Xfinal.
-------------------------
Bx = 50;
A = sqrt(log(2))/(2*pi*Bx);
fs = 500; %sampling frequency
dt = 1/fs; %time step
T=1; %total time window
t = -T/2:dt:T/2; %time grids
df = 1/T; %freq step
Fmax = 1/2/dt; %freq window
f=-Fmax:df:Fmax; %freq grids, not used in our examples, could be used by plot(f, X)

x = exp(-t.^2/(2*A^2));
Xan = A*sqrt(2*pi)*exp(-2*pi^2*f.^2*A^2); %X(f), analytical Fourier transform of x(t), real
Xfft = dt * fft(x); %directly using fft()
Xfftshift = dt * fft(fftshift(x)); %using fftshift() before fft()
Xfinal = dt * fftshift(fft(ifftshift(x))); %identical with analytical X(f), also note dt
subplot(211); plot(f,Xan,f,real(Xfinal),'--');
subplot(212); plot(f,imag(Xfinal));

18 Dec 2009 Matt Fetterman

### Matt Fetterman (view profile)

Very interesting!

21 Oct 2009 Kan Wu