kaira.models.fec.encoders.RepetitionCodeEncoder

Inheritance diagram for RepetitionCodeEncoder
- class kaira.models.fec.encoders.RepetitionCodeEncoder(repetition_factor: int = 3, **kwargs: Any)[source]
Bases:
LinearBlockCodeEncoderEncoder 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].
- 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
Initialize the repetition encoder.
Calculate the syndrome of a received word.
Calculate the coset leader weight distribution of the repetition code.
Extract the message bits from a codeword.
Applies the encoding mapping Enc: B^k → B^n of the code.
Decode the input tensor using the generator matrix right inverse.
Attributes
Get the code dimension (k).
Get the codeword length (n).
Get the rate of the code (k/n).
Get the number of parity bits (synonym for redundancy).
Get the check matrix H of the code.
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