You are on page 1of 5

Correct use of fftshift and ifftshift at inpu tto fft and ifft

http://www.mathkb.com/Uwe/Forum.aspx/matlab/158171/Correct-use-of-...

Math Forum / Math Software / MATLAB / June 2010

Tip: Looking for answers? Try searching our database.

Correct use of fftshift and ifftshift at inpu tto fft and ifft
Thread view: Enable EMail Alerts Start New Thread Thread rating: Chris Bore - 23 Jun 2010 10:10 GMT I've seen discussion here of the correct use of MATLAB's fftshift() function. I'd like to verify my interpretation, and to ask what are the effects when people do not use it correctly. Basically, MATLAB implements an fft (as do other sets of functions) whose results AND inputs are ordered (for an array of N elements), from 0 to (N/2-1) and then from –N/2 to -1. (I will call this 'swapped' from now on.) But in what I might call a 'natural' ordering, for a spectrum or time function centred at zero and extending equal time (or frequency) either side of zero, the arrays of input data and results would be ordered (2’s complement style) from –N/2 to N/2-1 where N is the number of elements. That is, time zero (or frequency zero) are at the centre of such an array. It is well known that the results of MATLAB's fft() function must be re-ordered with the MATLAB function fftshift() so that they are 'naturally' ordered (for instance when plotting). H = fftshift( fft ( h ) ); Similarly, the inverse fft also requires this fftshift: h = fftshift ( ifft ( H ) ); It seems to be less well known that both the fft() and ifft() functions also expects their INPUTS to be swapped. You can see this if you do the inverse fft of an fft (yielding the original input): h == ifft( fft( h ) ); The fft() results are swapped, so clearly the ifft() expects its input to be swapped. Thus, if you start with a naturally ordered spectrum (say, a desired filter frequency response centred at DC) then you must make it swapped before you use the ifft(). The function fftshift 'unswaps' an array, and the inverse function ifftshift() swaps one. So we use ifftchift BEFORE an ifft() in the case where the input (spectrum) is NOT the result of a MATLAB fft(): h = ifft( ifftshift( H ) ); (where H is an unswapped array). So to do an inverse FFT where both input and results are to be naturally ordered, using MATLAB, we must do: h = fftshift( ifft( ifftshift( H ) ) ); (which, by the way, most people seem not to do..).

1 of 5

11/9/2011 4:22 PM

Correct use of fftshift and ifftshift at inpu tto fft and ifft

http://www.mathkb.com/Uwe/Forum.aspx/matlab/158171/Correct-use-of-...

The fft() also requires a similar idiom. You can see this if you consider the fft of an inverse fft (yielding the original input): H == fft( ifft( H ) ); Since the result of ifft() is swapped, clearly fft() must also expect its input to be swapped, so if we start with an unswapped array we must swap it: H = fftshift( fft( ifftshift( h ) ) ); (which almost nobody seems to do..). (The reason things are like this can be explained in two ways, I think. First, that the original Cooley-Tukey FFT results were swapped and so all FFTs since return swapped results. Second, that the fft() implements a particular DFT equation that is a sum up from zero (no negative time or frequency) and so the inputs must be shifted from a DC-centred array.) I verified this for myself by generating an input whose analytic transform I knew and could code, and comparing the results of the two idioms to the expected anlytic result: H = fftshift( fft( h ) ); % most common usage, wrong result H = fftshift( fft( ifftshift( h ) ) ); % uncommon usage, correct result So far so good. My first questions: 1) is the above idiom for using MATLAB's fft() and ifft() correct? 2) is my explanation of why this is so, valid? Now to the real question. What is the effect if people do not do it right? The common way to use MATLAB's fft() is: H = fftshift( fft( h ) ); The lacking ifftshift is a circular shift by half the array length (there is a tweak depending if the array length is odd or even). If we look only at magnitude of the result, then the result is unchanged by any circular shift. And since probably most people do look only at the magnitude of the result of an FFT, perhaps this is why the incorrect idiom is most common? But if we look at complex numbers, or phase, then there is a clear difference and the most common idiom is wrong. I came upon this while using MATLAB to design an equalization filter, from a DC-centred spectrum, by an FFT to get the filter coefficients (before windowing etc). The results were incorrect (at least, differd from my expectation) until I adopted the correct idiom I have outlined above. So my next questions are: 3) are Mathworks aware of this and is it documented? 4) why do so many (most?) people not do it right? 5) what are the effects, and ramifications, of doing it wrong? Chris ================== Chris Bore BORES Signal Processing www.bores.com

2 of 5

11/9/2011 4:22 PM

Correct use of fftshift and ifftshift at inpu tto fft and ifft

http://www.mathkb.com/Uwe/Forum.aspx/matlab/158171/Correct-use-of-...

Reply to this Message Greg Heath - 23 Jun 2010 17:00 GMT > I've seen discussion here of the correct use of MATLAB's fftshift() function. > > I'd like to verify my interpretation, and to ask what are the effects when people do not use it correctly. ------SNIP > So my next questions are: > > 3) are Mathworks aware of this and is it documented? It's probably known. It is documented about as poorly as one could imagine. I cannot guess why ( Maybe it's too complicated?) It gets more confusing when N is odd because then fftshift and ifftshift yield different results and cannot be used interchangeably. > 4) why do so many (most?) people not do it right? Many are not even aware. Most are not interested in phase. It is not easy to remember (especially when N is odd.) > 5) what are the effects, and ramifications, of doing it wrong? phase shifts of 2*pi*Fs/N Hope this helps. Greg Reply to this Message Greg Heath - 23 Jun 2010 17:30 GMT > I've seen discussion here of the correct use of MATLAB's fftshift() function. > > I'd like to verify my interpretation, and to ask what are the effects when people do not use it correctly. > > Basically, MATLAB implements an fft (as do other sets of functions) whose results AND inputs are ordered (for an array of N elements), from 0 to (N/2-1) and then from N/2 to -1. (I will call this 'swapped' from now on.) But in what I might call a 'natural' ordering, for a spectrum or time function centred at zero and extending equal time (or frequency) either side of zero, the arrays of input data and results would be ordered (2s complement style) from N/2 to N/2-1 where N is the number of elements. That is, >time zero (or frequency zero) are at the centre of such an array. You are assuming N is even. If so, neither the bipolar time waveform xb defined on dt*(-N/2 : N/2-1) nor the bipolar frequency waveform Xb defined on df*(-N/2 : N/2-1) is "centered" at zero: there are only N/2-1 positive points compared to N/2 negative points. The "center" of the waveform is between 0 and the 1st negative point (-1/2?). In addition, when N is even, ifftshift(:) = fftshift(:). Therfore,

3 of 5

11/9/2011 4:22 PM

Correct use of fftshift and ifftshift at inpu tto fft and ifft

http://www.mathkb.com/Uwe/Forum.aspx/matlab/158171/Correct-use-of-...

it it is easy to fall into habits that are invalid when N is odd. Therefore, if you make any rules about using the shift functions, make sure they hold when N is odd. Hope this helps. Greg Reply to this Message Chris Bore - 23 Jun 2010 18:29 GMT Hello Greg, Thanks for your reply (and earlier posts on a similar topic that I have read). > You are assuming N is even. If so, neither the bipolar time waveform > xb defined on dt*(-N/2 : N/2-1) nor the bipolar frequency waveform Xb > defined on df*(-N/2 : N/2-1) is "centered" at zero: there are only > N/2-1 positive points compared to N/2 negative points. The "center" > of the waveform is between 0 and the 1st negative point (-1/2?). Indeed. I think in a more generic way what is needed is a circular shift that results in the 'zero' frequency ending up at the 'zero' of the fft's assumed unipolar interval. For inputs that extend the same either side of zero the ifftshift works, but for other cases an explicit circular shift may be needed by the required amount. In fact, this is all exploiting the property of the DFT in respect of circular shifts, to avoid an explicit demodulation (which is another way to look at it). It is a pity, I think, because it is an example where MATLAB's abstract and generic way to express things becomes narrowed and stuck to the implementation of a particular function - and a particular way to implement that function too. > > I've seen discussion here of the correct use of MATLAB's fftshift() function. >>
[quoted text clipped - 18 lines]

> > Greg Reply to this Message Greg Heath - 23 Jun 2010 18:49 GMT > Hello Greg, >
[quoted text clipped - 30 lines]

> > Therefore, if you make any rules about using the shift > > functions, make sure they hold when N is odd. After further thought I came up with this and just posted a similar message on another thread ("Inverse Fourier Transforms"): The MATLAB fft and ifft functions assume the nonnegative intervals t = dt*(0:N-1) and f = df*(0:N-1) corresponding to the Fourier pair x(t) and X(f). If x and X are considered periodic, fftshift yields the translation to

4 of 5

11/9/2011 4:22 PM

Correct use of fftshift and ifftshift at inpu tto fft and ifft

http://www.mathkb.com/Uwe/Forum.aspx/matlab/158171/Correct-use-of-...

the "zero centered" bipolar intervals with indexing tb = dt * [-ceil((N-1)/2) : floor((N-1)/2)] fb = df * [-ceil((N-1)/2) : floor((N-1)/2)] corresponding to the Fourier pair xb(tb) and Xb(fb). ifftshift is the inverse of fftshift. When N is even, fftshift and ifftshift yield the same result. However, when N is odd, x = ifftshift(xb) X = fft(x) Xb = fftshift(X) and to return X = ifftshift(Xb) x = ifft(X) xb = fftshift(x) The trick is to remember that fftshift will "center" the waveform about the coordinate zero. If in doubt type fftshift([-2:2]) into the command line. Hope this helps. Greg Reply to this Message

2011 Advenet LLC Privacy Policy - Terms of Use This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.

5 of 5

11/9/2011 4:22 PM

You might also like