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

📄 downlinksimulator.cpp

📁 用matlab程序实现WCDMA系统的仿真
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	//Generate the "current" signal
	Modulator.CurSignal1= Modulator.GenerateDownlinkSignal(Tx.CurrentFrame.Chips,Tx.PreviousFrame.Chips,Tx.NextFrame.Chips); 
	if (Tx.TxConfiguration.STTDflag)
		Modulator.CurSignal2 = Modulator.GenerateDownlinkSignal(Tx.CurrentFrame.Chips + CHIPS_PER_FRAME,
															Tx.PreviousFrame.Chips + CHIPS_PER_FRAME,
															Tx.NextFrame.Chips + CHIPS_PER_FRAME);

/********************************************************************************************
		//Debug Code
		fp = fopen("PreviousFrame.txt","w");
		for (j=0;j<38400;j++)
			fprintf(fp,"%g %g\n",(Tx.PreviousFrame.Chips+j)->real,(Tx.PreviousFrame.Chips+j)->imaginary);
		fclose(fp);

		fp = fopen("CurrentFrame.txt","w");
		for (j=0;j<38400;j++)
			fprintf(fp,"%g %g\n",(Tx.CurrentFrame.Chips+j)->real,(Tx.CurrentFrame.Chips+j)->imaginary);
		fclose(fp);

		fp = fopen("NextFrame.txt","w");
		for (j=0;j<38400;j++)
			fprintf(fp,"%g %g\n",(Tx.NextFrame.Chips+j)->real,(Tx.NextFrame.Chips+j)->imaginary);
		fclose(fp);

		fp = fopen("CurrentSignal.txt","w");
		for (j=0; j<192046;j++)
			fprintf(fp,"%g %g\n",(Modulator.CurSignal1+j)->real,(Modulator.CurSignal1+j)->imaginary);
		fclose(fp);
/********************************************************************************************/

	//Delete the "Previous" frame
	free(Tx.PreviousFrame.Chips);
	Tx.PreviousFrame.Chips = NULL;
	free(Tx.PreviousFrame.DataBits);
	Tx.PreviousFrame.DataBits = NULL;

	//Update the frames
	//The "current" frame is now the "previous" frame
	//The "next" frame is now the "current frame
	Tx.PreviousFrame = Tx.CurrentFrame;
	Tx.CurrentFrame = Tx.NextFrame;
	
	//Start the Iterations
	for (k=0; k < Config.Iterations; k++)
	{

		//Generate the "next" frame
		Tx.NextFrame = Tx.GenerateFrame();

		//Run the frames throgh the Modulator
		//Generate the "next" signal
		Modulator.NextSignal1= Modulator.GenerateDownlinkSignal(Tx.CurrentFrame.Chips,Tx.PreviousFrame.Chips,Tx.NextFrame.Chips); 

		//Now that we have a "previous" signal, a "current" signal and a "next" signal, 
		//we can process the through the channel.
		Channel1.MpathProcessor(Modulator.PrevSignal1,
								Modulator.CurSignal1,
								Modulator.NextSignal1,
								Modulator.NumSamples);

		//We no longer need the previous signal, so delete it
		free(Modulator.PrevSignal1);
		Modulator.PrevSignal1 = NULL;

		//Update the "current" signal to the "previous" signal
		//Update the "next" signal to the "current" signal
		Modulator.PrevSignal1 = Modulator.CurSignal1;
		Modulator.CurSignal1 = Modulator.NextSignal1;
		
		//If STTD was employed, then process the signals from the second antenna
		if (Tx.TxConfiguration.STTDflag)
		{
			//Geneate the "next" signal
			Modulator.NextSignal2 = Modulator.GenerateDownlinkSignal(Tx.CurrentFrame.Chips + CHIPS_PER_FRAME,
																Tx.PreviousFrame.Chips + CHIPS_PER_FRAME,
																Tx.NextFrame.Chips + CHIPS_PER_FRAME);

			//Send the "previous," "current," and "next" signals through the channel associated 
			//with the 2nd antenna
			Channel2->MpathProcessor(Modulator.PrevSignal2,
							 	    Modulator.CurSignal2,
								    Modulator.NextSignal2,
									Modulator.NumSamples);

			//Combine (add) the chanel outputs from both antenna 1 and antenna 2.
			Channel1.Combiner(Channel2->MultipathSignal);

			//We no longer need the "previous" signal, so delete it (deallocate the memory)
			free(Modulator.PrevSignal2);
			Modulator.PrevSignal2 = NULL;

			//Update the "current" signal to the "previous" signal
			//Update the "next" signal to the "current" signal
			Modulator.PrevSignal2 = Modulator.CurSignal2;
			Modulator.CurSignal2 = Modulator.NextSignal2;
		}
		//Add thermal noise
		/*************************/
		//Temporarily commend out for callibration purposes
		Channel1.NoiseGenerator();
		/*************************/

		//Extract the received data bits
		DataBits = Rx.Receiver(Channel1.MultipathSignal,Channel1.MultipathSignalLength);

		//Compute the length
		DataBitLength = Tx.TxConfiguration.DesiredDPCHformat.MaxSlotsPerFrame * (Tx.TxConfiguration.DesiredDPCHformat.Ndata1 + Tx.TxConfiguration.DesiredDPCHformat.Ndata2); 
/********************************************************************************************
		//Debug Code
		fp = fopen("DataBits.txt","w");
		for (j=0; j<DataBitLength; j++)
			fprintf(fp,"%d %d\n",*(Tx.PreviousFrame.DataBits+j),*(DataBits+j));
		fclose(fp);
/********************************************************************************************/
/***************************************************************************************************
		//Count the Symbol Errors
		ReceivedDataBits = DataBits;
		SentDataBits = Tx.PreviousFrame.DataBits;
		for (j=0; j<DataBitLength; j+=2)
		{
			if ((*ReceivedDataBits == *SentDataBits) && (*(ReceivedDataBits+1) == *(SentDataBits+1)))
			{
				if (!ErrorState) RunCount++;
				else
				{
					*TempErrorArray++ = RunCount;
					RunCount=1;
					ErrorState = false;
					if ( (ErrorRunOffset = TempErrorArray - ErrorRun.Array) >= ErrorRunBlockSize)
					{
						//Reallocate the array
						ErrorRunBlockSize += ErrorRunBlockIncrement;
						ErrorRun.Array = (unsigned *) realloc(ErrorRun.Array, ErrorRunBlockSize * sizeof(unsigned));
						//Reset the temporary pointer at the correct location
						TempErrorArray = ErrorRun.Array + ErrorRunOffset;
					}
				}
			}
			else
			{
				if(!ErrorState)
				{
					*TempErrorArray++ = RunCount;
					RunCount = 1;
					ErrorState = true;
					//Check to see if the error run array is full.  If it is, then reallocate
					if ( (ErrorRunOffset = TempErrorArray - ErrorRun.Array) >= ErrorRunBlockSize)
					{
						//Reallocate the array
						ErrorRunBlockSize += ErrorRunBlockIncrement;
						ErrorRun.Array = (unsigned *) realloc(ErrorRun.Array, ErrorRunBlockSize * sizeof(unsigned));
						//Reset the temporary pointer at the correct location
						TempErrorArray = ErrorRun.Array + ErrorRunOffset;
					}
				}
				else RunCount++;
			}
			ReceivedDataBits += 2;
			SentDataBits += 2;
		}
/***************************************************************************************************/

/***************************************************************************************************/
		//Count the Bit Errors
		ReceivedDataBits = DataBits;
		SentDataBits = Tx.PreviousFrame.DataBits;
		for (j=0; j<DataBitLength; j++)
		{
			if (*ReceivedDataBits++ == *SentDataBits++) //if the bit is correctly received
			{
				//If error state is false (the previous bit was correcly received)
				//then just increment the counter
				if (!ErrorState) RunCount++; 
				//If the error state is true (the previous bit was an error then
				//store the counter, reset the counter, change the error state, 
				//and increment the array length
				else
				{
					*TempErrorArray++ = RunCount;
					RunCount = 1;
					ErrorState = false;
					//Check to see if the error run array is full.  If it is, then reallocate
					if ( (ErrorRunOffset = TempErrorArray - ErrorRun.Array) >= ErrorRunBlockSize)
					{
						//Reallocate the array
						ErrorRunBlockSize += ErrorRunBlockIncrement;
						ErrorRun.Array = (unsigned *) realloc(ErrorRun.Array, ErrorRunBlockSize * sizeof(unsigned));
						//Reset the temporary pointer at the correct location
						TempErrorArray = ErrorRun.Array + ErrorRunOffset;
					}
				}
			}
			else
			{
				if(!ErrorState)
				{
					*TempErrorArray++ = RunCount;
					RunCount = 1;
					ErrorState = true;
					//Check to see if the error run array is full.  If it is, then reallocate
					if ( (ErrorRunOffset = TempErrorArray - ErrorRun.Array) >= ErrorRunBlockSize)
					{
						//Reallocate the array
						ErrorRunBlockSize += ErrorRunBlockIncrement;
						ErrorRun.Array = (unsigned *) realloc(ErrorRun.Array, ErrorRunBlockSize * sizeof(unsigned));
						//Reset the temporary pointer at the correct location
						TempErrorArray = ErrorRun.Array + ErrorRunOffset;
					}
				}
				else RunCount++;
			}
		}
/***************************************************************************************************/
		//The channel output is no longer required, so remove the signal(s)
		free(Channel1.MultipathSignal);
		Channel1.MultipathSignal = NULL;
		if (Tx.TxConfiguration.STTDflag)
		{
			free(Channel2->MultipathSignal);
			Channel2->MultipathSignal = NULL;
		}

		//The "data" and "chipping sequence" associatged with the "previous" frame is no longer
		//required, so deallocate the memory
		free(Tx.PreviousFrame.Chips);
		Tx.PreviousFrame.Chips = NULL;
		free(Tx.PreviousFrame.DataBits);
		Tx.PreviousFrame.DataBits = NULL;

		/**************************************************/
		//Temporary command
		//Used to detect memory leaks
		//Will remove when leaks are plugged
		free(DataBits);
		DataBits = NULL;
		/**************************************************/

		//Update the frames
		//The "current" frame is now the "previous" frame
		//The "next" frame is now the "current frame
		Tx.PreviousFrame = Tx.CurrentFrame;
		Tx.CurrentFrame = Tx.NextFrame;

		//Update the wait bar

			//Update up Wait Bar
		*ZeroPtr = (double) (k+1) / (double) Config.Iterations; //ZeroPtr alread reference to prhs[0]
		nlhs = 0; //No output arguements required
		mexStatus = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"waitbar");
		if (mexStatus != 0) 
			mexErrMsgTxt("Call to mexCallMATLAB failed!--exiting\n");
	}

	//Simulation is complete.  Load the results of the remaining decisions (stored in RunCount) into the Error Run array
	*TempErrorArray++ = RunCount;
	ErrorRun.Length = TempErrorArray - ErrorRun.Array;

	//Close the waitbar
	*ZeroPtr = FigureHandle;
	mxDestroyArray(prhs[1]);
	nrhs = 1;
	mexCallMATLAB(nlhs,plhs,nrhs,prhs,"close");


	return(ErrorRun);
}

⌨️ 快捷键说明

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