// ------------------------------------------------------------------------ // File: bch_awgn.c // Date: January 12, 2000 // // Simulation of hard-decision decoding of a binary BCH code with binary // transmission over an AWGN channel. Decoding using the BM algorithm. // ------------------------------------------------------------------------ // This program is complementary material for the book: // // R.H. Morelos-Zaragoza, The Art of Error Correcting Coding, Wiley, 2002. // // ISBN 0471 49581 6 // // This and other programs are available at http://the-art-of-ecc.com // // You may use this program for academic and personal purposes only. // If this program is used to perform simulations whose results are // published in a journal or book, please refer to the book above. // // The use of this program in a commercial product requires explicit // written permission from the author. The author is not responsible or // liable for damage or loss that may be caused by the use of this program. // // Copyright (c) 2002. Robert H. Morelos-Zaragoza. All rights reserved. // ------------------------------------------------------------------------ #include #include #include #include #include #define MAX_RANDOM LONG_MAX // Maximum value of random() int i; int m, n, length, k, t, d; int p[10]; int alpha_to[1024], index_of[1024], g[1024]; int recd[1024], data[1024], bb[1024]; int numerr, errpos[1024], decerror = 0; FILE *fp2; double rate; float init_snr; float final_snr; float snr_increment; double snr; double num_sim; double sim; double ber; double amp; long seed; int error; char filename[40], name2[40]; void read_p(void); void generate_gf(void); void gen_poly(void); void encode_bch(void); void decode_bch(void); void bpsk_awgn(void); main(int argc, char *argv[]) { // Command line processing if (argc != 10) { printf("\nSimulation of BCH codes over a binary input AWGN channel\n"); printf("Copyright 1994-2000. Robert Morelos-Zaragoza\n\n"); printf("Usage: %s m length t init_snr final_snr snr_inc num_sim output_file seed\n", argv[0]); printf(" - m is the order of GF(2^m)\n"); printf(" - length of the BCH code\n"); printf(" - t = [d-1/2]\n"); printf(" - init_snr is the initial value of Eb/No (dB)\n"); printf(" - final_snr is the final value of Eb/No (dB)\n"); printf(" - snr_inc is the increment in Eb/No (dB)\n"); printf(" - num_sim is the number of simulations per Eb/No value\n"); printf(" - output_file is the name of a file with Eb/No and BER\n"); printf(" - seed is the value used in srandom\n"); exit(0); } sscanf(argv[1],"%d", &m); sscanf(argv[2],"%d", &length); sscanf(argv[3],"%d", &t); sscanf(argv[4],"%f", &init_snr); sscanf(argv[5],"%f", &final_snr); sscanf(argv[6],"%f", &snr_increment); sscanf(argv[7],"%lf",&num_sim); sscanf(argv[8],"%s", name2); sscanf(argv[9],"%lf",&seed); fp2 = fopen(name2,"w"); read_p(); /* Read m */ generate_gf(); /* Construct the Galois Field GF(2**m) */ gen_poly(); /* Compute the generator polynomial of BCH code */ srandom(seed); rate = (float) k / (float) n; snr = init_snr; while ( snr < (final_snr+0.001) ) { amp = sqrt(2.0*rate*pow(10.0,(snr/10.0))); ber = 0.0; sim = 0.0; while (sim < num_sim) { /* Randomly generate DATA */ for (i = 0; i < k; i++) data[i] = ( random() & 65536 ) >> 16; encode_bch(); /* encode data */ for (i = 0; i < length - k; i++) recd[i] = bb[i]; for (i = 0; i < k; i++) recd[i + length - k] = data[i]; bpsk_awgn(); decode_bch(); /* DECODE received codeword recv[] */ // DECODING ERRORS? we compare only the data portion decerror = 0; for (i = length - k; i < length; i++) if (data[i - length + k] != recd[i]) decerror++; ber += decerror; sim += 1.0; } printf("%f %8.0f %8.0f %13.8e\n", snr, ber, (k*sim), (ber/(sim*k))); fflush(stdout); fprintf(fp2, "%f %13.8e\n", snr, (ber/(sim*k)) ); fflush(fp2); snr += snr_increment; } } void read_p() /* * Read m, the degree of a primitive polynomial p(x) used to compute the * Galois field GF(2**m). Get precomputed coefficients p[] of p(x). Read * the code length. */ { int i, ninf; printf("bch_awgn: Simulation of binary BCH codes over a binary input AWGN channel\n"); printf("Copyright (c) 1994-2000. Robert Morelos-Zaragoza. All rights reserved.\n"); printf("This program is free. Read the copyright notice.\n"); for (i=1; ipolynomial 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 the generator polynomial of a binary BCH code. Fist generate the * cycle sets modulo 2**m - 1, cycle[][] = (i, 2*i, 4*i, ..., 2^l*i). Then * determine those cycle sets that contain integers in the set of (d-1) * consecutive integers {1..(d-1)}. The generator polynomial is calculated * as the product of linear factors of the form (x+alpha^i), for every i in * the above cycle sets. */ { 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; for (ii = 1; ((ii <= jj) && (!test)); ii++) /* Examine previous cycle sets */ for (kaux = 0; ((kaux < size[ii]) && (!test)); kaux++) if (ll == cycle[ii][kaux]) test = 1; } while ((test) && (ll < (n - 1))); if (!(test)) { jj++; /* next cycle set index */ cycle[jj][0] = ll; size[jj] = 1; } } while (ll < (n - 1)); nocycles = jj; /* number of cycle sets modulo n */ // printf("Enter the error correcting capability, t: "); // scanf("%d", &t); d = 2 * t + 1; /* Search for roots 1, 2, ..., d-1 in cycle sets */ kaux = 0; rdncy = 0; for (ii = 1; ii <= nocycles; ii++) { min[kaux] = 0; test = 0; for (jj = 0; ((jj < size[ii]) && (!test)); jj++) for (root = 1; ((root < d) && (!test)); root++) if (root == cycle[ii][jj]) { test = 1; min[kaux] = ii; } if (min[kaux]) { rdncy += size[min[kaux]]; kaux++; } } noterms = kaux; kaux = 1; for (ii = 0; ii < noterms; ii++) for (jj = 0; jj < size[min[ii]]; jj++) { zeros[kaux] = cycle[min[ii]][jj]; kaux++; } 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() /* * Compute redundacy bb[], the coefficients of b(x). The redundancy * polynomial b(x) is the remainder after dividing x^(length-k)*data(x) * by the generator polynomial g(x). */ { register int i, j; register int feedback; for (i = 0; i < length - k; i++) bb[i] = 0; for (i = k - 1; i >= 0; i--) { feedback = data[i] ^ bb[length - k - 1]; if (feedback != 0) { for (j = length - k - 1; j > 0; j--) if (g[j] != 0) bb[j] = bb[j - 1] ^ feedback; else bb[j] = bb[j - 1]; bb[0] = g[0] && feedback; } else { for (j = length - k - 1; j > 0; j--) bb[j] = bb[j - 1]; bb[0] = 0; } } } void decode_bch() /* * Simon Rockliff's implementation of Berlekamp's algorithm. * * Assume we have received bits in recd[i], i=0..(n-1). * * Compute the 2*t syndromes by substituting alpha^i into rec(X) and * evaluating, storing the syndromes in s[i], i=1..2t (leave s[0] zero) . * Then we use the Berlekamp algorithm to find the error location polynomial * elp[i]. * * If the degree of the elp is >t, then we cannot correct all the errors, and * we have detected an uncorrectable error pattern. We output the information * bits uncorrected. * * If the degree of elp is <=t, we substitute alpha^i , i=1..n into the elp * to get the roots, hence the inverse roots, the error location numbers. * * This step is usually called "Chien's search". * * If the number of errors located is not equal the degree of the elp, then * the decoder assumes that there are more than t errors and cannot correct * them, only detect them. We output the information bits uncorrected. */ { 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 */ 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 */ /* * Note: If the code is used only for ERROR DETECTION, then * exit program here indicating the presence of errors. */ /* convert syndrome from polynomial form to index form */ s[i] = index_of[s[i]]; } if (syn_error) { /* if there are errors, try to correct them */ /* * Compute the error location polynomial via the Berlekamp * iterative algorithm. Following the terminology of Lin and * Costello's book : d[u] is the 'mu'th discrepancy, where * u='mu'+1 and 'mu' (the Greek letter!) is the step number * ranging from -1 to 2*t (see L&C), l[u] is the degree of * the elp at that step, and u_l[u] is the difference between * the step number and the degree of the elp. */ /* initialise table entries */ d[0] = 0; /* index form */ d[1] = s[1]; /* index form */ elp[0][0] = 0; /* index form */ elp[1][0] = 1; /* polynomial form */ for (i = 1; i < t2; i++) { elp[0][i] = -1; /* index form */ elp[1][i] = 0; /* polynomial form */ } l[0] = 0; l[1] = 0; u_lu[0] = -1; u_lu[1] = 0; u = 0; do { u++; if (d[u] == -1) { l[u + 1] = l[u]; for (i = 0; i <= l[u]; i++) { elp[u + 1][i] = elp[u][i]; elp[u][i] = index_of[elp[u][i]]; } } else /* * search for words with greatest u_lu[q] for * which d[q]!=0 */ { q = u - 1; while ((d[q] == -1) && (q > 0)) q--; /* have found first non-zero d[q] */ if (q > 0) { j = q; do { j--; if ((d[j] != -1) && (u_lu[q] < u_lu[j])) q = j; } while (j > 0); } /* * 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]]; /* 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++; } } 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 */ } } } void bpsk_awgn() // // BPSK map, AWGN add and BPSK detect // { double u1,u2,s,noise,randmum; double trans[1024]; int i; for (i=0; i= 1); noise = u1 * sqrt( (-2.0*log(s))/s ); trans[i] += noise/amp; if (trans[i]<0.0) recd[i] = 1; else recd[i] = 0; } }