A MATLAB study of what actually happens to an audio signal's spectrum when it's sampled below its Nyquist rate — comparing the original, undersampled, and oversampled spectra via FFT, instead of just citing the theorem and moving on.
The Nyquist theorem (fs ≥ 2 * f_max for perfect reconstruction) is one of those results everyone can state but few actually watch happen. I wanted to see the failure mode directly: take a real audio signal, deliberately halve its sampling rate without any anti-aliasing filter, and look at what the FFT actually does to the spectrum — rather than just trusting the theorem on faith.
Comparing it against an oversampled version of the same signal (using MATLAB's resample, which does apply proper anti-imaging filtering) makes the contrast concrete: one version distorts the frequency content, the other doesn't, and the only difference is the sampling rate relative to the signal's bandwidth.
- Load a
.wavfile and compute its FFT spectrum at the original sampling rate. - Undersample by simply decimating every other sample (
audioIn(1:2:end)), halving the effective sampling rate with no anti-aliasing lowpass filter first — this is the naive case that actually causes aliasing. - Oversample using
resample(audioIn, 2, 1), which doubles the sampling rate properly (with an anti-imaging filter), as a clean point of comparison. - Plot all three spectra as subplots of the same figure and save it to
results/.
Original vs. undersampled vs. oversampled spectrum
For this particular signal, most of the energy is concentrated well below the new (halved) Nyquist frequency to begin with, so the undersampled spectrum's shape looks broadly similar to the original rather than showing an obvious spectral peak folded to the wrong location — the compression on the frequency axis is visible, but a dramatic misplaced peak isn't, which is itself a useful (if less dramatic) result: aliasing severity depends entirely on how much of the signal's real energy sits above the new Nyquist frequency, not just on whether you undersampled at all. A signal with more high-frequency content (or a deliberately injected high-frequency tone) would show the effect far more obviously — see What I'd add next.
- Place any
.wavfile in the project root, and either rename it toclean_23294917003.wavor edit theaudio_filevariable at the top of the script to match your filename. - Open MATLAB (R2021a or later).
- Run
src/undersampling_aliasing.m.
This generates the 3-panel comparison figure and (re)saves it to results/01_over_under_sampling_spectrum.png.
.
├── src/
│ └── undersampling_aliasing.m # Loads audio, computes original/undersampled/oversampled spectra
├── results/
│ └── 01_over_under_sampling_spectrum.png
├── LICENSE
└── README.md
- Test with a synthetic signal (a couple of known sine tones above and below the new Nyquist frequency) alongside the real audio, so the "folded to the wrong frequency" effect is unambiguous instead of dependent on how much high-frequency energy the specific recording happens to have
- Add an anti-aliasing lowpass filter before decimation as a fourth comparison case, to show the difference between undersampling with and without proper filtering — right now the project only shows the "without" (worst) case
- Quantify the distortion numerically (e.g. spectral energy above the new Nyquist frequency before/after) instead of relying on eyeballing the plots
- Test at a sampling rate that sits exactly at the signal's actual Nyquist rate, not just at half and double the original — the current three cases don't include this in-between case
MIT — see LICENSE.
