Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Sampling and reconstruction

Open In Colab

Central question

  • How are analog signals turned into digital representation (i.e., data) on a computer?

  • How can we analyse these digital signals (i.e. extract information from them)?

  • How can we modify these signals? How can we convert them back to analog signals?

Course overview

Sampling and reconstruction

In the next 20 minutes, you will learn

  • What a continuous-time signal is.

  • What a discrete-time signal is.

  • How you turn a continuous-time signal into a discrete-time signal (i.e., sampling).

  • How you turn a discrete-time signal into a continuous-time signal (i.e., reconstruction).

In the figure below, the following is happening:

  1. An audio signal pi(t)p_\text{i}(t) is propagating through the air as pressure variations.

  2. The microphone picks up the pressure variations and turns them into voltage variations vi(t)v_\text{i}(t).

  3. The voltage is converted into a series of numbers xnx_n via sampling.

  4. A voltage signal vo(t)v_\text{o}(t) is reconstructed from the series of numbers xnx_n.

  5. A loudspeaker converts the voltage variations into pressure variations po(t)p_\text{o}(t).

Illustration of sampling and reconstruction

Continuous-time signal

A continuous-time signal is characterised by

  • time: the signal has a value x(t)x(t) for every possible time tt

  • amplitude: the signal value x(t)x(t) can take on any value from a continuum of numbers (such as the real numbers).

A continuous-time signal is often also referred to as an analog signal.

Continuous-time signal

Informally: You draw a continuous-time signal without lifting your pen from the paper.

Sampling

  • Storing a continuous-time signal on a computer requires an infinite amount of memory!

  • Solution: We only measure the value of a continuous-time signal every TsT_\text{s} seconds. This is called sampling.

  • A very important quantity is

    fs=1/Tsf_\text{s} = 1/T_\text{s}

    where

    • fsf_\text{s} is the sampling frequency (measured in Hz) and describes how many times per second the continuous-time signal is sampled

    • TsT_\text{s} is the sampling time (measured in seconds).

Sampling continuous-time signal

We can illustrate sampling by a person controlling a contact (see figure below):

  1. When TsT_\text{s} seconds has passed, the contact is pushed and released immidiately.

  2. At that exact time instant, the value of the continuous-time signal x(Ts)x(T_\text{s}) is stored on the computer.

  3. After another TsT_\text{s} seconds, the contact is again pushed and released immidiately so that x(2Ts)x(2T_\text{s}) is now stored.

  4. If we keep pushing/relasing the contact every TsT_\text{s} seconds, we will after nn times store the signal value x(tn)x(t_n) where

    tn=nTs=n/fs .t_n = nT_\text{s} = n/f_\text{s}\ .

    The scaler nn is often referred to as the sampling index.

Note that people often write x(tn)x(t_n) as

x(tn)=xn=x[n] .x(t_n) = x_n = x[n]\ .
Illustration of sampling

Discrete-time signal

A discrete-time signal is characterised by

  • time: the signal only has a value xnx_n at certain times, i.e., tn=nTst_n=nT_\text{s} for n=,3,2,1,0,1,2,3,n=\cdots,-3,-2,1,0,1,2,3,\cdots. Therefore, the xx-axis is often the sampling index nn instead of time.

  • amplitude: the signal value xnx_n can take on any value from a continuum of numbers (such as the real numbers).

A discrete-time signal is sometimes also referred to as a digital signal (although we will use this term for something slightly different later).

Discrete-time signal

Informally: A discrete-time signal is a series of time-ordered numbers.

Reconstruction

If we want to play back a discrete-time signal on, e.g., a loudspeaker, we have to convert the discrete-time signal back into a continuous-time signal.

  • Converting a discrete-time signal xnx_n into a continuous-time signal x(t)x(t) is called reconstruction.

  • Reconstruction is performed using two components:

  • hold circuit: holds a value xnx_n for TsT_\text{s} seconds. This will create a staircase signal.

  • post filter: smooth out the discountinuities in the staircase signal by using a low-pass filter with a cut-off frequency of fs/2f_\text{s}/2 Hz.

Illustration of reconstruction

Note that we will talk much more about filtering in the next lectures.

Summary

  1. A continuous-time signal x(t)x(t) can be drawn without lifting the pen from the paper.

  2. A discrete-time signal xn=x(tn)x_n = x(t_n) is a series of time-ordered numbers.

  3. Sampling converts a x(t)x(t) into xnx_n by measuring the value of x(t)x(t) at the times

    tn=nTs=n/fst_n = nT_\text{s} = n/f_\text{s}

    where

  • nn is the sampling index

  • TsT_\text{s} is the sampling time

  • fs=1/Tsf_\text{s}=1/T_\text{s} is the sampling frequency

  1. Reconstruction converts xnx_n into x(t)x(t) by first creating a staircase signal from xnx_n and then by filtering this staircase signal with a low-pass filter.

Aliasing

In the next 20 minutes, you will learn

  • How we write a discrete-time sinusoid.

  • What aliasing is.

  • How we can avoid aliasing by selecting the sampling frequency fsf_\text{s}.

  • What an anti-aliasing filter is and why we need it.

Discrete-time sinusoid

As we have seen in the first two lectures, a continuous-time sinusoid can be written as

x(t)=Acos(Ωt+ψ)x(t) = A\cos(\Omega t+\psi)

where

  • A0A\geq 0 is an amplitude

  • Ω=2πf\Omega=2\pi f is a frequency measured in rad/s

  • ψ\psi is the inial phase.

Let us now sample this signal with a sampling frequency of fsf_\text{s} Hz. We then get the discrete-time sinusoid

xn=x(tn)=x(n/fs)=Acos(Ωn/fs+ψ)=Acos((2πf/fs)n+ψ)=Acos(ωn+ψ)\begin{align} x_n &= x(t_n) = x(n/f_\text{s}) = A\cos(\Omega n/f_\text{s}+\psi) = A\cos((2\pi f/f_\text{s}) n+\psi)\\ &= A\cos(\omega n+\psi) \end{align}

where

  • ω=Ωfs=2πf/fs\omega = \Omega f_\text{s}= 2\pi f/f_\text{s} is the digital frequency measured in radians/sample.

For a discrete-time signal, ω=2π\omega = 2\pi corresponds to the sampling frequency, and we will also write this frequency as ωs\omega_\text{s}.

What is Aliasing?

  • Aliasing comes from the word alias.

  • It refers to that a sinusoidal component of one frequency is ‘disguising’ itself as a sinusoidal component with another frequency.

As an example, let us try to sample these continuous-time sinusoids

x(t)=cos(2πft)y(t)=cos(2π(fsf)t)\begin{align} x(t) &= \cos(2\pi f t)\\ y(t) &= \cos(2\pi (f_\text{s}-f) t) \end{align}

using a sampling frequency of fsf_\text{s} Hz.

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

def sinusoid(samplingIndices, digitalFreq):
    '''Compute a cosine'''
    return np.cos(2*np.pi*digitalFreq*samplingIndices)

nData = 100
samplingFreq = 100 # Hz
samplingTime = 1/samplingFreq # s
samplingIndices = np.arange(nData)
time = samplingIndices*samplingTime
freqA = 10 # Hz
freqB = 90 # Hz (samplingFreq-freqA)

# plot the results
plt.figure(figsize=(10,6))
plt.plot(time, sinusoid(samplingIndices,freqA/samplingFreq), linewidth=2, marker='o', label="$x(t)$")
plt.plot(time, sinusoid(samplingIndices,freqB/samplingFreq), linewidth=2, marker='o', label="$y(t)$")
plt.legend()
plt.xlim((time[0],time[nData-1])), plt.ylim((-1.5,1.5))
plt.xlabel('time [s]'), plt.ylabel('Amplitude [.]');
<Figure size 1000x600 with 1 Axes>

Observation: Even though the continuous time signals x(t)x(t) and y(t)y(t) have different frequencies, the discrete-time signals xnx_n and yny_n have the same digital frequency.

Some consequences:

  • Reconstructing a continuous-time signal from yny_n results in x(t)x(t) - not y(t)y(t).

  • We say that y(t)y(t) has been aliased when we cannot recover it again after sampling it.

  • A discrete-time sinusoid of digital frequency ω=2πf/fs\omega=2\pi f/f_\text{s}, could be a sampled continuous-time sinusoid given by

    y(t)=Acos((Ω+k2πfs)t+ψ)y(t) = A\cos((\Omega+k2\pi f_\text{s})t + \psi)

    for any integer kk.

Nyquist-Shannon sampling theorem

To avoid aliasing, the maximum frequency fmaxf_\text{max} in a continuous-time signal must satisfy that

2fmax<fs2f_\text{max} < f_\text{s}

where fsf_\text{s} is the sampling frequency.

We can satisfy the sampling theorem in two ways:

  1. Select the sampling frequency fsf_\text{s} high enough

  2. Pre-filter the continuous-time signal with a low-pass filter (a so-called anti-aliasing filter) with a cut-off frequency below fs/2f_\text{s}/2.

Typical sampling frequencies used for recording audio

ApplicationSampling frequency fsf_\text{s}
IMU200 Hz
XR controllers1000 Hz
Narrowband speech8000 Hz
Wideband speech, VoIP16000 Hz
CD Audio44100 Hz
Video recorders48000 Hz
DVD-audio and Blu-ray96000 and 192000 Hz
Sonar200000 Hz

Anti-aliasing filter

  • Often, we do not know the highest frequency fmaxf_\text{max} in our input signal.

  • Instead, we simply filter out all frequency content above fs/2f_\text{s}/2 to avoid aliasing.

  • This filter is called an anti-aliasing filter and is present in all practical sampling blocks.

Illustration of filtering and sampling

Aliasing also occours in videos and images

Image example of aliasing :width: 100% :align: center

Summary

  1. A discrete-time sinusoid is written as

    xn=Acos(ωn+ψ)x_n = A\cos(\omega n +\psi)

    where AA and ψ\psi have the same meaning as for the continuous-time sinusoid and

    • ω=2πf/fs\omega = 2\pi f/f_\text{s} is the digital frequency and measured in rad/sample

    • fsf_\text{s} is the sampling frequency measured in Hz.

  2. Aliasing refers to when the frequency of a sinusoid is lowered due to undersampling the signal.

  3. To avoid aliasing, we must satisfy Nyquist’s sampling theorem stating that

    2fmax<fs2f_\text{max} < f_\text{s}

    where fmaxf_\text{max} is the maximum frequency in the continuous-time input signal.

  4. We can limit the maximum frequency of a continuous-time input signal by passing it through an anti-aliasing filter. This filter, which is a low-pass filter, ensures that aliasing does not occur.

Binary numbers

In the next 20 minutes, you will learn

  • What a binary number is

  • What a bit and a byte is

  • How you store data on, e.g., a computer or a CD

  • To get the following slightly geeky joke :)

There are 10 types of people in this world. Those who understand binary numbers and those who don’t!

The decimal number systems

We are used to the decimal number system where we encounter numbers such 3, 42, and 89809.

Let’s look at the example

1314 .1314\ .

We can make the following observations about this decimal number:

  • The number consists of four symbols, each called a digit

  • Each digit can be one of ten possible symbols (either 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9).

  • The order of the digits matter. For example, the right-most one in 1314 represents the number of 10s whereas the left-most one represents the number of 1000s. A number system with ordering is called a positional number system.

We can rewrite the decimal number 1314 as

1314=1000+300+10+4=11000+3100+110+41=1103+3102+1101+4100 .\begin{align} 1314 &= 1000 + 300 + 10 + 4\\ &= 1\cdot 1000 + 3\cdot 100 + 1\cdot 10 + 4\cdot 1\\ &= 1\cdot 10^3 + 3\cdot 10^2 + 1\cdot 10^1 + 4\cdot 10^0\ . \end{align}

In general, we can write an NN digit decimal number dN1dN2d2d1d0d_{N-1}d_{N-2}\cdots d_2d_1d_0 as

dN1dN2d2d1d0=n=0N1dn10n .d_{N-1}d_{N-2}\cdots d_2d_1d_0 = \sum_{n=0}^{N-1}d_n10^n\ .

Note that

  • dn{0,1,2,3,4,5,6,7,8,9}d_n\in\{0,1,2,3,4,5,6,7,8,9\}

  • 10 is the number of symbols that dnd_n can take on and is called the base of the decimal number system.

Let us now allow for an arbitrary base bb. Then we can write numbers as

dN1dN2d2d1d0=n=0N1dnbnd_{N-1}d_{N-2}\cdots d_2d_1d_0 = \sum_{n=0}^{N-1} d_n b^n

where

  • dn{0,1,,b1}d_n\in\{0,1,\ldots,b-1\}

  • bb is the base of the number.

For different values of bb, we get different number systems. Some examples are

  • b=10b=10: The decimal number system with possible symbols 0,1,2,3,4,5,6,7,8,9

  • b=2b=2: The binary number system with possible symbols 0,1

  • b=16b=16: The hexadecimal number system with possible symbols 0,1,2,3,4,5,6,7,8,9, A, B, C, D, E, F

TODO The binary number system

A binary number

  • has base 2 and

  • is written only in terms of 0s and 1s.

An example of a binary number is

0110 110120110\ 1101_2

where the subscript 2 is here only added to make it explicit that 0110 11010110\ 1101 is a binary number.

Note that

  • a ‘digit’ in a binary number is called a bit

  • a collection of 8 bits is called a byte with symbol B

  • a computer represents everything (numbers, colours, text, etc.) as binary numbers

Thus, the binary number 0110 110120110\ 1101_2 has 8 bits and 1 byte (or 1 B)

Converting binary numbers to decimal numbers

To convert from a binary number to a decimal number, we simple use the expression

dN1dN2d2d1d0=n=0N1dnbn .d_{N-1}d_{N-2}\cdots d_2d_1d_0 = \sum_{n=0}^{N-1} d_n b^n\ .

As an example, we get that 11012 converts to

11012=123+122+021+120=18+14+02+11=1310 .\begin{align} 1101_2 &= 1\cdot 2^3 + 1\cdot 2^2 + 0\cdot 2^1 + 1\cdot 2^0\\ &= 1 \cdot 8 + 1 \cdot 4 + 0\cdot 2 + 1\cdot 1\\ &= 13_{10}\ . \end{align}

Converting from decimal numbers to binary numbers is also possible, but is not covered here.

Adding binary numbers

You do it exactly as you learned in 2nd grade with decimal numbers. That is,

  • 02+02=020_2+0_2 = 0_2

  • 02+12=120_2+1_2 = 1_2

  • 12+12=021_2+1_2 = 0_2 with 12 in carry

Using these three rules, we obtain $$ \begin{array}[t]{r} 0100\ 1001_2 \

  • \ 0111\ 1100_2 \ \hline 1100\ 0101_2 \end{array} $$

Example: Representing text using binary numbers

ASCII characters

Example: Storing data on a disc

Information is stored by making tiny indentations known as pits on a disc.

  • A pit represents a 0

  • The opposite of a pit (called land) represents a 1

ASCII characters
  • The binary data is stored along one long spiral on the disc.

  • CD: The spiral is 5.7 km long

  • DVD: The spiral is 12.3 km long

  • Blu-ray: The spiral is 28.4 km long

  • A laser is used for reading the binary data by following this spiral path.

ASCII characters

Summary

  1. A binary number

    • only contains 0s and 1s

    • consists of bits

  2. A collection of eight bit is called a byte

  3. Everything (numbers, text, images, video, audio, etc.) is stored and manipulated as binary numbers on a computer

  4. The following slightly geeky joke is now funny ;)

There are 10 types of people in this world. Those who understand binary numbers and those who don’t!

Assignment


Bonus info:

  1. With base b=2b=2, a binary number can be written as

    dN1dN2d2d1d0=n=0N1dnbn .d_{N-1}d_{N-2}\cdots d_2d_1d_0 = \sum_{n=0}^{N-1} d_n b^n\ .
  2. G means billion (109), M means million (106), k means thousand (103), and B means byte (8 bit)

Quantisation

In the next 20 minutes, you will learn

  • What quantisation is and why it is necessary

  • How you will typically do it

  • What signal-to-noise ratio (SNR) and dynamic range is

Example: Storing π\pi on a computer

How would you store π\pi (or other irrational numbers) on a computer?

Digits of pi
  • It requires an infinite amount of memory to store π\pi on a computer.

  • Therefore, we have to store an approximation to π\pi with only a finite number of digits. Let us call this approximation pp.

  • The approximation error ee can be written as

    e=πp .e = \pi-p\ .

Example: Let pp contain only the first two digits of π\pi after the comma (i.e., p=3.14p=3.14). Then

e=πp=π3.14=0.001592653589e = \pi-p = \pi-3.14 = 0.001592653589\ldots

We say that we have rounded of (or quantized) π\pi to its nearest two-digits-after-the-comma representation.

The need for quantisation

Sampling converts a continuous-time signal into a discrete-time signal. That is, we go from an infinite number of time values to a finite number of time values.

Illustration of sampling

However, we also have to do something about the signal value x(tn)=xnx(t_n)=x_n for every sampling time, so that we can store this number on the computer using a finite number of digits. This is called quantisation.

Illustration of ADC

Uniform quantisation

Assume that we sample the signal value xnx_n and that

  • the signal value is in the interval (α,α)(-\alpha,\alpha)

  • we have β\beta bits available for storing this signal value. Note that we can represent 2β2^\beta different values with β\beta bits.

We now do the following.

  1. Divide the interval (α,α)(-\alpha,\alpha) into 2β2^\beta equally large cells, each of size

    Δ=α(α)2β=2α2β=α2β1 .\Delta = \frac{\alpha-(-\alpha)}{2^\beta} = \frac{2\alpha}{2^\beta} = \frac{\alpha}{2^{\beta-1}}\ .
  2. Round (or quantise) the signal value xnx_n to the value yny_n at the nearest cell boundary, i.e.,

    yn=Q(xn)=ΔxnΔ=ΔxnΔ+12y_n = Q(x_n) = \Delta \left\lfloor\frac{x_n}{\Delta}\right\rceil = \Delta \left\lfloor\frac{x_n}{\Delta}+\frac{1}{2}\right\rfloor

    where \lfloor\cdot\rceil and \lfloor\cdot\rfloor refer to the rounding and flooring operations, respectively.

Example: 3 bit quantisation

In the figure below, the continuous-time signal (dashed gray) is first sampled (green) and then quantised (orange) using a three bit quantiser. The horisontal dashed red lines mark the quantisation levels. The final bit stream is

100 110 111 111 111 110 110 100 011 010 001 001 .100\ 110\ 111\ 111\ 111\ 110\ 110\ 100\ 011\ 010\ 001\ 001\ .
Three bit quantisation

Quantisation error

The quantisation error ene_n is the difference between the signal value xnx_n and its rounded value yn=Q(xn)y_n=Q(x_n), i.e.,

en=xnQ(xn) .e_n = x_n-Q(x_n)\ .

We can rearrange this into

Q(xn)=xn+en .Q(x_n) = x_n+e_n\ .

That is, we can think of quantisation as adding an error to the signal value xnx_n.

Quantisation block

A measure of quantisation quality is how big the average power of ene_n is compared to the average power of xnx_n. The average power of, e.g., ene_n is defined as

Pe=1Nn=0N1en2 .P_e = \frac{1}{N}\sum_{n=0}^{N-1} e_n^2\ .

If we define PxP_x in a similar way, the signal-to-noise ratio (SNR) is defined as

SNR=10log10PxPe ,\text{SNR} = 10\log_{10}\frac{P_x}{P_e}\ ,

and it is measured in decibel (dB).

Now, assume that

  • the signal values xnx_n take on values in (α,α)(-\alpha,\alpha) equally often (a uniform distribution)

  • the quantisation errors ene_n take on values in (Δ/2,Δ/2)(-\Delta/2,\Delta/2) equally often (a uniform distribution)

We then get

Px=(2α)212=α23Pe=Δ212=112(α2β1)2 .\begin{align} P_x &= \frac{(2\alpha)^2}{12} = \frac{\alpha^2}{3}\\ P_e &= \frac{\Delta^2}{12} = \frac{1}{12}\left(\frac{\alpha}{2^{\beta-1}}\right)^2\ . \end{align}

These results can be derived by computing the variance of a uniform distribution.

Finally, we get the SNR

SNR=10log10PxPe=10log10(α2312(2β1α)2)=10log10(22β)=β20log1026β .\begin{align} \text{SNR} &= 10\log_{10}\frac{P_x}{P_e} = 10\log_{10}\left(\frac{\alpha^2}{3}12\left(\frac{2^{\beta-1}}{\alpha}\right)^2\right)\\ &= 10\log_{10}\left(2^{2\beta}\right) = \beta 20 \log_{10} 2 \approx 6\beta\ . \end{align}

Thus, for every additional bit, the SNR is improved by approximately 6 dB.

Dynamic range

The dynamic range is the ratio between the loudest and softest values we can represent using a β\beta bit quantiser.

  • Softest value: 1

  • Loudest value: 2β2^\beta

The dynamic range of a quantiser is thus

DR=10log10((2β1)2)=10log10(22β)=β20log1026β .\text{DR} = 10\log_{10}\left(\left(\frac{2^\beta}{1}\right)^2\right) = 10\log_{10}\left(2^{2\beta}\right) = \beta 20 \log_{10} 2 \approx 6\beta\ .

Thus, we get a dynamic range of 96 dB for a 16 bit quantiser (typical CD quality) and 144 dB for a 24 bit quantiser. Note that the dynamic range of the human ear is approximately 120 dB.

Summary

  1. A quantiser rounds the signal values to a value on a grid.

  2. All the points of the grid can be represented using β\beta bits which results in 2β2^\beta possible values.

  3. Quantisation introduces noise into the digital signal. The signal-to-noise ratio (SNR) describes how powerful the signal is compared to this quantisation noise.

  4. The SNR (and dynamic range) depends on the number of bits used as

    β20log106β .\beta20\log_{10} \approx 6\beta\ .

Assignment

Think of ways for increasing the dynamic range without increasing the number of the bits. Hint = Could non-uniform quantisation work?