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

📄 main.cpp

📁 单用户dscdma的仿真
💻 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
#define x2 30000

int main()
{

int i,j,m,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.Tspreadfactor);
	//cout<<N2;
	for(i=0;i<17;i++)
		fscanf(fp,"%c");
    fscanf(fp,"%d",&tran.numberofuserbit);
//	cout<<N;
	fclose(fp);

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(m=0;m<25;m++)
	//m=1;
{   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;                                  //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]=3001,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=1;
//itnde = nd * IPOINT * clen * 30;              //number of fading counter to skip
chan.nsamp=chan.Tspreadfactor*(chan.numberofuserbit/2+chan.numberofuserbit%2);
//cout<<chan.nsamp<<"\n";
/****************************************************/
   //cout<<"---------"<<m<<"-----------";
for(k=0;k<10;k++)
{
//	cout<<"---------"<<k<<"-----------";
	tran.StartTdomaintransmit();
rece.numberofuserbit=tran.numberofuserbit;
chan.Tspreadfactor=rece.Tspreadfactor=tran.Tspreadfactor;	
chan.Fspreadfactor=rece.Fspreadfactor=tran.Fspreadfactor;
//cout<<"------------------------"<<tran.numberofuserbit;
/*rece.FFTRealIn=Allocate2DArray(1,15000);       /////无噪声的情况
rece.FFTImagIn=Allocate2DArray(1,15000);

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,1);
DeAllocate2DArray(tran.IFFTimagout,1);	
*/

chan.ChannelRealIn=Allocate2DArray(1,x2);
chan.ChannelImagIn=Allocate2DArray(1,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,1);
DeAllocate2DArray(tran.IFFTimagout,1);
chan.ChannelRealOut=Allocate2DArray(1,x2);
chan.ChannelImagOut=Allocate2DArray(1,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,1);
DeAllocate2DArray(chan.ChannelImagIn,1);

//cout<<"------------"<<chan.Fspreadfactor<<"============="<<chan.numberofuserbit/2+chan.numberofuserbit%2;
rece.FFTRealIn=Allocate2DArray(1,x2);
rece.FFTImagIn=Allocate2DArray(1,x2);

for(i=0;i<chan.Fspreadfactor;i++)
    for(j=0;j<chan.nsamp;j++) 
    {rece.FFTRealIn[i][j]=chan.ChannelRealOut[i][j];
     rece.FFTImagIn[i][j]=chan.ChannelImagOut[i][j];
	}

	DeAllocate2DArray(chan.ChannelRealOut,1);
DeAllocate2DArray(chan.ChannelImagOut,1);

	

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[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;
      delete tran.Userbit;
      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 + -