openkspace-recon 0.2.0

K-space -> image reconstruction: IFFT, coil combination, corrections.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Partial Fourier (half-echo / half-ky) reconstruction via homodyne detection.
//!
//! Partial-Fourier acquisitions skip one asymmetric tail of k-space (ky or kx)
//! to shorten scan time, exploiting the approximate Hermitian symmetry of
//! k-space for real-valued objects. The missing tail must be synthesized or
//! its absence compensated for, otherwise the reconstructed image is blurred
//! along the undersampled axis.
//!
//! This module implements homodyne detection (Noll, Nishimura, Macovski,
//! "Homodyne detection in magnetic resonance imaging", IEEE TMI 10(2), 1991),
//! which uses:
//!   1. a low-pass phase estimate from the symmetric central region, and
//!   2. a ramp / step weighting that doubles the asymmetric acquired tail,
//!      to recover a sharp magnitude image without explicitly filling the
//!      missing k-space region.
//!
//! References consulted for the algorithm (no code copied):
//!   - Noll, Nishimura, Macovski, IEEE TMI 10(2), 1991.
//!   - Bernstein, King, Zhou, *Handbook of MRI Pulse Sequences*, Ch. 13.
//!   - McGibney et al., "Quantitative evaluation of several partial Fourier
//!     reconstruction algorithms used in MRI", MRM 30(1), 1993.

use ndarray::{s, Array2, Array3, Array4};
use num_complex::Complex32;
use rustfft::FftPlanner;
use std::sync::Arc;
use tracing::{debug, info};

/// Detected partial-Fourier sampling pattern along ky.
#[derive(Debug, Clone, Copy)]
pub struct PartialFourierPlan {
    /// Length of the ky axis in the reconstructed grid.
    pub ny: usize,
    /// DC (k=0) location along ky.
    pub ky_dc: usize,
    /// First sampled ky index (inclusive).
    pub ky_lo: usize,
    /// Last sampled ky index (inclusive).
    pub ky_hi: usize,
    /// Half-width of the symmetric low-frequency band around DC that is
    /// present on BOTH sides of DC (min of the two arms).
    pub sym_half: usize,
    /// Width of the Hann transition at both edges of the symmetric band.
    pub ramp: usize,
}

impl PartialFourierPlan {
    /// Detect a partial-Fourier pattern from the per-cell sampled mask.
    ///
    /// Returns `Some(plan)` only when exactly one side of DC is clipped
    /// (the other side is at least `min_asymmetry_ratio` times as long).
    /// GRAPPA-style regular undersampling patterns (periodic gaps) are
    /// rejected; use the GRAPPA strategy for those.
    #[allow(clippy::needless_range_loop)]
    pub fn detect(mask: &Array3<bool>, ky_dc: usize) -> Option<Self> {
        let ny = mask.shape()[1];
        if ny < 8 {
            return None;
        }
        let mut ky_any = vec![false; ny];
        for ky in 0..ny {
            if mask.slice(s![.., ky, ..]).iter().any(|&b| b) {
                ky_any[ky] = true;
            }
        }

        let ky_lo = ky_any.iter().position(|&b| b)?;
        let ky_hi = ny - 1 - ky_any.iter().rev().position(|&b| b)?;

        // Require the region to be dense (no gaps) inside [ky_lo, ky_hi].
        // Otherwise this is a parallel-imaging / GRAPPA pattern.
        let gaps = (ky_lo..=ky_hi).filter(|&k| !ky_any[k]).count();
        if gaps > 0 {
            debug!("PF: {} gaps in sampled range -- not partial Fourier", gaps);
            return None;
        }

        // DC must lie inside the acquired range.
        if ky_dc < ky_lo || ky_dc > ky_hi {
            debug!("PF: DC outside sampled range");
            return None;
        }

        let below = ky_dc - ky_lo;
        let above = ky_hi - ky_dc;
        let (shorter, longer) = if below <= above {
            (below, above)
        } else {
            (above, below)
        };
        if longer == 0 {
            return None;
        }
        let asymmetry = longer as f32 / shorter.max(1) as f32;
        if asymmetry < 1.10 {
            return None; // essentially symmetric -- treat as fully sampled
        }
        if shorter < 4 {
            debug!(
                "PF: symmetric region too small ({} < 4) for reliable phase",
                shorter
            );
            return None;
        }

        let sym_half = shorter;
        let ramp = (sym_half / 4).max(2).min(sym_half - 1);

        Some(Self {
            ny,
            ky_dc,
            ky_lo,
            ky_hi,
            sym_half,
            ramp,
        })
    }
}

/// Hann window of length `n` (`0.5 - 0.5 * cos(2 pi i / (n - 1))`), peak 1.
fn hann_window(n: usize) -> Vec<f32> {
    if n == 0 {
        return vec![];
    }
    if n == 1 {
        return vec![1.0];
    }
    (0..n)
        .map(|i| {
            let x = (i as f32) / ((n - 1) as f32);
            0.5 - 0.5 * (std::f32::consts::TAU * x).cos()
        })
        .collect()
}

/// Build the symmetric low-pass weight `L[ky]` around DC, Hann-windowed
/// on `[ky_dc - sym_half, ky_dc + sym_half]`, zero elsewhere.
fn low_pass_weights(plan: &PartialFourierPlan) -> Vec<f32> {
    let mut w = vec![0.0f32; plan.ny];
    let lo = plan.ky_dc.saturating_sub(plan.sym_half);
    let hi = (plan.ky_dc + plan.sym_half).min(plan.ny - 1);
    let width = hi - lo + 1;
    let hann = hann_window(width);
    for (idx, ky) in (lo..=hi).enumerate() {
        w[ky] = hann[idx];
    }
    w
}

/// Build the homodyne weight `H[ky]`: 2 on the acquired asymmetric tail,
/// 1 on the symmetric band, 0 on the missing tail, with Hann-ramp edges.
#[allow(clippy::needless_range_loop)]
fn homodyne_weights(plan: &PartialFourierPlan) -> Vec<f32> {
    let mut w = vec![0.0f32; plan.ny];
    let c = plan.ky_dc as isize;
    let sym = plan.sym_half as isize;
    let r = plan.ramp as isize;

    // The "long" side is the asymmetric tail that gets weight 2.
    let below_len = c - plan.ky_lo as isize; // samples on the k<0 side
    let above_len = plan.ky_hi as isize - c; // samples on the k>0 side
    let above_is_long = above_len >= below_len;

    // Region boundaries (inclusive):
    //   missing tail:           outside [ky_lo, ky_hi]               -> 0
    //   symmetric band:         [c-sym, c+sym] except edges          -> 1
    //   asymmetric acquired:    beyond the symmetric band on long side -> 2
    //   ramps at:
    //     - (sym - ramp .. sym + ramp) transitioning 1 -> 2 (long side)
    //     - (sym - ramp .. sym + ramp) transitioning 1 -> 0 on the short side
    //       (mirrored Hann decays into the missing tail)
    for ky in plan.ky_lo..=plan.ky_hi {
        let d = ky as isize - c; // signed distance from DC
        let ad = d.abs();
        let on_long_side = (d >= 0 && above_is_long) || (d <= 0 && !above_is_long);

        let weight = if ad <= sym - r {
            1.0
        } else if ad <= sym + r {
            // Hann ramp of width 2*r centered at `sym`.
            let t = ((ad - (sym - r)) as f32) / ((2 * r) as f32);
            let hann_val = 0.5 - 0.5 * (std::f32::consts::TAU * t).cos();
            if on_long_side {
                // goes from 1 up to 2
                1.0 + hann_val
            } else {
                // goes from 1 down to 0
                1.0 - hann_val
            }
        } else if on_long_side {
            2.0
        } else {
            0.0
        };
        w[ky] = weight;
    }
    w
}

/// Centred 2-D inverse FFT of a single `[ny, nx]` complex plane, in place.
///
/// Pre-shifts with `ifftshift`, runs row-wise then column-wise inverse FFT,
/// normalises by `1/N`, and post-shifts with `fftshift` -- the same
/// convention used by [`crate::fft::ifft2_inplace`] but on a 2-D plane
/// accessed as a mutable view.
fn ifft2_plane(
    plane: &mut Array2<Complex32>,
    ifft_x: &Arc<dyn rustfft::Fft<f32>>,
    ifft_y: &Arc<dyn rustfft::Fft<f32>>,
) {
    let (ny, nx) = plane.dim();
    // ifftshift along ky (rows)
    let half_y = ny / 2;
    for x in 0..nx {
        let mut col: Vec<Complex32> = (0..ny).map(|y| plane[[y, x]]).collect();
        col.rotate_left(half_y);
        for y in 0..ny {
            plane[[y, x]] = col[y];
        }
    }
    // ifftshift along kx (cols)
    let half_x = nx / 2;
    for y in 0..ny {
        let mut row: Vec<Complex32> = (0..nx).map(|x| plane[[y, x]]).collect();
        row.rotate_left(half_x);
        for x in 0..nx {
            plane[[y, x]] = row[x];
        }
    }
    // IFFT along x (per row)
    let mut row_buf = vec![Complex32::new(0.0, 0.0); nx];
    for y in 0..ny {
        for x in 0..nx {
            row_buf[x] = plane[[y, x]];
        }
        ifft_x.process(&mut row_buf);
        let scale = 1.0 / (nx as f32);
        for x in 0..nx {
            plane[[y, x]] = row_buf[x] * scale;
        }
    }
    // IFFT along y (per column)
    let mut col_buf = vec![Complex32::new(0.0, 0.0); ny];
    for x in 0..nx {
        for y in 0..ny {
            col_buf[y] = plane[[y, x]];
        }
        ifft_y.process(&mut col_buf);
        let scale = 1.0 / (ny as f32);
        for y in 0..ny {
            plane[[y, x]] = col_buf[y] * scale;
        }
    }
    // fftshift along ky and kx
    for x in 0..nx {
        let mut col: Vec<Complex32> = (0..ny).map(|y| plane[[y, x]]).collect();
        col.rotate_left(ny - half_y);
        for y in 0..ny {
            plane[[y, x]] = col[y];
        }
    }
    for y in 0..ny {
        let mut row: Vec<Complex32> = (0..nx).map(|x| plane[[y, x]]).collect();
        row.rotate_left(nx - half_x);
        for x in 0..nx {
            plane[[y, x]] = row[x];
        }
    }
}

/// Homodyne reconstruction of a `[nc, nz, ny, nx]` k-space tensor along ky.
///
/// Replaces the standard 2-D IFFT + coil-combine step. The input is raw
/// k-space (acquired rows only, missing rows are zeros); the output is a
/// real `[nz, ny, nx]` magnitude volume obtained by RSS over coils.
///
/// Per slice and per coil:
///   1. Estimate phase `phi(y, x)` from the Hann-windowed symmetric
///      central band of k-space (low-pass IFFT).
///   2. Build the homodyne weighting `H(ky)` (2 on the asymmetric acquired
///      tail, 1 on the symmetric band, 0 on the missing tail) and apply it
///      to the coil's k-space plane.
///   3. IFFT the weighted plane and take `Re(img * exp(-i * phi))` as the
///      coil image estimate.
///
/// After the per-coil magnitude is computed, RSS combines across coils.
pub fn homodyne_reconstruct(kspace: &Array4<Complex32>, plan: &PartialFourierPlan) -> Array3<f32> {
    let (nc, nz, ny, nx) = (
        kspace.shape()[0],
        kspace.shape()[1],
        kspace.shape()[2],
        kspace.shape()[3],
    );
    assert_eq!(ny, plan.ny, "plan ny does not match kspace shape");
    info!(
        "Partial Fourier (homodyne): ny={}, ky=[{}, {}] (dc={}), sym_half={}, ramp={}",
        ny, plan.ky_lo, plan.ky_hi, plan.ky_dc, plan.sym_half, plan.ramp
    );

    let h_weights = homodyne_weights(plan);
    let l_weights = low_pass_weights(plan);

    let mut planner = FftPlanner::<f32>::new();
    let ifft_x = planner.plan_fft_inverse(nx);
    let ifft_y = planner.plan_fft_inverse(ny);

    let mut out = Array3::<f32>::zeros((nz, ny, nx));

    for kz in 0..nz {
        let mut rss_sq = Array2::<f32>::zeros((ny, nx));

        for ch in 0..nc {
            // Copy the (ky, kx) plane for this coil/slice.
            let mut homo = Array2::<Complex32>::zeros((ny, nx));
            let mut lowp = Array2::<Complex32>::zeros((ny, nx));
            for y in 0..ny {
                let hw = h_weights[y];
                let lw = l_weights[y];
                for x in 0..nx {
                    let k = kspace[[ch, kz, y, x]];
                    homo[[y, x]] = k * hw;
                    lowp[[y, x]] = k * lw;
                }
            }
            ifft2_plane(&mut homo, &ifft_x, &ifft_y);
            ifft2_plane(&mut lowp, &ifft_x, &ifft_y);

            // Demodulate and keep real part as the coil image estimate.
            for y in 0..ny {
                for x in 0..nx {
                    let phase = lowp[[y, x]];
                    let norm = phase.norm();
                    let demod = if norm > 1e-12 {
                        Complex32::new(phase.re / norm, -phase.im / norm)
                    } else {
                        Complex32::new(1.0, 0.0)
                    };
                    let img = homo[[y, x]] * demod;
                    let m = img.re;
                    // RSS contributions are squared per-coil magnitudes;
                    // here we keep the homodyne sign and use |Re|^2 so
                    // low-SNR regions don't accidentally double-up.
                    rss_sq[[y, x]] += m * m;
                }
            }
        }

        for y in 0..ny {
            for x in 0..nx {
                out[[kz, y, x]] = rss_sq[[y, x]].sqrt();
            }
        }
    }

    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use ndarray::Array3;

    #[test]
    fn detect_rejects_symmetric_mask() {
        // Fully sampled -- should not be flagged as partial Fourier.
        let mask = Array3::<bool>::from_elem((1, 64, 4), true);
        assert!(PartialFourierPlan::detect(&mask, 32).is_none());
    }

    #[test]
    fn detect_rejects_regular_undersampling() {
        // R=2 pattern -- should not be flagged (has gaps).
        let mut mask = Array3::<bool>::from_elem((1, 64, 4), false);
        for ky in (0..64).step_by(2) {
            for x in 0..4 {
                mask[[0, ky, x]] = true;
            }
        }
        assert!(PartialFourierPlan::detect(&mask, 32).is_none());
    }

    #[test]
    fn detect_flags_6_8_partial_fourier() {
        // ny=64, acquire ky in [8, 63] -> below DC = 24 samples, above = 31.
        // Asymmetry ~ 31/24 = 1.29 -> flagged.
        let mut mask = Array3::<bool>::from_elem((1, 64, 4), false);
        for ky in 8..64 {
            for x in 0..4 {
                mask[[0, ky, x]] = true;
            }
        }
        let plan = PartialFourierPlan::detect(&mask, 32).expect("PF detected");
        assert_eq!(plan.ky_lo, 8);
        assert_eq!(plan.ky_hi, 63);
        assert_eq!(plan.ky_dc, 32);
        // symmetric half = min(32-8, 63-32) = min(24, 31) = 24
        assert_eq!(plan.sym_half, 24);
    }

    #[test]
    fn homodyne_weights_sum_plausible() {
        // Construct a plan and verify the weighting properties:
        //  - weight is 0 outside sampled range
        //  - weight is ~2 deep into the long tail
        //  - weight is ~1 at DC
        let mut mask = Array3::<bool>::from_elem((1, 128, 2), false);
        for ky in 16..128 {
            mask[[0, ky, 0]] = true;
            mask[[0, ky, 1]] = true;
        }
        let plan = PartialFourierPlan::detect(&mask, 64).expect("PF");
        let w = homodyne_weights(&plan);
        // Outside sampled range
        assert!((0..16).all(|k| w[k] == 0.0));
        // At DC
        assert!((w[64] - 1.0).abs() < 1e-4);
        // Deep in the long (upper) tail, past the ramp region.
        // sym_half=48, ramp=12, so indices past DC+60 are pure 2.
        assert!((w[127] - 2.0).abs() < 1e-4);
    }

    /// Homodyne on a phantom with a spatial phase ramp should recover the
    /// true magnitude image with low NRMSE. A plain zero-filled IFFT on
    /// the same partial k-space blurs the long axis and mixes phase into
    /// magnitude -- we check that homodyne's NRMSE is materially lower.
    #[test]
    fn homodyne_beats_zero_fill_on_real_phantom() {
        // 1-coil, 1-slice, 64x32 image: a centred gaussian blob plus a
        // smooth spatial phase ramp. Truth = the magnitude image.
        use rustfft::num_complex::Complex32 as C32;
        let ny = 64;
        let nx = 32;

        let mut truth = Array2::<f32>::zeros((ny, nx));
        let mut complex_img = Array2::<C32>::zeros((ny, nx));
        for y in 0..ny {
            for x in 0..nx {
                let yy = y as f32 - ny as f32 / 2.0;
                let xx = x as f32 - nx as f32 / 2.0;
                let r2 = (yy * yy) / 90.0 + (xx * xx) / 40.0;
                let mag = (-r2).exp();
                // Non-trivial but smooth phase (typical of coil sensitivities).
                let phase = 0.35 * (yy / ny as f32) + 0.25 * (xx / nx as f32);
                truth[[y, x]] = mag;
                complex_img[[y, x]] = C32::new(mag * phase.cos(), mag * phase.sin());
            }
        }
        // Forward FFT to build full k-space (centred convention).
        let mut planner = FftPlanner::<f32>::new();
        let fft_x = planner.plan_fft_forward(nx);
        let fft_y = planner.plan_fft_forward(ny);

        let mut full_k = Array2::<C32>::zeros((ny, nx));
        for y in 0..ny {
            for x in 0..nx {
                full_k[[y, x]] = complex_img[[y, x]];
            }
        }
        // fftshift + FFT + fftshift to produce a centred k-space
        let half_y = ny / 2;
        let half_x = nx / 2;
        for x in 0..nx {
            let mut col: Vec<C32> = (0..ny).map(|y| full_k[[y, x]]).collect();
            col.rotate_left(half_y);
            for y in 0..ny {
                full_k[[y, x]] = col[y];
            }
        }
        for y in 0..ny {
            let mut row: Vec<C32> = (0..nx).map(|x| full_k[[y, x]]).collect();
            row.rotate_left(half_x);
            for x in 0..nx {
                full_k[[y, x]] = row[x];
            }
        }
        let mut row_buf = vec![C32::new(0.0, 0.0); nx];
        for y in 0..ny {
            for x in 0..nx {
                row_buf[x] = full_k[[y, x]];
            }
            fft_x.process(&mut row_buf);
            for x in 0..nx {
                full_k[[y, x]] = row_buf[x];
            }
        }
        let mut col_buf = vec![C32::new(0.0, 0.0); ny];
        for x in 0..nx {
            for y in 0..ny {
                col_buf[y] = full_k[[y, x]];
            }
            fft_y.process(&mut col_buf);
            for y in 0..ny {
                full_k[[y, x]] = col_buf[y];
            }
        }
        for x in 0..nx {
            let mut col: Vec<C32> = (0..ny).map(|y| full_k[[y, x]]).collect();
            col.rotate_left(ny - half_y);
            for y in 0..ny {
                full_k[[y, x]] = col[y];
            }
        }
        for y in 0..ny {
            let mut row: Vec<C32> = (0..nx).map(|x| full_k[[y, x]]).collect();
            row.rotate_left(nx - half_x);
            for x in 0..nx {
                full_k[[y, x]] = row[x];
            }
        }

        // Zero out ky < 16 (6/8 partial Fourier-ish).
        let ky_lo = 16;
        let mut partial_k = Array4::<C32>::zeros((1, 1, ny, nx));
        let mut mask = Array3::<bool>::from_elem((1, ny, nx), false);
        for y in 0..ny {
            for x in 0..nx {
                if y >= ky_lo {
                    partial_k[[0, 0, y, x]] = full_k[[y, x]];
                    mask[[0, y, x]] = true;
                }
            }
        }

        let plan = PartialFourierPlan::detect(&mask, ny / 2).expect("PF plan");
        let recon = homodyne_reconstruct(&partial_k, &plan);

        // Zero-filled reference: IFFT(partial_k), magnitude, RSS (single coil).
        let mut zf = Array2::<C32>::zeros((ny, nx));
        for y in 0..ny {
            for x in 0..nx {
                zf[[y, x]] = partial_k[[0, 0, y, x]];
            }
        }
        let ifft_x = planner.plan_fft_inverse(nx);
        let ifft_y = planner.plan_fft_inverse(ny);
        ifft2_plane(&mut zf, &ifft_x, &ifft_y);

        // Compare NRMSE of each against the truth over the central
        // region (edges are affected by the FFT boundary in both).
        let y0 = ny / 4;
        let y1 = 3 * ny / 4;
        let x0 = nx / 4;
        let x1 = 3 * nx / 4;

        // Normalise both to match integrated energy of truth (scale factor).
        let mut t_energy = 0.0f32;
        let mut h_energy = 0.0f32;
        let mut z_energy = 0.0f32;
        for y in y0..y1 {
            for x in x0..x1 {
                t_energy += truth[[y, x]] * truth[[y, x]];
                let h = recon[[0, y, x]];
                h_energy += h * h;
                let z = zf[[y, x]].norm();
                z_energy += z * z;
            }
        }
        let h_scale = (t_energy / h_energy.max(1e-20)).sqrt();
        let z_scale = (t_energy / z_energy.max(1e-20)).sqrt();

        let mut h_err = 0.0f32;
        let mut z_err = 0.0f32;
        let mut t_norm = 0.0f32;
        for y in y0..y1 {
            for x in x0..x1 {
                let t = truth[[y, x]];
                let h = recon[[0, y, x]] * h_scale;
                let z = zf[[y, x]].norm() * z_scale;
                h_err += (t - h) * (t - h);
                z_err += (t - z) * (t - z);
                t_norm += t * t;
            }
        }
        let h_nrmse = (h_err / t_norm.max(1e-20)).sqrt();
        let z_nrmse = (z_err / t_norm.max(1e-20)).sqrt();
        assert!(
            h_nrmse < 0.2,
            "homodyne NRMSE {:.4} should be small (zf NRMSE {:.4})",
            h_nrmse,
            z_nrmse
        );
    }
}