fn

rfft2

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

2-D FFT of a real-valued input over a pair of axes.

Computes the two-dimensional DFT of a real tensor and returns only the non-redundant complex coefficients. For a real input of shape (M,N)(M, N) (along the two transformed axes), the output shape is (M,N//2+1)(M, N//2 + 1) — full along the first transformed axis and compressed along the last.

This is a convenience wrapper around rfftn restricted to two axes (default: the last two).

Parameters

inputTensor
Real-valued input tensor with at least 2 dimensions.
ssequence of int= None
Output sizes (M_out, N_out) along the two transformed axes. The last value s[-1] is the full signal length along the last axis before the real FFT; the output last axis will be s[-1] // 2 + 1. If None, current axis sizes are used.
dimsequence of int= (-2, -1)
The two axes over which to compute the transform. Default is (-2, -1). The last element receives the conjugate-symmetry compression.
normstr or None= None
Normalisation mode — "backward" (default), "forward", or "ortho".

Returns

Tensor

Complex tensor (complex64) with the same shape as input except:

  • The first transformed axis has length s[0] (or input size).
  • The last transformed axis has length s[1]/2+1\lfloor s[-1]/2 \rfloor + 1.

Notes

Typical userfft2 is the standard choice for 2-D image convolution via the convolution theorem:

(fg)[m,n]=F1{F{f}F{g}}[m,n],(f * g)[m, n] = \mathcal{F}^{-1}\{\mathcal{F}\{f\} \cdot \mathcal{F}\{g\}\}[m, n],

where F\mathcal{F} denotes the 2-D DFT. Performing the multiplication in the frequency domain reduces the cost of a full convolution from O(M2N2)O(M^2 N^2) to O(MNlog(MN))O(MN \log(MN)).

Examples

2-D real FFT of an image-like tensor:
>>> x = lucid.randn(64, 64)
>>> X = lucid.fft.rfft2(x)
>>> X.shape   # last axis: 64 // 2 + 1 = 33
(64, 33)
Round-trip:
>>> x_rec = lucid.fft.irfft2(X, s=[64, 64])
>>> x_rec.shape
(64, 64)