Skip to main content

Fast Fourier Transform (FFT)

The torch.fft module provides Fast Fourier Transform operations for signal processing and spectral analysis.

1D FFT Functions

fft

torch.fft.fft(input, n=None, dim=-1, norm=None, *, out=None)
Computes the one-dimensional discrete Fourier transform of input.
input
Tensor
The input tensor.
n
int
default:"None"
Signal length. If given, the input will either be zero-padded or trimmed to this length before computing the FFT.
dim
int
default:"-1"
The dimension along which to take the one-dimensional FFT.
norm
str
default:"None"
Normalization mode. Options: "forward", "backward", "ortho".
  • "forward": normalize by 1/n
  • "backward": no normalization (default)
  • "ortho": normalize by 1/sqrt(n)
Returns
Tensor
Complex-valued tensor containing the FFT result

ifft

torch.fft.ifft(input, n=None, dim=-1, norm=None, *, out=None)
Computes the one-dimensional inverse discrete Fourier transform of input.
input
Tensor
The input tensor.
n
int
default:"None"
Signal length.
dim
int
default:"-1"
The dimension along which to take the one-dimensional IFFT.
norm
str
default:"None"
Normalization mode. Default is "backward" which normalizes by 1/n.

rfft

torch.fft.rfft(input, n=None, dim=-1, norm=None, *, out=None)
Computes the one-dimensional Fourier transform of real-valued input. Returns only positive frequencies (Hermitian symmetry).
input
Tensor
The real input tensor.
n
int
default:"None"
Signal length.
dim
int
default:"-1"
The dimension along which to take the real FFT.

irfft

torch.fft.irfft(input, n=None, dim=-1, norm=None, *, out=None)
Computes the inverse of rfft. Input is interpreted as a one-sided Hermitian signal.
input
Tensor
The input tensor representing a half-Hermitian signal.
n
int
default:"None"
Output signal length. Defaults to even output: n=2*(input.size(dim) - 1).

2D FFT Functions

fft2

torch.fft.fft2(input, s=None, dim=(-2, -1), norm=None, *, out=None)
Computes the 2-dimensional discrete Fourier transform of input.
input
Tensor
The input tensor.
s
Tuple[int]
default:"None"
Signal size in the transformed dimensions. If given, each dimension dim[i] will either be zero-padded or trimmed to the length s[i].
dim
Tuple[int]
default:"(-2, -1)"
Dimensions to be transformed. Default: last two dimensions.
norm
str
default:"None"
Normalization mode.

ifft2

torch.fft.ifft2(input, s=None, dim=(-2, -1), norm=None, *, out=None)
Computes the 2-dimensional inverse discrete Fourier transform of input.

rfft2

torch.fft.rfft2(input, s=None, dim=(-2, -1), norm=None, *, out=None)
Computes the 2-dimensional Fourier transform of real input.

irfft2

torch.fft.irfft2(input, s=None, dim=(-2, -1), norm=None, *, out=None)
Computes the inverse of rfft2.

N-Dimensional FFT Functions

fftn

torch.fft.fftn(input, s=None, dim=None, norm=None, *, out=None)
Computes the N-dimensional discrete Fourier transform of input.
input
Tensor
The input tensor.
s
Tuple[int]
default:"None"
Signal size in the transformed dimensions.
dim
Tuple[int]
default:"None"
Dimensions to be transformed. Default: all dimensions.

ifftn

torch.fft.ifftn(input, s=None, dim=None, norm=None, *, out=None)
Computes the N-dimensional inverse discrete Fourier transform of input.

rfftn

torch.fft.rfftn(input, s=None, dim=None, norm=None, *, out=None)
Computes the N-dimensional Fourier transform of real input.

irfftn

torch.fft.irfftn(input, s=None, dim=None, norm=None, *, out=None)
Computes the inverse of rfftn.

Hermitian FFT Functions

hfft

torch.fft.hfft(input, n=None, dim=-1, norm=None, *, out=None)
Computes the one-dimensional discrete Fourier transform of a Hermitian symmetric input signal.

ihfft

torch.fft.ihfft(input, n=None, dim=-1, norm=None, *, out=None)
Computes the inverse of hfft.

Helper Functions

fftfreq

torch.fft.fftfreq(n, d=1.0, *, out=None, dtype=None, device=None)
Computes the discrete Fourier Transform sample frequencies.
n
int
The FFT length.
d
float
default:"1.0"
The sampling length scale. The spacing between individual samples of the FFT input.
Returns
Tensor
Tensor of length n containing the sample frequencies

rfftfreq

torch.fft.rfftfreq(n, d=1.0, *, out=None, dtype=None, device=None)
Computes the sample frequencies for rfft with a signal of size n.

fftshift

torch.fft.fftshift(input, dim=None)
Reorders n-dimensional FFT output to have negative frequency terms first.
input
Tensor
The input tensor.
dim
int or Tuple[int]
default:"None"
The dimension(s) to shift. Default: all dimensions.

ifftshift

torch.fft.ifftshift(input, dim=None)
Inverse of fftshift.

Example Usage

import torch
import torch.fft

# Create a signal
t = torch.arange(0, 1, 0.01)
signal = torch.sin(2 * torch.pi * 5 * t) + torch.sin(2 * torch.pi * 10 * t)

# Compute FFT
fft_result = torch.fft.fft(signal)

# Get magnitude spectrum
magnitude = torch.abs(fft_result)

# Get frequencies
freqs = torch.fft.fftfreq(len(t), d=0.01)

print(f"Signal shape: {signal.shape}")
print(f"FFT result shape: {fft_result.shape}")
print(f"Frequencies shape: {freqs.shape}")

Normalization Modes

Different normalization modes affect how the FFT scales the output:
  • “backward” (default): No normalization on forward transform, 1/n on inverse
  • “forward”: 1/n normalization on forward transform, none on inverse
  • “ortho”: 1/sqrt(n) on both forward and inverse (orthonormal)
The choice affects energy preservation and should be consistent between forward and inverse transforms.
  • Use rfft for real-valued signals - it’s more efficient (about 2x faster)
  • rfft returns only positive frequencies due to Hermitian symmetry
  • Always use irfft to invert rfft results
  • When reconstructing with irfft, specify n parameter for odd-length signals
  • FFT is fastest when signal length is a power of 2
  • For batched operations, put the batch dimension first
  • Use rfft for real signals to save memory and computation
  • Consider using torch.fft.fftshift for visualization but not for filtering