fn

fft2

Tensor
fft2(input: Tensor, s: Sequence[int] | None = None, dim: Sequence[int] = (-2, -1), norm: str | None = None)
source

2-D discrete Fourier transform over a pair of axes.

Computes the two-dimensional DFT of the input tensor by applying fft along the two axes specified by dim (default: the last two axes). For an M×NM \times N input array, the 2-D DFT is:

X[p,q]=m=0M1n=0N1x[m,n]ei2π(pm/M+qn/N),X[p, q] = \sum_{m=0}^{M-1} \sum_{n=0}^{N-1} x[m, n]\, e^{-i 2\pi (pm/M + qn/N)},

where p=0,,M1p = 0, \ldots, M-1 and q=0,,N1q = 0, \ldots, N-1.

This is equivalent to calling fftn(input, s=s, dim=dim, norm=norm) with a two-element dim — it is provided as a convenience for the common case of 2-D signals such as images or spatial fields.

Parameters

inputTensor
Input tensor with at least 2 dimensions. May be real or complex.
ssequence of int= None
Output size (M_out, N_out) along each transformed axis. The input is zero-padded or truncated to this size before the transform. len(s) must be 2 when given. If None, the axes are transformed at their current sizes.
dimsequence of int= (-2, -1)
The two axes over which to compute the transform. Default is (-2, -1) (the last two axes).
normstr or None= None
Normalisation mode — "backward" (default), "forward", or "ortho". NN in the scaling formula is the product Mout×NoutM_{\text{out}} \times N_{\text{out}}.

Returns

Tensor

Complex tensor (complex64) with the same shape as input except that the two transformed axes have lengths s[0] and s[1] (or the original axis sizes when s is None).

Notes

Spatial frequency interpretation — in image processing, the 2-D DFT maps a spatial image x[m,n]x[m, n] to its spatial-frequency content X[p,q]X[p, q]. The bin (p,q)=(0,0)(p, q) = (0, 0) is the DC component (average pixel value times MNMN). Radial distance from the origin in the frequency plane corresponds to spatial frequency magnitude; fftshift is typically applied before displaying the spectrum so that low frequencies sit at the centre.

Separability — the 2-D DFT is separable: the row-wise 1-D DFTs followed by the column-wise 1-D DFTs yield the same result as a single 2-D DFT. The engine exploits this to run two passes of the 1-D algorithm.

Examples

2-D DFT of a real image-like tensor:
>>> x = lucid.randn(64, 64)
>>> X = lucid.fft.fft2(x)
>>> X.shape
(64, 64)
Zero-pad to a larger grid for spectral interpolation:
>>> X_padded = lucid.fft.fft2(x, s=[128, 128])
>>> X_padded.shape
(128, 128)
Centre the spectrum for visualisation:
>>> X_shifted = lucid.fft.fftshift(lucid.fft.fft2(x))