torch.fft.rfftfreq
function rfftfreq(n: number): Tensorfunction rfftfreq(n: number, d: number, options: TensorOptions): TensorComputes the sample frequencies for rfft() output (real-valued FFT).
Returns the positive frequency values for the one-sided spectrum produced by rfft(). Unlike fftfreq which includes negative frequencies, rfftfreq only returns non-negative frequencies, since real-valued signals have conjugate symmetry. Essential for:
- Real signal analysis: Frequency labels for rfft() results
- Efficient spectral processing: Avoiding redundant negative frequency bins
- Frequency domain visualization: Plotting one-sided power spectral density
- Peak detection: Finding actual frequencies in real signals
- Nyquist handling: Understanding the Nyquist frequency at rfft output
The output length is n//2 + 1, covering frequencies from 0 to the Nyquist frequency. This is much more efficient than fftfreq for real signals where negative frequencies are redundant.
- One-sided spectrum: rfftfreq produces only positive frequencies (0 to Nyquist). This is half the length of fftfreq for the same signal length.
- Real signal efficiency: For real-valued signals, rfft is about 2x faster than fft because it exploits conjugate symmetry. Always use rfft for real signals.
- Nyquist at last index: The Nyquist frequency (1/(2d)) appears at the last index. For even n, this is exactly at the Nyquist limit. For odd n, it's slightly below.
- Zero frequency at index 0: The DC component is always at index 0, representing the mean of the signal.
- Output length: For a signal of length n, rfftfreq returns n//2 + 1 frequencies. This is one more than n//2 to include both 0 and Nyquist frequencies.
- Only positive frequencies: Do NOT try to use this with fft() output or expect negative frequencies. This is specifically for rfft() results.
- Conjugate symmetry: Real signals have conjugate symmetry in their FFT. The negative frequency components are automatic from the conjugate symmetry.
- Sample spacing d: Make sure d matches your actual sample spacing. Wrong d value produces wrong frequency values.
Parameters
nnumber- Window length (number of samples in original real-valued signal)
Returns
Tensor– 1D tensor of length (n//2 + 1) containing non-negative frequencies: - Shape: [n//2 + 1] - Ranges from 0 to 1/(2d) (Nyquist frequency) - Only positive and zero frequencies, no negative frequencies - Dtype: float32 by defaultExamples
// One-sided frequency spectrum for 8-sample signal
const freqs = torch.fft.rfftfreq(8);
// [0, 0.125, 0.25, 0.375, 0.5] - only 5 values vs 8 for fftfreq// Real signal FFT with proper frequency labels
const signal = torch.cos(2 * Math.PI * torch.arange(1000) / 50); // 50 Hz cosine
const rfft_result = torch.fft.rfft(signal);
const magnitude = torch.abs(rfft_result);
const freqs = torch.fft.rfftfreq(signal.shape[0]);
// Much more efficient than fftfreq
// rfftfreq: 501 values, fftfreq: 1000 values// Real signal with actual frequencies in Hz
const sampling_rate = 44100; // Audio CD quality
const duration = 0.1; // 0.1 seconds
const n_samples = Math.floor(sampling_rate * duration); // 4410 samples
const d = 1.0 / sampling_rate;
const audio = torch.randn([n_samples]);
const spectrum = torch.fft.rfft(audio);
const freqs = torch.fft.rfftfreq(n_samples, d); // In Hz
// Frequencies from 0 to 22050 Hz (Nyquist)
const nyquist = freqs[freqs.shape[0] - 1].item();// Power spectral density for real signal
const signal = torch.randn([1024]);
const spectrum = torch.fft.rfft(signal);
const power = torch.pow(torch.abs(spectrum), 2) / signal.shape[0];
const freqs = torch.fft.rfftfreq(1024);
// Plot power vs freqs for PSD
// Much more efficient than plotting all 1024 frequencies// Filtering real signal by frequency
const signal = torch.randn([512]);
const spectrum = torch.fft.rfft(signal);
const freqs = torch.fft.rfftfreq(512);
// Highpass filter: keep frequencies above 0.2
const mask = freqs >= 0.2;
const filtered_spectrum = spectrum * mask.unsqueeze(-1);
const filtered = torch.fft.irfft(filtered_spectrum);See Also
- PyTorch torch.fft.rfftfreq()
- fftfreq - Frequencies for complex FFT (two-sided spectrum)
- rfft - Real-valued FFT computation
- irfft - Inverse real FFT to reconstruct real signal