Skip to content

Stream

pysdrlib.base.stream

What came with a block of samples (Device.read(..., meta=True)), the stream object (Device.stream), and how the samples were received (Device.get_rx_context: what a recording keeps of the receive chain)

with sdr.stream(65536, meta=True) as s:        # starts receiving, stops (and flushes) on exit
    for x, m in s:
        if m.dropped: ...                      # samples were lost before or inside this block (m.gaps)
        if m.events: ...                       # the frequency / rate / gain changed inside it (m.events)

Classes:

Name Description
Event

A setting that changed while receiving. offset: first sample of the block it applies to (it may still

RxContext

How one receive channel's samples came to be: what a recording keeps so a reader can place them over the air

RxMeta

index: position of the block's first sample in the stream (samples since start_rx, dropped ones counted)

RxStream

Fixed-size blocks from a device, receiving started on entry (if it wasn't) and stopped on exit

Functions:

Name Description
sample_time

time.monotonic() of sample index, from the last delivery: total samples were in by age seconds ago

Event dataclass

Event(offset: int, kind: str, value: object, channel: int = 0)

A setting that changed while receiving. offset: first sample of the block it applies to (it may still be settling for Capabilities.retune_settle seconds after); kind: "freq", "rate", "gain", "bandwidth"...

RxContext dataclass

RxContext(freq: Optional[float] = None, rate: Optional[float] = None, chain: Optional[object] = None, ppm: float = 0.0, ppm_accuracy: Optional[float] = None, clock_source: Optional[str] = None, gains: dict = dict(), level_offset: Optional[float] = None, level_accuracy: Optional[float] = None, port: Optional[str] = None, hw: Optional[str] = None, undone: bool = False)

How one receive channel's samples came to be: what a recording keeps so a reader can place them over the air (Device.get_rx_context; SigMF stores it as pysdrlib:rx, see pysdrlib.file.sigmf)

freq: the centre frequency over the air [Hz] (through chain); rate: the sample rate [Hz]. chain: the caps.Chain between the radio and the air (None: nothing), whose invert says the spectrum arrives mirrored: the samples are left as the radio gives them, so a reader conjugates them. ppm: the reference correction applied while tuning (set_freq_correction); ppm_accuracy: how well the reference is known after it [ppm, a standard error; None: not known] (with the chain's lo_accuracy, what a frequency read off these samples is worth); clock_source: the reference selected (None: never set, the radio's own); level_offset: dB to add to a level in dBFS (full scale: |x| = 1 in the complex64 samples) for dBm at the antenna port (Device.set_level_calibration), and level_accuracy its standard error [dB]; both None when not known: never calibrated, or the gains or frequency have moved off what the calibration was made at; gains: {stage: dB} as last set; port: the antenna port; hw: which radio (its driver's name, and the serial where known); undone: the samples were conjugated after the radio (by whoever wrote them), so the chain's inversion is already put right

Methods:

Name Description
json

Plain types only (numbers as float: a gain or frequency may arrive as a numpy scalar)

Attributes:

Name Type Description
inverted

Whether these samples are mirrored: an LO above the signal, not yet put right (undone)

inverted property

inverted

Whether these samples are mirrored: an LO above the signal, not yet put right (undone)

json

json()

Plain types only (numbers as float: a gain or frequency may arrive as a numpy scalar)

Source code in src/pysdrlib/base/stream.py
def json(self):
    """Plain types only (numbers as float: a gain or frequency may arrive as a numpy scalar)"""
    num = lambda v: None if v is None else float(v)
    return {"freq": num(self.freq), "rate": num(self.rate), "chain": None if self.chain is None else self.chain.json(),
            "inverted": self.inverted, "ppm": float(self.ppm), "ppm_accuracy": num(self.ppm_accuracy),
            "clock_source": self.clock_source, "gains": {str(k): num(v) for k, v in self.gains.items()},
            "level_offset": num(self.level_offset), "level_accuracy": num(self.level_accuracy),
            "port": self.port, "hw": self.hw, "undone": self.undone}

RxMeta dataclass

RxMeta(index: int, count: int, freq: float, rate: float, channel: int = 0, gaps: tuple = (), discarded: int = 0, time: Optional[float] = None, events: tuple = (), hw_time: Optional[float] = None)

index: position of the block's first sample in the stream (samples since start_rx, dropped ones counted) gaps: (offset in the block, samples missing just before it), for every place samples were lost discarded: samples skipped on purpose (settling after a retune with flush=True) before this block time: time.monotonic() at which the first sample was received (estimated from the deliveries, smoothed so it never goes backwards), or None freq / rate: what the device was set to for the first sample; events: changes inside the block, each reported once (offset 0: at the first sample, or in a gap just before it; a change a flush skipped past is not an event, the whole block being at the new setting) hw_time: the radio's own clock at the first sample (seconds; UHD's device time, a replayed recording's time), for devices that timestamp their samples; None otherwise. Unlike time it has no host jitter, and two radios sharing a clock reference and a time (GPS, PPS) agree on it

index is the absolute position in the stream, across gaps: sample k of the block is stream sample index_at(k) = index + k + sum(n for at, n in gaps if 0 < at <= k) (a gap at offset 0 is already in index). The next block's index is index + count + sum(n for at, n in gaps if at > 0), plus its own discarded and its own gap at offset 0. So a block can be stamped with its position and time straight from index and time/hw_time.

Methods:

Name Description
index_at

The stream index of the block's sample k (0 <= k < count; negative counts from the end, as for a list),

follows

Whether this block starts right where prev ended (same channel, nothing lost or discarded between). Gaps

Attributes:

Name Type Description
dropped

Samples lost in or just before this block

next_index int

The stream index just after this block: where the next one starts when nothing is lost or discarded first

wall_time Optional[float]

Best wall-clock time (time.time() seconds) of the first sample: hw_time when the radio's clock holds a date

dropped property

dropped

Samples lost in or just before this block

next_index property

next_index: int

The stream index just after this block: where the next one starts when nothing is lost or discarded first

wall_time property

wall_time: Optional[float]

Best wall-clock time (time.time() seconds) of the first sample: hw_time when the radio's clock holds a date (after 2000-01-01; a clock counting from 0 since power-up doesn't), else time taken to the wall clock, else None

index_at

index_at(k: int) -> int

The stream index of the block's sample k (0 <= k < count; negative counts from the end, as for a list), counting the samples lost before it inside the block

Source code in src/pysdrlib/base/stream.py
def index_at(self, k: int) -> int:
    """The stream index of the block's sample `k` (0 <= k < count; negative counts from the end, as for a list),
    counting the samples lost before it inside the block"""
    if not -self.count <= k < self.count:
        raise IndexError(f"sample {k} of a block of {self.count}")
    k %= self.count
    return self.index + k + sum(n for at, n in self.gaps if 0 < at <= k)

follows

follows(prev: RxMeta) -> bool

Whether this block starts right where prev ended (same channel, nothing lost or discarded between). Gaps inside this block don't count: see gaps

Source code in src/pysdrlib/base/stream.py
def follows(self, prev: "RxMeta") -> bool:
    """Whether this block starts right where `prev` ended (same channel, nothing lost or discarded between). Gaps
    inside this block don't count: see `gaps`"""
    return self.channel == prev.channel and self.index == prev.next_index

RxStream

RxStream(device, count, timeout=None, meta=False, channel=0, flush=True, on_drop=None, latest=False)

Fixed-size blocks from a device, receiving started on entry (if it wasn't) and stopped on exit

Iterate it for blocks (or (block, RxMeta) with meta=True); read() gets one. channel: one, or several (blocks are then (channels, count), aligned; see Device.read). A timeout (seconds) bounds each block's wait; None waits as long as the device keeps delivering (a stalled device raises err.StreamStalled).

Source code in src/pysdrlib/base/stream.py
def __init__(self, device, count, timeout=None, meta=False, channel=0, flush=True, on_drop=None, latest=False):
    self.device, self.count, self.timeout, self.meta, self.channel = device, int(count), timeout, meta, channel
    self.on_drop, self.latest = on_drop, latest
    self._flush = flush
    self._started = False

sample_time

sample_time(rate, total, age, index)

time.monotonic() of sample index, from the last delivery: total samples were in by age seconds ago

Source code in src/pysdrlib/base/stream.py
def sample_time(rate, total, age, index):
    """time.monotonic() of sample `index`, from the last delivery: `total` samples were in by `age` seconds ago"""
    if age is None or not rate:
        return None
    return time.monotonic() - age - (total - index) / rate