Convict the SBR noise-realization gap in synthesis, not adjust
Browse files at this commit30721a2757934aefda43addd16e15ec0a953e6a3Parent: 79dc3f6- fix
- convict the SBR noise-realization gap in synthesis, not adjust
2 changed files +110 -8
crates/ec-aac/src/sbr_qmf.rsmodified+81 -0
| @@ -936,4 +936,85 @@ mod tests { | |||
| 936 | "per-band synthesis gain ripple {ripple:e} exceeds 1e-4 -- SYNTH_TRIM's flat-scalar assumption is wrong" | 936 | "per-band synthesis gain ripple {ripple:e} exceeds 1e-4 -- SYNTH_TRIM's flat-scalar assumption is wrong" |
| 937 | ); | 937 | ); |
| 938 | } | 938 | } |
| 939 | + | ||
| 940 | + /// (Round-43, Task 1 conviction) The tone-excitation gain above | ||
| 941 | + /// (0.595853 amplitude / ~0.355 energy) was calibrated with a single | ||
| 942 | + /// SLOWLY ROTATING phasor -- narrowband within its own subband's | ||
| 943 | + /// Nyquist. `sbr_env::adjust`'s injected noise instead draws an | ||
| 944 | + /// INDEPENDENT complex value every slot (white across the subband's | ||
| 945 | + /// full available bandwidth, uncorrelated slot to slot), a completely | ||
| 946 | + /// different excitation shape through the SAME overlap-add prototype. | ||
| 947 | + /// This measures that white-noise round-trip ENERGY gain the same way, | ||
| 948 | + /// to check whether it matches the tone's ~0.355 or is substantially | ||
| 949 | + /// lower (which would mean the `namp`/`SYNTH_TRIM` calibration, tuned | ||
| 950 | + /// only against tones, silently starves broadband content specifically). | ||
| 951 | + #[test] | ||
| 952 | + fn synthesis_energy_gain_for_white_noise_excitation() { | ||
| 953 | + fn xorshift(seed: u64) -> impl FnMut() -> f64 { | ||
| 954 | + let mut state = seed | 1; | ||
| 955 | + move || { | ||
| 956 | + state ^= state << 13; | ||
| 957 | + state ^= state >> 7; | ||
| 958 | + state ^= state << 17; | ||
| 959 | + (state >> 11) as f64 / (1u64 << 53) as f64 - 0.5 | ||
| 960 | + } | ||
| 961 | + } | ||
| 962 | + let k0 = SYNTHESIS_BANDS / 2; | ||
| 963 | + let slots = 20_000usize; | ||
| 964 | + let mut rng = xorshift(0xdead_beef); | ||
| 965 | + let mut synthesis = Synthesis::new(); | ||
| 966 | + let mut in_energy = 0.0f64; | ||
| 967 | + let mut out_energy = 0.0f64; | ||
| 968 | + let mut out = Vec::with_capacity(slots * SYNTHESIS_BANDS); | ||
| 969 | + for _ in 0..slots { | ||
| 970 | + let mut v = [Complex::new(0.0, 0.0); SYNTHESIS_BANDS]; | ||
| 971 | + let c = Complex::new(rng(), rng()); | ||
| 972 | + v[k0] = c; | ||
| 973 | + in_energy += c.norm_sqr(); | ||
| 974 | + out.extend(synthesis.process_slot(&v)); | ||
| 975 | + } | ||
| 976 | + // Steady state only (skip the filter's fill-up transient). | ||
| 977 | + let steady = &out[out.len() / 2..]; | ||
| 978 | + for &s in steady { | ||
| 979 | + out_energy += f64::from(s) * f64::from(s); | ||
| 980 | + } | ||
| 981 | + let steady_slots = steady.len() / SYNTHESIS_BANDS; | ||
| 982 | + let steady_in_energy = in_energy * steady_slots as f64 / slots as f64; | ||
| 983 | + let energy_gain = out_energy / steady_in_energy; | ||
| 984 | + println!( | ||
| 985 | + "white-noise round-trip energy gain (band {k0}): {energy_gain:.6} \ | ||
| 986 | + (tone reference from synthesis_gain_is_flat_across_bands: ~0.355)" | ||
| 987 | + ); | ||
| 988 | + | ||
| 989 | + // Where did that energy actually land? A real PCM rate of | ||
| 990 | + // SYNTHESIS_BANDS*fs_core; the source subband centre sits at | ||
| 991 | + // (k0+0.5)/SYNTHESIS_BANDS of that rate's Nyquist. FFT the steady | ||
| 992 | + // output and sum energy in-band vs out-of-band. | ||
| 993 | + const FFT_LEN: usize = 2048; | ||
| 994 | + let mut rfft = ec_dsp::RealFft::<f32>::new(FFT_LEN); | ||
| 995 | + let bins = rfft.spectrum_len(); | ||
| 996 | + let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; | ||
| 997 | + let mut in_band = 0.0f64; | ||
| 998 | + let mut total = 0.0f64; | ||
| 999 | + let band_lo = (k0 as f64 / SYNTHESIS_BANDS as f64 * bins as f64) as usize; | ||
| 1000 | + let band_hi = ((k0 + 1) as f64 / SYNTHESIS_BANDS as f64 * bins as f64) as usize; | ||
| 1001 | + let win = ec_dsp::Window::<f32>::sine(FFT_LEN); | ||
| 1002 | + let hops = (steady.len() - FFT_LEN) / FFT_LEN; | ||
| 1003 | + for h in 0..hops { | ||
| 1004 | + let mut block = steady[h * FFT_LEN..h * FFT_LEN + FFT_LEN].to_vec(); | ||
| 1005 | + win.apply(&mut block); | ||
| 1006 | + rfft.forward(&block, &mut spectrum); | ||
| 1007 | + for (i, c) in spectrum.iter().enumerate() { | ||
| 1008 | + let e = f64::from(c.norm_sqr()); | ||
| 1009 | + total += e; | ||
| 1010 | + if i >= band_lo && i < band_hi { | ||
| 1011 | + in_band += e; | ||
| 1012 | + } | ||
| 1013 | + } | ||
| 1014 | + } | ||
| 1015 | + println!( | ||
| 1016 | + "energy in source subband's own frequency range: {:.4} of total (bins [{band_lo},{band_hi}) of {bins})", | ||
| 1017 | + in_band / total | ||
| 1018 | + ); | ||
| 1019 | + } | ||
| 939 | } | 1020 | } |
crates/ec-aac/tests/sbr_real_library.rsmodified+29 -8
| @@ -2235,14 +2235,35 @@ fn sbr_actual_noise_fraction() { | |||
| 2235 | // exactly where the pre-fix gap was largest. | 2235 | // exactly where the pre-fix gap was largest. |
| 2236 | // | 2236 | // |
| 2237 | // The charter's ±0.05-of-bookkept (~0.38) target is NOT met by this | 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 | 2238 | + // fix alone (round-43 investigated why, with `EC_AAC_SBR_CELLDUMP` in |
| 2239 | - // (6718/6891/7063Hz) still realize ~0.001-0.015 against a ~0.373 | 2239 | + // `sbr_env::adjust`): `sbr_env::adjust`'s per-cell signal/noise split |
| 2240 | - // bookkept split regardless of either accounting variant tried, and | 2240 | + // is NOT the culprit -- corrected-band dumps (the charter's quoted |
| 2241 | - // bands above ~8.5kHz show a fraction that decays steadily toward 0 as | 2241 | + // "6718/6891/7063Hz" and "8.5-13kHz decaying" bands turned out to be |
| 2242 | - // frequency rises (0.19 at 8.1kHz down to ~0.01 by 13kHz) -- a THIRD, | 2242 | + // mislabeled by a factor of two, this file's true SBR band spacing is |
| 2243 | - // still-unlocated defect, not this accounting mismatch (open, see | 2243 | + // `rate/128` = 344.5Hz not the 172.3Hz FFT-analysis spacing those Hz |
| 2244 | - // ledger). This assertion is therefore a regression floor against the | 2244 | + // labels come from) show `adjust` injecting 40-85% noise share in the |
| 2245 | - // two bugs fixed here re-appearing, not a claim of bookkept parity. | 2245 | + // QMF domain, at or ABOVE the ~0.37 bookkept split, not starved. The |
| 2246 | + // real defect is downstream, in `sbr_qmf::Synthesis`: | ||
| 2247 | + // `synthesis_energy_gain_for_white_noise_excitation` (round-43) drives | ||
| 2248 | + // ONE subband with an independent-per-slot random complex value (what | ||
| 2249 | + // `adjust`'s noise injection actually looks like) through the same | ||
| 2250 | + // overlap-add prototype `synthesis_gain_is_flat_across_bands` | ||
| 2251 | + // calibrated against a slowly-rotating TONE phasor, and finds | ||
| 2252 | + // essentially 0% of that injected energy lands back in the source | ||
| 2253 | + // subband's own frequency range (vs. a tone's clean in-band | ||
| 2254 | + // reconstruction) -- an i.i.d.-per-slot draw is not a legitimate | ||
| 2255 | + // "bandlimited content in this subband" input to this filterbank's | ||
| 2256 | + // complex-modulation convention, so it aliases out almost entirely | ||
| 2257 | + // rather than reconstructing as audible noise near the intended | ||
| 2258 | + // frequency. This is exactly the charter's Task 3 "noise SEQUENCE is | ||
| 2259 | + // the last mile" scenario -- fixing it needs the round-17 noise-stream | ||
| 2260 | + // derivation (per-slot values shaped to actually decode as noise | ||
| 2261 | + // through THIS synthesis convention), not another `adjust()` accounting | ||
| 2262 | + // pass. Deferred: round-17 derivation, budget did not reach it this | ||
| 2263 | + // round (open, see ledger). This assertion remains a regression floor | ||
| 2264 | + // against the two round-42 accounting bugs re-appearing, not a claim of | ||
| 2265 | + // bookkept parity -- NOT tightened, since the actual gating defect | ||
| 2266 | + // (synthesis-stage noise-sequence shape) is still unfixed. | ||
| 2246 | assert!( | 2267 | assert!( |
| 2247 | actual_total > 0.15, | 2268 | actual_total > 0.15, |
| 2248 | "whole-HF realized noise fraction regressed to {actual_total:.4} \ | 2269 | "whole-HF realized noise fraction regressed to {actual_total:.4} \ |