# Shifting in DFT (FFT)

Input image

Listing-1

i = imread('Untitled.png');
i = rgb2gray(i);
F = fft2(i);
%%%F = fftshift(F);
F = abs(F);
F = log(F+1);
F = mat2gray(F);
imshow(F);


Output

.

Listing-2

i = imread('Untitled.png');
i = rgb2gray(i);
F = fft2(i);
F = fftshift(F);
F = abs(F);
F = log(F+1);
F = mat2gray(F);
imshow(F);


Output

Seeing the the above two outputs, can you answer the following questions,

1. Why does the FFT of an image produce such an spectrum where zero frequencies are at the corner of the image?

2. Why is that a problem (or, is that)?

3. Why does the shifting operation fixes that phenomenon?

Traditionally, most 1D FFT algorithms on a $2n$-long real signal produce a "symmetric" sequence of coefficients $c_0,C_1,C_2,\ldots,C_{n-2},C_{n-1},c_n,\overline{C}_{n-1},\overline{C}_{n-2},\ldots,\overline{C}_{2},\overline{C}_{1}$. Little $c$s denote real numbers, big $C$s are potentially complex numbers (and their conjugates with the bar).

In 1D, the amplitude spectrum is easy to guess by looking at the first $n+1$ coefficients' moduli only. A spectrum draws a 1D line, and it is represented on a two-dimensional surface. But imagine what would happen if I drew a spectrum as a 1-pixel bar with a varying intensity, from white for high coefficients amplitude to black for low values. It would be more more difficult to read.

[I'll add a graph for that later ]

2D FFT implemented in a separable fashion (row-wise then column-wise) reproduce this symmetry in both dimensions. Each line or column of the 2D spectrum is like the 1D bar described above. Since image generally have a lo of low-pass content, you end up with somehow bright components on the borders, quite difficult to see. Additionally, the periodic effect inherent to FFT add some clutter (often displaying as a cross when the spectrum is centered). You can correct this with a 2D windowing, or instance.

The human eye needs a better arrangement of patterns to distinguish information. Hence, the centering around the DC or 0-frequency component, sometimes used in 1D, is used very often in 2D.

1. Why does the FFT of an image produce such an spectrum where zero frequencies are at the corner of the image? Because of the natural arrangement of output FFT components
2. Why is that a problem (or, is that)? Not really from an information point of view. The information remains the same. But yes for some processing: designing a low-pass filter with a single square mask is handier than correctly putting 4 masks in the corners.
3. Why does the shifting operation fixes that phenomenon? Because it put the 0 at some center, with a mere shift, as explained above

It is all about defining the support of the DFT.
The DFT is periodic with period of $2 \pi$.
Hence on any support of the length $2 \pi$ you see all the data.

On default, when using MATLAB's fft() the result is on the range $\left[ 0, 2 \pi \right]$, namely the Low Frequencies are at the beginning of the output vector.
Using fftshift() the DFT is shifted to match $\left[ -\pi, \pi \right]$ support which means the DC Frequency is centered.

The same logic works on 2D DFT.

• 2. Why is that a problem (or, is that really a problem)? – user23572 Feb 1 '17 at 6:39
• As I wrote above, no problem at all as all data is there. And since this way of calculation is consistent means anything works as it should be. – Royi Feb 1 '17 at 13:21