From be3ee94494bca1ebc2e944c8e45adac68d5fcde0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 20 Aug 2026 09:58:37 +0300 Subject: [PATCH] test(aac): QMF-domain exact third witness for SBR HF content (round-46) Adds HfAnalysis (a 64-band forward analysis bank sharing Synthesis's own convention) plus a validated self-consistency control, and rebuilds the STFT-approximate patch-copy simulator (round-45) in this exact band grid (EC_AAC_SBR_QMF_WITNESS) with a per-band 2nd-order complex transfer fit between the plain-copy sim and the reference's real HF content (hf_patch_transfer_fit) plus a bw-value dump (EC_AAC_SBR_BW_DUMP) for comparison against our own chirp coefficients. On reeln, the exact-domain witness overturns round-45's STFT-based lead: for patch (3,28,11) sim-vs-reference clears 0.5 on most bands (0.42-0.79) while sim-vs-ours stays well under it (0.26-0.42) -- the opposite polarity from the STFT approximation's hint. The fitted sim-to-reference transfer on that same patch shows non-trivial order-2 structure with a phase signature (~i, ~-1, ~-i across taps 0/1/2) matching this file's own already-characterized (and currently disabled) band_shift_correction quarter-turn convention -- flagged in the ledger as the next concrete thing to re-test with this sharper instrument, not implemented here (round-37's contrary real-file evidence used a scale-trap-prone 8-bin STFT metric this round's own control work found unreliable; re-enabling a previously-reverted correction needs its own verification budget). --- crates/ec-aac/src/lib.rs | 2 +- crates/ec-aac/src/sbr_hf.rs | 7 + crates/ec-aac/src/sbr_qmf.rs | 218 +++++++++++++++++++ crates/ec-aac/tests/sbr_real_library.rs | 273 ++++++++++++++++++++++++ 4 files changed, 499 insertions(+), 1 deletion(-) diff --git a/crates/ec-aac/src/lib.rs b/crates/ec-aac/src/lib.rs index 07f254e7..2175849a 100644 --- a/crates/ec-aac/src/lib.rs +++ b/crates/ec-aac/src/lib.rs @@ -35,7 +35,7 @@ mod sbr_chain; mod sbr_env; mod sbr_hf; mod sbr_payload; -mod sbr_qmf; +pub mod sbr_qmf; mod sbr_tables; pub mod tables; diff --git a/crates/ec-aac/src/sbr_hf.rs b/crates/ec-aac/src/sbr_hf.rs index 11c881a9..21fa9b36 100644 --- a/crates/ec-aac/src/sbr_hf.rs +++ b/crates/ec-aac/src/sbr_hf.rs @@ -400,6 +400,13 @@ pub fn generate( stabilize(lpc2(&ext)) }; let g = bw[noise_band_of(target_band)]; + // (Round-46, Task 2) `EC_AAC_SBR_BW_DUMP` -- one line per patched + // band per frame with the smoothed chirp bw actually applied, + // to compare against the QMF-domain fitted reference transfer + // (`hf_patch_transfer_fit` in the real-file test). + if std::env::var("EC_AAC_SBR_BW_DUMP").is_ok() { + eprintln!("BW_DUMP target_band={target_band} source_band={source_band} bw={g:.6}"); + } let ca1 = a1.scale(g); let ca2 = a2.scale(g * g); let (mut y_prev1, mut y_prev2) = if source_band < history.y.len() { diff --git a/crates/ec-aac/src/sbr_qmf.rs b/crates/ec-aac/src/sbr_qmf.rs index 7223fc56..2a39599a 100644 --- a/crates/ec-aac/src/sbr_qmf.rs +++ b/crates/ec-aac/src/sbr_qmf.rs @@ -224,6 +224,69 @@ impl Default for Analysis { } } +/// A [`SYNTHESIS_BANDS`]-channel complex analysis bank sharing [`Synthesis`]'s +/// own cutoff/phase convention (same `omega_step`, same prototype), forward +/// direction: doubled-rate PCM in, complex subband slots out, one slot per +/// [`SYNTHESIS_BANDS`] input samples. The decoder itself never runs this -- +/// it only ever synthesizes at this band count, from HF-generated subbands, +/// never analyzes a full-rate PCM signal back into them -- so this exists +/// for exact QMF-domain measurement only (round-46's third-witness +/// instrument): it lets a test read the reference decoder's and our own +/// already-decoded 44100Hz output straight into the same 64-band grid the +/// patch map's band indices (`rate/128`-wide, matching `SYNTHESIS_BANDS`'s +/// own `omega_step`) are defined in, with no STFT-bin-width approximation. +pub struct HfAnalysis { + hist: Vec, + cos_tab: Vec, + sin_tab: Vec, +} + +impl HfAnalysis { + pub fn new() -> HfAnalysis { + let omega_step = std::f64::consts::PI / (2.0 * SYNTHESIS_BANDS as f64); + let h = prototype(PROTO_LEN, omega_step, KAISER_BETA); + let gain = analytic_gain(&h); + let (cos_tab, sin_tab) = modulation_tables(&h, SYNTHESIS_BANDS, omega_step, gain); + HfAnalysis { + hist: vec![0.0; PROTO_LEN], + cos_tab, + sin_tab, + } + } + + /// Feeds exactly [`SYNTHESIS_BANDS`] new samples (oldest first, matching + /// PCM order) and returns the [`SYNTHESIS_BANDS`] complex subband + /// samples for this slot. + pub fn process_slot( + &mut self, + samples: &[f32; SYNTHESIS_BANDS], + ) -> [Complex; SYNTHESIS_BANDS] { + for &s in samples { + self.hist.copy_within(0..PROTO_LEN - 1, 1); + self.hist[0] = s as f64; + } + let mut out = [Complex::new(0.0, 0.0); SYNTHESIS_BANDS]; + for (k, slot) in out.iter_mut().enumerate() { + let row = k * PROTO_LEN; + let mut re = 0.0f64; + let mut im = 0.0f64; + for n in 0..PROTO_LEN { + let x = self.hist[n]; + re += x * self.cos_tab[row + n]; + im += x * self.sin_tab[row + n]; + } + *slot = Complex::new(re, im); + } + out + } +} + +impl Default for HfAnalysis { + fn default() -> HfAnalysis { + HfAnalysis::new() + } +} + /// The 64-band complex synthesis QMF bank: complex subband slots in, core (or /// doubled-rate, when fed above-Nyquist bands) PCM out, [`SYNTHESIS_BANDS`] /// output samples per slot. @@ -450,6 +513,161 @@ mod tests { ); } + /// (a2, round-46 control) `HfAnalysis` self-consistency, required before + /// trusting it as a measurement instrument: a pure tone centred in one + /// band's passband must (1) come back out concentrated in that same + /// band index, at high self-coherence with its own steady-state phase + /// ramp, and (2) round-trip through `Synthesis` (same band count, same + /// convention) back to a signal correlating with the original above the + /// same 0.9999 bar the 32/64 pair clears. + #[test] + fn hf_analysis_self_consistency_control() { + let n = 8192; + // Band 20 of 64 spans omega in [20*step, 21*step), step = pi/128; + // centre it exactly at (20.5)*step. + let step = std::f64::consts::PI / (2.0 * SYNTHESIS_BANDS as f64); + let target_band = 20usize; + let omega = (target_band as f64 + 0.5) * step; + let x: Vec = (0..n).map(|i| (omega * i as f64).sin()).collect(); + + let mut ana = HfAnalysis::new(); + let mut energies = vec![0.0f64; SYNTHESIS_BANDS]; + let mut band_series: Vec> = Vec::new(); + for slot in x.chunks(SYNTHESIS_BANDS) { + if slot.len() < SYNTHESIS_BANDS { + break; + } + let mut chunk = [0.0f32; SYNTHESIS_BANDS]; + for (d, &s) in chunk.iter_mut().zip(slot) { + *d = s as f32; + } + let sub = ana.process_slot(&chunk); + for (k, c) in sub.iter().enumerate() { + energies[k] += c.re * c.re + c.im * c.im; + } + band_series.push(sub[target_band]); + } + // Concentration: the target band must carry the large majority of + // total energy once the startup transient (first PROTO_LEN samples) + // has flushed through. + let margin_slots = PROTO_LEN / SYNTHESIS_BANDS + 4; + let mut energies_steady = vec![0.0f64; SYNTHESIS_BANDS]; + let mut ana2 = HfAnalysis::new(); + let mut steady_series: Vec> = Vec::new(); + for (i, slot) in x.chunks(SYNTHESIS_BANDS).enumerate() { + if slot.len() < SYNTHESIS_BANDS { + break; + } + let mut chunk = [0.0f32; SYNTHESIS_BANDS]; + for (d, &s) in chunk.iter_mut().zip(slot) { + *d = s as f32; + } + let sub = ana2.process_slot(&chunk); + if i >= margin_slots { + for (k, c) in sub.iter().enumerate() { + energies_steady[k] += c.re * c.re + c.im * c.im; + } + steady_series.push(sub[target_band]); + } + } + let total: f64 = energies_steady.iter().sum(); + let frac = energies_steady[target_band] / total; + println!("HfAnalysis control: band{target_band} energy fraction = {frac:.4}"); + let mut top: Vec<(usize, f64)> = energies_steady.iter().cloned().enumerate().collect(); + top.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); + println!("HfAnalysis control: top bands = {:?}", &top[..6]); + // This is a critically-sampled, adjacent-overlapping cosine-modulated + // bank by design (`prototype_is_near_perfect_reconstruction`'s own + // -6dB-at-half-a-band crossover): a tone dead-centre in one band's + // passband legitimately leaks a large fraction into its immediate + // neighbours (measured ~42% each side here) since neighbour centres + // sit only one band-width away, still inside this design's + // transition region (its `-40dB` bound is checked 1.5 bands out, not + // 1). The control this instrument actually needs is: the tone's OWN + // band must be the clear plurality winner (not tied/beaten by a + // neighbour) and carry a majority-scale share, not near-unity + // concentration. + assert_eq!( + top[0].0, target_band, + "tone in band {target_band} was NOT the dominant band (winner was band {})", + top[0].0 + ); + assert!( + frac > 0.3, + "tone in band {target_band} landed with only {frac:.4} of total energy in its own band" + ); + + // Self-coherence: within the target band, phase should advance at a + // near-constant rate slot-to-slot (a clean single-band tone, not + // noise) -- checked via the normalized magnitude of the mean + // per-slot phase-advance unit vector (1.0 for perfectly constant + // advance, near 0 for random phase). + let mut re_sum = 0.0f64; + let mut im_sum = 0.0f64; + let mut count = 0usize; + for w in steady_series.windows(2) { + let ratio = w[1] * w[0].conj(); + let mag = (ratio.re * ratio.re + ratio.im * ratio.im) + .sqrt() + .max(1e-300); + re_sum += ratio.re / mag; + im_sum += ratio.im / mag; + count += 1; + } + let coherence = (re_sum * re_sum + im_sum * im_sum).sqrt() / count as f64; + println!("HfAnalysis control: band{target_band} self-coherence = {coherence:.4}"); + assert!( + coherence > 0.99, + "tone's own-band phase advance not coherent: {coherence:.4}" + ); + + // Round trip: HfAnalysis -> Synthesis must reconstruct the tone. + let mut syn = Synthesis::new(); + let mut out = Vec::with_capacity(n); + let mut ana3 = HfAnalysis::new(); + for slot in x.chunks(SYNTHESIS_BANDS) { + if slot.len() < SYNTHESIS_BANDS { + break; + } + let mut chunk = [0.0f32; SYNTHESIS_BANDS]; + for (d, &s) in chunk.iter_mut().zip(slot) { + *d = s as f32; + } + let sub = ana3.process_slot(&chunk); + let mut v = [Complex::new(0.0, 0.0); SYNTHESIS_BANDS]; + v.copy_from_slice(&sub); + let pcm = syn.process_slot(&v); + out.extend(pcm.iter().map(|&s| s as f64)); + } + let margin = 3000usize; + let x_core = &x[margin..x.len() - margin]; + let mut best_corr = -1.0f64; + let mut best_lag = 0i64; + for lag in 0i64..4000 { + let start = (margin as i64 + lag) as usize; + if start + x_core.len() > out.len() { + continue; + } + let window = &out[start..start + x_core.len()]; + let corr = correlation(x_core, window); + if corr > best_corr { + best_corr = corr; + best_lag = lag; + } + } + println!("HfAnalysis control: round-trip correlation {best_corr:.6} at lag {best_lag}"); + // The `theta`/beta convention was tuned against the 32-analysis/ + // 64-synthesis PAIR `round_trip_reconstructs_the_passband` checks + // (0.9999 bar); this same-band-count (64/64) self-pairing is not the + // shape that tuning targeted, so a slightly looser bar applies -- + // still comfortably a near-perfect reconstruction, ruling out a + // gross convention mismatch (that would sit far below 0.99). + assert!( + best_corr >= 0.999, + "HfAnalysis->Synthesis round-trip correlation {best_corr} below 0.999" + ); + } + /// (b) Prototype sanity: the lowpass crosses -6 dB at its cutoff (the /// adjacent-band handoff point for a cosine-modulated bank) and is well /// attenuated a full subband away, within stated bounds. diff --git a/crates/ec-aac/tests/sbr_real_library.rs b/crates/ec-aac/tests/sbr_real_library.rs index cfef4534..bf898173 100644 --- a/crates/ec-aac/tests/sbr_real_library.rs +++ b/crates/ec-aac/tests/sbr_real_library.rs @@ -1276,6 +1276,229 @@ fn hf_patch_simulator(o: &[f32], t: &[f32], rate: u32) -> (BandCoherenceTable, B (band_coherence(&t_spec), band_coherence(&o_spec)) } +/// (Round-46) The QMF-domain exact version of [`hf_patch_simulator`]: same +/// plain-patch+gain model, same verified patch map, but read through the +/// crate's own [`ec_aac::sbr_qmf::HfAnalysis`] (64-band, `rate/128`-wide +/// bands -- exactly the patch map's own band unit) instead of an 8-bin-wide +/// STFT approximation, closing the "too crude to reach a verdict" gap +/// round-45's STFT simulator hit. Validated by +/// `hf_analysis_self_consistency_control` in `sbr_qmf.rs` before use here +/// (own-band plurality + self-coherence + round-trip control). Same +/// per-(band, window) energy-rescale-then-coherence method as the STFT +/// version, windowed over `WIN` consecutive QMF slots (`WIN * SYNTHESIS_BANDS +/// = 1024` samples, matching the STFT version's hop) so the rescale has +/// enough samples to estimate energy from, not so many that within-window +/// phase drift is averaged away. +const QMF_PATCHES: [(usize, usize, usize); 4] = [(0, 14, 14), (3, 28, 11), (0, 39, 3), (13, 42, 1)]; + +/// Shared build step for the QMF-domain third-witness instruments: both +/// PCMs read through [`ec_aac::sbr_qmf::HfAnalysis`], plus the plain +/// patch-copy simulation of the reference's own low band, all in the +/// `(band, slot)` domain. Returns `(ours, reference, sim)`. +/// `[band][slot]` complex QMF-domain series. +type QmfBandSlots = Vec>>; + +fn qmf_domain_spec(o: &[f32], t: &[f32]) -> (QmfBandSlots, QmfBandSlots, QmfBandSlots) { + use ec_aac::sbr_qmf::{HfAnalysis, SYNTHESIS_BANDS}; + let n = o.len().min(t.len()); + let slots = n / SYNTHESIS_BANDS; + let analyze = |pcm: &[f32]| -> QmfBandSlots { + let mut ana = HfAnalysis::new(); + let mut out: QmfBandSlots = (0..SYNTHESIS_BANDS) + .map(|_| Vec::with_capacity(slots)) + .collect(); + for s in 0..slots { + let mut chunk = [0.0f32; SYNTHESIS_BANDS]; + chunk.copy_from_slice(&pcm[s * SYNTHESIS_BANDS..(s + 1) * SYNTHESIS_BANDS]); + let sub = ana.process_slot(&chunk); + for (b, c) in sub.iter().enumerate() { + out[b].push(*c); + } + } + out + }; + let o_spec = analyze(o); + let t_spec = analyze(t); + // Plain patch copy: sim's target-band slot series is the reference's + // source-band slot series, unmodified. + let mut sim_spec = vec![vec![ec_dsp::Complex::new(0.0f64, 0.0); slots]; SYNTHESIS_BANDS]; + for (src, tgt, width) in QMF_PATCHES { + for i in 0..width { + if src + i >= SYNTHESIS_BANDS || tgt + i >= SYNTHESIS_BANDS { + continue; + } + sim_spec[tgt + i] = t_spec[src + i].clone(); + } + } + (o_spec, t_spec, sim_spec) +} + +fn hf_patch_simulator_qmf(o: &[f32], t: &[f32]) -> (BandCoherenceTable, BandCoherenceTable) { + use ec_aac::sbr_qmf::SYNTHESIS_BANDS; + const WIN: usize = 16; + let (o_spec, t_spec, sim_spec) = qmf_domain_spec(o, t); + let slots = sim_spec.first().map(Vec::len).unwrap_or(0); + if slots < WIN { + return (Vec::new(), Vec::new()); + } + let top_band = 43.min(SYNTHESIS_BANDS); + let windows = slots / WIN; + let band_coherence = |target: &[Vec>]| -> Vec<(usize, f64)> { + (14..top_band) + .map(|band| { + let mut cross_re = 0.0f64; + let mut cross_im = 0.0f64; + let mut sim_e = 0.0f64; + let mut tgt_e = 0.0f64; + for w in 0..windows { + let lo = w * WIN; + let hi = lo + WIN; + let se: f64 = sim_spec[band][lo..hi] + .iter() + .map(|c| c.re * c.re + c.im * c.im) + .sum(); + let te: f64 = target[band][lo..hi] + .iter() + .map(|c| c.re * c.re + c.im * c.im) + .sum(); + if se <= 1e-20 || te <= 0.0 { + continue; + } + let scale = (te / se).sqrt(); + for i in lo..hi { + let sc = sim_spec[band][i].scale(scale); + let tc = target[band][i]; + let cross = sc * tc.conj(); + cross_re += cross.re; + cross_im += cross.im; + sim_e += sc.re * sc.re + sc.im * sc.im; + tgt_e += tc.re * tc.re + tc.im * tc.im; + } + } + let den = (sim_e * tgt_e).sqrt(); + let coh = if den > 0.0 { + (cross_re * cross_re + cross_im * cross_im).sqrt() / den + } else { + 0.0 + }; + (band, coh) + }) + .collect() + }; + (band_coherence(&t_spec), band_coherence(&o_spec)) +} + +/// (Round-46, Task 2) Per-band 2nd-order complex prediction transfer fit +/// between the plain-copy sim series and the REFERENCE's own HF series for +/// that band: `ref[l] ~= a0*sim[l] + a1*sim[l-1] + a2*sim[l-2]`, least +/// squares over the whole file -- the same shape `sbr_hf::generate`'s own +/// chirp filter has (`y = residual + ca1*y[n-1] + ca2*y[n-2]`, an IIR rather +/// than this FIR, but both are order-2 linear reshapings of the same copied +/// content), so the fitted taps are directly comparable in spirit to our own +/// bandwidth-expanded LPC coefficients: near `(1,0,0)` means "reference is +/// also just a plain copy here", taps with real weight on lag 1/2 mean a +/// genuine order-2 transformation is present. +#[allow(clippy::needless_range_loop)] // fixed 3x3 complex Gaussian elimination, index math throughout +fn hf_patch_transfer_fit( + o: &[f32], + t: &[f32], + bands: &[usize], +) -> Vec<(usize, [ec_dsp::Complex; 3])> { + let (_, t_spec, sim_spec) = qmf_domain_spec(o, t); + let mut results = Vec::new(); + for &band in bands { + if band >= sim_spec.len() { + continue; + } + let sim_raw = &sim_spec[band]; + let ref_raw = &t_spec[band]; + let len = sim_raw.len().min(ref_raw.len()); + if len < 8 { + continue; + } + // Normalize each series to unit RMS first: the raw QMF-domain + // amplitudes differ by orders of magnitude (sim carries no envelope + // gain, `ref` does), which would otherwise make every fitted + // coefficient collapse toward the gain ratio rather than the + // fraction-of-`ref`-explained-by-which-lag this fit is meant to + // read off. + let rms = |s: &[ec_dsp::Complex]| -> f64 { + (s[..len] + .iter() + .map(|c| c.re * c.re + c.im * c.im) + .sum::() + / len as f64) + .sqrt() + .max(1e-300) + }; + let sim_scale = 1.0 / rms(sim_raw); + let ref_scale = 1.0 / rms(ref_raw); + let sim: Vec> = + sim_raw[..len].iter().map(|c| c.scale(sim_scale)).collect(); + let refb: Vec> = + ref_raw[..len].iter().map(|c| c.scale(ref_scale)).collect(); + let sim = &sim[..]; + let refb = &refb[..]; + // Complex 3x3 Hermitian normal equations: r[i][j] = sum sim[l-i] conj(sim[l-j]), + // p[i] = sum sim[l-i] conj(ref[l])... solved as R^T a = p with R real basis in + // complex arithmetic (Gaussian elimination with partial pivoting by magnitude). + let mut r = [[ec_dsp::Complex::new(0.0f64, 0.0); 3]; 3]; + let mut p = [ec_dsp::Complex::new(0.0f64, 0.0); 3]; + for l in 2..len { + let s = [sim[l], sim[l - 1], sim[l - 2]]; + let y = refb[l]; + for i in 0..3 { + p[i] = p[i] + s[i].conj() * y; + for j in 0..3 { + r[i][j] = r[i][j] + s[i].conj() * s[j]; + } + } + } + // Gaussian elimination on the augmented [r | p] system. + let mut aug = r; + let mut rhs = p; + for col in 0..3 { + let mut piv = col; + let mut best = aug[col][col].norm_sqr(); + for row in (col + 1)..3 { + let m = aug[row][col].norm_sqr(); + if m > best { + best = m; + piv = row; + } + } + if best < 1e-24 { + continue; + } + aug.swap(col, piv); + rhs.swap(col, piv); + let inv = aug[col][col].conj().scale(1.0 / best); + for row in (col + 1)..3 { + let f = aug[row][col] * inv; + for k in 0..3 { + aug[row][k] = aug[row][k] - f * aug[col][k]; + } + rhs[row] = rhs[row] - f * rhs[col]; + } + } + let mut a = [ec_dsp::Complex::new(0.0f64, 0.0); 3]; + for row in (0..3).rev() { + let mut acc = rhs[row]; + for k in (row + 1)..3 { + acc = acc - aug[row][k] * a[k]; + } + let d = aug[row][row]; + a[row] = if d.norm_sqr() > 1e-24 { + acc * d.conj().scale(1.0 / d.norm_sqr()) + } else { + ec_dsp::Complex::new(0.0, 0.0) + }; + } + results.push((band, a)); + } + results +} + /// A file discovered under the user's own media directories: its path, which /// AAC stream in it (0-based among AAC streams only) carries HE-AAC, and the /// core/SBR crossover an ffprobe/ASC inspection already pinned for it -- used @@ -2026,6 +2249,56 @@ fn sbr_real_library_matches_reference() { mean_in(&vs_ours, 28, 43) ); } + // (Round-46, Task 1) QMF-domain exact third witness -- see + // `hf_patch_simulator_qmf`'s doc comment. + if std::env::var("EC_AAC_SBR_QMF_WITNESS").is_ok() && n >= 4_096 { + let win = &o[oa..oa + n]; + let twin = &t[ob..ob + n]; + let (vs_ref, vs_ours) = hf_patch_simulator_qmf(win, twin); + let mean_in = |rows: &[(usize, f64)], lo: usize, hi: usize| { + let vals: Vec = rows + .iter() + .filter(|(b, _)| *b >= lo && *b < hi) + .map(|(_, c)| *c) + .collect(); + if vals.is_empty() { + 0.0 + } else { + vals.iter().sum::() / vals.len() as f64 + } + }; + println!(" ch{ch} QMF-WITNESS sim-vs-REFERENCE per-band coherence (band, coh):"); + for (b, coh) in &vs_ref { + println!(" band{b:>3}: {coh:.4}"); + } + println!( + " ch{ch} QMF-WITNESS sim-vs-REFERENCE mean: even-gap[14,28)={:.4} odd-gap[28,43)={:.4}", + mean_in(&vs_ref, 14, 28), + mean_in(&vs_ref, 28, 43) + ); + println!(" ch{ch} QMF-WITNESS sim-vs-OURS per-band coherence (band, coh):"); + for (b, coh) in &vs_ours { + println!(" band{b:>3}: {coh:.4}"); + } + println!( + " ch{ch} QMF-WITNESS sim-vs-OURS mean: even-gap[14,28)={:.4} odd-gap[28,43)={:.4}", + mean_in(&vs_ours, 14, 28), + mean_in(&vs_ours, 28, 43) + ); + // (Round-46, Task 2) fitted sim-to-reference transfer, one + // representative band per patch plus a spread across the + // strong odd-gap patch (3,28,11) where the QMF witness shows + // sim-vs-reference clearing 0.5. + let fit_bands = [14, 18, 22, 26, 28, 30, 32, 34, 36, 38, 39, 42]; + let fits = hf_patch_transfer_fit(win, twin, &fit_bands); + println!(" ch{ch} TASK2 FIT ref[l]~=a0*sim[l]+a1*sim[l-1]+a2*sim[l-2]:"); + for (b, a) in &fits { + println!( + " band{b:>3}: a0=({:>7.3},{:>7.3}) a1=({:>7.3},{:>7.3}) a2=({:>7.3},{:>7.3})", + a[0].re, a[0].im, a[1].re, a[1].im, a[2].re, a[2].im + ); + } + } println!( " ch{ch}: lag {lag}, full {full:.6}, below {:.0}Hz {low:.6}, above {:.0}Hz {high:.6}", c.crossover_hz, c.crossover_hz