LDPC5GEncoder#

class sionna.phy.fec.ldpc.LDPC5GEncoder(k: int, n: int, num_bits_per_symbol: int | None = None, bg: str | None = None, precision: str | None = None, device: str | None = None, **kwargs)[source]#

Bases: sionna.phy.block.Block

5G NR LDPC Encoder following the 3GPP 38.212 including rate-matching.

The implementation follows the 3GPP NR Initiative [3GPPTS38212].

Parameters:
  • k (int) – Number of information bits per codeword.

  • n (int) – Desired codeword length.

  • num_bits_per_symbol (int | None) – Number of bits per QAM symbol. If provided, the codeword will be interleaved after rate-matching as specified in Sec. 5.4.2.2 in [3GPPTS38212].

  • bg (str | None) – Basegraph to be used for the code construction. If None, the encoder will automatically select the basegraph according to [3GPPTS38212].

  • precision (str | None) – Precision used for internal calculations and outputs. If None, precision is used.

  • device (str | None) – Device for computation (e.g., ‘cpu’, ‘cuda:0’). If None, device is used.

Inputs:
  • bits – […, k], torch.float. Binary tensor containing the information bits to be encoded.

  • rvNone | list of int. List of redundancy version indices (0–3) for HARQ-IR rate-matching per TS 38.212 Table 5.4.2.1-2. If None (default), standard RV 0 encoding is used and the output shape is [..., n]. When provided, each RV produces an independent rate-matched codeword and the output shape becomes [..., len(rv), n].

Outputs:

cw – […, n] or […, len(rv), n], torch.float. Encoded codeword bits. Same shape as the input with the last dimension replaced by n. When rv is provided, an additional dimension of size len(rv) is inserted before the codeword dimension.

Notes

As specified in [3GPPTS38212], the encoder also performs rate-matching (puncturing and shortening). Thus, the corresponding decoder needs to invert these operations, i.e., must be compatible with the 5G encoding scheme.

Examples

import torch
from sionna.phy.fec.ldpc import LDPC5GEncoder

# Create encoder for k=100 information bits and n=200 codeword bits
encoder = LDPC5GEncoder(k=100, n=200)

# Generate random information bits
u = torch.randint(0, 2, (10, 100), dtype=torch.float32)
c = encoder(u)
print(c.shape)
# [10, 200]

# HARQ: produce two redundancy versions
c_harq = encoder(u, rv=[0, 2])
print(c_harq.shape)
# [10, 2, 200]

Attributes

property k: int#

Number of input information bits.

property n: int#

Number of output codeword bits.

property coderate: float#

Coderate of the LDPC code after rate-matching.

property k_ldpc: int#

Number of LDPC information bits after rate-matching.

property n_ldpc: int#

Number of LDPC codeword bits before rate-matching.

property pcm: scipy.sparse._csr.csr_matrix#

Parity-check matrix for given code parameters.

property z: int#

Lifting factor of the basegraph.

property num_bits_per_symbol: int | None#

Modulation order used for the rate-matching output interleaver.

property k_filler: int#

Number of filler bits added to pad k to k_ldpc.

property n_cb: int#

Circular buffer length (excludes first 2Z).

property n_cb_comp: int#

Compressed circular buffer length (excludes first 2Z and fillers).

property rv_starts: List[int]#

RV start positions k0 from TS 38.212 Table 5.4.2.1-2.

Returns a list of length 4 indexed by rv_id. Values are in spec buffer space (after the first 2Z punctured positions, before filler removal).

\[k_0 = \left\lfloor \frac{\text{coeff} \cdot N_{cb}} {N_{\text{cols}} \cdot Z_c} \right\rfloor \cdot Z_c\]

where \(N_{\text{cols}}\) is 66 (BG1) or 50 (BG2). Currently assumes I_LBRM=0, i.e., N_cb = N.

property out_int: torch.Tensor#

Output interleaver sequence as defined in 5.4.2.2.

property out_int_inv: torch.Tensor#

Inverse output interleaver sequence as defined in 5.4.2.2.

Methods

get_start_positions_comp(rv_list: List[int]) List[int][source]#

Return compressed-buffer start positions for a list of RV indices.

Parameters:

rv_list (List[int]) – List of RV indices (each 0–3).

Output:

Corresponding start positions in the compressed RM buffer.

generate_out_int(n: int, num_bits_per_symbol: int) Tuple[numpy.ndarray, numpy.ndarray][source]#

Generates LDPC output interleaver sequence as defined in Sec 5.4.2.2 in [3GPPTS38212].

Parameters:
  • n (int) – Desired output sequence length.

  • num_bits_per_symbol (int) – Number of bits per QAM symbol, i.e., the modulation order.

Notes

The interleaver pattern depends on the modulation order and helps to reduce dependencies in bit-interleaved coded modulation (BICM) schemes combined with higher order modulation.

build(input_shape: tuple, **kwargs) None[source]#

Build block and check for valid input shapes.

Parameters:

input_shape (tuple)