www.pudn.com > LDPCCode.rar > LDPC.c
#include "LDPC.h"
/************************************************************************************************************************
* NAME : void init_H_para(short *mat_para, FILE *f_H_data)
* ABSRTACT : initilize the matrix parameter by reading the data from the file
* ENTRY : input : FILE *f_H_data : the exited file download from the Mackay web
output : short *mat_para : contain these parameters,see detail in program
* return: void
*************************************************************************************************************************/
void init_mat_para(short *mat_para, FILE *f_H_data)
{
short COLUMN,H_ROW;
short Max_row_weight, Max_col_weight;//the max weight of the row and column,also suitable for irregular GF(2) LDPC
fscanf(f_H_data,"%hd",&COLUMN);//read from the file
fscanf(f_H_data,"%hd",&H_ROW);
fscanf(f_H_data,"%hd",&Max_col_weight);
fscanf(f_H_data,"%hd",&Max_row_weight);
mat_para[0] = COLUMN;//matrix column for both H and G
mat_para[1] = H_ROW;//matrix row for H
mat_para[2] = mat_para[0] - mat_para[1];//G_ROW : row of Generator matrix
mat_para[3] = Max_col_weight;
mat_para[4] = Max_row_weight;
}
/************************************************************************************************************************
* NAME : void array_sort (short *in , short length , short init_min)
* ABSTRACT : the total function aims to sort the series in decensend
* ENTRY : input : short *in;
short length;
short init_min;
output :short *in;
* RETURN : void
***********************************************************************************************************************/
void array_sort (short *in , short length , short init_min)
{
short i;
short j;
short temp;
short min;
short min_index;
for(i=0;i=0)
{
reorder_flag = -1;
memset((*t),0,sizeof(short)*H_ROW*COLUMN);//(* t)[] is a pointer varibles pointing to a array contain H_ROW*COLUMN element
//build the H matrix
for(i=0;i=0)
{
//store the all zeros row's postion temparoly
for(j=0;j<=Max_col_weight;j++)
temp_col[j] = mat_col[reorder_flag*(Max_col_weight+1)+j];
//column shift
for(k=(reorder_flag-1);k>=0;k--)
for(j=0;j<=Max_col_weight;j++)
mat_col[(k+1)*(Max_col_weight+1)+j] = mat_col[k*(Max_col_weight+1)+j];
for(j=0;j<=Max_col_weight;j++) mat_col[j] = temp_col[j];
}
//if reorder_flag ,reorder column
} //end of while
free(temp_col);
}
/****************************************************************************************************************
* NAME : void Construct_G_Matrix (short *mat_col , short *mat_row , short **p_gen, short *mat_para)
* ABSTRACT : constuct G matrix
* ENTRY : input : short *mat_col :
short *mat_row :
short *mat_para:
output : short **p_gen : G matrix
* RETURN : void
****************************************************************************************************************/
void Construct_G_Matrix (short *mat_col , short *mat_row , short **p_gen, short *mat_para)
{
int i,j,k;
short *t;
short *gen;
short reorder_flag=0,temp;
short COLUMN , G_ROW , H_ROW;
short Max_row_weight , Max_col_weight;
COLUMN = mat_para[0];
H_ROW = mat_para[1];
G_ROW = mat_para[2];
Max_col_weight = mat_para[3];
Max_row_weight = mat_para[4];
gen = *p_gen;
if ((t = (short *) malloc(sizeof(short)*(H_ROW*COLUMN))) == NULL)
{
printf("can not malloc \n");
}
gauss_elimate_to_P_I(mat_col,mat_row,&t,&temp,mat_para);
printf("the system check_sum matrix\n");
/*construct generate matrix*/
G_ROW = G_ROW + temp;
mat_para[2] = G_ROW;
if(temp!=0)
{
free(gen);
if ((gen = (short *) malloc(sizeof(short)*(G_ROW*COLUMN))) == NULL)
printf("can not malloc \n");
}
for(i=0;imat_param[0] = mat_param[0];//COLUMN
p_ldpc_param->mat_param[1] = mat_param[1];//H_ROW
p_ldpc_param->mat_param[2] = mat_param[2];//G_row
p_ldpc_param->mat_param[3] = mat_param[3];//MAX_COLUMN_WEIGHT
p_ldpc_param->mat_param[4] = mat_param[4];//MAX_ROW_WEIGHT
p_ldpc_param->MatCol = (short*) malloc(mat_param[0]*(mat_param[3]+1)*sizeof(short));
p_ldpc_param->MatRow = (short*) malloc(mat_param[1]*(mat_param[4]+1)*sizeof(short));
p_ldpc_param->GMatrix = (short*) malloc(mat_param[0]*mat_param[2]*sizeof(short));
p_ldpc_param->iterC = 100;
Construct_H_Matrix(p_ldpc_param->mat_param, p_ldpc_param->MatCol, p_ldpc_param->MatRow, fpLDPC);
Construct_G_Matrix(p_ldpc_param->MatCol, p_ldpc_param->MatRow, &(p_ldpc_param->GMatrix), p_ldpc_param->mat_param);
}
void LDPC_encode(short *gen, short *input, short *output, short *mat_para)
{
short i , j;
short COLUMN , G_ROW , H_ROW;
short Max_row_weight , Max_col_weight;
COLUMN = mat_para[0];
H_ROW = mat_para[1];
G_ROW = mat_para[2];
Max_col_weight = mat_para[3];
Max_row_weight = mat_para[4];
memset(output,0,sizeof(short)*COLUMN);
for(i=0;i Inf)
{
tmp = Inf;
}
if (x < -Inf)
{
tmp = -Inf;
}
return(tmp);
}
/**************************************************************************************************************************
* NAME : short check_sum(short *mat_row, short *cc, short length, short row_weight)
* ABSTRACT : check whether the parity equations satisfied based on the encoded bits
* ENTRY : input : short *mat_row :
short *cc : the encoded bits
short length : the check equations number
short row_weight : the max_row _weight
* RETURN : flag whether the parity equations satisfied : flag = 0 ,satisfied
*************************************************************************************************************************/
short check_sum(short *mat_row, short *cc, short length, short row_weight)
{
int i,j;
short temp , flag=0;
short weight , col;
for(i=0;i0)
{
a = 1;
b = vcQ[k];
}
else
{
a = -1;
b = -vcQ[k];
}
T = T*a;
S = S + log((exp(b)+1)/(exp(b)-1));
}
}
tmp = T * log((exp(S)+1)/(exp(S)-1));
tmp = saturate(tmp, 100);
return(tmp);
}*/
/*
// the Min-Sum Decoder
double Horiz_compute_llr(double *vcQ, int row_weight, int ex_vnode)
{
double T;
int k;
double tmp,a,b,min;
T = 1;
min = 1000000;
for(k=0; k0)
{
a = 1;
b = vcQ[k];
}
else
{
a = -1;
b = -vcQ[k];
}
T = T*a;
if(b(2*fabs(x+y))))
return 0.5;
else if( (fabs(x-y)<2) && ( (fabs(x+y)) > (2*fabs(x-y)) ) )
return -0.5;
else return 0.0;
}
double Horiz_compute_llr(double *vcQ, int row_weight, int ex_vnode)
{
double T;
int k;
double tmp,a,b,min,factor,tmpa,tmpb;
T = 1;
min = 1000000;
tmp = T * min;
for(k=0; k0)
{
a = 1;
b = vcQ[k];
}
else
{
a = -1;
b = -vcQ[k];
}
if(tmp>0)
{
tmpa = 1;
tmpb = tmp;
}
else
{
tmpa = -1;
tmpb = -tmp;
}
if(b0)&&(iter0)
{
dec_out[i] = 1;
}
else
{
dec_out[i] = 0;
}
}
flag = check_sum(mat_row, dec_out, H_row, Max_row_weight);
iter++;
}
if (flag == 0)
{
for(i=0; i 0)? 1000:-1000;
}
}
free(table_col);
free(table_row);
free(row_indx);
free(col_indx);
}