torch.fft.fftfreq
function fftfreq(n: number): Tensorfunction fftfreq(n: number, d: number, options: TensorOptions): TensorComputes the discrete Fourier Transform sample frequencies for fft() output.
Returns the frequency values corresponding to the FFT output bins. Essential for:
- Frequency domain plotting: Labeling axes when visualizing FFT results
- Frequency filtering: Identifying which frequencies correspond to which bins
- Spectral analysis: Correlating frequency bins with actual frequencies
- Peak detection: Understanding the actual frequency of detected peaks
- Nyquist calculation: Understanding frequency ranges and sampling limitations
The frequencies are arranged with positive frequencies first, then negative frequencies.
For a signal with sample spacing d, frequencies range from 0 to 1/(2d) (Nyquist frequency),
then negative frequencies from -1/(2d) to 0.
- Frequency arrangement: Positive frequencies come first (0 to N/2), then negative frequencies (-N/2 to -1). This is the standard FFT bin ordering.
- Zero frequency at index 0: The DC component (zero frequency) is always at index 0. This represents the mean of the signal.
- Negative frequencies: These are mathematical artifacts of the FFT that represent the conjugate symmetry property. For real-valued signals, use rfftfreq instead.
- Nyquist frequency: For even n, the Nyquist frequency (±1/(2d)) appears at indices n/2. This is the highest frequency that can be accurately represented.
- Sample spacing d: The parameter d scales all frequencies by 1/d. Setting d=1/fs (where fs is sampling rate in Hz) gives frequencies in Hz.
- Complex conjugate symmetry: For real-valued input, negative frequency bins are conjugate copies of positive frequency bins. Don't process them independently.
- Aliasing: If your signal contains frequencies above Nyquist (1/(2d)), they will alias to lower frequencies. Use low-pass filtering before FFT if needed.
- Spacing parameter 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 signal)
Returns
Tensor– 1D tensor of length n containing the frequencies: - Shape: [n] - Positive frequencies: indices 0 to n//2 - Negative frequencies: indices n//2+1 to n-1 - Dtype: float32 by defaultExamples
// Basic frequency computation for 8-sample signal
const freqs = torch.fft.fftfreq(8);
// [0, 0.125, 0.25, 0.375, -0.5, -0.375, -0.25, -0.125]// Frequencies for non-unit sample spacing
const sampling_rate = 1000; // 1000 Hz
const n_samples = 1000;
const d = 1.0 / sampling_rate; // Sample spacing
const freqs = torch.fft.fftfreq(n_samples, d);
// Frequencies now in Hz: 0, 1, 2, ..., 500, -500, ..., -1// Plotting FFT with proper frequency labels
const signal = torch.sin(2 * Math.PI * torch.arange(1000) / 100);
const fft_result = torch.fft.fft(signal);
const magnitude = torch.abs(fft_result);
const freqs = torch.fft.fftfreq(signal.shape[0]);
// Find peak frequency
const max_idx = torch.argmax(magnitude);
const peak_freq = freqs[max_idx.item()]; // Actual frequency of peak// Zero-padding affects frequency resolution
const signal = torch.randn([100]);
const padded = torch.cat([signal, torch.zeros([900])], 0); // Pad to 1000
const fft = torch.fft.fft(padded);
const freqs = torch.fft.fftfreq(padded.shape[0]); // 1000 frequency bins
// Higher resolution (0.001 spacing) vs original (0.01 spacing)// Bandpass filtering using frequency information
const signal = torch.randn([512]);
const fft_sig = torch.fft.fft(signal);
const freqs = torch.fft.fftfreq(512);
// Keep only frequencies between 0.1 and 0.3
const mask = torch.logical_and(torch.abs(freqs) >= 0.1, torch.abs(freqs) <= 0.3);
const filtered_fft = fft_sig * mask.unsqueeze(-1); // Broadcasting for complex
const filtered = torch.fft.ifft(filtered_fft);See Also
- PyTorch torch.fft.fftfreq()
- rfftfreq - Frequencies for real-valued FFT (one-sided spectrum)
- fft - Forward FFT computation
- ifft - Inverse FFT to reconstruct signal