#!/usr/bin/env python3 """Phase B step 1: spatial max-pooling hypothesis scan (offline, no rebuilds). lvl'(t,k) = pool(lvl)(t,k) with width w, then law -> chain -> median/rms gain at metric bin. Variants: pool_am (pool raw am then /res), pool_lvl, pool_db. """ import re import numpy as np SRC = '/home/m/re-tools/dsp/rt_mask_tables.cpp' src = open(SRC).read() def tab(name): m = re.search(r'const double %s\[\] = \{(.*?)\};' % name, src, re.S) return np.array([float(x) for x in re.findall(r'[-+0-9.eE]+', m.group(1))]) A1, B1 = tab('kIIR_A1'), tab('kIIR_B1') A2, B2 = tab('kIIR_A2'), tab('kIIR_B2') A3, B3 = tab('kIIR_A3'), tab('kIIR_B3') DUMPDIR = {'dump_res_new.bin': '/tmp/', 'dump_t1k.bin': '/tmp/'} def load_dump(p): d = np.loadtxt(p, skiprows=1); return d[:, 1], d[:, 2], d[:, 6] # am,res,W def load_traj(p): b = open(p, 'rb').read(); off = 0; fr = [] while off < len(b): _, nb = np.frombuffer(b, dtype=np.int32, count=2, offset=off); off += 8 fr.append(np.frombuffer(b, dtype='18} {"w":>3} ' + ' '.join(f'{n:>9}' for n in CASES) + ' (pred err, dB)') # baselines on CORRECT lvl (traj stores lvl_raw already) GBASE = {} for name,(T,res_k,W,bm) in CASES.items(): dB = np.log10(np.maximum(T, 1e-12)) GBASE[name] = agg(gains_batch(OLD(dB)[None], W, bm)[0], name) print('sanity new@w=1 (vs validated):') row=[] for name,(T,res_k,W,bm) in CASES.items(): dB = np.log10(np.maximum(T, 1e-12)) gn = agg(gains_batch(LAW['new'](dB)[None], W, bm)[0], name) row.append(MEAS_OLD[name] + 20*np.log10(gn/GBASE[name])) print(f'{"new":>18} {1:>3} ' + ' '.join(f'{v:+9.2f}' for v in row)) for variant in ['pool_lvl', 'pool_am', 'pool_db']: for w in [3, 5, 9, 17]: row = [] for name,(T,res_k,W,bm) in CASES.items(): am = T # stored lvl_raw = am/res*scale -> recover am = lvl/res*scale... careful # stored lvl_raw = am/res_k * SCALE => am = lvl_raw * res_k / SCALE am_abs = T * res_k[None,:] / SCALE if variant == 'pool_am': lv = slide_max(am_abs[None], w)[0] / res_k[None,:] * SCALE elif variant == 'pool_lvl': lv = slide_max(T[None], w)[0] else: db_ = np.log10(np.maximum(T, 1e-12))*20 lv = 10**(slide_max(db_[None], w)[0]/20) dB = np.log10(np.maximum(lv, 1e-12))*20 g = gains_batch(LAW['new'](dB)[None], W, bm) row.append(MEAS_OLD[name] + 20*np.log10(agg(g[0],name)/GBASE[name])) print(f'{variant:>18} {w:>3} ' + ' '.join(f'{v:+9.2f}' for v in row))