Sweep for a constant HF-only QMF-slot lag against the reference
Browse files at this commit4c5640d746558f6646e507b9a13f968c25a84671Parent: 1b74eca- test
- sweep for a constant HF-only QMF-slot lag against the reference
1 changed file +128 -0
crates/ec-aac/tests/sbr_real_library.rsmodified+128 -0
| @@ -833,6 +833,114 @@ fn bin_level_conviction( | |||
| 833 | out | 833 | out |
| 834 | } | 834 | } |
| 835 | 835 | ||
| 836 | +/// Round-38, Task 1: sweeps a constant HF-only sample-domain lag `k*64` | ||
| 837 | +/// (`k` in -8..=8; one QMF synthesis slot = 64 output samples) applied ONLY | ||
| 838 | +/// to `o`'s STFT read position, holding `t` fixed at the globally-aligned | ||
| 839 | +/// `ob` offset -- the low band (which drives the global `lag` this window is | ||
| 840 | +/// already re-sliced to via `oa`/`ob`) is never touched by this probe. Per | ||
| 841 | +/// bin, coherence is the SAME "direct" measure `bin_level_conviction` uses | ||
| 842 | +/// (`|sum_h O(h)*conj(T(h))| / sqrt(sum_h|O(h)|^2 * sum_h|T(h)|^2)`), then | ||
| 843 | +/// averaged uniformly over every bin in every HF SBR band from `min_band` | ||
| 844 | +/// upward (both parity regions together, not just the top-energy handful) -- | ||
| 845 | +/// a clear peak at k != 0 convicts a constant reference-vs-ours HF alignment | ||
| 846 | +/// offset of that many QMF slots; a flat or zero-peaked curve refutes it. | ||
| 847 | +fn hf_lag_sweep( | ||
| 848 | + o: &[f32], | ||
| 849 | + t: &[f32], | ||
| 850 | + oa: usize, | ||
| 851 | + ob: usize, | ||
| 852 | + n: usize, | ||
| 853 | + min_band: usize, | ||
| 854 | +) -> Vec<(i64, f64)> { | ||
| 855 | + const FFT_LEN: usize = 2048; | ||
| 856 | + const HOP: usize = 1024; | ||
| 857 | + const BINS_PER_SBR_BAND: usize = FFT_LEN / 256; | ||
| 858 | + const MAX_K: i64 = 8; | ||
| 859 | + const SLOT: i64 = 64; | ||
| 860 | + let win = ec_dsp::Window::<f32>::sine(FFT_LEN); | ||
| 861 | + let probe_bins = ec_dsp::RealFft::<f32>::new(FFT_LEN).spectrum_len(); | ||
| 862 | + let num_bands = probe_bins / BINS_PER_SBR_BAND; | ||
| 863 | + let lo_bin = min_band * BINS_PER_SBR_BAND; | ||
| 864 | + let hi_bin = (num_bands * BINS_PER_SBR_BAND).min(probe_bins); | ||
| 865 | + if lo_bin >= hi_bin { | ||
| 866 | + return Vec::new(); | ||
| 867 | + } | ||
| 868 | + let hops = if n >= FFT_LEN { | ||
| 869 | + (n - FFT_LEN) / HOP + 1 | ||
| 870 | + } else { | ||
| 871 | + 0 | ||
| 872 | + }; | ||
| 873 | + if hops == 0 { | ||
| 874 | + return Vec::new(); | ||
| 875 | + } | ||
| 876 | + let spec_of = |s: &[f32], start: usize| -> Vec<Vec<ec_dsp::Complex<f32>>> { | ||
| 877 | + let mut rfft = ec_dsp::RealFft::<f32>::new(FFT_LEN); | ||
| 878 | + let mut spec = vec![Vec::with_capacity(hops); probe_bins]; | ||
| 879 | + let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); probe_bins]; | ||
| 880 | + for h in 0..hops { | ||
| 881 | + let mut block = s[start + h * HOP..start + h * HOP + FFT_LEN].to_vec(); | ||
| 882 | + win.apply(&mut block); | ||
| 883 | + rfft.forward(&block, &mut spectrum); | ||
| 884 | + for (b, c) in spectrum.iter().enumerate() { | ||
| 885 | + spec[b].push(*c); | ||
| 886 | + } | ||
| 887 | + } | ||
| 888 | + spec | ||
| 889 | + }; | ||
| 890 | + let mut out = Vec::with_capacity((2 * MAX_K + 1) as usize); | ||
| 891 | + for k in -MAX_K..=MAX_K { | ||
| 892 | + let shift = k * SLOT; | ||
| 893 | + // Prefer shifting `o`'s read start (the literal ask), but `o` has no | ||
| 894 | + // samples before its own decode origin -- when the global alignment | ||
| 895 | + // already pins `oa` at (or near) 0, negative `k` has nowhere to read | ||
| 896 | + // from on that side. `coh(o[x], t[y])` only depends on `x - y`, so | ||
| 897 | + // shifting `t`'s read start the OPPOSITE direction instead | ||
| 898 | + // (`ob - shift`) tests the exact same relative offset and `t` (the | ||
| 899 | + // reference, decoded in full) always has the room. Only actually | ||
| 900 | + // falls back for `o`-boundary-starved `k`; the un-shifted `t` path | ||
| 901 | + // above is untouched for every `k` that fits within `o`. | ||
| 902 | + let (o_start, t_start) = { | ||
| 903 | + let os = oa as i64 + shift; | ||
| 904 | + if os >= 0 && os as usize + n <= o.len() { | ||
| 905 | + (os as usize, ob) | ||
| 906 | + } else { | ||
| 907 | + let ts = ob as i64 - shift; | ||
| 908 | + if ts >= 0 && ts as usize + n <= t.len() { | ||
| 909 | + (oa, ts as usize) | ||
| 910 | + } else { | ||
| 911 | + continue; | ||
| 912 | + } | ||
| 913 | + } | ||
| 914 | + }; | ||
| 915 | + let o_spec = spec_of(o, o_start); | ||
| 916 | + let t_spec = spec_of(t, t_start); | ||
| 917 | + let mut sum = 0.0f64; | ||
| 918 | + let mut count = 0usize; | ||
| 919 | + for b in lo_bin..hi_bin { | ||
| 920 | + let mut cross_re = 0.0f64; | ||
| 921 | + let mut cross_im = 0.0f64; | ||
| 922 | + let mut o_e = 0.0f64; | ||
| 923 | + let mut t_e = 0.0f64; | ||
| 924 | + for h in 0..hops { | ||
| 925 | + let oc = o_spec[b][h]; | ||
| 926 | + let tc = t_spec[b][h]; | ||
| 927 | + let cross = oc * tc.conj(); | ||
| 928 | + cross_re += f64::from(cross.re); | ||
| 929 | + cross_im += f64::from(cross.im); | ||
| 930 | + o_e += f64::from(oc.norm_sqr()); | ||
| 931 | + t_e += f64::from(tc.norm_sqr()); | ||
| 932 | + } | ||
| 933 | + let den = (o_e * t_e).sqrt(); | ||
| 934 | + if den > 0.0 { | ||
| 935 | + sum += (cross_re * cross_re + cross_im * cross_im).sqrt() / den; | ||
| 936 | + count += 1; | ||
| 937 | + } | ||
| 938 | + } | ||
| 939 | + out.push((k, if count > 0 { sum / count as f64 } else { 0.0 })); | ||
| 940 | + } | ||
| 941 | + out | ||
| 942 | +} | ||
| 943 | + | ||
| 836 | /// A file discovered under the user's own media directories: its path, which | 944 | /// A file discovered under the user's own media directories: its path, which |
| 837 | /// AAC stream in it (0-based among AAC streams only) carries HE-AAC, and the | 945 | /// AAC stream in it (0-based among AAC streams only) carries HE-AAC, and the |
| 838 | /// core/SBR crossover an ffprobe/ASC inspection already pinned for it -- used | 946 | /// core/SBR crossover an ffprobe/ASC inspection already pinned for it -- used |
| @@ -1464,6 +1572,26 @@ fn sbr_real_library_matches_reference() { | |||
| 1464 | " ch{ch} PARITY-SPLIT direct-coherence mean: even-gap[14,28)={even_mean:.4} (n={even_n}) odd-gap[28,43)={odd_mean:.4} (n={odd_n})" | 1572 | " ch{ch} PARITY-SPLIT direct-coherence mean: even-gap[14,28)={even_mean:.4} (n={even_n}) odd-gap[28,43)={odd_mean:.4} (n={odd_n})" |
| 1465 | ); | 1573 | ); |
| 1466 | } | 1574 | } |
| 1575 | + // (Round-38, Task 1) HF-only constant-lag sweep: see | ||
| 1576 | + // `hf_lag_sweep`'s doc comment. Reuses this window's already | ||
| 1577 | + // globally-aligned `oa`/`ob`/`n`; only `o`'s STFT read position is | ||
| 1578 | + // shifted, `n_slot`*64 samples at a time. | ||
| 1579 | + if std::env::var("EC_AAC_SBR_HF_LAG_SWEEP").is_ok() && n >= 4_096 { | ||
| 1580 | + let band_hz = f64::from(rate) / 256.0; | ||
| 1581 | + let min_band = (2_500.0 / band_hz).ceil() as usize; | ||
| 1582 | + let sweep = hf_lag_sweep(o, t, oa, ob, n, min_band); | ||
| 1583 | + println!( | ||
| 1584 | + " ch{ch} TASK1 HF-only lag sweep (k = QMF slots of 64 samples, mean direct HF-bin coherence):" | ||
| 1585 | + ); | ||
| 1586 | + for (k, coh) in &sweep { | ||
| 1587 | + println!(" k={k:>3}: coherence {coh:.4}"); | ||
| 1588 | + } | ||
| 1589 | + if let Some((peak_k, peak_c)) = | ||
| 1590 | + sweep.iter().max_by(|a, b| a.1.partial_cmp(&b.1).unwrap()) | ||
| 1591 | + { | ||
| 1592 | + println!(" ch{ch} HF lag sweep peak: k={peak_k} coherence {peak_c:.4}"); | ||
| 1593 | + } | ||
| 1594 | + } | ||
| 1467 | println!( | 1595 | println!( |
| 1468 | " ch{ch}: lag {lag}, full {full:.6}, below {:.0}Hz {low:.6}, above {:.0}Hz {high:.6}", | 1596 | " ch{ch}: lag {lag}, full {full:.6}, below {:.0}Hz {low:.6}, above {:.0}Hz {high:.6}", |
| 1469 | c.crossover_hz, c.crossover_hz | 1597 | c.crossover_hz, c.crossover_hz |