fn

fft

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

1-D discrete Fourier transform along a single axis.

Computes the one-dimensional DFT of the input tensor along the axis dim. For an input sequence x[0],x[1],,x[N1]x[0], x[1], \ldots, x[N-1], the output is:

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

The output bin X[0]X[0] is the DC component (sum of all samples); X[1]X[1] through X[N/2]X[\lfloor N/2 \rfloor] are the positive frequencies; X[N/2+1]X[\lfloor N/2 \rfloor + 1] through X[N1]X[N-1] are the negative frequencies.

Parameters

inputTensor
Input tensor of any shape. May be real or complex.
nint= None
Length of the transformed axis. If larger than the input axis, the input is zero-padded; if smaller, it is truncated. If None, the axis length is used as-is.
dimint= -1
Axis over which to compute the transform. Default is -1 (last axis). Negative indices are supported.
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 (or the original length when n is None).

Notes

Computational efficiency — under the hood this calls fftn restricted to one axis, which in turn delegates to the MLX/Accelerate FFT kernel. For a length-NN axis where NN is a power of two, the Cooley–Tukey radix-2 algorithm is used, reducing the O(N2)O(N^2) DFT to O(NlogN)O(N \log N).

DC and Nyquist — for an even-length transform of a real signal:

  • X[0] is real and equals the mean of x times NN.
  • X[N//2] is real and is the Nyquist component.
  • All other bins come in conjugate pairs: X[N-k] == conj(X[k]).

Use rfft to exploit this conjugate symmetry and halve the output size.

Examples

DFT of a pure cosine at the third harmonic:
>>> N = 16
>>> k0 = 3
>>> n = lucid.arange(N)
>>> # x[n] = cos(2π k0 n / N) — should spike at bins k0 and N-k0
>>> x = lucid.cos(n * (2 * 3.14159 * k0 / N))
>>> X = lucid.fft.fft(x)
>>> X.shape
(16,)
Zero-padding to the next power of two for spectral interpolation:
>>> x = lucid.randn(100)
>>> X = lucid.fft.fft(x, n=128)   # pads from 100 → 128
>>> X.shape
(128,)