"""Verify: preamble[3:7] = Tran[0], Tran[1] as int16 BE in 16-count units. And first 20/10 NN block = Tran deltas starting at sample 2. """ import os, sys sys.path.insert(0, ".") from analysis.load_bundle import _parse_txt from minimateplus.waveform_codec import walk_body, find_data_start def s4(n): return n if n < 8 else n - 16 def i8(b): return b if b < 128 else b - 256 def main(): for stem in ("M529LL1A.SP0", "M529LL1A.SS0", "M529LL1A.SV0"): path = f"decode-re/5-11-26/{stem}" with open(path, "rb") as f: raw = f.read() body = raw[43:-26] _, samples = _parse_txt(path + ".TXT") truth_T_16 = [round(v * 200) for v in samples["Tran"]] # Preamble parse T0_pre = int.from_bytes(body[3:5], "big", signed=True) T1_pre = int.from_bytes(body[5:7], "big", signed=True) print(f"\n=== {stem} ===") print(f" Preamble T[0]={T0_pre} (truth {truth_T_16[0]}) T[1]={T1_pre} (truth {truth_T_16[1]}) match={T0_pre==truth_T_16[0] and T1_pre==truth_T_16[1]}") # First block start = find_data_start(body) blocks = walk_body(body, start) if not blocks: print(f" no blocks found") continue # Assume first block = Tran deltas from sample 2 first = blocks[0] T = [T0_pre, T1_pre] cur_T = T1_pre if first.tag_hi == 0x10: # Nibble pairs for byte in first.data: for nib in ((byte >> 4) & 0xF, byte & 0xF): cur_T += s4(nib) T.append(cur_T) elif first.tag_hi == 0x20: # int8 per byte for byte in first.data: cur_T += i8(byte) T.append(cur_T) # Compare against truth n_check = min(len(T), len(truth_T_16)) match_count = sum(1 for i in range(n_check) if T[i] == truth_T_16[i]) print(f" First block type=0x{first.tag_hi:02x} NN=0x{first.tag_lo:02x} len={len(first.data)} → {len(T)} T samples decoded") print(f" Tran predicted[0:10]: {T[:10]}") print(f" Tran truth [0:10]: {truth_T_16[:10]}") print(f" Matches in first {n_check}: {match_count} / {n_check}") # Show where it diverges for i in range(n_check): if T[i] != truth_T_16[i]: print(f" First divergence: sample {i}: pred={T[i]}, truth={truth_T_16[i]}") break if __name__ == "__main__": main()