69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Phase B step 2: temporal detector-dynamics scan (offline).
|
|
|
|
Variants with time memory applied to FULL trajectory (state settles before
|
|
metric window), then validated pipeline (window, -6dB core, median/rms).
|
|
"""
|
|
import numpy as np
|
|
|
|
exec(open('/tmp/opencode/phaseA_grid_fast.py').read().split("# old-law reference gains")[0])
|
|
|
|
FULL = {}
|
|
for name, traj, dump, bm in [
|
|
('res_500','traj_res500.bin','dump_res_new.bin',85),
|
|
('al_12','traj_al12.bin','dump_t1k.bin',85),
|
|
('al_24','traj_al24.bin','dump_t1k.bin',85),
|
|
('t1k_1000','traj_t1k.bin','dump_t1k.bin',85)]:
|
|
FULL[name] = load_traj('/tmp/opencode/'+traj)
|
|
|
|
GO = {}
|
|
for name,(dB,W,bm,_) in DATA.items():
|
|
c_old = np.clip((dB+13.78)/82.07, 0, 1)**0.344*4.2
|
|
g = gains_batch(dB, W, bm, np.array([0.]), np.array([0.]), np.array([99.]), C_pre=c_old)
|
|
GO[name] = float(g[0].mean()) if name=='res_500' else float(np.median(g[0]))
|
|
|
|
def t_hold(T, b): # linear peak-hold decay
|
|
out = T.copy()
|
|
for t in range(1, len(T)):
|
|
out[t] = np.maximum(T[t], b*out[t-1])
|
|
return out
|
|
|
|
def t_dbdecay(T, r): # dB-domain peak decay r dB/frame
|
|
db = np.log10(np.maximum(T, 1e-12))*20.0
|
|
for t in range(1, len(db)):
|
|
db[t] = np.maximum(db[t], db[t-1]-r)
|
|
return 10**(db/20)
|
|
|
|
def t_ema(T, a): # EMA in dB domain
|
|
db = np.log10(np.maximum(T, 1e-12))*20.0
|
|
out = db.copy()
|
|
for t in range(1, len(db)):
|
|
out[t] = a*db[t] + (1-a)*out[t-1]
|
|
return 10**(out/20)
|
|
|
|
def eval_tf(fn):
|
|
out = {}
|
|
for name,(dB,W,bm,_) in DATA.items():
|
|
Tt = fn(FULL[name])[WIN[name][0]:WIN[name][1]]
|
|
dBp = np.log10(np.maximum(Tt, 1e-12))*20.0
|
|
if name != 'res_500':
|
|
lv = dBp[:, bm]; keep = dBp[lv >= lv.max()-6]
|
|
else:
|
|
keep = dBp
|
|
g = gains_batch(np.ascontiguousarray(keep), W, bm,
|
|
np.array([1.8]), np.array([0.11]), np.array([99.]))
|
|
agg = np.sqrt(np.mean(g**2, axis=1)) if name=='res_500' else np.median(g, axis=1)
|
|
out[name] = MEAS_OLD[name] + 20*np.log10(float(agg[0])/GO[name])
|
|
return out
|
|
|
|
VARS = [('none', lambda T: T)]
|
|
for b in [0.8, 0.9, 0.95, 0.99]: VARS.append((f'hold b={b}', lambda T, b=b: t_hold(T, b)))
|
|
for r in [0.25, 0.5, 1.0, 2.0]: VARS.append((f'dbdec r={r}', lambda T, r=r: t_dbdecay(T, r)))
|
|
for a in [0.3, 0.5, 0.7]: VARS.append((f'ema a={a}', lambda T, a=a: t_ema(T, a)))
|
|
|
|
print(f'{"variant":>12} ' + ' '.join(f'{n:>9}' for n in DATA) + ' rms')
|
|
for lbl, fn in VARS:
|
|
e = eval_tf(fn)
|
|
tot = np.sqrt(sum(v*v for v in e.values())/len(e))
|
|
print(f'{lbl:>12} ' + ' '.join(f'{v:+9.2f}' for v in e.values()) + f' {tot:.2f}')
|