fftshift

Moves the zero-frequency term of an FFT result to the center.

Category: Mathematics > Fourier Analysis and Filtering

Syntax

Y = fftshift(X)

Swaps the halves of X along every dimension.

Y = fftshift(X, dim)

Shifts only along dimension dim.

Description

FFT output starts with frequency zero and ends with the negative frequencies. fftshift circularly shifts each dimension by floor(n/2) so that zero frequency lands in the middle and the axis runs from negative to positive, which is the natural order for plotting.

For even lengths the operation is its own inverse. For odd lengths it is not, and ifftshift is needed to go back.

Input arguments

X
Array, typically the output of fft, fft2 or fftn.
dim
Dimension to shift along.

Output arguments

Y
Rearranged array of the same size as X.

Example

Even, odd and one dimension

fftshift(1:6)
fftshift(1:5)
fftshift([1 2 3; 4 5 6; 7 8 9], 1)
Output
ans = 1×6

     4     5     6     1     2     3
ans = 1×5

     4     5     1     2     3
ans = 3×3

     7     8     9
     1     2     3
     4     5     6

See also

ifftshift fft fft2 circshift fftn