Sampling and quantization#
Note
This notebook adapts content from the Sampling and Quantization sections of the Jupyter Book “Audio Signal Processing Concepts Explained with Python” by Francesco Papaleo.
Sampling#
Sound waves are temporally continuous processes. However, in the digital age, when we record the signal, we sample the original analog waveform into discrete digital samples. Our sampling strategy determines our ability to reconstruct the original signal from the samples. In general, a higher sampling rate helps with the reconstruction, but the actual frequency components in the signal determines if a recording generated from a particular sampling rate allows us to reconstruct the original signal.
Below we will demonstrate this by using a simple sine wave. Since we cannot have a continuous signal in the notebook here, we will start with one sine wave with very high sampling rate, and see what happens when we reduce the sampling rate.
Sampling a sine wave#
Here, we will generate a continuous 5 Hz sine wave with a sampling rate of 10000 samples/s, and reduce the sampling rate to 100 samples/s.
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
# Sine Wave 5Hz
freq = 5
N = 10000 # number of samples
end_time = 1
time = np.linspace(0, end_time, end_time*N, endpoint=False) # 1 sec
A = 1 # amplitude
cw = A*np.sin(2*np.pi*freq*time) # zero phase
plt.figure(figsize=(8,4))
plt.plot(time, cw)
plt.title("5 Hz sine wave")
plt.show()
freq = 5
N_high = 10000
N_low = 50
end_time = 1
A = 1
# high res
time_high = np.linspace(0, end_time, end_time*N, endpoint=False) # 1 sec
cw = A*np.sin(2*np.pi*freq*time_high) # zero phase
# low res
time_low = np.linspace(0, end_time, end_time*N_low, endpoint=False)
cw_sampled = A*np.sin(2*np.pi*freq*time_low) # sampled cw sample
plt.figure(figsize=(8,4))
plt.plot(time, cw, label="Original signal")
plt.stem(time_low, cw_sampled, linefmt='r-', markerfmt='.', basefmt='', label='Sampled signal')
plt.plot(time_low, cw_sampled, 'r--', alpha=0.5)
plt.title("5 Hz sine wave")
plt.legend(loc="lower left", fontsize=10)
plt.show()
Exercise
Using the widget below, can you see at what point the sampling process start to fail and you can no longer see the waveform from the discrete samples?
Important
The Nyquist rate of the signal is twice the highest frequency in the signal. When sampling at a higher rate, the reconstructio is distortion free, while if it is lower we would observe aliasing artifacts.
Quantization#
In the above, we discussed the digitization of the signal through sampling in time. What about the digitization of the signal ampliude? In the digital world, we cannot store values with infinite fractions. Instead, we have to store the digitized samples with a finite number of bits, which varies depending on the device.
When a signal is quantized to a bit depth of \(b\), the signal amplitude is represented by one of the \(2^{b}\) possible levels. Therefore, the higher the bit depth, the more accurate the signal amplitude. For example, a 24-bit device is able to represent the signal amplitude more accurately than a 16-bit device. We often use the dynamic range (dB) to characterize this.
You can use the widget below to see how quantization would affect the digital samples from the original waveform.
Exercise
Based on the number of levels one can represent using bits:
What is the maximum quantization error for a sample point in a 4-, 8-, 16-, or 32-bit system that accepts input signals at +/- 1 V?
What is the dynamic range for each of these systems?