The data model¶
RPPS has no graph to build and nothing to reset. Operators are applied to an rp.Data, which knows its representation
(samples, symbols, channel bits, data bits, packets), its rates, and the state of every operator that has touched it.
| Operator | Means | On |
|---|---|---|
data * pipe, data / pipe |
encode / decode | Pipe: an invertible transform (symbol, code, link) |
data + stage |
process | Stage: DSP on a stream (signal, sync, wave) |
pipe * pipe, stage + stage |
a reusable Pipeline |
either |
data >> sink |
write out | Sink |
The design behind it (the chain samples → symbols → channel bits → data bits → packets, which package each function
belongs in, and what each category may take and give) is in rpps.base.
Bits to symbols and back¶
"""Bits to symbols and back: the README's first example
python examples/quickstart.py
"""
import rpps as rp
mod = rp.symbol.load("psk/qpsk") # first bit map; load("psk/qpsk", "R090 inverted") for a rotated / spectrally inverted one
ecc = rp.code.Hamming(7, 4) # or rp.code.load("block/hamming/7_4")
scr = rp.code.scram.load("v35")
msg = rp.Data(b"Hello World!")
syms = msg * scr * ecc * mod # scramble, encode, modulate
out = syms / mod / ecc / scr # demodulate, decode, descramble
assert out.as_bytes().hex == msg.as_bytes().hex
# Pipes multiplied together make a reusable link, decoded in the opposite order
link = scr * ecc * mod
out = (msg * link) / link
assert out.as_bytes().hex == msg.as_bytes().hex
print("decoded:", out.as_bytes().hex)
Things that surprise people¶
- What
rp.Datamakes of an array.bytesanduint8arrays are bytes ([0, 1, 1, 0]as uint8 is 32 bits); bits arebool(an int 0/1 array is an error, not bits); a float array is real samples, never LLRs;complex64withsps=1is symbols,complex128stays samples.Fsdefaults to 1.0. - Partial blocks wait. A code or constellation keeps what doesn't fill a block in the data's context until more
comes or the block is flushed: three bits through QPSK give one symbol, or two with
rp.Data(bits, flush=True). - Two kinds of carried state. A
Stage(+) keeps its state in the stream: passstream=prev.streamto go on. APipe(*,/) keeps it in the context:ctx=prev.ctx. A freshrp.Data(chunk)starts both over. Iterating a source does this for you. - Some state lives on the operator.
Feedthrough.encode(array)called directly moves the register (reset()starts it over);control()changes the operator for everyone holding it. Decode status is not kept on the operator: it comes with the output,out.errorsafterdata / code(per codeword: corrections, -1 where it failed) ormsg, errors = code.decoder.decode(x, status=True), so threads can share one loaded code. - Bits to a link layer go through
rp.code.Uncoded().syms / qpsk / crcis an error: channel bits are not data bits until a code (orUncoded) says so. - LLRs are log(P0/P1): positive means 0, a hard decision is
llr < 0. - Bit order is most significant first (bytes, constellation labels, RS symbols, markers), except HDLC bytes on the
wire (least significant first), recurrence
initintegers (lsbunless"order": "msb") and headers that say"lsb". - Shapes and definitions are separate.
rp.symbol.load("psk/qpsk")reads a definition;rp.symbol.generate("16QAM")makes a shape; DVB-S2's 16APSK isrp.symbol.load("apsk/dvbs2_16apsk", rate="3/4"), notgenerate("16APSK"). - Shaping needs a symbol rate.
rp.Data(bits, Fs=1e6, sps=1)before a pulse: a pulse is designed from the stream's samples per symbol.
Operators, links and partial blocks¶
"""The data model: `*` encodes, `/` decodes, and the data carries what it is
python examples/operators.py
RPPS has no graph to build and nothing to reset. Operators are applied to an `rp.Data`, which knows its representation
(samples, symbols, channel bits, data bits, packets), its rates, and the state of every operator that has touched it:
data * op encode / modulate / scramble (transmit direction)
data / op decode / demodulate / descramble (receive direction)
data + op a DSP stage (samples to samples, see channel_and_sync.py)
Operators multiplied together are a reusable link that decodes in the opposite order. The representation is checked at
every step, so a wrong order is an error instead of garbage.
"""
import numpy as np
import rpps as rp
def main():
# Definitions ship with the package: `python -m rpps ls` lists them
mod = rp.symbol.load("psk/qpsk") # a constellation definition (rp.symbol.generate("16QAM") makes a shape)
ecc = rp.code.Hamming(7, 4) # or rp.code.load("block/hamming/7_4")
scr = rp.code.scram.load("v35")
# One step at a time
msg = rp.Data(b"Hello World!") # bytes are bytes; a bool array is bits
syms = msg * scr * ecc * mod # scramble, encode, modulate
out = syms / mod / ecc / scr # demodulate, decode, descramble: the reverse order
print(f"{syms} -> {out}")
assert out.as_bytes().hex == msg.as_bytes().hex
# The same thing as one link: multiply the operators once, use it both ways
link = scr * ecc * mod
print(f"link: {link}")
assert (rp.Data(b"Hello World!") * link / link).as_bytes().hex == msg.as_bytes().hex
# Partial blocks wait for more. QPSK takes 2 bits a symbol, so 3 bits give 1 symbol and the 3rd bit waits in the
# data's context for the next call; flush=True says there is no more and pads it out
bits = np.array([1, 0, 1], dtype=bool) # bits are bool: an int 0/1 array is an error, not bits
print(f"3 bits -> {len(rp.Data(bits) * mod)} symbol, flushed -> {len(rp.Data(bits, flush=True) * mod)}")
assert len(rp.Data(bits) * mod) == 1 and len(rp.Data(bits, flush=True) * mod) == 2
# A link layer works on data bits, not channel bits. With no FEC, rp.code.Uncoded() says "these channel bits are the
# data bits": `syms / mod / crc` is refused, because the representations don't line up
crc = rp.link.load("crc/crc16_xmodem")
unc = rp.code.Uncoded()
framed = msg * crc * unc * mod
back = framed / mod / unc / crc
ok = back.errors[0] == 0 # The decode's status comes with its output: 0 errors, or -1 where the CRC failed
print(f"crc ok: {ok}, {len(framed)} symbols for {len(msg.as_bytes().hex)//2} bytes + 16 CRC bits")
assert ok and back.as_bytes().hex == msg.as_bytes().hex
try:
_ = framed / mod / crc
raise AssertionError("channel bits went into a CRC")
except rp.errors.StreamError as e:
print(f"refused, as it should be: {e}")
# The demodulator's output is soft: LLRs, log(P0/P1), positive for a 0 (see soft_decisions.py)
# (noiseless symbols give enormous ones, so add a little noise first)
llr = (syms + rp.signal.channel.AWGN(esn0_db=6, seed=1)) / mod
print(f"demodulated: {llr}, soft={llr.is_soft}, first LLRs {np.round(llr.get_soft()[:4], 1)}")
if __name__ == "__main__":
main()
Soft decisions¶
symbols / constellation gives LLRs, and every decoder that can use them does.
"""Soft decisions: keep the demodulator's LLRs all the way into the decoder
python examples/soft_decisions.py
`symbols / constellation` gives LLRs, not bits: log(P0/P1), positive for a 0, so a hard decision is `llr < 0`. Every
decoder that can use them does (Viterbi, BCJR, LDPC, turbo, polar, Hamming on its trellis, repetition); one that can't
(Reed-Solomon) decides itself. So the recommended chain is just `noisy / mod / code`, with no hard decision in between.
This script measures what deciding early costs: the same K=7 rate 1/2 convolutional code, decoded from LLRs and from
hard bits, over QPSK in white noise.
"""
import numpy as np
import rpps as rp
def main(esn0s=(1.0, 2.0, 3.0, 4.0), nbytes=4000):
rng = np.random.default_rng(0)
qpsk = rp.symbol.load("psk/qpsk")
conv = rp.code.load("conv/k7_171_133/1_2")
data = rng.integers(0, 256, nbytes).astype(np.uint8)
sent = np.unpackbits(data).astype(bool)
def ber(out):
got = out.as_bits().data
return np.mean(got != sent[:len(got)])
print(f"{conv}, QPSK, {8*nbytes} bits a point")
print(" Es/N0 channel hard Viterbi soft Viterbi")
rows = []
for esn0 in esn0s:
syms = rp.Data(data, flush=True) * conv * qpsk
noisy = syms + rp.signal.channel.AWGN(esn0_db=esn0, seed=7) # unit-power symbols: Es/N0 is the SNR per symbol
llr = noisy / qpsk # Mod (CHANNEL/LLR)
# Hard: decide first. `as_hard()` (like as_bits / as_bytes) converts the Data it is called on in place, so copy
# it when the soft values are still needed
hard = llr.copy().as_hard()
raw = np.mean(hard.data != (rp.Data(data, flush=True) * conv).data[:len(hard)])
h, s = ber(hard / conv), ber(llr / conv)
rows.append((esn0, h, s))
print(f"{esn0:5.1f} dB {raw:9.2e} {h:12.2e} {s:12.2e}")
# Soft is worth about 2 dB here: it must never be worse, and clearly better where the channel is noisy
assert all(s <= h for _, h, s in rows)
assert rows[1][2] < rows[1][1]/5, "soft decisions should be far better than hard at 2 dB"
if __name__ == "__main__":
main()
More recipes (DSSS, O-QPSK, 802.15.4, GFSK) are in rpps.symbol.