@@ -33,7 +33,7 @@ class Trellis:
3333 Number of memory elements per input of the convolutional encoder.
3434 g_matrix : 2D ndarray of ints (decimal representation)
3535 Generator matrix G(D) of the convolutional encoder. Each element of
36- G(D) represents a polynomial with a MSB first convention (ie, 1+D^2+D^3 <-> 1101 <-> 13 or 0o15).
36+ G(D) represents a polynomial with a MSB first decimal convention (ie, 1+D^2+D^3 <-> 1101 <-> 13 or 0o15).
3737 Coef [i,j] is the influence of input i on output j.
3838 feedback : 2D ndarray of ints (decimal representation), optional
3939 Feedback matrix F(D) of the convolutional encoder. Each element of
@@ -47,6 +47,12 @@ class Trellis:
4747 If 'rsc' is specified, then the first 'k x k' sub-matrix of
4848 G(D) must represent a identity matrix along with a non-zero
4949 feedback polynomial.
50+ *Default* is 'default'.
51+ polynomial_format : {'MSB', 'LSB'}, optional
52+ Defines how to interpret g_matrix and feedback. In MSB format, we have 1+D <-> 3 <-> 011.
53+ In LSB format, we have 1+D <-> 6 <-> 110.
54+ *Default* is 'MSB' format.
55+
5056 Attributes
5157 ----------
5258 k : int
@@ -71,6 +77,12 @@ class Trellis:
7177 Table representing the output matrix of the convolutional code trellis.
7278 Rows represent current states and columns represent current inputs in
7379 decimal. Elements represent corresponding outputs in decimal.
80+
81+ Raises
82+ ------
83+ ValueError
84+ polynomial_format is not 'MSB', 'LSB' or 'Matlab'.
85+
7486 Examples
7587 --------
7688 >>> from numpy import array
@@ -103,7 +115,7 @@ class Trellis:
103115 [1] S. Benedetto, R. Garello et G. Montorsi, "A search for good convolutional codes to be used in the
104116 construction of turbo codes", IEEE Transactions on Communications, vol. 46, n. 9, p. 1101-1005, spet. 1998
105117 """
106- def __init__ (self , memory , g_matrix , feedback = None , code_type = 'default' ):
118+ def __init__ (self , memory , g_matrix , feedback = None , code_type = 'default' , polynomial_format = 'MSB ' ):
107119
108120 [self .k , self .n ] = g_matrix .shape
109121 self .code_type = code_type
@@ -118,7 +130,8 @@ def __init__(self, memory, g_matrix, feedback = None, code_type = 'default'):
118130
119131 if isinstance (feedback , int ):
120132 warn ('Trellis will only accept feedback as a matrix in the future. '
121- 'Using the backwards compatibility version that may contain bugs for k > 1.' , DeprecationWarning )
133+ 'Using the backwards compatibility version that may contain bugs for k > 1 or with LSB format.' ,
134+ DeprecationWarning )
122135
123136 if code_type == 'rsc' :
124137 for i in range (self .k ):
@@ -181,24 +194,37 @@ def __init__(self, memory, g_matrix, feedback = None, code_type = 'default'):
181194 bitarray2dec (shift_register )
182195
183196 else :
197+ if polynomial_format == 'MSB' :
198+ bit_order = - 1
199+ elif polynomial_format in ('LSB' , 'Matlab' ):
200+ bit_order = 1
201+ else :
202+ raise ValueError ('polynomial_format must be "LSB", "MSB" or "Matlab"' )
203+
184204 if feedback is None :
185205 feedback = np .identity (self .k , int )
206+ if polynomial_format in ('LSB' , 'Matlab' ):
207+ feedback *= 2 ** memory .max ()
208+
209+ max_values_lign = memory .max () + 1 # Max number of value on a delay lign
186210
187211 # feedback_array[i] holds the i-th bit corresponding to each feedback polynomial.
188- feedback_array = np .empty (( self . total_memory + self . k , self .k , self .k ), np .int8 )
212+ feedback_array = np .zeros (( max_values_lign , self .k , self .k ), np .int8 )
189213 for i in range (self .k ):
190214 for j in range (self .k ):
191- feedback_array [:, i , j ] = dec2bitarray (feedback [i , j ], self .total_memory + self .k )[::- 1 ]
215+ binary_view = dec2bitarray (feedback [i , j ], max_values_lign )[::bit_order ]
216+ feedback_array [:max_values_lign , i , j ] = binary_view [- max_values_lign - 2 :]
192217
193218 # g_matrix_array[i] holds the i-th bit corresponding to each g_matrix polynomial.
194- g_matrix_array = np .empty (( self . total_memory + self . k , self .k , self .n ), np .int8 )
219+ g_matrix_array = np .zeros (( max_values_lign , self .k , self .n ), np .int8 )
195220 for i in range (self .k ):
196221 for j in range (self .n ):
197- g_matrix_array [:, i , j ] = dec2bitarray (g_matrix [i , j ], self .total_memory + self .k )[::- 1 ]
222+ binary_view = dec2bitarray (g_matrix [i , j ], max_values_lign )[::bit_order ]
223+ g_matrix_array [:max_values_lign , i , j ] = binary_view [- max_values_lign - 2 :]
198224
199225 # shift_regs holds on each column the state of a shift register.
200226 # The first row is the input of each shift reg.
201- shift_regs = np .empty ((self . total_memory + self . k , self .k ), np .int8 )
227+ shift_regs = np .empty ((max_values_lign , self .k ), np .int8 )
202228
203229 # Compute the entries in the next state table and the output table
204230 for current_state in range (self .number_states ):
0 commit comments