Per-QMF-band correlation diagnostic for the SBR HF region
Browse files at this commit12a36736e4de41ac3ade76ccd3089c2357cbc32eParent: e0b5247- test
- per-QMF-band correlation diagnostic for the SBR HF region
1 changed file +93 -0
crates/ec-aac/tests/sbr_real_library.rsmodified+93 -0
| @@ -374,6 +374,92 @@ fn windowed_lag_drift( | ||
| 374 | 374 | out |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | +/// DIAGNOSTIC (round-14, Task 2): per-QMF-band correlation over the | |
| 378 | +/// above-crossover region. No 64-band QMF Analysis exists standalone in this | |
| 379 | +/// crate, so this substitutes an STFT: magnitude-per-bin time series (one | |
| 380 | +/// value per hop) for `ours` and `theirs`, correlated bin by bin, then | |
| 381 | +/// averaged over the bins a QMF band of width `rate/128` Hz covers. A patch | |
| 382 | +/// boundary in `build_patches`' target ranges landing on a low-correlation | |
| 383 | +/// block convicts patch construction; an even spread across all HF bands | |
| 384 | +/// with the map otherwise right points at gain mapping instead. | |
| 385 | +/// Pearson correlation with no minimum-length floor -- `correlation`'s | |
| 386 | +/// 1024-sample floor exists for raw audio windows, not the much shorter | |
| 387 | +/// per-bin magnitude series this diagnostic correlates. | |
| 388 | +fn magnitude_series_correlation(a: &[f32], b: &[f32]) -> f64 { | |
| 389 | + let n = a.len().min(b.len()); | |
| 390 | + if n < 2 { | |
| 391 | + return 0.0; | |
| 392 | + } | |
| 393 | + let (a, b) = (&a[..n], &b[..n]); | |
| 394 | + let ma = a.iter().map(|v| f64::from(*v)).sum::<f64>() / n as f64; | |
| 395 | + let mb = b.iter().map(|v| f64::from(*v)).sum::<f64>() / n as f64; | |
| 396 | + let ac: Vec<f32> = a.iter().map(|v| (f64::from(*v) - ma) as f32).collect(); | |
| 397 | + let bc: Vec<f32> = b.iter().map(|v| (f64::from(*v) - mb) as f32).collect(); | |
| 398 | + let num = dot(&ac, &bc); | |
| 399 | + let den = (dot(&ac, &ac) * dot(&bc, &bc)).sqrt(); | |
| 400 | + if den == 0.0 { 0.0 } else { num / den } | |
| 401 | +} | |
| 402 | + | |
| 403 | +fn per_band_correlation(o: &[f32], t: &[f32], rate: u32) -> Vec<(f64, f64, f64)> { | |
| 404 | + const FFT_LEN: usize = 2048; | |
| 405 | + const HOP: usize = 1024; | |
| 406 | + let win = ec_dsp::Window::<f32>::sine(FFT_LEN); | |
| 407 | + let mut rfft = ec_dsp::RealFft::<f32>::new(FFT_LEN); | |
| 408 | + let bins = rfft.spectrum_len(); | |
| 409 | + let n = o.len().min(t.len()); | |
| 410 | + let hops = if n >= FFT_LEN { | |
| 411 | + (n - FFT_LEN) / HOP + 1 | |
| 412 | + } else { | |
| 413 | + 0 | |
| 414 | + }; | |
| 415 | + let mut o_mag = vec![Vec::with_capacity(hops); bins]; | |
| 416 | + let mut t_mag = vec![Vec::with_capacity(hops); bins]; | |
| 417 | + let mut spectrum = vec![ec_dsp::Complex::new(0.0f32, 0.0); bins]; | |
| 418 | + for (src, dst) in [(o, &mut o_mag), (t, &mut t_mag)] { | |
| 419 | + for h in 0..hops { | |
| 420 | + let mut block = src[h * HOP..h * HOP + FFT_LEN].to_vec(); | |
| 421 | + win.apply(&mut block); | |
| 422 | + rfft.forward(&block, &mut spectrum); | |
| 423 | + for (b, c) in spectrum.iter().enumerate() { | |
| 424 | + dst[b].push(c.norm_sqr().sqrt()); | |
| 425 | + } | |
| 426 | + } | |
| 427 | + } | |
| 428 | + // QMF band width is `core_rate/128` where `core_rate` is the core | |
| 429 | + // decoder's own sample rate -- half the SBR extension/output rate for a | |
| 430 | + // plain (2x) HE-AAC stream -- not `output_rate/128`: measured against | |
| 431 | + // this file's own kx=14 crossover, the correlation break lands exactly | |
| 432 | + // at 14*(rate/256) Hz (2412 Hz), not at 14*(rate/128). | |
| 433 | + let band_hz = f64::from(rate) / 256.0; | |
| 434 | + let bin_hz = f64::from(rate) / FFT_LEN as f64; | |
| 435 | + let num_bands = (f64::from(rate) / 2.0 / band_hz).ceil() as usize; | |
| 436 | + let mut out = Vec::with_capacity(num_bands); | |
| 437 | + for band in 0..num_bands { | |
| 438 | + let lo = (band as f64 * band_hz / bin_hz).floor() as usize; | |
| 439 | + let hi = (((band + 1) as f64 * band_hz / bin_hz).ceil() as usize).min(bins); | |
| 440 | + if lo >= hi { | |
| 441 | + continue; | |
| 442 | + } | |
| 443 | + let mut sum = 0.0f64; | |
| 444 | + let mut cnt = 0usize; | |
| 445 | + for b in lo..hi { | |
| 446 | + // `correlation`'s 1024-sample floor is sized for raw audio, not | |
| 447 | + // this per-bin magnitude series (one value per STFT hop, ~200 | |
| 448 | + // long over the test window) -- use it anyway but on padded | |
| 449 | + // input so the floor doesn't zero every band out. | |
| 450 | + let c = magnitude_series_correlation(&o_mag[b], &t_mag[b]); | |
| 451 | + if c.is_finite() { | |
| 452 | + sum += c.abs(); | |
| 453 | + cnt += 1; | |
| 454 | + } | |
| 455 | + } | |
| 456 | + if cnt > 0 { | |
| 457 | + out.push((band as f64 * band_hz, sum / cnt as f64, band as f64)); | |
| 458 | + } | |
| 459 | + } | |
| 460 | + out | |
| 461 | +} | |
| 462 | + | |
| 377 | 463 | /// A file discovered under the user's own media directories: its path, which |
| 378 | 464 | /// AAC stream in it (0-based among AAC streams only) carries HE-AAC, and the |
| 379 | 465 | /// core/SBR crossover an ffprobe/ASC inspection already pinned for it -- used |
| @@ -533,6 +619,13 @@ fn sbr_real_library_matches_reference() { | ||
| 533 | 619 | } else { |
| 534 | 620 | 0.0 |
| 535 | 621 | }; |
| 622 | + if std::env::var("EC_AAC_SBR_BANDS").is_ok() && n >= 4_096 { | |
| 623 | + let spectrum = per_band_correlation(&o[oa..oa + n], &t[ob..ob + n], rate); | |
| 624 | + println!(" ch{ch} per-QMF-band |corr| (band_hz, |corr|, band_index):"); | |
| 625 | + for (hz, corr, band) in &spectrum { | |
| 626 | + println!(" {hz:>6.0}Hz band{band:>3.0}: {corr:.4}"); | |
| 627 | + } | |
| 628 | + } | |
| 536 | 629 | println!( |
| 537 | 630 | " ch{ch}: lag {lag}, full {full:.6}, below {:.0}Hz {low:.6}, above {:.0}Hz {high:.6}", |
| 538 | 631 | c.crossover_hz, c.crossover_hz |