Berlekamp–Massey algorithm: Difference between revisions

Content deleted Content added
m Fix indention
Line 101:
T(D) T(x) polynomial
-->
<syntaxhighlight lang=C"c">
polynomial(field K) s(x) = ... /* coeffs are s_j; output sequence as N-1 degree polynomial) */
/* connection polynomial */
polynomial(field K) C(x) = 1; /* coeffs are c_j */
polynomial(field K) B(x) = 1;
int L = 0;
int m = 1;
field K b = 1;
int n;
 
/* steps 2. and 6. */
for (n = 0; n < N; n++) {
/* step 2. calculate discrepancy */
field K d = s_n + \Sigma_{i=1}^L c_i * s_{n-i};
 
if (d == 0) {
/* step 3. discrepancy is zero; annihilation continues */
m = m + 1;
} else if (2 * L <= n) {
/* step 5. */
/* temporary copy of C(x) */
polynomial(field K) T(x) = C(x);
 
C(x) = C(x) - d b^{-1} x^m B(x);
L = n + 1 - L;
B(x) = T(x);
b = d;
m = 1;
} else {
/* step 4. */
C(x) = C(x) - d b^{-1} x^m B(x);
m = m + 1;
}
}
return L;
</syntaxhighlight>
 
In the case of binary GF(2) BCH code, the discrepancy d will be zero on all odd steps, so a check can be added to avoid calculating it.
 
<syntaxhighlight lang=C"c">
/* ... */
for (n = 0; n < N; n++) {
/* if odd step number, discrepancy == 0, no need to calculate it */
if ((n&1) != 0) {
m = m + 1;
continue;
}
/* ... */
</syntaxhighlight>