Exercise: compressed sensing

Goal

By the end of this exercise you should be able to:

  • explain why a sparse signal can be recovered from fewer measurements than unknowns, and which solution ℓ1 picks,
  • predict the zero-filled reconstruction of regular and random undersampling,
  • say which measurements are incoherent with which sparsity basis,
  • read a phase-transition diagram,
  • (math) prove that the null space property is sufficient, compute a mutual coherence, and derive the fold-over of regular undersampling.

Background

Idea What it means
Underdetermined M < N equations: infinitely many solutions \mathbf x + \mathbf h, A\mathbf h = 0
Minimum norm A^\top(AA^\top)^{-1}\mathbf y: spreads the weight
ℓ1 recovery \min\|\mathbf x\|_1 subject to A\mathbf x = \mathbf y (basis pursuit); with noise, the lasso
Zero filling unmeasured coefficients set to 0, transform back: A^\top\mathbf y for orthonormal rows
Coherent aliasing regular undersampling: copies
Incoherent aliasing random undersampling: noise-like junk
Measurements needed of the order of K\log(N/K)
Phase diagram δ = M/N against ρ = K/M; sharp boundary between success and failure

By hand

C1. A = \begin{pmatrix}1 & 1 & 1 & 1\\ 1 & -1 & 2 & 0\end{pmatrix}, \mathbf y = (3, 6). (a) Check that \mathbf x_a = (1.2, -0.6, 2.1, 0.3) and \mathbf x_b = (0, 0, 3, 0) both solve A\mathbf x = \mathbf y. (b) Compute their ℓ1 and ℓ2 norms. Which does each criterion prefer? (c) Why is \mathbf x_b the only solution with a single nonzero? (d) Compute the minimum-norm solution A^\top(AA^\top)^{-1}\mathbf y and compare with \mathbf x_a.

C2. (a) How many bits are needed to name the positions of 8 nonzeros among 256 samples? Compare with storing 256 samples. (b) Evaluate K\ln(N/K) for K = 8, N = 256 and for K = 1000, N = 10^6.

C3. Fold-over. A signal of N = 16 samples has one spike at n = 3. (a) Keep every 2nd DFT coefficient (even k) and zero-fill. Where are the nonzero samples and how large are they? (b) Every 4th coefficient? (c) The same number of coefficients at random: what kind of reconstruction do you expect?

C4. Which measurements are useless and which are good? (a) A spike signal measured at random pixels, or by random Fourier coefficients? (b) A sum of a few cosines measured at random pixels, or by random Fourier coefficients? (c) Explain both with the word “incoherent”.

C5. Reading the phase diagram of the lecture (ℓ1, Gaussian matrices; theoretical boundary ρ ≈ 0.27 at δ = 0.25, 0.39 at δ = 0.5, 0.57 at δ = 0.8). Predict success or failure: (a) N = 1000, M = 500, K = 100. (b) N = 1000, M = 250, K = 100. (c) N = 1000, M = 800, K = 400. (d) N = 1000, M = 500, K = 250. (e) How does the picture change when N doubles at fixed δ and ρ?

C6. A colleague undersamples a measurement 6-fold, reconstructs with ℓ1 in a wavelet basis and gets a crisp, clean image. What questions would you ask before trusting it?

C1. (a) \mathbf x_a: 1.2 - 0.6 + 2.1 + 0.3 = 3, 1.2 + 0.6 + 4.2 + 0 = 6 ✓. \mathbf x_b: 3 = 3, 2 \cdot 3 = 6 ✓. (b) \mathbf x_a: ℓ1 = 4.2, ℓ2 = 2.51; \mathbf x_b: ℓ1 = 3, ℓ2 = 3. ℓ2 prefers \mathbf x_a, ℓ1 prefers \mathbf x_b. (c) A single nonzero x_j requires x_j\mathbf a_j = \mathbf y = (3, 6), so column j must be proportional to (1, 2): only column 3. (d) AA^\top = \begin{pmatrix}4 & 2\\ 2 & 6\end{pmatrix}, (AA^\top)^{-1}\mathbf y = (0.3, 0.9), A^\top(0.3, 0.9) = (1.2, -0.6, 2.1, 0.3) = \mathbf x_a: the minimum-norm solution.

C2. (a) \log_2\binom{256}{8} = 48.5 bits, against 256 samples (at 8 bits: 2048 bits). (b) 8\ln 32 = 27.7; 1000\ln 1000 = 6908, about 0.7 % of 10^6.

C3. (a) Keeping the even coefficients averages the signal with its shift by N/2: \tfrac12(x_n + x_{n+8}). Nonzero samples at n = 3 and n = 11, each 0.5. (b) Shifts by N/4: samples 3, 7, 11, 15, each 0.25. (c) A peak at n = 3 (of height M/N) plus small, irregular, noise-like values everywhere else; a threshold can remove them.

C4. (a) Random Fourier coefficients; random pixels mostly miss the spikes. (b) Random pixels; each pixel sample sees every cosine, while a random set of Fourier coefficients misses the few frequencies that carry the signal. (c) The measurement basis must be spread out (incoherent) with respect to the sparsity basis: pixels and Fourier are incoherent with each other; pixels with spikes, or Fourier with cosines, are the same basis.

C5. (a) δ = 0.5, ρ = 0.2 < 0.39: success. (b) δ = 0.25, ρ = 0.4 > 0.27: failure. (c) δ = 0.8, ρ = 0.5 < 0.57: success, but close to the boundary. (d) δ = 0.5, ρ = 0.5 > 0.39: failure. (e) The boundary stays at the same place, but the transition band gets narrower: the change from success to failure becomes sharper.

C6. Is the object really sparse in that wavelet basis (at the scale of the details that matter)? Where does 6-fold undersampling sit relative to the phase transition for the expected sparsity? Were the samples random (incoherent) or regular? Does a reconstruction from fewer or different samples give the same image? Is there a fully sampled reference for a similar object? Small low-contrast details can be removed by the ℓ1 shrinkage without any visible sign.

Math questions

C7. (math) Prove that the null space property (\|\mathbf h_S\|_1 < \|\mathbf h_{S^c}\|_1 for all nonzero \mathbf h with A\mathbf h = 0 and all |S| \le K) implies that every K-sparse \mathbf x is the unique ℓ1 minimiser among the solutions of A\mathbf z = A\mathbf x.

C8. (math) Normalise the columns of the matrix in C1 and compute its mutual coherence μ. Which K does the coherence guarantee K < \tfrac12(1 + 1/μ) cover?

C9. (math) Show that keeping the even DFT coefficients X_{2m} of a length-N signal and zero-filling gives \tfrac12(x_n + x_{n+N/2}).

C10. (math) Show that the zero-filled reconstruction from a set of DFT coefficients is the circular convolution of the signal with the inverse DFT of the mask.

C7. Let \mathbf x be supported on S, |S| \le K, and let \mathbf z = \mathbf x + \mathbf h be another solution, so A\mathbf h = 0, \mathbf h \ne 0. Then \|\mathbf z\|_1 = \|\mathbf x_S + \mathbf h_S\|_1 + \|\mathbf h_{S^c}\|_1 \ge \|\mathbf x_S\|_1 - \|\mathbf h_S\|_1 + \|\mathbf h_{S^c}\|_1 > \|\mathbf x_S\|_1 = \|\mathbf x\|_1 (triangle inequality, then the null space property). So every other solution has a strictly larger ℓ1 norm.

C8. Unit columns: (1, 1)/\sqrt2, (1, -1)/\sqrt2, (1, 2)/\sqrt5, (1, 0). Inner products: 0, 0.949, 0.707, 0.316, 0.707, 0.447. μ = 3/√10 = 0.949 (columns 1 and 3). \tfrac12(1 + 1/0.949) = 1.03: only K = 1. (It does hold here: the 1-sparse (0, 0, 3, 0) was found.)

C9. Zero-filled: z_n = \frac1N\sum_{m} X_{2m}e^{2πi\,2mn/N}. Write M(k) = \tfrac12(1 + e^{iπk}), which is 1 for even k and 0 for odd k: z_n = \frac1N\sum_k \tfrac12(1 + e^{iπk})X_ke^{2πikn/N} = \tfrac12\big(x_n + \frac1N\sum_k X_ke^{2πik(n + N/2)/N}\big) = \tfrac12(x_n + x_{n+N/2}).

C10. Zero filling multiplies the spectrum by the 0/1 mask P_k: Z_k = P_kX_k. Multiplication of DFTs is circular convolution of the signals (the convolution theorem of the Fourier unit): \mathbf z = \mathbf x * \mathbf p with \mathbf p = the inverse DFT of the mask. For the even mask \mathbf p is ½ at 0 and ½ at N/2: C9 again.

Explore: recovering a sparse signal

A signal of 256 samples, K-sparse in spikes, DCT or Haar (or compressible), measured by random Fourier coefficients, every r-th frequency, a Gaussian matrix or random pixels. Panels: the truth with the ℓ1 and OMP recoveries; what was measured; the zero-filled reconstruction (scaled by N/M); right: the point spread function of the sampling.

E1. Preset regular: copies. Describe the zero-filled signal and the PSF. Read the relative errors of ℓ1 and OMP. Now switch to random Fourier with the same M (preset random: noise-like junk).

E2. Preset ℓ1 recovers from 25 % (K = 10, M = 64). Lower M step by step with the slider. At which M does ℓ1 fail? And OMP? Compare with C2(b).

E3. Preset random pixels see nothing. How many of the 8 spikes are sampled? Raise M to 192: what is recovered now?

E4. Preset Gaussian matrix works for any basis (sparse in the DCT). Then switch the measurement to random Fourier with the same signal. Explain.

E5. Preset compressible signal: best-K error. Compare the ℓ1 error with the best-K error in the readout. Raise M to 160.

E6. Preset noise: stable, not exact. Read the errors. Double the noise. Does the error grow in proportion?

E7. Press animate ℓ1 iterations on the random-Fourier preset and describe what happens to the junk.

E1. Regular: each spike appears four times, a quarter of the length (64 samples) apart, all as tall as the original; the PSF has four equal peaks. ℓ1 fails (error 0.87), OMP too (1.11): the copies are as consistent with the data as the truth. Random rows (same M = 64): the zero-filled signal shows the spikes above noise-like ripples, the PSF one peak with small sidelobes; ℓ1 and OMP both recover exactly (error 0.000).

E2. ℓ1 recovers K = 10 spikes down to M = 36 and fails at 32 (error 0.75): an abrupt drop. OMP needs more: it recovers from M = 44 but fails at 40. K\ln(N/K) = 10\ln 25.6 = 32, so 36 measurements are about 1.1 times that.

E3. Only 2 of the 8 spikes are sampled; ℓ1 and OMP can only return those (error 0.82). With M = 192, 5 spikes are sampled and recovered (error 0.56): the others are simply never seen. No algorithm can recover an unsampled spike.

E4. Gaussian: exact recovery (error 0.000) of the DCT-sparse signal from 64 measurements. Random Fourier rows fail (ℓ1 error 0.92): the DCT atoms are almost Fourier atoms, so each measurement sees only the atoms of its own frequency, the same coherence problem as random pixels for spikes. (OMP even becomes numerically unstable.)

E5. ℓ1 error 0.094 with M = 96 against a best-24-term error of 0.053; with M = 160: 0.045 against 0.035 (best 40). The recovery is of the same order as the best approximation with about M/4 terms, as the theory promises; exact recovery is impossible because the signal is not exactly sparse.

E6. Noise 0.05: ℓ1 error 0.19, OMP 0.08. Noise 0.1: ℓ1 0.50, OMP 0.32. The error grows with the noise, roughly in proportion until spikes start to be missed. There is no exact recovery, but no blow-up either: stable.

E7. At first the estimate is the zero-filled signal with its junk; the largest spikes are kept, the junk below the threshold is removed, the data are re-predicted and the junk caused by the large spikes disappears from the residual; weaker spikes emerge in later iterations, until all spikes stand alone.

Explore: the phase transition

The diagram fills in progressively (N = 64; more cells and trials give a smoother picture but take longer, N = 128 sharpens the transition). Click a cell to see one of its trials.

E8. Wait for the ℓ1 diagram. Where does it follow the theoretical curve, and where not? Click a cell just below and just above the curve at δ ≈ 0.5.

E9. Preset OMP: lower boundary. Compare with ℓ1. Which method would you choose for a measurement at δ = 0.5 with ρ = 0.3?

E10. Preset with noise: the edge blurs. What changes, and why?

E8. The success region (yellow) lies below the curve and its edge follows it, slightly below it at small δ and with a blurred band of partial success, because N = 64 is small (the curve is the limit for large N). Below the curve the trial is recovered exactly; just above it the recovery is clean-looking but has wrong spikes.

E9. OMP’s boundary lies lower: at δ ≈ 0.5 OMP succeeds only up to ρ ≈ 0.25, ℓ1 up to ≈ 0.35 (in the lecture’s figure). At ρ = 0.3, ℓ1. OMP is faster and has no shrinkage bias, which matters when the problem is well inside the success region.

E10. With 5 % noise no recovery is exact; success now means an error below 10 %. The edge is less sharp, the success region shrinks a little, and some cells deep in the success region have occasional failures: noise blurs the transition.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in sparse/src/compressed_sensing/exercise.rs. mat_vec, mat_t_vec and largest_gain (the power method of the last unit) are given. The coding tasks are numbered R1, R2, …, because this page already uses C1, C2, … for its pen-and-paper questions.

R1. min_norm(a, y): the least-energy solution of A\mathbf x = \mathbf y with fewer equations than unknowns:

G = A Aᵀ                 # m × m, entry (i, j) = row i · row j
solve G w = y
x = Aᵀ w

The check is by-hand question C1 (d): (1.2, −0.6, 2.1, 0.3).

R2. coherence(a): the largest |\mathbf a_i^\top\mathbf a_j| / (\|\mathbf a_i\|\,\|\mathbf a_j\|) over pairs of different columns. The check is by-hand question C8: μ = 3/√10 = 0.949.

R3. zero_filled(x, keep): the DFT of the signal, the coefficients that are not kept set to 0, the inverse DFT (real part). The checks are by-hand question C3 (a) and (b): a spike at 3 of 16 samples comes back as 0.5 at 3 and 11 (every 2nd coefficient) and as 0.25 at 3, 7, 11, 15 (every 4th).

R4. recover(a, y, iterations): ℓ1 recovery. FISTA from the last unit, with λ lowered step by step towards 0 (so that the data are fitted almost exactly), then a refit on the support it found (no shrinkage bias):

τ = 1 / L,   λ = λmax = largest |a_jᵀ y|
x = 0,  z = 0,  t = 1
repeat N times:
    one FISTA step with λ (gradient step from z, soft threshold at τλ, momentum)
    λ = λ · factor                         # factor^N = 1/10⁴
S = the j with |x_j| > 1 % of the largest |x_j|
refit x on the columns in S by least squares

The checks: by-hand question C1, where ℓ1 must find the sparsest solution (0, 0, 3, 0), and 4 spikes among 80 unknowns from 40 random measurements, recovered exactly.

R5. success_rate(n, m, k, trials, seed): one cell of the phase diagram:

repeat trials times:
    A = m × n matrix of random normal numbers / sqrt(m)
    x = k entries ±1 at random positions, 0 elsewhere
    x̂ = recover(A, A x)
    success if ‖x̂ − x‖ < 1 % of ‖x‖
return the fraction of successes

The checks: N = 32, M = 24, K = 2 always succeeds; N = 32, M = 8, K = 6 never does.

Then cargo run -p sparse -- compressed-sensing and open the pictures in out/compressed-sensing/. The program measures a signal with 8 spikes of random sign in 256 samples through 64 Fourier coefficients, every 4th or chosen at random, zero-fills (scaled by N/M, so that a spike that comes back has its own height) and recovers by ℓ1. Then it prints a phase diagram for N = 64, 8 trials per cell, and draws it in phase-diagram.png (white: always recovered).

R6. Compare zero-filled-regular.png with zero-filled-random.png, and recovered-regular.png with recovered-random.png. Read the printed size of the junk and the recovery errors. Explain the difference with the words “copies” and “noise-like”.

R7. In the printed phase diagram, where does recovery switch from success to failure at δ = 0.25 and at δ = 0.5? Compare with the theoretical boundary of by-hand question C5 (ρ ≈ 0.27 and 0.39).

R8. Count the cost. One iteration of recover multiplies by A and by A^\top: about 2MN multiplications. How many for one cell of the program’s diagram at δ = 0.5? How many, and how much memory for the matrix, for an image of N = 10^6 pixels and M = 10^5 Gaussian measurements?

The model solution is sparse/src/compressed_sensing/solution.rs in the download.

R6. Every 4th coefficient: each spike appears four times, 64 samples apart, and the copies are as tall as the original (the largest value away from the true spikes is 1.67, as tall as a spike). ℓ1 cannot tell the copies from the truth: it returns 32 small nonzeros spread over all copies, relative error 0.87. Random coefficients: the spikes stand out above noise-like junk (RMS 0.40, largest 1.10, the spikes are 1 to 2 high); ℓ1 recovers exactly the 8 spikes, relative error 0.000.

R7. At δ = 0.25 (M = 16): all 8 trials succeed at ρ = 0.2, 6 of 8 at ρ = 0.3, none at 0.4. At δ = 0.5 (M = 32): all succeed at ρ = 0.2, 7 of 8 at 0.3, 5 of 8 at 0.4, none at 0.5. The switch lies near 0.3 and 0.4, close to the theoretical 0.27 and 0.39. At N = 64 the switch is spread over a band of about 0.1 in ρ, and a single cell can be off (only 8 trials).

R8. M = 32, N = 64: 2 \cdot 32 \cdot 64 = 4096 per iteration, 4 \cdot 10^6 for 1000 iterations, 3.3 \cdot 10^7 for a cell of 8 trials: fast. For N = 10^6, M = 10^5: 2 \cdot 10^{11} per iteration, and the matrix has 10^{11} numbers, 800 GB. This is why practical compressed sensing uses measurements with a fast transform, such as random Fourier coefficients (one FFT, N\log N) in MRI, the next unit.

Where this leads

Compressed sensing needs three things: a sparsifying basis, incoherent measurements, and a nonlinear reconstruction. MRI has all three: images are sparse in wavelets, the scanner measures Fourier coefficients, and which ones it measures is up to us. Next unit: fast MRI (compressed-sensing MRI).