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.
91 lines
3.4 KiB
91 lines
3.4 KiB
#!/usr/bin/env python3 |
|
"""Convert an I/Q recording to SDRangel's .sdriq format (FileInput device). |
|
|
|
SigMF recordings carry their own format, rate and centre frequency. Raw files |
|
need --format, --fs and --lo. |
|
|
|
./to_sdriq.py ../ota_tx # SigMF -> ../ota_tx.sdriq |
|
./to_sdriq.py ../ota.cu8 --format cu8 --fs 2.4e6 --lo 223.33e6 |
|
|
|
Samples are written as 16-bit I/Q. The .sdriq header is 32 bytes, little |
|
endian: sample rate u32, centre frequency u64, start time u64 (ms since the |
|
epoch), sample size u32 (16), filler u32, CRC-32 of the first 28 bytes u32. |
|
""" |
|
|
|
import argparse |
|
import json |
|
import struct |
|
import time |
|
import zlib |
|
from datetime import datetime |
|
from pathlib import Path |
|
|
|
import numpy as np |
|
|
|
CHUNK = 1 << 20 # complex samples per block |
|
|
|
|
|
def sigmf_paths(name): |
|
p = Path(name) |
|
base = p.with_suffix("") if p.suffix in (".sigmf-meta", ".sigmf-data") else p |
|
return base.with_suffix(".sigmf-meta"), base.with_suffix(".sigmf-data") |
|
|
|
|
|
def to_int16(raw, fmt): |
|
"""Interleaved I/Q of the given format to int16, full scale 32767.""" |
|
if fmt == "cu8": |
|
x = (np.frombuffer(raw, np.uint8).astype(np.float32) - 127.5) * (32767 / 127.5) |
|
elif fmt == "ci8": |
|
x = np.frombuffer(raw, np.int8).astype(np.float32) * (32767 / 128) |
|
elif fmt == "ci16": |
|
return np.frombuffer(raw, "<i2") |
|
elif fmt == "cf32": |
|
x = np.frombuffer(raw, "<f4") * 32767 |
|
else: |
|
raise SystemExit(f"unknown format {fmt}") |
|
return np.clip(np.rint(x), -32768, 32767).astype("<i2") |
|
|
|
|
|
def main(): |
|
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
|
ap.add_argument("input", help="SigMF name or raw I/Q file") |
|
ap.add_argument("-o", "--output", help="output .sdriq (default: input name with .sdriq)") |
|
ap.add_argument("--format", choices=["cu8", "ci8", "ci16", "cf32"], help="raw input format") |
|
ap.add_argument("--fs", type=float, help="raw input sample rate, Hz") |
|
ap.add_argument("--lo", type=float, help="raw input centre frequency, Hz") |
|
a = ap.parse_args() |
|
|
|
meta_path, data_path = sigmf_paths(a.input) |
|
start_ms = int(time.time() * 1000) |
|
|
|
if a.format is None and meta_path.exists(): |
|
meta = json.loads(meta_path.read_text()) |
|
g, cap = meta["global"], meta["captures"][0] |
|
fmt = g["core:datatype"].replace("_le", "") |
|
fs, lo = g["core:sample_rate"], cap["core:frequency"] |
|
if "core:datetime" in cap: |
|
start_ms = int(datetime.fromisoformat(cap["core:datetime"].replace("Z", "+00:00")).timestamp() * 1000) |
|
src = data_path |
|
else: |
|
if not (a.format and a.fs and a.lo): |
|
raise SystemExit("raw input needs --format, --fs and --lo") |
|
fmt, fs, lo, src = a.format, a.fs, a.lo, Path(a.input) |
|
|
|
out = Path(a.output) if a.output else src.with_name(src.name.split(".")[0] + ".sdriq") |
|
header = struct.pack("<IQQII", int(round(fs)), int(round(lo)), start_ms, 16, 0) |
|
header += struct.pack("<I", zlib.crc32(header)) |
|
bytes_per_sample = {"cu8": 2, "ci8": 2, "ci16": 4, "cf32": 8}[fmt] |
|
|
|
n = 0 |
|
with open(src, "rb") as fi, open(out, "wb") as fo: |
|
fo.write(header) |
|
while raw := fi.read(CHUNK * bytes_per_sample): |
|
s = to_int16(raw, fmt) |
|
fo.write(s.tobytes()) |
|
n += len(s) // 2 |
|
|
|
print(f"{out}: {n} samples, {n / fs:.1f} s at {fs / 1e6:g} MS/s, centre {lo / 1e6:.6f} MHz, from {fmt}") |
|
|
|
|
|
if __name__ == "__main__": |
|
main()
|
|
|