Fast Fourier Transform (FFT)
Thetorch.fft module provides Fast Fourier Transform operations for signal processing and spectral analysis.
1D FFT Functions
fft
input.
The input tensor.
Signal length. If given, the input will either be zero-padded or trimmed to this length before computing the FFT.
The dimension along which to take the one-dimensional FFT.
Normalization mode. Options:
"forward", "backward", "ortho"."forward": normalize by1/n"backward": no normalization (default)"ortho": normalize by1/sqrt(n)
Complex-valued tensor containing the FFT result
ifft
input.
The input tensor.
Signal length.
The dimension along which to take the one-dimensional IFFT.
Normalization mode. Default is
"backward" which normalizes by 1/n.rfft
input. Returns only positive frequencies (Hermitian symmetry).
The real input tensor.
Signal length.
The dimension along which to take the real FFT.
irfft
rfft. Input is interpreted as a one-sided Hermitian signal.
The input tensor representing a half-Hermitian signal.
Output signal length. Defaults to even output:
n=2*(input.size(dim) - 1).2D FFT Functions
fft2
input.
The input tensor.
Signal size in the transformed dimensions. If given, each dimension
dim[i] will either be zero-padded or trimmed to the length s[i].Dimensions to be transformed. Default: last two dimensions.
Normalization mode.
ifft2
input.
rfft2
input.
irfft2
rfft2.
N-Dimensional FFT Functions
fftn
input.
The input tensor.
Signal size in the transformed dimensions.
Dimensions to be transformed. Default: all dimensions.
ifftn
input.
rfftn
input.
irfftn
rfftn.
Hermitian FFT Functions
hfft
input signal.
ihfft
hfft.
Helper Functions
fftfreq
The FFT length.
The sampling length scale. The spacing between individual samples of the FFT input.
Tensor of length
n containing the sample frequenciesrfftfreq
rfft with a signal of size n.
fftshift
The input tensor.
The dimension(s) to shift. Default: all dimensions.
ifftshift
fftshift.
Example Usage
Normalization Modes
Understanding Normalization
Understanding Normalization
Different normalization modes affect how the FFT scales the output:
- “backward” (default): No normalization on forward transform,
1/non inverse - “forward”:
1/nnormalization on forward transform, none on inverse - “ortho”:
1/sqrt(n)on both forward and inverse (orthonormal)
Real vs Complex FFT
Real vs Complex FFT
- Use
rfftfor real-valued signals - it’s more efficient (about 2x faster) rfftreturns only positive frequencies due to Hermitian symmetry- Always use
irfftto invertrfftresults - When reconstructing with
irfft, specifynparameter for odd-length signals
Performance Tips
Performance Tips
- FFT is fastest when signal length is a power of 2
- For batched operations, put the batch dimension first
- Use
rfftfor real signals to save memory and computation - Consider using
torch.fft.fftshiftfor visualization but not for filtering