You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
4.0 KiB
93 lines
4.0 KiB
#!/usr/bin/env python3 |
|
"""Check the C implementation against the format-v1 test vectors. |
|
|
|
Transmit side: c/cm_txvec rebuilds each vector's frame; every intermediate |
|
must match exactly and the waveform to within 1e-5 (sin/cos differ in the |
|
last bits between math libraries). Receive side (if c/cm_rxvec is built): the |
|
vector's waveform with noise added must decode to the vector's MAC frame. |
|
|
|
(cd c && make) && ./tools/make_vectors.py && ./tools/check_c.py |
|
""" |
|
import os, sys, json, subprocess, tempfile |
|
import numpy as np |
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__)) |
|
ROOT = os.path.join(HERE, "..") |
|
VEC = os.path.join(ROOT, "vectors") |
|
C = os.path.join(ROOT, "c") |
|
|
|
|
|
def tx_check(v, tmp): |
|
p = v["params"] |
|
out = os.path.join(tmp, v["name"] + ".cf32") |
|
cmd = [os.path.join(C, "cm_txvec"), str(p["sf"]), repr(p["bw"]), str(int(p["coherent"])), |
|
str(v["profile"]), str(int(v["encrypted"])), v["src"] or "-", v["dst"] or "-", |
|
v["body_hex"], out] |
|
r = subprocess.run(cmd, capture_output=True, text=True, check=True) |
|
got = dict(line.split(" ", 1) for line in r.stdout.splitlines() if " " in line) |
|
want_sym = " ".join(f"{i}:{q}" for i, q in v["header_symbols"] + v["payload_symbols"]) |
|
checks = [ |
|
("MAC frame", got["mac_frame_hex"], v["mac_frame_hex"]), |
|
("header word", got["header_hex"], v["header_hex"]), |
|
("header symbols", got["n_hdr_sym"], str(v["n_hdr_sym"])), |
|
("payload symbols", got["n_pay_sym"], str(v["n_pay_sym"])), |
|
("segments", got["segments"].split(), [str(s) for s in v["segments"]]), |
|
("header bits", got["header_bits"], v["header_bits"]), |
|
("payload bits", got["payload_bits"], v["payload_bits"]), |
|
("symbols", got["symbols"].strip(), want_sym), |
|
] |
|
bad = [name for name, a, b in checks if a != b] |
|
w = np.fromfile(out, np.float32) |
|
ref = np.fromfile(os.path.join(VEC, v["waveform"]["file"]), np.float32) |
|
err = float(np.max(np.abs(w - ref))) if len(w) == len(ref) else np.inf |
|
if err > 1e-5: |
|
bad.append(f"waveform (max error {err:.2e})") |
|
return bad, err |
|
|
|
|
|
def rx_check(v, tmp, snr_db=-3.0): |
|
"""The vector's waveform, delayed and in noise, must decode to its MAC frame.""" |
|
p = v["params"] |
|
w = np.fromfile(os.path.join(VEC, v["waveform"]["file"]), np.float32) |
|
x = w[0::2] + 1j * w[1::2] |
|
rng = np.random.default_rng(len(x)) |
|
pad = 4000 + int(rng.integers(0, 2000)) |
|
y = np.concatenate([np.zeros(pad), x, np.zeros(3000)]) |
|
sig2 = p["osr"] / 10 ** (snr_db / 10) |
|
y = y + np.sqrt(sig2 / 2) * (rng.standard_normal(len(y)) + 1j * rng.standard_normal(len(y))) |
|
f = os.path.join(tmp, v["name"] + "_rx.cf32") |
|
y.astype(np.complex64).tofile(f) |
|
r = subprocess.run([os.path.join(C, "cm_rxvec"), str(p["sf"]), repr(p["bw"]), "2", f], |
|
capture_output=True, text=True, check=True).stdout.split() |
|
fields = dict(kv.split("=", 1) for kv in r[2:]) |
|
bad = [] |
|
if r[1] != "ok": |
|
bad.append(f"stage {r[1]}") |
|
elif fields["mac"] != v["mac_frame_hex"]: |
|
bad.append("MAC frame differs") |
|
return bad, snr_db |
|
|
|
|
|
def main(): |
|
if not os.path.exists(os.path.join(C, "cm_txvec")): |
|
sys.exit("build first: (cd c && make)") |
|
names = json.load(open(os.path.join(VEC, "index.json"))) |
|
fails = 0 |
|
with tempfile.TemporaryDirectory() as tmp: |
|
for n in names: |
|
v = json.load(open(os.path.join(VEC, n + ".json"))) |
|
bad, err = tx_check(v, tmp) |
|
print(f"{'PASS' if not bad else 'FAIL'} tx {n}" + |
|
(f" (waveform max error {err:.1e})" if not bad else ": " + ", ".join(bad))) |
|
fails += bool(bad) |
|
if os.path.exists(os.path.join(C, "cm_rxvec")): |
|
bad, snr = rx_check(v, tmp) |
|
print(f"{'PASS' if not bad else 'FAIL'} rx {n} (in noise at {snr:+g} dB)" + |
|
("" if not bad else ": " + ", ".join(bad))) |
|
fails += bool(bad) |
|
print("all passed" if not fails else f"{fails} failed") |
|
sys.exit(1 if fails else 0) |
|
|
|
|
|
if __name__ == "__main__": |
|
main()
|
|
|