# Sampling & Aliasing: Theory and Math

## Sampling¶

### Impulse Train¶

The representation of signals through equidistant, quantized values is the fundamental principle of digital sound processing and has an influence on several aspects of sound synthesis. Mathematically, a continuous signal $x(t)$ is sampled by a multiplication with an impulse train $\delta_T(t)$ (also referred to as Dirac comb) of infitite length:

$x[n] = x(t) \delta_T (t) = \sum\limits_{n=-\infty}^{\infty} x(n T) \delta (t-nT)$

This impulse train can be expressed as a Fourier series:

$\delta_T = \frac{1}{T} \left[1 +2 \cos(\omega_s t) 2 \cos(2 \omega_s t) + \cdots \right]$, with $\omega_s=\frac{2\pi}{T}$

$\delta_T = \frac{1}{T} + \sum \left( \frac{2}{T} \cos(n \omega_s t) \right)$

### Fourier Transform of the Impulse Train¶

The Fourier transform of a time-domain impulse train is a frequency-domain impulse train:

$\mathfrak{F}(\delta_T) = \mathfrak{F}(\sum C_k e^{j k \omega_0 t})$

$\mathfrak{F}(\delta_T) = \sum\limits_{m = -\infty}^{\infty} \delta(T f)$

### Fourier Transform of the Sampled Signal¶

The Fourier transform of the sampled signal is periodic:

$X[i] = \frac{1}{T} + \sum\limits_{n=-\infty}^{\infty} X(\omega -n \omega_s)$

## 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 pure sinusoids, aliasing results in a sinusoid at the mirror- or folding 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}$:

f_m = 100

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 Square Wave¶

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: