FFT of a Sampled Signal
Sample a two-tone signal, take its discrete Fourier transform, and read the frequency peaks and bin resolution directly.
Problem
The fast Fourier transform (FFT) converts a sampled time series into its frequency content in O(N log N) operations. We build a signal from two sine tones plus noise and recover their frequencies from the magnitude spectrum.
Sampling
Sample at fs=1000 Hz for N=1000 points, giving a frequency resolution of fs/N = 1 Hz per bin and a Nyquist limit of 500 Hz. The signal contains a 50 Hz and a 120 Hz tone. Real FFT output has N/2+1 usable bins.
import numpy as np
fs=1000; N=1000; t=np.arange(N)/fs
x=np.sin(2*np.pi*50*t)+0.5*np.sin(2*np.pi*120*t)+0.2*np.random.default_rng(4).normal(size=N)
X=np.fft.rfft(x); f=np.fft.rfftfreq(N,1/fs)
mag=np.abs(X)/N*2
peaks=f[np.argsort(mag)[-2:]]
print('resolution Hz',fs/N)
print('detected peaks',sorted(peaks)) # ~50 and 120
Result
The two largest magnitude bins sit at 50 and 120 Hz, and the amplitude scaling 2/N recovers heights near 1.0 and 0.5, matching the input. A tone that falls between bins would smear across neighbors, an effect called spectral leakage that windowing reduces.
- Frequency resolution is set by total record length, not sampling rate: longer records give finer bins.
- Any frequency above the Nyquist limit aliases into a false lower frequency, so anti-alias filtering matters before sampling.
- Kronos processes magnetic and interferometer diagnostics with FFTs to identify MHD mode frequencies in the plasma.