kaira.models.fec.encoders.PolarCodeEncoder

Inheritance diagram of PolarCodeEncoder

Inheritance diagram for PolarCodeEncoder

class kaira.models.fec.encoders.PolarCodeEncoder(code_dimension: int, code_length: int, *args: Any, **kwargs: Any)[source]

Bases: BaseBlockCodeEncoder

Encoder for Polar code [Arıkan, 2008].

This class implements the encoding process for Polar codes, a type of linear block code used in error correction. Polar codes leverage the channel polarization property to achieve efficient encoding and decoding.

The encoder transforms binary input messages into codewords using the Polar transformation. It supports customization of frozen bits, device selection, and data type configuration.

device

Device on which the encoder operates (e.g., ‘cpu’ or ‘cuda’).

Type:

str

m

Number of stages in the Polar code (log2 of the code length).

Type:

int

polar_i

Indicates whether to apply permutation during the Polar transform.

Type:

bool

frozen_zeros

Specifies whether frozen bits are initialized to zeros.

Type:

bool

dtype

Data type used for computations (e.g., torch.float32).

Type:

torch.dtype

load_rank

Indicates whether to load rank-based polar indices as defined in the 5G standard.

Type:

bool

rank

Rank-based indices for frozen bits (loaded if load_rank is True).

Type:

torch.Tensor

info_indices

Boolean array indicating positions of information bits.

Type:

torch.Tensor

mask_dict

Mask dictionary for the Polar code structure.

Type:

torch.Tensor

Methods

__init__

Initializes the PolarCodeEncoder.

extract_message

Extract the message bits from a codeword.

forward

Encodes the input message using the Polar transformation.

get_generator_matrix

Returns the generator matrix of the Polar code (without interleaving).

polar_transform

Applies the Polar transform to the input tensor.

Attributes

code_dimension

Get the code dimension (k).

code_length

Get the codeword length (n).

code_rate

Get the rate of the code (k/n).

parity_bits

Get the number of parity bits (synonym for redundancy).

redundancy

Get the code redundancy (r = n - k).

__init__(code_dimension: int, code_length: int, *args: Any, **kwargs: Any)[source]

Initializes the PolarCodeEncoder.

Parameters:
  • code_dimension (int) – Number of information bits in the Polar code.

  • code_length (int) – Total length of the Polar codeword (must be a power of 2).

  • *args (Any) – Variable positional arguments passed to the base class.

  • **kwargs (Any) –

    Variable keyword arguments for additional configuration, including:

    • device (str): Device on which the encoder operates (default: ‘cpu’).

    • polar_i (bool): Whether to apply permutation during the Polar transform (default: False).

    • frozen_zeros (bool): Whether frozen bits are initialized to zeros (default: False).

    • dtype (torch.dtype): Data type used for computations (default: torch.float32).

    • load_rank (bool): Whether to load rank-based polar indices as defined in the 5G standard (default: True).

    • info_indices (torch.Tensor): Boolean array indicating positions of information bits. Required when load_rank=False. Must have length equal to code_length and exactly code_dimension True values.

get_generator_matrix() Tensor[source]

Returns the generator matrix of the Polar code (without interleaving).

The generator matrix is used to encode information bits into codewords. It is constructed based on the structure of Polar codes.

Returns:

Generator matrix of shape (N, N), where N is the code length.

Return type:

torch.Tensor

polar_transform(u: Tensor, return_arr: bool = False) Tensor[source]

Applies the Polar transform to the input tensor.

The Polar transform is a recursive process that combines and splits bits to achieve channel polarization. This method performs the transformation based on the mask dictionary and supports optional permutation.

Parameters:
  • u (torch.Tensor) – Input tensor of shape (batch_size, code_length).

  • return_arr (bool) – If True, returns intermediate results of the transformation as a list. Default is False.

Returns:

Transformed tensor of shape (batch_size, code_length) if return_arr is False. List[torch.Tensor]: List of intermediate tensors during the transformation if return_arr is True.

Return type:

torch.Tensor

property code_dimension: int

Get the code dimension (k).

Returns:

The number of information bits encoded in each codeword

property code_length: int

Get the codeword length (n).

Returns:

The number of bits in each codeword after encoding

property code_rate: float

Get the rate of the code (k/n).

The code rate is a measure of efficiency, representing the proportion of the total bits that carry information (as opposed to redundancy).

Returns:

The ratio of information bits to total bits (between 0 and 1)

extract_message(codeword: Tensor) Tensor

Extract the message bits from a codeword.

By default, this calls inverse_encode and returns just the decoded message. Subclasses can override this method to provide more efficient implementations.

Parameters:

codeword – Codeword tensor with shape (…, n) where n is the code length

Returns:

Extracted message tensor with shape (…, k) where k is the code dimension

Note

This implementation assumes the inverse_encode method can handle a single codeword correctly. Specific code types may override this with more efficient implementations.

forward(x: Tensor, *args: Any, **kwargs: Any) Tensor[source]

Encodes the input message using the Polar transformation.

Parameters:
  • input (torch.Tensor) – Input tensor of shape (batch_size, code_dimension).

  • *args – Additional positional arguments (unused).

  • **kwargs – Additional keyword arguments (unused).

Returns:

Encoded codeword of shape (batch_size, code_length).

Return type:

torch.Tensor

property parity_bits: int

Get the number of parity bits (synonym for redundancy).

Returns:

The number of parity/check bits in each codeword

property redundancy: int

Get the code redundancy (r = n - k).

Returns:

The number of redundant bits added during encoding