Contents

Sionna Forward Error Correction (fec/): LDPC, Polar, Turbo, and Convolutional Codes

Wireless Fundamentals
#Sionna#channel-coding#LDPC#Polar

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:

L(b)=ln⁡P(b=1∣y)P(b=0∣y)L(b) = \ln \frac{P(b = 1 \mid y)}{P(b = 0 \mid y)}

i.e., a positive LLR favors deciding 1, a negative LLR favors deciding 0.

1. Top-Level Files

FileFunctionMain public classes/functions
__init__.pyFEC package entryExports the conv/ldpc/linear/polar/turbo submodules and all codecs, CRC, scrambling, EXIT tools, etc.
coding.pyCode construction, matrices and I/O utilitiesload_parity_check_examples, alist2mat, load_alist, make_systematic, gm2pcm, pcm2gm, verify_gm_pcm, generate_reg_ldpc
crc.pyCyclic redundancy checkCRCEncoder(Block), CRCDecoder(Block) (CRC24A/B/C, CRC16, CRC11, CRC6)
interleaving.pyInterleavingRowColumnInterleaver, RandomInterleaver, Turbo3GPPInterleaver (max length 6144), Deinterleaver
scrambling.pyScrambling/descramblingScrambler, TB5GScrambler (NR PUSCH/PDSCH), Descrambler
plotting.pyEXIT chart visualizationplot_trajectory, plot_exit_chart, get_exit_analytic
utils.pyFEC utility functions and blocksGaussianPriorSource, 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

FileFunctionMain public classes/functions
__init__.pyConvolutional-code module entryExports ConvEncoder, ViterbiDecoder, BCJRDecoder, polynomial_selector, resolve_gen_poly, Trellis
encoding.pyConvolutional encodingConvEncoder(Block): rate = 1/n, supports gen_poly, rsc, terminate
decoding.pyViterbi and BCJR decodingViterbiDecoder(Block), BCJRDecoder(Block)
utils.pyConvolutional-code utilitiespolynomial_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

FileFunctionMain public classes/functions
__init__.pyLDPC module entryExports codes, LDPC5GEncoder, LDPCBPDecoder, LDPC5GDecoder, and the various update functions and callbacks
encoding.pyLDPC encoding (3GPP TS 38.212)LDPC5GEncoder(Block): incl. rate matching, QM interleaving, multi-redundancy-version HARQ-IR
decoding.pyLDPC belief propagation decodingLDPCBPDecoder(Block), LDPC5GDecoder(LDPCBPDecoder); update functions vn_update_sum, cn_update_tanh, cn_update_phi, cn_update_minsum, cn_update_offset_minsum, etc.
utils.pyLDPC decoder callbacksEXITCallback, DecoderStatisticsCallback, WeightedBPCallback
codes/__init__.pyPackage holding data files (no exports)5G_bg1.csv, 5G_bg2.csv, example_codes.npy

LDPC decoding is solving the parity-check equations

HcT=0\mathbf{H}\mathbf{c}^{\mathrm{T}} = \mathbf{0}

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

FileFunctionMain public classes/functions
__init__.pyPolar module entryExports the encoders/decoders and utility functions
encoding.pyPolar encoding (incl. 5G rate matching and CRC)PolarEncoder(Block), Polar5GEncoder(PolarEncoder)
decoding.pySC / SCL / BP decodingPolarSCDecoder, PolarSCLDecoder, PolarBPDecoder, Polar5GDecoder
utils.pyPolar utilitiesgenerate_5g_ranking, generate_polar_transform_mat, generate_rm_code, generate_dense_polar
codes/__init__.pyPackage 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

FileFunctionMain public classes/functions
__init__.pyTurbo module entryExports TurboEncoder, TurboDecoder, TurboTermination, puncture_pattern, polynomial_selector, coeffs
encoding.pyTurbo encodingTurboEncoder(Block): two rate-1/2 convolutional encoders forming a rate-1/3 code, puncturable to rate-1/2
decoding.pyTurbo decodingTurboDecoder(Block): iterative decoding based on two BCJR component decoders
utils.pyTurbo utilitiespolynomial_selector, puncture_pattern, TurboTermination(Object)
coeffs/__init__.pyPackage 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

FileFunctionMain public classes
__init__.pyLinear-code module entryExports LinearEncoder, OSDecoder
encoding.pyLinear-code encodingLinearEncoder(Block): encodes from a generator matrix or a parity-check matrix (is_pcm=True)
decoding.pyLinear-code decodingOSDecoder(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”;
  • Trellis in conv/ is the foundation for Turbo codes later and is worth reading thoroughly;
  • ldpc/ has the most code; focus on the differences between the cn_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…

    ← Back to home