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

📄 transmitterclass.cpp

📁 用matlab程序实现WCDMA系统的仿真
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		free( *(TxConfiguration.DPCCHforCPCHScrambleCodes + k) );
		*(TxConfiguration.DPCCHforCPCHScrambleCodes + k) = NULL;
	}
	if (TxConfiguration.NumDPCCHforCPCH > 0) 
	{
		free(TxConfiguration.DPCCHforCPCHScrambleCodes);
		TxConfiguration.DPCCHforCPCHScrambleCodes = NULL;
	}
	for (k=0;k<TxConfiguration.NumOtherDPCH; k++)
	{
		free( *(TxConfiguration.OtherScrambleCodes + k) );
		*(TxConfiguration.OtherScrambleCodes + k) = NULL;
	}
	if (TxConfiguration.NumOtherDPCH > 0) 
	{
		free(TxConfiguration.OtherScrambleCodes);
		TxConfiguration.OtherScrambleCodes = NULL;
	}
	free(TxConfiguration.P_CCPCHScramCode);
	TxConfiguration.P_CCPCHScramCode = NULL;
	free(TxConfiguration.P_CPICHScrambleCode);
	TxConfiguration.P_CPICHScrambleCode = NULL;
	free(TxConfiguration.PDSCHScramCode);
	TxConfiguration.PDSCHScramCode = NULL;
	for (k=0;k<TxConfiguration.NumS_CPICH;k++)
	{
		free( *(TxConfiguration.S_CPICHScramCodes + k) );
		*(TxConfiguration.S_CPICHScramCodes + k) = NULL;
	}
	if (TxConfiguration.NumS_CPICH > 0) 
	{
		free(TxConfiguration.S_CPICHScramCodes);
		TxConfiguration.S_CPICHScramCodes = NULL;
	}
	free(TxConfiguration.S_CCPCHScramCode);
	TxConfiguration.S_CCPCHScramCode = NULL;

	//Pointers to channel codes point to portions of arrays that were
	//allocated (and will be de-allocated) elsewhere.  
	//So just set pointers to NULL
	if (TxConfiguration.NumOtherDPCH > 0) TxConfiguration.OtherChannelCodes = NULL;
	if (TxConfiguration.NumDPCCHforCPCH > 0) TxConfiguration.DPCCHforCPCHChanCodes = NULL;
	if (TxConfiguration.NumS_CPICH > 0) TxConfiguration.S_CPICHChanCodes = NULL;


	free(P_CPICHptr);
	P_CPICHptr = NULL;
	free(P_SCHptr);
	P_SCHptr = NULL;
	free(S_SCHptr);
	S_SCHptr = NULL;
	free(Gains.S_CPICH);
	Gains.S_CPICH = NULL;
	free(Timing.S_CPICH);
	Timing.S_CPICH = NULL;
	free(RelativeOffset.S_CPICH);
	RelativeOffset.S_CPICH = NULL;
	if (TxConfiguration.NumDPCCHforCPCH > 0)
	{
		free(Gains.DPCHforCPCH);
		free(Timing.DPCHforCPCH);
		free(RelativeOffset.DPCHforCPCH);
	}

	if (TxConfiguration.NumOtherDPCH > 0)	
	{
		free(Gains.OtherDPCH);
		free(Timing.OtherDPCH);
		free(RelativeOffset.OtherDPCH);
	}

	if (TxConfiguration.NumS_CPICH> 0)	
	{
		free(Gains.S_CPICH);
		free(Timing.S_CPICH);
		free(RelativeOffset.S_CPICH);
	}

	if (CurrentFrame.Chips != NULL)
	{
		free(CurrentFrame.Chips);
		CurrentFrame.Chips = NULL;
	}
	if (CurrentFrame.DataBits != NULL)
	{
		free(CurrentFrame.DataBits);
		CurrentFrame.DataBits = NULL;
	}

	if (NextFrame.Chips != NULL)
	{
		free(NextFrame.Chips);
		NextFrame.Chips = NULL;
	}
	if (NextFrame.DataBits != NULL)
	{
		free(NextFrame.DataBits);
		NextFrame.DataBits = NULL;
	}

	if (PreviousFrame.Chips != NULL)
	{
		free(PreviousFrame.Chips);
		PreviousFrame.Chips = NULL;
	}
	if (PreviousFrame.DataBits != NULL)
	{
		free(PreviousFrame.DataBits);
		PreviousFrame.DataBits = NULL;
	}
}



ComplexNumber *TransmitterClass::ScrambleCode(unsigned N)
/*****************************************************************************************************
/ComplexNumber *ScrambleSequence(unsigned N)
/
/ Copyright 2002 The Mobile and Portable Radio Research Group
/
/This function generages the sequence that once can then use to generate all of the downlink spreading
/codes.  The sequence is generated in accordance to the algorithm specified in clause 5.2.2 of 
/ETSI TS 125 213 V3.2.0 (2000-03).  The function generates this code by using the implementation 
/that is pictorially described in figure 10 of the aforementioned specificatoin.  
/This implemtation takes advantage of two shift registers, which this function emulates via 
/circular buffers.  The first circular buffer, X, is initiallized by setting all the values, except X(0), \
/to 0.  X(0) is set to 1. The second circular buffer Y is initiallized by setting all elements to 1.
/
/The function then performs the following operation using modulo 2 arithmetic
/	x(n+18) = x(n+7) + x(n)
/	y(n+18) = y(n+10) + y(n+7) + y(n+5) + y(n)
/	v(n) = x(n+4) + x(n+6) + x(n+15)
/	w(n) = y(n+5) + y(n+6) + y(n+8) + y(n+9) + y(n+10) + y(n+11) + y(n+12) + y(n+13) + y(n+14) + y(n+15)
/	ClongI = x(n) + y(n)
/	ClongQ = w(n) + v(n)
/	Scramble_long(n) = ClongI + j*ClongQ;
/
/Returns	Pointer to an aray of length 50000 that contains the scramble code
/
/Parameters
/	N	unsigned	Scrambling code number
/******************************************************************************************************/

{
	ComplexNumber *Sequence,*TempSequence;
	unsigned short *x_buf_front,*x_buf_end;		//Points to the begining and end of the X circular buffer
	unsigned short *y_buf_front,*y_buf_end;		//Points to the begining and end of the Y circular buffer
	unsigned short *x0;		//Points to x(n)
	unsigned short *x4;		//Points to x(n+4)
	unsigned short *x6;		//Points to x(n+6)
	unsigned short *x7;		//Points to x(n+7)
	unsigned short *x15;	//Points to x(n+15)
	unsigned short *x18;	//Points to x(n+18)
	unsigned short *y0;		//Points to y(n)
	unsigned short *y5;		//Points to y(n+5)
	unsigned short *y6;		//Points to y(n+6)
	unsigned short *y7;		//Points to y(n+7)
	unsigned short *y8;		//Points to y(n+8)
	unsigned short *y9;		//Points to y(n+9)
	unsigned short *y10;	//Points to y(n+10)
	unsigned short *y11;	//Points to y(n+11)
	unsigned short *y12;	//Points to y(n+12)
	unsigned short *y13;	//Points to y(n+13)
	unsigned short *y14;	//Points to y(n+14)
	unsigned short *y15;	//Points to y(n+15)
	unsigned short *y18;	//Points to y(n+18)
	unsigned short *y_temp;	//Temporary Pointer	
	unsigned short x1_hold; //Stores x(n)
	unsigned short y1_hold; //Stores y(n)
	unsigned short v_hold;	//Stores the result of x(n+4) + x(n+7) + x(n+18)
	unsigned short w_hold;	//Stores the result of y(n+4) + y(n+6) + y(n+17)
	unsigned k;
//	FILE *fp;


	/* Will shift registers using two circular buffers: 
	   one for the "x" sequence and one for the "y" sequence
	*/

	//Allocate buffers
	if ((x_buf_front = (unsigned short *) calloc(ARRAY_LENGTH,sizeof(unsigned short)))==NULL)
	{
		printf("\nx_buf_front array not allocated!--exiting\n");
		exit(-2);
	}
	if ((y_buf_front = (unsigned short *) calloc(ARRAY_LENGTH,sizeof(unsigned short)))==NULL)
	{
		printf("\n y_buf_front array not allocated!--exiting\n");
		exit(-2);
	}

	//Assign pointers to the end of buffer
	x_buf_end=x_buf_front+BUFFER_LENGTH;
	y_buf_end=y_buf_front+BUFFER_LENGTH;

	//Initialize buffers
	/* The X buffer is initialized to 0, with the exception 
	/ of X(0), which is initialized to 1*/
		
	*x_buf_front = 1;

	//Initialize the "y" buffer by setting all 18 elements equal to 1
	y_temp=y_buf_front;
	for (k=0;k<BUFFER_LENGTH;k++) *y_temp++ = 1;

	//We now implement the sequence computation.
	//First initialize the locations of all pointers
	//Pointers for "x" buffer
	x0=x_buf_front;			//x(n)
	x4=x_buf_front+4;		//x(n+4)
	x6=x_buf_front+6;		//x(n+6)
	x7=x_buf_front+7;		//x(n+7)
	x15=x_buf_front+15;		//x(n+14)
	x18=x_buf_front;		//x(n+18)
	//Pointers for "y" buffer
	y0=y_buf_front;			//y(n)
	y5=y_buf_front+5;		//y(n+5)
	y6=y_buf_front+6;		//y(n+6)
	y7=y_buf_front+7;		//y(n+7)
	y8=y_buf_front+8;		//y(n+8)
	y9=y_buf_front+9;		//y(n+9)
	y10=y_buf_front+10;		//y(n+10)
	y11=y_buf_front+11;		//y(n+11)
	y12=y_buf_front+12;		//y(n+12)
	y13=y_buf_front+13;		//y(n+13)
	y14=y_buf_front+14;		//y(n+14)
	y15=y_buf_front+15;		//y(n+15)
	y18=y_buf_front;		//y(n+18)

	//Create complex array to store sequence output
	Sequence = (ComplexNumber *) calloc(SEQUENCE_LENGTH,sizeof(ComplexNumber));
	TempSequence = Sequence;

	//***********************************************************
	// ScrambleCode.real(k) = X(k+N) ^ Y(k)
	// SxrambleCode.imag(k) = V(k+N) ^ W(k) = X(k+131072+N) ^ Y(k+131072)
	//
	// Note that the X sequence must LEAD the Y sequence by N (the scramble code)
	// We accomplish this by generating the first N elements of the sequence
	// (i.e., X(0), X(1), ..., X(N-1) ).
	//This is accomplished in the following loop
	for (k=0; k<N; k++)
	{
		x1_hold = *x0;	//Shift register output for "x" sequence
		
		//Perform Shift register operations
		*x18 = *x0++ ^ *x7++;		//x(n+18) = x(n) + x(n+7)
		v_hold = ((*x4++ ^ *x6++) ^ *x15++);		//z(n) = x(n+4) + x(n+6) + x(n+15)
		x18++;

		//Check for pointer overflow
		if (x15 == x_buf_end) x15 = x_buf_front;
		else if (x7 == x_buf_end) x7 = x_buf_front;
		else if (x6 == x_buf_end) x6 = x_buf_front;
		else if (x4 == x_buf_end) x4 = x_buf_front;
		else if (x0 == x_buf_end) 
		{
			x0 = x_buf_front;
			x18 = x_buf_front;
		}
	}


	for (k=0;k<SEQUENCE_LENGTH;k++)
	{
		x1_hold = *x0;	//Shift register output for "x" sequence
		y1_hold = *y0;	//Shift register output for "y" sequence
		
		//Perform Shift register operations
		*x18 = *x0++ ^ *x7++;		//x(n+18) = x(n) + x(n+7)
		*y18 = (((*y0++ ^ *y5) ^ *y7++) ^ *y10);	//y(n+18) = y(n) + y(n+5) + y(n+7) + y(n+10)
		v_hold = ((*x4++ ^ *x6++) ^ *x15++);		//z(n) = x(n+4) + x(n+6) + x(n+15)
		//w(n) = y(n+5) + y(n+6) + y(n+8) + y(n+9) + y(n+10) + y(n+11) + y(n+12) + y(n+13) + y(n+14) + y(n+15)
		w_hold = (((((((((*y5++ ^ *y6++) ^ *y8++) ^ *y9++) ^ *y10++) ^ *y11++) ^ *y12++) ^ *y13++) ^ *y14++) ^ *y15++);		
		x18++;
		y18++;
				
		//Create output for c_long_1 and c_long_2 sequences
		// convert a "0" to +1 and a "1" to -1
		TempSequence -> real =  (1 - 2 * (short int) (x1_hold ^ y1_hold));
		TempSequence -> imaginary =  (1 - 2 * (short int) (v_hold ^ w_hold));
		TempSequence++;

		//Check for pointer overflow
		if (x15 == x_buf_end) x15 = x_buf_front;
		else if (x7 == x_buf_end) x7 = x_buf_front;
		else if (x6 == x_buf_end) x6 = x_buf_front;
		else if (x4 == x_buf_end) x4 = x_buf_front;
		else if (x0 == x_buf_end) 
		{
			x0 = x_buf_front;
			x18 = x_buf_front;
		}
		if (y15 == y_buf_end) y15 = y_buf_front;
		else if (y14 == y_buf_end) y14 = y_buf_front;
		else if (y13 == y_buf_end) y13 = y_buf_front;
		else if (y12 == y_buf_end) y12 = y_buf_front;
		else if (y11 == y_buf_end) y11 = y_buf_front;
		else if (y10 == y_buf_end) y10 = y_buf_front;
		else if (y9 == y_buf_end) y9 = y_buf_front;
		else if (y8 == y_buf_end) y8 = y_buf_front;
		else if (y7 == y_buf_end) y7 = y_buf_front;
		else if (y6 == y_buf_end) y6 = y_buf_front;
		else if (y5 == y_buf_end) y5 = y_buf_front;
		else if (y0 == y_buf_end) 
		{
			y0 = y_buf_front;
			y18 = y_buf_front;
		}
			
	}

//	Used for debugging purposes
/*
	fp=fopen("sequencenew.txt","w");
	TempSequence=Sequence;
	
	for (k=0;k<SEQUENCE_LENGTH;k++) 
	{
		fprintf( fp,"%d \t %f \t %f \n",k,TempSequence->real,TempSequence->imaginary);
		TempSequence++;
	}
	fclose(fp);*/
	
	free(x_buf_front);
	free(y_buf_front);
	return (Sequence);
}



int * TransmitterClass::OVSF_Code(unsigned SF)
/*****************************************************************************************************
/int * TransmitterClass::OVSF_Code(unsigned SF)
/
/ Copyright 2002 The Mobile and Portable Radio Research Group
/
/This function generates the all of the OVSF codes for the given spreading factor.  The codes are
/generated in accordance with the algorithms specified in ETSI Ts 125 213 V3.2.0 (2000-03).  The 
/number of codes that are generated is equal to the input paramter, which must be a power of 2.  For
/example, if the input is 4, then for codes are generated.  Further, they are stored in an integer array
/in the order that they were generated.
/
/Returns	Pointer to an aray of length SF^2 that contains all of the OVSF codes
/

⌨️ 快捷键说明

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