📄 decode.c
字号:
/*
** FILE : decode.c
** PURPOSE : The functions in this file enable decoding of a binary
** data-file, given the generator polynomial of the code.
** AUTHOR : Bart De Canne, Belgium (bdc@barco.be)
** COMPILER: BC++ 3.1 compiler
** DEPENDENCIES:
** Include-files: galois.h
** DATE : 01-09-1994
** VERSION : 1.0
*/
#define _DECODE
#include "galois.h"
static bool get_syndromes (void );
static void make_error_locator_polynomial (void );
static void get_error_values (void );
static int tmp;
static long blck_cnt;
static long unrec_blck;
static long err_blck;
static pPOLYNOM pp_code=NULL, /* codeword */
pp_data=NULL, /* decoded data */
pp_s=NULL; /* error-locator polynomial */
static int *p_err, /* error values */
*p_syn; /* syndromes */
/*
** Decodes data using current <pp_gen>.
** Sets some statistics parameters, interpreted afterwards by
** PrintDecodeStatistics().
*/
void DecodeData(void) {
int i;
BEGIN("DecodeData");
blck_cnt=unrec_blck=err_blck=0L;
OpenInputStream(InFile);
OpenOutputStream(OutFile);
pp_code=AllocPolynom(pp_code,pp_gen->Base,N-1);
pp_data=AllocPolynom(pp_data,pp_gen->Base,K-1);
p_syn=(int*)malloc((2*t+1)*sizeof(int));
/* p_syn[1] .. p_syn[2t] -> syndromes */
pp_s=AllocPolynom(pp_s,pg_l->Base,N); /* p_syn[0] will be fixed to 1*/
p_err=(int*)malloc(N*sizeof(int)); /* so p_syn[1]=S(1) for */
if(p_syn==NULL || p_err==NULL) ERROR; /* convenience */
START_PACIFIER;
length /= (long)N*bps;
do{
PACIFIER(100*blck_cnt/length);
memset(p_err,0,N*sizeof(int));
ToZero(pp_s);
READ_PP(pp_code);
if(get_syndromes()) { /* errors in this block! */
err_blck++;
make_error_locator_polynomial();
get_error_values();
/* correct received codeword */
for(i=pp_code->MaxDeg;i>=0;i--)
pp_code->c[i] = po_l->ADD(p_err[i],pp_code->c[i]);
DEGREE(pp_code);
if(!DivPoly(pp_code,pp_gen,pp_data,NULL,po_s))
unrec_blck++;
}
else if(!DivPoly(pp_code,pp_gen,pp_data,NULL,po_s)) {
/* This is a serious error. It should never
occur...
*/
printf("\nFATAL:Errors detected while syndromes are zero."
"\nblock # : %ld",blck_cnt);
printf("\ncode : ");PrintPoly(pp_code);
printf("\ngenerator: ");PrintPoly(pp_gen);
printf("\n");
ERROR;
}
blck_cnt++;
WRITE_PP(pp_data);
} while(!EOFReached());
free(p_err);free(p_syn);
pp_code=FreePolynom(pp_code);
pp_data=FreePolynom(pp_data);
pp_s=FreePolynom(pp_s);
STOP_PACIFIER;
}
void PrintDecodeStatistics(void) {
printf("\nDecoding statistics:"
"\n--------------------"
"\n# decoded blocks : %ld"
"\n# blocks containing errors : %ld"
"\n unrecoverable : %ld\n",
blck_cnt,err_blck,unrec_blck);
}
/*
** Calculates 2*t syndromes S(郶i) for the received codeword polynomial.
** The syndrome-array <p_syn> should be initialized upon entry (size: 2*t+1 )
** Convention: p_syn[i] = S(郶i) and p_syn[0]=1 always.
** Returns TRUE if errors have occurred (i.e. syndromes != 0 found) / FALSE
** otherwise
*/
static bool get_syndromes(void) {
int i,err=0,*ps;
ps=p_syn;
*ps++=1;
for(i=2;i<=2*t+1;i++) { /* 郶1 .. 郶2t */
*ps=f(pp_code,pg_l,po_l,i);
if(*ps++) err++;
}
if(err)
return TRUE;
return FALSE;
}
/*
** Constructs error locator polynomial given the syndromes (S1..S2t).
** <pp_s> should be intialized by caller.
*/
static void make_error_locator_polynomial(void) {
int i,g,L,dif;
pPOLYNOM pp_b=NULL,pp_s2=NULL,pp_tmp=NULL;
/* Allocation */
pp_b=AllocPolynom(pp_b,po_l->Base,po_l->MaxNo);
pp_s2=AllocPolynom(pp_s2,po_l->Base,po_l->MaxNo);
pp_tmp=AllocPolynom(pp_tmp,po_l->Base,po_l->MaxNo);
/* Initialization */
SET_POLY(pp_s,0,1);
SET_POLY(pp_b,0,1);
L=0;
/* Iteration */
for(g=1;g<=2*t;g++) {
for(i=0,dif=0;i<=L;i++) { /* dif =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -