📄 decode.cpp
字号:
#include "LDPC_head.h"
#include <stdlib.h>
struct code *initializtion(struct code *codewords,double n0);
void one_step(struct matrix_bit *matrix, struct node *head,struct code *f);
double basic(double x,double y);
struct code *judge(struct node *head,struct code *codewords,struct code *f);
unsigned int decode(struct code *codeword,struct matrix_bit *matrix,struct node *head1, unsigned int *message,double n0)
{
code *codewords;
code *final_code;
code *f;
node *head;
node *p1;
// node *p2;
unsigned int i;
unsigned int j;
unsigned int k;
head=head1;
codewords=codeword;
//calculate the LLRs of every bit
f=initializtion(codewords,n0);
for(i=0;i<matrix->N;i++)
{
p1=head->right+i;
p1->Q=f->codeword[i];
p1->R=0;
p1=p1->down;
while(p1->row!=-1)
{
p1->Q=f->codeword[i];
p1->R=0;
p1=p1->down;
}
}
for(i=0;i<iterative_num;i++)
{
// cout<<i<<endl;
one_step(matrix,head,f);
// cout<<i<<endl;
final_code=judge(head,codewords,f);
j=0;
for(k=0;k<matrix->M1;k++)
{
if(message[k]!=(unsigned int)final_code->codeword[k])
{
j++;
}
}
free(final_code->codeword);
free(final_code);
if(j==0)
{
break;
}
}
free(f->codeword);
free(f);
return (j);
}
//calculate the LLRs of every bit
code *initializtion(struct code *codewords,double n0)
{
unsigned int i;
code *f;
f=(struct code *) malloc (sizeof(struct code));
f->length=codewords->length;
f->codeword=(double *) malloc ((sizeof(double))*f->length);
for(i=0;i<f->length;i++)
{
// when LLRs decoding
f->codeword[i]=4*codewords->codeword[i]/n0;
// when LRs decoding
// f->codeword[i]=exp(-4*(codewords->codeword[i])/n0);
}
return (f);
}
double basic(double x,double y)
{
// when LLRs decoding
double z;
z=log((1+exp(x+y))/(exp(x)+exp(y)));
//overlow?
if(z>MAX)
{
z=MAX;
}
else if(z<-MAX)
{
z=-MAX;
}
return z;
// when LRs decoding
/* double z;
z=(x+y)/(1+x*y);
if(z>MAX)
{
z=MAX;
}
return z;
*/
}
void one_step(struct matrix_bit *matrix, struct node *head,struct code *f)
{
// when LLRs decoding
double temp;
node *p1;
node *p2;
//horizontal step;
p1=head->down;
while(p1->row!=-1)
{
p2=p1->right;
temp=p2->Q;
p2=p2->right;
while(p2->col!=-1)
{
p2->R=temp;
temp=basic(temp,p2->Q);
p2=p2->right;
}
p2=p1->left;
temp=p2->Q;
p2=p2->left;
while(p2->left->col!=-1)
{
p2->R=basic(p2->R, temp);
temp=basic(temp, p2->Q);
p2=p2->left;
}
p2->R=temp;
p1=p1->down;
}
//vertical step
p1=head->right;
while(p1->col!=-1)
{
p2=p1->down;
temp=f->codeword[p2->col];
while(p2->row!=-1)
{
p2->Q=temp;
temp+=p2->R;
p2=p2->down;
}
p2=p1->up;
temp=p2->R;
p2=p2->up;
while(p2->row!=-1)
{
p2->Q+=temp;
temp+=p2->R;
p2=p2->up;
}
p1=p1->right;
}
// when LRs decoding
/*
double temp;
node *p1;
node *p2;
//horizontal step;
p1=head->down;
while(p1->row!=-1)
{
p2=p1->right;
temp=0;
while(p2->col!=-1)
{
p2->R=temp;
temp=basic(temp,p2->Q);
p2=p2->right;
}
p2=p1->left;
temp=0;
while(p2->col!=-1)
{
p2->R=basic(p2->R, temp);
temp=basic(temp, p2->Q);
p2=p2->left;
}
p1=p1->down;
}
//vertical step
p1=head->right;
while(p1->col!=-1)
{
p2=p1->down;
temp=f->codeword[p2->col];
while(p2->row!=-1)
{
p2->Q=temp;
temp*=p2->R;
if(temp>MAX)
{
temp=MAX;
}
p2=p2->down;
}
p2=p1->up;
temp=1;
while(p2->row!=-1)
{
p2->Q*=temp;
temp*=p2->R;
if(temp>MAX)
{
temp=MAX;
}
p2=p2->up;
}
p1=p1->right;
}
*/
}
code *judge(struct node *head,struct code *codewords,struct code *f)
{
// when LLRs decoding
int i;
node *p;
code *final_code;
final_code=(struct code *)malloc(sizeof(struct code));
final_code->length=codewords->length;
final_code->codeword=(double *)malloc((sizeof(double))*final_code->length);
p=head->right;
i=0;
while(p->col!=-1)
{
if((p->down->Q+p->down->R)>0)
{
final_code->codeword[i]=0;
}
else
{
final_code->codeword[i]=1;
}
p=p->right;
i++;
}
return(final_code);
// when LRs decoding
/*
int i;
node *p;
code *final_code;
final_code=(struct code *)malloc(sizeof(struct code));
final_code->length=codewords->length;
final_code->codeword=(double *)malloc((sizeof(double))*final_code->length);
p=head->right;
i=0;
while(p->col!=-1)
{
if(p->down->Q*p->down->R>1)
{
final_code->codeword[i]=1;
}else
{
final_code->codeword[i]=0;
}
p=p->right;
i++;
}
return(final_code);
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -