# -*- coding: utf-8 -*-"""=================Repetition Coding=================Repetition codes are among the most basic channel coding schemes.The achieve redundancy by repeating all bits within a block during encoding."""from__future__importannotationsimportnumpyasnpfromhermespy.coreimportSerializablefrom.codingimportEncoder__author__="Tobias Kronauer"__copyright__="Copyright 2024, Barkhausen Institut gGmbH"__credits__=["Tobias Kronauer","Jan Adler"]__license__="AGPLv3"__version__="1.3.0"__maintainer__="Jan Adler"__email__="jan.adler@barkhauseninstitut.org"__status__="Prototype"
[docs]classRepetitionEncoder(Encoder,Serializable):"""A channel coding scheme based on block-wise repetition of bits. During encoding, the repetition encoder repeats a block of :math:`K_n` :meth:`.bit_block_size` bits :math:`\\tilde{M}` :meth:`.repetitions` times, leading to a :meth:`.code_block_size` of .. math:: L_n = \\tilde{M} \\cdot K_n bits and a coding rate of .. math:: R_n = \\frac{K_n}{L_n} = \\frac{1}{\\tilde{M}} \\mathrm{.} Let .. math:: \\mathbf{x} = \\left[ x_1, x_2, \\dots, x_{K_n} \\right]^\\intercal \\in \\left\\lbrace 0, 1 \\right\\rbrace^{K_n} be the vector of input bits and .. math:: \\mathbf{y} = \\left[ y_1, y_2, \\dots, y_{K_n} \\right]^\\intercal \\in \\left\\lbrace 0, 1 \\right\\rbrace^{L_n} be the vector of repeated output bits. The implemented block repetition scheme can be described by .. math:: y_k = x_{k \\mod{K_n}} \\mathrm{,} assigning input bits to output bits by index. """yaml_tag="Repetition"__bit_block_size:int__repetitions:intdef__init__(self,bit_block_size:int=32,repetitions:int=3)->None:""" Args: bit_block_size (int, optional): The number of input bits per data block. repetitions (int, optional): The number of times the input bit block is repeated. Raises: ValueError: If `bit_block_size` times `repetitions` is smaller than `code_block_size`. """# Default parametersEncoder.__init__(self)self.bit_block_size=bit_block_sizeself.repetitions=repetitions
@propertydefbit_block_size(self)->int:returnself.__bit_block_size@bit_block_size.setterdefbit_block_size(self,num_bits:int)->None:ifnum_bits<1:raiseValueError("Number data bits must be greater or equal to one")self.__bit_block_size=num_bits@propertydefcode_block_size(self)->int:returnself.__repetitions*self.__bit_block_size@propertydefrepetitions(self)->int:"""Number of times the bit block is repeated during encoding. Returns: int: Number of repetitions :math:`\\tilde{M}`. Raises: ValueError: If `repetitions` is smaller than one. ValueError: If `repetitions` is even. """returnself.__repetitions@repetitions.setterdefrepetitions(self,num:int)->None:ifnum<1:raiseValueError("The number of data bit repetitions must be at least one")ifnum%2==0:raiseValueError("Repetitions must be an uneven integer")self.__repetitions=num