Mathematics

The Nyquist–Shannon Sampling Theorem

Let us start with an utterly simple example, namely: imagine a sinusoid given by the formula

y\displaystyle y =Asin(2πft)\displaystyle {} = A \cdot \sin(2 \pi f t)
(1)

where AA is the amplitude, ff is the frequency, and the independent variable tt runs over the interval [0,1][0,\, 1]. On a graph such a sinusoid looks even more trivial.

A single period of a sinusoid with the crossings of the time axis marked and two arbitrary points t₁ and t₂
Fig. 1 — one period of the sinusoid y = A·sin(2πft).

Any sinusoid given by formula (1) can be stored efficiently as just two numbers — the frequency and the amplitude; so to “compress” a sinusoid one has to read these two numbers off the graph of a single period. That is done by solving a system of equations

y1\displaystyle y_1 =Asin(2πft1)\displaystyle {} = A \cdot \sin(2 \pi f t_1)y2\displaystyle y_2 =Asin(2πft2)\displaystyle {} = A \cdot \sin(2 \pi f t_2)
(2)

There are degenerate cases, of course: if both points land on zeros of the sinusoid, or sit symmetrically about its maximum, the equations become dependent and more than one sinusoid can pass through these two points. But on the whole it is all simple — and solving this system is not what matters to us. What matters is this: two points per period determine that very sinusoid uniquely.

An objection suggests itself here: how are you going to compute the frequency if you need two points inside a period, while the period itself is not yet known? A fair question with a simple answer: I measure at whatever rate the device has — say fsf_s samples per second — take two neighbouring samples, solve the system (2) and obtain ff. If the frequency that comes out is no more than half the measurement rate, that is fffs/2{} \le f_s / 2, then the measurement step did fit inside the period twice — so everything is computed correctly.

Naturally, a single sinusoid is of interest to nobody, so further on we shall talk about a linear combination of sinusoids — about the spectrum of a periodic — or periodized — signal. Periodicity is no restriction here: the theorem does not rely on it, and a period is needed only in order to speak of a set of harmonics. Say we observe a signal over a time TT — that very time is what we take for the period, and what the signal was doing before the observation began, or will be doing after, makes no difference to us.

First a few words about the Fourier series: what interests us is the passage from the time domain into the frequency domain and back. So let the signal y(t)y(t) be periodic with period TT, and let us denote the frequency of the nn-th harmonic by fnf_n=n/T{} = n / T. The corresponding angular frequency is ωn\omega_n=2πfn{} = 2 \pi f_n. Passing into the frequency domain is two sets of coefficients, each computed by its own integral

an\displaystyle a_n =2T0Ty(t)cos(ωnt)dt\displaystyle {} = \frac{2}{T} \int_0^T y(t)\, \cos(\omega_n t)\, dtbn\displaystyle b_n =2T0Ty(t)sin(ωnt)dt\displaystyle {} = \frac{2}{T} \int_0^T y(t)\, \sin(\omega_n t)\, dtAn\displaystyle A_n =an2+bn2\displaystyle {} = \sqrt{a_n^2 + b_n^2}
(3)

and the signal is assembled back by summing all these harmonics

y(t)\displaystyle y(t) =a02\displaystyle {} = \frac{a_0}{2}+n=1[ancos(ωnt)+bnsin(ωnt)]\displaystyle {} + \sum_{n=1}^{\infty} \big[ a_n \cos(\omega_n t) + b_n \sin(\omega_n t) \big]
(4)

The constant term a0/2a_0 / 2 is the mean value of the signal. The frequencies ωn\omega_n sit on a grid with step 2π/T2 \pi / T, so the spectrum of a periodic signal is simply a list of amplitudes A1,A2,A3,A_1, A_2, A_3, \ldots.

Fig. 2 — the partial sum of the series (4) for a square wave.

As one can see, the more harmonics, the closer the signal is to the original one (about the Gibbs phenomenon we deliberately keep silent — it is not the subject of this article). It follows that digitizing a signal always loses some part of its spectrum. Sometimes that is even a good thing — this is how high-frequency noise is cut off and the signal is smoothed — but again, that is not my point. My point is that a band-limited signal is completely determined by its values taken at an angular sampling rate ωs\omega_s=2πfs{} = 2 \pi f_s2ωmax{} \ge 2 \omega_{\max}, where ωmax\omega_{\max} is the upper bound of its spectrum. What I have formulated above was formulated long ago by V. A. Kotelnikov — back in 1933. Independently of him the same theorem was obtained by others: Harry Nyquist pointed out the rate twice the signal’s highest frequency as early as 1928, and Claude Shannon published his own proof in 1949 and built it into the foundations of information theory — which is why in the English literature the theorem is usually called the Nyquist–Shannon sampling theorem.

To cover the question fully we ought to look at how the signal is reconstructed back from its spectrum. But first let us lay a little groundwork — write out the well-known trigonometric formulas.

sinα\displaystyle \sin \alphasinβ\displaystyle {} - \sin \beta=2cosα+β2sinαβ2\displaystyle {} = 2 \cos\frac{\alpha+\beta}{2} \cdot \sin\frac{\alpha-\beta}{2}cosβ\displaystyle \cos \betacosα\displaystyle {} - \cos \alpha=2sinα+β2sinαβ2\displaystyle {} = 2 \sin\frac{\alpha+\beta}{2} \cdot \sin\frac{\alpha-\beta}{2}cosαcosβ\displaystyle \cos \alpha \cdot \cos \beta+sinαsinβ\displaystyle {} + \sin \alpha \cdot \sin \beta=cos(αβ)\displaystyle {} = \cos(\alpha - \beta)
(5)

Next we cannot do without the Dirichlet kernel — a closed formula for a sum of the form 12\frac{1}{2}+n=1Mcos(nx){} + \sum_{n=1}^{M} \cos(n x), so let us derive it right away. Multiply this sum by 2sin(x/2)2 \sin(x/2) and expand every term by the first formula of (5) — with α\alpha=(n+1/2)x{} = (n + 1/2) x and β\beta=(n1/2)x{} = (n - 1/2) x

2sinx2[12+n=1Mcos(nx)]\displaystyle 2 \sin\frac{x}{2} \cdot \left[ \frac{1}{2} + \sum_{n=1}^{M} \cos(n x) \right] =sinx2\displaystyle {} = \sin\frac{x}{2}+n=1M[sin((n+12)x)\displaystyle {} + \sum_{n=1}^{M} \biggl[ \sin\Big(\big(n + \tfrac{1}{2}\big) x\Big)sin((n12)x)]\displaystyle {} - \sin\Big(\big(n - \tfrac{1}{2}\big) x\Big) \biggr]=sin((M+12)x)\displaystyle {} = \sin\Big(\big(M + \tfrac{1}{2}\big) x\Big)

the sum telescopes: the inner terms cancel pairwise, and the edge ones cancel against the sin(x/2)\sin(x/2) on the left. It remains to divide both sides by 2sin(x/2)2 \sin(x/2) — and that is the Dirichlet kernel

12\displaystyle \frac{1}{2}+n=1Mcos(nx)\displaystyle {} + \sum_{n=1}^{M} \cos(n x)=sin((M+1/2)x)2sin(x/2)\displaystyle {} = \frac{\sin\big((M + 1/2) x\big)}{2 \sin(x/2)}
(6)

The groundwork is laid. Now let us take an arbitrary signal and its digitized version: the values are read off at the time instants kΔtk \Delta t with a uniform step Δt\Delta t.

A continuous signal and its samples taken at a uniform step, marked as dots on the curve
Fig. 3 — the continuous signal y(t) is measured at the time instants kΔt.

The measured signal is simply a set of numbers y(kΔt)y(k \Delta t), and it can be written through the Heaviside functions θ(t)\theta(t) — steps equal to zero for tt<0{} < 0 and to one for tt0{} \ge 0: every sample holds its value until the next one, and the whole staircase turns out to be a linear combination of shifted steps

ys(t)\displaystyle y_s(t) =k=0N1y(kΔt)[θ(tkΔt)θ(t(k+1)Δt)]\displaystyle {} = \sum_{k=0}^{N-1} y(k \Delta t)\, \big[ \theta(t - k \Delta t) - \theta\big(t - (k+1) \Delta t\big) \big]
(7)

Now let us carry our staircase into the spectral domain, for which we compute the coefficients by formula (3). Take the cosine coefficient; inside the period there are NN=T/Δt{} = T / \Delta t steps — we number them from zero, from kk=0{} = 0 to kk=N{} = N1{} - 1 — and the integral over the period falls apart into NN integrals over the steps, in each of which the value of the signal is constant and can be taken outside the integral

an\displaystyle a_n =2Tk=0N1y(kΔt)kΔt(k+1)Δtcos(ωnt)dt\displaystyle {} = \frac{2}{T} \sum_{k=0}^{N-1} y(k \Delta t) \int_{k \Delta t}^{(k+1) \Delta t} \cos(\omega_n t)\, dt
an\displaystyle a_n =2ωnTk=0N1y(kΔt)[sin(ωn(k+1)Δt)sin(ωnkΔt)]\displaystyle {} = \frac{2}{\omega_n T} \sum_{k=0}^{N-1} y(k \Delta t) \Big[ \sin\big(\omega_n (k+1) \Delta t\big) - \sin(\omega_n k \Delta t) \Big]
(8)

and by analogy for the sine coefficient we obtain

bn\displaystyle b_n =2ωnTk=0N1y(kΔt)[cos(ωnkΔt)cos(ωn(k+1)Δt)]\displaystyle {} = \frac{2}{\omega_n T} \sum_{k=0}^{N-1} y(k \Delta t) \Big[ \cos(\omega_n k \Delta t) - \cos\big(\omega_n (k+1) \Delta t\big) \Big]
(9)

collapse the differences in (8) and (9) with the first two formulas of (5): the half-sum of the arguments here is the middle of the step, and the half-difference is one and the same for all kk; let us denote

tk\displaystyle t_k =(k+12)Δt,\displaystyle {} = \left(k + \frac{1}{2}\right) \Delta t,Λn\displaystyle \Lambda_n=sin(ωnΔt/2)ωnΔt/2\displaystyle {} = \frac{\sin(\omega_n \Delta t / 2)}{\omega_n \Delta t / 2}
an\displaystyle a_n =2ΔtTΛnk=0N1y(kΔt)cos(ωntk)\displaystyle {} = \frac{2 \Delta t}{T}\, \Lambda_n \sum_{k=0}^{N-1} y(k \Delta t)\, \cos(\omega_n t_k)bn\displaystyle b_n =2ΔtTΛnk=0N1y(kΔt)sin(ωntk)\displaystyle {} = \frac{2 \Delta t}{T}\, \Lambda_n \sum_{k=0}^{N-1} y(k \Delta t)\, \sin(\omega_n t_k)a0\displaystyle a_0 =2ΔtTk=0N1y(kΔt)\displaystyle {} = \frac{2 \Delta t}{T} \sum_{k=0}^{N-1} y(k \Delta t)b0\displaystyle b_0 =0\displaystyle {} = 0
(10)

Note Λn\Lambda_n — this is the price of sampling. By the limit sinx/x\sin x / x1{} \to 1, the lower the frequency of the signal, or the smaller the interval Δt\Delta t, the closer Λn\Lambda_n is to one. From (10) it is clear that the staircase gives the sums over the samples we need, multiplied by Λn\Lambda_n — so to obtain the sums themselves, (10) has to be divided by Λn\Lambda_n. And now it is time to return from the frequency domain into the time domain.

A band-limited signal has no harmonics above ωmax\omega_{\max}, so the sum (4) breaks off: the last harmonic is the one with ωn\omega_n=ωmax{} = \omega_{\max}, that is nn=M{} = M=ωmaxT/(2π){} = \omega_{\max} T / (2 \pi)

y(t)\displaystyle y(t) =a02\displaystyle {} = \frac{a_0}{2}+n=1M[ancos(ωnt)+bnsin(ωnt)]\displaystyle {} + \sum_{n=1}^{M} \big[ a_n \cos(\omega_n t) + b_n \sin(\omega_n t) \big]
(11)

substitute (10), divided by Λn\Lambda_n, into it and swap the order of summation — first over the samples, then over the harmonics

y(t)\displaystyle y(t) =2ΔtTk=0N1y(kΔt)(12\displaystyle {} = \frac{2 \Delta t}{T} \sum_{k=0}^{N-1} y(k \Delta t) \Biggl( \frac{1}{2}+n=1M[cos(ωntk)cos(ωnt)+sin(ωntk)sin(ωnt)])\displaystyle {} + \sum_{n=1}^{M} \big[ \cos(\omega_n t_k) \cdot \cos(\omega_n t) + \sin(\omega_n t_k) \cdot \sin(\omega_n t) \big] \Biggr) y(t)\displaystyle y(t) =2ΔtTk=0N1y(kΔt)(12+n=1Mcos(ωn(ttk)))\displaystyle {} = \frac{2 \Delta t}{T} \sum_{k=0}^{N-1} y(k \Delta t) \left( \frac{1}{2} + \sum_{n=1}^{M} \cos\big(\omega_n (t - t_k)\big) \right)

and this sum of cosines we already know how to add up — it is the Dirichlet kernel (6) with xx=2πu/T{} = 2 \pi u / T, where uu=t{} = ttk{} - t_k is the distance from the point tt to the middle of the step

12\displaystyle \frac{1}{2}+n=1Mcos(ωnu)\displaystyle {} + \sum_{n=1}^{M} \cos\big(\omega_n u\big)=sin(π(2M+1)u/T)2sin(πu/T)\displaystyle {} = \frac{\sin\big(\pi (2M + 1) u / T\big)}{2 \sin(\pi u / T)}

it remains to let the period go to infinity — both the number of harmonics MM and the number of samples NN=T/Δt{} = T / \Delta t grow along with it. In the denominator sin(πu/T)\sin(\pi u / T)πu/T{} \to \pi u / T, in the numerator π(2M+1)u/T\pi (2M + 1) u / T2πMu/T{} \to 2 \pi M u / T=ωmaxu{} = \omega_{\max} u, and the factor 2Δt/T2 \Delta t / T against 2πu/T2 \pi u / T gives Δt/(πu)\Delta t / (\pi u). With Δt\Delta t=π/ωmax{} = \pi / \omega_{\max} — the sampling step exactly at the boundary ωs\omega_s=2ωmax{} = 2 \omega_{\max} — everything cancels exactly

2ΔtTsin(π(2M+1)u/T)2sin(πu/T)  \displaystyle \frac{2 \Delta t}{T} \cdot \frac{\sin\big(\pi (2M + 1) u / T\big)}{2 \sin(\pi u / T)} \;   sin(ωmaxu)ωmaxu\displaystyle {} \longrightarrow \; \frac{\sin(\omega_{\max} u)}{\omega_{\max} u}

in this limit the sum over the samples of a single period becomes a sum over all integer kk, and uu=t{} = ttk{} - t_k expands to ttΔt/2{} - \Delta t / 2kΔt{} - k \Delta t

y(t)\displaystyle y(t) =k=y(kΔt)sin(ωmax(tΔt/2kΔt))ωmax(tΔt/2kΔt)\displaystyle {} = \sum_{k=-\infty}^{\infty} y(k \Delta t)\, \frac{\sin\big(\omega_{\max} (t - \Delta t / 2 - k \Delta t)\big)}{\omega_{\max} (t - \Delta t / 2 - k \Delta t)}

in every term the argument is shifted by Δt/2\Delta t / 2 — by exactly half a step. Let us attribute each sample to the beginning of its step, that is, shift the time by Δt/2\Delta t / 2 back, and the shift disappears

y(t)\displaystyle y(t) =k=y(kΔt)sin(ωmax(tkΔt))ωmax(tkΔt)\displaystyle {} = \sum_{k=-\infty}^{\infty} y(k \Delta t)\, \frac{\sin\big(\omega_{\max} (t - k \Delta t)\big)}{\omega_{\max} (t - k \Delta t)}
(12)

This is the Kotelnikov (cardinal) series — the second half of the theorem: it is not enough to know that the samples suffice, one needs an explicit recipe for assembling the signal at any moment of time, including between the samples. The series (12) is exactly that recipe.

Look closely at sinu/u\sin u / u. It is shifted to the node kΔtk \Delta t and stretched so that its zeros land on all the other nodes of the grid: at its own node it equals one, at every other node — zero (fig. 4). This already shows why the series is built the way it is: at any node tt=mΔt{} = m \Delta t every term but one vanishes, and the sole survivor — the term with kk=m{} = m — yields exactly y(mΔt)y(m \Delta t). The series is guaranteed to pass through all the samples.

The sinc function: one at zero and zeros at all grid nodes
Fig. 4 — the basis function of the cardinal series.

Here is how it looks on the signal of fig. 3. The slider adds the terms of the series (12) one at a time: every sample brings its own sinu/u\sin u / u scaled by the sample value, and the sum of these terms assembles step by step into the original signal. We have only seven samples while the series is infinite, so near the ends of the interval the reconstructed curve departs from the original one — the price of truncating the series.

Fig. 5 — the original signal of fig. 3 (dashed) and its reconstruction by the series (12).