3

I am looking at some FFT code in a matlab project and the FFT and inverse FFT is computed this way:

% Here image is a 2D image.
image_fft = fftshift(image,1);
image_fft = fftshift(image_fft,2);

image_fft = fft(image_fft,[],1);
image_fft = fft(image_fft,[],2);

image_fft = fftshift(image_fft,1);
image_fft = fftshift(image_fft,2);

% Some processing and then same sequence of fftshift, ifft and fftshift to move to
% time domain

I tried to find some information online but am having trouble wondering why the fftshift needs to be done before computing the FFT.

Another question I have is whether this is something really Matlab specific. For example, I am planning to port this code to C++ and use KISS FFT. Do I need to be vary of this?

Luca
  • 10,458
  • 24
  • 107
  • 234

2 Answers2

3

The reason why people like to swap prior to the DFT is because it makes the center pixel of the image the one with zero-phase shift. It often makes algorithms that depend on phase easier to think about and implement. It is not matlab specific and if you want to port an exact version of the code to another language, you'll need to perform the quadswap beforehand too.

EDIT:

Let me give an example that I hope will clear things up. Let's say that our image is the sum of a bunch of sinc functions with varying locations throughout the image. In the frequency domain, each of these sinc functions is a rect function with the same amplitude but with a different linear phase component that determines the location of the sinc in the image domain. By swapping the image prior to taking the DFT, we make the linear phase component of the frequency domain representation of the center pixel be zero. Moreover, the linear phase components of the other sinc functions will now be a function of their distance from the center pixel. If we didn't swap the image beforehand, then the linear phase components of the rect functions would be a function of their distance from the pixel in the top-left of the image. This would be non-intutive and would involve the same kind of phase wrapping considerations that one sees with equating the frequencies in the range (pi,2pi) rad/sample with more the intuitive (-pi,0) rad/sample.

AnonSubmitter85
  • 933
  • 7
  • 14
  • Ok, so it makes the DC component in the centre of the bin. I am guessing one needs to do this also for other libraries. I see in the post here http://dsp.stackexchange.com/questions/2570/output-of-kiss-fft-interpretation that the left most value in Kiss FFT output is the DC component as well, followed by low and high frequencies. So, to replicate this matlab code I would need to also implement a fftshift like routine in Kiss FFT? – Luca Nov 11 '14 at 16:21
  • 3
    @Luca if you apply the shift *before* the FFT it does not shift the zeroth frequency to the center since it shifts the original image and not the spectrum. – Cape Code Nov 11 '14 at 16:23
  • 2
    If you're using a standard FFT library, there should be a mechanism to shift the frequency spectrum already. However, if it doesn't, you simply have to take your image (in pixel domain) and multiply each value by `(-1)^(x+y)` where `x` and `y` are the pixel locations in the image (0-indexed). By taking the FFT of this, the FFT frequency-shifting property will take effect and would effectively centre your spectrum. – rayryeng Nov 11 '14 at 16:26
  • @CapeCode: Yes, you are right. However, I think this also has something to do with the origin shift as well. In the original code, the fourier transformed image is multiplied with a Gaussian kernel (constructed in the frequency domain which is a Gaussian). So, I am guessing the fftshift of the image will make the origins consistent. – Luca Nov 11 '14 at 16:49
  • 1
    @Luca Please see the example my edit. I hope it helps clear things up. – AnonSubmitter85 Nov 11 '14 at 17:04
2

For images, it's better to use fft2. It's matlab's convention to arrange 2D ffts with the DC in the corners. Presumably because of the row/array conventions. fftshift allows for a more intuitive display of the FFT with the DC in the center.

I don't fully understand what the piece of code you copied is about, here is an example of fft and inverse fft of an image using matlab.

And a more detailed tutorial here.

Community
  • 1
  • 1
Cape Code
  • 3,584
  • 3
  • 24
  • 45