record.py 3.85 KB
Newer Older
1
#!/usr/bin/env python3
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
2
3
4

from argparse import ArgumentParser
import subprocess
5
6
import os
from make_sigmf import make_sigmf
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
7
from datetime import datetime
8

9
10
11
12
13
14
15
16
17
18
19
20
def dataformat_to_str(dataformat):
    if dataformat.strip('"') == "short":
        datatype = "ci16_le"
    elif dataformat.strip('"') == "float":
        datatype = "cf32_le"
    elif dataformat.strip('"') == "double":
        datatype = "cf64_le"
    else:
        datatype = "unknown"

    return datatype

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
21
22
23
24

if __name__ == "__main__":
    parser = ArgumentParser(description="This application streams data from a single channel of a USRP device to a file; it wraps a C++ program")
    parser.add_argument("--args", help="multi uhd device address args", default="")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
25
    parser.add_argument("--file", help="name of the file to write binary samples to (default deduced from date)")
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    parser.add_argument("--type", help="sample type", choices=["double", "float", "short"], default="short")
    parser.add_argument("--nsamps", help="total number of samples to receive", default=0)
    parser.add_argument("--duration", help="total number of seconds to receive", default=0)
    parser.add_argument("--spb", help="sample per buffer", default=10000)
    parser.add_argument("--rate", help="rate of incoming samples", default=1e6)
    parser.add_argument("--freq", help="RF center frequency in Hz", required=True, default=0.0)
    parser.add_argument("--lo-offset", help="offset for frontent LO in Hz (optional)")
    parser.add_argument("--gain", help="gain for the RF chain", required=True, default=40)
    parser.add_argument("--ant", help="antena selection", default="TX/RX")
    parser.add_argument("--subdev", help="subdevice specification", default="")
    parser.add_argument("--channel", help="which channel to use", default=0)
    parser.add_argument("--bw", help="analog frontend filter bandwidth in Hz", required=True)
    parser.add_argument("--ref", help="reference source", choices=["internal", "external", "mimo"])
    parser.add_argument("--wirefmt", help="wire format", choices=["sc8", "sc16", "s16"])
    parser.add_argument("--setup", help="seconds of setup time", default=1.0)
    parser.add_argument("--progress", help="periodically display short-term bandwidth", action="store_true")
    parser.add_argument("--stats", help="show average bandwidth on exit", action="store_true")
    parser.add_argument("--pps", help="use external pps signal", action="store_true")
    parser.add_argument("--null", help="run without writing to file", action="store_true")
    parser.add_argument("--continue", help="don't abort on a bad packet", action="store_true")
    parser.add_argument("--ski-lo", help="skip checking LO lock status", action="store_true")
    parser.add_argument("--int-n", help="tune USRP with integer-N tuning", action="store_true")

    args = parser.parse_args()

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
51
    if args.file is None:
52
53
54
55
56
        datatype = dataformat_to_str(args.type)
        args.file = f"camras-{datetime.utcnow():%Y_%m_%d_%H_%M_%S}" + \
                    f"_{float(args.freq)/1e6:.3f}MHz" + \
                    f"_{float(args.rate)/1e6:.1f}Msps" + \
                    f"_{datatype}.sigmf-data"
Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
57

Tammo Jan Dijkema's avatar
Tammo Jan Dijkema committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    args_dict = vars(args)

    bool_args = [key for key, value in args_dict.items() if value == True and isinstance(value, bool)]

    arg_list = []
    for key, value in args_dict.items():
        if isinstance(value, bool):
            if value == True:
                arg_list += ["--" + key]
        elif value == "" or value is None:
            continue
        else:
            arg_list += ["--" + key, str(value)]

72
73
74
75
76
77
78
79
80
81
82
    try:
        exit_code = subprocess.call(["./usrp_to_file"] + arg_list)
    except KeyboardInterrupt:
        if os.path.isfile(args.file):
            exit_code = 0
        else:
            raise

    if exit_code != 0:
        raise RuntimeError("Exit code of usrp_to_file was " + exit_code)

83
    sigmf_meta_filename = make_sigmf(args.file + ".metadata")