bool BCH::decode(int &err_n, _64bits &orig_n, const int encoded_bits[BCH_DEFAULT_LENGTH]) { assert(k == BCH_DEFAULT_K && length == BCH_DEFAULT_LENGTH); int temp_bits[BCH_DEFAULT_LENGTH]; for(int i=0; i<BCH_DEFAULT_LENGTH; i++) temp_bits[i] = encoded_bits[i]; err_n = decode_bch(temp_bits); if(err_n > t) return(false); orig_n = fromBitPattern(&temp_bits[length - k],BCH_DEFAULT_K); return(true); }
main() { int i; read_p(); /* Read m */ generate_gf(); /* Construct the Galois Field GF(2**m) */ gen_poly(); /* Compute the generator polynomial of BCH code */ /* Randomly generate DATA */ seed = 131073; srandom(seed); for (i = 0; i < k; i++) data[i] = ( random() & 65536 ) >> 16; encode_bch(); /* encode data */ /* * recd[] are the coefficients of c(x) = x**(length-k)*data(x) + b(x) */ for (i = 0; i < length - k; i++) recd[i] = bb[i]; for (i = 0; i < k; i++) recd[i + length - k] = data[i]; printf("Code polynomial:\nc(x) = "); for (i = 0; i < length; i++) { printf("%1d", recd[i]); if (i && ((i % 50) == 0)) printf("\n"); } printf("\n"); printf("Enter the number of errors:\n"); scanf("%d", &numerr); /* CHANNEL errors */ printf("Enter error locations (integers between"); printf(" 0 and %d): ", length-1); /* * recd[] are the coefficients of r(x) = c(x) + e(x) */ for (i = 0; i < numerr; i++) scanf("%d", &errpos[i]); if (numerr) for (i = 0; i < numerr; i++) recd[errpos[i]] ^= 1; printf("r(x) = "); for (i = 0; i < length; i++) { printf("%1d", recd[i]); if (i && ((i % 50) == 0)) printf("\n"); } printf("\n"); decode_bch(); /* DECODE received codeword recv[] */ /* * 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"); }