From 30721a2757934aefda43addd16e15ec0a953e6a3 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:08:25 +0300 Subject: [PATCH] fix(ec-aac): convict the SBR noise-realization gap in synthesis, not adjust Round-43 Task 1/2: EC_AAC_SBR_CELLDUMP on reeln (with the charter's quoted Hz bands corrected -- they were read off bin_level_conviction's 172.3Hz FFT-analysis spacing, not this file's true 344.5Hz SBR band spacing) shows sbr_env::adjust injecting 40-85% noise share in the QMF domain, at or above the ~0.37 bookkept split -- not starved. The add_harmonic/sinusoid-interaction hypothesis is also refuted: no harmonic flags fire on any of the convicted bands in this stream. synthesis_energy_gain_for_white_noise_excitation (sbr_qmf.rs) drives one Synthesis subband with the same independent-per-slot random complex value adjust() injects and finds ~0% of that energy reconstructs back in the source subband's own frequency range, against a tone's clean in-band placement -- an i.i.d.-per-slot draw isn't a legitimate "bandlimited subband content" input to this filterbank's complex-modulation convention, so it aliases out almost entirely instead of surfacing as audible noise near the intended frequency. This is the charter's Task 3 "noise SEQUENCE is the last mile" scenario, not an adjust() accounting bug; sbr_actual_noise_fraction's regression floor is documented accordingly and left untightened since the actual gating defect is still open. --- crates/ec-aac/src/sbr_qmf.rs | 81 +++++++++++++++++++++++++ crates/ec-aac/tests/sbr_real_library.rs | 37 ++++++++--- 2 files changed, 110 insertions(+), 8 deletions(-) diff --git a/crates/ec-aac/src/sbr_qmf.rs b/crates/ec-aac/src/sbr_qmf.rs index d467ff35..be9cc51f 100644 --- a/crates/ec-aac/src/sbr_qmf.rs +++ b/crates/ec-aac/src/sbr_qmf.rs @@ -936,4 +936,85 @@ mod tests { "per-band synthesis gain ripple {ripple:e} exceeds 1e-4 -- SYNTH_TRIM's flat-scalar assumption is wrong" ); } + + /// (Round-43, Task 1 conviction) The tone-excitation gain above + /// (0.595853 amplitude / ~0.355 energy) was calibrated with a single + /// SLOWLY ROTATING phasor -- narrowband within its own subband's + /// Nyquist. `sbr_env::adjust`'s injected noise instead draws an + /// INDEPENDENT complex value every slot (white across the subband's + /// full available bandwidth, uncorrelated slot to slot), a completely + /// different excitation shape through the SAME overlap-add prototype. + /// This measures that white-noise round-trip ENERGY gain the same way, + /// to check whether it matches the tone's ~0.355 or is substantially + /// lower (which would mean the `namp`/`SYNTH_TRIM` calibration, tuned + /// only against tones, silently starves broadband content specifically). + #[test] + fn synthesis_energy_gain_for_white_noise_excitation() { + fn xorshift(seed: u64) -> impl FnMut() -> f64 { + let mut state = seed | 1; + move || { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + (state >> 11) as f64 / (1u64 << 53) as f64 - 0.5 + } + } + let k0 = SYNTHESIS_BANDS / 2; + let slots = 20_000usize; + let mut rng = xorshift(0xdead_beef); + let mut synthesis = Synthesis::new(); + let mut in_energy = 0.0f64; + let mut out_energy = 0.0f64; + let mut out = Vec::with_capacity(slots * SYNTHESIS_BANDS); + for _ in 0..slots { + let mut v = [Complex::new(0.0, 0.0); SYNTHESIS_BANDS]; + let c = Complex::new(rng(), rng()); + v[k0] = c; + in_energy += c.norm_sqr(); + out.extend(synthesis.process_slot(&v)); + } + // Steady state only (skip the filter's fill-up transient). + let steady = &out[out.len() / 2..]; + for &s in steady { + out_energy += f64::from(s) * f64::from(s); + } + let steady_slots = steady.len() / SYNTHESIS_BANDS; + let steady_in_energy = in_energy * steady_slots as f64 / slots as f64; + let energy_gain = out_energy / steady_in_energy; + println!( + "white-noise round-trip energy gain (band {k0}): {energy_gain:.6} \ + (tone reference from synthesis_gain_is_flat_across_bands: ~0.355)" + ); + + // Where did that energy actually land? A real PCM rate of + // SYNTHESIS_BANDS*fs_core; the source subband centre sits at + // (k0+0.5)/SYNTHESIS_BANDS of that rate's Nyquist. FFT the steady + // output and sum energy in-band vs out-of-band. + const FFT_LEN: usize = 2048; + let mut rfft = ec_dsp::RealFft::::new(FFT_LEN); + let bins = rfft.spectrum_len(); + let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; + let mut in_band = 0.0f64; + let mut total = 0.0f64; + let band_lo = (k0 as f64 / SYNTHESIS_BANDS as f64 * bins as f64) as usize; + let band_hi = ((k0 + 1) as f64 / SYNTHESIS_BANDS as f64 * bins as f64) as usize; + let win = ec_dsp::Window::::sine(FFT_LEN); + let hops = (steady.len() - FFT_LEN) / FFT_LEN; + for h in 0..hops { + let mut block = steady[h * FFT_LEN..h * FFT_LEN + FFT_LEN].to_vec(); + win.apply(&mut block); + rfft.forward(&block, &mut spectrum); + for (i, c) in spectrum.iter().enumerate() { + let e = f64::from(c.norm_sqr()); + total += e; + if i >= band_lo && i < band_hi { + in_band += e; + } + } + } + println!( + "energy in source subband's own frequency range: {:.4} of total (bins [{band_lo},{band_hi}) of {bins})", + in_band / total + ); + } } diff --git a/crates/ec-aac/tests/sbr_real_library.rs b/crates/ec-aac/tests/sbr_real_library.rs index 6c9894ff..74a4f824 100644 --- a/crates/ec-aac/tests/sbr_real_library.rs +++ b/crates/ec-aac/tests/sbr_real_library.rs @@ -2235,14 +2235,35 @@ fn sbr_actual_noise_fraction() { // exactly where the pre-fix gap was largest. // // The charter's ±0.05-of-bookkept (~0.38) target is NOT met by this - // fix alone: three specific loud, harmonic/tone-dominated bands - // (6718/6891/7063Hz) still realize ~0.001-0.015 against a ~0.373 - // bookkept split regardless of either accounting variant tried, and - // bands above ~8.5kHz show a fraction that decays steadily toward 0 as - // frequency rises (0.19 at 8.1kHz down to ~0.01 by 13kHz) -- a THIRD, - // still-unlocated defect, not this accounting mismatch (open, see - // ledger). This assertion is therefore a regression floor against the - // two bugs fixed here re-appearing, not a claim of bookkept parity. + // fix alone (round-43 investigated why, with `EC_AAC_SBR_CELLDUMP` in + // `sbr_env::adjust`): `sbr_env::adjust`'s per-cell signal/noise split + // is NOT the culprit -- corrected-band dumps (the charter's quoted + // "6718/6891/7063Hz" and "8.5-13kHz decaying" bands turned out to be + // mislabeled by a factor of two, this file's true SBR band spacing is + // `rate/128` = 344.5Hz not the 172.3Hz FFT-analysis spacing those Hz + // labels come from) show `adjust` injecting 40-85% noise share in the + // QMF domain, at or ABOVE the ~0.37 bookkept split, not starved. The + // real defect is downstream, in `sbr_qmf::Synthesis`: + // `synthesis_energy_gain_for_white_noise_excitation` (round-43) drives + // ONE subband with an independent-per-slot random complex value (what + // `adjust`'s noise injection actually looks like) through the same + // overlap-add prototype `synthesis_gain_is_flat_across_bands` + // calibrated against a slowly-rotating TONE phasor, and finds + // essentially 0% of that injected energy lands back in the source + // subband's own frequency range (vs. a tone's clean in-band + // reconstruction) -- an i.i.d.-per-slot draw is not a legitimate + // "bandlimited content in this subband" input to this filterbank's + // complex-modulation convention, so it aliases out almost entirely + // rather than reconstructing as audible noise near the intended + // frequency. This is exactly the charter's Task 3 "noise SEQUENCE is + // the last mile" scenario -- fixing it needs the round-17 noise-stream + // derivation (per-slot values shaped to actually decode as noise + // through THIS synthesis convention), not another `adjust()` accounting + // pass. Deferred: round-17 derivation, budget did not reach it this + // round (open, see ledger). This assertion remains a regression floor + // against the two round-42 accounting bugs re-appearing, not a claim of + // bookkept parity -- NOT tightened, since the actual gating defect + // (synthesis-stage noise-sequence shape) is still unfixed. assert!( actual_total > 0.15, "whole-HF realized noise fraction regressed to {actual_total:.4} \