#!/usr/bin/env python3 """Find the decoder's lower limit on real hardware: Pluto -> attenuator -> RTL-SDR. Transmits a looping recording from one Pluto session and steps its TX gain down (the AD936x attenuator is accurate in 0.25 dB steps), capturing and decoding at each step. Stops after two steps in a row decode nothing. ./sweep_test.py # SF9: -30 dB down in 3 dB steps ./sweep_test.py --sf 12 --seconds 25 --start -40 --step 2 ./sweep_test.py --tx-gains -50 -55 -58 -60 -62 -64 --rx-gains 20 30 40 Per step it reports frames decoded vs. expected, and the median SNR the receiver estimates. At the end it fits SNR = TX gain + c over the steps whose SNR is below +10 dB (where the estimate should be linear), uses the fit to put an SNR on every step, and compares the measured threshold with the simulator's. Results also go to _sweep.ods (or .csv without odfpy). """ import argparse, csv, os, shutil, subprocess, sys, time import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import iq_io from rx_file import decode_capture from phy import PhyParams from frame import resolve_mode # simulated 10%-frame-loss SNR (sim_frame.py, all impairments on, 400-2000 trials per point on # the 5900X), dB in the signal bandwidth. Keys (sf, bw, coherent). At 125 kHz: a 24 B body; # at 62.5 kHz: the sweep's own frame (15 B coherent / 14 B non-coherent) with 0.5 Hz/s drift. SIM_THRESHOLD = { (7, 125e3, True): -11.5, (7, 125e3, False): -11.3, (9, 125e3, True): -17.1, (9, 125e3, False): -17.1, (12, 125e3, True): -25.3, (12, 125e3, False): -25.1, (12, 62.5e3, True): -22.5, (12, 62.5e3, False): -25.0, } def write_table(rows, base): cols = list(rows[0].keys()) try: from odf.opendocument import OpenDocumentSpreadsheet from odf.table import Table, TableRow, TableCell from odf.text import P doc = OpenDocumentSpreadsheet() t = Table(name="sweep") def row(vals): tr = TableRow() for v in vals: if isinstance(v, (int, float)) and v is not None and np.isfinite(v): c = TableCell(valuetype="float", value=float(v)) else: c = TableCell(valuetype="string") c.addElement(P(text="" if v is None else str(v))) tr.addElement(c) t.addElement(tr) row(cols) for r in rows: row([r[c] for c in cols]) doc.spreadsheet.addElement(t) doc.save(base + ".ods") return base + ".ods" except ImportError: with open(base + ".csv", "w", newline="") as f: w = csv.DictWriter(f, fieldnames=cols) w.writeheader() w.writerows(rows) return base + ".csv" def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--src", default="KD2HSG") ap.add_argument("--sf", type=int, default=9) ap.add_argument("--bw", type=float, default=62.5e3) ap.add_argument("--freq", type=float, default=223.58e6) ap.add_argument("--repeat", type=int, default=5) ap.add_argument("--gap", type=float, default=0.4) ap.add_argument("--start", type=float, default=-30.0, help="first TX gain, dB") ap.add_argument("--stop", type=float, default=-89.75, help="last TX gain, dB") ap.add_argument("--step", type=float, default=3.0, help="TX gain step, dB") ap.add_argument("--tx-gains", type=float, nargs="+", help="explicit TX gains (overrides start/stop/step)") ap.add_argument("--rx-gains", type=float, nargs="+", default=[30.0], help="RTL gains to sweep") ap.add_argument("--seconds", type=float, default=15.0, help="capture per step") ap.add_argument("--tx-fs", type=float, default=1e6) ap.add_argument("--tx-if", type=float, default=100e3) ap.add_argument("--rx-fs", type=float, default=2.4e6) ap.add_argument("--rx-if", type=float, default=250e3) ap.add_argument("--uri", default="ip:192.168.2.1") ap.add_argument("--rtl-device", default="0") ap.add_argument("--name", default="sweep") ap.add_argument("--keep", action="store_true", help="keep each step's capture") ap.add_argument("--coherent", action="store_true", help="coherent payload (index + QPSK); default only at SF 7-9 above 62.5 kHz") ap.add_argument("--noncoherent", action="store_true", help="index-only payload; default at SF >= 10 and at <= 62.5 kHz") a = ap.parse_args() if not shutil.which("rtl_sdr"): sys.exit("rtl_sdr not found on PATH") gains = a.tx_gains or list(np.arange(a.start, a.stop - 1e-9, -abs(a.step))) if min(gains) < -89.75 or max(gains) > 0: sys.exit("Pluto TX gain must be within -89.75..0 dB") rec, rx_lo = f"{a.name}_tx", a.freq - a.rx_if # 1. recording def make(repeat): subprocess.run([sys.executable, os.path.join(HERE, "tx_file.py"), rec, "--src", a.src, "--msg", f"sweep de {a.src}", "--sf", str(a.sf), "--bw", repr(a.bw), "--fs", repr(a.tx_fs), "--freq", repr(a.freq), "--if-offset", repr(a.tx_if), "--repeat", str(repeat), "--gap", repr(a.gap)] + (["--noncoherent"] if a.noncoherent else []) \ + (["--coherent"] if a.coherent else []), check=True) make(a.repeat) n = iq_io.recording_samples(rec) if n > iq_io.PLUTO_MAX_SAMPLES and a.repeat > 1: # Pluto TX buffer limit rep = max(1, int(a.repeat * iq_io.PLUTO_MAX_SAMPLES / n)) print(f"recording too large for the Pluto's buffer ({n / 1e6:.1f} M samples); " f"using {rep} frame(s) per loop") make(rep) if iq_io.recording_samples(rec) > iq_io.PLUTO_MAX_SAMPLES: sys.exit("even one frame exceeds the Pluto's buffer; lower --tx-fs") m = iq_io.read_sigmf_meta(rec) x = iq_io.read_raw(m["data_path"], m["fmt"]) anns = m["meta"]["annotations"] rec_s = len(x) / a.tx_fs frame_s = anns[0]["core:sample_count"] / a.tx_fs expect = max(0.0, (a.seconds - frame_s) * len(anns) / rec_s) # mean complete frames per capture # 2. one Pluto session for the whole sweep try: import adi except ImportError: sys.exit("needs pyadi-iio") sdr = adi.Pluto(a.uri) sdr.sample_rate = int(a.tx_fs) sdr.tx_rf_bandwidth = int(min(a.tx_fs, 2 * (abs(a.tx_if) + a.bw))) sdr.tx_lo = int(round(a.freq - a.tx_if)) sdr.tx_hardwaregain_chan0 = float(gains[0]) sdr.tx_cyclic_buffer = True sdr.tx((x * 2 ** 14).astype(np.complex64)) print(f"Pluto transmitting {rec} in a loop ({rec_s:.2f} s, {len(anns)} frames of " f"{frame_s:.3f} s); ~{expect:.1f} complete frames per {a.seconds:g} s capture") rows = [] try: for rg in a.rx_gains: misses = 0 print(f"\nRTL gain {rg:g} dB\n{'TX gain':>8} {'decoded':>8} {'expected':>9} " f"{'delivered':>10} {'SNR med':>8} {'offset Hz':>10} {'ADC rms':>8}") for g in gains: sdr.tx_hardwaregain_chan0 = float(g) time.sleep(0.5) cap = f"{a.name}_{rg:g}_{g:g}.cu8" subprocess.run(["rtl_sdr", "-d", a.rtl_device, "-f", str(int(round(rx_lo))), "-s", str(int(a.rx_fs)), "-g", repr(rg), "-n", str(int(a.seconds * a.rx_fs)), cap], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) r = decode_capture(cap, "cu8", a.rx_fs, rx_lo, a.freq, a.sf, a.bw) if not a.keep: os.remove(cap) snrs = [f["snr_db"] for f in r["frames"]] n = len(snrs) row = dict(rx_gain_db=rg, tx_gain_db=float(g), decoded=n, expected=round(expect, 1), delivered=round(min(1.0, n / expect), 3) if expect else None, snr_median_db=round(float(np.median(snrs)), 2) if n else None, offset_hz=round(float(np.median([f["offset_hz"] for f in r["frames"]])), 1) if n else None, failed_after_sync=r["n_fail"], adc_rms_lsb=round(r["rms_lsb"], 2) if r["rms_lsb"] is not None else None) rows.append(row) print(f"{g:8.2f} {n:8d} {expect:9.1f} {row['delivered'] or 0:10.0%} " f"{row['snr_median_db'] if n else float('nan'):8.1f} " f"{row['offset_hz'] if n else float('nan'):10.1f} " f"{row['adc_rms_lsb'] or float('nan'):8.2f}" + (" <- ADC under 1 LSB: raise --rx-gains" if (row["adc_rms_lsb"] or 99) < 1 else ""), flush=True) misses = misses + 1 if n == 0 else 0 if misses >= 2: break except KeyboardInterrupt: print("\ninterrupted") finally: sdr.tx_destroy_buffer() print("Pluto stopped") if not rows: return # 3. linearity fit and threshold for rg in a.rx_gains: rs = [r for r in rows if r["rx_gain_db"] == rg] lin = [r for r in rs if r["snr_median_db"] is not None and r["snr_median_db"] < 10] print(f"\nRTL gain {rg:g} dB:") if len(lin) >= 2: g = np.array([r["tx_gain_db"] for r in lin]) s = np.array([r["snr_median_db"] for r in lin]) c = float(np.mean(s - g)) slope = float(np.polyfit(g, s, 1)[0]) print(f" SNR vs TX gain slope {slope:.2f} dB/dB over {len(lin)} steps " f"(1.00 = estimator linear); SNR ~= TX gain {c:+.1f} dB") for r in rs: r["snr_from_fit_db"] = round(r["tx_gain_db"] + c, 1) ok = [r for r in rs if r["delivered"] is not None and r["delivered"] >= 0.9] bad = [r for r in rs if r["delivered"] is not None and r["delivered"] < 0.9] if ok and bad: lo_ok = min(ok, key=lambda r: r["tx_gain_db"]) hi_bad = max(bad, key=lambda r: r["tx_gain_db"]) print(f" >=90% delivered down to {lo_ok['snr_from_fit_db']:+.1f} dB SNR; " f"below 90% from {hi_bad['snr_from_fit_db']:+.1f} dB") coh = resolve_mode(PhyParams(sf=a.sf, bw=a.bw), a.coherent, a.noncoherent) mode = "coherent" if coh else "non-coherent" key = (a.sf, a.bw, coh) if key in SIM_THRESHOLD: print(f" simulator: 10% frame loss at {SIM_THRESHOLD[key]:+.1f} dB for this " f"frame (SF{a.sf}, {a.bw / 1e3:g} kHz, {mode})") elif (a.sf, 125e3, coh) in SIM_THRESHOLD: print(f" simulator: 10% frame loss at " f"{SIM_THRESHOLD[(a.sf, 125e3, coh)]:+.1f} dB for SF{a.sf} " f"{mode} (125 kHz, 24 B body; shorter frames need ~1-2 dB more)") else: print(" not enough steps below +10 dB SNR to fit; extend the sweep downward") for r in rows: r.setdefault("snr_from_fit_db", None) print(f"\nresults: {write_table(rows, a.name + '_sweep')}") if __name__ == "__main__": main()