Jump to content

Berlekamp–Massey algorithm

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 67.128.198.190 (talk) at 17:33, 12 July 2010 (Cleaning up the unecessary clones since several people will be using this code directly and removing an incorrect -1 in the for (; <N - N_M; ) loop). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The Berlekamp–Massey algorithm is an algorithm that will find the shortest linear feedback shift register (LFSR) for a given binary output sequence. The algorithm will also find the minimal polynomial of a linearly recurrent sequence in a (restricted) field (mathematics)[1].

Elwyn Berlekamp invented an algorithm for decoding Bose–Chaudhuri–Hocquenghem (BCH) codes.[2][3] James Massey recognized its application to linear feedback shift registers and simplified the algorithm[4][5]. Massey termed the algorithm the LFSR Synthesis Algorithm (Berlekamp Iterative Algorithm) (Massey 1969, p. 124), but it is now known as the Berlekamp–Massey algorithm. The algorithm became the key to practical application of the now ubiquitous Reed–Solomon code.

The algorithm for the binary field

The following is the Berlekamp–Massey algorithm specialized for the typical binary finite field F2 and GF(2). The field elements are 0 and 1. The field operations + and − are identical and become the exclusive or operation (xor). The multiplication operator * becomes the logical and operation. The division operator reduces to the identity operation (i.e., field division is only defined for dividing by 1, and x/1 = x).

  1. Let be the bits of the stream.
  2. Initialise two arrays and each of length to be zeroes, except
  3. assign .
  4. For step 1 while :
    • Let 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 (where is the Exclusive or operator).
      • 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 .

Code sample for the binary field in Java

The following code sample is for a binary field.

    public static int runTest(int[] array) {
        final int N = array.length;
        int[] b = new int[N];
        int[] c = new int[N];
        int[] t = new int[N];
        b[0] = 1;
        c[0] = 1;
        int l = 0;
        int m = -1;
        for (int n = 0; n < N; n++) {
            int d = 0;
            for (int i = 0; i <= l; i++) {
                d ^= c[i] * array[n - i];
            }
            if (d == 1) {
                System.arraycopy(c, 0, t, 0, N)
                int N_M = n - m;
                for (int j = 0; j < N - N_M; j++) {
                    c[N_M + j] ^= b[j];
                }
                if (l <= n / 2) {
                    l = n + 1 - l;
                    m = n;
                    System.arraycopy(t, 0, b, 0, N)
                }
            }
        }
        return l;
    }

Berlekamp–Massey algorithm for fields

The algorithm from Massey (1969, p. 124).

  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;
  
  for (n = 0; n < N; n++)
    {
      /* calculate discrepancy */
      field K d = s_n + \Sigma_{i=1}^L c_i * s_{n-i};
  
      if (d == 0)
        {
          /* anihilation continues */
          m = m + 1;
        }
      else if (2 * L <= n)
        {
          /* 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
        {
          C(x) = C(x) - d b^{-1} x^m B(x);
          m = m + 1;
        }
    }
  return L;

See also

References

Reeds, J. A.; Sloane, N. J. A. (1985), "Shift-Register Synthesis (Modulo n)" (PDF), SIAM Journal on Computing, 14 (3): 505–513, doi:10.1137/0214038


  1. ^ The algorithm requires all non zero elements to have a multiplicative inverse. (Reeds & Sloane 1985, p. 2)
  2. ^ Berlekamp, Elwyn R. (1967), Nonbinary BCH decoding, International Symposium on Information Theory, San Remo, Italy{{citation}}: CS1 maint: location missing publisher (link)
  3. ^ Berlekamp, Elwyn R. (1984) [1968], Algebraic Coding Theory, Laguna Hills, CA: Aegean Park Press, ISBN 0894120638 {{citation}}: Unknown parameter |ed= ignored (help). Previous publisher McGraw-Hill, New York, NY.
  4. ^ Massey, J. L. (1969), "Shift-register synthesis and BCH decoding" (PDF), IEEE Trans. Information Theory, IT-15 (1): 122–127
  5. ^ Ben Atti, Nadia; Diaz-Toca, Gema M.; Lombardi, Henri, The Berlekamp–Massey Algorithm revisited