Sampling & Aliasing
Aliasing is an (undesired) effect in digital systems, caused by frequencies that are higher than the Nyqist frequency.
Aliasing¶
Since the spectrum of a sampled signal is periodic, it must be band-limited, in order to avoid misinterpretations, known as aliasing. Since the spectrum is periodic with $\omega_s$, the maximum frequency which can be represented - the Nyquist frequency - is:
$f_N = \frac{f_s}{2}$
As soon as components of a digitally sampled signal exceed this boundary, aliases occur. The following example can be used to set the frequency of a sine wave beyond the Nyquist frequency, resulting in aliasing and ambiguity, visualized in the time domain. The static version of the following example shows the time-domain signal of a $900 \ \mathrm{Hz}$ sinusoid at a sampling rate $f_s = 1000 \ \mathrm{Hz}$:
Aliasing Frequency for a Sinusoid¶
For a pure sinusoids with the frequency $f$, aliasing results in a sinusoid at the mirror frequency $f_m$:
$f_m = \Big| f - f_s \Big\lfloor \frac{f}{f_s} \Big\rfloor \Big|$
With $\lfloor x \rfloor$ as round to next integer.
At a sampling rate $f_s = 1000 \ \mathrm{Hz}$ and a Nyquist frequency $f_N = 500 \ \mathrm{Hz}$, a sinusoid with $f = 900 \ \mathrm{Hz}$ will be interpreted as one with $f = 100 \ \mathrm{Hz}$:
The following example can be used interactively as a Jupyter notebook, by changing the frequency of a sinusoid and listening to the aliased output. When sweeping the range up to $2500 \ \mathrm{Hz}$, the resulting output will increase and decrease in frequency. In the static version, a sinusoid of $f = 900 \ \mathrm{Hz}$ is used, resulting in an audible output at $f_m = 100 \ \mathrm{Hz}$:
Aliasing for Signals with Overtones¶
For signals with overtones, undersampling leads to inharmonic alisases and happen before the fundamental itself exceeds the Nyquist frequency. For a harmonic signal with a fundamental frequeny $f_0$, the alias frequencies for all $N$ harmonics can be calculated:
$$f_m = \sum\limits_{n=1}^{N} \Big| n f_0 - f_s \Big\lfloor \frac{n f_0}{f_s} \Big\rfloor \Big|$$
For certain fundamental frequencies, all aliases will be located at actual multiples of the fundamental, resulting in a correct synthesis despite aliasing. The following example uses a sampling rate $f_s = 16000 \ \mathrm{Hz}$, with an adjustable $f_0$ for the use as a Jupyter notebook. In the static HTML version, a square wave of $f_0 = 277 \ \mathrm{Hz}$ is used and the result with aliasing artefacts can be heard. The plot shows the additional content caused by the mirror frequencies:
Anti-Aliasing Filter¶
In analog-to-digital conversion, simple anti-aliasing filters can be used to band-limit the input and discard signal components above the Nyquist frequency. In case of digital synthesis, however, this principle can not be applied. When generating a square wave signal with an infinite number of harmonics, aliasing happens instantaneoulsy and can not be removed, afterwards.
The following example illustrates this, by using a 5th order Butterworth filter with a cutoff frequency of $f_c = 0.95 \frac{f_s}{2}$. Although the output signal is band-limited, the aliasing artifacts are still contained in the output signal.
Band Limited Synthesis¶
In order to avoid aliasing problems, most environments for audio signal processing and sound synthesis offer band-limited oscillators for specific waveforms. Users thus do not need to worry about this issue. The following example makes use of a Fourier series for generating a square wave signal with $f_0 = 177 \ \mathrm{Hz}$. Partials exceeding the Nyquist frequency can thus be excluded from synthesis: