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

📄 code.c

📁 模擬無線通訊論文的模擬程式, 可用此程式驗證論文方法是否正確
💻 C
字号:
/* include */
#include "code.h"

/****************************************************** 
 * global variables 
 ******************************************************/
static u8 code_status[CODE_SF][CODE_SF_NUM]; /* code word status, 
                                              * being able to be assigned CODE_ABLE (1)
                                              * not being able to be assigned CODE_UNABLE (0) */

static int Ct[CODE_SF];    /* code for Ct */
static int Copt[CODE_SF];  /* code for code_opt1 */
static int Copt1[CODE_SF]; /* code for code_opt */

/* Ncode */
extern u32 Ncode;
/******************************************************/ 


/*************************************************************
 * utility function
 *************************************************************/
/*
 * set code word to be zero
 */
static void set_code_word_to_be_zero(int *code_word)
{
/* loop counter */     
int i;

    /* initial code words */
    for(i=0; i<CODE_SF; ++i)
        code_word[i]  = 0;               
}

/*
 * get code word value
 */
static int get_code_word_val(int *code_word)
{
/* code val */
int code_val = 0;

    /* calculate code word's value */
    code_val = (code_word[SF_4] << SF_4_S)   + (code_word[SF_8] << SF_8_S)     +
               (code_word[SF_16] << SF_16_S) + (code_word[SF_32] << SF_32_S)   +   
               (code_word[SF_64] << SF_64_S) + (code_word[SF_128] << SF_128_S) +
                code_word[SF_256];
                              
    /* return it */
    return code_val;
}

/*
 * convert value to code word inter
 */
static void convert_value_to_code_word_inter(int *code_word, int *code_word_val, u8 sf, 
                                             u8 shift_bits, u16 max_code_words)
{
/* factor */
int factor;

    factor = (*code_word_val) >> shift_bits;
    if(factor > max_code_words){ /* exceed maximum */
        code_word[sf] = max_code_words;              
        (*code_word_val) -= (max_code_words << shift_bits);
    }else{ /* not exceed maximum */
        code_word[sf] = factor; 
        (*code_word_val) -= (factor << shift_bits);
    }
}


/*
 * convert value to code word
 */
static void convert_value_to_code_word(int *code_word, int code_word_val)
{
 
    /* convert SF=4 */
    convert_value_to_code_word_inter(code_word, &code_word_val, SF_4, SF_4_S, MAX_C_SF_4);
    /* convert SF=8 */
    convert_value_to_code_word_inter(code_word, &code_word_val, SF_8, SF_8_S, MAX_C_SF_8);
    /* convert SF=16 */
    convert_value_to_code_word_inter(code_word, &code_word_val, SF_16, SF_16_S, MAX_C_SF_16);
    /* convert SF=32 */
    convert_value_to_code_word_inter(code_word, &code_word_val, SF_32, SF_32_S, MAX_C_SF_32);
    /* convert SF=64 */
    convert_value_to_code_word_inter(code_word, &code_word_val, SF_64, SF_64_S, MAX_C_SF_64);
    /* convert SF=128 */
    convert_value_to_code_word_inter(code_word, &code_word_val, SF_128, SF_128_S, MAX_C_SF_128);
    /* convert SF=256 */
    convert_value_to_code_word_inter(code_word, &code_word_val, SF_256, SF_256_S, MAX_C_SF_256);

}
/******************************************************/ 


/*************************************************************
 * print function
 *************************************************************/
/* print code words */
static void print_code_words(const char *name, int *cw)
{
/* loop counter */
int i;

    fprintf(stderr, "%s(", name);
    for(i=0; i<CODE_SF; ++i){
        if(i == CODE_SF - 1)
             fprintf(stderr, "%d)", cw[i]);         
        else fprintf(stderr, "%d,", cw[i]);
    }
    fprintf(stderr, "\n");
}

/* print Ct */
void print_Ct()
{
    print_code_words("Ct", Ct);
}

/* print Copt */
void print_Copt()
{
    print_code_words("Copt", Copt);
}

/* print Copt1 */
void print_Copt1()
{
    print_code_words("Copt1", Copt1);
} 
/*************************************************************/




/*
 * initialize all code-word array
 */
void initialize_code_word(int Ct_val)
{
/* loop counter */     
int i, j, k;
#if 0
    for(i=0; i<CODE_SF; ++i){
        for(j=0; j<CODE_SF_NUM; ++j){
            code_status[i][j] = CODE_UNABLE;
        }     
    }
    
    /* set able */ 
    for(i=1; i<CODE_SF; ++i){
        k = (int)pow( 2, i+1);
        for(j=1; j<=k; ++j){
            code_status[i][j] = CODE_ABLE;      
        }         
    }
#endif
    
    /* initial code words */
    for(i=0; i<CODE_SF; ++i){
        Ct[i]    = 0;
        Copt1[i] = 0;
        Copt[i]  = 0;         
    }
    
    convert_value_to_code_word(Ct, Ct_val);
}


/*************************************************************
 * Copt1 function
 *************************************************************/

/*
 * This function is used to find the Copt1
 * After executig this subroutine, the result was stored in
 * the an array named Copt1[1..7]
 * Copt1 = Ct - (0, 0, 0, 0, 0, 0, n)
 */
u8 sub_code_sub(int data_rate)
{
/* tmp value */
int tmp_val = 0;

    if(!data_rate) return 1;
    
    /* set zero first */
    set_code_word_to_be_zero(Copt1);

    /* get all Ct value */
    tmp_val = get_code_word_val(Ct) - data_rate;
    
    if(tmp_val < 0){ /* it's not enough code, Pb */
        return Pb_type;       
    }else{
        /* convert back to code word */
        convert_value_to_code_word(Copt1, tmp_val);
      //  printf("Copt1(%d,%d,%d,%d,%d,%d,%d) \n", Copt1[0], Copt1[1], Copt1[2],
      //         Copt1[3], Copt1[4], Copt1[5], Copt1[6]);
        return 0;
    }
}
/*************************************************************/



/*************************************************************
 * Copt function
 *************************************************************/
/*
 * directly minus
 */
static u8 directly_minus(int *Ct, int *Copt1, int *Copt)
{
/* loop counter */
int i;

    for(i=0; i<CODE_SF; ++i){
        Copt[i] = Ct[i] - Copt1[i];
        if(Copt[i] < 0)  return 0; /* can't minus */
    }
    return 1; /* directly minus */
}

/*
 * This function is used to calculate N(C)
 */
static int number_of_code_word(int *code_word)
{
    return (code_word[SF_4]  + code_word[SF_8]  + code_word[SF_16]  +
            code_word[SF_32] + code_word[SF_64] + code_word[SF_128] +
            code_word[SF_256]); /* add all # */
}

/*
 * This function is used to find the Copt
 * After executig this subroutine, the result was stored in
 * the an array named Copt[1..7]
 */
u8 generate_code_opt_and_delete_BS(int *get_cw)
{
/* tmp value */
int tmp_val = 0;

    if(!directly_minus(Ct, Copt1, Copt)){ /* check is it directly minus ? */     
        /* can't directly minus */
        tmp_val = get_code_word_val(Ct) - get_code_word_val(Copt1);
        convert_value_to_code_word(Copt, tmp_val);    
    }

    /* get Copt */
    memcpy(get_cw, Copt, sizeof(int) * CODE_SF);

    /* check N(Copt) and Ncode */
    if(number_of_code_word(Copt) > Ncode){
        return Pf_type; /* Pf type */     
    }else{
        /* get new Ct */
        memcpy(Ct, Copt1, sizeof(int) * CODE_SF);      
    }
    
    return 0; /* ok */
}
/*************************************************************/




/*************************************************************
 * Ct function
 *************************************************************/

/*
 * check is it greather
 */
static u8 check_greater_sf(int *back_cw)
{
    
    /* check code word */
    if(back_cw[SF_256] > MAX_C_SF_256) return 1;
    if(back_cw[SF_128] > MAX_C_SF_128) return 1;
    if(back_cw[SF_64]  > MAX_C_SF_64)  return 1;
    if(back_cw[SF_32]  > MAX_C_SF_32)  return 1;
    if(back_cw[SF_16]  > MAX_C_SF_16)  return 1;
    if(back_cw[SF_8]   > MAX_C_SF_8)   return 1;
    if(back_cw[SF_4]   > MAX_C_SF_4)   return 1;
    
    /* no greater */
    return 0;
}
     
     
/*
 * back code word from event to Ct
 */
void back_code_word_to_Ct(int *back_cw)
{
/* loop counter */
int i;

    for(i=0; i<CODE_SF; ++i) /* add back */
        Ct[i] += back_cw[i];
    
    /* check is it voliate the assume ? */
    //if(check_greater_sf(Ct)){ /* Yes, it's greater than conditions. */
        /* do combine action */
        convert_value_to_code_word(Ct, get_code_word_val(Ct)); 
    //}       
}
/*************************************************************/

⌨️ 快捷键说明

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