fn

fftshift

Tensor
fftshift(input: Tensor, dim: int | Sequence[int] | None = None)
source

Shift the zero-frequency component to the centre of the spectrum.

Rearranges the output of fft, fft2, or fftn so that the zero-frequency bin (DC component) moves to the centre of each specified axis. For an axis of length NN, the operation performs a circular shift of N/2\lfloor N/2 \rfloor positions:

  • Bins N/2,,N1\lfloor N/2 \rfloor, \ldots, N-1 (the negative frequencies) wrap around to the left half of the axis.
  • Bins 0,,N/210, \ldots, \lfloor N/2 \rfloor - 1 (DC and positive frequencies) move to the right half.

The result is a spectrum ordered from the most-negative frequency to the most-positive, with DC at index N/2\lfloor N/2 \rfloor.

Parameters

inputTensor
Tensor containing DFT output (or any data in FFT frequency ordering) of any shape.
dimint or sequence of int= None
Axis or axes along which to shift. Negative indices are supported. Defaults to all axes when None.

Returns

Tensor

Tensor with the same shape and dtype as input, rearranged so that zero frequency is centred along each specified axis.

Notes

Visual motivation — the standard FFT ordering places the DC component at index 0 and the Nyquist frequency at index N//2N//2. When displaying spectra (e.g. 2-D power spectra of images), it is conventional to show the DC component at the centre of the image. fftshift performs exactly this reordering so that the spatial-frequency origin is visually centred.

Frequency correspondence — after shifting, use fftfreq with the same n and apply the same shift to obtain a matching, monotonically increasing frequency axis:

fcentred=fftshift(fftfreq(N,d)).f_{\text{centred}} = \text{fftshift}(\text{fftfreq}(N, d)).

Inverseifftshift undoes fftshift exactly. For even NN, fftshift and ifftshift are self-inverse (applying either twice returns the original). For odd NN the two differ by one position.

Implementation — realised as lucid.roll with shift N/2\lfloor N/2 \rfloor along each axis; no data copy is required when the underlying buffer supports strides.

Examples

Centre the spectrum of a 1-D signal:
>>> x = lucid.randn(8)
>>> X = lucid.fft.fft(x)
>>> X_centred = lucid.fft.fftshift(X)
>>> # X_centred[4] == X[0]  (DC at centre for N=8)
Centre both the 2-D spectrum and its frequency axes:
>>> x = lucid.randn(64, 64)
>>> X = lucid.fft.fft2(x)
>>> X_c = lucid.fft.fftshift(X)           # centred spectrum
>>> freqs = lucid.fft.fftshift(lucid.fft.fftfreq(64))  # centred frequency axis