forge
Explore
Log in

Don't you dare go Hollow

diag(aac-sbr): numpy round-trip of the normative 4.6.18.4.1/4.6.18.8.2 32/64-band QMF exhausts all listed carrier variants, dead end

Browse files at this commit

93d147f44bf9e11b6441c6be47e1bdce1394a768Ahmet Kaan GümüşParent: 2928bfe
scripts/aac-tables/qmf_check.py implements the analysis (32-band, ISO/IEC
14496-3 4.6.18.4.1) and synthesis (64-band, 4.6.18.8.2) equations exactly as
specified against the checksum-pinned 640-tap window, then round-trips 8192
white-noise samples through analysis -> zero-upper-32 -> synthesis and
correlates the decimated-by-2 output against the original at the best lag.

Impulse-response probe confirms the cascade is not garbage (a clean
symmetric twin-peak decaying kernel centred near the expected group delay),
but the broadband round-trip correlation tops out at 0.624 (newest-sample-
at-x[0], Z[n]=x[n]*c[2n], across both analysis and synthesis phase-sign
choices, Re-only vs Re+Im) -- far under the 0.999 gate. All four binary
alternatives from the charter (newest-sample position, c[2n] vs c[n],
analysis exp sign, synthesis Re-vs-both) were cross-producted (32 trials,
full table in run output); "scaling" is correlation-invariant so not a
separate axis. Output energy ratio is also off (~0.5x input energy where a
true PR interpolator should read ~2x), pointing at a real normalization/
equation bug beyond the four listed axes, not a coefficient tie-break.

Per the charter's gate ("only once numpy >=0.999: port to Rust"), this
round does not touch sbr_qmf.rs / sbr_hf.rs -- no Rust changes, no test
gate re-run. Recorded as a dead end for the next round.
scripts/aac-tables/qmf_check.pyadded+137 -0
@@ -0,0 +1,137 @@
1+#!/usr/bin/env python3
2+"""Numpy prototype of the ISO/IEC 14496-3 4.6.18.4.1 (32-band analysis) and
3+4.6.18.8.2 (64-band synthesis) QMF banks, run back-to-back as a passthrough
4+round-trip (upper 32 synthesis subbands zeroed) to verify the exact carrier
5+equations before porting to Rust. See ledger sbr-hunt-resume for context.
6+
7+Usage: python3 qmf_check.py
8+"""
9+import numpy as np
10+
11+TAPS_PATH = "/tmp/claude-1000/-home-user-Documents-Code-Rust-edith-codecs/041875c4-7950-4e24-950e-7b6e9eb5ba3c/scratchpad/qmf_proto_640.txt"
12+c = np.array([float(x) for x in open(TAPS_PATH)])
13+assert c.shape == (640,)
14+
15+
16+class Analysis32:
17+ """4.6.18.4.1: 32 new real samples in -> 32 complex subbands out per slot."""
18+
19+ def __init__(self, window):
20+ self.c = window
21+ self.x = np.zeros(320) # analysis history buffer, x[0]=newest
22+
23+ def process_slot(self, new32, newest_at_zero=True, tap=None, sign=-1.0):
24+ x = self.x
25+ # shift: x[n] = x[n-32] for n=319..32 ; new samples into x[31..0]
26+ x[32:320] = x[0:288]
27+ if newest_at_zero:
28+ x[0:32] = new32[::-1] # newest at x[0]
29+ else:
30+ x[0:32] = new32 # oldest-at-front variant
31+ if tap == "c2n":
32+ Z = x * self.c[0::2][: len(x)]
33+ else:
34+ Z = x * self.c[: len(x)]
35+ u = np.zeros(64)
36+ for n in range(64):
37+ u[n] = Z[n] + Z[n + 64] + Z[n + 128] + Z[n + 192] + Z[n + 256]
38+ k = np.arange(32)
39+ n = np.arange(64)
40+ # X[k] = sum_n u[n] * exp(sign * j*pi/64*(k+0.5)*(2n-1))
41+ phase = sign * 1j * np.pi / 64.0 * np.outer(2 * n - 1, k + 0.5)
42+ X = u @ np.exp(phase)
43+ self.x = x
44+ return X # shape (32,) complex
45+
46+
47+class Synthesis64:
48+ """4.6.18.8.2: 64 complex subbands in -> 64 real samples out per slot."""
49+
50+ def __init__(self, window, sign=1.0, use_re=True):
51+ self.c = window
52+ self.v = np.zeros(1280)
53+ self.sign = sign
54+ self.use_re = use_re
55+
56+ def process_slot(self, X64):
57+ v = self.v
58+ v[128:1280] = v[0:1152]
59+ k = np.arange(64)
60+ n = np.arange(128)
61+ phase = self.sign * 1j * np.pi / 128.0 * np.outer(2 * n - 255, k + 0.5)
62+ vv = (1.0 / 64.0) * (np.exp(phase) @ X64)
63+ v[0:128] = vv.real if self.use_re else (vv.real + vv.imag)
64+ self.v = v
65+ g = np.zeros(640)
66+ for i in range(5):
67+ g[128 * i : 128 * i + 64] = v[256 * i : 256 * i + 64]
68+ g[128 * i + 64 : 128 * i + 128] = v[256 * i + 192 : 256 * i + 256]
69+ w = g * self.c
70+ y = np.zeros(64)
71+ for k_ in range(10):
72+ y += w[64 * k_ : 64 * k_ + 64]
73+ return y
74+
75+
76+def run_trial(name, syn_sign=1.0, syn_use_re=True, **kw):
77+ rng = np.random.default_rng(0)
78+ n_slots = 128 # 8192 samples / 64
79+ x_in = rng.standard_normal(8192)
80+ ana = Analysis32(c)
81+ syn = Synthesis64(c, sign=syn_sign, use_re=syn_use_re)
82+ out = np.zeros(8192 * 2)
83+ for s in range(n_slots):
84+ new32 = x_in[s * 32 : s * 32 + 32]
85+ X32 = ana.process_slot(new32, **{k: v for k, v in kw.items() if k in ("newest_at_zero", "tap", "sign")})
86+ X64 = np.zeros(64, dtype=complex)
87+ X64[0:32] = X32
88+ y64 = syn.process_slot(X64)
89+ out[s * 64 : s * 64 + 64] = y64
90+
91+ best_corr = -2.0
92+ best_lag = None
93+ best_ratio = None
94+ for lag in range(0, 800):
95+ a = out[lag : lag + 8192]
96+ b = x_in[: len(a)]
97+ if len(a) < 4000:
98+ continue
99+ # correlate against decimated-out-at-even-samples vs input directly:
100+ oa = out[lag::2][: len(x_in)]
101+ n = min(len(oa), len(x_in)) - 10
102+ oa = oa[:n]
103+ bb = x_in[:n]
104+ if np.std(oa) < 1e-12:
105+ continue
106+ corr = np.corrcoef(oa, bb)[0, 1]
107+ if corr > best_corr:
108+ best_corr = corr
109+ best_lag = lag
110+ best_ratio = np.std(oa) / np.std(bb)
111+ pass # summarized in table below
112+ return best_corr
113+
114+
115+if __name__ == "__main__":
116+ results = {}
117+ for newest_at_zero in (True, False):
118+ for tap in (None, "c2n"):
119+ for sign in (-1.0, 1.0):
120+ for syn_sign in (-1.0, 1.0):
121+ for syn_use_re in (True, False):
122+ name = (
123+ f"newest0={newest_at_zero} tap={tap} asign={sign:+.0f} "
124+ f"ssign={syn_sign:+.0f} re_only={syn_use_re}"
125+ )
126+ results[name] = run_trial(
127+ name,
128+ newest_at_zero=newest_at_zero,
129+ tap=tap,
130+ sign=sign,
131+ syn_sign=syn_sign,
132+ syn_use_re=syn_use_re,
133+ )
134+ print()
135+ print("Trial table (sorted):")
136+ for k, v in sorted(results.items(), key=lambda kv: -kv[1]):
137+ print(f" {k}: corr={v:.6f}")