fn

rfftfreq

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

Frequency bin centres for the output of rfft / rfftn.

Returns a 1-D tensor of length n/2+1\lfloor n/2 \rfloor + 1 containing the non-negative discrete frequencies corresponding to the output bins of a length-n real DFT. Because rfft exploits conjugate symmetry and returns only the unique half of the spectrum, all returned frequencies are non-negative:

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

Parameters

nint
Length of the original (real) signal, not the length of the rfft output. Must be positive.
dfloat= 1.0
Sample spacing (inverse of the sample rate). Default is 1.0 (cycles per sample). Pass d = 1/fs to get frequencies in Hz.
dtypeDTypeLike= None
Data type of the output tensor.
deviceDeviceLike= None
Device on which to allocate the output tensor.

Returns

Tensor

1-D float tensor of shape (n/2+1,)(\lfloor n/2 \rfloor + 1,) containing the non-negative frequency bin centres, in ascending order from 0 to the Nyquist frequency 1/(2d)1/(2d).

Notes

Non-negative only — unlike fftfreq, rfftfreq returns only non-negative frequencies. The last bin is the Nyquist frequency fn//2=1/(2d)=fs/2f_{n//2} = 1/(2d) = f_s/2. This is because for a real input the negative-frequency bins are redundant (they are the complex conjugates of the positive-frequency bins).

Bin count — for even nn, there are n/2+1n/2 + 1 bins (including DC and Nyquist); for odd nn, there are (n+1)/2(n+1)/2 bins (including DC but no exact Nyquist bin).

Monotone ordering — the output is always in ascending order, making it directly usable as an xx-axis for power spectra. No fftshift is needed.

Examples

Frequencies for a length-8 real DFT:
>>> lucid.fft.rfftfreq(8)
# [0., 0.125, 0.25, 0.375, 0.5]   — shape (5,)
Frequencies in Hz at 44100 Hz sample rate:
>>> fs = 44100.0
>>> freqs = lucid.fft.rfftfreq(2048, d=1.0/fs)
>>> freqs.shape
(1025,)
>>> # freqs[-1] ≈ 22050.0 Hz  (Nyquist)
Pair with rfft output:
>>> x = lucid.randn(1024)
>>> X = lucid.fft.rfft(x)
>>> freqs = lucid.fft.rfftfreq(1024)
>>> # len(freqs) == X.shape[-1] == 513