Sbr_hf::generate runs the spec's feed-forward FIR, not an all-pole resynthesis
Browse files at this commit857b408bf0321a6be4941d3d4a639f8eb371cf74Parent: 20037c7- fix
- sbr_hf::generate runs the spec's feed-forward FIR, not an all-pole resynthesis
2 changed files +176 -51
- crates/ec-aac/src/sbr_hf.rs +123 -40
- crates/ec-aac/tests/sbr_real_library.rs +53 -11
crates/ec-aac/src/sbr_hf.rsmodified+123 -40
| @@ -24,14 +24,16 @@ | |||
| 24 | //! | 24 | //! |
| 25 | //! For each *source* subband, a covariance-method order-2 LPC is fit to | 25 | //! For each *source* subband, a covariance-method order-2 LPC is fit to |
| 26 | //! that subband's own QMF time series (two slots of carried-over history | 26 | //! that subband's own QMF time series (two slots of carried-over history |
| 27 | -//! plus the current frame's slots). The residual against those raw | 27 | +//! plus the current frame's slots), giving prediction coefficients `(a1, |
| 28 | -//! coefficients is then re-synthesised through a *bandwidth-expanded* | 28 | +//! a2)` such that `x[l] ~= a1*x[l-1] + a2*x[l-2]`. Per §4.6.18.6, the copied |
| 29 | -//! all-pole filter -- coefficients scaled by `bw` and `bw^2` -- which is | 29 | +//! series is then extended *feed-forward* by a bandwidth-expanded 2-tap FIR |
| 30 | -//! the standard SBR "inverse filtering" trick: `bw` near 1 leaves the | 30 | +//! built from those same coefficients: `X_high[l] = X_src[l] + bw*a1* |
| 31 | -//! subband's own resonance intact (a strongly tonal patch), `bw` near 0 | 31 | +//! X_src[l-1] + bw^2*a2*X_src[l-2]`. This is the standard SBR "inverse |
| 32 | -//! flattens it toward its whitened residual (a noise-like patch). `bw` | 32 | +//! filtering" trick: `bw` near 1 reinforces the subband's own resonance atop |
| 33 | -//! itself comes from `bs_invf_mode` and is smoothed frame to frame with a | 33 | +//! the raw copy (a strongly tonal patch), `bw` at 0 degenerates to the plain |
| 34 | -//! fast-attack/slow-release curve so inverse-filter mode changes don't pop. | 34 | +//! copy (no extra tap energy at all). `bw` itself comes from `bs_invf_mode` |
| 35 | +//! and is smoothed frame to frame with a fast-attack/slow-release curve so | ||
| 36 | +//! inverse-filter mode changes don't pop. | ||
| 35 | 37 | ||
| 36 | use crate::sbr_bands::BandTables; | 38 | use crate::sbr_bands::BandTables; |
| 37 | use ec_dsp::Complex; | 39 | use ec_dsp::Complex; |
| @@ -317,25 +319,27 @@ pub fn lpc2(x: &[Complex<f64>]) -> (Complex<f64>, Complex<f64>) { | |||
| 317 | (a1, a2) | 319 | (a1, a2) |
| 318 | } | 320 | } |
| 319 | 321 | ||
| 320 | -/// Bounds an AR(2) predictor's coefficients so the resynthesis recursion | 322 | +/// Bounds an AR(2) predictor's coefficients so the feed-forward FIR |
| 321 | -/// this module runs (`y = residual + ca1*y[n-1] + ca2*y[n-2]`) cannot | 323 | +/// extension `generate` builds from them (`X_high[l] = X_src[l] + |
| 322 | -/// diverge. | 324 | +/// bw*a1*X_src[l-1] + bw^2*a2*X_src[l-2]`) cannot blow up the copied tap |
| 325 | +/// energy on a badly-conditioned fit. | ||
| 323 | /// | 326 | /// |
| 324 | /// `lpc2` solves an unwindowed covariance-method normal-equation system, | 327 | /// `lpc2` solves an unwindowed covariance-method normal-equation system, |
| 325 | /// which -- unlike the autocorrelation/Levinson-Durbin method the reference | 328 | /// which -- unlike the autocorrelation/Levinson-Durbin method the reference |
| 326 | /// SBR tool uses -- carries no built-in stability guarantee: on a short (32 | 329 | /// SBR tool uses -- carries no built-in stability guarantee: on a short (32 |
| 327 | /// QMF slot) window of a strongly resonant real-music subband it can and | 330 | /// QMF slot) window of a strongly resonant real-music subband it can and |
| 328 | -/// does return a pole outside the unit circle, and the 32-sample feedback | 331 | +/// does return a pole outside the unit circle, driving the FIR taps to |
| 329 | -/// loop below then grows geometrically within a single frame (observed: | 332 | +/// unreasonably large magnitude for what should be a bounded-gain |
| 330 | -/// output rms in the hundreds of thousands on real HE-AAC files, versus | 333 | +/// extension (observed: output rms in the hundreds of thousands on real |
| 331 | -/// ~0.2 for a working decode). `|a1| + |a2| < 1` is a standard sufficient | 334 | +/// HE-AAC files, versus ~0.2 for a working decode, back when this fed an |
| 332 | -/// (if conservative) BIBO-stability bound for a direct-form-II section, so | 335 | +/// IIR recursion; the same unstable estimate still over-weights the taps |
| 333 | -/// clamping the pair to it whenever it is violated is a corner-cut: it | 336 | +/// here). `|a1| + |a2| < 1` is a standard sufficient (if conservative) |
| 334 | -/// trades exactness on the rare unstable-estimate frame for boundedness | 337 | +/// bound, so clamping the pair to it whenever it is violated is a |
| 335 | -/// everywhere, ceiling = slightly under-resonant HF on whichever frames hit | 338 | +/// corner-cut: it trades exactness on the rare unstable-estimate frame for |
| 336 | -/// the clamp; upgrade path = replace `lpc2` with the spec's own | 339 | +/// boundedness everywhere, ceiling = slightly under-resonant HF on |
| 337 | -/// Levinson-Durbin-derived reflection coefficients, which are stable by | 340 | +/// whichever frames hit the clamp; upgrade path = replace `lpc2` with the |
| 338 | -/// construction and would make this function a no-op. | 341 | +/// spec's own Levinson-Durbin-derived reflection coefficients, which are |
| 342 | +/// stable by construction and would make this function a no-op. | ||
| 339 | fn stabilize(coeffs: (Complex<f64>, Complex<f64>)) -> (Complex<f64>, Complex<f64>) { | 343 | fn stabilize(coeffs: (Complex<f64>, Complex<f64>)) -> (Complex<f64>, Complex<f64>) { |
| 340 | let (a1, a2) = coeffs; | 344 | let (a1, a2) = coeffs; |
| 341 | let mag = a1.norm_sqr().sqrt() + a2.norm_sqr().sqrt(); | 345 | let mag = a1.norm_sqr().sqrt() + a2.norm_sqr().sqrt(); |
| @@ -349,20 +353,19 @@ fn stabilize(coeffs: (Complex<f64>, Complex<f64>)) -> (Complex<f64>, Complex<f64 | |||
| 349 | } | 353 | } |
| 350 | } | 354 | } |
| 351 | 355 | ||
| 352 | -/// Per-source-subband history: the last two QMF slots, carried across | 356 | +/// Per-source-subband history: the last two QMF slots (`X_src[l-1]`, |
| 353 | -/// `generate` calls so the LPC and whitening filter have continuity at | 357 | +/// `X_src[l-2]`), carried across `generate` calls so the LPC fit and the |
| 354 | -/// frame boundaries instead of restarting cold every frame. | 358 | +/// feed-forward FIR extension both have continuity at frame boundaries |
| 359 | +/// instead of restarting cold every frame. | ||
| 355 | #[derive(Clone, Debug)] | 360 | #[derive(Clone, Debug)] |
| 356 | pub struct HfHistory { | 361 | pub struct HfHistory { |
| 357 | x: Vec<[Complex<f64>; 2]>, | 362 | x: Vec<[Complex<f64>; 2]>, |
| 358 | - y: Vec<[Complex<f64>; 2]>, | ||
| 359 | } | 363 | } |
| 360 | 364 | ||
| 361 | impl HfHistory { | 365 | impl HfHistory { |
| 362 | pub fn new(bands: usize) -> HfHistory { | 366 | pub fn new(bands: usize) -> HfHistory { |
| 363 | HfHistory { | 367 | HfHistory { |
| 364 | x: vec![[Complex::ZERO; 2]; bands], | 368 | x: vec![[Complex::ZERO; 2]; bands], |
| 365 | - y: vec![[Complex::ZERO; 2]; bands], | ||
| 366 | } | 369 | } |
| 367 | } | 370 | } |
| 368 | } | 371 | } |
| @@ -425,13 +428,18 @@ pub fn generate( | |||
| 425 | if std::env::var("EC_AAC_SBR_BW_DUMP").is_ok() { | 428 | if std::env::var("EC_AAC_SBR_BW_DUMP").is_ok() { |
| 426 | eprintln!("BW_DUMP target_band={target_band} source_band={source_band} bw={g:.6}"); | 429 | eprintln!("BW_DUMP target_band={target_band} source_band={source_band} bw={g:.6}"); |
| 427 | } | 430 | } |
| 431 | + // (Round-48, Task 3) §4.6.18.6 feed-forward extension: `X_high[l] | ||
| 432 | + // = X_src[l] + bw*a1*X_src[l-1] + bw^2*a2*X_src[l-2]`. `a1`/`a2` | ||
| 433 | + // are used directly (not negated): `lpc2` returns them in the | ||
| 434 | + // "predicts forward" convention (`x[l] ~= a1*x[l-1] + | ||
| 435 | + // a2*x[l-2]`, pinned by `lpc_recovers_the_poles_of_a_synthetic_ | ||
| 436 | + // ar2_process`), so applying them unnegated to the raw copy | ||
| 437 | + // continues the source subband's own AR resonance into the | ||
| 438 | + // patched band -- `bw` at 1 is maximally tonal, `bw` at 0 | ||
| 439 | + // degenerates the whole tap to zero, leaving the plain copy | ||
| 440 | + // (see `feed_forward_extension_matches_bw_endpoints`). | ||
| 428 | let ca1 = a1.scale(g); | 441 | let ca1 = a1.scale(g); |
| 429 | let ca2 = a2.scale(g * g); | 442 | let ca2 = a2.scale(g * g); |
| 430 | - let (mut y_prev1, mut y_prev2) = if source_band < history.y.len() { | ||
| 431 | - (history.y[source_band][1], history.y[source_band][0]) | ||
| 432 | - } else { | ||
| 433 | - (Complex::ZERO, Complex::ZERO) | ||
| 434 | - }; | ||
| 435 | let (mut x_prev1, mut x_prev2) = if source_band < history.x.len() { | 443 | let (mut x_prev1, mut x_prev2) = if source_band < history.x.len() { |
| 436 | (history.x[source_band][1], history.x[source_band][0]) | 444 | (history.x[source_band][1], history.x[source_band][0]) |
| 437 | } else { | 445 | } else { |
| @@ -439,8 +447,6 @@ pub fn generate( | |||
| 439 | }; | 447 | }; |
| 440 | let dst = &mut out[target_band - kx]; | 448 | let dst = &mut out[target_band - kx]; |
| 441 | for (n, &x) in series.iter().enumerate() { | 449 | for (n, &x) in series.iter().enumerate() { |
| 442 | - let residual = x - a1 * x_prev1 - a2 * x_prev2; | ||
| 443 | - let y = residual + ca1 * y_prev1 + ca2 * y_prev2; | ||
| 444 | // Round-37 (Task 2): `band_shift_correction` is proven exact | 450 | // Round-37 (Task 2): `band_shift_correction` is proven exact |
| 445 | // in isolation (a stationary tone replayed through this | 451 | // in isolation (a stationary tone replayed through this |
| 446 | // patch-copy site lands within a couple of FFT bins of its | 452 | // patch-copy site lands within a couple of FFT bins of its |
| @@ -454,11 +460,9 @@ pub fn generate( | |||
| 454 | // on ch0). The reference apparently exhibits this bank's own | 460 | // on ch0). The reference apparently exhibits this bank's own |
| 455 | // "wrong" cross-band placement convention, so matching it is | 461 | // "wrong" cross-band placement convention, so matching it is |
| 456 | // the bar: `dst` stays the plain, uncorrected copy. | 462 | // the bar: `dst` stays the plain, uncorrected copy. |
| 457 | - dst[n] = y; | 463 | + dst[n] = x + ca1 * x_prev1 + ca2 * x_prev2; |
| 458 | x_prev2 = x_prev1; | 464 | x_prev2 = x_prev1; |
| 459 | x_prev1 = x; | 465 | x_prev1 = x; |
| 460 | - y_prev2 = y_prev1; | ||
| 461 | - y_prev1 = y; | ||
| 462 | } | 466 | } |
| 463 | } | 467 | } |
| 464 | } | 468 | } |
| @@ -471,9 +475,6 @@ pub fn generate( | |||
| 471 | } | 475 | } |
| 472 | let n = series.len(); | 476 | let n = series.len(); |
| 473 | history.x[band] = [series[n - 2], series[n - 1]]; | 477 | history.x[band] = [series[n - 2], series[n - 1]]; |
| 474 | - // y-history only meaningfully advances for bands a patch actually | ||
| 475 | - // wrote; untouched bands hold their input as a neutral seed. | ||
| 476 | - history.y[band] = [series[n - 2], series[n - 1]]; | ||
| 477 | } | 478 | } |
| 478 | 479 | ||
| 479 | out | 480 | out |
| @@ -552,4 +553,86 @@ mod tests { | |||
| 552 | "fall {fall_step} rise {rise_step}" | 553 | "fall {fall_step} rise {rise_step}" |
| 553 | ); | 554 | ); |
| 554 | } | 555 | } |
| 556 | + | ||
| 557 | + /// (Round-48, Task 3) Pins the feed-forward extension's sign convention | ||
| 558 | + /// and `bw` endpoints against the spec-shaped formula (`generate`'s own | ||
| 559 | + /// doc comment): `X_high[l] = X_src[l] + bw*a1*X_src[l-1] + | ||
| 560 | + /// bw^2*a2*X_src[l-2]`, `a1`/`a2` used unnegated. | ||
| 561 | + /// | ||
| 562 | + /// Drives a *noiseless* AR(2) source (`x[n] = a1*x[n-1] + a2*x[n-2]` | ||
| 563 | + /// exactly, `|a1|+|a2| = 0.9 < 0.999` so `stabilize` never clamps) through | ||
| 564 | + /// two consecutive frames so the second frame's history/LPC fit is on | ||
| 565 | + /// data that satisfies the recurrence exactly end to end. At `bw=0` | ||
| 566 | + /// (`bs_invf_mode` OFF) the extension must degenerate to the plain copy. | ||
| 567 | + /// At `bw` near 1 (`bs_invf_mode` HIGH, `BW_TABLE[3] = 0.98`), since the | ||
| 568 | + /// noiseless source satisfies `a1*x[l-1] + a2*x[l-2] == x[l]` exactly, | ||
| 569 | + /// the correct (unnegated) convention algebraically collapses to | ||
| 570 | + /// `y[l] ~= (1 + bw) * x[l] ~= 1.98 * x[l]` -- strictly *more* tonal | ||
| 571 | + /// (same phase, amplified), not attenuated or inverted the way the | ||
| 572 | + /// negated convention would produce (`y[l] ~= (1 - bw) * x[l] ~= 0.02 * | ||
| 573 | + /// x[l]`, nearly cancelling the signal). This directly discriminates the | ||
| 574 | + /// two sign conventions, not just "some nonzero tap fired". | ||
| 575 | + #[test] | ||
| 576 | + fn feed_forward_extension_matches_bw_endpoints() { | ||
| 577 | + let tables = real_file_tables(); | ||
| 578 | + let kx = tables.kx as usize; | ||
| 579 | + let a1 = 0.6f64; | ||
| 580 | + let a2 = -0.3f64; | ||
| 581 | + let total = 60usize; | ||
| 582 | + let mut x = vec![1.0f64, 0.5]; | ||
| 583 | + for _ in 2..total { | ||
| 584 | + let n = x.len(); | ||
| 585 | + x.push(a1 * x[n - 1] + a2 * x[n - 2]); | ||
| 586 | + } | ||
| 587 | + let make_low_cur = |chunk: &[f64]| -> Vec<Vec<Complex<f64>>> { | ||
| 588 | + let mut low = vec![vec![Complex::ZERO; chunk.len()]; kx]; | ||
| 589 | + low[0] = chunk.iter().map(|&v| Complex::new(v, 0.0)).collect(); | ||
| 590 | + low | ||
| 591 | + }; | ||
| 592 | + let frame1 = make_low_cur(&x[0..30]); | ||
| 593 | + let frame2 = make_low_cur(&x[30..60]); | ||
| 594 | + let invf_off = vec![0u8; tables.n_q]; | ||
| 595 | + let invf_high = vec![3u8; tables.n_q]; | ||
| 596 | + | ||
| 597 | + // bw = 0: extension is an exact plain copy. | ||
| 598 | + let mut chirp0 = ChirpState::new(tables.n_q); | ||
| 599 | + let mut hist0 = HfHistory::new(kx); | ||
| 600 | + let _ = generate(&frame1, &tables, &invf_off, &mut chirp0, &mut hist0); | ||
| 601 | + let out0 = generate(&frame2, &tables, &invf_off, &mut chirp0, &mut hist0); | ||
| 602 | + let dst0 = &out0[14 - kx]; // target band 14, patch (0,14,14) offset 0 | ||
| 603 | + for (n, &y) in dst0.iter().enumerate() { | ||
| 604 | + let expect = x[30 + n]; | ||
| 605 | + assert!( | ||
| 606 | + (y.re - expect).abs() < 1e-9 && y.im.abs() < 1e-9, | ||
| 607 | + "bw=0 mismatch at n={n}: {y:?} vs {expect}" | ||
| 608 | + ); | ||
| 609 | + } | ||
| 610 | + | ||
| 611 | + // bw ~= 0.98: reinforces the source's own resonance, ~1.98x, same | ||
| 612 | + // phase -- not the ~0.02x a negated convention would produce. | ||
| 613 | + let mut chirp1 = ChirpState::new(tables.n_q); | ||
| 614 | + chirp1.bw = vec![0.0; tables.n_q]; // priming frame at bw=0 | ||
| 615 | + let mut hist1 = HfHistory::new(kx); | ||
| 616 | + let _ = generate(&frame1, &tables, &invf_off, &mut chirp1, &mut hist1); | ||
| 617 | + chirp1.bw = vec![0.98; tables.n_q]; // preload the exact target: `update` below is then a no-op | ||
| 618 | + let out1 = generate(&frame2, &tables, &invf_high, &mut chirp1, &mut hist1); | ||
| 619 | + let dst1 = &out1[14 - kx]; | ||
| 620 | + // Skip the first couple of slots (LPC fit needs the history to | ||
| 621 | + // settle); check the steady-state ratio on the rest. | ||
| 622 | + for (n, &y) in dst1.iter().enumerate().skip(4) { | ||
| 623 | + let expect = x[30 + n]; | ||
| 624 | + if expect.abs() < 1e-6 { | ||
| 625 | + continue; | ||
| 626 | + } | ||
| 627 | + let ratio = y.re / expect; | ||
| 628 | + assert!( | ||
| 629 | + (1.0..2.1).contains(&ratio), | ||
| 630 | + "bw=0.98 ratio out of range at n={n}: ratio={ratio} y={y:?} x={expect}" | ||
| 631 | + ); | ||
| 632 | + assert!( | ||
| 633 | + y.im.abs() < 0.05 * expect.abs().max(1e-6), | ||
| 634 | + "unexpected imaginary part at n={n}: {y:?}" | ||
| 635 | + ); | ||
| 636 | + } | ||
| 637 | + } | ||
| 555 | } | 638 | } |
crates/ec-aac/tests/sbr_real_library.rsmodified+53 -11
| @@ -1333,17 +1333,26 @@ fn qmf_domain_spec(o: &[f32], t: &[f32]) -> (QmfBandSlots, QmfBandSlots, QmfBand | |||
| 1333 | (o_spec, t_spec, sim_spec) | 1333 | (o_spec, t_spec, sim_spec) |
| 1334 | } | 1334 | } |
| 1335 | 1335 | ||
| 1336 | -fn hf_patch_simulator_qmf(o: &[f32], t: &[f32]) -> (BandCoherenceTable, BandCoherenceTable) { | 1336 | +/// Returns `(sim-vs-reference, sim-vs-ours, ours-vs-reference)` -- the last |
| 1337 | +/// pairing (round-48, Task 4a) is the ultimate content-match witness: how | ||
| 1338 | +/// well our actual HF QMF content agrees with the reference's, with no | ||
| 1339 | +/// simulator in between. | ||
| 1340 | +fn hf_patch_simulator_qmf( | ||
| 1341 | + o: &[f32], | ||
| 1342 | + t: &[f32], | ||
| 1343 | +) -> (BandCoherenceTable, BandCoherenceTable, BandCoherenceTable) { | ||
| 1337 | use ec_aac::sbr_qmf::SYNTHESIS_BANDS; | 1344 | use ec_aac::sbr_qmf::SYNTHESIS_BANDS; |
| 1338 | const WIN: usize = 16; | 1345 | const WIN: usize = 16; |
| 1339 | let (o_spec, t_spec, sim_spec) = qmf_domain_spec(o, t); | 1346 | let (o_spec, t_spec, sim_spec) = qmf_domain_spec(o, t); |
| 1340 | let slots = sim_spec.first().map(Vec::len).unwrap_or(0); | 1347 | let slots = sim_spec.first().map(Vec::len).unwrap_or(0); |
| 1341 | if slots < WIN { | 1348 | if slots < WIN { |
| 1342 | - return (Vec::new(), Vec::new()); | 1349 | + return (Vec::new(), Vec::new(), Vec::new()); |
| 1343 | } | 1350 | } |
| 1344 | let top_band = 43.min(SYNTHESIS_BANDS); | 1351 | let top_band = 43.min(SYNTHESIS_BANDS); |
| 1345 | let windows = slots / WIN; | 1352 | let windows = slots / WIN; |
| 1346 | - let band_coherence = |target: &[Vec<ec_dsp::Complex<f64>>]| -> Vec<(usize, f64)> { | 1353 | + let band_coherence = |source: &[Vec<ec_dsp::Complex<f64>>], |
| 1354 | + target: &[Vec<ec_dsp::Complex<f64>>]| | ||
| 1355 | + -> Vec<(usize, f64)> { | ||
| 1347 | (14..top_band) | 1356 | (14..top_band) |
| 1348 | .map(|band| { | 1357 | .map(|band| { |
| 1349 | let mut cross_re = 0.0f64; | 1358 | let mut cross_re = 0.0f64; |
| @@ -1353,7 +1362,7 @@ fn hf_patch_simulator_qmf(o: &[f32], t: &[f32]) -> (BandCoherenceTable, BandCohe | |||
| 1353 | for w in 0..windows { | 1362 | for w in 0..windows { |
| 1354 | let lo = w * WIN; | 1363 | let lo = w * WIN; |
| 1355 | let hi = lo + WIN; | 1364 | let hi = lo + WIN; |
| 1356 | - let se: f64 = sim_spec[band][lo..hi] | 1365 | + let se: f64 = source[band][lo..hi] |
| 1357 | .iter() | 1366 | .iter() |
| 1358 | .map(|c| c.re * c.re + c.im * c.im) | 1367 | .map(|c| c.re * c.re + c.im * c.im) |
| 1359 | .sum(); | 1368 | .sum(); |
| @@ -1366,7 +1375,7 @@ fn hf_patch_simulator_qmf(o: &[f32], t: &[f32]) -> (BandCoherenceTable, BandCohe | |||
| 1366 | } | 1375 | } |
| 1367 | let scale = (te / se).sqrt(); | 1376 | let scale = (te / se).sqrt(); |
| 1368 | for i in lo..hi { | 1377 | for i in lo..hi { |
| 1369 | - let sc = sim_spec[band][i].scale(scale); | 1378 | + let sc = source[band][i].scale(scale); |
| 1370 | let tc = target[band][i]; | 1379 | let tc = target[band][i]; |
| 1371 | let cross = sc * tc.conj(); | 1380 | let cross = sc * tc.conj(); |
| 1372 | cross_re += cross.re; | 1381 | cross_re += cross.re; |
| @@ -1385,7 +1394,11 @@ fn hf_patch_simulator_qmf(o: &[f32], t: &[f32]) -> (BandCoherenceTable, BandCohe | |||
| 1385 | }) | 1394 | }) |
| 1386 | .collect() | 1395 | .collect() |
| 1387 | }; | 1396 | }; |
| 1388 | - (band_coherence(&t_spec), band_coherence(&o_spec)) | 1397 | + ( |
| 1398 | + band_coherence(&sim_spec, &t_spec), | ||
| 1399 | + band_coherence(&sim_spec, &o_spec), | ||
| 1400 | + band_coherence(&o_spec, &t_spec), | ||
| 1401 | + ) | ||
| 1389 | } | 1402 | } |
| 1390 | 1403 | ||
| 1391 | /// (Round-46, Task 2) Per-band 2nd-order complex prediction transfer fit | 1404 | /// (Round-46, Task 2) Per-band 2nd-order complex prediction transfer fit |
| @@ -1398,20 +1411,26 @@ fn hf_patch_simulator_qmf(o: &[f32], t: &[f32]) -> (BandCoherenceTable, BandCohe | |||
| 1398 | /// bandwidth-expanded LPC coefficients: near `(1,0,0)` means "reference is | 1411 | /// bandwidth-expanded LPC coefficients: near `(1,0,0)` means "reference is |
| 1399 | /// also just a plain copy here", taps with real weight on lag 1/2 mean a | 1412 | /// also just a plain copy here", taps with real weight on lag 1/2 mean a |
| 1400 | /// genuine order-2 transformation is present. | 1413 | /// genuine order-2 transformation is present. |
| 1414 | +/// `target_is_ours`: fit against our own decode (`o_spec`) instead of the | ||
| 1415 | +/// reference's (`t_spec`) -- same `sim` (built from the reference's own low | ||
| 1416 | +/// band either way), so the two calls' fitted taps are directly comparable: | ||
| 1417 | +/// what transform sim would need to become ours vs. to become the reference. | ||
| 1401 | #[allow(clippy::needless_range_loop)] // fixed 3x3 complex Gaussian elimination, index math throughout | 1418 | #[allow(clippy::needless_range_loop)] // fixed 3x3 complex Gaussian elimination, index math throughout |
| 1402 | fn hf_patch_transfer_fit( | 1419 | fn hf_patch_transfer_fit( |
| 1403 | o: &[f32], | 1420 | o: &[f32], |
| 1404 | t: &[f32], | 1421 | t: &[f32], |
| 1405 | bands: &[usize], | 1422 | bands: &[usize], |
| 1423 | + target_is_ours: bool, | ||
| 1406 | ) -> Vec<(usize, [ec_dsp::Complex<f64>; 3])> { | 1424 | ) -> Vec<(usize, [ec_dsp::Complex<f64>; 3])> { |
| 1407 | - let (_, t_spec, sim_spec) = qmf_domain_spec(o, t); | 1425 | + let (o_spec, t_spec, sim_spec) = qmf_domain_spec(o, t); |
| 1426 | + let target_spec = if target_is_ours { &o_spec } else { &t_spec }; | ||
| 1408 | let mut results = Vec::new(); | 1427 | let mut results = Vec::new(); |
| 1409 | for &band in bands { | 1428 | for &band in bands { |
| 1410 | if band >= sim_spec.len() { | 1429 | if band >= sim_spec.len() { |
| 1411 | continue; | 1430 | continue; |
| 1412 | } | 1431 | } |
| 1413 | let sim_raw = &sim_spec[band]; | 1432 | let sim_raw = &sim_spec[band]; |
| 1414 | - let ref_raw = &t_spec[band]; | 1433 | + let ref_raw = &target_spec[band]; |
| 1415 | let len = sim_raw.len().min(ref_raw.len()); | 1434 | let len = sim_raw.len().min(ref_raw.len()); |
| 1416 | if len < 8 { | 1435 | if len < 8 { |
| 1417 | continue; | 1436 | continue; |
| @@ -2254,7 +2273,7 @@ fn sbr_real_library_matches_reference() { | |||
| 2254 | if std::env::var("EC_AAC_SBR_QMF_WITNESS").is_ok() && n >= 4_096 { | 2273 | if std::env::var("EC_AAC_SBR_QMF_WITNESS").is_ok() && n >= 4_096 { |
| 2255 | let win = &o[oa..oa + n]; | 2274 | let win = &o[oa..oa + n]; |
| 2256 | let twin = &t[ob..ob + n]; | 2275 | let twin = &t[ob..ob + n]; |
| 2257 | - let (vs_ref, vs_ours) = hf_patch_simulator_qmf(win, twin); | 2276 | + let (vs_ref, vs_ours, ours_vs_ref) = hf_patch_simulator_qmf(win, twin); |
| 2258 | let mean_in = |rows: &[(usize, f64)], lo: usize, hi: usize| { | 2277 | let mean_in = |rows: &[(usize, f64)], lo: usize, hi: usize| { |
| 2259 | let vals: Vec<f64> = rows | 2278 | let vals: Vec<f64> = rows |
| 2260 | .iter() | 2279 | .iter() |
| @@ -2285,14 +2304,37 @@ fn sbr_real_library_matches_reference() { | |||
| 2285 | mean_in(&vs_ours, 14, 28), | 2304 | mean_in(&vs_ours, 14, 28), |
| 2286 | mean_in(&vs_ours, 28, 43) | 2305 | mean_in(&vs_ours, 28, 43) |
| 2287 | ); | 2306 | ); |
| 2307 | + // (Round-48, Task 4a) the ultimate content-match witness: | ||
| 2308 | + // our HF QMF content directly against the reference's, no | ||
| 2309 | + // simulator step in between. | ||
| 2310 | + println!(" ch{ch} QMF-WITNESS ours-vs-REFERENCE per-band coherence (band, coh):"); | ||
| 2311 | + for (b, coh) in &ours_vs_ref { | ||
| 2312 | + println!(" band{b:>3}: {coh:.4}"); | ||
| 2313 | + } | ||
| 2314 | + println!( | ||
| 2315 | + " ch{ch} QMF-WITNESS ours-vs-REFERENCE mean: even-gap[14,28)={:.4} odd-gap[28,43)={:.4}", | ||
| 2316 | + mean_in(&ours_vs_ref, 14, 28), | ||
| 2317 | + mean_in(&ours_vs_ref, 28, 43) | ||
| 2318 | + ); | ||
| 2288 | // (Round-46, Task 2) fitted sim-to-reference transfer, one | 2319 | // (Round-46, Task 2) fitted sim-to-reference transfer, one |
| 2289 | // representative band per patch plus a spread across the | 2320 | // representative band per patch plus a spread across the |
| 2290 | // strong odd-gap patch (3,28,11) where the QMF witness shows | 2321 | // strong odd-gap patch (3,28,11) where the QMF witness shows |
| 2291 | // sim-vs-reference clearing 0.5. | 2322 | // sim-vs-reference clearing 0.5. |
| 2292 | let fit_bands = [14, 18, 22, 26, 28, 30, 32, 34, 36, 38, 39, 42]; | 2323 | let fit_bands = [14, 18, 22, 26, 28, 30, 32, 34, 36, 38, 39, 42]; |
| 2293 | - let fits = hf_patch_transfer_fit(win, twin, &fit_bands); | 2324 | + let fits_ref = hf_patch_transfer_fit(win, twin, &fit_bands, false); |
| 2294 | println!(" ch{ch} TASK2 FIT ref[l]~=a0*sim[l]+a1*sim[l-1]+a2*sim[l-2]:"); | 2325 | println!(" ch{ch} TASK2 FIT ref[l]~=a0*sim[l]+a1*sim[l-1]+a2*sim[l-2]:"); |
| 2295 | - for (b, a) in &fits { | 2326 | + for (b, a) in &fits_ref { |
| 2327 | + println!( | ||
| 2328 | + " band{b:>3}: a0=({:>7.3},{:>7.3}) a1=({:>7.3},{:>7.3}) a2=({:>7.3},{:>7.3})", | ||
| 2329 | + a[0].re, a[0].im, a[1].re, a[1].im, a[2].re, a[2].im | ||
| 2330 | + ); | ||
| 2331 | + } | ||
| 2332 | + // (Round-48, Task 2) same fit, target = ours: side by side | ||
| 2333 | + // with the above shows exactly what our transform does to | ||
| 2334 | + // the plain copy vs what the reference's does. | ||
| 2335 | + let fits_ours = hf_patch_transfer_fit(win, twin, &fit_bands, true); | ||
| 2336 | + println!(" ch{ch} TASK2 FIT ours[l]~=a0*sim[l]+a1*sim[l-1]+a2*sim[l-2]:"); | ||
| 2337 | + for (b, a) in &fits_ours { | ||
| 2296 | println!( | 2338 | println!( |
| 2297 | " band{b:>3}: a0=({:>7.3},{:>7.3}) a1=({:>7.3},{:>7.3}) a2=({:>7.3},{:>7.3})", | 2339 | " band{b:>3}: a0=({:>7.3},{:>7.3}) a1=({:>7.3},{:>7.3}) a2=({:>7.3},{:>7.3})", |
| 2298 | a[0].re, a[0].im, a[1].re, a[1].im, a[2].re, a[2].im | 2340 | a[0].re, a[0].im, a[1].re, a[1].im, a[2].re, a[2].im |