Signals and images problem set: convolution, sampling, Fourier, mel, contrastive losses, and WER
Overview
Section titled “Overview”| Module | S-M12 · solve · none · Pass 12 · 6 to 8 h |
| You build | answers in solve/S-M12.toml (68 checked by SymPy) and 4 proofs in solve/S-M12/q7.md, q21.md, q22.md, and q24.md (self-graded against their rubrics) |
| Contract | none: a pen and paper set |
| Tests | course/solve/S-M12/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M12/problems.md and in section 4 |
| Needs | no module. Reading: M12.1 to M12.6 and the Signals and Images topic |
| Used by | no call site (a solve set). It checks the arithmetic behind M12.1 to M12.6, and holds two solve-only topics the speech and vision parts lean on: contrastive losses (CLIP and SigLIP in L13) and word error rate (L14) |
| Milestone | MS-P12 (the Pass 12 gate runs ol check on every solve part of the pass) |
| Optional depth | Dumoulin and Visin, “A guide to convolution arithmetic for deep learning” (free); Oppenheim and Schafer, Discrete-Time Signal Processing, ch. 4 and 8; van den Oord, Li, Vinyals, “Representation learning with contrastive predictive coding” (2018); Zhai et al., “Sigmoid loss for language image pre-training” (ICCV 2023); Jurafsky and Martin, Speech and Language Processing, ch. 2.5 (edit distance, free) |
Key Takeaways
Section titled “Key Takeaways”- One formula sizes every conv, pool, and patch embedding, and receptive fields grow by times the product of earlier strides (q1 to q3).
- A conv is a Toeplitz matrix; its transpose (col2im) is the backward pass, and the backward of a correlation is a true convolution (q4 to q7).
- Sampling keeps only frequencies below half the rate; everything above folds back, in time and in images (q11, q12).
- The DFT is a unitary change of basis: Parseval, conjugate symmetry for real signals, and the convolution theorem with zero padding (q15, q16); an FFT needs radices that divide (q17).
- InfoNCE is cross-entropy with the positive as the label, bounded below by ; SigLIP’s sigmoid loss decouples the pairs (q20 to q22). WER is an edit distance over the reference length: it can exceed 1 and is not symmetric (q23, q24).
How to work this chapter
Section titled “How to work this chapter”ol start S-M12 # writes solve/S-M12.toml and one file per proofol check S-M12 # SymPy checks the answers, then asks each proof rubric (y/n)ol check S-M12 --regrade # ask the rubrics again after you change a proof1. Why now
Section titled “1. Why now”Pass 12 hands the system eyes and ears, and most of its bugs are arithmetic: an encoder that expects 1500 positions and gets 1501, a patch embedding with 196 tokens where the prompt reserved 197 slots, a spectrogram whose bins are 80 Hz apart instead of 40, a thumbnail that aliased before it was hashed. M12.1 to M12.6 built the functions; this set makes you predict their numbers by hand, so that a wrong shape in L13 or L14 looks wrong to you before a test says so. It also covers two topics with no module of their own: the contrastive losses CLIP and SigLIP train with, and the word error rate L14 reports.
2. Principles
Section titled “2. Principles”| Symbol | Meaning | Type / shape |
|---|---|---|
| input length, kernel size, stride, padding, dilation | ints | |
| Toeplitz matrix of a 1-D correlation | matrix | |
| , | sample rate and Nyquist frequency | Hz |
| , | interpolation kernel and Keys’ parameter | function, float |
| DFT | vector | |
| , | STFT frame length and hop | ints |
| mel value | mel | |
| , | contrastive logits and candidate count | floats, int |
| logistic sigmoid | function | |
| substitutions, deletions, insertions | ints |
2.1 Convolution arithmetic and the conv as a matrix
Section titled “2.1 Convolution arithmetic and the conv as a matrix”. A stack of layers sees input samples. The correlation has the kernel along each row of , shifted one column per row; maps an output gradient to an input gradient, which is the full true convolution of the gradient with the kernel. im2col copies each pixel into every window that contains it; col2im adds those copies back (M12.1).
2.2 Sampling, aliasing, and interpolation
Section titled “2.2 Sampling, aliasing, and interpolation”A tone at sampled at is indistinguishable from for any integer ; it appears at the representative in . Keeping every -th pixel divides the rate by , so frequencies above cycles per input pixel alias. Antialiased resizing stretches the kernel by the scale factor to remove them first (M12.3).
2.3 Fourier
Section titled “2.3 Fourier”. . Real input gives ; real and even input gives a real spectrum. The product of DFTs is the DFT of the circular convolution; padding to length makes it linear. An FFT recursion splits by its factors (M12.4). Centered STFT frames number , bins are apart, and a periodic Hann at hop overlap-adds its square to (M12.5).
2.4 Mel and decibels
Section titled “2.4 Mel and decibels”Slaney mel is linear to 15 mel at 1000 Hz, then 27 mel per factor 6.4. Power in dB is . Whisper’s feature is with (M12.6).
2.5 Contrastive losses
Section titled “2.5 Contrastive losses”CLIP scores a batch of image-text pairs with a similarity matrix and, for each image, classifies its own caption among the with a softmax: InfoNCE, . Its gradient is softmax minus one-hot, and van den Oord et al. show : more negatives can certify more mutual information. SigLIP replaces the softmax by an independent logistic loss per pair, with on the diagonal and elsewhere, so no normalizer couples the pairs.
2.6 Edit distance and WER
Section titled “2.6 Edit distance and WER”The word edit distance is the minimum number of substitutions, deletions, and insertions turning one word sequence into another, computed by a DP over prefixes. WER divides it by the number of reference words: insertions can push it above 1, and swapping reference and hypothesis changes the denominator.
3. Worked example by hand
Section titled “3. Worked example by hand”Conv. , , , : . The window starts sit at in input coordinates, and the next one () would need input sample 11.
Aliasing. 9 kHz sampled at 16 kHz: Hz, below , so it appears at 7 kHz.
DFT. : ; only survives, , and Parseval gives .
InfoNCE. Two candidates with equal logits: ; with the positive ahead by , .
WER. Reference “a b c”, hypothesis “a x c d”: one substitution and one insertion, WER . Written in solve/ as 2/3.
4. The problem set
Section titled “4. The problem set”Write each answer in solve/S-M12.toml; lettered parts are their own tables:
[q1.a]answer = "112"[q5.a]answer = "[[1, 0, -1, 0], [0, 1, 0, -1]]"[q15.c]answer = "[1, -I, -1, I]"[q7]proof = "S-M12/q7.md"Give exact values, not decimals.
Convolution arithmetic
Section titled “Convolution arithmetic”q1. Give the output length for (a) , , , (a ResNet stem); (b) a max pool with , on that output, along one axis; (c) Whisper’s stem on 3000 frames: a conv with , , , then one with , , ; (d) , , , , . [number]
q2. (a) How many input samples does a kernel of size with dilation span? Answer in and . (b) For odd , stride 1, and dilation 1, which padding keeps the output length equal to the input length? Answer in . [expr]
q3. The receptive field of an output is the number of input samples (along one axis) it depends on. Give it for (a) three stacked convs with stride 1; (b) a conv with stride 2 followed by a conv with stride 1. [number]
Correlation, convolution, and the conv as a matrix
Section titled “Correlation, convolution, and the conv as a matrix”q4. and , no padding. Give (a) the cross-correlation and (b) the true convolution (kernel flipped). [vector]
q5. For the cross-correlation of q4 written as : (a) give . [matrix] (b) Give the input gradient for the upstream gradient . [vector] (c) Is equal to the full (zero-padded) true convolution of with ? [bool]
q6. (a) With and as in q4 and q5, give the weight gradient . [vector] (b) A image goes through im2col with a kernel, stride 1, no padding. How many times is its center pixel copied into the columns? [number]
q7. Prove that col2im is the adjoint of im2col: for every input and every column array of the matching shape, , where col2im adds every column entry back to the pixel it was copied from and drops entries copied from the zero padding. [proof]
Parameter and FLOP counts
Section titled “Parameter and FLOP counts”q8. A conv from 64 to 128 channels with a kernel, groups 1, and a bias. Give (a) its parameter count; (b) the parameter count of a depthwise conv on 64 channels with a bias; (c) the multiply-accumulates of the first conv for one image with a output. [number]
q9. ViT-B/16 on a image with width 768. Give (a) the number of patches; (b) the parameters of the patch embedding (a conv with kernel and stride 16, with a bias); (c) the sequence length after prepending a CLS token. [number]
Layouts and strides
Section titled “Layouts and strides”q10. A float32 tensor with logical shape . (a) Give its byte strides per logical axis when stored NCHW, and (b) when stored NHWC (channels last). [vector] (c) Give the byte offset of element in the NHWC storage. [number] (d) Give the length of one flattened patch when a image is cut into patches. [number]
Sampling and aliasing
Section titled “Sampling and aliasing”q11. (a) Give the Nyquist frequency in Hz at a 16 kHz sample rate. At what frequency in Hz does a pure tone appear when (b) 7 kHz is sampled at 10 kHz; (c) 12 kHz is sampled at 16 kHz; (d) 25 kHz is sampled at 16 kHz? [number]
q12. An image has a stripe pattern with a period of 3 pixels. It is shrunk by 4 on each axis by keeping every fourth pixel, with no filter. (a) Give the period in output pixels of the stripes that appear. [number] (b) Can the output grid represent the original pattern without aliasing? [bool] (c) Give the highest frequency, in cycles per input pixel, that a shrink by 4 can keep without aliasing. [number] (d) A 1-pixel checkerboard is shrunk by 2 by keeping every other pixel on each axis. The result is: (a) a checkerboard, (b) a constant image, (c) stripes. [choice]
Interpolation kernels
Section titled “Interpolation kernels”q13. Keys’ cubic kernel is for and for . Give (a) and (b) with (Pillow), and (c) with (PyTorch). [number]
q14. Pillow shrinks with an antialiased kernel stretched by the scale factor. (a) Give the support radius in input pixels of the bilinear kernel (support 1) when shrinking 1000 pixels to 250. (b) Give the number of taps in Pillow’s weight table for that pass, . (c) Give the support radius of Lanczos3 when shrinking by 2. [number]
The DFT
Section titled “The DFT”q15. With , give the DFT of (a) , (b) , (c) . [vector] (d) A real signal of length 8 has . Give . [number]
q16. (a) Give for . (b) Give the circular convolution of length 4 of and . [vector] (c) Give the smallest transform length for which an FFT product computes the linear convolution of signals of lengths 300 and 100. [number] (d) Is the DFT of a real, even signal () real? [bool]
The FFT
Section titled “The FFT”q17. (a) Into how many radix stages does an FFT split when it uses radices 4, 4, 5, 5? (b) How many complex multiplies does the direct DFT of length 400 take ()? (c) How many butterflies does a radix-2 FFT of length 1024 take, ? [number] (d) Can a radix-2-only FFT compute a 400-point DFT, without changing the bin frequencies? [bool]
Windows and the STFT
Section titled “Windows and the STFT”q18. Whisper: 30 s of audio at 16 kHz, , hop 160, centered frames. Give (a) the number of STFT frames before Whisper drops the last one; (b) the spacing of the bins in Hz; (c) the number of rfft bins per frame; (d) the constant value of for a periodic Hann window at hop ; (e) the frame length in milliseconds. [number] (f) Give the periodic Hann window of length 4. [vector]
Mel and decibels
Section titled “Mel and decibels”q19. On the Slaney scale, give the mel value of (a) 1000 Hz and (b) 6400 Hz. (c) On the HTK scale, : give . (d) Give a power ratio of 1000 in decibels. (e) Whisper computes , then , then . Give the feature of a band with mel power in a clip whose largest is 1. (f) Give the feature value of pure silence. [number]
Contrastive losses
Section titled “Contrastive losses”q20. InfoNCE for one anchor scores candidates with logits (the positive is ) and takes , the cross-entropy with the positive as the label. (a) Give for . (b) Give when all logits are equal. Answer in . [expr] (c) The InfoNCE bound says . Give the bound in nats for and . [number] (d) SigLIP’s loss for one pair is with label . Give for a positive pair () with , , . [number]
q21. Prove that the gradient of the InfoNCE loss of q20 with respect to the logits is , and conclude that for every finite and that only when for every . [proof]
q22. Prove that for SigLIP’s loss , , and explain why, unlike q21, the gradient of one pair does not depend on the logits of the other pairs in the batch. [proof]
Word error rate
Section titled “Word error rate”q23. The word error rate is , the minimum number of substitutions, deletions, and insertions turning the reference into the hypothesis, over the number of reference words. Give the WER of the hypothesis “oh hello there” against the reference “hello”. [number]
q24. Prove that WER is not a metric on word sequences, by showing it is not symmetric with an example (compute both directions), and state which metric axiom fails and why. [proof]
5. Pitfalls
Section titled “5. Pitfalls”| Pitfall | Symptom | Caught by |
|---|---|---|
| Dropping the in the size formula | 1499 positions where Whisper’s encoder has 1500 | q1 c (canary) |
| Ignoring dilation in the kernel span | a dilated layer sized like a dense one | q1 d (canary 30) |
| Flipping the kernel in a network conv | correlation and convolution swapped | q4 a, q5 a (canaries) |
| Counting FLOPs where MACs were asked (or the reverse) | a factor 2 in every cost estimate | q8 c (canary) |
| Forgetting the CLS token | 196 positions where the encoder has 197 | q9 c (canary) |
| Strides in elements, or in memory order instead of per logical axis | a kernel indexing the wrong neighbour | q10 a, q10 b (canaries) |
| Folding once instead of to the nearest multiple of the rate | 9 kHz instead of 7 kHz | q11 d (canary) |
| The bilinear weight where the cubic was asked, or the wrong | interpolation that does not match Pillow or PyTorch | q13 (canaries) |
| Forgetting the antialias stretch | support 1 where Pillow uses 4 | q14 a (canary) |
| The inverse DFT’s sign | a mirrored spectrum | q15 c (canary) |
| Treating 400 as stages | six radix-2 and radix-5 stages instead of four | q17 a (canary 6) |
| Counting frames without the centered padding’s extra frame | 3000 where Whisper computes 3001 | q18 a (canary) |
| instead of for weighted overlap-add | reconstructions scaled by 3/4 | q18 d (canary 2) |
| The symmetric Hann window | q18 f (canary) | |
| 20 for a power | decibels doubled | q19 d (canary 60) |
| Applying only the floor in Whisper’s scaling | where the 8-unit clip gives | q19 e (canary) |
| Mixing into a nats bound | instead of | q20 c (canary) |
| Dividing WER by the hypothesis length | where the answer is 2 | q23 (canary) |
6. Where it’s used next
Section titled “6. Where it’s used next”| Direction | Module | How it uses this |
|---|---|---|
| Back | M12.1 | conv sizes, Toeplitz form, im2col and col2im (q1 to q9) |
| Back | M12.2 | strides and patches (q10) |
| Back | M12.3 | aliasing, interpolation kernels, antialias support (q11 to q14) |
| Back | M12.4 | DFT properties and FFT counts (q15 to q17) |
| Back | M12.5, M12.6 | frames, windows, mel, and Whisper’s scaling (q18, q19) |
| Forward | L13.1, L13.3 | conv layers and the ViT patch embedding (with B14’s L13 group) |
| Forward | L13.5 | CLIP and SigLIP contrastive training (q20 to q22) |
| Forward | L14.1, L14.2, L14.5 | the Whisper frontend, stem, and the WER the speech evals report (q18, q19, q23, q24) |