Chapter 18: Signals and Images
Images and audio are samples of continuous signals. Every operation in a multimodal front end (convolve, resize, transform, filter, take a log) is a linear map followed at most by a pointwise nonlinearity, and getting its conventions exactly right is what makes a reimplementation agree with the pipeline a model was trained on. This chapter builds that numerical front end of every vision and speech model as numpy reference code: convolution, image layouts, resampling, the Fourier transform, the short-time Fourier transform, and the mel scale. Each section derives the mathematics, works an example by hand, and then walks through a solution that matches the reference library (PyTorch, Pillow, torchaudio, numpy, librosa) value for value.
Concepts from other sections (18)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Linear map: a function between vector spaces with ; once bases are chosen, every linear map is a matrix. Read more: Chapter 9: Linear Algebra
- Mel scale: a perceptual pitch scale; Slaney’s is linear below 1000 Hz and logarithmic above, HTK’s is . Read more: 18.6 Mel scale and filterbanks
- DFT: multiplication by , writing samples as coefficients of complex sinusoids. Read more: 18.4 DFT and FFT
- Change of basis: rewriting a matrix in new coordinates as ; the transformation is the same, only its description changes. Read more: Chapter 9: Linear Algebra
- STFT: the DFT of overlapping windowed frames, giving a spectrum per time step. Read more: 18.5 Sampling, Nyquist, windows, and the STFT
- Mel filterbank: a matrix of triangular filters on mel-spaced edges that pools an rfft power spectrum into bands. Read more: 18.6 Mel scale and filterbanks
- word error rate (WER): pooled substitutions, deletions and insertions divided by the total reference word count. Read more: 15.5 Multimodal eval metrics: WER, CER, VQA, captions, zero-shot in AI Engineering
- im2col: copying every receptive field into a column, ordered , so a convolution becomes one matmul. Read more: 18.1 Convolution as a linear map
- col2im: the adjoint of im2col, which sums column entries back into the pixels they came from. Read more: 18.1 Convolution as a linear map
- Patchify: cutting an image into squares, each flattened into a vector of numbers. Read more: 18.2 Image tensors and layouts
- DCT-II: the cosine transform ; orthonormal with the scales . Read more: 18.4 DFT and FFT
- Log-mel: Whisper’s features, of the mel spectrogram with a floor, an 8-unit clip, and the map . Read more: 18.6 Mel scale and filterbanks
- Euler’s formula: ; multiplying by it rotates by . Read more: 6.2 Trigonometry, rotations, complex numbers, and Euler’s formula
- Vector-Jacobian product (VJP): , one step of reverse-mode differentiation. Read more: 10.3 Calculus 3 problem set: vectors, gradients, the chain rule, Hessians, multiple integrals, Lagrange
- Cross-correlation: sliding a kernel over a signal and taking a dot product at each position, with no flip; what deep learning calls convolution. Read more: 18.1 Convolution as a linear map
- Channels last (NHWC): a memory order in which the channel is the fastest axis; the logical shape can stay NCHW. Read more: 18.2 Image tensors and layouts
- Spectral leakage: energy from a tone spreading into many DFT bins because the frame does not hold a whole number of periods. Read more: 18.5 Sampling, Nyquist, windows, and the STFT
- Periodic Hann window: , one period of a cosine of the DFT length. Read more: 18.5 Sampling, Nyquist, windows, and the STFT
The arc runs from images to audio. You start by seeing a convolution as a matrix, which turns it into one matmul and gives its backward pass for free. You then fix how images sit in memory, and how they are resized without aliasing, bit for bit as Pillow does it. The second half moves to sound: the DFT as a unitary change of basis and the FFT that computes it for Whisper’s 400-sample frames, the windowed STFT that turns a clip into a spectrogram, and the mel filterbank and logarithm that turn the spectrogram into Whisper’s features. A problem set closes the chapter and adds two topics the multimodal models need: contrastive losses and word error rate.
Small conventions decide parity throughout: pass order, rounding, padding, window symmetry. Reproduce the reference library’s numbers, not just its idea.
Learning objectives
- Treat a convolution as a linear map: size its output, write it as a Toeplitz matrix and as an im2col matmul, and derive its backward pass with col2im.
- Explain an image tensor’s layout through its strides, and normalize and patchify images the way vision models expect.
- Resize images and resample audio without aliasing, matching Pillow,
F.interpolate, and torchaudio.- Compute the DFT with a mixed-radix FFT, a real FFT, the convolution theorem, and the DCT-II.
- Build Whisper’s log-mel features from a centered, windowed STFT and a Slaney mel filterbank.
Before you begin
Section titled “Before you begin”You need logarithms and Euler’s formula from Precalculus, matrices, transposes, and orthogonality from Linear Algebra, vector-Jacobian products from Matrix Calculus and Autodiff, and the floating-point error bounds of Numerical Methods and Floating Point. Comfort with numpy views, broadcasting, and fancy indexing helps throughout.
In this chapter
Section titled “In this chapter”- Convolution as a linear map. Deep learning’s convolution is a cross-correlation and a doubly block Toeplitz matrix; im2col makes it one matmul, and its adjoint col2im gives the backward pass.
- Image tensors and layouts. NCHW versus NHWC strides, why a layout change is a copy, CLIP normalization, Pillow’s fixed-point grayscale, and patchify as a reshape.
- Resampling and interpolation. Bilinear, bicubic, and Lanczos kernels, antialiasing as a stretched kernel, Pillow’s separable fixed-point passes,
F.interpolate’s convention, and a polyphase sinc resampler for audio. - DFT and FFT. The DFT as a unitary map, the convolution theorem, mixed-radix Cooley-Tukey for sizes like 400, the half-size real FFT, and the DCT-II via an FFT.
- Sampling, Nyquist, windows, and the STFT. Aliasing, spectral leakage and the periodic Hann window, center-padded frame counts, and the overlap-add inverse.
- Mel scale and filterbanks. The Slaney and HTK mel scales, area-normalized triangular filters, decibels, and Whisper’s log-mel scaling.
- Signals and images problem set. Pen-and-paper practice on convolution arithmetic, sampling, Fourier, mel, contrastive losses, and WER, with worked answers.
These ideas connect to several other chapters. Convolution and im2col underlie the CNN layers and C kernels of Neural Architectures in AI Engineering; the FFT and DCT reappear in transform coding in Information Theory; and the STFT and mel features are the front end of Whisper and audio LLMs in Foundation Models. In AI Engineering, the vision and speech parts call every function built here.
Further reading
Section titled “Further reading”- Oppenheim and Schafer, Discrete-Time Signal Processing (3rd ed.), the primary reference; a free alternative is Smith, Mathematics of the Discrete Fourier Transform and Spectral Audio Signal Processing (ccrma.stanford.edu).
- Dumoulin and Visin, “A guide to convolution arithmetic for deep learning” (free).
- Goodfellow, Bengio, Courville, Deep Learning, ch. 9 (free).
- Szeliski, Computer Vision: Algorithms and Applications, ch. 3 (free).