Boost accounting conserved signal-only against the full envelope target, and noise_amps divided per-sample energy by width a second time
Browse files at this commitf737a340eefbfdf234981eb0b03967c6120bd96cParent: 64cc642- fix
- boost accounting conserved signal-only against the full envelope target, and noise_amps divided per-sample energy by width a second time
2 changed files +71 -7
crates/ec-aac/src/sbr_env.rsmodified+34 -6
| @@ -164,7 +164,8 @@ impl NoiseGen { | |||
| 164 | fn complex_unit(&mut self) -> Complex<f64> { | 164 | fn complex_unit(&mut self) -> Complex<f64> { |
| 165 | // (Round-41, Task 2) `next()` is uniform on [-0.5, 0.5) (Var = 1/12 | 165 | // (Round-41, Task 2) `next()` is uniform on [-0.5, 0.5) (Var = 1/12 |
| 166 | // per component), so a raw `(re, im)` pair has E[|z|^2] = 1/6, not | 166 | // per component), so a raw `(re, im)` pair has E[|z|^2] = 1/6, not |
| 167 | - // 1 -- `adjust` scales this by `namp = sqrt(noise_here/width)` | 167 | + // 1 -- `adjust` scales this by `namp = sqrt(noise_here)` (round-42: |
| 168 | + // no `/width`, `noise_here` is already a per-sample average) | ||
| 168 | // expecting E[|namp*z|^2] = noise_here, so without this correction | 169 | // expecting E[|namp*z|^2] = noise_here, so without this correction |
| 169 | // only ~1/6 of the transmitted noise split was ever actually | 170 | // only ~1/6 of the transmitted noise split was ever actually |
| 170 | // realized in the output. Measured directly (decode reeln twice, | 171 | // realized in the output. Measured directly (decode reeln twice, |
| @@ -361,6 +362,17 @@ pub fn adjust( | |||
| 361 | let raw_gain = (signal_target / cur).sqrt(); | 362 | let raw_gain = (signal_target / cur).sqrt(); |
| 362 | let lim_idx = limiter_band_index(limiter_table, lo, hi).min(n_lim - 1); | 363 | let lim_idx = limiter_band_index(limiter_table, lo, hi).min(n_lim - 1); |
| 363 | let width = (hi - lo).max(1) as f64; | 364 | let width = (hi - lo).max(1) as f64; |
| 365 | + // (Round-42, Task 1) `lim_e_orig` conserves the cell's FULL | ||
| 366 | + // transmitted `target` (signal+noise), matching the limiter cap | ||
| 367 | + // and boost pass to the TOTAL energy budget rather than the | ||
| 368 | + // signal share alone -- `lim_e_actual` below is widened to | ||
| 369 | + // match (adds `cell_noise_here` alongside the capped signal | ||
| 370 | + // energy) so the boost's `lim_e_orig/lim_e_actual` ratio | ||
| 371 | + // compares total-to-total. Measured signal-target-only variant | ||
| 372 | + // (both sides restricted to `signal_target`) FIRST and it made | ||
| 373 | + // the realized noise fraction anchor WORSE (0.1325 -> 0.1233 | ||
| 374 | + // whole-HF, moving further from the ~0.38 bookkept split) -- | ||
| 375 | + // this total-conserving variant is the one that holds. | ||
| 364 | lim_e_orig[lim_idx] += target * width; | 376 | lim_e_orig[lim_idx] += target * width; |
| 365 | lim_e_curr[lim_idx] += cur * width; | 377 | lim_e_curr[lim_idx] += cur * width; |
| 366 | cell_current.push(cur); | 378 | cell_current.push(cur); |
| @@ -389,7 +401,13 @@ pub fn adjust( | |||
| 389 | let lo = table[b] as usize; | 401 | let lo = table[b] as usize; |
| 390 | let hi = table[b + 1] as usize; | 402 | let hi = table[b + 1] as usize; |
| 391 | let width = (hi - lo).max(1) as f64; | 403 | let width = (hi - lo).max(1) as f64; |
| 392 | - lim_e_actual[j] += cell_current[b] * gains[b] * gains[b] * width; | 404 | + // Total realized energy this cell will carry: the capped |
| 405 | + // signal gain applied to the raw HF estimate, PLUS the noise | ||
| 406 | + // share `target` already budgeted for this cell (noise is | ||
| 407 | + // injected independently of `gains`, at `noise_amps` below) -- | ||
| 408 | + // matches `lim_e_orig`'s total-target accounting above. | ||
| 409 | + lim_e_actual[j] += | ||
| 410 | + cell_current[b] * gains[b] * gains[b] * width + cell_noise_here[b] * width; | ||
| 393 | } | 411 | } |
| 394 | let lim_boost: Vec<f64> = (0..n_lim) | 412 | let lim_boost: Vec<f64> = (0..n_lim) |
| 395 | .map(|j| { | 413 | .map(|j| { |
| @@ -405,9 +423,19 @@ pub fn adjust( | |||
| 405 | for b in 0..gains.len() { | 423 | for b in 0..gains.len() { |
| 406 | let j = cell_lim[b]; | 424 | let j = cell_lim[b]; |
| 407 | gains[b] *= lim_boost[j]; | 425 | gains[b] *= lim_boost[j]; |
| 408 | - let lo = table[b] as usize; | 426 | + // (Round-42, Task 1 corollary) `cell_noise_here[b]` is already a |
| 409 | - let hi = table[b + 1] as usize; | 427 | + // PER-SAMPLE average energy (same units as `target`/`current` -- |
| 410 | - noise_amps.push((cell_noise_here[b] / (hi - lo).max(1) as f64).sqrt() * lim_boost[j]); | 428 | + // see `raw_gain = sqrt(signal_target / cur)` just above, which |
| 429 | + // treats both sides as per-sample averages without any width | ||
| 430 | + // normalization). Dividing by the cell's QMF-band width here | ||
| 431 | + // (as the pre-round-42 code did) additionally shrank the | ||
| 432 | + // per-sample injected noise variance by `1/width`, on top of | ||
| 433 | + // whatever the sample count already contributes when this same | ||
| 434 | + // amplitude is applied once per (band, slot) below -- worse at | ||
| 435 | + // wider high-frequency sfb cells, matching the measured | ||
| 436 | + // realized-noise-fraction shortfall growing toward the top of | ||
| 437 | + // the band in `EC_AAC_SBR_NOISE_ANCHOR`. | ||
| 438 | + noise_amps.push(cell_noise_here[b].sqrt() * lim_boost[j]); | ||
| 411 | } | 439 | } |
| 412 | // (Round-22 sweep instrumentation) the next envelope's gains, only | 440 | // (Round-22 sweep instrumentation) the next envelope's gains, only |
| 413 | // when `EC_AAC_SBR_GAIN_LERP` is set and the band layouts match -- | 441 | // when `EC_AAC_SBR_GAIN_LERP` is set and the band layouts match -- |
| @@ -696,7 +724,7 @@ mod tests { | |||
| 696 | 724 | ||
| 697 | /// (Round-41, Task 2) `NoiseGen::complex_unit`'s realized expected | 725 | /// (Round-41, Task 2) `NoiseGen::complex_unit`'s realized expected |
| 698 | /// squared magnitude must be ~1.0 -- `adjust` scales it by | 726 | /// squared magnitude must be ~1.0 -- `adjust` scales it by |
| 699 | - /// `namp = sqrt(noise_here/width)` expecting `E[|namp*z|^2] = | 727 | + /// `namp = sqrt(noise_here)` expecting `E[|namp*z|^2] = |
| 700 | /// noise_here`, so anything but ~1.0 here means the injected noise | 728 | /// noise_here`, so anything but ~1.0 here means the injected noise |
| 701 | /// under- or over-realizes the transmitted split regardless of the | 729 | /// under- or over-realizes the transmitted split regardless of the |
| 702 | /// limiter/boost interaction (measured separately, at the whole-decode | 730 | /// limiter/boost interaction (measured separately, at the whole-decode |
crates/ec-aac/tests/sbr_real_library.rsmodified+37 -1
| @@ -2167,7 +2167,18 @@ fn sbr_actual_noise_fraction() { | |||
| 2167 | // band, not just each side's own top few). | 2167 | // band, not just each side's own top few). |
| 2168 | let rows_on = bin_level_conviction(on, off, rate, MIN_BAND, TOP_N); | 2168 | let rows_on = bin_level_conviction(on, off, rate, MIN_BAND, TOP_N); |
| 2169 | let rows_off = bin_level_conviction(off, on, rate, MIN_BAND, TOP_N); | 2169 | let rows_off = bin_level_conviction(off, on, rate, MIN_BAND, TOP_N); |
| 2170 | - let band_hz = f64::from(rate) / 256.0; | 2170 | + // (Round-42, Task 2) This `band_hz` feeds the `band` used below to look |
| 2171 | + // up `bookkept` (`noise_fraction_table()`'s QMF band index, `tables.kx` | ||
| 2172 | + // .. `tables.k2`, width `rate/128` -- 64 QMF bands over the SBR | ||
| 2173 | + // Nyquist, per the NOISE-FRACTION-DEBUG dump above). It is NOT | ||
| 2174 | + // `bin_level_conviction`'s own internal FFT-band scale (`rate/256`, | ||
| 2175 | + // used correctly elsewhere in this file for THAT function's own rows). | ||
| 2176 | + // Using `rate/256` here silently doubled every looked-up `band` index, | ||
| 2177 | + // so every real band above roughly half of `k2` missed the table and | ||
| 2178 | + // read back a false 0.0 bookkept fraction -- a test-side lookup bug, | ||
| 2179 | + // not a decoder coverage gap (verified: `noise_fraction_table()` itself | ||
| 2180 | + // has nonzero entries for QMF bands up to `k2`). | ||
| 2181 | + let band_hz = f64::from(rate) / 128.0; | ||
| 2171 | println!( | 2182 | println!( |
| 2172 | " ACTUAL noise fraction (band_hz, energy_on, energy_off, actual_fraction, bookkept_fraction):" | 2183 | " ACTUAL noise fraction (band_hz, energy_on, energy_off, actual_fraction, bookkept_fraction):" |
| 2173 | ); | 2184 | ); |
| @@ -2212,4 +2223,29 @@ fn sbr_actual_noise_fraction() { | |||
| 2212 | " full-band corr: noise-on {corr_on:.6} noise-off {corr_off:.6} delta {:.6}", | 2223 | " full-band corr: noise-on {corr_on:.6} noise-off {corr_off:.6} delta {:.6}", |
| 2213 | corr_off - corr_on | 2224 | corr_off - corr_on |
| 2214 | ); | 2225 | ); |
| 2226 | + | ||
| 2227 | + // (Round-42, Task 1/3 acceptance) Two real accounting bugs fixed this | ||
| 2228 | + // round pushed the realized whole-HF fraction 0.1325 -> 0.1787 (reeln, | ||
| 2229 | + // this same measurement): the boost pass was conserving the cell's FULL | ||
| 2230 | + // envelope target against a signal-only realized sum (now conserves | ||
| 2231 | + // total-vs-total, matching the reference algorithm's known | ||
| 2232 | + // E_orig/E_curr+Q_M boost ratio), and `noise_amps` divided an | ||
| 2233 | + // already-per-sample `noise_here` by the cell's QMF-band width a SECOND | ||
| 2234 | + // time (removed) -- worse at wide high-frequency sfb cells, which is | ||
| 2235 | + // exactly where the pre-fix gap was largest. | ||
| 2236 | + // | ||
| 2237 | + // The charter's ±0.05-of-bookkept (~0.38) target is NOT met by this | ||
| 2238 | + // fix alone: three specific loud, harmonic/tone-dominated bands | ||
| 2239 | + // (6718/6891/7063Hz) still realize ~0.001-0.015 against a ~0.373 | ||
| 2240 | + // bookkept split regardless of either accounting variant tried, and | ||
| 2241 | + // bands above ~8.5kHz show a fraction that decays steadily toward 0 as | ||
| 2242 | + // frequency rises (0.19 at 8.1kHz down to ~0.01 by 13kHz) -- a THIRD, | ||
| 2243 | + // still-unlocated defect, not this accounting mismatch (open, see | ||
| 2244 | + // ledger). This assertion is therefore a regression floor against the | ||
| 2245 | + // two bugs fixed here re-appearing, not a claim of bookkept parity. | ||
| 2246 | + assert!( | ||
| 2247 | + actual_total > 0.15, | ||
| 2248 | + "whole-HF realized noise fraction regressed to {actual_total:.4} \ | ||
| 2249 | + (round-42 fix floor is 0.15; pre-fix was ~0.13, post-fix ~0.18)" | ||
| 2250 | + ); | ||
| 2215 | } | 2251 | } |