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.
 
 
 
 
 

74 lines
3.3 KiB

#!/usr/bin/env python3
"""Run the Python and C receivers on the same simulated frames and compare
delivery, agreement and speed.
./tools/compare_rx.py --sf 9 --snr -16 --trials 100 [--noncoherent] [--fading rayleigh --fd 20]
"""
import os, sys, argparse, subprocess, tempfile, time
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, ".."))
import numpy as np
from phy import PhyParams
from channel import apply_channel
from frame import build_frame, receive_frame, LICENSED, MacFrame
import sim_frame
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--sf", type=int, default=9)
ap.add_argument("--bw", type=float, default=125e3)
ap.add_argument("--snr", type=float, default=-16)
ap.add_argument("--trials", type=int, default=50)
ap.add_argument("--bytes", type=int, default=24)
ap.add_argument("--effort", type=int, default=2)
ap.add_argument("--noncoherent", action="store_true")
ap.add_argument("--fading", default="none")
ap.add_argument("--fd", type=float, default=0.0)
ap.add_argument("--drift", type=float, default=0.0)
ap.add_argument("--seed", type=int, default=0)
a = ap.parse_args()
p = PhyParams(sf=a.sf, bw=a.bw)
chan = dict(fading=a.fading, fd_hz=a.fd, k_db=6.0, itf=[], sir_db=None, adc_bits=0,
lo_offset_hz=0.0, drift_hz_per_s=a.drift)
exe = os.path.join(HERE, "..", "c", "cm_rxvec")
py_ok = c_ok = agree = 0
t_py = t_c = air = 0.0
with tempfile.TemporaryDirectory() as tmp:
files, truth = [], []
for i, s in enumerate(np.random.SeedSequence(a.seed).spawn(a.trials)):
rng = np.random.default_rng(s)
body = rng.bytes(a.bytes)
iq, _ = build_frame(p, body, profile=LICENSED, src="W1AW", coherent=not a.noncoherent)
y, _ = apply_channel(iq, p, sim_frame.impaired_cfg(rng, p, a.snr, False, chan), rng)
y = y.astype(np.complex64).astype(np.complex128) # both see the same float32 samples
air += len(iq) / p.fs
mac = MacFrame(body=body, src="W1AW").pack()
t0 = time.perf_counter()
res = receive_frame(y, p, effort=a.effort, excise=False)
t_py += time.perf_counter() - t0
pok = res.stage == "ok" and res.mac.body == body
f = os.path.join(tmp, f"t{i}.cf32")
y.astype(np.complex64).tofile(f)
files.append(f)
truth.append((mac.hex(), pok))
out = subprocess.run([exe, str(a.sf), repr(a.bw), str(a.effort)] + files,
capture_output=True, text=True, check=True).stdout.splitlines()
for line, (mac, pok) in zip(out, truth):
fields = dict(kv.split("=", 1) for kv in line.split()[2:])
cok = line.split()[1] == "ok" and fields["mac"] == mac
t_c += float(fields["cpu"])
py_ok += pok
c_ok += cok
agree += pok == cok
n = a.trials
print(f"SF{a.sf} {a.snr:+g} dB {'non-coherent' if a.noncoherent else 'coherent'}, "
f"effort {a.effort}, {n} frames (no excision on either side)")
print(f" delivered: Python {py_ok / n:.0%}, C {c_ok / n:.0%}; same outcome {agree}/{n}")
print(f" speed (one core): Python {air / t_py:.1f}x real time, C {air / t_c:.1f}x "
f"({t_py / t_c:.0f}x faster)")
if __name__ == "__main__":
main()