What happens to a signal when you only measure it at discrete points: the Nyquist frequency, aliasing, leakage, and rebuilding a signal from a few frequencies.
Every recording is a list of numbers taken at discrete times, and the Fourier transform is how we ask such a list which frequencies it contains. The transform itself is a one-line formula. What takes getting used to is the way sampling changes what the formula can tell you: measure too slowly and a fast oscillation shows up as a slow one, record for too short a time and a clean peak smears out. This page lets you produce those effects on purpose, with a signal whose true content you know.
Everything runs in your browser, and the figure updates as soon as you move a slider.
Code
exFont = ({size:getComputedStyle(document.body).fontSize,family:getComputedStyle(document.body).fontFamily,toString() { returnthis.size+" "+this.family; }})exFg = () =>getComputedStyle(document.body).colormkCanvas = (W, H) => {const c = DOM.context2d(W, H); c.canvas.style.width="100%"; c.canvas.style.maxWidth= W +"px"; c.canvas.style.height="auto";return c;}axisBox = (c, x, y, w, h) => { c.strokeStyle="#999"; c.lineWidth=1; c.strokeRect(x, y, w, h); }
with frequencies \(f_1\) and \(f_2\) in cycles per unit length and amplitudes \(a_1\) and \(a_2\). I write the variable as \(x\) rather than \(t\) because the same mathematics applies to a signal in space, for instance a pattern of activity along a strip of cortex, but you can read it as time throughout.
We do not get to see \(y(x)\) itself. We see \(N\) samples \(y_n = y(x_n)\) taken at points spaced \(\Delta x\) apart over an interval of length \(T\), so that \(N = T / \Delta x\). The discrete Fourier transform (DFT) of these samples is
\[Y_m = \sum_{n=0}^{N-1} y_n\, e^{-2\pi i\, m n / N},\]
and it tells us how much of each frequency \(f_m = m / T\) is present. Two facts about these frequencies are the whole point of the page.
First, the frequencies are spaced \(1/T\) apart. A longer recording gives a finer frequency grid. If a true frequency of the signal does not fall on the grid, the DFT has to represent it with a mix of neighbouring grid frequencies, and the peak spreads out. This is called leakage.
Second, the grid stops at the Nyquist frequency \(f_{\text{Ny}} = 1 / (2
\Delta x)\), half the sampling rate (Nyquist 1928; Shannon 1949). A sine wave needs at least two samples per cycle to be told apart from a slower one. If the signal contains a frequency above \(f_{\text{Ny}}\), the samples are indistinguishable from the samples of a lower frequency, and the DFT reports that lower frequency instead. This is aliasing, and once it has happened there is no way to undo it from the samples alone. The same effect makes wagon wheels appear to turn backwards in films.
The last slider, \(K\), uses the transform in the other direction. It keeps the \(K\) strongest frequencies in the spectrum, throws the rest away and rebuilds the signal from what is left. If the spectrum is clean, two components are enough to reproduce the signal exactly. If it is not, you need more.
The upper panel shows the signal. The thin grey curve is the true continuous \(y(x)\), which in a real experiment you would never get to see. The blue dots are the samples, the only thing the transform has access to. The orange curve is the signal rebuilt from the \(K\) strongest frequencies. When the orange curve lies on top of the grey one, the reconstruction has recovered the signal from its samples.
The lower panel is the amplitude spectrum. There is one stem for each grid frequency \(f_m = m/T\), with the height scaled so that a pure sine of amplitude \(a\) gives a stem of height \(a\). The red dashed line marks the Nyquist frequency, where the grid ends. The two green dashed lines mark where the true frequencies \(f_1\) and \(f_2\) ought to appear. If a true frequency lies above Nyquist, its green line is drawn at the frequency it gets aliased to, so you can see the stem land in the wrong place. Stems used in the reconstruction are drawn in orange, the rest in blue.
With the default settings everything is well behaved. Both frequencies sit on the grid, both are below Nyquist, the spectrum shows two clean stems of heights \(a_1\) and \(a_2\), and with \(K = 2\) the reconstruction is perfect.
Things to try
Raise \(f_2\) slowly past the Nyquist frequency, which is 5 cycles per unit at the default sampling interval. Just below it the stem still sits where it should. Just above it, the green line and the stem jump to a lower frequency, and the orange reconstruction now contains a slow oscillation that was never in the signal. Look at the blue dots in the upper panel while you do this: they are consistent with that slower wave. Then make the sampling interval smaller. Nyquist moves up, the fast component is resolved again, and the problem goes away. That is the one reliable cure for aliasing: sample faster, or filter out the fast components before you sample.
Set \(f_1\) to 1 and change \(T\) from 4 to 4.5. The signal has not changed, but the grid has, and 1 cycle per unit no longer lies on it. The single stem turns into a cluster around the true frequency. With \(K = 2\) the reconstruction is now visibly wrong, and you need to raise \(K\) to get the signal back. The general lesson is that the width of a peak in a spectrum is set by the recording length, not by the signal, and that a sharp peak needs a recording that is long compared to the period.
Set both amplitudes to the same value and bring \(f_1\) and \(f_2\) close together. At some point the two stems merge into one, and no setting of \(K\) separates them. The frequency resolution is \(1/T\), so two frequencies closer than that cannot be distinguished, no matter how finely you sample.
This page is based on the Fourier exercises in Dayan and Abbott (2001). The transform here is computed by the direct sum, which is fine for a few hundred samples. For anything larger one uses the fast Fourier transform of Cooley and Tukey (1965), which gives the same numbers in a fraction of the time. Press et al. (2007) is a practical reference for both the algorithm and the pitfalls of sampling.
References
Cooley JW, Tukey JW (1965). An algorithm for the machine calculation of complex Fourier series. Mathematics of Computation 19: 297–301. DOI
Dayan P, Abbott LF (2001). Theoretical Neuroscience: Computational and Mathematical Modeling of Neural Systems. MIT Press.
Nyquist H (1928). Certain topics in telegraph transmission theory. Transactions of the American Institute of Electrical Engineers 47: 617–644. DOI
Press WH, Teukolsky SA, Vetterling WT, Flannery BP (2007). Numerical Recipes: The Art of Scientific Computing, 3rd edition. Cambridge University Press.
Shannon CE (1949). Communication in the presence of noise. Proceedings of the IRE 37: 10–21. DOI