The Berlekamp–Massey algorithm is an alternative to the Reed–Solomon Peterson decoder for solving the set of linear equations. It can be summarized as finding the coefficients Λj of a polynomial Λ so that for all positions i in an input streamS: In the code examples below, C is a potential instance of Λ. The error locator polynomial C for L errors is defined as: or reversed: The goal of the algorithm is to determine the minimal degree L and C which results in all syndromes being equal to 0: Algorithm: C is initialized to 1, L is the current number of assumed errors, and initialized to zero. N is the total number of syndromes. n is used as the main iterator and to index the syndromes from 0 to N−1. B is a copy of the last C since L was updated and initialized to 1. b is a copy of the last discrepancy d since L was updated and initialized to 1. m is the number of iterations since L, B, and b were updated and initialized to 1. Each iteration of the algorithm calculates a discrepancy d. At iteration k this would be: If d is zero, the algorithm assumes that C and L are correct for the moment, increments m, and continues. If d is not zero, the algorithm adjusts C so that a recalculation of d would be zero: The xm term shifts B so it follows the syndromes corresponding to b. If the previous update of L occurred on iteration j, then m = k − j, and a recalculated discrepancy would be: This would change a recalculated discrepancy to: The algorithm also needs to increase L as needed. If L equals the actual number of errors, then during the iteration process, the discrepancies will become zero before n becomes greater than or equal to 2L. Otherwise L is updated and algorithm will update B, b, increase L, and reset m = 1. The formula L = limits L to the number of available syndromes used to calculate discrepancies, and also handles the case where L increases by more than 1.
Code sample
The algorithm from for an arbitrary field: polynomial s */ /* connection polynomial */ polynomial C = 1; /* coeffs are c_j */ polynomial B = 1; int L = 0; int m = 1; field K b = 1; int n; /* steps 2. and 6. */ for return L;
The following is the Berlekamp–Massey algorithm specialized for the binary finite field F2. The field elements are '0' and '1'. The field operations '+' and '−' are identical and are equivalent to the 'exclusive or' operation, XOR. The multiplication operator '*' becomes the logical AND operation. The division operator reduces to the identity operation.
Let be the bits of the stream.
Initialise two arrays and each of length to be zeroes, except
assign.
Forstep 1 while :
*Let discrepancy be.
*if, then is already a polynomial which annihilates the portion of the stream from to.
*else:
** Let be a copy of.
** Set up to .
** If, set, set, and let ; otherwise leave, and alone.
At the end of the algorithm, is the length of the minimal LFSR for the stream, and we have for all.