Skip to content

About

MATLAB demo of the Nyquist theorem — compares FFT spectra of an audio signal at its original, undersampled (aliased), and oversampled rates to show how sampling below Nyquist distorts frequency content.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Undersampling & Aliasing: Frequency-Response Distortion Analysis

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.

MATLAB License


Contents

Why

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.

How it works

  1. Load a .wav file and compute its FFT spectrum at the original sampling rate.
  2. 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.
  3. Oversample using resample(audioIn, 2, 1), which doubles the sampling rate properly (with an anti-imaging filter), as a clean point of comparison.
  4. Plot all three spectra as subplots of the same figure and save it to results/.

Results

Original vs. undersampled vs. oversampled spectrum

Over and Under Sampling 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.

Running it

  1. Place any .wav file in the project root, and either rename it to clean_23294917003.wav or edit the audio_file variable at the top of the script to match your filename.
  2. Open MATLAB (R2021a or later).
  3. Run src/undersampling_aliasing.m.

This generates the 3-panel comparison figure and (re)saves it to results/01_over_under_sampling_spectrum.png.

Repository structure

.
├── src/
│   └── undersampling_aliasing.m   # Loads audio, computes original/undersampled/oversampled spectra
├── results/
│   └── 01_over_under_sampling_spectrum.png
├── LICENSE
└── README.md

What I'd add next

  • 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

License

MIT — see LICENSE.

About

MATLAB demo of the Nyquist theorem — compares FFT spectra of an audio signal at its original, undersampled (aliased), and oversampled rates to show how sampling below Nyquist distorts frequency content.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages