coding
¶
The general channel codes — the families a standard configures, rather than
any standard's picks. ConvEncoder and Viterbi are the two directions of a
rate-1/n convolutional code and both take the generator polynomials;
ReedSolomon is both directions of an RS code over GF(2**J) and takes the
five numbers that define one. A caller names their own code rather than
choosing from a menu.
ccsds_tm holds CCSDS 131.0-B's configuration of these, the way any other
caller would — and note that a standard adds things that are NOT properties
of the code, such as the dual-basis symbol representation CCSDS transmits its
Reed-Solomon symbols in. Matching the algebra is not the same as matching the
wire.
Examples:
>>> import numpy as np
>>> from doppler.coding import ConvEncoder, ReedSolomon, Viterbi
>>> bits = np.array([1, 0, 1, 1, 0, 0, 1, 0], dtype=np.uint8)
>>> sym = ConvEncoder([0o171, 0o133], k=7).encode(bits)
>>> sym.size == 2 * bits.size
True
>>> llr = np.where(sym, -8.0, 8.0).astype(np.float32)
>>> bits_out = Viterbi([0o171, 0o133], k=7, depth=35).decode(llr)
>>> bits_out.dtype
dtype('uint8')
>>> rs = ReedSolomon(nroots=32) # RS(255,223), corrects 16
>>> rs.n, rs.k, rs.e
(255, 223, 16)
Modules:
| Name | Description |
|---|---|
benchmarks |
|
coding |
|
tests |
|
Classes:
| Name | Description |
|---|---|
ConvEncoder |
Build an encoder for the code the polynomials describe. |
Deinterleaver |
The RECEIVE face of the same interleaver. |
Interleaver |
Build an interleaver over a rows x cols block of unit_bits units. |
ReedSolomon |
Create a codec for the code named by the five arguments. |
Viterbi |
Build a decoder for the code the polynomials describe. |
__all__
module-attribute
¶
ConvEncoder
¶
Build an encoder for the code the polynomials describe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly
|
NDArray[uint32]
|
Generator polynomials, one per output. The array IS the code;
|
required |
k
|
int
|
Constraint length, 2 to |
7
|
invert
|
int
|
Bit |
0
|
punct_period
|
int
|
Input bits per puncturing pattern, 0 for none (default: 0). See conv_code_t for the pattern. |
0
|
punct_mask
|
int
|
Bit |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If construction fails. The exception message is |
Examples:
>>> import numpy as np
>>> from doppler.coding import ConvEncoder
>>> e = ConvEncoder([0o171, 0o133], k=7, invert=0x2)
>>> e.encode(np.zeros(8, dtype=np.uint8)).size
16
>>> p = ConvEncoder([0o171, 0o133], k=7, punct_period=3,
... punct_mask=0b100111)
>>> p.encode(np.zeros(6, dtype=np.uint8)).size
8
Methods:
| Name | Description |
|---|---|
__init__ |
|
reset |
Return the register to all-zero, keeping the code. |
encode |
Encode information bits into channel symbols. |
encode_max_out |
Symbols dp_conv_enc_encode writes for n_in input bits. |
state_bytes |
Size in bytes of this object's serialized state. |
get_state |
Serialize this object's mutable state to bytes. |
set_state |
Restore mutable state from a |
destroy |
Release the underlying C resources immediately. |
__enter__ |
Enter a context manager, returning this object. |
__exit__ |
Exit a context manager, releasing the ConvEncoder. |
__init__
¶
__init__(
poly: NDArray[uint32],
k: int = 7,
invert: int = 0,
punct_period: int = 0,
punct_mask: int = 0,
) -> None
reset
¶
Return the register to all-zero, keeping the code.
The boundary between two independent records, not a reconfiguration. The next encode starts from the same state a freshly created encoder is in, which is what makes a reset stream byte-identical to a fresh one.
Examples:
>>> import numpy as np
>>> from doppler.coding import ConvEncoder
>>> e = ConvEncoder([0o171, 0o133], k=7)
>>> e.reset()
encode
¶
encode(
x: NDArray[uint8] | bytes | bytearray | memoryview,
out: NDArray[uint8] | None = None,
) -> NDArray[np.uint8]
Encode information bits into channel symbols.
The register carries across calls, so a long record may be fed in blocks and the symbol sequence is identical to one call — which is the property a standard fixes and a chunked encoder silently breaks.
Outputs are emitted in polynomial order per input bit: for [G1, G2],
out[2i] is G1's symbol for input bit i and out[2i+1] is G2's.
A punctured encoder omits the symbols its pattern deletes, so the
indexing above holds only for the unpunctured code; the pattern
position carries across calls like the register does.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[uint8] | bytes | bytearray | memoryview
|
n_in unpacked input bits, one per byte. |
required |
out
|
NDArray[uint8] | None
|
Receives |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
Symbols written, or 0 if max_out is too small — in which case out is untouched. |
Examples:
>>> import numpy as np
>>> from doppler.coding import ConvEncoder, Viterbi
>>> bits = np.array([1, 0, 1, 1, 0, 0, 1, 0] * 40, dtype=np.uint8)
>>> sym = ConvEncoder([0o171, 0o133], k=7).encode(bits)
>>> llr = np.where(sym, -8.0, 8.0).astype(np.float32)
>>> out = Viterbi([0o171, 0o133], k=7, depth=35).decode(llr)
>>> bool(np.array_equal(out, bits[: out.size]))
True
encode_max_out
¶
Symbols dp_conv_enc_encode writes for n_in input bits.
Exactly n_in * n unpunctured — a convolutional code has no fill and
no latency on the encode side, which is the asymmetry with
dp_viterbi_decode_max_out, where the traceback still owes bits at the
start of a stream. Punctured, it is dp_conv_coded_len from the
encoder's current place in the pattern, so it depends on what has been
encoded already.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_in
|
int
|
Number of input bits. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Symbols that call will write. |
state_bytes
¶
Size in bytes of this object's serialized state.
The exact length get_state returns and set_state requires. It
depends on how the object was constructed (state arrays are sized at
construction), so read it from the instance rather than assuming a
constant.
Raises RuntimeError if the ConvEncoder has already been destroyed.
Returns:
| Type | Description |
|---|---|
int
|
Byte length of one serialized state blob. |
get_state
¶
Serialize this object's mutable state to bytes.
Captures the state that evolves as the object runs, including any value a setter changed after construction, so a blob taken now and restored later resumes from this point. Construction parameters are not restored: restore into an object built the same way. A core may check some of them and refuse a blob built differently.
The blob is opaque and always state_bytes() long. Its layout is an
implementation detail of the C core and is not a stable format across
builds.
Raises RuntimeError if the ConvEncoder has already been destroyed.
Returns:
| Type | Description |
|---|---|
bytes
|
Opaque snapshot, |
set_state
¶
Restore mutable state from a get_state() blob.
Overwrites the live state in place, including any value a setter
changed after construction. Construction parameters are not restored:
restore into an object built the same way. Length is validated against
state_bytes() before the blob is handed to the C core, which may
reject it too: a core may check some construction parameters and refuse
a blob built differently.
Raises TypeError if blob is not bytes, ValueError if its
length differs from state_bytes() or the core rejects it, and
RuntimeError if the ConvEncoder has already been destroyed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blob
|
bytes
|
A |
required |
destroy
¶
Release the underlying C resources immediately.
Ordinarily unnecessary: the resources are freed when the object is garbage-collected. Call this to release them at a definite point instead, or use the object as a context manager, which calls it on exit.
Idempotent: calling it again on an already-released object does
nothing. Every other method raises RuntimeError once it has run.
__enter__
¶
Enter a context manager, returning this object.
Lets a ConvEncoder be used in a with statement so its C resources are
released deterministically on exit rather than at collection time.
Returns:
| Type | Description |
|---|---|
ConvEncoder
|
This same object, not a copy. |
__exit__
¶
__exit__(
exc_type: object | None = ...,
exc: object | None = ...,
tb: object | None = ...,
) -> None
Exit a context manager, releasing the ConvEncoder.
Equivalent to calling destroy(). Returns None, so an exception
raised inside the with body propagates normally; this never
suppresses one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
object | None
|
Exception class, or None. Ignored. |
...
|
exc
|
object | None
|
Exception instance, or None. Ignored. |
...
|
tb
|
object | None
|
Traceback object, or None. Ignored. |
...
|
Deinterleaver
¶
The RECEIVE face of the same interleaver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
int
|
Interleaving depth, as the transmitter used. |
...
|
cols
|
int
|
Units per codeword, as the transmitter used. |
...
|
unit_bits
|
int
|
Bits per interleaved unit, as the transmitter used. |
1
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If construction fails. The exception message is |
Examples:
>>> import numpy as np
>>> from doppler.coding import Interleaver, Deinterleaver
>>> tx = Interleaver(rows=3, cols=4)
>>> rx = Deinterleaver(rows=3, cols=4)
>>> bits = np.arange(12, dtype=np.uint8)
>>> wire = np.asarray(tx.interleave(bits))
>>> np.array_equal(np.asarray(rx.deinterleave(wire)), bits)
True
Methods:
| Name | Description |
|---|---|
__init__ |
|
reset |
No-op; an interleaver carries nothing between calls. |
deinterleave |
Undo |
deinterleave_max_out |
Output bits for n_in input bits — the same number. |
deinterleave_soft |
Undo an interleave over SOFT values -- the receive path that |
deinterleave_soft_max_out |
Output values for n_in soft input values — the same number. |
destroy |
Release the underlying C resources immediately. |
__enter__ |
Enter a context manager, returning this object. |
__exit__ |
Exit a context manager, releasing the Deinterleaver. |
Attributes:
| Name | Type | Description |
|---|---|---|
rows |
int
|
Interleaving depth -- codewords interleaved. |
cols |
int
|
Units per codeword. |
unit_bits |
int
|
Bits per interleaved unit; 1 interleaves bits, 8 octets. |
block_bits |
int
|
Bits in one block -- |
burst_len |
int
|
The longest burst this geometry fully spreads: a burst of up to this |
separation |
int
|
The other half of the link budget -- what |
block_bits
property
¶
Bits in one block -- rows * cols * unit_bits. Every call takes a
whole multiple of this.
burst_len
property
¶
The longest burst this geometry fully spreads: a burst of up to this
many consecutive units on the wire touches each codeword at most once.
An outer code correcting t units per codeword survives a burst of t
* burst_len.
separation
property
¶
The other half of the link budget -- what burst_len spreads a
burst ACROSS. Equal to cols, and named for what it buys rather than
for the matrix it comes from.
reset
¶
No-op; an interleaver carries nothing between calls.
Present because the object surface has it, and honest about why it does nothing: a reset that pretended to clear something would suggest there was something to clear. The geometry is configuration, not state, so it survives — a reset that cleared THAT would leave every later call refusing.
Examples:
>>> import numpy as np
>>> from doppler.coding import Interleaver
>>> il = Interleaver(rows=2, cols=3)
>>> il.reset()
>>> il.block_bits
6
deinterleave
¶
deinterleave(
x: NDArray[uint8] | bytes | bytearray | memoryview,
out: NDArray[uint8] | None = None,
) -> NDArray[np.uint8]
Undo interleave() over the same geometry. De-interleaving a rows
x cols block is interleaving a cols x rows one, so this is the
same kernel with two arguments exchanged -- which is also why a SQUARE
block is its own inverse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[uint8] | bytes | bytearray | memoryview
|
n_in interleaved bits, one bit per byte. |
required |
out
|
NDArray[uint8] | None
|
Where to write n_in bits; must not overlap in. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
n_in, or 0 on a refusal. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the C call writes no output. Its return value is a count, so a
zero-length result is a REFUSAL rather than an empty answer. The
exception message is |
Examples:
>>> import numpy as np
>>> from doppler.coding import Interleaver
>>> il = Interleaver(rows=3, cols=4)
>>> x = np.arange(12, dtype=np.uint8)
>>> y = np.asarray(il.interleave(x))
>>> np.array_equal(np.asarray(il.deinterleave(y)), x)
True
deinterleave_max_out
¶
Output bits for n_in input bits — the same number.
Identical to dp_interleaver_interleave_max_out, and for the same reason: the inverse of a permutation is a permutation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_in
|
int
|
Input length in bits. |
required |
Returns:
| Type | Description |
|---|---|
int
|
n_in. |
Examples:
>>> from doppler.coding import Interleaver
>>> Interleaver(rows=4, cols=8).deinterleave_max_out(32)
32
deinterleave_soft
¶
deinterleave_soft(
x: NDArray[float32], out: NDArray[float32] | None = None
) -> NDArray[np.float32]
Undo an interleave over SOFT values -- the receive path that
matters. DsssBurstReceiver.llrs span the whole frame, and an outer
decoder wants them de-interleaved BEFORE it runs; slicing to hard bits
first throws away the confidence the soft output exists to carry. There
is no interleave_soft, because a transmitter has bits, not LLRs.
dsss_burst_receiver's llrs span the whole frame, and an outer
decoder wants them de-interleaved BEFORE it runs. Slicing to hard bits
first and de-interleaving those throws away the confidence the soft
output exists to carry, which is most of what an outer code is for.
There is no interleave_soft: a transmitter has bits, not LLRs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[float32]
|
n_in soft values, one per interleaved unit-bit. |
required |
out
|
NDArray[float32] | None
|
Where to write n_in values; must not overlap in. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[float32]
|
n_in, or 0 on a refusal. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the C call writes no output. Its return value is a count, so a
zero-length result is a REFUSAL rather than an empty answer. The
exception message is |
Examples:
>>> import numpy as np
>>> from doppler.coding import Interleaver
>>> il = Interleaver(rows=2, cols=3)
>>> llr = np.array([1., 2., 3., 4., 5., 6.], dtype=np.float32)
>>> np.asarray(il.deinterleave_soft(llr)).tolist()
[1.0, 3.0, 5.0, 2.0, 4.0, 6.0]
deinterleave_soft_max_out
¶
destroy
¶
Release the underlying C resources immediately.
Ordinarily unnecessary: the resources are freed when the object is garbage-collected. Call this to release them at a definite point instead, or use the object as a context manager, which calls it on exit.
Idempotent: calling it again on an already-released object does
nothing. Every other method raises RuntimeError once it has run.
__enter__
¶
Enter a context manager, returning this object.
Lets a Deinterleaver be used in a with statement so its C resources
are released deterministically on exit rather than at collection time.
Returns:
| Type | Description |
|---|---|
Deinterleaver
|
This same object, not a copy. |
__exit__
¶
__exit__(
exc_type: object | None = ...,
exc: object | None = ...,
tb: object | None = ...,
) -> None
Exit a context manager, releasing the Deinterleaver.
Equivalent to calling destroy(). Returns None, so an exception
raised inside the with body propagates normally; this never
suppresses one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
object | None
|
Exception class, or None. Ignored. |
...
|
exc
|
object | None
|
Exception instance, or None. Ignored. |
...
|
tb
|
object | None
|
Traceback object, or None. Ignored. |
...
|
Interleaver
¶
Build an interleaver over a rows x cols block of unit_bits units.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
int
|
Interleaving depth; the longest burst fully spread. Must be non-zero. |
...
|
cols
|
int
|
Units per codeword. Must be non-zero. |
...
|
unit_bits
|
int
|
Bits per interleaved unit. 1 interleaves bits; 8 interleaves octets, which is what spreads a burst across the codewords of a symbol-oriented code such as Reed-Solomon over GF(256). Must be non-zero. |
1
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If construction fails. The exception message is |
Examples:
>>> import numpy as np
>>> from doppler.coding import Interleaver
>>> il = Interleaver(rows=3, cols=4)
>>> il.block_bits, il.burst_len, il.separation
(12, 3, 4)
Methods:
| Name | Description |
|---|---|
__init__ |
|
reset |
No-op; an interleaver carries nothing between calls. |
interleave |
Interleave a whole number of blocks: write the input by rows into a |
interleave_max_out |
Output bits for n_in input bits — the same number. |
deinterleave |
Undo |
deinterleave_max_out |
Output bits for n_in input bits — the same number. |
deinterleave_soft |
Undo an interleave over SOFT values -- the receive path that |
deinterleave_soft_max_out |
Output values for n_in soft input values — the same number. |
destroy |
Release the underlying C resources immediately. |
__enter__ |
Enter a context manager, returning this object. |
__exit__ |
Exit a context manager, releasing the Interleaver. |
Attributes:
| Name | Type | Description |
|---|---|---|
rows |
int
|
Interleaving depth -- codewords interleaved. |
cols |
int
|
Units per codeword. |
unit_bits |
int
|
Bits per interleaved unit; 1 interleaves bits, 8 octets. |
block_bits |
int
|
Bits in one block -- |
burst_len |
int
|
The longest burst this geometry fully spreads: a burst of up to this |
separation |
int
|
The other half of the link budget -- what |
block_bits
property
¶
Bits in one block -- rows * cols * unit_bits. Every call takes a
whole multiple of this.
burst_len
property
¶
The longest burst this geometry fully spreads: a burst of up to this
many consecutive units on the wire touches each codeword at most once.
An outer code correcting t units per codeword survives a burst of t
* burst_len.
separation
property
¶
The other half of the link budget -- what burst_len spreads a
burst ACROSS. Equal to cols, and named for what it buys rather than
for the matrix it comes from.
reset
¶
No-op; an interleaver carries nothing between calls.
Present because the object surface has it, and honest about why it does nothing: a reset that pretended to clear something would suggest there was something to clear. The geometry is configuration, not state, so it survives — a reset that cleared THAT would leave every later call refusing.
Examples:
>>> import numpy as np
>>> from doppler.coding import Interleaver
>>> il = Interleaver(rows=2, cols=3)
>>> il.reset()
>>> il.block_bits
6
interleave
¶
interleave(
x: NDArray[uint8] | bytes | bytearray | memoryview,
out: NDArray[uint8] | None = None,
) -> NDArray[np.uint8]
Interleave a whole number of blocks: write the input by rows into a
rows x cols matrix and read it back by columns. Length-preserving.
A length that is not a whole multiple of block_bits is REFUSED rather
than padded -- padding changes the length, and a receiver
de-interleaving the padded block recovers different bits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[uint8] | bytes | bytearray | memoryview
|
n_in bits, one bit per byte. |
required |
out
|
NDArray[uint8] | None
|
Where to write n_in bits; must not overlap in. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
n_in on success, 0 if the length is not a whole number of blocks or out is too small. A partial block is REFUSED rather than padded: padding changes the length, and a receiver that de-interleaved the padded block would recover different bits. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the C call writes no output. Its return value is a count, so a
zero-length result is a REFUSAL rather than an empty answer. The
exception message is |
Examples:
>>> import numpy as np
>>> from doppler.coding import Interleaver
>>> il = Interleaver(rows=3, cols=4)
>>> x = np.arange(12, dtype=np.uint8)
>>> np.asarray(il.interleave(x)).tolist()
[0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11]
interleave_max_out
¶
Output bits for n_in input bits — the same number.
A permutation moves bits and does not add or remove any, so this is the identity. It exists because the binding asks a method how much room its output needs, and answering "the same" is not something a caller should have to know.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_in
|
int
|
Input length in bits. |
required |
Returns:
| Type | Description |
|---|---|
int
|
n_in. |
Examples:
>>> from doppler.coding import Interleaver
>>> Interleaver(rows=4, cols=8).interleave_max_out(32)
32
deinterleave
¶
deinterleave(
x: NDArray[uint8] | bytes | bytearray | memoryview,
out: NDArray[uint8] | None = None,
) -> NDArray[np.uint8]
Undo interleave() over the same geometry. De-interleaving a rows
x cols block is interleaving a cols x rows one, so this is the
same kernel with two arguments exchanged -- which is also why a SQUARE
block is its own inverse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[uint8] | bytes | bytearray | memoryview
|
n_in interleaved bits, one bit per byte. |
required |
out
|
NDArray[uint8] | None
|
Where to write n_in bits; must not overlap in. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
n_in, or 0 on a refusal. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the C call writes no output. Its return value is a count, so a
zero-length result is a REFUSAL rather than an empty answer. The
exception message is |
Examples:
>>> import numpy as np
>>> from doppler.coding import Interleaver
>>> il = Interleaver(rows=3, cols=4)
>>> x = np.arange(12, dtype=np.uint8)
>>> y = np.asarray(il.interleave(x))
>>> np.array_equal(np.asarray(il.deinterleave(y)), x)
True
deinterleave_max_out
¶
Output bits for n_in input bits — the same number.
Identical to dp_interleaver_interleave_max_out, and for the same reason: the inverse of a permutation is a permutation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_in
|
int
|
Input length in bits. |
required |
Returns:
| Type | Description |
|---|---|
int
|
n_in. |
Examples:
>>> from doppler.coding import Interleaver
>>> Interleaver(rows=4, cols=8).deinterleave_max_out(32)
32
deinterleave_soft
¶
deinterleave_soft(
x: NDArray[float32], out: NDArray[float32] | None = None
) -> NDArray[np.float32]
Undo an interleave over SOFT values -- the receive path that
matters. DsssBurstReceiver.llrs span the whole frame, and an outer
decoder wants them de-interleaved BEFORE it runs; slicing to hard bits
first throws away the confidence the soft output exists to carry. There
is no interleave_soft, because a transmitter has bits, not LLRs.
dsss_burst_receiver's llrs span the whole frame, and an outer
decoder wants them de-interleaved BEFORE it runs. Slicing to hard bits
first and de-interleaving those throws away the confidence the soft
output exists to carry, which is most of what an outer code is for.
There is no interleave_soft: a transmitter has bits, not LLRs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[float32]
|
n_in soft values, one per interleaved unit-bit. |
required |
out
|
NDArray[float32] | None
|
Where to write n_in values; must not overlap in. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[float32]
|
n_in, or 0 on a refusal. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the C call writes no output. Its return value is a count, so a
zero-length result is a REFUSAL rather than an empty answer. The
exception message is |
Examples:
>>> import numpy as np
>>> from doppler.coding import Interleaver
>>> il = Interleaver(rows=2, cols=3)
>>> llr = np.array([1., 2., 3., 4., 5., 6.], dtype=np.float32)
>>> np.asarray(il.deinterleave_soft(llr)).tolist()
[1.0, 3.0, 5.0, 2.0, 4.0, 6.0]
deinterleave_soft_max_out
¶
destroy
¶
Release the underlying C resources immediately.
Ordinarily unnecessary: the resources are freed when the object is garbage-collected. Call this to release them at a definite point instead, or use the object as a context manager, which calls it on exit.
Idempotent: calling it again on an already-released object does
nothing. Every other method raises RuntimeError once it has run.
__enter__
¶
Enter a context manager, returning this object.
Lets a Interleaver be used in a with statement so its C resources are
released deterministically on exit rather than at collection time.
Returns:
| Type | Description |
|---|---|
Interleaver
|
This same object, not a copy. |
__exit__
¶
__exit__(
exc_type: object | None = ...,
exc: object | None = ...,
tb: object | None = ...,
) -> None
Exit a context manager, releasing the Interleaver.
Equivalent to calling destroy(). Returns None, so an exception
raised inside the with body propagates normally; this never
suppresses one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
object | None
|
Exception class, or None. Ignored. |
...
|
exc
|
object | None
|
Exception instance, or None. Ignored. |
...
|
tb
|
object | None
|
Traceback object, or None. Ignored. |
...
|
ReedSolomon
¶
Create a codec for the code named by the five arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nroots
|
int
|
Parity symbols per codeword, |
required |
symbol_bits
|
int
|
|
8
|
field_poly
|
int
|
|
29
|
first_root
|
int
|
|
1
|
root_stride
|
int
|
|
1
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If construction fails. The exception message is |
Examples:
>>> from doppler.coding import ReedSolomon
>>> rs = ReedSolomon(nroots=32) # RS(255,223) over the usual GF(256)
>>> rs.n, rs.k, rs.e
(255, 223, 16)
>>> ReedSolomon(nroots=4, symbol_bits=4, field_poly=0b0011).n
15
Methods:
| Name | Description |
|---|---|
__init__ |
|
encode |
Encode |
encode_max_out |
Symbols dp_rs_codec_encode writes for n_in information symbols: a |
decode |
Correct up to |
syndromes |
The |
syndromes_max_out |
Syndromes dp_rs_codec_syndromes writes: |
codeword_ok |
Is this a valid codeword? — every syndrome zero. |
generator |
The |
destroy |
Release the underlying C resources immediately. |
__enter__ |
Enter a context manager, returning this object. |
__exit__ |
Exit a context manager, releasing the ReedSolomon. |
Attributes:
| Name | Type | Description |
|---|---|---|
n |
int
|
Symbols per codeword, |
k |
int
|
Information symbols per codeword, |
e |
int
|
Correctable symbols per codeword, |
nroots |
int
|
Parity symbols per codeword, |
symbol_bits |
int
|
Symbol width |
__init__
¶
__init__(
nroots: int,
symbol_bits: int = 8,
field_poly: int = 29,
first_root: int = 1,
root_stride: int = 1,
) -> None
encode
¶
encode(
x: NDArray[uint8] | bytes | bytearray | memoryview,
out: NDArray[uint8] | None = None,
) -> NDArray[np.uint8]
Encode k information symbols into a whole n-symbol codeword.
Systematic: the information symbols are copied through untouched and
the nroots parity symbols follow them, which is the order they are
transmitted in. dp_rs_encode computes the parity; this places it.
The WHOLE codeword rather than the parity alone, because that is the
unit every other method here takes — dp_rs_codec_decode,
dp_rs_codec_syndromes and dp_rs_codec_codeword_ok all read n symbols,
and a caller who wants the parity by itself can take the last nroots
of the answer. (dp_rs_encode is the other split, and is still there
for a frame assembler that has already placed the information.)
out may alias in — dp_rs_codec_encode (rs, buf, k, buf, n) appends
the parity to a buffer that already holds the information, which is the
call a frame assembler makes and the one dp_rs_encode exists for.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[uint8] | bytes | bytearray | memoryview
|
Exactly |
required |
out
|
NDArray[uint8] | None
|
Receives |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
|
Examples:
>>> import numpy as np
>>> from doppler.coding import ReedSolomon
>>> rs = ReedSolomon(nroots=32)
>>> info = np.arange(rs.k, dtype=np.uint8)
>>> word = rs.encode(info)
>>> word.size, bool(np.array_equal(word[: rs.k], info))
(255, True)
>>> rs.codeword_ok(word)
1
encode_max_out
¶
Symbols dp_rs_codec_encode writes for n_in information symbols: a
whole codeword, n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_in
|
int
|
Ignored: a codeword is |
required |
Returns:
| Type | Description |
|---|---|
int
|
|
decode
¶
Correct up to E symbol errors, IN PLACE.
dp_rs_decode, over the caller's own buffer: the corrected symbols
land in codeword itself, which is why the binding demands a writable
array rather than quietly working on a copy the caller would then
discard.
It either refuses or leaves a codeword. On success the key equation has zeroed every syndrome by construction, so the result passes dp_rs_codec_codeword_ok. On refusal codeword is untouched.
A refusal is not the same claim as "more than E errors". Beyond E a
bounded-distance decoder can land inside another codeword's sphere and
miscorrect — a property of the code, not of this implementation — which
is why this reports a COUNT rather than a verdict, and why frame-level
accounting is the protection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
codeword
|
NDArray[uint8]
|
|
required |
Returns:
| Type | Description |
|---|---|
int
|
Symbols corrected, 0 for an already-valid codeword, -1 when the
word is too far from every codeword to name one, or -2 when
codeword_len is not |
Examples:
>>> import numpy as np
>>> from doppler.coding import ReedSolomon
>>> rs = ReedSolomon(nroots=32)
>>> word = rs.encode(np.arange(rs.k, dtype=np.uint8))
>>> word[3] ^= 0xFF # one symbol, however many bits it moved
>>> word[40] ^= 0x01
>>> rs.decode(word) # corrected in place
2
>>> bool(np.array_equal(word[: rs.k], np.arange(rs.k, dtype=np.uint8)))
True
syndromes
¶
syndromes(
x: NDArray[uint8] | bytes | bytearray | memoryview,
out: NDArray[uint8] | None = None,
) -> NDArray[np.uint8]
The nroots syndromes of an n-symbol word.
All zero is the DEFINING property of the code: it needs no encoder and no decoder to check, which is what makes it usable both as a test oracle and as a receiver's error detector. dp_rs_codec_codeword_ok is this reduced to the one bit most callers want.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[uint8] | bytes | bytearray | memoryview
|
|
required |
out
|
NDArray[uint8] | None
|
Receives |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
|
Examples:
>>> import numpy as np
>>> from doppler.coding import ReedSolomon
>>> rs = ReedSolomon(nroots=32)
>>> word = rs.encode(np.zeros(rs.k, dtype=np.uint8))
>>> bool(rs.syndromes(word).any()) # a codeword has none
False
>>> word[7] ^= 0x20
>>> bool(rs.syndromes(word).any())
True
syndromes_max_out
¶
Syndromes dp_rs_codec_syndromes writes: nroots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_in
|
int
|
Ignored: the syndrome count does not depend on it. |
required |
Returns:
| Type | Description |
|---|---|
int
|
|
codeword_ok
¶
Is this a valid codeword? — every syndrome zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
codeword
|
NDArray[uint8] | bytes | bytearray | memoryview
|
|
required |
Returns:
| Type | Description |
|---|---|
int
|
1 when every syndrome is zero, 0 otherwise — including when
codeword_len is not |
Examples:
>>> import numpy as np
>>> from doppler.coding import ReedSolomon
>>> rs = ReedSolomon(nroots=32)
>>> rs.codeword_ok(np.zeros(rs.n, np.uint8)) # all-zero IS a codeword
1
>>> rs.codeword_ok(np.zeros(rs.n - 1, np.uint8)) # at the right size
0
generator
¶
The nroots + 1 coefficients of g(x), out[i] for x^i.
Exposed because standards PUBLISH them — CCSDS 131.0-B Annex G prints
all 33 for E = 16 — so a caller who has just configured a code from a
document can check that they read the five numbers correctly, against
the document rather than against this implementation.
The caller supplies the buffer rather than being handed one, because
the length is a property of the CODE and not of the call: g(x) has
exactly nroots + 1 coefficients and there is no other number a caller
could ask for. A self-sizing method would carry a count parameter
that means nothing, which is a worse trade than one line of allocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out
|
NDArray[uint8]
|
Receives |
required |
Returns:
| Type | Description |
|---|---|
int
|
|
Examples:
>>> import numpy as np
>>> from doppler.coding import ReedSolomon
>>> rs = ReedSolomon(nroots=32, field_poly=0x87, first_root=112,
... root_stride=11) # CCSDS 131.0-B 4.3
>>> g = np.empty(rs.nroots + 1, np.uint8)
>>> rs.generator(g) # Annex G prints all 33
33
>>> int(g[0]), int(g[-1])
(1, 1)
destroy
¶
Release the underlying C resources immediately.
Ordinarily unnecessary: the resources are freed when the object is garbage-collected. Call this to release them at a definite point instead, or use the object as a context manager, which calls it on exit.
Idempotent: calling it again on an already-released object does
nothing. Every other method raises RuntimeError once it has run.
__enter__
¶
Enter a context manager, returning this object.
Lets a ReedSolomon be used in a with statement so its C resources are
released deterministically on exit rather than at collection time.
Returns:
| Type | Description |
|---|---|
ReedSolomon
|
This same object, not a copy. |
__exit__
¶
__exit__(
exc_type: object | None = ...,
exc: object | None = ...,
tb: object | None = ...,
) -> None
Exit a context manager, releasing the ReedSolomon.
Equivalent to calling destroy(). Returns None, so an exception
raised inside the with body propagates normally; this never
suppresses one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
object | None
|
Exception class, or None. Ignored. |
...
|
exc
|
object | None
|
Exception instance, or None. Ignored. |
...
|
tb
|
object | None
|
Traceback object, or None. Ignored. |
...
|
Viterbi
¶
Build a decoder for the code the polynomials describe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly
|
NDArray[uint32]
|
Generator polynomials, one per output. The array IS the code;
|
required |
k
|
int
|
Constraint length, 2 to |
7
|
invert
|
int
|
Bitmask over outputs: bit j set means output j is transmitted inverted (default: 0; CCSDS sets bit 1). |
0
|
depth
|
int
|
Traceback depth in information bits, >= 1 (default: 35). |
35
|
punct_period
|
int
|
Input bits per puncturing pattern, 0 for none (default: 0). Must be the encoder's. |
0
|
punct_mask
|
int
|
Bit |
0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If construction fails. The exception message is |
Examples:
>>> import numpy as np
>>> from doppler.coding import Viterbi
>>> v = Viterbi([0o171, 0o133], k=7, depth=35)
>>> v.decode(np.zeros(8, dtype=np.float32)).dtype
dtype('uint8')
Methods:
| Name | Description |
|---|---|
__init__ |
|
reset |
Return to the all-zero start state, discarding the traceback. |
decode |
Decode soft channel symbols into information bits. |
decode_max_out |
Bits dp_viterbi_decode will emit for n_in soft symbols. |
state_bytes |
Size in bytes of this object's serialized state. |
get_state |
Serialize this object's mutable state to bytes. |
set_state |
Restore mutable state from a |
destroy |
Release the underlying C resources immediately. |
__enter__ |
Enter a context manager, returning this object. |
__exit__ |
Exit a context manager, releasing the Viterbi. |
__init__
¶
__init__(
poly: NDArray[uint32],
k: int = 7,
invert: int = 0,
depth: int = 35,
punct_period: int = 0,
punct_mask: int = 0,
) -> None
reset
¶
Return to the all-zero start state, discarding the traceback.
The code and the depth are unchanged — this is the boundary between two independent captures, not a reconfiguration. The next decode refills the traceback before it emits, exactly as after create, and the all-zero state is given the winning metric, matching an encoder that starts from a reset register.
Examples:
decode
¶
Decode soft channel symbols into information bits.
The input carries one value per channel symbol, in the convention
mpsk_soft_demap produces: L = log(P(0)/P(1)), so positive means
symbol 0. The branch metric for an expected symbol e is +L when e
== 0 and -L otherwise, and the survivor maximises the sum — which
makes the decoder agree with mpsk_demap on hard decisions by
construction rather than by a second convention.
A maximum-likelihood path cannot move when every metric is scaled by a positive constant, so the LLRs need no accurate scaling — a caller with no SNR estimate may pass unscaled values.
Streaming: state carries across calls, so a long capture may be fed in
blocks and the bits come out continuously. The first depth - 1
branches of a stream produce no output — the traceback walks depth -
1 steps back, so a decision needs that many branches BEHIND it — and
thereafter one bit is emitted per n symbols consumed.
dp_viterbi_decode_max_out is the same statement as arithmetic, and is
what a caller should size a buffer with rather than repeating this
sentence: they disagreed by one until a test asserted the count against
a literal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[float32]
|
Log-likelihood ratios, one per TRANSMITTED channel symbol. n_in
must end on a whole input bit's symbols: a multiple of |
required |
out
|
NDArray[uint8] | None
|
Receives the decoded information bits, one per byte. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
Bits written, which may be 0 while the traceback fills. |
Examples:
>>> import numpy as np
>>> from doppler.coding import Viterbi
>>> v = Viterbi([0o171, 0o133], k=7, depth=35)
>>> llr = np.array([2.0, -2.0] * 128, dtype=np.float32)
>>> bits = v.decode(llr)
>>> set(np.unique(bits)) <= {0, 1}
True
decode_max_out
¶
Bits dp_viterbi_decode will emit for n_in soft symbols.
Accounts for the fill still owed at the start of a stream, so a caller
can size a buffer exactly rather than conservatively. For a punctured
code the step count comes from the pattern, not from n_in / n, so it
depends on where in the pattern the decoder is. A length that ends
mid-bit gives 0, which is also what decode will return for it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_in
|
int
|
Number of soft symbols the next call would be given. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Bits that call would write. |
state_bytes
¶
Size in bytes of this object's serialized state.
The exact length get_state returns and set_state requires. It
depends on how the object was constructed (state arrays are sized at
construction), so read it from the instance rather than assuming a
constant.
Raises RuntimeError if the Viterbi has already been destroyed.
Returns:
| Type | Description |
|---|---|
int
|
Byte length of one serialized state blob. |
get_state
¶
Serialize this object's mutable state to bytes.
Captures the state that evolves as the object runs, including any value a setter changed after construction, so a blob taken now and restored later resumes from this point. Construction parameters are not restored: restore into an object built the same way. A core may check some of them and refuse a blob built differently.
The blob is opaque and always state_bytes() long. Its layout is an
implementation detail of the C core and is not a stable format across
builds.
Raises RuntimeError if the Viterbi has already been destroyed.
Returns:
| Type | Description |
|---|---|
bytes
|
Opaque snapshot, |
set_state
¶
Restore mutable state from a get_state() blob.
Overwrites the live state in place, including any value a setter
changed after construction. Construction parameters are not restored:
restore into an object built the same way. Length is validated against
state_bytes() before the blob is handed to the C core, which may
reject it too: a core may check some construction parameters and refuse
a blob built differently.
Raises TypeError if blob is not bytes, ValueError if its
length differs from state_bytes() or the core rejects it, and
RuntimeError if the Viterbi has already been destroyed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blob
|
bytes
|
A |
required |
destroy
¶
Release the underlying C resources immediately.
Ordinarily unnecessary: the resources are freed when the object is garbage-collected. Call this to release them at a definite point instead, or use the object as a context manager, which calls it on exit.
Idempotent: calling it again on an already-released object does
nothing. Every other method raises RuntimeError once it has run.
__enter__
¶
Enter a context manager, returning this object.
Lets a Viterbi be used in a with statement so its C resources are
released deterministically on exit rather than at collection time.
Returns:
| Type | Description |
|---|---|
Viterbi
|
This same object, not a copy. |
__exit__
¶
__exit__(
exc_type: object | None = ...,
exc: object | None = ...,
tb: object | None = ...,
) -> None
Exit a context manager, releasing the Viterbi.
Equivalent to calling destroy(). Returns None, so an exception
raised inside the with body propagates normally; this never
suppresses one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
object | None
|
Exception class, or None. Ignored. |
...
|
exc
|
object | None
|
Exception instance, or None. Ignored. |
...
|
tb
|
object | None
|
Traceback object, or None. Ignored. |
...
|