fftshift
→Tensorfftshift(input: Tensor, dim: int | Sequence[int] | None = None)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 , the
operation performs a circular shift of
positions:
- Bins (the negative frequencies) wrap around to the left half of the axis.
- Bins (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 .
Parameters
inputTensordimint or sequence of int= NoneNone.Returns
TensorTensor 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
. 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:
Inverse — ifftshift undoes fftshift exactly.
For even , fftshift and ifftshift are
self-inverse (applying either twice returns the original). For
odd the two differ by one position.
Implementation — realised as lucid.roll with shift
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