BCH Codes
BCH Codes
4
BCH codes
#include <math.h>
#include <stdio.h>
int
int
int
int
int
int
m, n, length, k, t, d;
p[21];
alpha_to[1048576], index_of[1048576], g[548576];
recd[1048576], data[1048576], bb[548576];
seed;
numerr, errpos[1024], decerror = 0;
void
read_p()
{
int i, ninf;
printf("bch3: An encoder/decoder for binary BCH codes\n");
printf("Copyright (c) 1994-7. Robert Morelos-Zaragoza.\n");
printf("This program is free, please read first the copyright notice.\n");
printf("\nFirst, enter a value of m such that the code length is\n");
printf("2**(m-1) - 1 < length <= 2**m - 1\n\n");
do {
printf("Enter m (between 2 and 20): ");
scanf("%d", &m);
} while ( !(m>1) || !(m<21) );
for (i=1; i<m; i++)
p[i] = 0;
p[0] = p[m] = 1;
if (m == 2)
p[1] = 1;
else if (m == 3) p[1] = 1;
else if (m == 4) p[1] = 1;
else if (m == 5) p[2] = 1;
else if (m == 6) p[1] = 1;
else if (m == 7) p[1] = 1;
else if (m == 8) p[4] = p[5] = p[6] = 1;
else if (m == 9) p[4] = 1;
else if (m == 10) p[3] = 1;
void
generate_gf()
{
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;
void
gen_poly()
{
register int
ii, jj, ll, kaux;
register int
test, aux, nocycles, root, noterms, rdncy;
int
cycle[1024][21], size[1024], min[1024], zeros[1024];
/* Generate cycle sets modulo n, n = 2**m - 1 */
cycle[0][0] = 0;
size[0] = 1;
cycle[1][0] = 1;
size[1] = 1;
jj = 1;
/* cycle set index */
if (m > 9) {
printf("Computing cycle sets modulo %d\n", n);
printf("(This may take some time)...\n");
}
do {
/* Generate the jj-th cycle set */
ii = 0;
do {
ii++;
cycle[jj][ii] = (cycle[jj][ii - 1] * 2) % n;
size[jj]++;
aux = (cycle[jj][ii] * 2) % n;
} while (aux != cycle[jj][0]);
/* Next cycle set representative */
ll = 0;
do {
ll++;
test = 0;
}
k = length - rdncy;
if (k<0)
{
printf("Parameters invalid!\n");
exit(0);
}
printf("This is a (%d, %d, %d) binary BCH code\n", length, k, d);
/* Compute the generator polynomial */
g[0] = alpha_to[zeros[1]];
g[1] = 1;
/* g(x) = (X + zeros[1]) initially */
for (ii = 2; ii <= rdncy; ii++) {
g[ii] = 1;
for (jj = ii - 1; jj > 0; jj--)
if (g[jj] != 0)
g[jj] = g[jj - 1] ^ alpha_to[(index_of[g[jj]] + zeros[ii]) % n];
else
g[jj] = g[jj - 1];
g[0] = alpha_to[(index_of[g[0]] + zeros[ii]) % n];
}
printf("Generator polynomial:\ng(x) = ");
for (ii = 0; ii <= rdncy; ii++) {
printf("%d", g[ii]);
if (ii && ((ii % 50) == 0))
printf("\n");
}
printf("\n");
}
void
encode_bch()
{
register int i, j;
register int feedback;
for (i = 0; i < length - k; i++)
bb[i] = 0;
void
decode_bch()
{
register int i, j, u, q, t2, count = 0, syn_error = 0;
int
elp[1026][1024], d[1026], l[1026], u_lu[1026], s[1025];
int
root[200], loc[200], err[1024], reg[201];
t2 = 2 * t;
/* first form the syndromes */
printf("S(x) = ");
for (i = 1; i <= t2; i++) {
s[i] = 0;
for (j = 0; j < length; j++)
if (recd[j] != 0)
s[i] ^= alpha_to[(i * j) % n];
if (s[i] != 0)
syn_error = 1; /* set error flag if non-zero syndrome */
/* convert syndrome from polynomial form to index form */
s[i] = index_of[s[i]];
printf("%3d ", s[i]);
}
printf("\n");
}
/*
* have now found q such that d[u]!=0 and
* u_lu[q] is maximum
*/
/* store degree of new elp polynomial */
if (l[u] > l[q] + u - q)
l[u + 1] = l[u];
else
l[u + 1] = l[q] + u - q;
/* form new elp(x) */
for (i = 0; i < t2; i++)
elp[u + 1][i] = 0;
for (i = 0; i <= l[q]; i++)
if (elp[q][i] != -1)
elp[u + 1][i + u - q] =
alpha_to[(d[u] + n - d[q] + elp[q][i]) % n];
for (i = 0; i <= l[u]; i++) {
elp[u + 1][i] ^= elp[u][i];
elp[u][i] = index_of[elp[u][i]];
}
}
u_lu[u + 1] = u - l[u + 1];
/* form (u+1)th discrepancy */
if (u < t2) {
/* no discrepancy computed on last iteration */
if (s[u + 1] != -1)
d[u + 1] = alpha_to[s[u + 1]];
else
d[u + 1] = 0;
for (i = 1; i <= l[u + 1]; i++)
if ((s[u + 1 - i] != -1) && (elp[u + 1][i] != 0))
d[u + 1] ^= alpha_to[(s[u + 1 - i]
+ index_of[elp[u + 1][i]]) % n];
/* put d[u+1] into index form */
d[u + 1] = index_of[d[u + 1]];
}
} while ((u < t2) && (l[u + 1] <= t));
u++;
if (l[u] <= t) {/* Can correct errors */
/* put elp into index form */
for (i = 0; i <= l[u]; i++)
elp[u][i] = index_of[elp[u][i]];
printf("sigma(x) = ");
for (i = 0; i <= l[u]; i++)
printf("%3d ", elp[u][i]);
printf("\n");
printf("Roots: ");
/* Chien search: find roots of the error location polynomial */
for (i = 1; i <= l[u]; i++)
reg[i] = elp[u][i];
count = 0;
for (i = 1; i <= n; i++) {
q = 1;
for (j = 1; j <= l[u]; j++)
if (reg[j] != -1) {
reg[j] = (reg[j] + j) % n;
q ^= alpha_to[reg[j]];
}
if (!q) { /* store root and error
* location number indices */
root[count] = i;
loc[count] = n - i;
count++;
printf("%3d ", n - i);
}
}
printf("\n");
if (count == l[u])
/* no. roots = degree of elp hence <= t errors */
for (i = 0; i < l[u]; i++)
recd[loc[i]] ^= 1;
else
/* elp has degree >t hence cannot solve */
printf("Incomplete decoding: errors detected\n");
}
}
}
main()
{
int
i;
read_p();
generate_gf();
gen_poly();
/* Read m */
/* Construct the Galois Field GF(2**m) */
/* Compute the generator polynomial of BCH code */
/*
* print out original and decoded data
*/
printf("Results:\n");
printf("original data = ");
for (i = 0; i < k; i++) {
printf("%1d", data[i]);
if (i && ((i % 50) == 0))
printf("\n");
}
printf("\nrecovered data = ");
for (i = length - k; i < length; i++) {
printf("%1d", recd[i]);
if ((i-length+k) && (((i-length+k) % 50) == 0))
printf("\n");
}
printf("\n");
/*
* DECODING ERRORS? we compare only the data portion
*/
for (i = length - k; i < length; i++)
if (data[i - length + k] != recd[i])
decerror++;
if (decerror)
printf("There were %d decoding errors in message positions\n", decerror);
else
printf("Succesful decoding\n");
}