From 5537faef9372cf2a408d76c68ac705dc7162b654 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 01:02:30 +0300 Subject: [PATCH] fix(aac-sbr): noise-floor dequant is a signal-relative ratio, not an absolute energy Round-21 Task 1: a reference-decoder probe (scripts/aac-tables/sbrnoise.py) sweeping the noise-floor raw value q_q at a fixed envelope target shows total HF-band output power staying nearly flat across the whole q_q range, not scaling additively with dequant_noise(q_q) as our own decoder assumed. That's the signature of the envelope target being split signal=target/(1+Q_div), noise=target*Q_div/(1+Q_div) with Q_div=dequant_noise(q_q), not noise added independently on top of the full target. adjust() now computes that split per (envelope, band) cell and injects noise there directly, replacing the old separate noise-grid pass that treated dequant_noise's return value as a standalone absolute energy comparable to dequant_env's unrelated unit scale. On the real-file bar (reeln) this moves correlation only slightly (ch0 0.9726->0.9732, ch1 0.9705->0.9716): real transmitted q_q values apparently keep Q_div small regardless of interpretation, so the noise-floor circularity hypothesis still doesn't explain this file's HF correlation gap -- but the fix itself is now unit-consistent with the measured reference behavior and is kept as a correctness fix. --- crates/ec-aac/src/sbr_env.rs | 108 +++++++++++++++++--------------- scripts/aac-tables/sbrnoise.py | 111 +++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+), 49 deletions(-) create mode 100644 scripts/aac-tables/sbrnoise.py diff --git a/crates/ec-aac/src/sbr_env.rs b/crates/ec-aac/src/sbr_env.rs index 2c73ba24..4f329bdd 100644 --- a/crates/ec-aac/src/sbr_env.rs +++ b/crates/ec-aac/src/sbr_env.rs @@ -29,10 +29,14 @@ //! # Gain, limiter, noise, sinusoids //! //! For every `(envelope, band)` cell this measures the HF estimate's own -//! average energy over that time/frequency window and scales it toward -//! the transmitted target; the limiter then caps how far above the -//! band's own transmitted noise floor that gain is allowed to reach -//! (`bs_limiter_gains`), noise is mixed in at the transmitted floor, and +//! average energy over that time/frequency window; the cell's envelope +//! target is split into a signal share and a noise share via the cell's +//! `Q_div = dequant_noise(q_q)` ratio (`signal = target/(1+Q_div)`, +//! `noise = target*Q_div/(1+Q_div)`, round-21 -- `Q_div` is a ratio +//! relative to the envelope target, not a standalone absolute energy), the +//! HF estimate is scaled toward the signal share, the limiter caps how far +//! above the cell's own noise floor that gain is allowed to reach +//! (`bs_limiter_gains`), the noise share is mixed in at that level, and //! flagged bands get an added tone. #![allow(clippy::needless_range_loop)] @@ -55,7 +59,13 @@ pub fn dequant_env(e_q: i32, amp_res: u8) -> f64 { 2f64.powf(alpha(amp_res) * f64::from(e_q)) } -/// Plain noise-floor dequantization. +/// Plain noise-floor dequantization: `Q_div = 2^(NOISE_FLOOR_OFFSET-q_q)`, a +/// ratio relative to the ENVELOPE energy of the same (time, band) cell, not +/// an absolute energy on its own (round-21: `adjust` splits each cell's +/// envelope target into `signal = target/(1+Q_div)` and +/// `noise = target*Q_div/(1+Q_div)`; using this return value as a +/// standalone absolute energy, as pre-round-21 code did, compares it +/// against `dequant_env`'s unrelated unit scale). pub fn dequant_noise(q_q: i32) -> f64 { 2f64.powf(NOISE_FLOOR_OFFSET - f64::from(q_q)) } @@ -236,6 +246,11 @@ pub fn adjust( // frequencies) so both application modes share the exact same gain // values. let mut gains = Vec::with_capacity(table.len().saturating_sub(1)); + // (Round-21) Actual per-cell noise energy this envelope/band injects, + // parallel to `gains` -- filled below and consumed by the + // application loop that follows instead of the old separate + // noise-grid pass (see round-21 module doc note). + let mut noise_amps = Vec::with_capacity(table.len().saturating_sub(1)); for b in 0..table.len().saturating_sub(1) { let lo = table[b] as usize; let hi = table[b + 1] as usize; @@ -252,12 +267,27 @@ pub fn adjust( } } let current = if count > 0 { sum / count as f64 } else { 0.0 }; - let mut gain = (target / current.max(1e-12)).sqrt(); let q = noise_band_for(tables, lo); - let noise_here = noise_row + // `dequant_noise` returns `Q_div = 2^(NOISE_FLOOR_OFFSET-q_q)`, a + // ratio relative to this cell's OWN envelope target -- not an + // absolute energy in the same units as `target` (round-21 Task + // 1/2: a reference-decoder q_q sweep at a FIXED envelope target + // showed total output power staying nearly flat as q_q swept its + // whole range, the signature of `target` being split + // `signal=target/(1+Q_div)`, `noise=target*Q_div/(1+Q_div)` + // rather than noise being added independently on top of the full + // target -- the previous code used `dequant_noise` as an + // absolute energy standalone, which for real content's near-1e-7 + // apparent "fraction" (round-17) was actually comparing two + // energies in unrelated unit scales, not a genuinely near-zero + // noise contribution). + let noise_div = noise_row .and_then(|r| r.get(q).copied()) .unwrap_or(0.0) - .max(1e-12); + .max(0.0); + let signal_target = target / (1.0 + noise_div); + let noise_here = target - signal_target; // = target*noise_div/(1+noise_div) + let mut gain = (signal_target / current.max(1e-12)).sqrt(); // Cap gain at `limiter_max` times the cell's own // noise-plus-signal headroom over its raw HF estimate (Sec // 4.6.18.7.5's `limGain * sqrt((Q_M+E_curr)/E_curr)` shape): a @@ -273,13 +303,14 @@ pub fn adjust( gain = gain.min(limiter_max * ((noise_here + cur) / cur).sqrt()); gain = gain.min(limiter_max * 64.0); // absolute ceiling: never amplify a silent cell to infinity gains.push(gain); + noise_amps.push((noise_here / (hi - lo).max(1) as f64).sqrt()); if track_fraction { let slots = (t1.max(0) - t0.max(0)).max(0) as usize; for band in lo..hi { if band < kx { continue; } - accumulate_noise_fraction(band, slots, target, 0.0); + accumulate_noise_fraction(band, slots, signal_target, noise_here); } } } @@ -300,6 +331,7 @@ pub fn adjust( let lo = table[b] as usize; let hi = table[b + 1] as usize; let gain = gains[b]; + let namp = noise_amps[b]; for band in lo..hi { if band < kx || band - kx >= hf.len() { continue; @@ -307,6 +339,11 @@ pub fn adjust( let row = &mut hf[band - kx]; for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { row[slot] = row[slot].scale(gain); + // Consume the RNG unconditionally (matches the pre-round-21 + // stream shape) even under EC_AAC_SBR_NOISE_ZERO, which + // only skips the PCM addition, not the bookkeeping. + let n = rng.complex_unit().scale(namp); + row[slot] = row[slot] + if zero_noise { Complex::ZERO } else { n }; } } } @@ -347,45 +384,14 @@ pub fn adjust( } } - // Noise floor last: every target bin gets the transmitted floor for its - // noise-time-segment and noise-frequency-band, independent of the - // envelope grid (they are different, coarser grids over the same axes). - // This must run after the envelope-gain loop above -- gain is computed - // from the HF-signal-only estimate and must not also rescale the noise - // that lands on top of it (injecting noise first made the gain step - // measure signal+noise together and rescale both, which distorts the - // noise/signal split by however much noise dominates a given cell -- a - // content- and channel-dependent error, not a uniform scale bug). - for (ni, (t0, t1)) in ch.t_noise.windows(2).map(|w| (w[0], w[1])).enumerate() { - if ni >= noise_energy.len() { - break; - } - for q in 0..tables.n_q { - let lo = tables.f_noise[q] as usize; - let hi = tables.f_noise[q + 1] as usize; - let energy = noise_energy[ni].get(q).copied().unwrap_or(0.0); - let amp = (energy / (hi - lo).max(1) as f64).sqrt(); - if track_fraction { - let slots = (t1.max(0) - t0.max(0)).max(0) as usize; - for band in lo..hi { - if band < kx { - continue; - } - accumulate_noise_fraction(band, slots, 0.0, amp * amp); - } - } - for band in lo..hi { - if band < kx || band - kx >= hf.len() { - continue; - } - let row = &mut hf[band - kx]; - for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { - let n = rng.complex_unit().scale(amp); - row[slot] = row[slot] + if zero_noise { Complex::ZERO } else { n }; - } - } - } - } + // (Round-21) Noise is now injected inside the envelope-gain loop above, + // per (envelope, band) cell using each cell's own `signal_target`/ + // `noise_here` split -- the noise grid is coarser than the envelope + // grid but every envelope cell resolves its own noise band via + // `noise_band_for`/`noise_row`, so this covers the same ground the old + // separate noise-grid pass did, consistently with the gain that was + // computed for the same cell (previously the two were split across + // unrelated unit scales; see the comment at `noise_div` above). } fn noise_band_for(tables: &BandTables, low_band: usize) -> usize { @@ -499,7 +505,11 @@ mod tests { add_harmonic: None, }; let env_energy = vec![vec![1.0e6f64; tables.n_low]]; - let noise_energy = vec![vec![1e-3f64; tables.n_q]]; + // (Round-21) noise_energy is now a Q_div ratio against the cell's + // own envelope target, not a standalone absolute energy -- zero + // keeps this test focused on the signal-gain limiter it names, not + // noise injection. + let noise_energy = vec![vec![0.0f64; tables.n_q]]; let mut rng = NoiseGen::new(1); adjust( &mut hf, diff --git a/scripts/aac-tables/sbrnoise.py b/scripts/aac-tables/sbrnoise.py new file mode 100644 index 00000000..b241d082 --- /dev/null +++ b/scripts/aac-tables/sbrnoise.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python3 +"""Black-box measurement of the reference decoder's SBR noise-floor +dequantization law (round-21 Task 1). + +Method: reuse `sbrchirp.py`'s repaired probe shape (SCE, FIXFIX num_env=2, +amp_res=1, broadband comb core so the HF-signal envelope target is fixed +and non-zero), fix the envelope raw value, and sweep only the noise-floor +raw value `q_q` (`noise0`) across its 5-bit range. Noise is spec-additive +on top of the gain-adjusted HF signal (module doc in sbr_env.rs), so the +total power measured in the target band at each `q_q` is +`signal_energy(fixed) + noise_energy(q_q)`; subtracting the high-`q_q` +(minimal-noise) baseline isolates `noise_energy(q_q)` and its shape can be +fit against `q_q` directly, independent of our own decoder. +""" +import os +import sys + +import numpy as np + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) + +import sbrchirp as C +import sbrpayload_fixtures as F + +RATE = C.RATE + + +def noise_stream(cfg, tables, n_frames, q, env=40): + """Like `sbrchirp.stream()` but `bs_invf_mode` held at NONE (0, so the + injected noise is not reshaped by the inverse filter) and `noise0` + swept to `[q, q]` instead of `bs_invf_mode`; `env` is the fixed + envelope raw value.""" + grid = dict(frame_class=0, num_env=2, freq_res=0) + frames = [C.P.silent(C.SF_INDEX)] + for _ in range(n_frames): + c = dict(cfg, invf=0, header=1, amp_res=1, element="sce", coupling=0, + df_env=[0] * 8, df_noise=[0, 0], env0=[env] * 8, noise0=[q, q]) + w = C.P.BitW() + C.broadband_core_bits(w) + body = F.sbr_bits_full(c, tables, grid, []) + w.w(6, 3) + cnt = (len(body.bits) + 4 + 7) // 8 + if cnt >= 15: + w.w(15, 4) + w.w(cnt - 15 + 1, 8) + else: + w.w(cnt, 4) + start = len(w) + w.w(13, 4) + w.wbits(body.bits) + while len(w) - start < cnt * 8: + w.w(0, 1) + w.w(7, 3) + frames.append(C.P.adts(w.pack(pad=0), C.SF_INDEX)) + frames.append(C.P.silent(C.SF_INDEX)) + return frames + + +def band_power(pcm, lo_hz, hi_hz, skip=6, n_fft=8192): + start = skip * 1024 + seg = pcm[start : start + n_fft] + if len(seg) < n_fft: + seg = np.pad(seg, (0, n_fft - len(seg))) + spec = np.abs(np.fft.rfft(seg * np.hanning(len(seg)))) ** 2 + freqs = np.fft.rfftfreq(n_fft, 1 / RATE) + mask = (freqs >= lo_hz) & (freqs < hi_hz) + return float(np.sum(spec[mask])) + + +if __name__ == "__main__": + cfg0, tables0 = F.one_band_header() + lo, hi = 19 * RATE / 128, 23 * RATE / 128 + print(f"target region {lo:.1f}-{hi:.1f} Hz, RAW_NOISE bits={C.T.RAW_NOISE} (q range 0..{2**C.T.RAW_NOISE - 1})") + + print("\n== value-engagement check: envelope raw sweep (fixed q_q) must move power by orders ==") + for e in (10, 40, 60): + cfg = dict(cfg0) + frames = noise_stream(cfg, tables0, 20, 30, env=e) + pcm, err = C.decode(frames, f"env_check{e}") + pwr = band_power(pcm, lo, hi) + print(f" env0={e} (q_q=30, minimal noise) pwr={pwr:.4e} err={err.strip()[:60]!r}") + + print("\n== Task 1: noise-floor raw (q_q) sweep, fixed envelope ==") + qs = list(range(0, 31, 3)) + pwrs = {} + for q in qs: + cfg = dict(cfg0) + frames = noise_stream(cfg, tables0, 20, q) + pcm, err = C.decode(frames, f"q{q}") + pwr = band_power(pcm, lo, hi) + pwrs[q] = pwr + print(f" q_q={q:2d} pwr={pwr:.6e} err={err.strip()[:80]!r}") + + baseline = pwrs[qs[-1]] + print(f"\nbaseline (q_q={qs[-1]}, minimal noise) pwr={baseline:.6e}") + print("q_q, noise_energy=pwr-baseline, log2(noise_energy)") + fit_x, fit_y = [], [] + for q in qs[:-1]: + n = pwrs[q] - baseline + if n > 0: + l2 = np.log2(n) + print(f" {q:2d} {n:.6e} {l2:.4f}") + fit_x.append(q) + fit_y.append(l2) + else: + print(f" {q:2d} {n:.6e} (non-positive, skipped)") + if len(fit_x) >= 2: + slope, intercept = np.polyfit(fit_x, fit_y, 1) + print(f"\nfit: log2(noise_energy) = {slope:.4f} * q_q + {intercept:.4f}") + print(f"(spec/ours form: log2(noise_energy) = -1*q_q + 6 => slope~-1, intercept~6 expected if law matches)")