Signals, sampling and aliasing

A digital signal is a list of samples. Sample too slowly and a fast wave masquerades as a slow one.

What you'll learn
  • Build test signals from sine waves and describe them with mean, variance and standard deviation
  • State the sampling theorem and compute the Nyquist frequency
  • Predict the alias frequency of an undersampled sine

A computer never sees a continuous wave, only samples taken every \(1/f_s\) seconds, where \(f_s\) is the sampling rate. 11_signal_generation.py makes its test signals this way:

t = np.linspace(0, 1.0, 2001)      # 2001 points over 1 s: spacing 1/2000, fs = 2000 Hz
signal_5Hz   = np.sin(2*np.pi*5*t)
signal_250Hz = np.sin(2*np.pi*250*t)
sig_5Hz_250Hz = signal_5Hz + signal_250Hz

Note the 2001: linspace includes both ends, so 2001 points make 2000 intervals. That off-by-one between “points” and “intervals” recurs in every frequency axis in this course.

Describing a signal with three numbers

The signal_and_noise/ scripts compute the basic statistics of the 1 kHz + 15 kHz test signal twice — once with NumPy and once with explicit loops:

for x in range(len(sig_src_arr)):
    _variance += (sig_src_arr[x] - _mean)**2
_variance = _variance/len(sig_src_arr)

Both give mean 0.0371, variance 0.6182 and standard deviation 0.7863 (dividing by \(N\), like np.var; the sample version, dividing by \(N-1\), is 0.7875). The mean is the signal’s DC level; the variance is its average power about that level. One caution for the hand-written versions: _mean and _variance are module-level globals that are never reset, so calling the function a second time adds onto the first result.

The sampling theorem

A sampled sine can only be recovered if there are more than two samples per cycle. The highest frequency a sampling rate can represent is the Nyquist frequency \(f_s/2\). Anything above it does not vanish — it aliases: its samples are identical to those of a slower sine at

\[f_{\text{alias}} = \vert f - k f_s \vert, \qquad k = \operatorname{round}(f / f_s).\]
A sine (blue) sampled at fs (dots). Push f above fs/2 and the same dots fit a slower wave (orange, dashed): the alias. At f = fs exactly the samples all land on the same value and the wave disappears.

The aliasing notebook shows the same with a 5 Hz sine measured at 10 Hz — exactly two samples per cycle, the knife edge where the samples can fall on the zero crossings and see nothing at all. The practical rule is to sample at well over twice the highest frequency present, and to filter out everything above \(f_s/2\) before sampling, because once aliased, a component cannot be told apart from a genuine low frequency.

Try it yourself. Sample np.sin(2*np.pi*9*t) at 10 Hz for one second and plot the samples over np.sin(2*np.pi*(-1)*t). They coincide: 9 Hz aliases to 1 Hz with the phase flipped. Predict, then check, where 23 Hz lands at \(f_s = 10\) Hz.

View source on GitHub →