#!/usr/bin/env python3 """Write format-v1 test vectors (vectors/*.json + *.cf32) for other implementations. Each vector gives a frame's inputs (PHY parameters, profile, callsigns, body) and every intermediate an implementation can be checked against, exactly: the packed MAC frame, the header word, the on-air bits of header and payload (modulator order, after interleaving and whitening), and each data symbol's (index, QPSK bits). The waveform at osr = 2 is in .cf32 (interleaved float32 I/Q, little-endian) and is to be matched to a tolerance (~1e-5), since sin/cos differ in the last bits between math libraries. ./tools/make_vectors.py # -> vectors/ """ import os, sys, json, hashlib HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "..")) import numpy as np from phy import PhyParams, bits_to_symbols, inv_gray, bits_per_symbol from frame import (build_frame, header_codec, Header, MacFrame, LICENSED, UNLICENSED, MODE_COHERENT, MODE_NONCOHERENT) from fec import SegmentedCodec CASES = [ dict(name="sf7_coherent_hello", sf=7, bw=125e3, coherent=True, profile=LICENSED, src="KD2HSG", dst=None, body=b"hello"), dict(name="sf9_noncoherent_cq", sf=9, bw=62.5e3, coherent=False, profile=LICENSED, src="KD2HSG", dst="W1AW", body=b"CQ CQ de KD2HSG"), dict(name="sf12_coherent_40B", sf=12, bw=62.5e3, coherent=True, profile=LICENSED, src="KD2HSG", dst=None, body=bytes(range(40))), dict(name="sf9_coherent_segmented_1200B", sf=9, bw=125e3, coherent=True, profile=LICENSED, src="KD2HSG", dst=None, body=bytes((i * 7 + 3) % 251 for i in range(1200))), dict(name="sf8_unlicensed_encrypted", sf=8, bw=250e3, coherent=True, profile=UNLICENSED, src=None, dst=None, body=bytes.fromhex("8f1e2d3c4b5a69788796a5b4c3d2e1f0"), encrypted=True), ] def bitstr(b): return "".join("1" if v else "0" for v in np.asarray(b, np.uint8)) def main(): out = os.path.join(HERE, "..", "vectors") os.makedirs(out, exist_ok=True) index = [] for c in CASES: p = PhyParams(sf=c["sf"], bw=c["bw"], osr=2) enc = c.get("encrypted", False) iq, meta = build_frame(p, c["body"], profile=c["profile"], src=c["src"], dst=c["dst"], encrypted=enc, coherent=c["coherent"]) mac = MacFrame(body=c["body"], src=c["src"], dst=c["dst"]).pack() hdr = Header(length=len(mac), profile=c["profile"], encrypted=enc, mode=MODE_COHERENT if c["coherent"] else MODE_NONCOHERENT).pack() hc = header_codec(p) pc = SegmentedCodec(len(mac), bits_per_symbol(p, c["coherent"])) hbits, pbits = hc.encode(hdr), pc.encode(mac) hi, ha = bits_to_symbols(hbits, p, coherent=False) pi, pa = bits_to_symbols(pbits, p, coherent=c["coherent"]) q = lambda a: ((a.real < 0).astype(int) * 2 + (a.imag < 0).astype(int)).tolist() wav = np.empty(2 * len(iq), np.float32) wav[0::2], wav[1::2] = iq.real, iq.imag wav.tofile(os.path.join(out, c["name"] + ".cf32")) v = dict( name=c["name"], format_version=1, params=dict(sf=p.sf, bw=p.bw, osr=p.osr, n_preamble=p.n_preamble, sync_word=p.sync_word, pilot_every=p.pilot_every, coherent=c["coherent"]), profile=c["profile"], src=c["src"], dst=c["dst"], encrypted=enc, body_hex=c["body"].hex(), mac_frame_hex=mac.hex(), header_hex=hdr.hex(), n_hdr_sym=hc.n_sym, n_pay_sym=pc.n_sym, segments=[s.n_bytes for s in pc.segs], header_bits=bitstr(hbits), payload_bits=bitstr(pbits), header_symbols=[[int(i), int(b)] for i, b in zip(hi, q(ha))], payload_symbols=[[int(i), int(b)] for i, b in zip(pi, q(pa))], waveform=dict(file=c["name"] + ".cf32", n_samples=len(iq), rms=float(np.sqrt(np.mean(np.abs(iq) ** 2))), sha256_float32=hashlib.sha256(wav.tobytes()).hexdigest())) with open(os.path.join(out, c["name"] + ".json"), "w") as f: json.dump(v, f, indent=1) index.append(c["name"]) print(f"{c['name']}: {hc.n_sym} + {pc.n_sym} symbols, {len(iq)} samples") with open(os.path.join(out, "index.json"), "w") as f: json.dump(index, f, indent=1) if __name__ == "__main__": main()