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.
75 lines
3.4 KiB
75 lines
3.4 KiB
#!/usr/bin/env python3 |
|
|
|
"""BER / symbol-error sweeps for the K=1 reference modem (uncoded). |
|
|
|
Usage: |
|
./sim.py # default sweep |
|
./sim.py --sf 9 --snr -16 -10 --trials 30 --impaired --seed 42069 |
|
./sim.py --sf 7 9 --trials 200 --impaired --jobs 8 |
|
""" |
|
import parallel # before numpy: one BLAS thread per worker |
|
import argparse, time |
|
from functools import partial |
|
import numpy as np |
|
from phy import PhyParams, modulate, bits_to_symbols |
|
from channel import ChannelConfig, apply_channel |
|
from rx import receive |
|
|
|
|
|
def _trial(rng, p, snr_db, n_data_syms, impaired): |
|
"""One frame -> (detected, bit errors, index errors, QPSK bit errors).""" |
|
nb = n_data_syms * p.bits_per_symbol |
|
bits = rng.integers(0, 2, nb).astype(np.uint8) |
|
tx = modulate(bits, p) |
|
if impaired: |
|
cfg = ChannelConfig( |
|
snr_db=snr_db, |
|
ppm=rng.uniform(-50, 50), # +-50 ppm shared-crystal mismatch |
|
delay_chips=rng.uniform(0, p.M), # arbitrary frame start |
|
phase=rng.uniform(0, 2 * np.pi), |
|
dc=0.3 * (rng.standard_normal() + 1j * rng.standard_normal()), |
|
iq_gain_db=0.5, iq_phase_deg=3.0) |
|
else: |
|
cfg = ChannelConfig(snr_db=snr_db, delay_chips=rng.uniform(0, p.M), |
|
phase=rng.uniform(0, 2 * np.pi)) |
|
y, _ = apply_channel(tx, p, cfg, rng) |
|
out, sync, info = receive(y, p, nb) |
|
if out is None: # count a miss as coin-flip |
|
return 0, nb // 2, n_data_syms, n_data_syms |
|
idx_true, a_true = bits_to_symbols(bits, p) |
|
return (1, int(np.sum(out != bits)), int(np.sum(info["idx"] != idx_true)), |
|
int(np.sum(np.sign(info["a"].real) != np.sign(a_true.real)) + |
|
np.sum(np.sign(info["a"].imag) != np.sign(a_true.imag)))) |
|
|
|
|
|
def run_point(p, snr_db, trials, n_data_syms, impaired, seed=0, jobs=1): |
|
fn = partial(_trial, p=p, snr_db=snr_db, n_data_syms=n_data_syms, impaired=impaired) |
|
det, be, ie, qe = np.sum(parallel.run_trials(fn, trials, seed, jobs), axis=0) |
|
nb = n_data_syms * p.bits_per_symbol |
|
return dict(det=det / trials, ber=be / (nb * trials), |
|
ser_idx=ie / (n_data_syms * trials), |
|
qpsk_bit_err=qe / (2 * n_data_syms * trials)) |
|
|
|
|
|
if __name__ == "__main__": |
|
ap = argparse.ArgumentParser() |
|
ap.add_argument("--sf", type=int, nargs="+", default=[7]) |
|
ap.add_argument("--snr", type=float, nargs="+", default=[-12, -10, -8, -6]) |
|
ap.add_argument("--trials", type=int, default=20) |
|
ap.add_argument("--syms", type=int, default=40) |
|
ap.add_argument("--osr", type=int, default=2) |
|
ap.add_argument("--impaired", action="store_true") |
|
ap.add_argument("--seed", type=int, default=0) |
|
ap.add_argument("--jobs", type=int, default=parallel.default_jobs(), |
|
help="worker processes (default: all cores)") |
|
a = ap.parse_args() |
|
print(f"{'SF':>3} {'SNR':>6} {'det':>5} {'idxSER':>8} {'QPSKbitER':>10} {'BER':>9} " |
|
f"(impaired={a.impaired}, jobs={a.jobs})") |
|
for sf in a.sf: |
|
p = PhyParams(sf=sf, osr=a.osr) |
|
for snr in a.snr: |
|
t0 = time.time() |
|
r = run_point(p, snr, a.trials, a.syms, a.impaired, a.seed, a.jobs) |
|
print(f"{sf:>3} {snr:>6.1f} {r['det']:>5.2f} {r['ser_idx']:>8.4f} " |
|
f"{r['qpsk_bit_err']:>10.5f} {r['ber']:>9.5f} ({time.time()-t0:.1f}s)", |
|
flush=True)
|
|
|