fn

rfft

Tensor
rfft(input: Tensor, n: int | None = None, dim: int = -1, norm: str | None = None)
source

1-D FFT of a real-valued input along a single axis.

Computes the one-dimensional DFT of a real signal and returns only the non-redundant complex output bins. For a real input of length NN, the DFT satisfies X[Nk]=X[k]X[N-k] = X^*[k], so only N/2+1\lfloor N/2 \rfloor + 1 unique complex values need to be stored.

The mathematical definition is identical to fft:

X[k]=n=0N1x[n]ei2πkn/N,k=0,1,,N/2.X[k] = \sum_{n=0}^{N-1} x[n]\, e^{-i 2\pi k n / N}, \quad k = 0, 1, \ldots, \lfloor N/2 \rfloor.

Parameters

inputTensor
Real-valued input tensor of any shape.
nint= None
Number of input samples used (along dim). The axis is zero-padded or truncated to this length before the transform. If None, the full axis length is used.
dimint= -1
Axis over which to compute the transform. Default is -1 (last axis).
normstr or None= None
Normalisation mode — "backward" (default), "forward", or "ortho". See fftn for the full description.

Returns

Tensor

Complex tensor (complex64) with the same shape as input except that axis dim has length n/2+1\lfloor n/2 \rfloor + 1 (where nn is the effective input length along that axis).

Notes

Output size — for an input axis of length NN the output has N//2+1N//2 + 1 complex bins:

  • Bin 0 is the DC component (purely real for a real input).
  • Bins 1 through N//21N//2 - 1 are complex (positive frequencies).
  • Bin N//2N//2 is the Nyquist component (purely real for even NN).

Inverse — use irfft to reconstruct the real signal. You must pass the original length n to resolve the even/odd ambiguity when the output of rfft has an odd number of bins.

Examples

Real FFT of a length-128 signal:
>>> x = lucid.randn(128)
>>> X = lucid.fft.rfft(x)
>>> X.shape   # 128 // 2 + 1 = 65
(65,)
Round-trip:
>>> x_rec = lucid.fft.irfft(X, n=128)
>>> x_rec.shape
(128,)