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

📄 main.c

📁 模擬無線通訊論文的模擬程式, 可用此程式驗證論文方法是否正確
💻 C
字号:
 /* 
 * This is simulation for 
 * "OVSF Code Channel Assignment for IMT-2000*" 
 * Authors of paper are Ray-Guang Cheng and Phone Lin
 */


/* include */
#include "common.h"
#include "events.h"
#include "queue.h"
#include "code.h"
#include "ranlib.h"

/****************************************************** 
 * global variables 
 ******************************************************/
/* system information */
static u8     system_state[MAX_BS];
static double simulation_clock = 0.0f;

/* statistical counters */
static u32 N;  /* number N of call arrivals */
static u32 Nd; /* number Nd of call departures */
static u32 Nf; /* number Nf of blocked calls 
                * due to the number of allocated 
                * code words > Ncode */
static u32 Nb; /* number Nb of blocked calls 
                * due to not enough code words
                * in a BS */

/* input parameters */
/* (lambda) the new call setup request rate received by BS
 * "expoinential distributed" */
                              
static double start_lambda   = 0.5f; /* start lambda for testing */                              
static double end_lambda     = 2.5f; /* end lambda for testing */                              
static double lambda_inv     = 0.5f; /* interval for lambda */                             
static int    lambda_inv_num = 0;    /* interval # for lambda */
                              
                              
/* (mu) the service time "expoinential distributed" */                           
static double mu   = 0.5f;                          
                           
                           
static int R = 55;    /* data rate requested by each UE
                        * be uniformly distributed between 
                        * 1 to R 
                        * the overall capacity of the system 
                        * is assumed to be "512" */
                        
static int capacity = 256; /* capacity of system */
u32 Ncode = 3;        /* the maximum number of code-words
                       * supported by UEs (constant) */
static int bound_case = 10;
static u8  num_of_bs  = 2 * 2; /* number of base stations */
                
/* output */
static double delay_time;  /* time for delay */
static double Pf;  /* Probability of Exceeding UE Code */
static double Pb;  /* Probability of Not Enough Code */ 
static double Pnc; /* Incomplete Probability */

/******************************************************/



/* function */


/* initial function */
static void initialization_routine()
{
/* loop counter */
int i;

    /* initialize clock */    
    simulation_clock = 0.0f;
    
    /* initialize state */
    for(i=0; i<num_of_bs; ++i)
        system_state[i] = SERVER_IDLE;  
    
    
    /****************************************************** 
     * initialize sttistical counters 
     ******************************************************/
    /* initialize counters */
    N  = 0;
    Nd = 0;
    Nf = 0;
    Nb = 0;
    
    /* initialize output */
    delay_time = 0.0f;
    Pf  = 0.0f;
    Pb  = 0.0f;
    Pnc = 0.0f;        
    /******************************************************/    
    
    /* initialize event list */ 
    initialize_event_list(num_of_bs);
    /* initialize event queue */  
    initialize_queue(num_of_bs);
    /* initialize code word */
    initialize_code_word(capacity);
   
    /* random seed */
    srand(time(NULL));
}

/* schedule arrival function */
static void schedule_arrival_event(double input_lambda)
{
event_t tmp_event; /* tmp for events */
u8 bs_index = generate_bs(num_of_bs);

    tmp_event.type        = ARRIVAL_TYPE;
//    tmp_event.timestamp   = get_bs_arrival_clock(bs_index) + generate_exp_X(input_lambda);
    tmp_event.timestamp   = simulation_clock + generate_exp_X(input_lambda);
    tmp_event.data_rate   = generate_normal_R(R);
    tmp_event.bs_location = bs_index;
    memset(tmp_event.code_word, -1, sizeof(int) * CODE_SF); /* set be -1 */
    events_add(&tmp_event); /* add to event list */   
    
    printf("schedule(arrival), %f, bs: %d\n", tmp_event.timestamp, tmp_event.bs_location);
}

/* schedule departure function */
static void schedule_departure_event(event_t *arrival_event, double input_mu)
{
event_t tmp_event; /* tmp for events */

    tmp_event.type        = DEPARTURE_TYPE;
    tmp_event.timestamp   = simulation_clock + generate_exp_X(input_mu);
    tmp_event.data_rate   = arrival_event->data_rate;
    tmp_event.bs_location = arrival_event->bs_location;
    memcpy(tmp_event.code_word, arrival_event->code_word, sizeof(int) * CODE_SF); /* copy code word */
    events_add(&tmp_event); /* add to event list */   
    
    printf("schedule(departure), %f, bs: %d\n", tmp_event.timestamp, tmp_event.bs_location);
}

/* add to event queue function */
static void add_to_event_queue(event_t *ins_event)
{
    queue_add_event(ins_event); /* add to event list */   
}

/* allocate code word */
static u8 allocate_code_word_for_UE(event_t *ins_event)
{
/* ret */
u8 ret = 0, ret2 = 0;

    /* calculate Copt1 */
    ret = sub_code_sub(ins_event->data_rate);
    
    if(ret == Pb_type){ /* Pb, it's not enough code */
        ++Nb;
        printf("pb\n");        
        return 0;
    }else if(!ret){ /* scuccess to calculate Copt1 */
        /* calculate Copt */
        ret2 = generate_code_opt_and_delete_BS(ins_event->code_word);
        printf("[%d,%d,%d,%d,%d,%d,%d]\n", ins_event->code_word[0],
        ins_event->code_word[1], ins_event->code_word[2], 
        ins_event->code_word[3], ins_event->code_word[4],
        ins_event->code_word[5], ins_event->code_word[6]);
        if(ret2 == Pf_type){ /* Pf, it's exceed Ncode */
            ++Nf;
            printf("pf\n");
            return 0;
        }
        return 1; /* success allocate */
    }
    return 0;
}


/* main program */
int main(int argc, char **argv)
{
int i; /* loop counter */
double now_lambda, lambda_cnt;
event_t tmp_event, tmp_event2; /* tmp for events */
u8 bs_index = 255;

    /****************************************************** 
     * calculate lambda, mu parameters
     ******************************************************/
    lambda_inv_num = ((int)floor((end_lambda - start_lambda)  / lambda_inv)) + 1;
    /******************************************************/

    /* run loop for lambda, mu intervals */
    for(i=0, now_lambda = start_lambda * mu, lambda_cnt = start_lambda;  /* set start value */
        i<lambda_inv_num;  /* set bound */
        ++i, lambda_cnt += lambda_inv, now_lambda = lambda_cnt * mu){ /* add interval */
    
        /* initial */
        initialization_routine();

        /* gerenate first events */
        schedule_arrival_event(now_lambda);

        /* start */
        print_event_list(0);print_event_list(1);print_event_list(2);print_event_list(3);
        print_event_queue(0);print_event_queue(1);print_event_queue(2);print_event_queue(3);
        while(!get_nearest_event(&tmp_event, num_of_bs)){
                                    
            //printf("%s, ", system_state[tmp_event.bs_location] ? "Busy":"Idle");
            print_event_list(0);print_event_list(1);print_event_list(2);print_event_list(3);
            print_event_queue(0);print_event_queue(1);print_event_queue(2);print_event_queue(3);

            /* 
             * adjust the system clock according to 
             * the timestamp of current event 
             */
            bs_index = tmp_event.bs_location;
            simulation_clock = tmp_event.timestamp;
        
            /* check type */
            switch(tmp_event.type){
            case ARRIVAL_TYPE: /* arrival type */
                 ++N; /* call arrival */
             
                 if(N < bound_case){
                     /* schedule the next arrival event */
                     schedule_arrival_event(now_lambda);
                 }
             
                 /* allocate code-word */
                 if(allocate_code_word_for_UE(&tmp_event)){ /* scuccess allocate */
                                       
                     /* check server */
                     if(system_state[bs_index] == SERVER_IDLE){ /* idle */
                         system_state[bs_index] = SERVER_BUSY;  /* set busy */
                         /* schedule the next arrival event */
                         schedule_departure_event(&tmp_event, mu);
                     }else if(system_state[bs_index] == SERVER_BUSY){ /* busy */
                         /* add to event queue */      
                         add_to_event_queue(&tmp_event);
                     }
                     printf("event %d(bs:%d) arrival (time:%f)\n", N, bs_index, simulation_clock);           
                 }else{
                     printf("event %d(bs:%d) arrival (time:%f, blocked)\n", N, bs_index, simulation_clock);
                 }
                                     
                 break;                       
             
            case DEPARTURE_TYPE: /* departure type */
                 ++Nd;
                 printf("event %d departure (time:%f)\n", Nd, simulation_clock);
             
                 /* back code word */
                 back_code_word_to_Ct(tmp_event.code_word);
             
                 /* check queue */
                 if(!get_event_queue_size(bs_index)){ /* queue is empty */
                     system_state[bs_index] = SERVER_IDLE;  /* set idle */                            
                 }else{ /* queue is not empty */
                     /* get event from queue */  
                     get_event_from_queue(&tmp_event2, bs_index);
                     /* schedule a departure event for this customer */
                     schedule_departure_event(&tmp_event2, mu);
                 
                     /* get delay time */
                     delay_time += (simulation_clock - tmp_event2.timestamp);
                 }
             
                 break;
            } /* switch case */
        /* start */
        print_event_list(0);print_event_list(1);print_event_list(2);print_event_list(3);
        print_event_queue(0);print_event_queue(1);print_event_queue(2);print_event_queue(3);        
            printf("\n");
        } /* while loop: event */
        
        Pf  = (double)Nf / N;
        Pb  = (double)Nb / N;    
        Pnc = (Pf + Pb) * 100;
        Pf *= 100;
        Pb *= 100;

        fprintf(stderr, "(lambda:%.2f) N:%ld, Nf:%ld, Nb:%ld, Pnc:%.3f%%, Pf:%.3f%%, Pb:%.3f%%, average delay time: %f\n", 
                now_lambda, N, Nf, Nb, Pnc, Pf, Pb, delay_time);
        print_Ct();
                
    }/* for loop: lambda interval*/

    return 0; /* success */    
}

⌨️ 快捷键说明

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