📄 main.cpp
字号:
#include "main.h"
#include "sparse.h"
#include "decoder.h"
void input(int *message,int seed);
void transmit(int *codedbit,double *receive,double *lratio,double n0);
void Read_Matrix(int S[][EncodedLength],int T[][H_ColNum],int infoBitsLocation[]);
void encoder(int S[][EncodedLength],int info[],int coded[]);
double startsnr=2.0;
double endsnr=2.75;
double snrstep=0.25;
int Gen[G_RowNum][EncodedLength]; //generation matrix
int infoBitsLocation[InfoLength]; //information position
int InfoBit[InfoLength]; //vector stores information
int CodedWords[EncodedLength]; //vector stores code encoded
int Parity[ParityCheckNum][H_ColNum];//check matrix in given sparse term
int ERR[max_iter]; //vector stores errors of each iteration
//int DERR[max_iter]; //vector stores detected errors of each iteration
char DecodedData[EncodedLength]; //vector stores code decoded
mod2sparse *H; //pointer point to a sparse matrix with the link structure
/*function: fist read the generation check and informationlocation matrix from given files
second generate link table of given LDPC matrix
third simulating the process of the encoding transforming and deconding*/
void main()
{
Read_Matrix(Gen,Parity,infoBitsLocation);
H=Make_LDPC(Parity, 0,ParityCheckNum,EncodedLength);
simulation();
}
/*function: simulating the process of the encoding transforming and decoding
calculating the BER after each iteration.
*/
void simulation()
{
double snr,realsnr,N0;
double BER[max_iter];
double Receive[EncodedLength],lratio[EncodedLength],BitProb[EncodedLength];
char filename[20];
int i,TotalNum;
FILE *file;
//open file "SimResult.dat" to recode the results
sprintf(filename,"SimResult.txt");
if( ( file = fopen( filename, "w" ) ) == NULL )
{
printf( "can't open the file\n" );
exit( 1 );
}
//from statrsnr to endsnr, calculate the bit error rate for each snr
for(snr = startsnr;snr <= endsnr;snr += snrstep)
{
TotalNum = 0;
srand( (unsigned) time(NULL) );
//calculate the single side noise spectrum density N0
realsnr = pow( 10, -snr/10.);
N0 = realsnr/CodeRate;
for(i=0;i<max_iter;i++)
ERR[i]=0;
//for each snr simulate codewords the number of Totalblock
while( TotalNum< Totalblock )
{
//produce the information bits for a codeword
input( InfoBit, rand() );
//encode the information bits to a codeword
encoder( Gen, InfoBit, CodedWords);
//simulate a codeword being transmitted through AWGN channel
transmit( CodedWords, Receive, lratio, N0 );
//decode the received codeword using sum-product algorithm
prprp_decode( H,lratio, DecodedData,BitProb);
TotalNum++;
//display the result on the screen
if( TotalNum%10 == 0 )
{
for(i=0;i<max_iter;i++)
{
BER[i] = (double)ERR[i]/(double)(TotalNum*InfoLength);
printf("snr=%f iterater=%4d TotalNum=%4d errors=%d ber=%1.8f\n",snr,i+1,TotalNum,ERR[i],BER[i]);
}
if( TotalNum%1000 == 0 )
{
fprintf(file, "TotalNum=%d\n", TotalNum);
for(i=0;i<max_iter;i++)
{
fprintf(file,"snr=%f iterater=%4d errors=%d ber=%1.8f\n",snr,i+1,ERR[i],BER[i]);
fflush(file);
}
}
}
}
//recode in the file the result for each iteration at a certain snr condition
for(i=0;i<max_iter;i++)
{
fprintf(file,"snr=%f iterater=%4d errors=%d ber=%1.8f\n",snr,i+1,ERR[i],BER[i]);
fflush(file);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -