📄 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 8
#define x2 120000
#define x3 10
int main()
{
int i,j,m,y,error=0,lenth=0;
double k,l,rata_error[200],snr[200];
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;
for(i=0;i<15;i++)
fscanf(fp,"%c");
fscanf(fp,"%d",&tran.Tspreadfactor);
fclose(fp);
rece.numberofuser=tran.numberofuser=1;
chan.numberofuserbit=tran.numberofuserbit;
chan.Tspreadfactor=rece.Tspreadfactor=tran.Tspreadfactor;
chan.Fspreadfactor=rece.Fspreadfactor=tran.Fspreadfactor;
//tran.numberofuserbit=200;
//tran.Fspreadfactor=8;
/*频域扩频
//chan.Fspreadfactor=rece.Fspreadfactor=tran.Fspreadfactor;
//chan.numberofuserbit=rece.numberofuserbit=tran.numberofuserbit;
*/
/*
tran.StartFdomaintransmit();
*/
for(y=0;y<1;y++)
{//chan.rayornot=y;
for(m=8;m<25;m++)
// m=24;
{ error=0;
lenth=0;
snr[m]=m-8;
chan.SNRdB=snr[m];
//************************** Fading initialization **************************/
//rfade = 1; //Rayleigh fading 0:nothing 1:consider
chan.itau[0] = 0,chan.itau[1]=8,chan.itau[2]=16,chan.itau[3]=32; //delay time
//chan.dlvl[0]=0.0,chan.dlvl[1]=40.0; //attenuation level
chan.dlvl[0]=0.0,chan.dlvl[1]=10.0,chan.dlvl[2]=20.0,chan.dlvl[3]=25.0;
chan.n0[0]=6,chan.n0[1]=7,chan.n0[2]=6,chan.n0[3]=7; //number of waves to generate fading
chan.th[0]=0.0,chan.th[1]=0.0 ,chan.th[2]=0.0 ,chan.th[3]=0.0 ; //initial Phase of delayed wave
//chan.itn[0]=3001,chan.itn[1]=4004; //set fading counter
chan.itn[0]=1001,chan.itn[1]=2004,chan.itn[3]=3001,chan.itn[4]=4004 ;
chan.itnd=tran.numberofuserbit*tran.Tspreadfactor*30*tran.Fspreadfactor; // % Number of fading counter to skip
chan.numberofpath = 4; //number of directwave + delayed wave
chan.tstp = 1.0/256000/tran.Tspreadfactor/tran.Fspreadfactor; //time resolution
//cout<<chan.tstp ;
chan.fd = 160; //doppler frequency [Hz]
chan.flat = 1; //flat Rayleigh environment
chan.rayornot=1;
//itnde = nd * IPOINT * clen * 30; //number of fading counter to skip
chan.nsamp=chan.Tspreadfactor*chan.Fspreadfactor*(chan.numberofuserbit/2+chan.numberofuserbit%2);
//cout<<chan.nsamp<<"\n";
/****************************************************/
//cout<<"---------"<<m<<"-----------";
for(k=0;k<300;k++)
{
tran.StartTdomaintransmit();
//cout<<"---------"<<k<<"-----------";
rece.numberofuserbit=tran.numberofuserbit;
chan.Tspreadfactor=rece.Tspreadfactor=tran.Tspreadfactor;
chan.Fspreadfactor=rece.Fspreadfactor=tran.Fspreadfactor;
//
//tran.numberofuserbit=chan.numberofuserbit;
//DeAllocate2DArray(tran.IFFTrealout,x1);
//DeAllocate2DArray(tran.IFFTimagout,x1);
// intDeAllocate2DArray(tran.Userbit,x3);
//cout<<"------------------------"<<tran.numberofuserbit;
/*rece.FFTRealIn=Allocate2DArray(x1,x2); /////无噪声的情况
rece.FFTImagIn=Allocate2DArray(x1,x2);
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,x2);
chan.ChannelImagIn=Allocate2DArray(x1,x2);
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,x2);
chan.ChannelImagOut=Allocate2DArray(x1,x2);
//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,x2);
rece.FFTImagIn=Allocate2DArray(x1,x2);
for(i=0;i<chan.Fspreadfactor;i++)
for(j=0;j<chan.nsamp/chan.Fspreadfactor;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);
//
//intDeAllocate2DArray(tran.Userbit,x3);
//DeAllocate2DArray(rece.FFTRealIn,x1);
//DeAllocate2DArray(rece.FFTImagIn,x1);
rece.StartTdomainreceive();
tran.numberofuserbit=rece.numberofuserbit;
//rece.StartFdomainreceive();
// cout<<"\n";
///***************Bit Error Rate (BER)***********************////
//cout<<" error="<<error;
for(i=0;i<rece.numberofuserbit;i++)
if(tran.Userbit[0][i]!=2*rece.FinalOut[i]-1) error++; // %sum:built infunction
lenth+=rece.numberofuserbit; //%length:build in function
//cout<<" lenth="<<lenth;
rata_error[m]=1.0*error/lenth;
intDeAllocate2DArray(tran.Userbit,x3);
delete rece.FinalOut;
//cout<<" "<<snr[m];
// %for iii=1:nloop
//%****************Output result*********************///
}
cout<<"\n"<<"rata_error in snr"<<snr[m]<<"db="<<rata_error[m]<<"\n";
}
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];
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -