功能描述:
LDPC編碼分為隨機LDPC和準循環LDPC;而準循環LDPC(QC-LDPC)碼是一類具有低復雜度編碼的構造碼,它可以利用簡單的移位寄存器完成編碼,其復雜度與生成矩陣有關。所以準循環LDPC的編碼中的H矩陣主要是通過對一個基礎矩陣的移位擴展得到的。此外,在構造QC-LDPC的時候,通常使用的方法是進行高斯消元法進行實現的。這里,還得明確一個概念,QC-LDPC指的是你所構造的H矩陣的性質為準循環結構,而高斯消元法是指的時候構造這種準循環結構的一種方法,所以這里并不矛盾。準循環結構式相對于隨機校驗結構而言的。
而對于多進制的情況,這里采用的方法,由于多進制H矩陣的構造非常復雜,其最大的區別是需要從多進制伽羅達域考慮。代碼中,是使用C進行計算的,這樣可以進行快速得到H矩陣。
注意,多進制QC矩陣的構造有很多方法,其和二進制QC矩陣的構造是完全不同的,這里注意區別,不要和二進制的QC矩陣放在一起考慮,通常多進制所使用的方法有:
基于GF域的多進制QC-LDPC,基于加法群的多進制QC-LDPC以及基于RS碼的多進制QC-LDPC。這里所采用的方法為:
基于在有限元伽羅達域GF(q)上進行準循環矩陣的構造(即代碼中的C語言)。
部分內核代碼:
#include
#include "mex.h"
/* Input Arguments: parameters*/
#define M_IN prhs[0] /* number of parity checks */
#define N_IN prhs[1] /* blocklength */
#define T_IN prhs[2] /* mean column weight */
#define Q_IN prhs[3] /* GF base */
#define SEED_IN prhs[4] /* seed for random generator */
/* Output Arguments: matrices*/
#define H_OUT plhs[0]
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]
)
{
short **M_list, **N_list, *M_target;
double *pp,*sr,*s,*ss;
int N,M,q,i,j,k,nzmax,*irs,*jcs,l,
tr,tm,tc,done,redo,tmp,regime,tr_max,t_max, m_low;
float t;
long seed;
void adjust(int *, int *, int *, int *);
unsigned int K2,M2;
char c;
mxArray *arg_in[2], *arg_out[1]; /* to call rand generator of Matlab*/
/* Check for proper number of arguments */
if (nrhs != 5) {
mexErrMsgTxt("GENERATE requires five input arguments.");
} else if (nlhs > 1) {
mexErrMsgTxt("GENERATE requires one output argument.");
}
pp = mxGetPr(N_IN); N = (int) (*pp);
pp = mxGetPr(M_IN); M = (int) (*pp);
pp = mxGetPr(T_IN); t = (float) (*pp);
pp = mxGetPr(Q_IN); q = (int) (*pp);
pp = mxGetPr(SEED_IN); seed = (int) (*pp);
arg_in[0] = mxCreateString("state");
arg_in[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
s = mxGetPr(arg_in[1]);
s[0] = seed; /* this will be used to call rand*/
/* initialize random generator */
mexCallMATLAB(0, NULL, 2, arg_in, "rand"); /* rand('state',seed) */
s[0] = 1; /* use s to store "1"*/
t_max=(int)ceil(t);
M_list=(short **)mxMalloc(N*sizeof(short *));
M_target=(short *)mxMalloc(M*sizeof(short *));
N_list=(short **)mxMalloc(M*sizeof(short *));
for(i=0;i
M_list[i]=(short *)mxMalloc((t_max+1)*sizeof(short));
}
i=0;
K2=0;
if(t<3){
K2=ceil((double)N*(3-t));
if(K2>M)
mexErrMsgTxt("GENERATE: Can't have more than M weight 2 columns.");
j=2;
done=0;
for(i=0;!done;i++){
M2=floor((double)M/(double)j);
if((M2*(j-1))>=K2) done=1;
j*=2;
}
M2*=(j/4);
}
/*
* i contains number of identity blocks we'll need.... */
tr=((short)floor((double)(t*N)/(double)M));
if (i>tr){
tr_max=i;
done=0;
k=1;
j=floor((double)t*N)-2*K2; /* Number of ones left to distribute */
for(i=0;!done;i++){
/* (M-2*M2) rows will be empty after identity blocks */
j-=((M-2*M2)+(2*M2*(k-1))/k);
if(j<0) {
done=1;
}
else {
k*=2;
}
}
tr=i-1;
tm=M+j;
}
else {
/* This is easier! */
tr_max=tr+1;
tm=(int)floor((((double)t*N)/(double)M-tr)*M +0.5);
}
tc=M-tm;
for(i=0;i
N_list[i]=(short *)mxMalloc((tr_max+1)*sizeof(short));
}
for(i=0;i
N_list[i][0]=0;
}
regime=0;
j=M2;
k=0;
i=0;
while(i
for(;(i-k)
M_list[i][0]=2;
M_list[i][1]=i;
M_list[i][2]=i+j;
N_list[i][0]++;
if(N_list[i][0]==tr) adjust(&tm,&tr,&tc,®ime);
N_list[i][N_list[i][0]]=i;
N_list[i+j][0]++;
if(N_list[i+j][0]==tr) adjust(&tm,&tr,&tc,®ime);
N_list[i+j][N_list[i+j][0]]=i;
}
k=i;
j/=2;
}
/* Now fill the unsystematic columns, ensuring weight per row as even as poss. */
i=K2;
if(K2==0){
/* Fill low weight columns */
for(i=0;i<(int)(N*(t_max-t)+0.5);i++){
for(k=1;k<=(int)floor(t);k++){
done=0;
do {
mexCallMATLAB(1, arg_out,1 , &arg_in[1], "rand"); /* ss = rand(1) */
ss = mxGetPr(arg_out[0]);
j=(short)floor(M*ss[0]);
mexCallMATLAB(1, arg_out,1 , &arg_in[1], "rand");
ss = mxGetPr(arg_out[0]);
if((ss[0])<(1-(double)N_list[j][0]/(double)tr)) {
done=1;
for(l=1;l
}
} while(!done);
N_list[j][0]++;
N_list[j][N_list[j][0]]=i;
if(N_list[j][0]==tr) adjust(&tm,&tr,&tc,®ime);
M_list[i][k]=j;
}
M_list[i][0]=k-1;
}
}
redo=1;
for(;i
fprintf(stderr,"%d\r",i);
for(k=1;k<=t_max;k++){
done=0;
do {
/* find the lowest weight rows, and fill one of them */
if(redo){
l=tr_max;
for(j=0;j
m_low=0;
for(j=0;j
}
mexCallMATLAB(1, arg_out,1 , &arg_in[1], "rand");
ss = mxGetPr(arg_out[0]);
j=M_target[tmp=(short)floor(m_low*ss[0])];
/* if(ss[0]<(1-(double)N_list[j][0]/(double)tr)) {*/
done=1;
for(l=1;l
if(done==1){
if(m_low==1) redo=1;
else {
for(;tmp<(m_low-1);tmp++) M_target[tmp]=M_target[tmp+1];
m_low--;
redo=0;
}
}
/* }*/
} while(!done);
N_list[j][0]++;
N_list[j][N_list[j][0]]=i;
if(N_list[j][0]==tr) adjust(&tm,&tr,&tc,®ime);
M_list[i][k]=j;
}
M_list[i][0]=k-1;
}
tr=((short)ceil((double)(3*N-K2)/(double)M));
for(i=0;i
mxFree(N_list[i]);
}
mxFree(N_list);
/* done generating H matrix - positions only */
/* Allocate space for sparse matrix */
nzmax=0; for(j=0 ; j
mexPrintf("%d \n",nzmax);
if (N>nzmax){
nzmax=N;
}
plhs[0] = mxCreateSparse(M,N,nzmax,mxREAL);
sr = mxGetPr(plhs[0]);
irs = mxGetIr(plhs[0]); /* row */
jcs = mxGetJc(plhs[0]); /* column */
k = 0;
for (j=0; (j
jcs[j] = k;
for (i=1; (i<=M_list[j][0] ); i++) {
mexCallMATLAB(1, arg_out,1 , &arg_in[1], "rand"); /* ss = rand(q) */
ss = mxGetPr(arg_out[0]);
sr[k] = floor(1 + ss[0]*(q-1));
irs[k] = M_list[j][i];
k++;
}
}
jcs[N] = k;
for(i=0;i
mxFree(M_list[i]);
}
mxFree(M_list);
return;
}
void adjust(int *tm, int *tr, int *tc, int *regime){
switch(*regime){
case 0:
(*tc)--;
if((*tc)==0){
*regime=1;
(*tr)++;
}
break;
case 1:
(*tm)--;
if((*tm)==0){
*regime=2;
(*tr)--;
}
break;
}
}