问题描述
一个对于LDPC码子译码的程序,在运行时出现了Unhandledexceptionat0x00194B81inLDPC_main.exe:0xC0000005:Accessviolationreadinglocation0x00000014.的错误,请哪位大神指导一下,要怎么修改!!!main函数:#include"LDPC_head.h"unsignedinttest_num=10000;doubleber=0;doubleerror_bit=0;//thenumberoferrorbitslongnow;matrix_bit*get_matrix();//matrix_bit*Gen(unsignedintM,unsignedintN,unsignedintt);unsignedinttest(structmatrix_bit*matrix);node*space(structmatrix_bit*matrix);code*encode(structmatrix_bit*matrix,unsignedint*message);code*Gaussian_channel(code*codewordsk,doublen0);unsignedintdecode(structcode*codewords,structmatrix_bit*matrix,structnode*head1,unsignedint*message,doublen0);voidmain(){unsignedinti;unsignedintj;unsignedintnoise;unsignedintnum;//thenumberoftestedcodewordsunsignedintsuccess=0;//decodingjudgesymbolunsignedintk=0;doubleN0;//theSNR信噪比doublen0;//thepowerspectraldensity功率谱密度structmatrix_bit*matrix;structnode*head;unsignedint*message;code*codewords;interror;//thenumberoferrorcodewordsFILE*fp;//getthematrixfromfile"matrix.txt"cout<<"getthematrix......."<<endl;matrix=get_matrix();//generatethedoublecirclelinkcout<<"applingforthecorrespondingspace......"<<endl;head=space(matrix);i=test(matrix);if(i){cout<<"matrixerror!"<<endl;}structtm*date_tm;time_tTime1;srand(time(&Time1));cout<<"startingsimulation....."<<endl;fp=fopen("result.txt","w+");message=(unsignedint*)malloc((sizeof(unsignedint))*matrix->M1);for(noise=32;noise<=32;noise++){N0=0.25*noise;//hereN0denotesEb/N0(dB)n0=pow(10,-(double)N0/10)*7/4;//*matrix->N/matrix->M1;//thenoisepowerspectraldensity(symbolpowerequal1)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]=0;//generatethemessagebits}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>1000*test_num)){break;}}fprintf(fp,"N0=%fnum=%dber=%fn",N0,num,ber);}free(message);free(matrix->G);free(matrix->H);free(matrix);free(head);fclose(fp);}读取H矩阵和G矩阵的函数:#include"LDPC_head.h"#include<stdlib.h>#include<iostream>usingnamespacestd;matrix_bit*get_matrix(){matrix_bit*matrix;unsignedinti;unsignedintj;FILE*fp;matrix=(structmatrix_bit*)malloc(sizeof(structmatrix_bit));//建立一个指向该结构体的指针fp=fopen("matrix2.txt","r");if(fp==NULL){cout<<"can'topenthefile!"<<endl;exit(0);}else{fscanf(fp,"%dn",&(matrix->N));//1008fscanf(fp,"%dn",&(matrix->M1));//504fscanf(fp,"%dn",&(matrix->M));//504fscanf(fp,"%dn",&(matrix->G_mcw));//281fscanf(fp,"%dn",&(matrix->H_mrw));//6//getG,N*G_mcwmatrix->G=(unsignedint*)malloc((sizeof(unsignedint))*matrix->N*matrix->G_mcw);for(i=0;i<matrix->N*matrix->G_mcw;i++)//i<1008*281{matrix->G[i]=0;}for(i=0;i<matrix->G_mcw;i++){for(j=0;j<matrix->N;j++){fscanf(fp,"%dn",&(matrix->G[i*matrix->N+j]));}}//getHmatrix->H=(unsignedint*)malloc((sizeof(unsignedint))*matrix->H_mrw*matrix->M);for(i=0;i<matrix->H_mrw*matrix->M;i++){matrix->H[i]=0;}for(i=0;i<matrix->M;i++){for(j=0;j<matrix->H_mrw;j++){fscanf(fp,"%dn",&(matrix->H[i*matrix->H_mrw+j]));}}//for(intpp=0;pp<50;pp++)//std::cout<<pp<<""<<matrix->H[pp]<<"n";fclose(fp);}return(matrix);}构造节点的函数,问题是出在这里的:#include"LDPC_head.h"#include<stdlib.h>node*space(structmatrix_bit*matrix){node*head;node*p1;node*p2;unsignedinti;unsignedintj;//unsignedintk;head=(structnode*)malloc(sizeof(structnode));head->row=-1;head->col=-1;head->left=NULL;head->right=NULL;head->up=NULL;head->down=NULL;head->Q=0;head->R=0;//分配行骨架节点head->right=(structnode*)malloc((sizeof(structnode))*matrix->N);for(i=0;i<matrix->N;i++){p1=head->right+i;p1->row=-1;p1->col=i;p1->up=NULL;p1->down=NULL;p1->Q=0;p1->R=0;if(i==0){p1->left=head;}else{p1->left=p1-1;}if(i==matrix->N-1){p1->right=head;}else{p1->right=p1+1;}}head->left=head->right+matrix->N-1;//分配列骨架节点head->down=(structnode*)malloc((sizeof(structnode))*matrix->M);for(i=0;i<matrix->M;i++){p1=head->down+i;p1->row=i;p1->col=-1;p1->left=NULL;p1->right=NULL;p1->Q=0;p1->R=0;if(i==0){p1->up=head;}else{p1->up=p1-1;}if(i==matrix->M-1){p1->down=head;}else{p1->down=p1+1;}}head->up=head->down+matrix->M-1;//构造体节点for(i=0;i<matrix->M;i++){for(j=0;j<matrix->H_mrw;j++){if(matrix->H[i*matrix->H_mrw+j]!=0){p1=(structnode*)malloc(sizeof(structnode));p1->row=i;p1->col=matrix->H[i*matrix->H_mrw+j]-1;p1->R=0;p1->Q=0;p2=head->right+matrix->H[i*matrix->H_mrw+j]-1;while(p2->down!=NULL){p2=p2->down;}p2->down=p1;p1->up=p2;p1->down=NULL;p2=head->down+i;while(p2->right!=NULL){p2=p2->right;}p2->right=p1;p1->left=p2;p1->right=NULL;}}}p1=head->right;for(i=0;i<matrix->N;i++){p2=p1->down;while(p2->down!=NULL)报错的时候显示的是这一行有问题{p2=p2->down;}p2->down=p1;p1->up=p2;p1++;}p1=head->down;for(i=0;i<matrix->M;i++){p2=p1->right;while(p2->right!=NULL){p2=p2->right;}p2->right=p1;p1->left=p2;p1++;}return(head);}译码函数:#include"LDPC_head.h"#include<stdlib.h>structcode*initializtion(structcode*codewords,doublen0);voidone_step(structmatrix_bit*matrix,structnode*head,structcode*f);doublebasic(doublex,doubley);structcode*judge(structnode*head,structcode*codewords,structcode*f);unsignedintdecode(structcode*codeword,structmatrix_bit*matrix,structnode*head1,unsignedint*message,doublen0){code*codewords;code*final_code;code*f;node*head;node*p1;//node*p2;unsignedinti;unsignedintj;unsignedintk;head=head1;codewords=codeword;//calculatetheLLRsofeverybitf=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]!=(unsignedint)final_code->codeword[k]){j++;}}free(final_code->codeword);free(final_code);if(j==0){break;}}free(f->codeword);free(f);return(j);}//calculatetheLLRsofeverybitcode*initializtion(structcode*codewords,doublen0){unsignedinti;code*f;f=(structcode*)malloc(sizeof(structcode));f->length=codewords->length;f->codeword=(double*)malloc((sizeof(double))*f->length);for(i=0;i<f->length;i++){//whenLLRsdecodingf->codeword[i]=4*codewords->codeword[i]/n0;//whenLRsdecoding//f->codeword[i]=exp(-4*(codewords->codeword[i])/n0);}return(f);}doublebasic(doublex,doubley){//whenLLRsdecodingdoublez;z=log((1+exp(x+y))/(exp(x)+exp(y)));//overlow?if(z>MAX){z=MAX;}elseif(z<-MAX){z=-MAX;}returnz;//whenLRsdecoding/*doublez;z=(x+y)/(1+x*y);if(z>MAX)
解决方案
解决方案二:
编码函数:#include"LDPC_head.h"#include<stdlib.h>code*encode(structmatrix_bit*matrix,unsignedint*message){code*code1;unsignedinti;unsignedintj;unsignedintk;code1=(structcode*)malloc(sizeof(structcode));code1->length=matrix->N;code1->codeword=(double*)malloc((sizeof(double))*code1->length);for(i=0;i<code1->length;i++){k=0;for(j=0;j<matrix->G_mcw;j++){if(matrix->G[j*matrix->N+i]!=0){k+=message[matrix->G[j*matrix->N+i]-1];}}if(k%2==0){code1->codeword[i]=1;}else{code1->codeword[i]=-1;}}return(code1);}模拟高斯信道函数:#include"LDPC_head.h"#include<stdlib.h>code*Gaussian_channel(code*codewords,doublen0){unsignedinti;doubleu1,u0;doublegaussnoise;for(i=0;i<codewords->length;i++){u0=(double)rand()/(double)RAND_MAX;u1=(double)rand()/(double)RAND_MAX;if(u0<limit){u0=limit;}gaussnoise=sqrt(n0*log(1./u0))*cos(2*pi*u1);//gaussnoise=0;codewords->codeword[i]+=gaussnoise;}return(codewords);}生成矩阵函数:#include"LDPC_head.h"#include<stdlib.h>matrix_bit*Gen(unsignedintM,unsignedintN,unsignedintt){matrix_bit*Matrix;unsignedint*col;unsignedint*row;unsignedint*check;unsignedinti;unsignedintj;unsignedintk;unsignedintl;unsignedintrank;unsignedintretry;longnow;//applyforthecorespondingspaceMatrix=(structmatrix_bit*)malloc(sizeof(structmatrix_bit));Matrix->H_mrw=M;Matrix->G_mcw=N;Matrix->H=(unsignedint*)malloc(sizeof(unsignedint)*(N-M)*N);Matrix->G=(unsignedint*)malloc(sizeof(unsignedint)*M*N);for(j=0;j<N;j++){for(i=0;i<M;i++){Matrix->G[i*N+j]=0;}}col=(unsignedint*)malloc((sizeof(unsignedint))*N);for(i=0;i<N;i++){col[i]=0;}row=(unsignedint*)malloc((sizeof(unsignedint))*N);//generatetheHatrandomloop1:for(j=0;j<N;j++){for(i=0;i<N-M;i++){Matrix->H[i*N+j]=0;}}for(i=0;i<N-M;i++){row[i]=0;}retry=0;structtm*date_tm;time_tTime1;srand(time(&Time1));for(j=0;j<N;j++){for(k=0;k<t;k++){i=rand()%(N-M);if((Matrix->H[i*N+j]==0)&&(row[i]<(double)(t*N/(N-M)))){Matrix->H[i*N+j]=1;row[i]++;}else{k--;retry++;if(retry>N*(N-M)){gotoloop1;}}}}//checkifHisfullrankcheck=(unsignedint*)malloc((sizeof(unsignedint))*(N-M)*N);for(i=0;i<N-M;i++){for(j=0;j<N;j++){check[i*N+j]=Matrix->H[i*N+j];//cout<<check[i*N+j]<<"";}//cout<<endl;}//changeHintouppertriangleformwhenchecktherankofHrank=0;for(j=0;j<N;j++){for(i=rank;i<N-M;i++){if(check[i*N+j]==1){//cout<<"("<<rank<<","<<j<<")"<<endl;for(k=0;k<N;k++){col[k]=check[i*N+k];check[i*N+k]=check[rank*N+k];check[rank*N+k]=col[k];}for(k=i+1;k<N-M;k++){if(check[k*N+j]==1){for(l=j;l<N;l++){check[k*N+l]^=check[rank*N+l];}}}rank++;/*for(k=0;k<N-M;k++){for(l=0;l<N;l++){cout<<check[k*N+l]<<"";}cout<<endl;}*/break;}}}/*cout<<"arranged!-------"<<endl;for(i=0;i<N-M;i++){for(j=0;j<N;j++){cout<<check[i*N+j]<<"";}cout<<endl;}*///cout<<rank<<endl;if(rank!=N-M){//cout<<"notfullrank"<<endl;//getchar();gotoloop1;}//re-arrangeHmatrix//exchangethecolumnsofHtochangeHintostandarduppertriangleformfor(i=0;i<N-M;i++){j=i;while(check[i*N+j]==0){j++;}if(j>=N){cout<<j<<"Error!"<<endl;getchar();}if(i!=j){for(k=0;k<N-M;k++){row[k]=Matrix->H[k*N+i];Matrix->H[k*N+i]=Matrix->H[k*N+j];Matrix->H[k*N+j]=row[k];row[k]=check[k*N+i];check[k*N+i]=check[k*N+j];check[k*N+j]=row[k];}}}//changeHintounitmatrix+Pmatrixformfor(j=0;j<N-M;j++){for(i=0;i<j;i++){if(check[i*N+j]==1){for(k=0;k<N;k++){check[i*N+k]^=check[j*N+k];}}}}//checkifthereisall-zerocolumninitfor(j=N-M;j<N;j++){k=1;for(i=0;i<N-M;i++){if(check[i*N+j]==1){k=0;break;}}}if(k){gotoloop1;}//generateGfor(i=0;i<N-M;i++){for(j=N-M;j<N;j++){Matrix->G[(j-(N-M))*N+i+M]=check[i*N+j];}}for(i=0;i<M;i++){Matrix->G[i*N+i]=1;}//rearrangeHintostandardformfor(i=0;i<N-M;i++){for(j=0;j<N;j++){check[i*N+j]=Matrix->H[i*N+j];}}for(i=0;i<N-M;i++){for(j=0;j<N;j++){Matrix->H[i*N+j]=check[i*N+(j+M)%N];}}return(Matrix);}然后这个是我输入进去的矩阵:54183612111111111111111111111111111111111111111111111111111111123456789101112131415161718123456789101112131415161718123456789101112131415161718192021222324252627282930313233343536271920212223242526353628293031323334313233343536282930192021222324252627