Contents
Sionna Forward Error Correction (fec/): LDPC, Polar, Turbo, and Convolutional Codes
fec/ handles bit-level reliability: from CRC checking, channel coding, interleaving and scrambling, all the way to soft-information processing on the decoder side. All codecs are Block subclasses; inputs and outputs are uniformly float32 0/1 tensors, and the decoders consume LLRs.
The LLR sign convention matches mapping.py:
i.e., a positive LLR favors deciding 1, a negative LLR favors deciding 0.
1. Top-Level Files
| File | Function | Main public classes/functions |
|---|---|---|
__init__.py | FEC package entry | Exports the conv/ldpc/linear/polar/turbo submodules and all codecs, CRC, scrambling, EXIT tools, etc. |
coding.py | Code construction, matrices and I/O utilities | load_parity_check_examples, alist2mat, load_alist, make_systematic, gm2pcm, pcm2gm, verify_gm_pcm, generate_reg_ldpc |
crc.py | Cyclic redundancy check | CRCEncoder(Block), CRCDecoder(Block) (CRC24A/B/C, CRC16, CRC11, CRC6) |
interleaving.py | Interleaving | RowColumnInterleaver, RandomInterleaver, Turbo3GPPInterleaver (max length 6144), Deinterleaver |
scrambling.py | Scrambling/descrambling | Scrambler, TB5GScrambler (NR PUSCH/PDSCH), Descrambler |
plotting.py | EXIT chart visualization | plot_trajectory, plot_exit_chart, get_exit_analytic |
utils.py | FEC utility functions and blocks | GaussianPriorSource, llr2mi, j_fun, j_fun_inv, bin2int, int2bin, int_mod_2 (also re-exports the plotting/coding symbols) |
In coding.py, gm2pcm / pcm2gm convert between generator matrices and parity-check matrices; combined with make_systematic you can put any code into systematic form — handy when doing manual code design.
The j_fun / j_fun_inv in utils.py are the J functions used for EXIT analysis, mapping mutual information to variance under the Gaussian approximation.
2. conv/: Convolutional Codes
| File | Function | Main public classes/functions |
|---|---|---|
__init__.py | Convolutional-code module entry | Exports ConvEncoder, ViterbiDecoder, BCJRDecoder, polynomial_selector, resolve_gen_poly, Trellis |
encoding.py | Convolutional encoding | ConvEncoder(Block): rate = 1/n, supports gen_poly, rsc, terminate |
decoding.py | Viterbi and BCJR decoding | ViterbiDecoder(Block), BCJRDecoder(Block) |
utils.py | Convolutional-code utilities | polynomial_selector, resolve_gen_poly, Trellis (state transition/output tables) |
rsc=True gives a recursive systematic convolutional code — exactly the form of the component codes used in Turbo codes, so the Turbo encoder directly reuses the convolutional-code utilities. Trellis expands the generator polynomials into a state-transition table, the shared infrastructure of Viterbi/BCJR.
3. ldpc/: LDPC Codes
| File | Function | Main public classes/functions |
|---|---|---|
__init__.py | LDPC module entry | Exports codes, LDPC5GEncoder, LDPCBPDecoder, LDPC5GDecoder, and the various update functions and callbacks |
encoding.py | LDPC encoding (3GPP TS 38.212) | LDPC5GEncoder(Block): incl. rate matching, QM interleaving, multi-redundancy-version HARQ-IR |
decoding.py | LDPC belief propagation decoding | LDPCBPDecoder(Block), LDPC5GDecoder(LDPCBPDecoder); update functions vn_update_sum, cn_update_tanh, cn_update_phi, cn_update_minsum, cn_update_offset_minsum, etc. |
utils.py | LDPC decoder callbacks | EXITCallback, DecoderStatisticsCallback, WeightedBPCallback |
codes/__init__.py | Package holding data files (no exports) | 5G_bg1.csv, 5G_bg2.csv, example_codes.npy |
LDPC decoding is solving the parity-check equations
iteratively under belief propagation. This implementation provides several check-node update rules: cn_update_tanh (sum-product algorithm), cn_update_phi, cn_update_minsum, and cn_update_offset_minsum (offset min-sum). Min-sum-style algorithms have lower complexity but need an offset for compensation; sum-product is exact but involves products of tanh, which requires numerical care.
5G_bg1.csv / 5G_bg2.csv are the two base graphs defined in 38.212; LDPC5GEncoder selects between them automatically according to the code rate and the information length.
4. polar/: Polar Codes
| File | Function | Main public classes/functions |
|---|---|---|
__init__.py | Polar module entry | Exports the encoders/decoders and utility functions |
encoding.py | Polar encoding (incl. 5G rate matching and CRC) | PolarEncoder(Block), Polar5GEncoder(PolarEncoder) |
decoding.py | SC / SCL / BP decoding | PolarSCDecoder, PolarSCLDecoder, PolarBPDecoder, Polar5GDecoder |
utils.py | Polar utilities | generate_5g_ranking, generate_polar_transform_mat, generate_rm_code, generate_dense_polar |
codes/__init__.py | Package holding data files (no exports) | polar_5G.csv |
generate_5g_ranking produces the channel-reliability ranking prescribed by the 5G specification, and the sub-channel allocation is entirely determined by it. The decoders come in three tiers: SC is the simplest, SCL keeps a list to retain the best path, and BP runs belief propagation — each with its own performance/complexity trade-off.
5. turbo/: Turbo Codes
| File | Function | Main public classes/functions |
|---|---|---|
__init__.py | Turbo module entry | Exports TurboEncoder, TurboDecoder, TurboTermination, puncture_pattern, polynomial_selector, coeffs |
encoding.py | Turbo encoding | TurboEncoder(Block): two rate-1/2 convolutional encoders forming a rate-1/3 code, puncturable to rate-1/2 |
decoding.py | Turbo decoding | TurboDecoder(Block): iterative decoding based on two BCJR component decoders |
utils.py | Turbo utilities | polynomial_selector, puncture_pattern, TurboTermination(Object) |
coeffs/__init__.py | Package holding interleaver coefficient files (no exports) | turbo_coeffs.csv |
Turbo decoding is an iterative structure where “two BCJR decoders feed each other extrinsic information”, and num_iter controls the number of iterations. The interleaver coefficients (turbo_coeffs.csv) determine the degree of randomization between the two component codes and directly affect iterative convergence.
6. linear/: Linear Codes
| File | Function | Main public classes |
|---|---|---|
__init__.py | Linear-code module entry | Exports LinearEncoder, OSDecoder |
encoding.py | Linear-code encoding | LinearEncoder(Block): encodes from a generator matrix or a parity-check matrix (is_pcm=True) |
decoding.py | Linear-code decoding | OSDecoder(Block): ordered-statistics decoding (OSD), approaching maximum likelihood |
The linear-code part is a “general container”: any code works, as long as you provide a generator matrix or a parity-check matrix. OSDecoder uses ordered-statistics decoding (OSD) to approach ML performance, which is very useful for short codes.
7. Usage Examples
CRC encoding and checking:
import torch
from sionna.phy.fec.crc import CRCEncoder, CRCDecoder
encoder = CRCEncoder("CRC24A")
decoder = CRCDecoder(encoder)
bits = torch.randint(0, 2, (10, 100), dtype=torch.float32)
encoded = encoder(bits) # [10, 124]
decoded, crc_valid = decoder(encoded) # decoded: [10, 100]
LDPC 5G encoding/decoding (with HARQ redundancy versions):
import torch
from sionna.phy.fec.ldpc import LDPC5GEncoder, LDPC5GDecoder
encoder = LDPC5GEncoder(k=100, n=200)
decoder = LDPC5GDecoder(encoder, num_iter=20)
u = torch.randint(0, 2, (10, 100), dtype=torch.float32)
c = encoder(u) # [10, 200]
c_harq = encoder(u, rv=[0, 2]) # HARQ: output multiple redundancy versions [10, 2, 200]
llr = 2.0 * (2.0 * c - 1.0) # Lossless LLRs
u_hat = decoder(llr)
Here rv=[0, 2] generates two redundancy versions in one call, which is the HARQ incremental redundancy (IR) usage. llr = 2.0 * (2.0 * c - 1.0) maps the bits 0/1 to -2/+2 as “lossless” soft information, used to verify the codec chain itself in isolation.
Polar 5G encoding/decoding (SCL):
import torch
from sionna.phy.fec.polar import Polar5GEncoder, Polar5GDecoder
encoder = Polar5GEncoder(k=100, n=200)
decoder = Polar5GDecoder(encoder, dec_type="SCL", list_size=8)
u = torch.randint(0, 2, (10, 100), dtype=torch.float32)
c = encoder(u)
llr = 20.0 * (2.0 * c - 1.0)
u_hat = decoder(llr)
Convolutional / Turbo codes:
import torch
from sionna.phy.fec.conv import ConvEncoder, ViterbiDecoder
from sionna.phy.fec.turbo import TurboEncoder, TurboDecoder
enc = ConvEncoder(rate=0.5, constraint_length=5)
dec = ViterbiDecoder(rate=0.5, constraint_length=5)
u = torch.randint(0, 2, (10, 100), dtype=torch.float32)
u_hat = dec(torch.randn(10, 200)) # Channel LLRs as input
tenc = TurboEncoder(rate=1/3, constraint_length=4, terminate=True)
tdec = TurboDecoder(tenc, num_iter=6)
tc = tenc(torch.randint(0, 2, (10, 40), dtype=torch.float32))
tu_hat = tdec(torch.randn_like(tc))
Scrambling and interleaving:
import torch
from sionna.phy.fec.scrambling import TB5GScrambler
from sionna.phy.fec.interleaving import RowColumnInterleaver
scrambler = TB5GScrambler(n_rnti=1, n_id=1)
bits = torch.randint(0, 2, (10, 100), dtype=torch.float32)
scrambled = scrambler(bits)
unscrambled = scrambler(scrambled) # Reusing the same instance descrambles
interleaver = RowColumnInterleaver(row_depth=4)
y = interleaver(torch.arange(12).reshape(1, 12).float())
Note that a single TB5GScrambler instance can both scramble and descramble — because it internally rebuilds the same sequence from n_rnti/n_id, and XOR-ing twice restores the original.
8. Summary
Suggested reading order for fec/: crc.py → conv/ → ldpc/ → polar/ → turbo/ → linear/.
- CRC is the simplest — start with the intuition of “
Block+ encoder/decoder pairs”; Trellisinconv/is the foundation for Turbo codes later and is worth reading thoroughly;ldpc/has the most code; focus on the differences between thecn_update_*update functions;linear/is an escape hatch for custom codes; look at it when needed.
The next post moves on to mimo/, to see how this soft information is used once it enters the detectors.
Comments
Loading comments…
Want to leave a note? Sign in first — comments are open to every signed-in reader.