fn

fftfreq

Tensor
fftfreq(n: int, d: float = 1.0, dtype: DTypeLike = None, device: DeviceLike = None)
source

Frequency bin centres for the output of fft / fftn.

Returns a 1-D tensor of length n containing the normalised discrete frequencies corresponding to the output bins of a length-n DFT computed with sample spacing d. The values follow the standard FFT frequency ordering:

fk=knd,k{0,1,,n/21,n/2,,1}.f_k = \frac{k}{n \cdot d}, \quad k \in \{0, 1, \ldots, \lfloor n/2 \rfloor - 1, -\lfloor n/2 \rfloor, \ldots, -1\}.

Equivalently the output tensor is:

.. code-block:: text

[0, 1, 2, ..., n/2-1, -n/2, ..., -2, -1] / (d * n) # n even [0, 1, 2, ..., (n-1)/2, -(n-1)/2, ..., -1] / (d * n) # n odd

Parameters

nint
Window length (number of DFT points). Must be positive.
dfloat= 1.0
Sample spacing (inverse of the sample rate). Default is 1.0, which gives frequencies in units of cycles per sample. Pass d = 1/fs where fs is the sample rate in Hz to get frequencies in Hz.
dtypeDTypeLike= None
Data type of the output tensor. Default uses the framework's default floating-point type.
deviceDeviceLike= None
Device on which to allocate the output tensor.

Returns

Tensor

1-D float tensor of shape (n,) containing the frequency bin centres in the same order as the DFT output.

Notes

Physical frequency interpretation — for a signal sampled at rate fs=1/df_s = 1/d:

  • The first bin (k=0k=0) is DC (zero frequency).
  • Bins k=1,,n/21k = 1, \ldots, \lfloor n/2 \rfloor - 1 are positive frequencies kfs/nk f_s / n.
  • Bin k=n/2k = \lfloor n/2 \rfloor is the Nyquist frequency fs/2f_s / 2 (for even nn). This is the highest representable frequency without aliasing.
  • Bins k=n/2+1,,n1k = \lfloor n/2 \rfloor + 1, \ldots, n-1 are aliased negative frequencies.

Centred display — to get frequencies in monotonically increasing order (centred at zero), apply fftshift to both the frequency vector and the DFT output.

Closed-form implementationfftfreq is computed as a closed-form expression over lucid.arange; no external libraries are required.

Examples

Frequencies for a length-8 DFT at sample spacing 1:
>>> lucid.fft.fftfreq(8)
# [ 0.,   0.125,  0.25,  0.375, -0.5,  -0.375, -0.25, -0.125]
Frequencies in Hz for a 1 kHz sample rate:
>>> fs = 1000.0
>>> freqs = lucid.fft.fftfreq(256, d=1.0/fs)
>>> freqs.shape
(256,)
>>> # freqs[0] == 0.0,  freqs[128] == -500.0 (Nyquist)
Centred frequency axis for plotting:
>>> freqs_centred = lucid.fft.fftshift(lucid.fft.fftfreq(256, d=1.0/1000.0))
>>> # Now ranges from -500 Hz to ~496 Hz, centred at 0