Skip to main content
Open In Colab This section was written by Nazib Khan (pull request #46), with help from an AI coding agent on the code. It reproduces the ideas of Chapter 20 of Foundations of Computer Vision by Antonio Torralba, Phillip Isola, and William T. Freeman. The book’s own figures are not reproduced here, because the book’s license covers only the work in full; links point to them instead. This section regenerates the figures of the book’s sampling chapter in PyTorch. Change a parameter in any cell and its figure updates. The thread through the section. Sampling keeps a continuous signal only at instants spaced by Ts=1/fsT_s = 1/f_s. Frequencies separated by a whole multiple of fsf_s then leave identical samples, so the original is no longer recoverable: this is aliasing. The sampling theorem says aliasing is avoided exactly when fs>2fmax⁡f_s > 2 f_{\max} (the Nyquist rate). Reconstruction fills the gaps between samples, and an anti-aliasing filter band-limits a signal before sampling so the theorem can be honored. Figure 20.15, the cone mosaic of the primate fovea, is a micrograph rather than a computed plot, so a link points to it.
Images replaced for licensing. The book is published under a CC BY-NC-ND license, which covers only the book as a whole and not its individual images, so this page does not republish the book’s photographs. License-free images stand in for them:If you use the book for non-commercial purposes, you can swap the originals back in: each line that loads a stand-in carries the original’s link in a comment.

Figure 20.1: the continuous wave

The input is a cosine ℓ(t)=cos⁡(ωt)\ell(t) = \cos(\omega t) with radian frequency ω=18π\omega = 18\pi, i.e. an ordinary frequency of ω/2π=9\omega/2\pi = 9 Hz, completing nine cycles over the unit interval. This is the signal that sampling has to capture.
Output from cell 4

Figure 20.2: sampling the wave

Sample with period Ts=1/11T_s = 1/11, giving 11 samples across the interval. With 11 samples for 9 periods it seems sufficient, with more samples than periods, yet Nyquist needs fs>18f_s > 18. The block records the samples ℓ[n]=ℓ(nTs)\ell[n] = \ell(n T_s).
Output from cell 6

Figure 20.3: many waves, the same samples

Reconstruction is ambiguous: nothing constrains the signal between samples. Under the slow-and-smooth prior the reconstruction is the slowest cosine through the dots. Here the 9 Hz input aliases to ∣9−11∣=2|9 - 11| = 2 Hz: subtracting one sampling rate gives the low frequency that shares every sample, since cos⁡(2π 9 n11)=cos⁡(2π (9−11)n11)\cos(2\pi\,9\,\tfrac{n}{11}) = \cos(2\pi\,(9-11)\tfrac{n}{11}).
Output from cell 8

Figure 20.4: aliasing bends a 2-D pattern

The 2-D analogue is ℓ(x,y)=cos⁡ ⁣(2π a (x+y)2)\ell(x,y) = \cos\!\big(2\pi\,a\,(x+y)^2\big): diagonal waves whose frequency rises with (x+y)2(x+y)^2, slow at the bottom-left and ever finer toward the top-right. Point-sampling to 52×5252\times52 with no pre-filter folds the unrepresentable high frequencies back, and the dominant orientation of the stripes flips.
Output from cell 10

Figure 20.5: a band-limited signal

The sampling theorem is stated for band-limited signals: those whose Fourier transform L(ω)L(\omega) is zero above some maximum frequency ωmax⁡\omega_{\max}. The sketch below shows such a spectrum, nonzero only inside [−ωmax⁡,ωmax⁡][-\omega_{\max}, \omega_{\max}].
Output from cell 12

Figure 20.6: the delta train

Sampling is modelled by multiplying the signal with a delta train (Dirac comb) III(t)=∑nδ(t−nTs)\mathrm{III}(t) = \sum_n \delta(t - n T_s): impulses at every multiple of TsT_s. Here Ts=1T_s = 1. Arrow heads mark the impulses (each of infinite height, unit area).
Output from cell 14

Figure 20.7: sampling with the delta train

Multiplying a continuous signal by the delta train keeps its values only at the impulse times, producing the sampled signal ℓs(t)=ℓ(t) III(t)\ell_s(t) = \ell(t)\,\mathrm{III}(t). Left: the continuous signal. Right: its delta-sampled version, impulses scaled by the signal.
Output from cell 16

Figure 20.8: aliasing in the Fourier domain

Sampling replicates the spectrum: the transform of the sampled signal is ∑kL(ω−k ωs)\sum_k L(\omega - k\,\omega_s), copies of LL spaced by the sampling frequency ωs\omega_s. (a) When ωs>2ωmax⁡\omega_s > 2\omega_{\max} the copies stay separate and the original is recoverable. (b) When ωs<2ωmax⁡\omega_s < 2\omega_{\max} neighboring copies overlap; high frequencies leak into low ones. That is aliasing.
Output from cell 18

Figure 20.9: the sinc function

Ideal reconstruction convolves the samples with a sinc\mathrm{sinc}, the impulse response of the ideal low-pass (box) filter. sinc(t)=sin⁡(πt)/(πt)\mathrm{sinc}(t) = \sin(\pi t)/(\pi t) peaks at the origin, is symmetric, and decays as 1/t1/t with zero crossings at the integers.
Output from cell 20

Figure 20.10: sinc interpolation

The ideal reconstruction is ℓ^(t)=∑nℓ[n] sinc ⁣((t−nTs)/Ts)\hat\ell(t) = \sum_n \ell[n]\,\mathrm{sinc}\!\big((t - nT_s)/T_s\big): a scaled, shifted sinc centered on each sample. The thin curves are the individual sincs; their sum (thick) is the smooth interpolation. Each sinc’s zero crossings fall on the other sample locations, so every sample is honoured exactly.
Output from cell 22

Figure 20.11: reconstruction degrades with sampling rate

One band-limited signal (a sum of a 3 Hz and 7 Hz cosine, so fmax⁡=7f_{\max}=7) is sampled at three rates. Left: the samples on the signal. Middle: the magnitude spectrum of the samples: peaks land at the true frequencies only while Nyquist holds, otherwise they fold. Right: sinc reconstruction (red) vs the original. Above Nyquist (fs>14f_s>14) it is exact; below, it is wrong.
Output from cell 24

Figure 20.12: local interpolation kernels

Ideal sinc reconstruction needs every sample. Cheaper local kernels trade accuracy for support. Nearest interpolation convolves the samples with a box of width TsT_s; linear interpolation convolves with a triangle of width 2Ts2T_s (the convolution of two boxes). Both are shown as the kernel that the samples are convolved with.
Output from cell 26

Figure 20.13: 2-D sampling patterns

Images need not be sampled on a square grid. Three arrangements: a rectangular grid, a hexagonal grid (alternate rows offset by half a step, rows spaced by 3/2\sqrt{3}/2), and an irregular grid (jittered positions).
Output from cell 28

Figure 20.14: Fourier transforms of the sampling lattices

In the frequency domain each sampling lattice repeats the spectrum on the reciprocal lattice, and the no-aliasing region is its Voronoi cell (red). The rectangular lattice gives a square cell; the hexagonal lattice gives a hexagon, which packs the largest alias-free area per sample, about a 14% resolution gain.
Output from cell 30

Figure 20.15: the cone mosaic

The book illustrates that the photoreceptors in the primate fovea sit on an approximately hexagonal lattice, using a micrograph of a monkey retina from Curcio et al. (Figure 20.15). That is measured microscopy, not a computed plot, so there is nothing to regenerate here. The previous figure already shows why hexagonal sampling is favored.

Figure 20.16: aliasing in a real image

The book downsamples a photograph of a zebra and the stripes change orientation. Here the public-domain cameraman test image stands in for it: you point-sample it with no pre-filter at two rates. Reconstructed to full size, the fine detail (tripod, coat texture) breaks up, and the log magnitude spectrum shows energy folding inward.
Output from cell 32

Figure 20.17: the anti-aliasing filter

Blurring with a low-pass filter before sampling removes the frequencies the grid cannot represent, so they no longer fold. The next cell applies a Kornia Gaussian blur, then point-samples at the same 1/8 rate. The result keeps the recognizable low-frequency structure, and its spectrum no longer shows the aliased fold-in, at the cost of the lost (genuinely unrepresentable) detail.
Output from cell 34

Concluding remarks

Aliasing appears whenever a continuous light field is sampled, and again at every downsampling or upsampling step (chapter 21 of the book). The sampling theorem says it is avoided when fs>2fmax⁡f_s > 2 f_{\max}; when it cannot be (a finite-resolution grid is never band-limited), an anti-aliasing filter trades unrepresentable detail for an artifact-free image. Aliasing is not always harmful: super-resolution methods exploit it to recover detail across frames. Reference. A. Torralba, P. Isola, W. T. Freeman, Foundations of Computer Vision, MIT Press, chapter 20, visionbook.mit.edu.