fftfreq
→Tensorfftfreq(n: int, d: float = 1.0, dtype: DTypeLike = None, device: DeviceLike = None)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:
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
nintdfloat= 1.01.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= NonedeviceDeviceLike= NoneReturns
Tensor1-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 :
- The first bin () is DC (zero frequency).
- Bins are positive frequencies .
- Bin is the Nyquist frequency (for even ). This is the highest representable frequency without aliasing.
- Bins 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 implementation — fftfreq 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