⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ldpc_main.cpp

📁 LDPC的c代码 包括编码 解码 加噪声 校验矩阵 生成矩阵
💻 CPP
字号:
#include "LDPC_head.h"


unsigned int	test_num=10000;
double          ber=0; 
double          error_bit=0; //the number of error bits
//long            now;

matrix_bit *get_matrix();
//matrix_bit *Gen(unsigned int M, unsigned int N, unsigned int t);
unsigned int test(struct matrix_bit *matrix);
node *space(struct matrix_bit *matrix);
code *encode(struct matrix_bit *matrix, unsigned int *message);
code *Gaussian_channel(code *codewordsk,double n0);
unsigned int decode(struct code *codewords,struct matrix_bit *matrix,struct node *head1, unsigned int *message,double n0);


void main()
{
	unsigned int i;
	unsigned int j;
	unsigned int noise;
	unsigned int num;  //the number of tested codewords
	unsigned int success=0; //decoding judge symbol
	unsigned int k=0;

	double N0; //the SNR
	double n0; //the power spectral density

	struct matrix_bit *matrix;
	struct node	*head;
	unsigned int *message;
	code *codewords;
	int error; //the number of error codewords
	FILE	*fp;

	//get the matrix from file "matrix.txt"
	cout<<"get the matrix......."<<endl;
	matrix=get_matrix();
 
	//generate the double circle link
	cout<<"appling for the corresponding space......"<<endl;
	head=space(matrix);
	i=test(matrix);
	if(i)
	{
		cout<<"matrix error!"<<endl;
	}

	srand(time(NULL));
	cout<<"starting simulation....."<<endl;
	fp=fopen("result.txt","w+");
	message=(unsigned int *) malloc ((sizeof(unsigned int))*matrix->M1);
	for(noise=8;noise<=12;noise++)
	{
		N0=0.25*noise;//here N0 denotes Eb/N0(dB)
		n0=pow(10,-(double)N0/10)*matrix->N/matrix->M1;//the noise power spectral density (symbol power equal 1)
		ber=0;
		error_bit=0;
		success=0;
		error=0;
		for(num=0;num<test_num;num++)
		/*****for(num=0;num>=0;num++)****/
		{
			for(i=0;i<matrix->M1;i++)
			{
				message[i]=rand()%2;//generate the message bits
			}

			codewords=encode(matrix,message);
			codewords=Gaussian_channel(codewords,n0);
			j=0;

			j=decode(codewords,matrix,head,message,n0);
			if(j==0)
			{
				success++;
			}
			else
			{
				error++;
			}
		
			error_bit+=j;
			ber=error_bit/(double)((num+1)*matrix->M1);
			if((num+1)%50==0)
			{
				cout<<"N0="<<N0<<"  num="<<num+1<<"  success="<<success<<" error_bit="<<error_bit<<"  error_code="<<error<<"  ber="<<ber<<endl;
			}			
			free(codewords->codeword);
			free(codewords);
			if(((error>1000)&&(num>test_num))||(num>10*test_num))
			{
				break;
			}
		}
		fprintf(fp,"N0=%f  num=%d  ber=%f\n",N0,num,ber);
	}
	free(message);
	free(matrix->G);
	free(matrix->H);
	free(matrix);
	free(head);
	fclose(fp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -