Coding
softlora.coding
calc_lora_crc16(payload)
Recompute the LoRa CRC-16 (CCITT-16, poly 0x1021) for a payload.
Returns the two CRC bytes that must accompany payload for the
decoder's CRC check to pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
array_like
|
Payload bytes (without the CRC). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The 2 CRC bytes (uint8). |
Source code in softlora/coding.py
calc_payload_sym_num(payload_len, has_crc, sf, cr, ldro=False, impl_header=False)
Number of symbols a packet of payload_len bytes occupies.
Includes the 8 header symbols (explicit header) or the in-header data
symbols (implicit header), plus the cr + 4-symbol coding blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload_len
|
int
|
Payload length in bytes. |
required |
has_crc
|
bool
|
Whether the packet carries a 2-byte CRC. |
required |
sf
|
int
|
Spreading factor. |
required |
cr
|
int
|
Code rate (1-4). |
required |
ldro
|
bool
|
Low-data-rate optimization. |
False
|
impl_header
|
bool
|
Implicit header (no header transmitted). |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Total number of symbols. |
Source code in softlora/coding.py
decode(data_symbols, sf=10, impl_header=False, forced_payload_len=None, forced_has_crc=True, forced_cr=1)
Decode a full payload from its symbol bins.
Handles both explicit headers (the first 8 symbols carry length / CRC / code rate) and implicit headers (forced parameters). Returns the payload and CRC bytes together with the decoded header information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_symbols
|
array_like
|
Demodulated symbol bins (header + payload). |
required |
sf
|
int
|
Spreading factor. |
10
|
impl_header
|
bool
|
Implicit header mode. |
False
|
forced_payload_len
|
int or None
|
Payload length when |
None
|
forced_has_crc
|
bool
|
CRC presence when |
True
|
forced_cr
|
int
|
Code rate when |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
payload |
ndarray
|
Decoded payload bytes (uint8). |
crc_bytes |
ndarray
|
The 2 CRC bytes (empty when the packet has no CRC). |
info |
dict
|
|
Source code in softlora/coding.py
decode_header(hdr_syms, sf)
Decode the 8-symbol explicit header.
Extracts the payload length, CRC presence flag and code rate. The header
checksum is computed and compared but a mismatch is not warned on -- it is
surfaced through the decode result (crc_valid / error text) instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdr_syms
|
array_like
|
The 8 demodulated header symbols. |
required |
sf
|
int
|
Spreading factor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
payload_len |
int
|
Declared payload length in bytes. |
has_crc |
bool
|
Whether the packet carries a CRC. |
cr |
int
|
Code rate (1-4). |
hdr_nib |
ndarray
|
The decoded header nibbles. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When the header cannot be decoded or declares an invalid length/rate. |
Source code in softlora/coding.py
decode_symbols(symbols, sf, cr, is_header, ldro=False)
Full symbol decode: Gray -> deinterleave -> Hamming -> nibbles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols
|
array_like
|
Demodulated symbol bins. |
required |
sf
|
int
|
Spreading factor. |
required |
cr
|
int
|
Code rate (1-4). |
required |
is_header
|
bool
|
Header block (uses the 4/5 header code). |
required |
ldro
|
bool
|
Low-data-rate optimization. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Decoded data nibbles (uint8). |
Source code in softlora/coding.py
deinterleave(symbols, sf, cr, is_header, ldro=False)
Undo the LoRa symbol interleaver for one coding block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols
|
array_like
|
Demodulated (Gray-decoded) symbol bins for one |
required |
sf
|
int
|
Spreading factor. |
required |
cr
|
int
|
Code rate (1-4). |
required |
is_header
|
bool
|
Header blocks use |
required |
ldro
|
bool
|
Low-data-rate optimization. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The deinterleaved coded bytes (one per effective symbol). |
Source code in softlora/coding.py
dewhiten(nibbles, payload_len, crc_presence=False)
De-whiten decoded nibbles into payload (and CRC) bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nibbles
|
array_like
|
Decoded data nibbles (payload region is XORed with the whitening sequence; the CRC nibbles are read raw). |
required |
payload_len
|
int
|
Expected payload length in bytes. |
required |
crc_presence
|
bool
|
Whether the packet carries a 2-byte CRC. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
payload |
ndarray
|
Decoded payload bytes (uint8). |
crc_bytes |
ndarray
|
The 2 CRC bytes (empty when |
Raises:
| Type | Description |
|---|---|
ValueError
|
When fewer nibbles than needed are supplied. |
Source code in softlora/coding.py
gray_coding_rx(symbols, sf, is_header=False, ldro=False)
Apply the LoRa Gray decode (RX side) to symbol bins.
Undoes the TX Gray code: symbol - 1 (data frames) or symbol // 4
(header/LDRO frames), then x ^ (x >> 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbols
|
array_like
|
Demodulated symbol bins. |
required |
sf
|
int
|
Spreading factor. |
required |
is_header
|
bool
|
Header frames use the |
False
|
ldro
|
bool
|
Low-data-rate optimization frames behave like headers. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Gray-decoded symbol values (uint16). |
Source code in softlora/coding.py
hamming_decode(codewords, cr, is_header)
Decode a block of Hamming codewords into data nibbles.
Each codeword is cr + 4 (or 8 for headers) bits wide. Single-bit
errors are corrected via the syndrome; code rate 4 additionally enforces
even parity over the whole codeword.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
codewords
|
array_like
|
Deinterleaved coded bytes. |
required |
cr
|
int
|
Code rate (1-4); headers always use 4/5. |
required |
is_header
|
bool
|
Whether this is a header block. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Decoded data nibbles (uint8). |
Source code in softlora/coding.py
nibbles_to_bytes(nibbles)
Pack nibbles into bytes (low nibble first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nibbles
|
array_like
|
Data nibbles. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Packed bytes (uint8). |
Source code in softlora/coding.py
verify_header_checksum(hdr_nib)
True when the 5-bit explicit-header checksum matches the header.