kaira.models.fec.encoders.ReedSolomonCodeEncoder

Inheritance diagram of ReedSolomonCodeEncoder

Inheritance diagram for ReedSolomonCodeEncoder

class kaira.models.fec.encoders.ReedSolomonCodeEncoder(mu: int, delta: int, information_set: List[int] | Tensor | str = 'left', dtype: dtype = torch.float32, **kwargs: Any)[source]

Bases: SystematicLinearBlockCodeEncoder

Encoder for Reed-Solomon (RS) codes.

Reed-Solomon codes are maximum distance separable (MDS) codes with parameters: - Length: n = 2^m - 1 - Dimension: k = n - (δ - 1) - Minimum distance: d = δ

Parameters:
  • mu (int) – The parameter μ of the code (field size is 2^μ).

  • delta (int) – The design distance δ of the code.

  • information_set (Union[List[int], torch.Tensor, str], optional) – Information set specification. Default is “left”.

  • dtype (torch.dtype, optional) – Data type for internal tensors. Default is torch.float32.

  • **kwargs – Additional keyword arguments passed to the parent class.

Examples

>>> encoder = ReedSolomonCodeEncoder(mu=4, delta=5)
>>> message = torch.tensor([1., 0., 1., 1., 0., 1., 0., 1., 0., 1., 0.])
>>> codeword = encoder(message)

Methods

__init__

Initialize the Reed-Solomon code encoder.

calculate_syndrome

Calculate the syndrome of a received word.

create_standard_code

Create a standard Reed-Solomon code by name.

extract_message

Extract the message bits from a codeword.

forward

Encode the input tensor using systematic encoding.

from_design_rate

Create a Reed-Solomon code with a design rate close to the target rate.

get_standard_codes

Get a dictionary of standard Reed-Solomon codes with their parameters.

inverse_encode

Decode the input tensor using the generator matrix right inverse.

project_word

Project a codeword onto the information set.

Attributes

code_dimension

Dimension k of the code.

code_length

Length n of the code.

code_rate

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

delta

Design distance δ of the code.

error_correction_capability

Error correction capability t = ⌊(δ-1)/2⌋ of the code.

information_set

Either indices of information positions, which must be a k-sublist of [0...n), or one of the strings 'left' or 'right'.

mu

Parameter μ of the code.

parity_bits

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

parity_check_matrix

Get the check matrix H of the code.

parity_set

Parity set M of the code.

parity_submatrix

Parity submatrix P of the code.

redundancy

Redundancy r = n - k of the code.

__init__(mu: int, delta: int, information_set: List[int] | Tensor | str = 'left', dtype: dtype = torch.float32, **kwargs: Any)[source]

Initialize the Reed-Solomon code encoder.

calculate_syndrome(received: Tensor) Tensor[source]

Calculate the syndrome of a received word.

The syndrome of a received word r is H·r^T (mod 2), where H is the parity check matrix. For a valid codeword c, H·c^T = 0. For a received word with errors, the syndrome will be non-zero.

Parameters:

received – The received word tensor of shape (…, code_length).

Returns:

The syndrome tensor of shape (…, redundancy).

Raises:

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

property mu: int

Parameter μ of the code.

property delta: int

Design distance δ of the code.

property error_correction_capability: int

Error correction capability t = ⌊(δ-1)/2⌋ of the code.

property code_length: int

Length n of the code.

property code_dimension: int

Dimension k of the code.

property redundancy: int

Redundancy r = n - k of the code.

classmethod from_design_rate(mu: int, target_rate: float, **kwargs: Any) ReedSolomonCodeEncoder[source]

Create a Reed-Solomon code with a design rate close to the target rate.

classmethod get_standard_codes() Dict[str, Dict[str, Any]][source]

Get a dictionary of standard Reed-Solomon codes with their parameters.

classmethod create_standard_code(name: str, **kwargs: Any) ReedSolomonCodeEncoder[source]

Create a standard Reed-Solomon code by name.

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

Encode the input tensor using systematic encoding.

For systematic codes, encoding can be done efficiently by placing information bits directly in the information positions and calculating parity bits only. This implementation is optimized compared to the general matrix multiplication used in the parent class.

Parameters:
  • x – The input tensor of shape (…, message_length) or (…, b*message_length) where b is a positive integer.

  • *args – Additional positional arguments (unused).

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

Returns:

Encoded tensor of shape (…, codeword_length) or (…, b*codeword_length)

Raises:

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

property information_set: Tensor

Either indices of information positions, which must be a k-sublist of [0…n), or one of the strings ‘left’ or ‘right’.

Default is ‘left’.

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 parity_set: Tensor

Parity set M of the code.

property parity_submatrix: Tensor

Parity submatrix P of the code.

project_word(x: Tensor) Tensor

Project a codeword onto the information set.

This extracts the information bits directly from a codeword without decoding, which is a key advantage of systematic codes.

Parameters:

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

Returns:

Projected tensor of shape (…, message_length) or (…, b*message_length)

Raises:

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