diag(ec-aac): SBR noise-fraction ceiling hypothesis, measured and refuted
Browse files at this commit2cccf178a8bb4ba04fa845f4a9ed002c6a135fe3Parent: 80e5af33 changed files +98 -1
- crates/ec-aac/src/lib.rs +4 -0
- crates/ec-aac/src/sbr_env.rs +69 -1
- crates/ec-aac/tests/sbr_real_library.rs +25 -0
crates/ec-aac/src/lib.rsmodified+4 -0
| @@ -48,6 +48,10 @@ pub use config::{ | |||
| 48 | pub use decode::{FRAME_LEN, WindowSequence}; | 48 | pub use decode::{FRAME_LEN, WindowSequence}; |
| 49 | pub use ec_core::{Error, Result}; | 49 | pub use ec_core::{Error, Result}; |
| 50 | pub use encode::{AacEncoder, AacEncoderConfig, EncodedPacket, WindowShape}; | 50 | pub use encode::{AacEncoder, AacEncoderConfig, EncodedPacket, WindowShape}; |
| 51 | +/// Diagnostic only (round-17 Task 1 conviction check): reads back the | ||
| 52 | +/// per-absolute-QMF-band transmitted signal/noise energy sums | ||
| 53 | +/// `EC_AAC_SBR_NOISE_FRACTION_DEBUG` accumulates during decode. | ||
| 54 | +pub use sbr_env::noise_fraction_table; | ||
| 51 | pub use tables::SAMPLE_RATES; | 55 | pub use tables::SAMPLE_RATES; |
| 52 | 56 | ||
| 53 | use decode::BlockDecoder; | 57 | use decode::BlockDecoder; |
crates/ec-aac/src/sbr_env.rsmodified+69 -1
| @@ -144,6 +144,48 @@ impl NoiseGen { | |||
| 144 | 144 | ||
| 145 | const LIMITER_FACTOR: [f64; 4] = [1.4125, 2.0, 4.0, 1.0e6]; // 1.5/3/6 dB, "no limit" | 145 | const LIMITER_FACTOR: [f64; 4] = [1.4125, 2.0, 4.0, 1.0e6]; // 1.5/3/6 dB, "no limit" |
| 146 | 146 | ||
| 147 | +/// (Round-17, Task 1 conviction check) Accumulates transmitted signal vs. | ||
| 148 | +/// noise energy per absolute QMF band across every [`adjust`] call in the | ||
| 149 | +/// process, weighted by how many QMF slots each contributes, when | ||
| 150 | +/// `EC_AAC_SBR_NOISE_FRACTION_DEBUG` is set. [`noise_fraction_table`] reads | ||
| 151 | +/// it back; both are no-ops (empty table) otherwise, so this costs nothing | ||
| 152 | +/// on the normal path. | ||
| 153 | +static NOISE_FRACTION_STATS: std::sync::OnceLock<std::sync::Mutex<Vec<(f64, f64)>>> = | ||
| 154 | + std::sync::OnceLock::new(); | ||
| 155 | + | ||
| 156 | +fn noise_fraction_debug() -> bool { | ||
| 157 | + std::env::var("EC_AAC_SBR_NOISE_FRACTION_DEBUG").is_ok() | ||
| 158 | +} | ||
| 159 | + | ||
| 160 | +/// `(band, signal_energy_sum, noise_energy_sum)` for every band that | ||
| 161 | +/// accumulated anything, both sums slot-count-weighted. | ||
| 162 | +pub fn noise_fraction_table() -> Vec<(usize, f64, f64)> { | ||
| 163 | + let Some(stats) = NOISE_FRACTION_STATS.get() else { | ||
| 164 | + return Vec::new(); | ||
| 165 | + }; | ||
| 166 | + stats | ||
| 167 | + .lock() | ||
| 168 | + .unwrap() | ||
| 169 | + .iter() | ||
| 170 | + .enumerate() | ||
| 171 | + .filter(|&(_, &(s, n))| s + n > 0.0) | ||
| 172 | + .map(|(b, &(s, n))| (b, s, n)) | ||
| 173 | + .collect() | ||
| 174 | +} | ||
| 175 | + | ||
| 176 | +fn accumulate_noise_fraction(band: usize, slots: usize, signal: f64, noise: f64) { | ||
| 177 | + if slots == 0 { | ||
| 178 | + return; | ||
| 179 | + } | ||
| 180 | + let stats = NOISE_FRACTION_STATS.get_or_init(|| std::sync::Mutex::new(vec![(0.0, 0.0); 256])); | ||
| 181 | + let mut g = stats.lock().unwrap(); | ||
| 182 | + if band >= g.len() { | ||
| 183 | + g.resize(band + 1, (0.0, 0.0)); | ||
| 184 | + } | ||
| 185 | + g[band].0 += signal * slots as f64; | ||
| 186 | + g[band].1 += noise * slots as f64; | ||
| 187 | +} | ||
| 188 | + | ||
| 147 | /// Applies envelope adjustment in place to `hf` (`[target band 0-based | 189 | /// Applies envelope adjustment in place to `hf` (`[target band 0-based |
| 148 | /// from kx][slot]`, as [`crate::sbr_hf::generate`] returns it): gain | 190 | /// from kx][slot]`, as [`crate::sbr_hf::generate`] returns it): gain |
| 149 | /// toward `env_energy`, the transmitted noise floor from `noise_energy`, | 191 | /// toward `env_energy`, the transmitted noise floor from `noise_energy`, |
| @@ -160,6 +202,13 @@ pub fn adjust( | |||
| 160 | ) { | 202 | ) { |
| 161 | let kx = tables.kx as usize; | 203 | let kx = tables.kx as usize; |
| 162 | let limiter_max = LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)]; | 204 | let limiter_max = LIMITER_FACTOR[usize::from(header.limiter_gains).min(3)]; |
| 205 | + let track_fraction = noise_fraction_debug(); | ||
| 206 | + // (Round-17, Task 1 corroboration) zeroes our injected noise so the | ||
| 207 | + // sweep's measured correlation can be checked against the | ||
| 208 | + // signal-only ceiling `noise_fraction_table` predicts, without | ||
| 209 | + // touching the transmitted noise ENERGY bookkeeping above (only the | ||
| 210 | + // actual PCM addition is skipped). | ||
| 211 | + let zero_noise = std::env::var("EC_AAC_SBR_NOISE_ZERO").is_ok(); | ||
| 163 | 212 | ||
| 164 | // Gain toward the transmitted envelope, per (envelope, band) cell, | 213 | // Gain toward the transmitted envelope, per (envelope, band) cell, |
| 165 | // limited relative to that cell's own noise floor. | 214 | // limited relative to that cell's own noise floor. |
| @@ -224,6 +273,15 @@ pub fn adjust( | |||
| 224 | gain = gain.min(limiter_max * ((noise_here + cur) / cur).sqrt()); | 273 | gain = gain.min(limiter_max * ((noise_here + cur) / cur).sqrt()); |
| 225 | gain = gain.min(limiter_max * 64.0); // absolute ceiling: never amplify a silent cell to infinity | 274 | gain = gain.min(limiter_max * 64.0); // absolute ceiling: never amplify a silent cell to infinity |
| 226 | gains.push(gain); | 275 | gains.push(gain); |
| 276 | + if track_fraction { | ||
| 277 | + let slots = (t1.max(0) - t0.max(0)).max(0) as usize; | ||
| 278 | + for band in lo..hi { | ||
| 279 | + if band < kx { | ||
| 280 | + continue; | ||
| 281 | + } | ||
| 282 | + accumulate_noise_fraction(band, slots, target, 0.0); | ||
| 283 | + } | ||
| 284 | + } | ||
| 227 | } | 285 | } |
| 228 | // `bs_interpol_freq` (per-subband linear interpolation of these sfb | 286 | // `bs_interpol_freq` (per-subband linear interpolation of these sfb |
| 229 | // gains, Sec 4.6.18.7.6) was tried here: a centre-frequency lerp | 287 | // gains, Sec 4.6.18.7.6) was tried here: a centre-frequency lerp |
| @@ -307,13 +365,23 @@ pub fn adjust( | |||
| 307 | let hi = tables.f_noise[q + 1] as usize; | 365 | let hi = tables.f_noise[q + 1] as usize; |
| 308 | let energy = noise_energy[ni].get(q).copied().unwrap_or(0.0); | 366 | let energy = noise_energy[ni].get(q).copied().unwrap_or(0.0); |
| 309 | let amp = (energy / (hi - lo).max(1) as f64).sqrt(); | 367 | let amp = (energy / (hi - lo).max(1) as f64).sqrt(); |
| 368 | + if track_fraction { | ||
| 369 | + let slots = (t1.max(0) - t0.max(0)).max(0) as usize; | ||
| 370 | + for band in lo..hi { | ||
| 371 | + if band < kx { | ||
| 372 | + continue; | ||
| 373 | + } | ||
| 374 | + accumulate_noise_fraction(band, slots, 0.0, amp * amp); | ||
| 375 | + } | ||
| 376 | + } | ||
| 310 | for band in lo..hi { | 377 | for band in lo..hi { |
| 311 | if band < kx || band - kx >= hf.len() { | 378 | if band < kx || band - kx >= hf.len() { |
| 312 | continue; | 379 | continue; |
| 313 | } | 380 | } |
| 314 | let row = &mut hf[band - kx]; | 381 | let row = &mut hf[band - kx]; |
| 315 | for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { | 382 | for slot in t0.max(0) as usize..(t1.max(0) as usize).min(row.len()) { |
| 316 | - row[slot] = row[slot] + rng.complex_unit().scale(amp); | 383 | + let n = rng.complex_unit().scale(amp); |
| 384 | + row[slot] = row[slot] + if zero_noise { Complex::ZERO } else { n }; | ||
| 317 | } | 385 | } |
| 318 | } | 386 | } |
| 319 | } | 387 | } |
crates/ec-aac/tests/sbr_real_library.rsmodified+25 -0
| @@ -791,6 +791,31 @@ fn sbr_real_library_matches_reference() { | |||
| 791 | } else { | 791 | } else { |
| 792 | 0.0 | 792 | 0.0 |
| 793 | }; | 793 | }; |
| 794 | + if ch == 0 && std::env::var("EC_AAC_SBR_NOISE_FRACTION_DEBUG").is_ok() { | ||
| 795 | + // (Round-17, Task 1) the noise-energy-fraction ceiling | ||
| 796 | + // hypothesis: predicted per-band correlation ceiling | ||
| 797 | + // sqrt(1 - noise_energy/(signal_energy+noise_energy)), | ||
| 798 | + // from OUR OWN transmitted (dequantized) envelope/noise | ||
| 799 | + // split -- printed once per file so it can be eyeballed | ||
| 800 | + // against per_band_lag_search's measured |corr| curve | ||
| 801 | + // below. Real QMF band width in output Hz is | ||
| 802 | + // `rate/128` (64-band synthesis over the output Nyquist). | ||
| 803 | + let band_hz = f64::from(rate) / 128.0; | ||
| 804 | + println!( | ||
| 805 | + " NOISE-FRACTION ceiling prediction (band_hz, f_noise, predicted_ceiling):" | ||
| 806 | + ); | ||
| 807 | + for (band, signal, noise) in ec_aac::noise_fraction_table() { | ||
| 808 | + if signal + noise <= 0.0 { | ||
| 809 | + continue; | ||
| 810 | + } | ||
| 811 | + let f = noise / (signal + noise); | ||
| 812 | + println!( | ||
| 813 | + " {:>6.0}Hz band{band:>3}: signal {signal:.3e} noise {noise:.3e} f_noise {f:.6} ceiling {:.4}", | ||
| 814 | + band as f64 * band_hz, | ||
| 815 | + (1.0 - f).max(0.0).sqrt() | ||
| 816 | + ); | ||
| 817 | + } | ||
| 818 | + } | ||
| 794 | if std::env::var("EC_AAC_SBR_BANDS").is_ok() && n >= 4_096 { | 819 | if std::env::var("EC_AAC_SBR_BANDS").is_ok() && n >= 4_096 { |
| 795 | let spectrum = per_band_correlation(&o[oa..oa + n], &t[ob..ob + n], rate); | 820 | let spectrum = per_band_correlation(&o[oa..oa + n], &t[ob..ob + n], rate); |
| 796 | println!(" ch{ch} per-QMF-band |corr| (band_hz, |corr|, band_index):"); | 821 | println!(" ch{ch} per-QMF-band |corr| (band_hz, |corr|, band_index):"); |