To process the audio signal, we need to somehow represent it on our machine. Several different options are possible, but the most common and useful one for sound processing is the discrete sample representation. That’s where the concepts of sampling and quantization come into play.

Mind you, that in this article expressions function and signal are used interchangeably. Both function and signal can be continuous or discrete.

How to represent a continuous function through discrete values?

The computers and hardware we are using are only capable of storing finite-valued numbers, e.g. 2, -5, 9.5, 10e3, some with less accuracy than others due to binary representation. How to represent a continuous (analog) function through such numbers?

If we know that the observed function of time tt is affine we can write:

s(t)=at+bs(t) = at + b

and store the a and b coefficients, which are discrete numbers. Whenever someone asks for the ss value at a particular time tt we can easily calculate the output using the formula above.

If the signal is a sinusoid, it is completely determined by its amplitude AA , frequency ff and phase offset ϕ\phi:

s(t)=Asin(2πft+ϕ)s(t) = A\sin(2\pi f t + \phi)

In general, we do not know how does the signal we are observing look like: that’s the whole point of observing it, right? That’s where sampling comes in.

An example of the observed signal: a 1 Hz sine (one period).

1 Hz sine sampled with sample rate equal to 8 Hz. Red dots mark the samples taken. Clearly the original signal can be reconstructed.

Sampling is the process of measuring and storing values of the observed continuous function s(t)s(t) at discrete time intervals nTnT. From mathematical point of view, we can present it as:

sdiscrete(n)=scontinuous(t)p(t)=scontinuous(t)δ(tnT) {eq:sampling}s_{discrete}(n) = s_{continuous}(t) p(t) = s_{continuous}(t) \delta (t - n T) \text{ \{eq:sampling\}}


p(t)=n=+δ(tnT)p(t) = \sum_{n=-\infty}^{+\infty}\delta (t - n T)

is called an a sampling function or an impulse-train (a series of discrete Dirac impulses spaced at time intervals TT). In the equations above, tt is the continuous time, whereas nn is the index of the sample taken. Discrete Dirac’s delta δ(x)\delta (x) is equal to 11 if xx is equal to 00 and 00 everywhere else:

δ(x)={1if x=00if x0\delta (x) = \begin{cases}1 & \quad\text{if } x = 0 \newline 0 & \quad\text{if } x \ne 0 \end{cases}

so the right hand side expression in {eq:sampling} is equal to 00 unless t=nTt = nT. TT is called the sampling period and is equal to the reciprocal of the sampling frequency:

fs=1Tf_s = \frac{1}{T}

Sampling frequency (or sampling rate) is the number of samples of the continuous function (signal) we observe during one second. It is expressed in Hz (1s\frac{1}{s}).

Sampling rate is one of the most fundamentals parameters of any digital system we work with. It determines the behavior of many algorithms and the way we process sound. Most importantly, it specifies how we store and reproduce the sound.

A theorem of utmost importance is the sampling theorem.

The sampling theorem

If fmaxf_{max} is the maximum frequency in the observed signal s(t)s(t), it can be uniquely represented by a discrete sequence s(nT)s(nT) if

fmax<fs2,f_{max} < \frac{f_s}{2},

where tRt \in \mathbb{R}, nZn \in \mathbb{Z} and T=1fsT = \frac{1}{f_s}.

What sampling theorem says, is that you need to take at least 3 samples per function’s period in order to be able to restore it to the continuous form. How the restoration will not be explained here.

This theorem goes by many names, among which different combinations of names Nyquist, Shannon and Kotelnikov are used. Its significance can be seen in its multiple corollaries and many applications, also in sound programming.

The quantity of fs2\frac{f_s}{2} is referred to as Nyquist frequency. In general, the signal we wish to observe, should not contain frequencies above the Nyquist frequency. Otherwise our sampling is prone to aliasing which will be explained in another article. Suffice it to say, that it disturbes our view of the signal.


Not only we are not able to store continuous functions directly on digital machines, but we also fail to represent all its values precisely. Quantization describes, which values can we store adequately.

Quantization in time directly follows from the sampling methodology. We are only able to sample as fast as our analog-to-digital converters (ADCs) let us. From a practical perspective, it is quite desirable to have evenly spaced samples, rather than intervals of varying length between successive samples. It lets us analyze the signal more easily and apply various mathematical operations (e.g. we are able to formulate it with the use of the impulse-train function p(t)p(t)).

Quantization in amplitude restricts the precision of our observation in the value of the samples we take. It starts in ADCs and can be changed throughout the digital system. Whenever we change quantization to a less fine-grained system, we lose information.

On a machine samples’ values can be stored as integers (integer numbers of range specified by the number of bits used to represent them) or floating-point numbers (capable of capturing very small as well as very large quantities). The former can be met in programming under the names of short, int, long and the latter as float or double. It is not within the scope of this article to explain the above-mentioned formats, but all of them are well-documented throughout the web.

Having discussed the basics of sampling and quantization, we are ready to put them into practice!

Sampling code example

This example code samples a 1 Hz sine with the sample rate of 8 Hz and displays the results:

#!/usr/bin/env python3
"""Example of sine sampling"""
import numpy as np
import matplotlib.pyplot as plt
__author__  = "Jan Wilczek"
__license__ = "GPL"
__version__ = "1.0.0"

# Plotting parameters
plt.rcParams.update({'font.size': 18})
xlim = [-0.01, 1.01]
yticks = [-1.0, 0.0, 1.0]

def signal(A, f, t):
    :param A: amplitude
    :param f: frequency in Hz
    :param t: time in s
    :return: sine with amplitude A at frequency f over time t
    return A * np.sin(2 * np.pi * f * t)

# Observed signal's parameters
A = 1       # amplitude
f = 1       # frequency of the observed signal in Hz
fs = 48000  # basic sampling rate in Hz, to make observed signal seem "continuous"

# Signal's generation
t = np.linspace(0, 1, fs)   # "continuous" time in s
s_t = signal(A, f, t)

plt.title(f'Sine to be sampled at f={f} Hz')
plt.plot(t, s_t, linewidth=3)
plt.hlines(0, xmin=xlim[0], xmax=xlim[1], colors='black')
plt.xlabel('t [s]')

# The sample rate we are sampling the observed signal with
sample_rate = 8     # Hz

# Actual sampling
sampled_time = np.linspace(0, 1, sample_rate)
sampled_signal = signal(A, f, sampled_time)

plt.title(f'Sampled sine at f={f} Hz sampled at fs={sample_rate} Hz')
plt.plot(t, s_t, linewidth=3)
plt.stem(sampled_time, sampled_signal, linefmt='r-', markerfmt='ro', basefmt=' ')
plt.hlines(0, xmin=xlim[0], xmax=xlim[1])
plt.xlabel('t [s]')

You can copy the above code and run it yourself!

[1] Oppenheim, A. V. and Willsky, A. S. Signals & Systems. 2nd ed. Upper Sadle River, New Jersey: Prentice Hall, 1997.