torch.fft.fftshift
function fftshift(x: Tensor, options?: ShiftOptions): Tensorfunction fftshift(x: Tensor, dim: number | number[], options?: ShiftOptions): TensorShifts the zero-frequency component to the center of the spectrum.
Rearranges the output of the FFT to move the DC (zero-frequency) component from the beginning of the tensor to the center. This is standard for visualizing spectra where negative frequencies are on the left and positive frequencies on the right. Useful for:
- Spectrum visualization: Creating plots with zero-frequency at the center
- Filter design: Designing symmetric frequency filters
- Image processing: Centering the Fourier transform of an image
- Signal analysis: Viewing frequency content in a human-readable layout
- Inverse shift: Use ifftshift to reverse this operation
- Inverse operation: Use ifftshift() to undo fftshift before ifft. Always apply ifftshift before ifft if you shifted the FFT output.
- Center location: After fftshift, zero frequency is at index n//2. For n=256, it's at index 128. For n=255, it's at index 127.
- Circular shift: fftshift performs a circular shift (roll) operation. Elements wrap around, nothing is lost, only reordered.
- Complex tensors: fftshift works with both real and complex tensors. For complex (with last dimension size 2), the shift is applied correctly.
- Multi-dimensional: When dim is omitted, all dimensions are shifted. This is common for 2D image spectra where you want both frequency dimensions centered.
- Must use ifftshift before ifft: If you shift FFT output before processing, you MUST use ifftshift to undo the shift before applying ifft. Otherwise, results are wrong.
- Shape preservation: fftshift does not change shape or data values, only their order. The tensor shape remains exactly the same.
- Odd-length signals: For odd-length signals (n=odd), the shift is slightly asymmetric. Index at n//2 becomes the new center (zero frequency).
Parameters
xTensor- Input tensor (typically FFT output)
optionsShiftOptionsoptional- Shift options: -
dim: Dimension(s) along which to shift (default: shift all dimensions)
Returns
Tensor– Shifted tensor with same shape as inputExamples
// Shift 1D spectrum
const x = torch.fft.fft(torch.randn(10));
const shifted = torch.fft.fftshift(x);
// DC component is now at index 5 instead of index 0// Shift 2D FFT of image
const img_fft = torch.fft.fft2(image);
const centered = torch.fft.fftshift(img_fft);
// Low frequencies now centered, high frequencies at edges// Using specific dimension
const shifted_dim0 = torch.fft.fftshift(x, { dim: 0 });// Round-trip processing
const x = torch.fft.fft(signal);
const shifted = torch.fft.fftshift(x);
// Apply frequency domain processing
const filtered = applyFilter(shifted); // Easier with centered spectrum
// Shift back before ifft
const unshifted = torch.fft.ifftshift(filtered);
const result = torch.fft.ifft(unshifted);// Visualizing 2D frequency spectrum (image FFT)
const image = torch.randn([256, 256]);
const spectrum = torch.fft.rfft2d(image);
const log_magnitude = torch.log(torch.abs(spectrum) + 1e-8);
const shifted = torch.fft.fftshift(log_magnitude); // Shift both dimensions
// Now can display with DC (brightest) in center, falloff toward edgesSee Also
- PyTorch torch.fft.fftshift()
- ifftshift - Inverse operation to undo fftshift
- fft - Forward FFT (output is not centered)
- ifft - Inverse FFT (use with ifftshift before this)