"""Deterministic chunk walker: each chunk = [10 NN][NN/2 bytes data][2 bytes trailer].""" import sys sys.path.insert(0, ".") from analysis.load_bundle import load_bundle def walk_chunks(body: bytes, start: int = 7): """Yield (offset, NN, data_bytes, trailer_bytes) tuples.""" i = start while i + 1 < len(body): if body[i] != 0x10: break NN = body[i + 1] if NN == 0 or NN > 0x80 or NN % 4 != 0: break chunk_len = NN // 2 + 4 if i + chunk_len > len(body): break data = bytes(body[i + 2 : i + 2 + NN // 2]) trailer = bytes(body[i + 2 + NN // 2 : i + chunk_len]) yield (i, NN, data, trailer) i += chunk_len def main(): for name in ("event-c", "event-d", "event-a", "event-b"): b = load_bundle(name) body = b.body chunks = list(walk_chunks(body)) print(f"\n=== {name} === body={len(body)} N_samples={len(b.samples['Tran'])}") print(f" chunks parsed: {len(chunks)}") if chunks: last = chunks[-1] end_of_walk = last[0] + last[1] // 2 + 4 print(f" walk ended at offset {end_of_walk} (= {len(body) - end_of_walk} bytes from end)") # Stats total_data_bytes = sum(len(c[2]) for c in chunks) print(f" total data bytes: {total_data_bytes}, total nibbles: {2*total_data_bytes}") if name in ("event-c", "event-d"): ratio = (2 * total_data_bytes) / (len(b.samples['Tran']) * 4) print(f" nibbles per (sample × channel): {ratio:.3f}") # Sum of trailer second-byte trailer_sums = [c[3][-1] if c[3] else None for c in chunks] print(f" first 10 chunks: {[(c[0], c[1], c[3].hex()) for c in chunks[:10]]}") # Print last 10 chunks (likely transition to trailer) print(f" last 10 chunks: {[(c[0], c[1], c[3].hex()) for c in chunks[-10:]]}") if __name__ == "__main__": main()