forge
Explore
Log in

Don't you dare go Hollow

Per-HF-band PCM lag search discriminates round-15 candidate A

Browse files at this commit

b768ffa5c9d2d8e6f8e44d56086386b30e85467fAhmet Kaan GümüşParent: 12a3673
  • test
    • per-HF-band PCM lag search discriminates round-15 candidate A
Show full message
test(aac): per-HF-band PCM lag search discriminates round-15 candidate A

round-14's per-band STFT magnitude-series correlation (0.04-0.5, even
spread) was consistent with either a per-stage time-alignment bug or a
common patch-map transform -- both give an even spread with no cliffs.
This adds a per-band bandpass PCM-domain lag search (sharp cascaded
RC bandpass + coarse-then-refine lag search, same shape as
best_lag_correlation) that searches actual sample lag directly instead
of relying on the STFT hop's 1024-sample bucket, which is too coarse to
see a one-QMF-slot (64-sample) delay.

Result on reeln (the one healthy real-file candidate): every HF band
from 2412Hz to 21878Hz peaks at lag 0 (ch0) / -1 (ch1), with a smooth,
monotonically-declining (not noisy) correlation from ~0.999 near the
crossover down to ~0.80 at 22kHz -- no band drifts to a nonzero multiple
of 64 samples. This exonerates candidate A (per-stage delay): the HF
path is not time-offset from the reference at any QMF-band granularity.
FMJ (the separately pre-SBR-broken file) shows the opposite signature --
noisy, band-to-band-jumping lags (288-704) with near-zero correlation --
confirming the diagnostic is sensitive to a real misalignment when one
exists, not just reporting 0 by construction.
crates/ec-aac/tests/sbr_real_library.rsmodified+86 -0
@@ -460,6 +460,87 @@ fn per_band_correlation(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, f64, f64)>
460 out460 out
461 }461 }
462 462
463+/// Per-band PCM-domain lag search (round-15 candidate-A discriminator):
464+/// `per_band_correlation`'s magnitude series has one sample per 1024-sample
465+/// STFT `HOP`, far coarser than a single QMF slot (64 samples) -- a stage
466+/// delay of one or a few slots between our HF path and the reference's would
467+/// vanish into that hop bucket instead of showing up as a lag. This instead
468+/// bandpasses `o`/`t` to each HF QMF band (the same zero-phase RC
469+/// `highpass`/`lowpass` primitives the crossover split already uses) and
470+/// searches PCM-sample lag directly, coarse-then-refine like
471+/// `best_lag_correlation`, so a consistent nonzero peak lag (especially a
472+/// multiple of 64 samples) convicts a per-stage time-alignment bug; lags
473+/// clustered at/near zero exonerate it in favour of a patch-map or
474+/// gain-mapping bug instead. Bands below kx (already known clean from
475+/// `per_band_correlation`) are skipped -- only HF bands (14..) are searched.
476+fn per_band_lag_search(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, i64, f64)> {
477+ const LAG_MAX: i64 = 1_024;
478+ const COARSE_STEP: i64 = 8;
479+ const COARSE: usize = 4_096;
480+ const WIN: usize = 65_536;
481+ let band_hz = f64::from(rate) / 256.0;
482+ let num_bands = (f64::from(rate) / 2.0 / band_hz).ceil() as usize;
483+ let n = o.len().min(t.len());
484+ let start = n.saturating_sub(WIN + LAG_MAX as usize).min(n / 4);
485+ let mut out = Vec::new();
486+ for band in 14..num_bands {
487+ let lo = (band as f64 * band_hz).max(1.0);
488+ let hi = ((band + 1) as f64 * band_hz).min(f64::from(rate) / 2.0 - 1.0);
489+ // A single-pole RC (the plain `highpass`/`lowpass` used for the
490+ // crossover split) has too gentle a rolloff to isolate a ~172Hz-wide
491+ // QMF band -- it leaks in the much stronger, well-correlated
492+ // below-crossover energy, which was confirmed here as a smooth,
493+ // frequency-monotonic false-high correlation with no matching cliff
494+ // in `per_band_correlation`'s FFT-bin version of the same bands.
495+ // `sharp_lowpass` (3x cascade, already used for the crossover-band
496+ // debug check) plus its highpass complement sharpens the stopband
497+ // enough that leakage from outside this one band stops dominating.
498+ let sharp_highpass = |s: &[f32], cutoff: f64| -> Vec<f32> {
499+ s.iter()
500+ .zip(&sharp_lowpass(s, rate, cutoff))
501+ .map(|(a, b)| a - b)
502+ .collect::<Vec<f32>>()
503+ };
504+ let bp = |s: &[f32]| -> Vec<f32> { sharp_lowpass(&sharp_highpass(s, lo), rate, hi) };
505+ let ob = bp(o);
506+ let tb = bp(t);
507+ let slice_at = |lag: i64, len: usize| -> Option<(usize, usize)> {
508+ let (oa, ta) = if lag >= 0 {
509+ (start, start + lag as usize)
510+ } else {
511+ (start + (-lag) as usize, start)
512+ };
513+ if oa + len > ob.len() || ta + len > tb.len() {
514+ None
515+ } else {
516+ Some((oa, ta))
517+ }
518+ };
519+ let mut coarse = (0i64, -1.0f64, 0.0f64);
520+ let mut lag = -LAG_MAX;
521+ while lag <= LAG_MAX {
522+ if let Some((oa, ta)) = slice_at(lag, COARSE) {
523+ let c = correlation(&ob[oa..oa + COARSE], &tb[ta..ta + COARSE]);
524+ if c.abs() > coarse.1 {
525+ coarse = (lag, c.abs(), c);
526+ }
527+ }
528+ lag += COARSE_STEP;
529+ }
530+ let mut best = coarse;
531+ for lag in (coarse.0 - COARSE_STEP)..=(coarse.0 + COARSE_STEP) {
532+ if let Some((oa, ta)) = slice_at(lag, WIN) {
533+ let c = correlation(&ob[oa..oa + WIN], &tb[ta..ta + WIN]);
534+ if c.abs() > best.1 {
535+ best = (lag, c.abs(), c);
536+ }
537+ }
538+ }
539+ out.push((band as f64 * band_hz, best.0, best.2));
540+ }
541+ out
542+}
543+
463 /// A file discovered under the user's own media directories: its path, which544 /// A file discovered under the user's own media directories: its path, which
464 /// AAC stream in it (0-based among AAC streams only) carries HE-AAC, and the545 /// AAC stream in it (0-based among AAC streams only) carries HE-AAC, and the
465 /// core/SBR crossover an ffprobe/ASC inspection already pinned for it -- used546 /// core/SBR crossover an ffprobe/ASC inspection already pinned for it -- used
@@ -625,6 +706,11 @@ fn sbr_real_library_matches_reference() {
625 for (hz, corr, band) in &spectrum {706 for (hz, corr, band) in &spectrum {
626 println!(" {hz:>6.0}Hz band{band:>3.0}: {corr:.4}");707 println!(" {hz:>6.0}Hz band{band:>3.0}: {corr:.4}");
627 }708 }
709+ let lags = per_band_lag_search(&o[oa..oa + n], &t[ob..ob + n], rate);
710+ println!(" ch{ch} per-HF-band PCM lag search (band_hz, best_lag, |corr|):");
711+ for (hz, lag, corr) in &lags {
712+ println!(" {hz:>6.0}Hz: lag {lag:>5} corr {corr:.4}");
713+ }
628 }714 }
629 println!(715 println!(
630 " ch{ch}: lag {lag}, full {full:.6}, below {:.0}Hz {low:.6}, above {:.0}Hz {high:.6}",716 " ch{ch}: lag {lag}, full {full:.6}, below {:.0}Hz {low:.6}, above {:.0}Hz {high:.6}",