📄 main.cpp
字号:
#include <iostream>
using namespace std;
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include<math.h>
#include<fstream>
#include"transmitter.h"
#include"receiver.h"
#include"channel.h"
#include"Allocate2DArray.cpp"
#define x1 100
/*************************** Fading initialization **************************
rfade = 1; //Rayleigh fading 0:nothing 1:consider
itau = [0,8]; //delay time
dlvl1 = [0.0,40.0]; //attenuation level
n0 = [6,7]; //number of waves to generate fading
th1 = [0.0,0.0]; //initial Phase of delayed wave
itnd1 = [3001,4004]; //set fading counter
now1 = 2; //number of directwave + delayed wave
tstp = 1 / sr / IPOINT / clen; //time resolution
tstp
fd = 160; //doppler frequency [Hz]
flat = 1; //flat Rayleigh environment
itndel = nd * IPOINT * clen * 30; //number of fading counter to skip
****************************************************/
int main()
{
int i,j,m,error=0,lenth=0;
double k,l,rata_error[20],snr[20];
time_t t;
srand((unsigned) time(&t));
FILE *fp;
transmitter tran;
receiver rece;
channel chan;
fp=fopen("cdma.txt","r");
for(i=0;i<14;i++)
fscanf(fp,"%c");
fscanf(fp,"%d",&tran.Fspreadfactor);
//cout<<N2;
for(i=0;i<17;i++)
fscanf(fp,"%c");
fscanf(fp,"%d",&tran.numberofuserbit);
// cout<<N;
fclose(fp);
//for(m=0;m<17;m++)
m=8;
{ error=0;
lenth=0;
snr[m]=m-8;
chan.SNRdB=snr[m];
//tran.numberofuserbit=200;
//tran.Fspreadfactor=8;
chan.Fspreadfactor=rece.Fspreadfactor=tran.Fspreadfactor;
chan.numberofuserbit=rece.numberofuserbit=tran.numberofuserbit;
//************************** Fading initialization **************************/
//rfade = 1; //Rayleigh fading 0:nothing 1:consider
chan.itau[0] = 0/*,chan.itau[1]=8*/; //delay time
chan.dlvl[0]=0.0/*chan.dlvl[1]=40.0*/; //attenuation level
chan.n0[0]=6/*chan.n0[1]=7*/; //number of waves to generate fading
chan.th[0]=0.0/*chan.th[1]=0.0*/ ; //initial Phase of delayed wave
chan.itn[0]=1000/*chan.itn[1]=4004*/; //set fading counter
chan.itnd=4000; // % Number of fading counter to skip
chan.numberofpath = 1; //number of directwave + delayed wave
chan.tstp = 4.8828e-007; //time resolution
chan.fd = 160; //doppler frequency [Hz]
chan.flat = 1; //flat Rayleigh environment
chan.rayornot=0;
//itnde = nd * IPOINT * clen * 30; //number of fading counter to skip
chan.nsamp=chan.Fspreadfactor*(chan.numberofuserbit/2+chan.numberofuserbit%2);
/****************************************************/
for(k=0;k<10;k++)
{
tran.StartFdomaintransmit();
/*rece.FFTRealIn=Allocate2DArray(x1,1000); /////无噪声的情况
rece.FFTImagIn=Allocate2DArray(x1,1000);
for(i=0;i<tran.Fspreadfactor;i++)
for(j=0;j<tran.numberofuserbit/2+tran.numberofuserbit%2;j++)
{rece.FFTRealIn[i][j]=tran.IFFTrealout[i][j];
rece.FFTImagIn[i][j]=tran.IFFTimagout[i][j];
}
DeAllocate2DArray(tran.IFFTrealout,x1);
DeAllocate2DArray(tran.IFFTimagout,x1);
*/
chan.ChannelRealIn=Allocate2DArray(x1,30000);
chan.ChannelImagIn=Allocate2DArray(x1,30000);
for(i=0;i<tran.Fspreadfactor;i++)
for(j=0;j<tran.numberofuserbit/2+tran.numberofuserbit%2;j++)
{chan.ChannelRealIn[i][j]=tran.IFFTrealout[i][j];
chan.ChannelImagIn[i][j]=tran.IFFTimagout[i][j];
}
DeAllocate2DArray(tran.IFFTrealout,x1);
DeAllocate2DArray(tran.IFFTimagout,x1);
chan.ChannelRealOut=Allocate2DArray(x1,1000);
chan.ChannelImagOut=Allocate2DArray(x1,1000);
//chan.SNRdB=0;
chan.frequencyselectingfade();
chan.AWGN();
for(j=0;j<chan.numberofpath;j++)
chan.itn[j]=chan.itn[j]+chan.itnd;
DeAllocate2DArray(chan.ChannelRealIn,x1);
DeAllocate2DArray(chan.ChannelImagIn,x1);
//cout<<"------------"<<chan.Fspreadfactor<<"============="<<chan.numberofuserbit/2+chan.numberofuserbit%2;
rece.FFTRealIn=Allocate2DArray(x1,1000);
rece.FFTImagIn=Allocate2DArray(x1,1000);
for(i=0;i<chan.Fspreadfactor;i++)
for(j=0;j<chan.numberofuserbit/2+chan.numberofuserbit%2;j++)
{rece.FFTRealIn[i][j]=chan.ChannelRealOut[i][j];
rece.FFTImagIn[i][j]=chan.ChannelImagOut[i][j];
}
DeAllocate2DArray(chan.ChannelRealOut,x1);
DeAllocate2DArray(chan.ChannelImagOut,x1);
rece.StartFdomainreceive();
//cout<<"\n";
///////////////////////////////////////////////
///* for(i=0;i<rece.numberofuserbit;i++)
// if(tran.Userbit[i]!=2*rece.FinalOut[i]-1) error++;
// cout<<error;
// rata_error=1.0*error/rece.numberofuserbit;
// cout<<"\n";
// cout<<"rata_error="<<rata_error<<"\n";*/
//***************Bit Error Rate (BER)***********************//
for(i=0;i<rece.numberofuserbit;i++)
if(tran.Userbit[i]!=2*rece.FinalOut[i]-1) error++; // %sum:built infunction
lenth+=rece.numberofuserbit; //%length:build in function
rata_error[m]=1.0*error/lenth;
// %for iii=1:nloop
//%****************Output result*********************//
}
}
//for(i=0;i<m;i++)
i=m;
cout<<"rata_error in snr"<<i-8<<"db="<<rata_error[i]<<"\n";
//for(i=0;i<m;i++)
i=m;
cout<<" "<<rata_error[i];
delete tran.Userbit;
delete rece.FinalOut;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -