Skip to content

Sample files

File reads a sample file, FileSink writes one, and the file driver plays a recording back as a radio (and records what is transmitted to it). The formats are pysdrlib.Formats (Formats.ls() lists them). Their names follow SigMF: the number is the bits of one value (I or Q). So a GNU Radio .cfile is cf32, an RTL-SDR .cu8 is cui8, and ci16 is two int16. The semantics of every call are on the pysdrlib.file page.

Raw files

with FileSink("out.cs16", "ci16", clip=True) as out:      # ci16: two int16 per sample; clip: saturate, don't raise
    out.write(samples)
print(out.count, out.clipped)

with File("out.cs16", "ci16") as f:                        # the format of a raw file is yours to give
    for block in f.forward(8192, partial=True):            # blocks until EOF; partial=True yields the short last one
        pass
    f.seek(0)                                              # positions are in samples
    head = f.next(8192)
    back = f.prev(4096)                                    # step back over the samples just read
    print(f.cur_samp, f.max_samp, f.percent())

Neither class scales: integer formats are read and written as stored. A value outside an integer format's range raises ValueError unless the sink was made with clip=True.

SigMF

A path ending in .sigmf (or .sigmf-data / .sigmf-meta) makes FileSink write SigMF. Giving write the RxMeta of each block records the stream as the radio saw it:

sdr = pysdrlib.open("sim")
sdr.initialize(cf=100e6, Fs=2e6)
with FileSink("capture.sigmf", "ci16", hw=sdr.NAME) as out, sdr.stream(65536, meta=True) as s:
    for i, (x, m) in enumerate(s):
        out.write(x, meta=m)               # the rate, frequency, retunes and lost samples go into capture.sigmf-meta
        if i == 2:
            sdr.set_rx_freq(101e6)         # a new SigMF capture
        if i == 5:
            break
sdr.close()

f = File("capture.sigmf")                  # or "capture", "capture.sigmf-meta", "capture.sigmf-data"
print(f.fmt, f.rate, f.freq, f.meta.captures)

FileSink(..., context=sdr.get_rx_context()) also keeps how the radio received: the LNB or converter in front of it (its LO, and whether the spectrum arrives mirrored), the reference correction and clock, gains and antenna port. It is stored as pysdrlib:rx, an optional SigMF extension that other readers skip. sigmf.read(path).context or File(path).meta.context gives it back, a replayed recording reports it, and pysdrlib play --info prints it.

A recording as a radio

sdr = pysdrlib.open("file", path="capture.sigmf", loop=True)   # a recording, received like a radio
sdr.initialize()                           # the recording's rate and frequency
with sdr.stream(65536, meta=True) as s:
    for i, (x, m) in enumerate(s):
        print(m.index, m.freq, m.events)   # each SigMF capture is a retune
        if i == 4:
            break
sdr.close()

The recording is delivered on a thread, paced to its sample rate (realtime=False: as fast as it is read), and each SigMF capture is a retune. A server hosts it like a radio: pysdrlib server --open file:path=capture.sigmf,loop=true.

Transmitting into a file

sdr = pysdrlib.open("file", tx_path="sent.sigmf", tx_format="ci16", rate=2e6, realtime=False)
sdr.initialize(cf=915e6, Fs=2e6)
sdr.start_tx()
sdr.write(samples)                         # as to a radio: complex64, full scale +-1
sdr.stop_tx()
sdr.close()
print(File("sent.sigmf").max_samp)

With path too, it plays a recording and records what is transmitted at once: a loopback for testing a transmitter.

pysdrlib play

pysdrlib play capture.sigmf                         what it is, then its power second by second as it plays
pysdrlib play capture.cfile --fmt cf32 --rate 2e6 --freq 100e6 --loop
pysdrlib play capture.sigmf --info                  just what it is: format, length, rate, captures, annotations
pysdrlib play capture.sigmf --serve tcp://0.0.0.0:5555 --loop    host it: clients watch (or claim) device file-0

--fast plays as fast as it can be read, and --interval S sets how often a line is printed. Serving needs pyzmq.

Recording from the command line

pysdrlib client tcp://host:5555 rx hackrf --freq 100e6 --rate 10e6 -t 5 -f ci8 -o capture.sigmf

See Remote radios.