BCH Encoder
-
I'm writing an application that requires some BCH ECC (Error Correction Coding). I found some code on the net but the result of calculation is different with the results from MATLAB. Is there anyone that has some experience in this field? The MATLAB code is as follow:
n =31
k=21%gf is Galois field
msg=gf([1 1 1 0 0 1 1 1 1 0 0 0 0 1 1 0 0 1 1 0 1])cbch=bchenc(msg, n, k)
cbch Array elements =
1 1 1 0 0 1 1 1 1 0 0
0 0 1 1 0 0 1 1 0 1 1
0 1 0 0 1 0 1 0 1Here is the C code (console app):
#include "StdAfx.h"
#include
#include
#includeint m = 5, n = 31, k = 21, t = 2, d = 5;
int length = 31;
int p[6]; /* irreducible polynomial */
int alpha_to[32], index_of[32], g[11];
int recd[31], data[21], bb[11];
int numerr, errpos[32], decerror = 0;
int seed;void
read_p()
/* Primitive polynomial of degree 5 */
{
// register int i;
p[0] = p[2] = p[5] = 1; p[1] = p[3] = p[4] =0;
}void
generate_gf()
/*
* generate GF(2**m) from the irreducible polynomial p(X) in p[0]..p[m]
* lookup tables: index->polynomial form alpha_to[] contains j=alpha**i;
* polynomial form -> index form index_of[j=alpha**i] = i alpha=2 is the
* primitive element of GF(2**m)
*/
{
register int i, mask;
mask = 1;
alpha_to[m] = 0;
for (i = 0; i < m; i++) {
alpha_to[i] = mask;
index_of[alpha_to[i]] = i;
if (p[i] != 0)
alpha_to[m] ^= mask;
mask <<= 1;
}
index_of[alpha_to[m]] = m;
mask >>= 1;
for (i = m + 1; i < n; i++) {
if (alpha_to[i - 1] >= mask)
alpha_to[i] = alpha_to[m] ^ ((alpha_to[i - 1] ^ mask) << 1);
else
alpha_to[i] = alpha_to[i - 1] << 1;
index_of[alpha_to[i]] = i;
}
index_of[0] = -1;
}void
gen_poly()
/*
* Compute generator polynomial of BCH code of length = 31, redundancy = 10
* (OK, this is not very efficient, but we only do it once, right? :)
*/
{
register int ii, jj, ll, kaux;
int test, aux, nocycles, root, noterms, rdncy;
int cycle[15][6], size[15], min[11], zeros[11];
/* Generate cycle sets modulo 31 */
cycle[0][0] = 0; size[0] = 1;
cycle[1][0] = 1; size[1] = 1;
jj = 1; /* cycle set index */
do {
/* Generate the jj-th cycle set */
ii = 0;
do {
ii++;
cycle