Walidacja RF: przypiete nagrania syntetyka i negatywy
This commit is contained in:
1 parent
78406ecf40
commit
6d2c620f95
5 files changed
+249
-358
No files matched your search
+42
-70
@@ -1,25 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Synthesise a .cu8 IQ capture of the TPMS simulator's RF output.
|
||||
|
||||
The generated file is what an RTL-SDR would record while the ESP32+CC1101
|
||||
simulator is transmitting. Feed it to rtl_433 to verify the frame format
|
||||
without any hardware:
|
||||
|
||||
python3 tools/gen_cu8.py --id 2FC871 --pressure 260 --temp 23 -o /tmp/sim.cu8
|
||||
rtl_433 -r /tmp/sim.cu8
|
||||
|
||||
expected:
|
||||
model : Schrader-EG53MA4 id : 2FC871 pressure_kPa: 260.0 temperature_F: 73
|
||||
|
||||
Modulations:
|
||||
ook ASK/OOK, Manchester data (default; matches the rtl_433 reference
|
||||
capture and the built-in Schrader-EG53MA4 decoder)
|
||||
fsk 2-FSK, +/-47.6 kHz deviation, same data (verify with
|
||||
rtl_433 -X 'n=schrader,m=FSK_MC_ZEROBIT,s=120,l=120,r=300')
|
||||
|
||||
DISCLAIMER: diagnostic/test tool only, generates artificial pressure data.
|
||||
"""
|
||||
|
||||
"""Synteza CU8 OOK/Manchester dla niezaleznego testu rtl_433 (bez RF)."""
|
||||
import argparse
|
||||
import math
|
||||
import os
|
||||
@@ -27,73 +7,65 @@ import random
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from tpms_frame import build_row, build_tx_bits, CHIP_US # noqa: E402
|
||||
from tpms_frame import build_row, build_tx_bits, CHIP_US
|
||||
|
||||
SAMPLE_RATE = 250000
|
||||
|
||||
|
||||
def synth(bits: str, mode: str, deviation_hz: float, amp: float, noise: float,
|
||||
rng: random.Random):
|
||||
"""Yield (i, q) int8 pairs for one chip string."""
|
||||
sps = SAMPLE_RATE * CHIP_US / 1e6 # samples per chip (~30)
|
||||
total = int(round(len(bits) * sps))
|
||||
def synth(bits: str, amp: float, noise: float, rng: random.Random):
|
||||
samples_per_chip = SAMPLE_RATE * CHIP_US / 1e6
|
||||
phase = 0.0
|
||||
for n in range(total):
|
||||
k = int(n / sps) % len(bits)
|
||||
bit = bits[k]
|
||||
if mode == "fsk":
|
||||
freq = deviation_hz if bit == "1" else -deviation_hz
|
||||
phase += 2 * math.pi * freq / SAMPLE_RATE
|
||||
a = amp
|
||||
else:
|
||||
a = amp if bit == "1" else 0.0
|
||||
i = a * math.cos(phase) + rng.gauss(0, noise)
|
||||
q = a * math.sin(phase) + rng.gauss(0, noise)
|
||||
yield i, q
|
||||
for n in range(int(round(len(bits) * samples_per_chip))):
|
||||
chip = bits[int(n / samples_per_chip) % len(bits)]
|
||||
amplitude = amp if chip == "1" else 0.0
|
||||
phase += 0.1 # dowolna nośna w pasmie IQ; demodulator uzywa amplitudy
|
||||
yield (amplitude * math.cos(phase) + rng.gauss(0, noise),
|
||||
amplitude * math.sin(phase) + rng.gauss(0, noise))
|
||||
|
||||
|
||||
def write_cu8(path, samples):
|
||||
with open(path, "wb") as f:
|
||||
with open(path, "wb") as output:
|
||||
for i, q in samples:
|
||||
f.write(bytes((max(0, min(255, int(round(i + 127.5)))),
|
||||
max(0, min(255, int(round(q + 127.5)))))))
|
||||
output.write(bytes((max(0, min(255, int(round(i + 127.5)))),
|
||||
max(0, min(255, int(round(q + 127.5)))))))
|
||||
|
||||
|
||||
def main() -> int:
|
||||
p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
p.add_argument("--id", default="2FC871")
|
||||
p.add_argument("--pressure", type=float, default=260.0)
|
||||
p.add_argument("--temp", type=float, default=23.0)
|
||||
p.add_argument("--flags", default="4C900070")
|
||||
p.add_argument("--mode", choices=("ook", "fsk"), default="ook")
|
||||
p.add_argument("--deviation", type=float, default=47.6, help="FSK deviation kHz")
|
||||
p.add_argument("--frames", type=int, default=3)
|
||||
p.add_argument("--gap-ms", type=float, default=50.0, help="gap between burst frames")
|
||||
p.add_argument("--amp", type=float, default=60.0)
|
||||
p.add_argument("--noise", type=float, default=2.0)
|
||||
p.add_argument("-o", "--out", default="/tmp/opencode/tpms_ref/sim.cu8")
|
||||
args = p.parse_args()
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--id", default="2FC871")
|
||||
parser.add_argument("--pressure", type=float, default=260.0)
|
||||
parser.add_argument("--temp", type=float, default=23.0)
|
||||
parser.add_argument("--flags", default="4C900070")
|
||||
parser.add_argument("--frames", type=int, default=3)
|
||||
parser.add_argument("--gap-ms", type=float, default=50.0)
|
||||
parser.add_argument("--amp", type=float, default=60.0)
|
||||
parser.add_argument("--noise", type=float, default=2.0)
|
||||
parser.add_argument("--corrupt", choices=("checksum", "manchester", "truncate"))
|
||||
parser.add_argument("-o", "--out", required=True)
|
||||
args = parser.parse_args()
|
||||
if not 1 <= args.frames <= 32 or not 20 <= args.gap_ms <= 500:
|
||||
parser.error("frames=1..32, gap-ms=20..500")
|
||||
|
||||
row = build_row(int(args.id, 16), args.pressure, args.temp, int(args.flags, 16))
|
||||
if args.corrupt == "checksum":
|
||||
row = row[:-1] + ("0" if row[-1] == "1" else "1")
|
||||
bits = build_tx_bits(row)
|
||||
print(f"frame: {len(bits)} chips, tx bytes:"
|
||||
f" {' '.join(f'{int(bits[i:i+8],2):02X}' for i in range(0, len(bits), 8))}")
|
||||
|
||||
out_dir = os.path.dirname(os.path.abspath(args.out))
|
||||
if out_dir:
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
if args.corrupt == "manchester":
|
||||
bits = bits[:20] + "00" + bits[22:]
|
||||
if args.corrupt == "truncate":
|
||||
bits = bits[:-24]
|
||||
|
||||
os.makedirs(os.path.dirname(os.path.abspath(args.out)), exist_ok=True)
|
||||
rng = random.Random(1)
|
||||
gap_sps = int(SAMPLE_RATE * args.gap_ms / 1000)
|
||||
out = []
|
||||
for f in range(args.frames):
|
||||
out.extend(synth(bits, args.mode, args.deviation * 1000, args.amp,
|
||||
args.noise, rng))
|
||||
out.extend((0.0, 0.0) for _ in range(gap_sps))
|
||||
write_cu8(args.out, out)
|
||||
print(f"wrote {args.out} ({len(out) / SAMPLE_RATE:.3f} s, {args.frames} frames)")
|
||||
gap = [(0.0, 0.0)] * int(SAMPLE_RATE * args.gap_ms / 1000)
|
||||
output = []
|
||||
for _ in range(args.frames):
|
||||
output.extend(synth(bits, args.amp, args.noise, rng))
|
||||
output.extend(gap)
|
||||
write_cu8(args.out, output)
|
||||
print(f"{args.out}: {len(output) / SAMPLE_RATE:.3f} s, {args.frames} ramek")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user