fn

fftn

Tensor
fftn(input: Tensor, s: int | Sequence[int] | None = None, dim: int | Sequence[int] | None = None, norm: str | None = None)
source

N-dimensional discrete Fourier transform.

Computes the N-dimensional discrete Fourier transform (DFT) by applying 1-D FFTs over each of the specified axes in succession. For a single axis of length NN, the DFT is defined as:

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.

For multiple axes the transforms are applied sequentially, one axis at a time (the final result is equivalent to a single multi-dimensional transform).

The output is always a complex tensor (complex64). Its shape is identical to input except along the transformed axes, where axis i has length s[i] when s is provided (the input is zero-padded or truncated to that size first).

Parameters

inputTensor
Input tensor of any shape. May be real or complex.
sint or sequence of int= None
Signal length(s) along each transformed axis. When given, each axis is zero-padded (if s[i] > input.shape[dim[i]]) or truncated (if smaller) before the transform. len(s) must equal len(dim) when both are sequences. If None, each axis is transformed at its current size.
dimint or sequence of int= None
Axis or axes over which to compute the transform. Negative indices are supported. Defaults to all axes when None.
normstr or None= None
Normalisation mode. One of:
  • "backward" (default, same as None) — forward transform is unscaled; inverse divides by NN.
  • "forward" — forward transform divides by NN; inverse is unscaled.
  • "ortho" — both forward and inverse divide by N\sqrt{N}, making the transforms mutually unitary.
Here NN is the product of the lengths of all transformed axes (after padding/truncation).

Returns

Tensor

Complex tensor (complex64) with the same shape as input except that each transformed axis i has length s[i] (or input.shape[dim[i]] when s is None).

Notes

Frequency ordering — the output follows the standard FFT ordering. For an axis of length NN, the output bins represent frequencies:

fk=kN,k=0,1,,N1.f_k = \frac{k}{N}, \quad k = 0, 1, \ldots, N-1.

Bins k=0,,N/2k = 0, \ldots, \lfloor N/2 \rfloor are the non-negative frequencies; bins k=N/2+1,,N1k = \lfloor N/2 \rfloor + 1, \ldots, N-1 wrap around and represent negative frequencies (kN)/N(k-N)/N. Use fftshift to reorder so that the zero-frequency bin sits at the centre of each axis.

Autograd — the backward pass computes gradx=ifft(gradout,dual(norm))\text{grad}_x = \text{ifft}(\text{grad}_{\text{out}}, \text{dual}(\text{norm})) where dual swaps "backward" and "forward" and leaves "ortho" unchanged.

Examples

1-D DFT of a length-8 signal:
>>> x = lucid.fft.fft(lucid.ones(8))
>>> x.shape
(8,)
2-D DFT over the last two axes with explicit output sizes:
>>> x = lucid.ones(4, 4)
>>> X = lucid.fft.fftn(x, s=[8, 8], dim=[-2, -1])
>>> X.shape
(8, 8)
Orthonormal convention (``"ortho"``) makes the transform unitary:
>>> x = lucid.randn(16)
>>> X = lucid.fft.fftn(x, norm="ortho")
>>> # lucid.fft.ifftn(X, norm="ortho") recovers x exactly