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

📄 gsmsim.c

📁 基于MATLAB的GSM仿真系统 并附有直接画出性能曲线的简化程序。
💻 C
字号:
//include modules
#include "GSMSim.h"                    //Common header
//#include "test_GSMSource.h"            //Source  (temporory)
#include "m_sequence_and_brf.h"        //Source  (formal)
#include "CYCLICRE.h"                  //Block & Conv coding  (formal)
#include "Interleaving.h"              //Interleaving (formal)
#include "test_GSMModulation.h"        //Modulation  (temporory)
//#include "test_GSMChannel.h"           //Channel  (temporory)
#include "ChannelFading.h"             //Channel  (formal)
#include "GSMMLSE.h"                   //Equalizer  (formal)
//#include "deinterleaving_WY.h"            //de-interleaving  (formal)
#include "deinterleave_MY.h"           //de-interleaving  (formal)
#include "GSM_Cov.h"                   //Conv decoding  (formal)
#include "test_GSMResult.h"            //Write to file  (temporory)


void main()
{
	//Variables
	int TS[]={0,0,1,0,0,
		      1,0,1,1,1,0,0,0,0,1,0,0,0,1,0,0,
		      1,0,1,1,1};

	int reg[25]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0};
	int SourceBlock1[VOCODELEN],SourceBlock2[VOCODELEN],SourceBlock3[VOCODELEN];
	int CodedBlock1[Ninterleave],CodedBlock2[Ninterleave],CodedBlock3[Ninterleave];
	int SourceBlock4[VOCODELEN],SourceBlock5[VOCODELEN],SourceBlock6[VOCODELEN];
	int CodedBlock4[Ninterleave],CodedBlock5[Ninterleave],CodedBlock6[Ninterleave];
	float fCodedBlock4[Ninterleave],fCodedBlock5[Ninterleave],fCodedBlock6[Ninterleave];
	int inputSerial[267],outputSerial[267];
	interleaving_table table1[Ninterleave];
	//int detable[8][114];
	int detable1[8*114];
	int blockforinter[Iinterleave*Ninterleave];
	int Tblockinterleaved[(Iinterleave+1)*Ninterleave];
	int Rblockinterleaved[(Iinterleave+1)*Ninterleave];
	int blockafterinter[Iinterleave*Ninterleave];
	int TDMABursts[BURSTLEN*(Iinterleave+1)*4];
	int TDMARecvdBursts[BURSTLEN*(Iinterleave+1)*4];
	int GSMBurst[BURSTLEN];
	int GSMRecvBurst[BURSTLEN];
	double sigFrame[2][BURSTLEN*OVERSAMPLE];
	double tempArray[2][BURSTLEN*OVERSAMPLE];
	double sigRecv[2][BURSTLEN*OVERSAMPLE];

	double sigLow[2];
	float SNR;
	int offset,mode,init;

	int FERflag[1],FERcnt;
	double BER[1],BERCI[1],BERCII[1];
	double FER[1];
	int burstNum,i,j,errCount[1],loopNum,sampleNum;
	int errCI[1],errCII[1];
	int kde,mde;
	int tempin[Ninterleave*2];                    //每组输入数据解交织前存放的数组           
	int tempout[Ninterleave];                     //每组数据解交织完存放的数组 

	double inXdata[BURSTLEN*OVERSAMPLE];
	double inYdata[BURSTLEN*OVERSAMPLE];
	double outXdata[BURSTLEN*OVERSAMPLE+20];
	double outYdata[BURSTLEN*OVERSAMPLE+20];
	int lasttime[1];
	unsigned int gseed[2];
	double gM[BURSTLEN*OVERSAMPLE],gN[BURSTLEN*OVERSAMPLE];
	int chchoice,vchoice,samplechoice;
	double sigma,sum;
	time_t time1,time2;
	char wait;

	
	//User Interface
	printf("请选择信道模型:\n(0-AWGN; 1-GSM HT; 2-GSM EQ)");
	scanf("%d",&chchoice);
	printf("\n");
	if (chchoice != 0)
	{
		printf("请输入移动台速度(单位km/h):");
		scanf("%d",&vchoice);
		printf("\n");
	}
	printf("请输入信噪比Eb/No(单位dB):");
	scanf("%f",&SNR);
	printf("\n");
	printf("请输入仿真样点数(实际样点将是输入数字×780)\n(样点数选择请参阅说明文件):");
	scanf("%d",&samplechoice);
	printf("\n");

	//Initializing
	lasttime[0] = 0;
	gseed[0] = 999987256;
	gseed[1] = 198102025;
	
	//Normalize Channel gain
	if (chchoice != 0)
	{
	printf("归一化信道增益(大约需要1分钟)...(1 # = 10 percent)\n");
	sum = 0;
	for (j = 0;j<1000;j++)
	{
		if (j%100 ==0)
		{
			printf("#");
		}
		//first, data format convertion
		for (i = 0;i<(BURSTLEN*OVERSAMPLE);i++)
		{
			inXdata[i] = 1/sqrt(2);
			inYdata[i] = 1/sqrt(2);
		}
		//second, transfer through channel
		JakesModel(inXdata,inYdata,outXdata,outYdata,lasttime,gseed,100,chchoice,vchoice,1);
		//third, statistic
		for (i = 0;i<(BURSTLEN*OVERSAMPLE);i++)
		{
			sum = sum+outXdata[i]*outXdata[i]+outYdata[i]*outYdata[i];
		}
	}
	sum = sum/(1000*BURSTLEN*OVERSAMPLE);
	}

	//Begin
	printf("\n\n仿真开始...\n");
	loopNum = 0;
//for (loopNum = 0;loopNum<30;loopNum++)
//{
	lasttime[0] = 0;
	gseed[0] = 999987256;
	gseed[1] = 198102025;
	errCount[0] = 0;
	errCI[0] = 0;
	errCII[0] = 0;
	FERcnt = 0;
	for (sampleNum = 0;sampleNum<samplechoice;sampleNum++)
	{
	//Estimate simulation time
	if (sampleNum == 0)
	{
		time(&time1);
	}

	//Voice block,need 3
	GSMSource(reg,SourceBlock1);
	GSMSource(reg,SourceBlock2);
	GSMSource(reg,SourceBlock3);
	

	//Channel Encoding
	GSM_CyclicReorder_Encoder(SourceBlock1,1,inputSerial);
    GSM_Conv_Encoder(inputSerial,1,CodedBlock1);
	GSM_CyclicReorder_Encoder(SourceBlock2,1,inputSerial);
    GSM_Conv_Encoder(inputSerial,1,CodedBlock2);
	GSM_CyclicReorder_Encoder(SourceBlock3,1,inputSerial);
    GSM_Conv_Encoder(inputSerial,1,CodedBlock3);


	//Interleaving
	//first, join 3 codedblocks together
	jointblock3to1(CodedBlock1,CodedBlock2,CodedBlock3,blockforinter);
	//second, creating interleaving table
	create_table(table1);
	//third, interleaving
	interleaving(blockforinter,Tblockinterleaved,table1);
	//forth, mappint to TDMA bursts
	mapping_2(Tblockinterleaved,TDMABursts);


	//Loop to process all TDMA Bursts
	for (burstNum = 0;burstNum<((Iinterleave+1)*4);burstNum++)
	{
		//Processing a burst once
		for (i = 0;i<BURSTLEN;i++)
		{
			GSMBurst[i] = TDMABursts[burstNum*BURSTLEN+i];
		}


		//Modulation
		//first, mapping to anti-podal signal
		for (i=0;i<BURSTLEN;i++)
		{
			GSMMVA_SigMap(GSMBurst[i],sigLow);
			for (j=0;j<OVERSAMPLE;j++)
			{
				sigFrame[0][i*OVERSAMPLE+j]=sigLow[0];
				sigFrame[1][i*OVERSAMPLE+j]=sigLow[1];
			}
		}
		//second, rotation
		init = 0;
		DebugGSMRotation(sigFrame,tempArray,init);
		//third, shaping by gauss filter
		DebugGSMGMSK(tempArray,sigFrame);


		//Trainsmit through Channel
		//DebugGSMChannel(sigFrame,sigRecv,SNR[loopNum],idum);
		if (chchoice ==0)
		{
			G_rand(gM, gN, gseed);
			sigma = pow(10.0,(SNR/10));
			for (i = 0;i<(BURSTLEN*OVERSAMPLE);i++)
			{
				sigRecv[0][i]=gM[i]/(sqrt(2*sigma))+sigFrame[0][i];//加入噪声M[j]
				sigRecv[1][i]=gN[i]/(sqrt(2*sigma))+sigFrame[1][i];//加入噪声N[j]
			}
		}
		else
		{
			//first, data format convertion
			for (i = 0;i<(BURSTLEN*OVERSAMPLE);i++)
			{
				inXdata[i] = sigFrame[0][i];
				inYdata[i] = sigFrame[1][i];
			}
			//second, transfer through channel
			JakesModel(inXdata,inYdata,outXdata,outYdata,lasttime,gseed,SNR,chchoice,vchoice,sum);
			//third, convert data back
			for (i = 0;i<(BURSTLEN*OVERSAMPLE);i++)
			{
				sigRecv[0][i] = outXdata[i];
				sigRecv[1][i] = outYdata[i];
			}
		}
	


		//Equalizing
		if (chchoice == 2)
		{
			offset = 2;
		}
		else
		{
			offset = 3;
		}
		mode = 0;
		GSMEQ(TS,sigRecv,GSMRecvBurst,init,offset,mode);


		//Combine together
		for (i = 0;i<BURSTLEN;i++)
		{
			TDMARecvdBursts[burstNum*BURSTLEN+i] = GSMRecvBurst[i];
		}
	}


	//De-interleaving
	//first, mapping to interleaved block
	mapping_3(TDMARecvdBursts,Rblockinterleaved);
	//second, de-interleaving
	//create_detable(detable);
	dtable(detable1);
	//deinterleaving(Rblockinterleaved,blockafterinter,detable);
	for(kde=0;kde<Iinterleave;kde++)                     //将输入数据分组进行解交织处理
	{
		for(mde=0;mde<Ninterleave*2;mde++)               
		{
			tempin[mde]=Rblockinterleaved[kde*Ninterleave+mde];
		}
		deinterleaving(tempin,tempout,detable1);
		for(mde=0;mde<Ninterleave;mde++)
		{
			blockafterinter[kde*Ninterleave+mde]=tempout[mde];        //将解交织完的数据放到输出数组中,如果需要即时处理,可直接将tempout数据输出,不造成较大延时
		}
	}
	//third, split block
	splitblock1to3(blockafterinter,CodedBlock4,CodedBlock5,CodedBlock6);


	//int to float
	for (i = 0;i<Ninterleave;i++)
	{
		fCodedBlock4[i] = (float)CodedBlock4[i];
		fCodedBlock5[i] = (float)CodedBlock5[i];
		fCodedBlock6[i] = (float)CodedBlock6[i];
	}


	//Channel decoding
	GSM_Cov_ViterbiDecoder(fCodedBlock4,outputSerial);
    GSM_CyclicReorder_Decoder(outputSerial,1,SourceBlock4,FERflag);
	FERcnt = FERcnt+FERflag[0];
	GSM_Cov_ViterbiDecoder(fCodedBlock5,outputSerial);
    GSM_CyclicReorder_Decoder(outputSerial,1,SourceBlock5,FERflag);
	FERcnt = FERcnt+FERflag[0];
	GSM_Cov_ViterbiDecoder(fCodedBlock6,outputSerial);
    GSM_CyclicReorder_Decoder(outputSerial,1,SourceBlock6,FERflag);
	FERcnt = FERcnt+FERflag[0];

	
	//Result
	GSMSink(SourceBlock1,SourceBlock4,errCount,errCI,errCII);
	GSMSink(SourceBlock2,SourceBlock5,errCount,errCI,errCII);
	GSMSink(SourceBlock3,SourceBlock6,errCount,errCI,errCII);

	if (sampleNum == 9)
	{
		time(&time2);
		printf("本次仿真估计需要 %4.2f 分钟\n\n",((time2-time1)/60.0)*((samplechoice-10)/10));
	}

	}

	BER[0] = (double)errCount[0]/(double)(VOCODELEN*3*sampleNum);
	BERCI[0] = (double)errCI[0]/(double)(50*3*sampleNum);
	BERCII[0] = (double)errCII[0]/(double)((VOCODELEN-182)*3*sampleNum);
	FER[0] = (double)FERcnt/(double)(3*sampleNum);

	printf("Results:\n");
	printf("BER(Over all):%f\n",BER[0]);
	printf("*************************\n");
	printf("BER(Class Ia bit):%f\n",BERCI[0]);
	printf("BER(Class II bit):%f\n",BERCII[0]);
	printf("*************************\n");
	printf("FER:%f\n",FER[0]);
	printf("\n每次请记录上面的结果,因为系统没有自动保存的功能。\n\n");
	printf("GSM Simulation Project   ver1.0  2004.6\n");
	printf("Thanks to:\n");
	printf("**************************************\n");
	printf("朱志敏、王德方、陈皓\n");
	printf("王艳、马燕、杨珏\n");
	printf("沈麟、巩兴华\n");
	printf("金利忠、蒋登峰、高霁\n");
	printf("沈亮、何海建、陈声健\n");
	printf("林翌\n");
	printf("沈平\n");
	printf("**************************************\n");
	printf("We open our work,\n");
	printf("we open our heart.\n\n");
	printf("按Enter键退出...");
	scanf("%c",&wait);
	scanf("%c",&wait);

//}

//DebugGSMResult(SNR,BER,30);

//while (1)
//{
	//Waiting
//}

}

⌨️ 快捷键说明

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