kaira.models.fec.encoders.LinearBlockCodeEncoder

Inheritance diagram for LinearBlockCodeEncoder
- class kaira.models.fec.encoders.LinearBlockCodeEncoder(generator_matrix: Tensor, *args: Any, **kwargs: Any)[source]
Bases:
BaseBlockCodeEncoderEncoder for linear block coding.
This encoder transforms binary input messages into codewords according to the specified generator matrix. It serves as the encoding component of a linear block code system.
The encoder applies the formula: c = mG, where: - c is the codeword - m is the message - G is the generator matrix
This implementation follows the standard approach to linear block coding described in the error control coding literature [Lin and Costello, 2004, Moon, 2005, Sklar, 2001].
- generator_matrix
The generator matrix G of the code
- Type:
- generator_right_inverse
The right pseudo-inverse of the generator matrix
- Type:
- check_matrix
The parity check matrix H
- Type:
- Parameters:
generator_matrix (torch.Tensor) – The generator matrix for encoding. Must be a binary matrix of shape (k, n) where k is the message length and n is the codeword length.
*args – Variable positional arguments passed to the base class.
**kwargs – Variable keyword arguments passed to the base class.
Methods
Initialize the linear block encoder.
Calculate the syndrome of a received word.
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__(generator_matrix: Tensor, *args: Any, **kwargs: Any)[source]
Initialize the linear block encoder.
- Parameters:
generator_matrix (torch.Tensor) – The generator matrix for encoding. Must be a binary matrix of shape (k, n) where k is the message length and n is the codeword length.
*args – Variable positional arguments passed to the base class.
**kwargs – Variable keyword arguments passed to the base class.
- 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
- forward(x: Tensor, *args: Any, **kwargs: Any) Tensor[source]
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.
- calculate_syndrome(x: Tensor) Tensor[source]
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.
- inverse_encode(x: Tensor, *args: Any, **kwargs: Any) Tuple[Tensor, Tensor][source]
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.