Skip to content

Receiving a signal

Stages added together make a pipeline; blocks from a source flow through it with every stage's state carried from one block to the next, so the output does not depend on how the stream is cut.

# docs: skip
import rpps as rp

src = rp.source.File("ci16t", "capture.cs16", Fs=100e6, cf=1.0e9)       # or rp.source.Device("hackrf", cf=..., Fs=...)
rx = (rp.signal.filters.ShiftFreq(22.3e6) + rp.signal.filters.low_pass(401, 3e6, 100e6) + rp.signal.sample.down.Decimate(8)
      + rp.sync.Acquire() + rp.sync.time.Mat() + rp.sync.phase.FFCarrier())

for out in src(16384) + rx:          # blocks flow through, state carried from one to the next
    print(out.Fs, out.sps, out.det)  # rates follow the stages; Acquire's detection is on the data

src(16384) + rx >> rp.source.Sink("cf32", "symbols.iq")       # or straight into a file

A stage can also be applied by hand to one block: blk + rp.signal.filters.ShiftFreq(1e3).

Package Gives
rpps.source File, SigMF, Wav, Socket, Memory, Device (a radio through pysdrlib); Sink; formats by pysdrlib's names (cf32, ci16t, ...)
rpps.signal filters (designed in Hz, or definitions), resampling, channelize, DC block, de-emphasis; signal.channel: AWGN, CFO, PhaseOffset, ClockDrift, Multipath, Fading, Gain
rpps.sync acquisition, AGC, timing, carrier, equalizers, frame sync; block estimators first, per-sample loops second
rpps.wave matched filters, FM/AM/SSB demodulation, OFDM, staggering

A simulated channel, and recovery with block estimators

Sync is first class when it estimates from a window with block operations (FFTs, correlation, least squares) and a slow loop carries the estimate from window to window; rpps.sync has the rules and the list.

"""Test a receiver against a simulated channel, and recover the signal with block estimators

    python examples/channel_and_sync.py

The channel is a chain of stages like any other (`rp.signal.channel`: AWGN, CFO, PhaseOffset, ClockDrift, Multipath,
Fading, Gain), so the impairments are the ones a radio gives and they stream block by block.

The receiver uses the first-class sync of rpps.sync: estimate from a window with block operations,
then carry the estimate across windows with a slow loop. `Acquire` finds the symbol rate, modulation, roll-off and
frequency offset from the stream itself; `time.Mat` takes the symbols with a matrix matched filter at the detected rate;
`phase.FFCarrier` removes frequency and phase feed-forward; `quality.Monitor` reports Es/N0 as the symbols go by.
Nothing is told the baud rate or the offsets.

The same estimators receive a real capture in examples/e2e.py (Oerder & Meyr timing, the M-th power carrier) and
e2e_mat.py (the matrix matched filter).
"""
import numpy as np

import rpps as rp

def main(esn0_db=15.0, cfo=3.1e3, ppm=20.0):
    rng = np.random.default_rng(1)
    Fs, baud = 1e6, 125e3

    # Transmit: QPSK, root-raised-cosine pulses at 8 samples a symbol
    qpsk = rp.symbol.load("psk/qpsk")
    syms = rp.Data(rng.integers(0, 2, 2*30000).astype(bool), Fs=baud, sps=1) * qpsk
    tx = syms + rp.wave.Shape(Fs/baud, rp.signal.filters.load("pulse/rrc/0p35"))

    # The channel: a frequency offset, a phase, a sample clock 20 ppm fast, a weak echo, and noise at Es/N0 (power=None
    # measures the signal: the pulse shaping changed its power)
    channel = (rp.signal.channel.CFO(cfo) + rp.signal.channel.PhaseOffset(0.7) + rp.signal.channel.ClockDrift(ppm)
               + rp.signal.channel.Multipath([1, 0.1j], [0, 3]) + rp.signal.channel.AWGN(esn0_db=esn0_db, power=None, seed=2))
    air = tx + channel

    # Receive: the samples as a radio would give them, in blocks; nothing about the signal is given
    rx = rp.sync.Acquire() + rp.sync.time.Mat() + rp.sync.phase.FFCarrier() + rp.sync.quality.Monitor()
    n = 0
    for out in rp.source.Memory(air.data, Fs=Fs)(16384) + rx:
        n += len(out)
    det, q = out.det, out.stream.est["quality"]
    print(f"{n} symbols out of {len(syms)} sent")
    print(f"detected: {det}")
    print(f"measured: {q}")

    assert det.mod == "qpsk"
    assert abs(det.baud/baud - 1 - ppm*1e-6) < 20e-6 # the clock error shows up as the symbol rate
    assert abs(det.cfo - cfo) < 20
    assert abs(q.esn0_db - esn0_db) < 1.0 # within 1 dB of the channel: a bigger gap is a diagnostic, not noise

if __name__ == "__main__":
    main()

Sources, sinks and carried state

Iterating a source carries stream and ctx for you; calling stages by hand, pass them on yourself.

"""Sources, sinks, and the state carried from one block to the next

    python examples/streaming_io.py

Blocks come from a source (`rp.source.File`, `SigMF`, `Wav`, `Socket`, `Device` for a radio through pysdrlib,
`Memory` for an array or a capture held in memory) called with a block length. Stages added to the blocks run on
each one as it comes; `>>` writes them to a sink. Every block carries the stream's state, so a stage picks up exactly
where the last block left off:

    for out in src(4096) + rx: ...             # iterate
    src(4096) + rx >> rp.source.SigMFSink(..)  # or straight into a file
    src(4096).pipelined(rx)                    # each stage of rx in a thread of its own (real time)

Two kinds of state, carried for you when you iterate a source, by hand otherwise:
    a Stage (`+`) keeps it in the stream:  rp.Data(chunk, stream=prev.stream)
    a Pipe (`*`, `/`) keeps it in the context:  rp.Data(chunk, ctx=prev.ctx)
A fresh rp.Data(chunk) starts both over.
"""
import pathlib
import tempfile

import numpy as np

import rpps as rp

Fs, BAUD, FO = 1e6, 50e3, 150e3 # a 50 kBd QPSK carrier 150 kHz above the centre of a 1 MS/s recording

def recording(path):
    """Write a synthetic capture as a SigMF recording (16-bit integers, as a radio would give them)"""
    rng = np.random.default_rng(3)
    syms = rp.Data(rng.integers(0, 2, 2*20000).astype(bool), Fs=BAUD, sps=1) * rp.symbol.load("psk/qpsk")
    wave = syms + rp.wave.Shape(Fs/BAUD, rp.signal.filters.load("pulse/rrc/0p35"))
    air = wave + rp.signal.channel.CFO(FO + 900) + rp.signal.channel.AWGN(esn0_db=18, power=None, seed=1)
    x = (0.3*air.data/np.sqrt(np.mean(np.abs(air.data)**2))).astype(np.complex64)
    with rp.source.SigMFSink(path, datatype="ci16_le", description="streaming_io example") as sink:
        rp.source.Memory(x, Fs=Fs, cf=915e6)(8192) >> sink # Fs and cf come from the blocks
    return x

def conditioning():
    """Samples to the signal alone: shift it to 0 Hz, filter, decimate to 4 samples a symbol"""
    return (rp.signal.filters.ShiftFreq(-FO) + rp.signal.filters.low_pass(101, 60e3, Fs)
            + rp.signal.sample.down.Decimate(5))

def receiver():
    return conditioning() + rp.sync.Acquire() + rp.sync.time.Mat() + rp.sync.phase.FFCarrier() + rp.sync.quality.Monitor()

def main():
    with tempfile.TemporaryDirectory() as tmp:
        path = pathlib.Path(tmp)/"capture"
        x = recording(path)
        # Fs, cf and the sample format come from the metadata. `with` unmaps the file when done: Windows can't delete
        # (or overwrite) a file that is still mapped
        with rp.source.SigMF(path, sps=Fs/BAUD) as src:
            print(f"{src}, {len(src)} samples")

            # 1. Iterate: rates follow the stages (Fs /5, sps from the detection), the detection rides on the data
            parts = []
            for out in src(4096) + receiver():
                parts.append(out.data)
        iterated = np.concatenate(parts)
        print(f"iterated: {len(iterated)} symbols at {out.Fs:.0f} Bd, {out.det.mod} detected, {out.stream.est['quality']}")

        # 2. By hand: the same blocks, the stream passed on
        rx, prev, parts = receiver(), None, []
        for at in range(0, len(x), 4096):
            blk = rp.Data(x[at:at + 4096], Fs=Fs, cf=915e6, sps=Fs/BAUD, flush=at + 4096 >= len(x),
                          stream=None if prev is None else prev.stream)
            prev = blk + rx
            parts.append(prev.data)
        by_hand = np.concatenate(parts)
        assert len(by_hand) == len(iterated) and np.allclose(by_hand, iterated, atol=2e-3) # (16-bit samples on disk)

        # 3. Pipelined: the source and every stage in a thread of their own, the same output; stream.load() is the time
        # each stage took as a fraction of real time (over 1.0: too slow for a live radio at this rate)
        parts = []
        with rp.source.SigMF(path, sps=Fs/BAUD) as src:
            for out in src(4096).pipelined(receiver()):
                parts.append(out.data)
        assert np.allclose(np.concatenate(parts), iterated)
        print("pipelined: same symbols; load per stage " + ", ".join(f"{type(s).__name__} {v:.2f}"
                                                                    for s, v in out.stream.load().items()))

        # 4. Any block cut gives the same samples through fixed stages (filters, shifts, resamplers). An estimator that
        # waits for enough signal (Acquire) can decide at a slightly different point, so compare before it
        with rp.source.SigMF(path) as src:
            a = (src(4096) + conditioning()).collect()
            b = (src(1500) + conditioning()).collect()
        assert np.allclose(a, b, atol=1e-5)
        print(f"conditioning in blocks of 4096 and 1500: the same {len(a)} samples")

        # 5. Pipes (`*`, `/`) carry their state in the context: decode symbols that arrive in pieces
        qpsk, conv = rp.symbol.load("psk/qpsk"), rp.code.load("conv/k7_171_133/1_2")
        msg = np.random.default_rng(4).integers(0, 2, 3000).astype(bool)
        syms = (rp.Data(msg, flush=True) * conv * qpsk).data
        prev, bits = None, []
        for at in range(0, len(syms), 777):
            d = rp.Data(syms[at:at + 777], sps=1, flush=at + 777 >= len(syms), ctx=None if prev is None else prev.ctx)
            prev = d / qpsk / conv
            bits.append(prev.as_bits().data)
        bits = np.concatenate(bits)
        assert np.array_equal(bits, msg[:len(bits)]) and len(bits) == len(msg)
        print(f"decoded {len(bits)} bits from symbols in blocks of 777, the decoder state carried by ctx=")

if __name__ == "__main__":
    main()