diag(aac-sbr): convict per_band_lag_search's bandpass, task-2 finds direct-match
Browse files at this commitbf135ff3860d7e3f31bf7c0af6d7cb89edfea2a1Parent: 8bdf7f51 changed file +236 -0
crates/ec-aac/tests/sbr_real_library.rsmodified+236 -0
| @@ -480,6 +480,24 @@ fn per_band_correlation(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, f64, f64)> | |||
| 480 | /// clustered at/near zero exonerate it in favour of a patch-map or | 480 | /// clustered at/near zero exonerate it in favour of a patch-map or |
| 481 | /// gain-mapping bug instead. Bands below kx (already known clean from | 481 | /// gain-mapping bug instead. Bands below kx (already known clean from |
| 482 | /// `per_band_correlation`) are skipped -- only HF bands (14..) are searched. | 482 | /// `per_band_correlation`) are skipped -- only HF bands (14..) are searched. |
| 483 | +/// | ||
| 484 | +/// CONVICTED (round-26, Task 1): `bandpass_offset_probe` correlated this same | ||
| 485 | +/// `sharp_lowpass`/`sharp_highpass` cascade's band-k output against the | ||
| 486 | +/// REFERENCE's band-(k+/-10) output for several HF `k` -- and the DOWNWARD | ||
| 487 | +/// offset (band k-10, closer to the strongly-correlated below-crossover | ||
| 488 | +/// content) read a HIGHER correlation than the nominal same-band pairing in | ||
| 489 | +/// every HF band tried (e.g. band 34: same 0.937 vs -10 0.966). A filter | ||
| 490 | +/// with real ~172Hz selectivity cannot produce that; a "sharp" 3x-cascaded | ||
| 491 | +/// single-pole RC (~18dB/oct) at a 9kHz center still passes a wide swath | ||
| 492 | +/// below its corner, so each "band k" reading here is really "everything up | ||
| 493 | +/// to and including band k" -- cumulative correlation, dominated by the | ||
| 494 | +/// genuinely-matching low band, which manufactures the smooth monotonic | ||
| 495 | +/// decline earlier rounds chased. The reported LAG values are still valid | ||
| 496 | +/// (a filter with no stopband selectivity still has a passband delay that | ||
| 497 | +/// tracks the true alignment), but the reported per-band |corr| AMPLITUDE | ||
| 498 | +/// here is NOT a trustworthy per-band figure -- use `per_band_phase`'s | ||
| 499 | +/// cross-spectrum magnitude ratio (or the bin-level `bin_level_conviction`) | ||
| 500 | +/// for that instead. | ||
| 483 | fn per_band_lag_search(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, i64, f64)> { | 501 | fn per_band_lag_search(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, i64, f64)> { |
| 484 | const LAG_MAX: i64 = 1_024; | 502 | const LAG_MAX: i64 = 1_024; |
| 485 | const COARSE_STEP: i64 = 8; | 503 | const COARSE_STEP: i64 = 8; |
| @@ -632,6 +650,189 @@ fn per_band_phase(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, f64, f64)> { | |||
| 632 | out | 650 | out |
| 633 | } | 651 | } |
| 634 | 652 | ||
| 653 | +/// Round-26, Task 1: convicts (or acquits) `per_band_lag_search`'s bandpass. | ||
| 654 | +/// Applies the SAME 3x-cascaded RC bandpass that diagnostic uses to `o` at | ||
| 655 | +/// band `band` and to `t` at band `band` (same-band control), `band+offset` | ||
| 656 | +/// and `band-offset`. A single-pole cascade's stopband is gentle enough that | ||
| 657 | +/// "band k" may really pass "everything below f_k" -- if so, `o`-band-k and | ||
| 658 | +/// `t`-band-(k+offset) still both carry the same dominant low-band leakage | ||
| 659 | +/// and read highly correlated even though their nominal passbands don't | ||
| 660 | +/// overlap at all, which convicts the filter, not the content. Below the SBR | ||
| 661 | +/// crossover, genuinely narrowband-matching content should decorrelate | ||
| 662 | +/// quickly as `offset` grows; a HF band that does NOT decorrelate at | ||
| 663 | +/// `offset` bands away is evidence the filter has no real selectivity there. | ||
| 664 | +fn bandpass_offset_probe( | ||
| 665 | + o: &[f32], | ||
| 666 | + t: &[f32], | ||
| 667 | + rate: u32, | ||
| 668 | + band: usize, | ||
| 669 | + offset: i64, | ||
| 670 | +) -> (f64, f64, f64) { | ||
| 671 | + let band_hz = f64::from(rate) / 256.0; | ||
| 672 | + let bp_at = |s: &[f32], b: usize| -> Vec<f32> { | ||
| 673 | + let lo = (b as f64 * band_hz).max(1.0); | ||
| 674 | + let hi = ((b + 1) as f64 * band_hz).min(f64::from(rate) / 2.0 - 1.0); | ||
| 675 | + let sharp_highpass = |s: &[f32], cutoff: f64| -> Vec<f32> { | ||
| 676 | + s.iter() | ||
| 677 | + .zip(&sharp_lowpass(s, rate, cutoff)) | ||
| 678 | + .map(|(a, b)| a - b) | ||
| 679 | + .collect::<Vec<f32>>() | ||
| 680 | + }; | ||
| 681 | + sharp_lowpass(&sharp_highpass(s, lo), rate, hi) | ||
| 682 | + }; | ||
| 683 | + let ob = bp_at(o, band); | ||
| 684 | + let same_corr = correlation(&ob, &bp_at(t, band)); | ||
| 685 | + let hi_band = band as i64 + offset; | ||
| 686 | + let hi_corr = if hi_band >= 0 { | ||
| 687 | + correlation(&ob, &bp_at(t, hi_band as usize)) | ||
| 688 | + } else { | ||
| 689 | + 0.0 | ||
| 690 | + }; | ||
| 691 | + let lo_band = band as i64 - offset; | ||
| 692 | + let lo_corr = if lo_band >= 0 { | ||
| 693 | + correlation(&ob, &bp_at(t, lo_band as usize)) | ||
| 694 | + } else { | ||
| 695 | + 0.0 | ||
| 696 | + }; | ||
| 697 | + (same_corr, hi_corr, lo_corr) | ||
| 698 | +} | ||
| 699 | + | ||
| 700 | +/// Round-26, Task 2: bin-level (no filter, no cross-band summing) comparison | ||
| 701 | +/// of `o` against `t` in the most energetic HF SBR bands at/above `min_band`, | ||
| 702 | +/// three ways: DIRECT (bin i vs bin i), MIRROR (bin i vs the band's bins in | ||
| 703 | +/// REVERSED order -- the signature of a QMF band-orientation/parity flip | ||
| 704 | +/// between our own-design synthesis and the reference's convention: energies | ||
| 705 | +/// right, direct corr ~0, phase random, exactly what round-14/16 observed), | ||
| 706 | +/// and SHIFT (the whole `BINS_PER_SBR_BAND`-bin band matched one band up or | ||
| 707 | +/// down -- the signature of a `build_patches` source-mapping offset). | ||
| 708 | +/// Coherence per bin pair is | ||
| 709 | +/// `|sum_h O(h)*conj(T(h))| / sqrt(sum_h|O(h)|^2 * sum_h|T(h)|^2)` (in | ||
| 710 | +/// [0,1]; 1 = perfectly phase+magnitude locked, ~0 = unrelated), accumulated | ||
| 711 | +/// over the SAME aligned hops `per_band_phase` uses, then averaged over the | ||
| 712 | +/// band's 8 bins. Bands are picked by OUR OWN energy (the "real" HF content), | ||
| 713 | +/// not the reference's, so a genuinely-silent HF band can't crowd out an | ||
| 714 | +/// energetic one just because it happens to sort first. | ||
| 715 | +fn bin_level_conviction( | ||
| 716 | + o: &[f32], | ||
| 717 | + t: &[f32], | ||
| 718 | + rate: u32, | ||
| 719 | + min_band: usize, | ||
| 720 | + top_n: usize, | ||
| 721 | +) -> Vec<(f64, f64, f64, f64, f64, f64)> { | ||
| 722 | + const FFT_LEN: usize = 2048; | ||
| 723 | + const HOP: usize = 1024; | ||
| 724 | + const BINS_PER_SBR_BAND: usize = FFT_LEN / 256; | ||
| 725 | + let win = ec_dsp::Window::<f32>::sine(FFT_LEN); | ||
| 726 | + let mut rfft = ec_dsp::RealFft::<f32>::new(FFT_LEN); | ||
| 727 | + let bins = rfft.spectrum_len(); | ||
| 728 | + let n = o.len().min(t.len()); | ||
| 729 | + let hops = if n >= FFT_LEN { | ||
| 730 | + (n - FFT_LEN) / HOP + 1 | ||
| 731 | + } else { | ||
| 732 | + 0 | ||
| 733 | + }; | ||
| 734 | + let mut o_spec = vec![Vec::with_capacity(hops); bins]; | ||
| 735 | + let mut t_spec = vec![Vec::with_capacity(hops); bins]; | ||
| 736 | + let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; | ||
| 737 | + for (src, dst) in [(o, &mut o_spec), (t, &mut t_spec)] { | ||
| 738 | + for h in 0..hops { | ||
| 739 | + let mut block = src[h * HOP..h * HOP + FFT_LEN].to_vec(); | ||
| 740 | + win.apply(&mut block); | ||
| 741 | + rfft.forward(&block, &mut spectrum); | ||
| 742 | + for (b, c) in spectrum.iter().enumerate() { | ||
| 743 | + dst[b].push(*c); | ||
| 744 | + } | ||
| 745 | + } | ||
| 746 | + } | ||
| 747 | + if hops == 0 { | ||
| 748 | + return Vec::new(); | ||
| 749 | + } | ||
| 750 | + let band_hz = f64::from(rate) / 256.0; | ||
| 751 | + let num_bands = bins / BINS_PER_SBR_BAND; | ||
| 752 | + let coh = |ob: usize, tb: usize| -> f64 { | ||
| 753 | + if ob >= bins || tb >= bins { | ||
| 754 | + return 0.0; | ||
| 755 | + } | ||
| 756 | + let mut cross_re = 0.0f64; | ||
| 757 | + let mut cross_im = 0.0f64; | ||
| 758 | + let mut o_e = 0.0f64; | ||
| 759 | + let mut t_e = 0.0f64; | ||
| 760 | + for h in 0..hops { | ||
| 761 | + let oc = o_spec[ob][h]; | ||
| 762 | + let tc = t_spec[tb][h]; | ||
| 763 | + let cross = oc * tc.conj(); | ||
| 764 | + cross_re += f64::from(cross.re); | ||
| 765 | + cross_im += f64::from(cross.im); | ||
| 766 | + o_e += f64::from(oc.norm_sqr()); | ||
| 767 | + t_e += f64::from(tc.norm_sqr()); | ||
| 768 | + } | ||
| 769 | + let den = (o_e * t_e).sqrt(); | ||
| 770 | + if den > 0.0 { | ||
| 771 | + (cross_re * cross_re + cross_im * cross_im).sqrt() / den | ||
| 772 | + } else { | ||
| 773 | + 0.0 | ||
| 774 | + } | ||
| 775 | + }; | ||
| 776 | + // Rank HF bands by OUR OWN energy, so the energetic-band handful is | ||
| 777 | + // chosen by real content, not by whatever the reference happens to carry. | ||
| 778 | + let mut ranked: Vec<(usize, f64)> = (min_band..num_bands) | ||
| 779 | + .map(|band| { | ||
| 780 | + let lo = band * BINS_PER_SBR_BAND; | ||
| 781 | + let hi = (lo + BINS_PER_SBR_BAND).min(bins); | ||
| 782 | + let e: f64 = (lo..hi) | ||
| 783 | + .flat_map(|b| o_spec[b].iter()) | ||
| 784 | + .map(|c| f64::from(c.norm_sqr())) | ||
| 785 | + .sum(); | ||
| 786 | + (band, e) | ||
| 787 | + }) | ||
| 788 | + .collect(); | ||
| 789 | + ranked.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); | ||
| 790 | + let mut selected: Vec<usize> = ranked.into_iter().take(top_n).map(|(b, _)| b).collect(); | ||
| 791 | + selected.sort_unstable(); | ||
| 792 | + | ||
| 793 | + let mut out = Vec::with_capacity(selected.len()); | ||
| 794 | + for band in selected { | ||
| 795 | + let lo = band * BINS_PER_SBR_BAND; | ||
| 796 | + let hi = (lo + BINS_PER_SBR_BAND).min(bins); | ||
| 797 | + if lo >= hi { | ||
| 798 | + continue; | ||
| 799 | + } | ||
| 800 | + let mut energy = 0.0f64; | ||
| 801 | + let mut direct = 0.0f64; | ||
| 802 | + let mut mirror = 0.0f64; | ||
| 803 | + let mut shift_up = 0.0f64; | ||
| 804 | + let mut shift_down = 0.0f64; | ||
| 805 | + let count = (hi - lo) as f64; | ||
| 806 | + // `i` feeds several derived indices (mirror/shift math below), not a | ||
| 807 | + // plain element access -- an iterator/enumerate rewrite would be | ||
| 808 | + // less readable here, not more. | ||
| 809 | + #[allow(clippy::needless_range_loop)] | ||
| 810 | + for i in lo..hi { | ||
| 811 | + energy += o_spec[i] | ||
| 812 | + .iter() | ||
| 813 | + .map(|c| f64::from(c.norm_sqr())) | ||
| 814 | + .sum::<f64>(); | ||
| 815 | + direct += coh(i, i); | ||
| 816 | + mirror += coh(i, hi + lo - 1 - i); | ||
| 817 | + shift_up += coh(i, i + BINS_PER_SBR_BAND); | ||
| 818 | + shift_down += if i >= BINS_PER_SBR_BAND { | ||
| 819 | + coh(i, i - BINS_PER_SBR_BAND) | ||
| 820 | + } else { | ||
| 821 | + 0.0 | ||
| 822 | + }; | ||
| 823 | + } | ||
| 824 | + out.push(( | ||
| 825 | + band as f64 * band_hz, | ||
| 826 | + energy, | ||
| 827 | + direct / count, | ||
| 828 | + mirror / count, | ||
| 829 | + shift_up / count, | ||
| 830 | + shift_down / count, | ||
| 831 | + )); | ||
| 832 | + } | ||
| 833 | + out | ||
| 834 | +} | ||
| 835 | + | ||
| 635 | /// A file discovered under the user's own media directories: its path, which | 836 | /// A file discovered under the user's own media directories: its path, which |
| 636 | /// AAC stream in it (0-based among AAC streams only) carries HE-AAC, and the | 837 | /// AAC stream in it (0-based among AAC streams only) carries HE-AAC, and the |
| 637 | /// core/SBR crossover an ffprobe/ASC inspection already pinned for it -- used | 838 | /// core/SBR crossover an ffprobe/ASC inspection already pinned for it -- used |
| @@ -1190,6 +1391,41 @@ fn sbr_real_library_matches_reference() { | |||
| 1190 | ); | 1391 | ); |
| 1191 | } | 1392 | } |
| 1192 | } | 1393 | } |
| 1394 | + if std::env::var("EC_AAC_SBR_HF_CONVICTION").is_ok() && n >= 4_096 { | ||
| 1395 | + let band_hz = f64::from(rate) / 256.0; | ||
| 1396 | + let kx = (c.crossover_hz / band_hz).round() as usize; | ||
| 1397 | + let win = &o[oa..oa + n]; | ||
| 1398 | + let twin = &t[ob..ob + n]; | ||
| 1399 | + println!( | ||
| 1400 | + " ch{ch} TASK1 bandpass-selectivity probe (band_hz, same-band corr, +offset corr, -offset corr):" | ||
| 1401 | + ); | ||
| 1402 | + for hf_band in [kx + 5, kx + 15, kx + 25] { | ||
| 1403 | + let (same, hi_c, lo_c) = bandpass_offset_probe(win, twin, rate, hf_band, 10); | ||
| 1404 | + println!( | ||
| 1405 | + " HF band{hf_band:>3} ({:.0}Hz) offset=10: same {same:.4} +10band {hi_c:.4} -10band {lo_c:.4}", | ||
| 1406 | + hf_band as f64 * band_hz | ||
| 1407 | + ); | ||
| 1408 | + } | ||
| 1409 | + if kx > 6 { | ||
| 1410 | + let lf_band = kx.saturating_sub(6); | ||
| 1411 | + let (same, hi_c, lo_c) = bandpass_offset_probe(win, twin, rate, lf_band, 3); | ||
| 1412 | + println!( | ||
| 1413 | + " LF CONTROL band{lf_band:>3} ({:.0}Hz) offset=3: same {same:.4} +3band {hi_c:.4} -3band {lo_c:.4}", | ||
| 1414 | + lf_band as f64 * band_hz | ||
| 1415 | + ); | ||
| 1416 | + } | ||
| 1417 | + println!( | ||
| 1418 | + " ch{ch} TASK2 bin-level conviction (band_hz, ours-energy, direct, mirror, shift+1band, shift-1band):" | ||
| 1419 | + ); | ||
| 1420 | + let min_band = (2_500.0 / band_hz).ceil() as usize; | ||
| 1421 | + for (hz, energy, direct, mirror, shift_up, shift_down) in | ||
| 1422 | + bin_level_conviction(win, twin, rate, min_band, 6) | ||
| 1423 | + { | ||
| 1424 | + println!( | ||
| 1425 | + " {hz:>6.0}Hz: energy {energy:.3e} direct {direct:.4} mirror {mirror:.4} shift+1 {shift_up:.4} shift-1 {shift_down:.4}" | ||
| 1426 | + ); | ||
| 1427 | + } | ||
| 1428 | + } | ||
| 1193 | println!( | 1429 | println!( |
| 1194 | " ch{ch}: lag {lag}, full {full:.6}, below {:.0}Hz {low:.6}, above {:.0}Hz {high:.6}", | 1430 | " ch{ch}: lag {lag}, full {full:.6}, below {:.0}Hz {low:.6}, above {:.0}Hz {high:.6}", |
| 1195 | c.crossover_hz, c.crossover_hz | 1431 | c.crossover_hz, c.crossover_hz |