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

📄 code.c

📁 wcdma模型
💻 C
字号:
/* | Project:     WCDMA simulation environment | Module:     | Author:      Maarit Melvasao | Date:        May 15, 1999 | | History: |              June 10, 1999 Maarit Melvasalo |                      Double function added for matlab mex files. |  | Limitations: |              Code length must be in between 2^2 ... 2^8 |              The index must be greater tahn 0 and less or equal |              to code length       | | Copyright disclaimer: |   This software was developed at the National Institute of Standards |   and Technology by employees of the Federal Government in the course |   of their official duties. Pursuant to title 17 Section 105 of the |   United States Code this software is not subject to copyright |   protection and is in the public domain. | |   We would appreciate acknowledgement if the software is used. |*/      #include <math.h>#include "code.h"#include "config_wcdma.h"#include "wcdma_simulink.h"#ifndef TRUE#define TRUE 1#define FALSE 0#endif /* TRUE *//* ------------ S T A T I C   D A T A   S T R U C T U R E S ----------- *//* List that keeps record which code instances are used /**/static enum instance_state code_alloc_list[MAX_CODES];/* Total number of occupied codes /**/static int code_count = 0;/* General initialization flag /**/static int general_code_init_flag = FALSE;/* Code matirx to keep in memory the code length and    the index number of each code instance /**/static code_matrix code_data[MAX_CODES];/* -------------------------------------------------------------------- *//* * Function:    wcdma_channel_init *  * Desc.:       Code initialization *               *              Checks that number of codes is accurate.  *              Saves the spreading code length to instance. *              Returns the code instance.    * Note: *//* -------------------------------------------------------------------- */int wcdma_code_init(int code_lenght){  int i,j,instance,n_intervals;    /*     * If first call, initialize static data.     */    if (general_code_init_flag == FALSE) {        for (i=0; i < MAX_CODES; i++) {            code_alloc_list[i] = FREE_INSTANCE;        } /* for */        general_code_init_flag = TRUE;    } /* if general_init_flag */    /*     * Find first free instance number.     */    instance = -1;    for (i=0; i < MAX_CODES; i++) {        if (code_alloc_list[i] == FREE_INSTANCE) {            instance = i;            break;        }    }    if (instance == -1) return(-1); /* no free instances */        code_count ++;    code_data[instance].code_len = code_lenght;    code_alloc_list[i] =  INSTANCE_IN_USE;   return(instance);}/* -------------------------------------------------------------------- *//* * Function:    wcdma_ovsf_code (int instance, int code[]) * * Desc.:               *          *              Finds the next available code for given code instance. *              Return value = index number   /**//* -------------------------------------------------------------------- */int wcdma_ovsf_code(		    int instance, 		    int code[]){  int tmp,nCode,index ;  code_matrix *code_tree;  code_tree = &code_data[instance];  /*    Finds the next suitable code index for given code instance    If no orthogonal codes can be found returns -1    /**/  tmp = wcdma_code_tree_update(instance, code_tree);	    if(tmp == -1) return(-1);  /*       Gets the OVSF code with given length and index.    /**/  nCode = code_tree->code_len;  index = code_tree->index;  wcdma_get_ovsf_code(nCode, index, code);/**/    return(code_tree->index);}/* -------------------------------------------------------------------- *//* * Function:    wcdma_ovsf_code_double * * Desc.:       Interface function between mex-functions (matlab) and *              function:   wcdma_ovsf_code (int instance, int code[]) * *              Return value : code index * Note:        Here the code is double vector not a integer.       * *//* -------------------------------------------------------------------- */int wcdma_ovsf_code_double(int instance, double code[]){  int code_tmp[MAX_SPREADING_FACTOR];  int tmp,i,nCode;  code_matrix *code_tree;  /*      Call  wcdma_ovsf_code  with a integer code vector  /**/     tmp = wcdma_ovsf_code(instance, code_tmp);  if(tmp == -1) return(-1);  /* If the code was succesfully found return a double vector      /**/  code_tree = &code_data[instance];  nCode = code_tree->code_len;  for (i=0; i < nCode ;i++){    code[i] = code_tmp[i];    }  return(tmp);}/* -------------------------------------------------------------------- *//* * Function:    wcdma_get_ovsf_code * * Desc.:       Gets the OVSF code for given code length and index * * Note:        Returns 0 *//* -------------------------------------------------------------------- */int wcdma_get_ovsf_code(			int nCode, 			int index, 			int *code){    code_matrix *code_tree;    int i, k ,l, k_max, sf, i_tmp,cur_k, a;    int tmp[9];    /*        If the index is 1 code is just 1's       /**/    if (index == 1 ){      for (i = 0; i<nCode; i++){	code[i] = 1; }    return(0);    }    /*        If the index is nCode code is just - 1's       /**/    if (index == nCode ){      for (i = 0; i<nCode; i++){	code[i] = - 1; }    return(0);    }    /* else /**/    k_max = 0;    sf = nCode / 2;    i_tmp = index;    tmp[0] = index;    /*        Find the code index's in the tree above the given code        /**/    while ( sf > 1) {      k_max++;      i_tmp = floor((i_tmp + 1 )/2);      tmp[k_max] = i_tmp;       sf = sf / 2;    }    /*        Get the code for the given path in the tree       Initial code is 1.        /**/        code[0] = 1;    for (k = 0; k <= k_max ; k++){      /* 		 The length of the current code  /**/      cur_k = pow(2, k );      /* 	 Indicator to which brach to take next 	 /**/       /*      a = 1;  	      if((tmp[k_max - k] % 2) == 0 ) a = -1;  /**/      a = ((tmp[k_max - k] % 2) == 0 ) ? -1 : 1;      /*	Add the new branch to existing code	/**/      for (i = 0; i < cur_k ; i++){	code[ cur_k + i ] = a * code[ i ];	}    }    return(0);}/* -------------------------------------------------------------------- *//* * Function:    wcdma_get_ovsf_code_double * Desc.:        * Desc.:       Interface function between mex-functions (matlab) and *              function:   wcdma_get_ovsf_code ( ) * *              Return value : int * Note:        Here the code is double vector not a integer.       * *//* -------------------------------------------------------------------- */int wcdma_get_ovsf_code_double(			       int nCode, 			       int index, 			       double code[]){  int code_tmp[MAX_SPREADING_FACTOR];  int i,tmp; /*      Call  wcdma_ovsf_code  with a integer code vector  /**/     tmp = wcdma_get_ovsf_code(nCode, index, code_tmp);  /* If the code was succesfully found return a double vector      /**/  for (i=0; i<nCode;i++){    code[i] = (int) code_tmp[i];    }/**/   return(0);}/* -------------------------------------------------------------------- *//* * Function:    wcdma_code_tree_update * * Desc.:       Finds the next suitable code index ofr given code instance *              and updates the code_matrix        * *              Return value =  *                        1 if a acceptable candidate was found *                        -1 if no more codes of this leght are available  * Note:  /**//* -------------------------------------------------------------------- */int wcdma_code_tree_update(			   int instance, 			   code_matrix *code_tree){    int i, j, c, k, k_index, c_index, c_len, nCode, pot,p,ican;    int small,big,k_tmp;    int flag[MAX_SPREADING_FACTOR];    nCode = code_tree->code_len;        /*      If this is the first coe      /**/    if (code_count == 1) {      code_tree->index = nCode / 2;        return (1);    }    /*        Tmp vector to indicate if the index is acceptable or not       /**/    for (i = 0; i<nCode ; i++ ){      flag[i] = 1;    }    /*      Search through all possible index's until suitable index is found.      Start the search from the codes in  the middle      /**/    for (i = nCode/2; i<nCode ; i++ ){        /* 	 look for codes in the upper and in the lower branch   /**/      for (j = 0; j < 2; j++ ){         	ican = 1 + j * nCode + pow(-1, j) * (i + j);  /*candidate for index /**/	/* j = 0 -> ican = nCode/2 + 1... nCode i.e. lower codes/*/	/* j = 1 -> ican = nCode/2  .... 1 i.e. upper codes/**/	/* ican = 1 + [8 7 9 6 10 5 11 4 12 3 13 2 14 1 15 0] /**/	/*	  Searches over the existing codes	  /**/	for (c = 0; c < MAX_CODES ; c++){  /*code_count; c++){/**/	  if ((c != instance) && (code_alloc_list[c] == INSTANCE_IN_USE))	    {	       	      c_index = code_data[c].index;	      c_len = code_data[c].code_len;	      /* 		 First check if the codes are on the different sides of the middle 		 /**/	      if (j > 0 &&  c_index > (c_len /2) ) {	      }	      else if ( j == 0 && c_index <= (c_len /2 ) ){	      } /* it is ok for to use this index compared to this existing code /**/	      	      /* Else if  		  the code length is same with the existing one		  /**/  	      else if (nCode == c_len ){   		if( ican == c_index) {    /* if the index is already in use /**/  		  flag[ican - 1] = 0; break;}  /* this index can not be used/**/	      }	      /* Else if 		 the existing code is longer /**/	      else if (nCode < c_len){ 		k_tmp = 2;		while (k_tmp * nCode < c_len) 		  { k_tmp = k_tmp * 2; };		if( (c_index  <=  (k_tmp * ican ) ) && ( c_index >  (k_tmp * (ican-1) ))){		  flag[ican - 1] = 0; break; /* this index can not be used/**/		}}	      /* Else if		 the existing code is shorter /**/ 	      	      else { 		k_tmp = 2;		while (k_tmp * c_len < nCode) 		  { k_tmp = k_tmp * 2; };		if(  (ican /k_tmp + min(1,(ican % k_tmp))) ==  c_index){ 		  flag[ican - 1] = 0; break; }  /* this index can not be used/**/	      }	    }  /* for-loop over the existing codes/**/	}	/* if 	   ican was a suitable candidate compared to all existing codes	   /**/	if (flag[ican - 1 ] == 1){	  code_tree->index = ican; 	  return(1);	}      }    }    code_tree->index = -1 ;     return (-1); /*no possible code index was found/**/}/* -------------------------------------------------------------------- *//* * Function:    wcdma_find_index * * Desc.:       Finds the next suitable code index for given code lengths *               * *              Return value =  *                        code index  if a acceptable candidate was found *                        -1 if no more codes of this leght are available  * * Note:        This is a interface function for matlab mex   /**//* -------------------------------------------------------------------- */int wcdma_find_index( int instance) {  int index,tmp ;  code_matrix *code_tree;  code_tree = &code_data[instance];  /*    Finds the next suitable code index for given code instance    If no orthogonal codes can be found returns -1    /**/  tmp = wcdma_code_tree_update(instance, code_tree);    if(tmp == -1) return(-1);  /*    Retuns the suitable index    /**/  return(code_tree->index);}/* -------------------------------------------------------------------- *//* * Function:    wcdma_find_index * * Desc.:       Finds the next suitable code index for given code lengths *               * *              Return value =  *                        code index  if a acceptable candidate was found *                        -1 if no more codes of this leght are available  * * Note:        This is a interface function for matlab mex   /**//* -------------------------------------------------------------------- */int wcdma_get_indexes( int number_of_codes,		       double codes[],		       double *indexes) {  int index,tmp,i ;  for (i = 0 ; i<number_of_codes; i++){     tmp = wcdma_code_init((int)codes[i]);    /*      Finds the next suitable code index for given code instance      If no orthogonal codes can be found returns -1      /**/    indexes[i] = (double)wcdma_find_index(tmp);  }  for (i = 0 ; i<number_of_codes; i++){     tmp = wcdma_code_free(i);  }}/* -------------------------------------------------------------------- *//* * Function:    wcdma_code_print * * Desc.:       just a PRINT FUNCTION !!!!!!!!!!!   * * Note:        For testing purposes only *//* -------------------------------------------------------------------- */void code_print(int instance){  int tmp ;    code_matrix *code_tree;    code_tree = &code_data[instance];    printf("instance: %d  len: %d  index: %d \n", instance,code_tree->code_len,code_tree->index);}/* -------------------------------------------------------------------- *//* * Function:    wcdma_code_free * * Desc.:       code clear function. * * Note: *//* -------------------------------------------------------------------- */int wcdma_code_free(int instance){     code_alloc_list[instance] = FREE_INSTANCE;  code_count--;  return(0);}

⌨️ 快捷键说明

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