kaira.models.fec.encoders.RepetitionCodeEncoder

Inheritance diagram of RepetitionCodeEncoder

Inheritance diagram for RepetitionCodeEncoder

class kaira.models.fec.encoders.RepetitionCodeEncoder(repetition_factor: int = 3, **kwargs: Any)[source]

Bases: LinearBlockCodeEncoder

Encoder for repetition coding that extends LinearBlockCodeEncoder.

This encoder implements a repetition code, which is a special case of linear block codes where each bit is repeated n times. A repetition code has the following properties:

  • Length: n (the repetition factor)

  • Dimension: k = 1 (one information bit produces n coded bits)

  • Redundancy: r = n - 1 (number of redundant bits)

  • Minimum distance: d = n (can correct up to ⌊(n-1)/2⌋ errors)

Its dual is the single parity-check code. The generator matrix is a single row of all ones [1, 1, …, 1].

repetition_factor

The length n of the code. Must be a positive integer.

Type:

int

Parameters:

repetition_factor (int) – Number of times to repeat each bit

Examples

>>> import torch
>>> encoder = RepetitionCodeEncoder(repetition_factor=5)
>>> encoder.code_length, encoder.code_dimension, encoder.redundancy
(5, 1, 4)
>>> encoder.generator_matrix
tensor([[1., 1., 1., 1., 1.]])
>>> encoder(torch.tensor([[1.]]))
tensor([[1., 1., 1., 1., 1.]])

Methods

__init__

Initialize the repetition encoder.

calculate_syndrome

Calculate the syndrome of a received word.

coset_leader_weight_distribution

Calculate the coset leader weight distribution of the repetition code.

extract_message

Extract the message bits from a codeword.

forward

Applies the encoding mapping Enc: B^k → B^n of the code.

inverse_encode

Decode the input tensor using the generator matrix right inverse.

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).

parity_check_matrix

Get the check matrix H of the code.

redundancy

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

__init__(repetition_factor: int = 3, **kwargs: Any)[source]

Initialize the repetition encoder.

Parameters:
  • repetition_factor – Number of times to repeat each bit. Must be a positive integer.

  • **kwargs – Variable keyword arguments passed to the base class.

Raises:

ValueError – If repetition_factor is less than 1.

coset_leader_weight_distribution() Tensor[source]

Calculate the coset leader weight distribution of the repetition code.

For a repetition code of length n, the coset leader weight distribution is given by the binomial coefficients C(n,w) for w from 0 to ⌊n/2⌋, with a special case for n/2 when n is even.

Returns:

Tensor containing the coset leader weight distribution

calculate_syndrome(x: Tensor) Tensor

Calculate the syndrome of a received word.

The syndrome is computed as s = xH^T and is used to detect errors. A non-zero syndrome indicates the presence of errors [Lin and Costello, 2004, Moon, 2005]. This approach is a fundamental technique in error detection and correction for linear block codes [Sklar, 2001].

Parameters:

x – Received word tensor of shape (…, codeword_length) or (…, b*codeword_length) where b is a positive integer.

Returns:

Syndrome tensor of shape (…, redundancy) or (…, b*redundancy)

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

Applies the encoding mapping Enc: B^k → B^n of the code.

This method takes one or more sequences of messages and returns their corresponding codeword sequences. The encoding process follows standard linear block code principles [Lin and Costello, 2004, Richardson and Urbanke, 2008].

Parameters:
  • x – The input tensor. Can be either a single sequence whose length is a multiple of k, or a multidimensional tensor where the last dimension is a multiple of k.

  • *args – Additional positional arguments (unused).

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

Returns:

The output tensor. Has the same shape as the input, with the last dimension expanded from b*k to b*n, where b is a positive integer.

Raises:

ValueError – If the last dimension of the input is not a multiple of k.

inverse_encode(x: Tensor, *args: Any, **kwargs: Any) Tuple[Tensor, Tensor]

Decode the input tensor using the generator matrix right inverse.

This method takes one or more sequences of codewords and returns their corresponding decoded messages along with syndromes. The decoding approach follows standard techniques in error control coding literature [Lin and Costello, 2004, Sklar, 2001].

Parameters:
  • x – The input tensor. Can be either a single sequence whose length is a multiple of n, or a multidimensional tensor where the last dimension is a multiple of n.

  • *args – Additional positional arguments (unused).

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

Returns:

  • Decoded tensor of shape (…, b*k). Has the same shape as the input, with the last dimension reduced from b*n to b*k, where b is a positive integer.

  • Syndrome tensor for error detection of shape (…, b*r), where r is the redundancy.

Return type:

Tuple containing

Raises:

ValueError – If the last dimension of the input is not a multiple of n.

property parity_bits: int

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

Returns:

The number of parity/check bits in each codeword

property parity_check_matrix: Tensor

Get the check matrix H of the code.

The check matrix H satisfies the property: GH^T = 0

Returns:

The check matrix H of the code

property redundancy: int

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

Returns:

The number of redundant bits added during encoding