diff --git a/crates/ec-aac/src/sbr_env.rs b/crates/ec-aac/src/sbr_env.rs index 85bfc292..384dac4a 100644 --- a/crates/ec-aac/src/sbr_env.rs +++ b/crates/ec-aac/src/sbr_env.rs @@ -164,7 +164,8 @@ impl NoiseGen { fn complex_unit(&mut self) -> Complex { // (Round-41, Task 2) `next()` is uniform on [-0.5, 0.5) (Var = 1/12 // per component), so a raw `(re, im)` pair has E[|z|^2] = 1/6, not - // 1 -- `adjust` scales this by `namp = sqrt(noise_here/width)` + // 1 -- `adjust` scales this by `namp = sqrt(noise_here)` (round-42: + // no `/width`, `noise_here` is already a per-sample average) // expecting E[|namp*z|^2] = noise_here, so without this correction // only ~1/6 of the transmitted noise split was ever actually // realized in the output. Measured directly (decode reeln twice, @@ -361,6 +362,17 @@ pub fn adjust( let raw_gain = (signal_target / cur).sqrt(); let lim_idx = limiter_band_index(limiter_table, lo, hi).min(n_lim - 1); let width = (hi - lo).max(1) as f64; + // (Round-42, Task 1) `lim_e_orig` conserves the cell's FULL + // transmitted `target` (signal+noise), matching the limiter cap + // and boost pass to the TOTAL energy budget rather than the + // signal share alone -- `lim_e_actual` below is widened to + // match (adds `cell_noise_here` alongside the capped signal + // energy) so the boost's `lim_e_orig/lim_e_actual` ratio + // compares total-to-total. Measured signal-target-only variant + // (both sides restricted to `signal_target`) FIRST and it made + // the realized noise fraction anchor WORSE (0.1325 -> 0.1233 + // whole-HF, moving further from the ~0.38 bookkept split) -- + // this total-conserving variant is the one that holds. lim_e_orig[lim_idx] += target * width; lim_e_curr[lim_idx] += cur * width; cell_current.push(cur); @@ -389,7 +401,13 @@ pub fn adjust( let lo = table[b] as usize; let hi = table[b + 1] as usize; let width = (hi - lo).max(1) as f64; - lim_e_actual[j] += cell_current[b] * gains[b] * gains[b] * width; + // Total realized energy this cell will carry: the capped + // signal gain applied to the raw HF estimate, PLUS the noise + // share `target` already budgeted for this cell (noise is + // injected independently of `gains`, at `noise_amps` below) -- + // matches `lim_e_orig`'s total-target accounting above. + lim_e_actual[j] += + cell_current[b] * gains[b] * gains[b] * width + cell_noise_here[b] * width; } let lim_boost: Vec = (0..n_lim) .map(|j| { @@ -405,9 +423,19 @@ pub fn adjust( for b in 0..gains.len() { let j = cell_lim[b]; gains[b] *= lim_boost[j]; - let lo = table[b] as usize; - let hi = table[b + 1] as usize; - noise_amps.push((cell_noise_here[b] / (hi - lo).max(1) as f64).sqrt() * lim_boost[j]); + // (Round-42, Task 1 corollary) `cell_noise_here[b]` is already a + // PER-SAMPLE average energy (same units as `target`/`current` -- + // see `raw_gain = sqrt(signal_target / cur)` just above, which + // treats both sides as per-sample averages without any width + // normalization). Dividing by the cell's QMF-band width here + // (as the pre-round-42 code did) additionally shrank the + // per-sample injected noise variance by `1/width`, on top of + // whatever the sample count already contributes when this same + // amplitude is applied once per (band, slot) below -- worse at + // wider high-frequency sfb cells, matching the measured + // realized-noise-fraction shortfall growing toward the top of + // the band in `EC_AAC_SBR_NOISE_ANCHOR`. + noise_amps.push(cell_noise_here[b].sqrt() * lim_boost[j]); } // (Round-22 sweep instrumentation) the next envelope's gains, only // when `EC_AAC_SBR_GAIN_LERP` is set and the band layouts match -- @@ -696,7 +724,7 @@ mod tests { /// (Round-41, Task 2) `NoiseGen::complex_unit`'s realized expected /// squared magnitude must be ~1.0 -- `adjust` scales it by - /// `namp = sqrt(noise_here/width)` expecting `E[|namp*z|^2] = + /// `namp = sqrt(noise_here)` expecting `E[|namp*z|^2] = /// noise_here`, so anything but ~1.0 here means the injected noise /// under- or over-realizes the transmitted split regardless of the /// limiter/boost interaction (measured separately, at the whole-decode diff --git a/crates/ec-aac/tests/sbr_real_library.rs b/crates/ec-aac/tests/sbr_real_library.rs index 40984c96..6c9894ff 100644 --- a/crates/ec-aac/tests/sbr_real_library.rs +++ b/crates/ec-aac/tests/sbr_real_library.rs @@ -2167,7 +2167,18 @@ fn sbr_actual_noise_fraction() { // band, not just each side's own top few). let rows_on = bin_level_conviction(on, off, rate, MIN_BAND, TOP_N); let rows_off = bin_level_conviction(off, on, rate, MIN_BAND, TOP_N); - let band_hz = f64::from(rate) / 256.0; + // (Round-42, Task 2) This `band_hz` feeds the `band` used below to look + // up `bookkept` (`noise_fraction_table()`'s QMF band index, `tables.kx` + // .. `tables.k2`, width `rate/128` -- 64 QMF bands over the SBR + // Nyquist, per the NOISE-FRACTION-DEBUG dump above). It is NOT + // `bin_level_conviction`'s own internal FFT-band scale (`rate/256`, + // used correctly elsewhere in this file for THAT function's own rows). + // Using `rate/256` here silently doubled every looked-up `band` index, + // so every real band above roughly half of `k2` missed the table and + // read back a false 0.0 bookkept fraction -- a test-side lookup bug, + // not a decoder coverage gap (verified: `noise_fraction_table()` itself + // has nonzero entries for QMF bands up to `k2`). + let band_hz = f64::from(rate) / 128.0; println!( " ACTUAL noise fraction (band_hz, energy_on, energy_off, actual_fraction, bookkept_fraction):" ); @@ -2212,4 +2223,29 @@ fn sbr_actual_noise_fraction() { " full-band corr: noise-on {corr_on:.6} noise-off {corr_off:.6} delta {:.6}", corr_off - corr_on ); + + // (Round-42, Task 1/3 acceptance) Two real accounting bugs fixed this + // round pushed the realized whole-HF fraction 0.1325 -> 0.1787 (reeln, + // this same measurement): the boost pass was conserving the cell's FULL + // envelope target against a signal-only realized sum (now conserves + // total-vs-total, matching the reference algorithm's known + // E_orig/E_curr+Q_M boost ratio), and `noise_amps` divided an + // already-per-sample `noise_here` by the cell's QMF-band width a SECOND + // time (removed) -- worse at wide high-frequency sfb cells, which is + // 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. + assert!( + actual_total > 0.15, + "whole-HF realized noise fraction regressed to {actual_total:.4} \ + (round-42 fix floor is 0.15; pre-fix was ~0.13, post-fix ~0.18)" + ); }