diag(aac-sbr): bisect the 0.62 cross-family QMF corr with same-rate 64->64 and 32->32 round-trips; cross-checks T1/T2/T3 refuted
Browse files at this commit4de6175997511fb42f47e9c07cf682af771c126eParent: 93d147f1 changed file +253 -1
- scripts/aac-tables/qmf_check.py +253 -1
scripts/aac-tables/qmf_check.pymodified+253 -1
| @@ -112,6 +112,223 @@ def run_trial(name, syn_sign=1.0, syn_use_re=True, **kw): | |||
| 112 | return best_corr | 112 | return best_corr |
| 113 | 113 | ||
| 114 | 114 | ||
| 115 | +class Analysis64: | ||
| 116 | + """4.6.18.4: 64 new real samples in -> 64 complex subbands out per slot | ||
| 117 | + (same-rate variant, x of 640, full c[n] tap, folded to 128).""" | ||
| 118 | + | ||
| 119 | + def __init__(self, window): | ||
| 120 | + self.c = window | ||
| 121 | + self.x = np.zeros(640) # x[0]=newest | ||
| 122 | + | ||
| 123 | + def process_slot(self, new64): | ||
| 124 | + x = self.x | ||
| 125 | + x[64:640] = x[0:576] | ||
| 126 | + x[0:64] = new64[::-1] # newest at x[0] | ||
| 127 | + Z = x * self.c | ||
| 128 | + u = np.zeros(128) | ||
| 129 | + for n in range(128): | ||
| 130 | + u[n] = Z[n] + Z[n + 128] + Z[n + 256] + Z[n + 384] + Z[n + 512] | ||
| 131 | + k = np.arange(64) | ||
| 132 | + n = np.arange(128) | ||
| 133 | + phase = 1j * np.pi / 128.0 * np.outer(2 * n - 1, k + 0.5) | ||
| 134 | + X = u @ np.exp(phase) | ||
| 135 | + self.x = x | ||
| 136 | + return X # shape (64,) complex | ||
| 137 | + | ||
| 138 | + | ||
| 139 | +class Synthesis32Downsampled: | ||
| 140 | + """4.6.18.8.3: 32 complex subbands in -> 32 real samples out per slot | ||
| 141 | + (downsampled, same-rate variant).""" | ||
| 142 | + | ||
| 143 | + def __init__(self, window): | ||
| 144 | + self.c = window | ||
| 145 | + self.v = np.zeros(640) | ||
| 146 | + | ||
| 147 | + def process_slot(self, X32): | ||
| 148 | + v = self.v | ||
| 149 | + v[64:640] = v[0:576] | ||
| 150 | + k = np.arange(32) | ||
| 151 | + n = np.arange(64) | ||
| 152 | + phase = 1j * np.pi / 64.0 * np.outer(2 * n - 63, k + 0.5) | ||
| 153 | + vv = (1.0 / 32.0) * (np.exp(phase) @ X32) | ||
| 154 | + v[0:64] = vv.real | ||
| 155 | + self.v = v | ||
| 156 | + g = np.zeros(320) | ||
| 157 | + for i in range(5): | ||
| 158 | + g[64 * i : 64 * i + 32] = v[128 * i : 128 * i + 32] | ||
| 159 | + g[64 * i + 32 : 64 * i + 64] = v[128 * i + 96 : 128 * i + 128] | ||
| 160 | + w = g * self.c[0::2] | ||
| 161 | + y = np.zeros(32) | ||
| 162 | + for k_ in range(10): | ||
| 163 | + y += w[32 * k_ : 32 * k_ + 32] | ||
| 164 | + return y | ||
| 165 | + | ||
| 166 | + | ||
| 167 | +class Analysis64Param: | ||
| 168 | + """64-band analysis with the shift-direction (T1) and modulation-index | ||
| 169 | + (T2) axes exposed as parameters, for cross-check bisection.""" | ||
| 170 | + | ||
| 171 | + def __init__(self, window, shift_mode="reverse_newest_at_0", mod_sign=-1.0): | ||
| 172 | + self.c = window | ||
| 173 | + self.x = np.zeros(640) | ||
| 174 | + self.shift_mode = shift_mode | ||
| 175 | + self.mod_sign = mod_sign # -1.0 -> (2n-1), +1.0 -> (2n+1) | ||
| 176 | + | ||
| 177 | + def process_slot(self, new64): | ||
| 178 | + x = self.x | ||
| 179 | + if self.shift_mode == "reverse_newest_at_0": | ||
| 180 | + x[64:640] = x[0:576] | ||
| 181 | + x[0:64] = new64[::-1] | ||
| 182 | + else: # "forward_newest_at_top": x[n] <- x[n+64], new64 forward into x[576:640] | ||
| 183 | + x[0:576] = x[64:640] | ||
| 184 | + x[576:640] = new64 | ||
| 185 | + Z = x * self.c | ||
| 186 | + u = np.zeros(128) | ||
| 187 | + for n in range(128): | ||
| 188 | + u[n] = Z[n] + Z[n + 128] + Z[n + 256] + Z[n + 384] + Z[n + 512] | ||
| 189 | + k = np.arange(64) | ||
| 190 | + n = np.arange(128) | ||
| 191 | + expo = 2 * n + 1 if self.mod_sign > 0 else 2 * n - 1 | ||
| 192 | + phase = 1j * np.pi / 128.0 * np.outer(expo, k + 0.5) | ||
| 193 | + X = u @ np.exp(phase) | ||
| 194 | + self.x = x | ||
| 195 | + return X | ||
| 196 | + | ||
| 197 | + | ||
| 198 | +class Synthesis64Param: | ||
| 199 | + """64-band synthesis with shift-direction (T1), modulation offset (T2: | ||
| 200 | + 2n-255 vs 2n+1) and the 1/64 scale (T3) exposed as parameters.""" | ||
| 201 | + | ||
| 202 | + def __init__(self, window, shift_mode="reverse_newest_at_0", mod_variant="2n-255", scale=True): | ||
| 203 | + self.c = window | ||
| 204 | + self.v = np.zeros(1280) | ||
| 205 | + self.shift_mode = shift_mode | ||
| 206 | + self.mod_variant = mod_variant | ||
| 207 | + self.scale = scale | ||
| 208 | + | ||
| 209 | + def process_slot(self, X64): | ||
| 210 | + v = self.v | ||
| 211 | + k = np.arange(64) | ||
| 212 | + n = np.arange(128) | ||
| 213 | + expo = (2 * n + 1) if self.mod_variant == "2n+1" else (2 * n - 255) | ||
| 214 | + phase = 1j * np.pi / 128.0 * np.outer(expo, k + 0.5) | ||
| 215 | + scale_factor = (1.0 / 64.0) if self.scale else 1.0 | ||
| 216 | + vv = scale_factor * (np.exp(phase) @ X64) | ||
| 217 | + if self.shift_mode == "reverse_newest_at_0": | ||
| 218 | + v[128:1280] = v[0:1152] | ||
| 219 | + v[0:128] = vv.real | ||
| 220 | + else: # "forward_newest_at_top" | ||
| 221 | + v[0:1152] = v[128:1280] | ||
| 222 | + v[1152:1280] = vv.real | ||
| 223 | + self.v = v | ||
| 224 | + g = np.zeros(640) | ||
| 225 | + for i in range(5): | ||
| 226 | + g[128 * i : 128 * i + 64] = v[256 * i : 256 * i + 64] | ||
| 227 | + g[128 * i + 64 : 128 * i + 128] = v[256 * i + 192 : 256 * i + 256] | ||
| 228 | + w = g * self.c | ||
| 229 | + y = np.zeros(64) | ||
| 230 | + for k_ in range(10): | ||
| 231 | + y += w[64 * k_ : 64 * k_ + 64] | ||
| 232 | + return y | ||
| 233 | + | ||
| 234 | + | ||
| 235 | +def run_same_rate_64_param(shift_mode, mod_sign, mod_variant, scale, n_slots=160, lag_max=1300): | ||
| 236 | + rng = np.random.default_rng(0) | ||
| 237 | + x_in = rng.standard_normal(64 * n_slots) | ||
| 238 | + ana = Analysis64Param(c, shift_mode=shift_mode, mod_sign=mod_sign) | ||
| 239 | + syn = Synthesis64Param(c, shift_mode=shift_mode, mod_variant=mod_variant, scale=scale) | ||
| 240 | + out = np.zeros(64 * n_slots) | ||
| 241 | + for s in range(n_slots): | ||
| 242 | + X64 = ana.process_slot(x_in[64 * s : 64 * s + 64]) | ||
| 243 | + out[64 * s : 64 * s + 64] = syn.process_slot(X64) | ||
| 244 | + energy_ratio = np.sum(out**2) / np.sum(x_in**2) | ||
| 245 | + corr, lag, ratio = best_lag_corr(out, x_in, lag_max) | ||
| 246 | + return corr, lag, ratio, energy_ratio | ||
| 247 | + | ||
| 248 | + | ||
| 249 | +def bisect_t1_t2_t3(): | ||
| 250 | + print() | ||
| 251 | + print("T1/T2/T3 cross-check bisection (64->64, same family):") | ||
| 252 | + rows = [] | ||
| 253 | + for shift_mode in ("reverse_newest_at_0", "forward_newest_at_top"): | ||
| 254 | + for mod_sign in (-1.0, 1.0): # analysis (2n-1) vs (2n+1) | ||
| 255 | + for mod_variant in ("2n-255", "2n+1"): # synthesis | ||
| 256 | + for scale in (True, False): | ||
| 257 | + corr, lag, ratio, energy = run_same_rate_64_param(shift_mode, mod_sign, mod_variant, scale) | ||
| 258 | + name = f"shift={shift_mode} a_mod={'2n+1' if mod_sign>0 else '2n-1'} s_mod={mod_variant} scale64={scale}" | ||
| 259 | + rows.append((name, corr, lag, ratio, energy)) | ||
| 260 | + rows.sort(key=lambda r: -r[1]) | ||
| 261 | + for name, corr, lag, ratio, energy in rows: | ||
| 262 | + print(f" {name}: corr={corr:.6f} lag={lag} amp_ratio={ratio} energy_ratio={energy:.6f}") | ||
| 263 | + return rows | ||
| 264 | + | ||
| 265 | + | ||
| 266 | +def best_lag_corr(out, x_in, lag_max): | ||
| 267 | + """Same-rate (no decimation) lag search.""" | ||
| 268 | + best_corr, best_lag, best_ratio = -2.0, None, None | ||
| 269 | + for lag in range(0, lag_max): | ||
| 270 | + a = out[lag:] | ||
| 271 | + n = min(len(a), len(x_in)) - 10 | ||
| 272 | + if n < 4000: | ||
| 273 | + continue | ||
| 274 | + a, b = a[:n], x_in[:n] | ||
| 275 | + if np.std(a) < 1e-12: | ||
| 276 | + continue | ||
| 277 | + corr = np.corrcoef(a, b)[0, 1] | ||
| 278 | + if corr > best_corr: | ||
| 279 | + best_corr, best_lag, best_ratio = corr, lag, np.std(a) / np.std(b) | ||
| 280 | + return best_corr, best_lag, best_ratio | ||
| 281 | + | ||
| 282 | + | ||
| 283 | +def run_same_rate_64(n_slots=128): | ||
| 284 | + rng = np.random.default_rng(0) | ||
| 285 | + x_in = rng.standard_normal(64 * n_slots) | ||
| 286 | + ana = Analysis64(c) | ||
| 287 | + syn = Synthesis64(c, sign=1.0, use_re=True) | ||
| 288 | + out = np.zeros(64 * n_slots) | ||
| 289 | + for s in range(n_slots): | ||
| 290 | + X64 = ana.process_slot(x_in[64 * s : 64 * s + 64]) | ||
| 291 | + out[64 * s : 64 * s + 64] = syn.process_slot(X64) | ||
| 292 | + energy_ratio = np.sum(out**2) / np.sum(x_in**2) | ||
| 293 | + corr, lag, ratio = best_lag_corr(out, x_in, 900) | ||
| 294 | + print(f"(1) 64->64 same-family round-trip: corr={corr:.6f} lag={lag} amp_ratio={ratio} energy_ratio={energy_ratio:.6f}") | ||
| 295 | + return corr | ||
| 296 | + | ||
| 297 | + | ||
| 298 | +def run_same_rate_32(n_slots=256): | ||
| 299 | + rng = np.random.default_rng(0) | ||
| 300 | + x_in = rng.standard_normal(32 * n_slots) | ||
| 301 | + ana = Analysis32(c) | ||
| 302 | + syn = Synthesis32Downsampled(c) | ||
| 303 | + out = np.zeros(32 * n_slots) | ||
| 304 | + for s in range(n_slots): | ||
| 305 | + X32 = ana.process_slot(x_in[32 * s : 32 * s + 32], newest_at_zero=True, tap="c2n", sign=1.0) | ||
| 306 | + out[32 * s : 32 * s + 32] = syn.process_slot(X32) | ||
| 307 | + energy_ratio = np.sum(out**2) / np.sum(x_in**2) | ||
| 308 | + corr, lag, ratio = best_lag_corr(out, x_in, 700) | ||
| 309 | + print(f"(2) 32->32 same-family round-trip: corr={corr:.6f} lag={lag} amp_ratio={ratio} energy_ratio={energy_ratio:.6f}") | ||
| 310 | + return corr | ||
| 311 | + | ||
| 312 | + | ||
| 313 | +def run_single_band_impulse_spectrum(k_impulse=10): | ||
| 314 | + """(3)-fallback: single-band impulse at slot 0, k=k_impulse into | ||
| 315 | + Synthesis64; report where the output spectrum peaks land.""" | ||
| 316 | + syn = Synthesis64(c, sign=1.0, use_re=True) | ||
| 317 | + n_slots = 20 | ||
| 318 | + out = np.zeros(64 * n_slots) | ||
| 319 | + for s in range(n_slots): | ||
| 320 | + X64 = np.zeros(64, dtype=complex) | ||
| 321 | + if s == 0: | ||
| 322 | + X64[k_impulse] = 1.0 | ||
| 323 | + out[64 * s : 64 * s + 64] = syn.process_slot(X64) | ||
| 324 | + spec = np.fft.rfft(out) | ||
| 325 | + mags = np.abs(spec) | ||
| 326 | + top2 = np.argsort(mags)[-2:] | ||
| 327 | + freqs = np.fft.rfftfreq(len(out)) | ||
| 328 | + print(f"single-band impulse k={k_impulse}: top-2 bins {sorted(top2)} at normalized freq {sorted(freqs[top2])}") | ||
| 329 | + print(f" expected bin center (band k of 64, full band 0..0.5): {(k_impulse + 0.5) / 128.0:.5f}") | ||
| 330 | + | ||
| 331 | + | ||
| 115 | if __name__ == "__main__": | 332 | if __name__ == "__main__": |
| 116 | results = {} | 333 | results = {} |
| 117 | for newest_at_zero in (True, False): | 334 | for newest_at_zero in (True, False): |
| @@ -132,6 +349,41 @@ if __name__ == "__main__": | |||
| 132 | syn_use_re=syn_use_re, | 349 | syn_use_re=syn_use_re, |
| 133 | ) | 350 | ) |
| 134 | print() | 351 | print() |
| 135 | - print("Trial table (sorted):") | 352 | + print("Trial table (sorted, 32->64 cross-family):") |
| 136 | for k, v in sorted(results.items(), key=lambda kv: -kv[1]): | 353 | for k, v in sorted(results.items(), key=lambda kv: -kv[1]): |
| 137 | print(f" {k}: corr={v:.6f}") | 354 | print(f" {k}: corr={v:.6f}") |
| 355 | + | ||
| 356 | + print() | ||
| 357 | + print("Same-family bisection:") | ||
| 358 | + c1 = run_same_rate_64() | ||
| 359 | + c2 = run_same_rate_32() | ||
| 360 | + if c1 >= 0.999 and c2 >= 0.999: | ||
| 361 | + print("Both same-family round-trips pass -> the 32->64 cross-family pairing is the bug (see charter step 3).") | ||
| 362 | + if c1 < 0.999: | ||
| 363 | + run_single_band_impulse_spectrum(k_impulse=10) | ||
| 364 | + | ||
| 365 | + rows = bisect_t1_t2_t3() | ||
| 366 | + best_name, best_corr, best_lag, best_ratio, best_energy = rows[0] | ||
| 367 | + print() | ||
| 368 | + print(f"Best T1/T2/T3 configuration: {best_name} corr={best_corr:.6f} amp_ratio={best_ratio} energy_ratio={best_energy:.6f}") | ||
| 369 | + | ||
| 370 | + print() | ||
| 371 | + print("32-band-analysis -> confirmed-correct-64-band-synthesis cross-family, scale/conjugate probe:") | ||
| 372 | + for scale_k in (1.0, 2.0, 0.5, np.sqrt(2)): | ||
| 373 | + for conj in (False, True): | ||
| 374 | + rng = np.random.default_rng(0) | ||
| 375 | + n_slots = 160 | ||
| 376 | + x_in = rng.standard_normal(32 * n_slots) | ||
| 377 | + ana = Analysis32(c) | ||
| 378 | + syn = Synthesis64Param(c, shift_mode="reverse_newest_at_0", mod_variant="2n-255", scale=True) | ||
| 379 | + out = np.zeros(64 * n_slots) | ||
| 380 | + for s in range(n_slots): | ||
| 381 | + X32 = ana.process_slot(x_in[32 * s : 32 * s + 32], newest_at_zero=True, tap="c2n", sign=1.0) | ||
| 382 | + if conj: | ||
| 383 | + X32 = np.conj(X32) | ||
| 384 | + X64 = np.zeros(64, dtype=complex) | ||
| 385 | + X64[0:32] = X32 * scale_k | ||
| 386 | + out[64 * s : 64 * s + 64] = syn.process_slot(X64) | ||
| 387 | + energy = np.sum(out**2) / np.sum(x_in**2) | ||
| 388 | + corr, lag, ratio = best_lag_corr(out, x_in, 900) | ||
| 389 | + print(f" scale_k={scale_k:.4f} conj={conj}: corr={corr:.6f} lag={lag} amp_ratio={ratio} energy_ratio={energy:.6f}") | ||