?? bch_decoder.c
字號:
fprintf(stdout," 0x%x", elp[u][i]);
fprintf(stdout,"\n");
fprintf(stdout," deg(elp) = %2d --> elp(%2d):", L[u+2], u+2);
for (i=0; i<=L[u+2]; i++)
fprintf(stdout," 0x%x", elp[u+2][i]);
fprintf(stdout,"\n");
fprintf(stdout," u_L[%2d] = %2d\n", u, u_L[u]);
fprintf(stdout," u_L[%2d] = %2d\n", u+2, u_L[u+2]);
}
} while ((u < (ttx2-1)) && (L[u + 2] <= tt));
if (Verbose) fprintf(stdout,"\n");
u=u+2;
L[ttx2-1] = L[u];
if (L[ttx2-1] > tt)
decode_flag = 0;
else {
// Chien's search to find roots of the error location polynomial
// Ref: L&C pp.216, Fig.6.1
if (Verbose) fprintf(stdout,"Chien Search: L[%d]=%d=%x\n", ttx2-1,L[ttx2-1],L[ttx2-1]);
if (Verbose) fprintf(stdout,"Sigma(x) = \n");
if (Verbose)
for (i = 0; i <= L[u]; i++)
if (elp[u][i] != 0)
fprintf(stdout," %4d (%4d)\n", elp[u][i], index_of[elp[u][i]]);
else
fprintf(stdout," 0\n");
for (i = 1; i <= L[ttx2-1]; i++) {
reg[i] = index_of[elp[u][i]];
if (Verbose) fprintf(stdout," reg[%d]=%d=%x\n", i,reg[i],reg[i]);
}
count = 0 ;
// Begin chien search
for (i = 1; i <= nn; i++) {
elp_sum = 1 ;
for (j = 1; j <= L[ttx2-1]; j++)
if (reg[j] != -1) {
reg[j] = (reg[j] + j) % nn ;
elp_sum ^= alpha_to[reg[j]] ;
}
// store root and error location number indices
if (!elp_sum) {
location[count] = nn - i ;
if (Verbose) fprintf(stdout,"count: %d location: %d L[ttx2-1] %d\n",
count,location[count],L[ttx2-1]);
count++ ;
}
}
// Number of roots = degree of elp hence <= tt errors
if (count == L[ttx2-1]) {
decode_flag = 1 ;
// Correct errors by flipping the error bit
for (i = 0; i < L[ttx2-1]; i++)
recd[location[i]] ^= 1 ;
}
// Number of roots != degree of ELP => >tt errors and cannot solve
else
decode_flag = 0 ;
}
}
}
int main(int argc, char** argv)
{ int i, j ;
int Help ;
int Input_kk, Output_Syndrome ; // Input & Output switch
int in_count, in_v, in_codeword; // Input statistics
int decode_success, decode_fail; // Decoding statistics
int code_success[kk_max], code_fail[kk_max]; // Decoded and failed words
int codeword[kk_max], recd_data[kk_max], recd_parity[kk_max] ;
char in_char;
fprintf(stderr, "# Binary BCH decoder. Use -h for details.\n\n");
Verbose = 0;
Input_kk = 0;
Help = 0;
mm = df_m;
tt = df_t;
Parallel = df_p;
decode_success = 0;
decode_fail = 0;
for (i=1; i < argc;i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'm': mm = atoi(argv[++i]);
break;
case 't': tt = atoi(argv[++i]);
break;
case 'p': Parallel = atoi(argv[++i]);
break;
case 'k': kk_shorten = atoi(argv[++i]);
Input_kk = 1;
if (kk_shorten % 4 != 0) {
fprintf(stderr, "### k must divide 4.\n\n");
Help = 1;
}
break;
case 's': Output_Syndrome = 1;
break;
case 'v': Verbose = 1;
break;
default: Help = 1;
}
}
else
Help = 1;
}
if (Help == 1) {
fprintf(stdout,"# Usage %s: BCH decoder\n",argv[0]);
fprintf(stdout," -h: This help message\n");
fprintf(stdout," -m <field>: Galois field, GF, for code. Code length is 2^<field>-1.\n");
fprintf(stdout," The default value is %d for a code length %d. If the parameter is\n", df_m, (int)pow(2,df_m) - 1);
fprintf(stdout," set to 0, the program will estimate the value based upon the values\n");
fprintf(stdout," chosen for k and t.\n");
fprintf(stdout," -t <correct>: Correction power of the code. Default = %d\n", df_t);
fprintf(stdout," -k <data bits>: Number of data bits to be encoded. Must divide 4.\n");
fprintf(stdout," The default value is the maximum supported by the code which\n");
fprintf(stdout," depends upon the field (-m) and the correction (-t) chosen.\n");
fprintf(stdout," -p <parallel>: Parallelism in decoder. Does not effect results but\n");
fprintf(stdout," does change the algorithm used to generate them. Default = %d\n", df_p);
fprintf(stdout," -s Syndrome output after the decoded data. Default disabled. \n");
fprintf(stdout," -v Verbose mode. Output detailed information, such as encoded codeword,\n");
fprintf(stdout," received codeword and decoded codeword. Default disabled. \n");
fprintf(stdout," <stdin>: character string to decode in hex format. All other \n");
fprintf(stdout," characters are ignored. Comments are enclosed in brackets: { }.\n");
fprintf(stdout," The hex values are converted to binary and taken <data bits> \n");
fprintf(stdout," at a time.\n");
fprintf(stdout," <stdout>: resulting decoded character string in hex format.\n");
fprintf(stdout," <stderr>: information about the decode process as well as error messages.\n");
}
else {
nn = (int)pow(2, mm) - 1;
nn_shorten = nn;
// generate the Galois Field GF(2**mm)
generate_gf() ;
// Compute the generator polynomial and lookahead matrix for BCH code
gen_poly() ;
// Check if code is shortened
if (Input_kk == 1)
nn_shorten = kk_shorten + rr ;
else {
kk_shorten = nn_shorten - rr ;
// Make the shortened length divide 4
kk_shorten = kk_shorten - kk_shorten % 4 ;
nn_shorten = kk_shorten + rr ;
}
ttx2 = 2 * tt ;
fprintf(stdout, "{# (m = %d, n = %d, k = %d, t = %d) Binary BCH code.}\n\n", mm, nn_shorten, kk_shorten, tt) ;
// Set input data.
in_count = 0;
in_codeword = 0;
in_char = getchar();
while (in_char != EOF) {
if (in_char=='{') {
while ((in_char != EOF) && ((char)in_char != '}'))
in_char = getchar();
}
in_v = hextoint(in_char);
if (in_v != -1) {
for (i = 3; i >= 0; i--) {
if ((int)pow(2,i) & in_v)
codeword[in_count] = 1 ;
else
codeword[in_count] = 0 ;
in_count++;
}
}
if (in_count == ceil(nn_shorten / (double)4) * 4) {
in_codeword++ ;
// Parity check bits
for (j = kk_shorten; j < nn_shorten; j++)
recd[j - kk_shorten] = codeword[j] ;
// Data bits
for (j = 0; j < kk_shorten; j++)
recd[j + rr] = codeword[j] ;
decode_bch() ;
if ( decode_flag == 1 ) {
decode_success++ ;
code_success[decode_success] = in_codeword;
if (count == 0)
fprintf(stdout, "{ Codeword %d: No errors.}\n", in_codeword) ;
else {
fprintf(stdout, "{ Codeword %d: %d errors found at location:", in_codeword, count) ;
for (i = count - 1; i >= 0 ; i--) {
// Convert error location from systematic form to storage form
if (location[i] >= rr)
location[i] = location[i] - rr;
else
location[i] = location[i] + kk_shorten;
fprintf(stdout, " %d", location[i]) ;
}
fprintf(stdout, "}");
printf("\n");
}
}
else {
decode_fail++ ;
code_fail[decode_fail] = in_codeword;
fprintf(stdout, "{ Codeword %d: Unable to decode!}", in_codeword) ;
printf("\n");
}
// Convert decoded data into information data and parity checks
for (i = 0; i < kk_shorten; i++)
recd_data[i] = recd[i + rr];
for (i = 0; i < rr; i++)
recd_parity[i] = recd[i];
print_hex_low(kk_shorten, recd_data, stdout);
if (Output_Syndrome == 1) {
fprintf(stdout, " ");
print_hex_low(rr, recd_parity, stdout);
if (Verbose) fprintf(stdout,"rr: %d\n",rr);
}
fprintf(stdout, "\n\n");
in_count = 0;
}
in_char = getchar();
}
fprintf(stdout, "{### %d codewords received.}\n", in_codeword) ;
fprintf(stdout, "{@@@ %d codewords are decoded successfully:}\n{", decode_success) ;
for (i = 1; i <= decode_success; i++)
fprintf(stdout, " %d", code_success[i]);
fprintf(stdout, " }\n");
fprintf(stdout, "{!!! %d codewords are unable to correct:}\n{", decode_fail) ;
for (i = 1; i <= decode_fail; i++)
fprintf(stdout, " %d", code_fail[i]);
fprintf(stdout, " }\n");
}
return(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -