LinearEncoder#

class sionna.phy.fec.linear.LinearEncoder(enc_mat: numpy.ndarray, *, is_pcm: bool = False, precision: str | None = None, device: str | None = None, **kwargs)[source]#

Bases: sionna.phy.block.Block

Linear binary encoder for a given generator or parity-check matrix.

If is_pcm is True, enc_mat is interpreted as parity-check matrix and internally converted to a corresponding generator matrix.

Parameters:
  • enc_mat (numpy.ndarray) – Binary generator matrix of shape [k, n]. If is_pcm is True, enc_mat is interpreted as parity-check matrix of shape [n-k, n].

  • is_pcm (bool) – If True, the enc_mat is interpreted as parity-check matrix instead of a generator matrix.

  • 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:

info_bits – […, k], torch.float or torch.int. Binary tensor containing the information bits.

Outputs:

c – […, n], same dtype as info_bits. Binary tensor containing codewords with same shape as inputs, except the last dimension changes to […, n].

Notes

If is_pcm is True, this block uses pcm2gm() to find the generator matrix for encoding. Please note that this imposes a few constraints on the provided parity-check matrix such as full rank and it must be binary.

Note that this encoder is generic for all binary linear block codes and, thus, cannot implement any code specific optimizations. As a result, the encoding complexity is \(O(k^2)\). Please consider code specific encoders such as the Polar5GEncoder or LDPC5GEncoder for an improved encoding performance.

Examples

import torch
from sionna.phy.fec.utils import load_parity_check_examples
from sionna.phy.fec.linear import LinearEncoder

# Load (7,4) Hamming code
pcm, k, n, _ = load_parity_check_examples(0)
encoder = LinearEncoder(pcm, is_pcm=True)

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

Attributes

property k: int#

Number of information bits per codeword.

property n: int#

Codeword length.

property gm: torch.Tensor#

Generator matrix used for encoding.

property coderate: float#

Coderate of the code.

Methods

build(input_shape: tuple) None[source]#

Check for valid input shapes.

Parameters:

input_shape (tuple)