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

📄 transmitter.cpp

📁 多用户多载波时频域cdma扩频的仿真
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include <iostream>
using namespace std;
#include <complex>
#include "transmitter.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include<math.h>
#include<fstream>
#define PI 3.1415926535
#define sqr(x) ((x)*(x))
#define x1 8
#define x2 120000
#define x3 10

/////////////////////////////////////////////////////////
//                                                  /////
//                   时域频域扩频的传送过程2d-cdma  ///////
//////////////////////////////////////////////////////////
void transmitter::StartTdomaintransmit ( )
{
	int i,j,k,N=10,n=3,Fs,**User;
	double n1=0,snr=2,*b,*a;

//double e[1000],f[10000],g[1000]={0},md[10][1000],de[10][1000],ee[10][100]={0},sp[10][100],I[10][100],Q[10][100],rI[10][1000],rQ[10][1000];
  // double dic[4],min,fI[10][1000],fQ[10][1000],finde[10][1000]={0},n;
n1=log(Fspreadfactor)/log(2);
	Fs=Fspreadfactor;                                 //储存频域扩频系数
	Fspreadfactor=1;                                  //在时域扩频时,令频域扩频因子为1
Userbit=intAllocate2DArray(x3,x2);                         //为Userbit分配内存


 random(numberofuserbit,numberofuser);                              //产生随机数,给Userbit赋值并实现串并转换
 /////////////////////////////////////////输出///////////////////////////////////////////////
  /*  cout<<"单双极性变换"<<"\n";                        //输出结果
 for(j=0;j<numberofuser;j++)
 {for(i=0; i<numberofuserbit; i++)
    cout<<Userbit[j][i]<<" ";
   cout<<"\n";
 } */
   /////////////////////////////////////////////////////////////////////////////////////
User=intAllocate2DArray(x3,x2);                       //动态2维数组分配内存,
Ireal=Allocate2DArray(x3,x2);                       //动态2维数组分配内存,存放QPSK调制的结果
Qimag=Allocate2DArray(x3,x2);                      //动态2维数组分配内存,存放QPSK调制的结果
for(j=0;j<numberofuser;j++)                         
 {for(i=0; i<numberofuserbit; i++)
    User[j][i]=Userbit[j][i];
   
 } 

QPSK(numberofuser,User);                                          //进行QPSK调制
intDeAllocate2DArray(User,x3);                //删除内存
//intDeAllocate2DArray(SpreadOut,1);                //删除内存
/////////////////////////////////////////输出///////////////////////////////////////////////
/*cout<<"I支路"<<"\n";                               //输出结果
for(i=0;i<numberofuser;i++)
  
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<<Ireal[i][j]<<" ";
    cout<<"\n";
}   
 cout<<"Q支路"<<"\n";
for(i=0;i<numberofuser;i++)
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<<Qimag[i][j]<<" ";
    cout<<"\n";
} */  
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 
TSpreadOutReal=Allocate2DArray(x3,x2);                    //分配内存,存放时域扩频的结果
TSpreadOutImag=Allocate2DArray(x3,x2);                    //分配内存,存放时域扩频的结果
 b=new double[x2];                                      //分配内存,为时域扩频用
  Mseq=intAllocate2DArray(x3,x2);                        //为m序列分配内存
M_seq(4);                                             //产生m序列作为扩频码
for(j=1;j<numberofuser;j++)
  for(k=0;k<=20000;k++)
   Mseq[j][k]=Mseq[j-1][k+1];

  Walsh=intAllocate2DArray(x3,x2);                        //为Walsh序列分配内存
for(j=0;j<numberofuser;j++)
 walsh_gen(3,j);

/*cout<<"M序列"<<"\n";
for(j=0;j<numberofuser;j++)
{for(i=0; i<20; i++)
    cout<<Mseq[j][i] <<" ";
   cout<<"\n";
}*/

////////////////////////////
 
 SpreadOut=Allocate2DArray(x1,x2);           //动态2维数组分配内存,存放时域扩频后的数组
for(j=0;j<numberofuser;j++)
{
     for(i=0; i<numberofuserbit/2+numberofuserbit%2; i++)   //把I支路的值赋给b
	 {b[i]=Ireal[j][i];
 //cout<<b[i]<<" ";
	 }

	T_domainSpread(Tspreadfactor,b,j);            //对I支路进行时域扩频
 for(i=0; i<(numberofuserbit/2+numberofuserbit%2)*Tspreadfactor; i++)
  TSpreadOutReal[j][i]=SpreadOut[0][i];          //把结果赋给SpreadOutReal


//cout<<"\n";
 for(i=0; i<numberofuserbit/2+numberofuserbit%2; i++) 
 {b[i]=Qimag[j][i];                              //把Q支路的值赋给b
 }
 T_domainSpread(Tspreadfactor,b,j);                 //对Q支路进行时域扩频
 for(i=0; i<(numberofuserbit/2+numberofuserbit%2)*Tspreadfactor; i++)
  TSpreadOutImag[j][i]=SpreadOut[0][i];               //把结果赋给SpreadOutImag
//cout<<"Mseq[256*8]="<<Mseq[256*8];
}
 intDeAllocate2DArray(Mseq,x3);                       //删除内存Mseq
  intDeAllocate2DArray(Walsh,x3);                       //删除内存Walsh
 delete b;                                          //删除内存b 
 DeAllocate2DArray(Ireal,x3);                    //删除内存Ireal
 DeAllocate2DArray(Qimag,x3);                    //删除内存Qimag
 numberofuserbit=(numberofuserbit+numberofuserbit%2)*(Tspreadfactor);   //把numberofuserbit转换为时域扩频后的值
/////////////////////////////////////////输出///////////////////////////////////////////////
/*cout<<"时域扩频I"<<"\n";                             //输出结果
for(i=0;i<numberofuser;i++)
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<< TSpreadOutReal[i][j]<<" ";
 cout<<"\n";
}
cout<<"时域扩频Q"<<"\n";
for(i=0;i<numberofuser;i++)
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<< TSpreadOutImag[i][j]<<" ";
 cout<<"\n";
}*/
 //////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
//cout<<"SpreadOut[0][256*8]="<<SpreadOut[0][256*8-1];
a=new double[x2];                                      //分配内存,为频域扩频用
   Mseq=intAllocate2DArray(x3,x2);                        //为m序列分配内存
M_seq(4);                                             //产生m序列作为扩频码
for(j=1;j<numberofuser;j++)
  for(k=0;k<=20000;k++)
   Mseq[j][k]=Mseq[j-1][k+1];

  Walsh=intAllocate2DArray(x3,x2);                        //为Walsh序列分配内存
for(j=0;j<numberofuser;j++)
 walsh_gen(3,j);
/*cout<<"M序列"<<"\n";
for(j=0;j<numberofuser;j++)
{for(i=0; i<20; i++)
    cout<<Mseq[j][i] <<" ";
   cout<<"\n";
}

cout<<"Walsh序列"<<"\n";
for(j=0;j<numberofuser;j++)
{for(i=0; i<20; i++)
    cout<<Walsh[j][i] <<" ";
   cout<<"\n";
}*/
 FSpreadOutReal=Allocate2DArray(x1,x2);           //动态2维数组分配内存,存放频域扩频后的数组
FSpreadOutImag=Allocate2DArray(x1,x2);           //动态2维数组分配内存,存放频域扩频后的数组
 for(i=0;i<Fs;i++)
   for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
	{FSpreadOutReal[i][j]=0;
     FSpreadOutImag[i][j]=0;
    }
for(k=0;k<numberofuser;k++)
{for(i=0; i<numberofuserbit/2+numberofuserbit%2; i++)   //把I支路的值赋给a
 {a[i]=TSpreadOutReal[k][i];
 //cout<<a[i]<<" ";
 }
 F_domainSpread(Fs,a,k);                               //对I支路进行频域扩频
//////////////////////////

 for(i=0;i<Fs;i++)
  for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
	   FSpreadOutReal[i][j]+=SpreadOut[i][j];
   /////////////////////////////////////////输出///////////////////////////////////////////////
/* cout<<"I支路频域扩频用户"<<k<<"\n";
for(i=0;i<Fs;i++)
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<<SpreadOut[i][j]<<" ";
 cout<<"\n";
}*/
///////////////////////////////////////////////////////////////////////////////////////////
 for(i=0; i<numberofuserbit/2+numberofuserbit%2; i++)   //把Q支路的值赋给a
 {a[i]=TSpreadOutImag[k][i];                                   
 //cout<<a[i]<<" ";
 }
 F_domainSpread(Fs,a,k);                                 //对Q支路进行频域扩频
 for(i=0;i<Fs;i++)
  for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
	   FSpreadOutImag[i][j]+=SpreadOut[i][j];
   /////////////////////////////////////////输出///////////////////////////////////////////////
/*cout<<"Q支路频域扩频用户"<<k<<"\n";
for(i=0;i<Fs;i++)
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<<SpreadOut[i][j]<<" ";
 cout<<"\n";
}*/
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
}
 /////////////////////////////////////////输出///////////////////////////////////////////////
/*cout<<"I支路多用户叠加"<<"\n";
for(i=0;i<Fs;i++)
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<<FSpreadOutReal[i][j]<<" ";
 cout<<"\n";
}
cout<<"Q支路多用户叠加"<<"\n";
for(i=0;i<Fs;i++)
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<<FSpreadOutImag[i][j]<<" ";
 cout<<"\n";
}*/
 //////////////////////////////////////////////////////////////////////////////////////////////////////// 

intDeAllocate2DArray(Mseq,x3);                      //删除内存Mseq
 intDeAllocate2DArray(Walsh,x3);                       //删除内存Walsh
delete a;                                          //删除内存a
DeAllocate2DArray(TSpreadOutReal,x3);               //删除内存 TSpreadOutReal
DeAllocate2DArray(TSpreadOutImag,x3);               //删除内存 TSpreadOutImag
DeAllocate2DArray(SpreadOut,x1);                   //删除内存SpreadOut
Fspreadfactor=Fs;                                  //恢复频域扩频系数的值
complex<double> *TD=new complex<double>[x2];
complex<double> *FD=new complex<double>[x2];

IFFTrealout=Allocate2DArray(x1,x2);                       //动态2维数组分配内存,存放IFFT变换的结果
IFFTimagout=Allocate2DArray(x1,x2);                      //动态2维数组分配内存,存放IFFT变换的结果
 //cout<<"--------------IFFT变换-------------------";
 //cout<<"\n";
for(i=0;i<numberofuserbit/2+numberofuserbit%2;i++)
{for( k=0;k<Fspreadfactor;k++)
 FD[k]=complex<double>(FSpreadOutReal[k][i], FSpreadOutImag[k][i]); 
  for( k=0;k<Fspreadfactor;k++)
 //cout<<FD[k].real()<<"+"<<FD[k].imag()<<"i   ";
 // cout<<"\n------------------\n";
  //cout<<int(n1+0.5);
FFT(TD,FD,int(n1+0.5),-1);

  for( k=0;k<Fspreadfactor;k++)
  {IFFTrealout[k][i]=TD[k].real();
   IFFTimagout[k][i]=TD[k].imag();
  }

}
DeAllocate2DArray(FSpreadOutReal,x1);                                   //删除内存
DeAllocate2DArray(FSpreadOutImag,x1);                                   //删除内存
/////////////////////////////////////////输出///////////////////////////////////////////////
/*cout<<"I支路IFFT变换"<<"\n";
for(i=0;i<Fspreadfactor;i++)                                  
  
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<<IFFTrealout[i][j]<<" ";
    cout<<"\n";
}   
 cout<<"Q支路IFFT变换"<<"\n";

for(i=0;i<Fspreadfactor;i++)
{ for(j=0;j<numberofuserbit/2+numberofuserbit%2;j++) 
    cout<<IFFTimagout[i][j]<<" ";
    cout<<"\n";
} */
 /////////////////////////////////////////////////////////////////////////////////////////
delete TD;
delete    FD;

}



/////////////////////////////////////////////////////////
//                                                  /////
//                    产生随机数                   ///////
//////////////////////////////////////////////////////////
void transmitter::random(int bitnumber,int number)
{  int i,j;
   time_t t;
	 srand((unsigned) time(NULL));
 for(j=0;j<number;j++)
 {   
      for(i=0; i<bitnumber; i++)
   {  Userbit[j][i]=rand() % (2);
  
   }
   /* cout<<"原始数据"<<"\n";
for(i=0; i<numberofuserbit; i++)
    cout<<Userbit[j][i]<<" ";
   cout<<"\n";*/
  Userbit[j][i]=0;//最末位为0
   /////////////////单双极性变换/////////////////
   for(i=0;i<bitnumber;i++)
	   Userbit[j][i]=Userbit[j][i]*2-1;
Userbit[j][i]=-1;//最末位为0
 }
}
/////////////////////////////////////////////////////////
//                                                  /////
//                    产生Walsh扩频码             ///////
//////////////////////////////////////////////////////////
	void transmitter::walsh_gen(int order,int index )
	{
     int i,j,k;
	int nOrderLength;
	int sign;
	k=index;
	{ Walsh[k][0]=1;
	 for(i=0;i<order;i++)
	 {
		nOrderLength=1<<i;
		sign=((index&nOrderLength)==0)?1:-1;
		for(j=0;j<nOrderLength;j++)
			Walsh[k][nOrderLength+j]=sign*Walsh[k][j];
	 }
	  for(i=0;i<x2/int(pow(2,order));i++)
		for(j=0;j<int(pow(2,order));j++)
		{Walsh[k][i*int(pow(2,order))+j]=Walsh[k][j];
		}
	}
		}
/////////////////////////////////////////////////////////
//                                                  /////
//                    产生m序列扩频码             ///////
//////////////////////////////////////////////////////////
	void transmitter::M_seq(int n)
    {
		int *zita,i,k,N;
		zita=new int[x2];
		N=int(pow(2,n)-1);
	for(i=0;i<n;i++)                    //对zita[k]赋初值
	{
      zita[i]=1;
	   Mseq[0][i]=1;
    }
	
	switch(n)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -