Esempio n. 1
0
main(int argc,char **argv) {

  int input[LENGTH];
  int energy=0,dc_r=0,dc_i=0;
  short s=1,i;
  int amp;

  amp = atoi(argv[1]);// arguments to integer
  if (argc>1)
    printf("Amp = %d\n",amp);

  for (i=0;i<LENGTH;i++) {
    s = -s;
    ((short*)input)[2*i]     = 31 + (short)(amp*sin(2*M_PI*i/LENGTH));
    ((short*)input)[1+(2*i)] = 30 + (short)(amp*cos(2*M_PI*i/LENGTH));
    energy += (((short*)input)[2*i]*((short*)input)[2*i]) + (((short*)input)[1+(2*i)]*((short*)input)[1+(2*i)]);
    dc_r += ((short*)input)[2*i];
    dc_i += ((short*)input)[1+(2*i)];


  }
  energy/=LENGTH;
  dc_r/=LENGTH;
  dc_i/=LENGTH;

  printf("signal_energy = %d dB(%d,%d,%d,%d)\n",dB_fixed(signal_energy(input,LENGTH)),signal_energy(input,LENGTH),energy-(dc_r*dc_r+dc_i*dc_i),energy,(dc_r*dc_r+dc_i*dc_i));
  printf("dc = (%d,%d)\n",dc_r,dc_i);
}
void
phy_adjust_gain (PHY_VARS_UE *phy_vars_ue, uint8_t eNB_id)
{

  uint16_t rx_power_fil_dB;
#ifdef EXMIMO
  exmimo_config_t *p_exmimo_config = openair0_exmimo_pci[card].exmimo_config_ptr;
  uint16_t i;
#endif
  int rssi;

  rssi = dB_fixed(phy_vars_ue->PHY_measurements.rssi);

  if (rssi>0) rx_power_fil_dB = rssi;
  else rx_power_fil_dB = phy_vars_ue->PHY_measurements.rx_power_avg_dB[eNB_id];

  LOG_D(PHY,"Gain control: rssi %d (%d,%d)\n",
         rssi,
         phy_vars_ue->PHY_measurements.rssi,
         phy_vars_ue->PHY_measurements.rx_power_avg_dB[eNB_id]
        );

  // Gain control with hysterisis
  // Adjust gain in phy_vars_ue->rx_vars[0].rx_total_gain_dB

  if (rx_power_fil_dB < TARGET_RX_POWER - 5) //&& (phy_vars_ue->rx_total_gain_dB < MAX_RF_GAIN) )
    phy_vars_ue->rx_total_gain_dB+=5;
  else if (rx_power_fil_dB > TARGET_RX_POWER + 5) //&& (phy_vars_ue->rx_total_gain_dB > MIN_RF_GAIN) )
    phy_vars_ue->rx_total_gain_dB-=5;

  if (phy_vars_ue->rx_total_gain_dB>MAX_RF_GAIN) {
    /*
    if ((openair_daq_vars.rx_rf_mode==0) && (openair_daq_vars.mode == openair_NOT_SYNCHED)) {
      openair_daq_vars.rx_rf_mode=1;
      phy_vars_ue->rx_total_gain_dB = max(MIN_RF_GAIN,MAX_RF_GAIN-25);
    }
    else {
    */
    phy_vars_ue->rx_total_gain_dB = MAX_RF_GAIN;
  } else if (phy_vars_ue->rx_total_gain_dB<MIN_RF_GAIN) {
    /*
    if ((openair_daq_vars.rx_rf_mode==1) && (openair_daq_vars.mode == openair_NOT_SYNCHED)) {
      openair_daq_vars.rx_rf_mode=0;
      phy_vars_ue->rx_total_gain_dB = min(MAX_RF_GAIN,MIN_RF_GAIN+25);
    }
    else {
    */
    phy_vars_ue->rx_total_gain_dB = MIN_RF_GAIN;
  }

  LOG_D(PHY,"Gain control: rx_total_gain_dB = %d (max %d,rxpf %d)\n",phy_vars_ue->rx_total_gain_dB,MAX_RF_GAIN,rx_power_fil_dB);

#ifdef EXMIMO

  if (phy_vars_ue->rx_total_gain_dB>phy_vars_ue->rx_gain_max[0]) {
    phy_vars_ue->rx_total_gain_dB = phy_vars_ue->rx_gain_max[0];

    for (i=0; i<phy_vars_ue->lte_frame_parms.nb_antennas_rx; i++) {
      p_exmimo_config->rf.rx_gain[i][0] = 30;
    }

  } else if (phy_vars_ue->rx_total_gain_dB<(phy_vars_ue->rx_gain_max[0]-30)) {
    // for the moment we stay in max gain mode
    phy_vars_ue->rx_total_gain_dB = phy_vars_ue->rx_gain_max[0] - 30;

    for (i=0; i<phy_vars_ue->lte_frame_parms.nb_antennas_rx; i++) {
      p_exmimo_config->rf.rx_gain[i][0] = 0;
    }

    /*
      phy_vars_ue->rx_gain_mode[0] = byp;
      phy_vars_ue->rx_gain_mode[1] = byp;
      exmimo_pci_interface->rf.rf_mode0 = 22991; //bypass
      exmimo_pci_interface->rf.rf_mode1 = 22991; //bypass

      if (phy_vars_ue->rx_total_gain_dB<(phy_vars_ue->rx_gain_byp[0]-50)) {
      exmimo_pci_interface->rf.rx_gain00 = 0;
      exmimo_pci_interface->rf.rx_gain10 = 0;
      }
    */
  } else {

    for (i=0; i<phy_vars_ue->lte_frame_parms.nb_antennas_rx; i++) {
      p_exmimo_config->rf.rx_gain[i][0] =  30 - phy_vars_ue->rx_gain_max[0] + phy_vars_ue->rx_total_gain_dB;
    }
  }

  /*
    break;
  case med_gain:
  case byp_gain:
      if (phy_vars_ue->rx_total_gain_dB>phy_vars_ue->rx_gain_byp[0]) {
          phy_vars_ue->rx_gain_mode[0]   = max_gain;
          phy_vars_ue->rx_gain_mode[1]   = max_gain;
          exmimo_pci_interface->rf.rf_mode0 = 55759; //max gain
          exmimo_pci_interface->rf.rf_mode1 = 55759; //max gain

          if (phy_vars_ue->rx_total_gain_dB>phy_vars_ue->rx_gain_max[0]) {
              exmimo_pci_interface->rf.rx_gain00 = 50;
              exmimo_pci_interface->rf.rx_gain10 = 50;
          }
          else {
              exmimo_pci_interface->rf.rx_gain00 = 50 - phy_vars_ue->rx_gain_max[0] + phy_vars_ue->rx_total_gain_dB;
              exmimo_pci_interface->rf.rx_gain10 = 50 - phy_vars_ue->rx_gain_max[1] + phy_vars_ue->rx_total_gain_dB;
          }
      }
      else if (phy_vars_ue->rx_total_gain_dB<(phy_vars_ue->rx_gain_byp[0]-50)) {
          exmimo_pci_interface->rf.rx_gain00 = 0;
          exmimo_pci_interface->rf.rx_gain10 = 0;
      }
      else {
          exmimo_pci_interface->rf.rx_gain00 = 50 - phy_vars_ue->rx_gain_byp[0] + phy_vars_ue->rx_total_gain_dB;
          exmimo_pci_interface->rf.rx_gain10 = 50 - phy_vars_ue->rx_gain_byp[1] + phy_vars_ue->rx_total_gain_dB;
      }
      break;
  default:
      exmimo_pci_interface->rf.rx_gain00 = 50;
      exmimo_pci_interface->rf.rx_gain10 = 50;
      break;
  }
      */
#endif

#ifdef DEBUG_PHY
  /*  if ((phy_vars_ue->frame%100==0) || (phy_vars_ue->frame < 10))
  msg("[PHY][ADJUST_GAIN] frame %d,  rx_power = %d, rx_power_fil = %d, rx_power_fil_dB = %d, coef=%d, ncoef=%d, rx_total_gain_dB = %d (%d,%d,%d)\n",
    phy_vars_ue->frame,rx_power,rx_power_fil,rx_power_fil_dB,coef,ncoef,phy_vars_ue->rx_total_gain_dB,
  TARGET_RX_POWER,MAX_RF_GAIN,MIN_RF_GAIN);
  */
#endif //DEBUG_PHY

}
Esempio n. 3
0
int main(int argc, char **argv) {

    char c;

    int i,aa,aarx;
    double sigma2, sigma2_dB=0,SNR,snr0=10.0,snr1=10.2;
    int snr1set=0;
    uint32_t *txdata,*rxdata[2];
    double *s_re[2],*s_im[2],*r_re[2],*r_im[2];
    double iqim=0.0;
    int trial, ntrials=1;
    int n_rx=1;

    int awgn_flag=0;
    int n_frames=1;
    channel_desc_t *ch;
    uint32_t tx_lev,tx_lev_dB;
    int interf1=-19,interf2=-19;
    SCM_t channel_model=AWGN;
    uint32_t sdu_length_samples;
    TX_VECTOR_t tx_vector;
    int errors=0,misdetected_errors=0,signal_errors=0;
    int symbols=0;
    int tx_offset = 0,rx_offset;
    RX_VECTOR_t *rxv;
    uint8_t *data_ind,*data_ind_rx;
    int no_detection=1;
    int missed_packets=0;
    uint8_t rxp;
    int off,off2;
    double txg,txg_dB;
    int log2_maxh;
    double  snr_array[100];
    int  errors_array[100];
    int  trials_array[100];
    int  misdetected_errors_array[100];
    int  signal_errors_array[100];
    int  missed_packets_array[100];
    int  cnt=0;
    char fname[100],vname[100];
    int stop=0;

    data_ind    = (uint8_t*)malloc(4095+2+1);
    data_ind_rx = (uint8_t*)malloc(4095+2+1);

    tx_vector.rate=1;
    tx_vector.sdu_length=256;
    tx_vector.service=0;

    logInit();

    randominit(0);
    set_taus_seed(0);

    // Basic initializations
    init_fft(64,6,rev64);
    init_interleavers();
    ccodedot11_init();
    ccodedot11_init_inv();
    phy_generate_viterbi_tables();

    init_crc32();

    data_ind[0] = 0;
    data_ind[1] = 0;

    tx_offset = taus()%(FRAME_LENGTH_SAMPLES_MAX/2);

    while ((c = getopt (argc, argv, "hag:n:s:S:z:r:p:d:")) != -1) {
        switch (c) {
        case 'a':
            printf("Running AWGN simulation\n");
            awgn_flag = 1;
            ntrials=1;
            break;
        case 'g':
            switch((char)*optarg) {
            case 'A':
                channel_model=SCM_A;
                break;
            case 'B':
                channel_model=SCM_B;
                break;
            case 'C':
                channel_model=SCM_C;
                break;
            case 'D':
                channel_model=SCM_D;
                break;
            case 'E':
                channel_model=EPA;
                break;
            case 'F':
                channel_model=EVA;
                break;
            case 'G':
                channel_model=ETU;
                break;
            case 'H':
                channel_model=Rayleigh8;
            case 'I':
                channel_model=Rayleigh1;
            case 'J':
                channel_model=Rayleigh1_corr;
            case 'K':
                channel_model=Rayleigh1_anticorr;
            case 'L':
                channel_model=Rice8;
            case 'M':
                channel_model=Rice1;
                break;
            default:
                printf("Unsupported channel model!\n");
                exit(-1);
            }
            break;
        case 'd':
            tx_offset = atoi(optarg);
            break;
        case 'p':
            tx_vector.sdu_length = atoi(optarg);
            if (atoi(optarg)>4095) {
                printf("Illegal sdu_length %d\n",tx_vector.sdu_length);
                exit(-1);
            }
            break;
        case 'r':
            tx_vector.rate = atoi(optarg);
            if (atoi(optarg)>7) {
                printf("Illegal rate %d\n",tx_vector.rate);
                exit(-1);
            }
            break;
        case 'n':
            n_frames = atoi(optarg);
            break;
        case 's':
            snr0 = atof(optarg);
            printf("Setting SNR0 to %f\n",snr0);
            break;
        case 'S':
            snr1 = atof(optarg);
            snr1set=1;
            printf("Setting SNR1 to %f\n",snr1);
            break;
        case 'z':
            n_rx=atoi(optarg);
            if ((n_rx==0) || (n_rx>2)) {
                printf("Unsupported number of rx antennas %d\n",n_rx);
                exit(-1);
            }
            break;
        default:
        case 'h':
            printf("%s -h(elp) -a(wgn on) -p(extended_prefix) -N cell_id -f output_filename -F input_filename -g channel_model -n n_frames -t Delayspread -r Ricean_FactordB -s snr0 -S snr1 -x transmission_mode -y TXant -z RXant -i Intefrence0 -j Interference1 -A interpolation_file -C(alibration offset dB) -N CellId\n",argv[0]);
            printf("-h This message\n");
            printf("-a Use AWGN channel and not multipath\n");
            printf("-n Number of frames to simulate\n");
            printf("-s Starting SNR, runs from SNR0 to SNR0 + 5 dB.  If n_frames is 1 then just SNR is simulated\n");
            printf("-S Ending SNR, runs from SNR0 to SNR1\n");
            printf("-g [A,B,C,D,E,F,G] Use 3GPP SCM (A,B,C,D) or 36-101 (E-EPA,F-EVA,G-ETU) models (ignores delay spread and Ricean factor)\n");
            printf("-z Number of RX antennas used\n");
            printf("-F Input filename (.txt format) for RX conformance testing\n");
            exit (-1);
            break;
        }
    }

    if (n_frames==1)
        snr1 = snr0+.2;
    else
        snr1 = snr0+5;
    for (i=0; i<tx_vector.sdu_length; i++)
        data_ind[i+2] = i;//taus();  // randomize packet
    data_ind[tx_vector.sdu_length+2+4]=0;  // Tail byte

    // compute number of OFDM symbols in DATA period
    symbols = ((4+2+1+tx_vector.sdu_length)<<1) / nibbles_per_symbol[tx_vector.rate];
    if ((((4+2+1+tx_vector.sdu_length)<<1) % nibbles_per_symbol[tx_vector.rate]) > 0)
        symbols++;

    sdu_length_samples = (symbols + 5) * 80;

    printf("Number of symbols for sdu : %d, samples %d\n",symbols,sdu_length_samples);

    txdata = (uint32_t*)memalign(16,sdu_length_samples*sizeof(uint32_t));
    for (i=0; i<n_rx; i++) {
        rxdata[i] = (uint32_t*)memalign(16,(FRAME_LENGTH_SAMPLES_MAX+1280)*sizeof(uint32_t));
        bzero(rxdata[i],(FRAME_LENGTH_SAMPLES_MAX+1280)*sizeof(uint32_t));
    }
    s_re[0] = (double *)malloc(sdu_length_samples*sizeof(double));
    bzero(s_re[0],sdu_length_samples*sizeof(double));
    s_im[0] = (double *)malloc(sdu_length_samples*sizeof(double));
    bzero(s_im[0],sdu_length_samples*sizeof(double));
    for (i=0; i<n_rx; i++) {
        r_re[i] = (double *)malloc((sdu_length_samples+100)*sizeof(double));
        bzero(r_re[i],(sdu_length_samples+100)*sizeof(double));
        r_im[i] = (double *)malloc((sdu_length_samples+100)*sizeof(double));
        bzero(r_im[i],(sdu_length_samples+100)*sizeof(double));
    }

    ch = new_channel_desc_scm(1,
                              n_rx,
                              channel_model,
                              BW,
                              0.0,
                              0,
                              0);


    if (ch==NULL) {
        printf("Problem generating channel model. Exiting.\n");
        exit(-1);
    }


    phy_tx_start(&tx_vector,txdata,0,FRAME_LENGTH_SAMPLES_MAX,data_ind);

    tx_lev = signal_energy((int32_t*)txdata,320);
    tx_lev_dB = (unsigned int) dB_fixed(tx_lev);

    write_output("txsig0.m","txs", txdata,sdu_length_samples,1,1);

    // multipath channel

    for (i=0; i<sdu_length_samples; i++) {
        s_re[0][i] = (double)(((short *)txdata)[(i<<1)]);
        s_im[0][i] = (double)(((short *)txdata)[(i<<1)+1]);
    }

    for (SNR=snr0; SNR<snr1; SNR+=.2) {

        printf("n_frames %d SNR %f sdu_length %d rate %d\n",n_frames,SNR,tx_vector.sdu_length,tx_vector.rate);
        errors=0;
        misdetected_errors=0;
        signal_errors=0;
        missed_packets=0;
        stop=0;
        for (trial=0; trial<n_frames; trial++) {
            //      printf("Trial %d (errors %d), sdu_length_samples %d\n",trial,errors,sdu_length_samples);
            sigma2_dB = 25; //10*log10((double)tx_lev) - SNR;
            txg_dB = 10*log10((double)tx_lev) - (SNR + sigma2_dB);
            txg = pow(10.0,-.05*txg_dB);
            if (n_frames==1)
                printf("sigma2_dB %f (SNR %f dB) tx_lev_dB %f, txg %f\n",sigma2_dB,SNR,10*log10((double)tx_lev)-txg_dB,txg_dB);
            //AWGN
            sigma2 = pow(10,sigma2_dB/10);
            //      printf("Sigma2 %f (sigma2_dB %f)\n",sigma2,sigma2_dB);

            //          sigma2 = 0;

            multipath_channel(ch,s_re,s_im,r_re,r_im,
                              sdu_length_samples,0);

            if (n_frames==1) {
                printf("rx_level data symbol %f, tx_lev %f\n",
                       10*log10(signal_energy_fp(r_re,r_im,1,80,0)),
                       10*log10(tx_lev));
            }

            for (aa=0; aa<n_rx; aa++) {
                for (i=0; i<(sdu_length_samples+100); i++) {


                    ((short*)&rxdata[aa][tx_offset])[(i<<1)]   = (short) (((txg*r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
                    ((short*)&rxdata[aa][tx_offset])[1+(i<<1)] = (short) (((txg*r_im[aa][i]) + (iqim*r_re[aa][i]*txg) + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));

                    //	  if (i<128)
                    //	    printf("i%d : rxdata %d, txdata %d\n",i,((short *)rxdata[aa])[rx_offset+(i<<1)],((short *)txdata)[i<<1]);
                }

                for (i=0; i<tx_offset; i++) {
                    ((short*) rxdata[aa])[(i<<1)]   = (short) (sqrt(sigma2/2)*gaussdouble(0.0,1.0));
                    ((short*) rxdata[aa])[1+(i<<1)] = (short) (sqrt(sigma2/2)*gaussdouble(0.0,1.0));
                }
                for (i=(tx_offset+sdu_length_samples+100); i<FRAME_LENGTH_SAMPLES_MAX; i++) {
                    ((short*) rxdata[aa])[(i<<1)]   = (short) (sqrt(sigma2/2)*gaussdouble(0.0,1.0));
                    ((short*) rxdata[aa])[1+(i<<1)] = (short) (sqrt(sigma2/2)*gaussdouble(0.0,1.0));
                }

            }
            if (n_frames==1) {
                write_output("rxsig0.m","rxs", &rxdata[0][0],FRAME_LENGTH_SAMPLES_MAX,1,1);
            }
            no_detection=1;
            off = 0;
            while(off<FRAME_LENGTH_SAMPLES_MAX) {

                rxp = dB_fixed(signal_energy(rxdata[0]+off,512));
                if (n_frames==1)
                    printf("off %d: rxp %d (%d)\n",off,rxp,signal_energy(rxdata[0]+off,104));

                if (rxp>RX_THRES_dB) {
                    if (off<105)
                        off2 = FRAME_LENGTH_SAMPLES_MAX-105;
                    else
                        off2=off;
                    if ((initial_sync(&rxv,&rx_offset,&log2_maxh,(uint32_t*)rxdata[0],FRAME_LENGTH_SAMPLES_MAX,off2,1) == BUSY)) {
                        if (n_frames==1)
                            printf("Channel is busy, rxv %p, offset %d\n",(void*)rxv,rx_offset);
                        no_detection=0;
                        if (rxv) {
                            if (n_frames==1)
                                printf("Rate %d, SDU_LENGTH %d\n",rxv->rate,rxv->sdu_length);
                            if ( (rxv->rate != tx_vector.rate)||(rxv->sdu_length != tx_vector.sdu_length)) {
                                signal_errors++;
                                if ((signal_errors > (n_frames/10)) && (trial>=100)) {
                                    stop=1;
                                }
                                if (n_frames == 1)
                                    printf("SIGNAL error: rx_offset %d, tx_offset %d (off2 %d)\n",rx_offset,tx_offset,off2);
                                break;
                            }
                            else {
                                memset(data_ind_rx,0,rxv->sdu_length+4+2+1);
                                if (data_detection(rxv,data_ind_rx,(uint32_t*)rxdata[0],FRAME_LENGTH_SAMPLES_MAX,rx_offset,log2_maxh,NULL)) {
                                    for (i=0; i<rxv->sdu_length+6; i++) {
                                        if (data_ind[i]!=data_ind_rx[i]) {
                                            //printf("error position %d : %x,%x\n",i,data_ind[i],data_ind_rx[i]);
                                            misdetected_errors++;
                                            errors++;
                                        }
                                    }
                                    if ((errors > (n_frames/10)) && (trial>100)) {
                                        stop=1;
                                        break;
                                    }
                                } // initial_synch returns IDLE
                                else {
                                    errors++;
                                    if (n_frames == 1) {
                                        printf("Running data_detection fails\n");

                                        for (i=0; i<rxv->sdu_length+6; i++) {
                                            if (data_ind[i]!=data_ind_rx[i]) {
                                                printf("error position %d : %x,%x\n",i,data_ind[i],data_ind_rx[i]);
                                            }
                                        }
                                    }
                                    if ((errors > (n_frames/10)) && (trial>=100)) {
                                        stop=1;
                                        break;
                                    }

                                }
                                break;
                            }
                        }
                    }
                }

                off+=105;
            }
            if (no_detection==1)
                missed_packets++;
            if (stop==1)
                break;
        }

        printf("\nSNR %f dB: errors %d/%d, misdetected errors %d/%d,signal_errors %d/%d, missed_packets %d/%d\n",SNR,errors,trial-signal_errors,misdetected_errors,trial-signal_errors,signal_errors,trial,missed_packets,trial);
        snr_array[cnt] = SNR;
        errors_array[cnt] = errors;
        trials_array[cnt] = trial;
        misdetected_errors_array[cnt] = misdetected_errors;
        signal_errors_array[cnt] = signal_errors;
        missed_packets_array[cnt] = missed_packets;
        cnt++;
        if (cnt>99) {
            printf("too many SNR points, exiting ...\n");
            break;
        }
        if (errors == 0)
            break;
#ifdef EXECTIME
        print_is_stats();
        print_dd_stats();
#endif
    }


    sprintf(fname,"SNR_%d_%d.m",tx_vector.rate,tx_vector.sdu_length);
    sprintf(vname,"SNR_%d_%d_v",tx_vector.rate,tx_vector.sdu_length);
    write_output(fname,vname,snr_array,cnt,1,7);
    sprintf(fname,"errors_%d_%d.m",tx_vector.rate,tx_vector.sdu_length);
    sprintf(vname,"errors_%d_%d_v",tx_vector.rate,tx_vector.sdu_length);
    write_output(fname,vname,errors_array,cnt,1,2);
    sprintf(fname,"trials_%d_%d.m",tx_vector.rate,tx_vector.sdu_length);
    sprintf(vname,"trials_%d_%d_v",tx_vector.rate,tx_vector.sdu_length);
    write_output(fname,vname,trials_array,cnt,1,2);
    sprintf(fname,"signal_errors_%d_%d.m",tx_vector.rate,tx_vector.sdu_length);
    sprintf(vname,"signal_errors_%d_%d_v",tx_vector.rate,tx_vector.sdu_length);
    write_output(fname,vname,signal_errors_array,cnt,1,2);
    free(data_ind);
    free(data_ind_rx);
    //  free_channel_desc_scm(ch);

    free(txdata);
    for (i=0; i<n_rx; i++) {
        free(rxdata[i]);
    }

    free(s_re[0]);
    free(s_im[0]);

    for (i=0; i<n_rx; i++) {
        free(r_re[i]);
        free(r_im[i]);
    }

    return(0);

}
Esempio n. 4
0
int main(int argc, char **argv) {

  char c;

  int i,aa,aarx;
  double sigma2, sigma2_dB=0,SNR,snr0=-2.0,snr1=0.0,ue_speed0=0.0,ue_speed1=0.0;
  uint8_t snr1set=0;
  uint8_t ue_speed1set=0;
  //mod_sym_t **txdataF;
#ifdef IFFT_FPGA
  int **txdataF2;
#endif
  int **txdata;
  double **s_re,**s_im,**r_re,**r_im;
  double iqim=0.0;
  int trial, ntrials=1;
  uint8_t transmission_mode = 1,n_tx=1,n_rx=1;
  uint16_t Nid_cell=0;

  uint8_t awgn_flag=0;
  uint8_t hs_flag=0;
  int n_frames=1;
  channel_desc_t *UE2eNB;
  uint32_t nsymb,tx_lev,tx_lev_dB;
  uint8_t extended_prefix_flag=0;
  //  int8_t interf1=-19,interf2=-19;
  LTE_DL_FRAME_PARMS *frame_parms;
#ifdef EMOS
  fifo_dump_emos emos_dump;
#endif


  SCM_t channel_model=Rayleigh1;

  //  uint8_t abstraction_flag=0,calibration_flag=0;
  //  double prach_sinr;
  uint8_t osf=1,N_RB_DL=25;
  uint32_t prach_errors=0;
  uint8_t subframe=3;
  uint16_t preamble_energy_list[64],preamble_tx=99,preamble_delay_list[64];
  uint16_t preamble_max,preamble_energy_max;
  PRACH_RESOURCES_t prach_resources;
  uint8_t prach_fmt;
  int N_ZC;
  int delay = 0;
  double delay_avg=0;
  double ue_speed = 0;
  int NCS_config = 1,rootSequenceIndex=0;
  logInit();

  number_of_cards = 1;
  openair_daq_vars.rx_rf_mode = 1;
  
  /*
    rxdataF    = (int **)malloc16(2*sizeof(int*));
    rxdataF[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
    rxdataF[1] = (int *)malloc16(FRAME_LENGTH_BYTES);
    
    rxdata    = (int **)malloc16(2*sizeof(int*));
    rxdata[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
    rxdata[1] = (int *)malloc16(FRAME_LENGTH_BYTES);
  */
  while ((c = getopt (argc, argv, "hHaA:Cr:p:g:n:s:S:t:x:y:v:V:z:N:F:d:Z:L:R:")) != -1)
    {
      switch (c)
	{
	case 'a':
	  printf("Running AWGN simulation\n");
	  awgn_flag = 1;
	  ntrials=1;
	  break;
	case 'd':
	  delay = atoi(optarg);
	  break;
	case 'g':
	  switch((char)*optarg) {
	  case 'A': 
	    channel_model=SCM_A;
	    break;
	  case 'B': 
	    channel_model=SCM_B;
	    break;
	  case 'C': 
	    channel_model=SCM_C;
	    break;
	  case 'D': 
	    channel_model=SCM_D;
	    break;
	  case 'E': 
	    channel_model=EPA;
	    break;
	  case 'F': 
	    channel_model=EVA;
	    break;
	  case 'G': 
	    channel_model=ETU;
	    break;
	  case 'H':
	    channel_model=Rayleigh8;
	  case 'I':
	    channel_model=Rayleigh1;
	  case 'J':
	    channel_model=Rayleigh1_corr;
	  case 'K':
	    channel_model=Rayleigh1_anticorr;
	  case 'L':
	    channel_model=Rice8;
	  case 'M':
	    channel_model=Rice1;
	  case 'N':
	    channel_model=Rayleigh1_800;
	  break;
	  default:
	    msg("Unsupported channel model!\n");
	    exit(-1);
	  }
	break;
	case 'n':
	  n_frames = atoi(optarg);
	  break;
	case 's':
	  snr0 = atof(optarg);
	  msg("Setting SNR0 to %f\n",snr0);
	  break;
	case 'S':
	  snr1 = atof(optarg);
	  snr1set=1;
	  msg("Setting SNR1 to %f\n",snr1);
	  break;
	case 'p':
	  preamble_tx=atoi(optarg);
	  break;
	case 'v':
	  ue_speed0 = atoi(optarg);
	  break;
	case 'V':
	  ue_speed1 = atoi(optarg);
      ue_speed1set = 1;
	  break;
	case 'Z':
	  NCS_config = atoi(optarg);
	  if ((NCS_config > 15) || (NCS_config < 0))
	    printf("Illegal NCS_config %d, (should be 0-15)\n",NCS_config);
	  break;
	case 'H':
	  printf("High-Speed Flag enabled\n");
	  hs_flag = 1;
	  break;
	case 'L':
	  rootSequenceIndex = atoi(optarg);
	  if ((rootSequenceIndex < 0) || (rootSequenceIndex > 837))
	    printf("Illegal rootSequenceNumber %d, (should be 0-837)\n",rootSequenceIndex);
	  break;
	case 'x':
	  transmission_mode=atoi(optarg);
	  if ((transmission_mode!=1) &&
	      (transmission_mode!=2) &&
	      (transmission_mode!=6)) {
	    msg("Unsupported transmission mode %d\n",transmission_mode);
	    exit(-1);
	  }
	  break;
	case 'y':
	  n_tx=atoi(optarg);
	  if ((n_tx==0) || (n_tx>2)) {
	    msg("Unsupported number of tx antennas %d\n",n_tx);
	    exit(-1);
	  }
	  break;
	case 'z':
	  n_rx=atoi(optarg);
	  if ((n_rx==0) || (n_rx>2)) {
	    msg("Unsupported number of rx antennas %d\n",n_rx);
	    exit(-1);
	  }
	  break;
	case 'N':
	  Nid_cell = atoi(optarg);
	  break;
	case 'R':
	  N_RB_DL = atoi(optarg);
	  break;
	case 'O':
	  osf = atoi(optarg);
	  break;
	case 'F':
	  break;
	default:
	case 'h':
	  printf("%s -h(elp) -a(wgn on) -p(extended_prefix) -N cell_id -f output_filename -F input_filename -g channel_model -n n_frames -s snr0 -S snr1 -x transmission_mode -y TXant -z RXant -i Intefrence0 -j Interference1 -A interpolation_file -C(alibration offset dB) -N CellId\n",argv[0]);
	  printf("-h This message\n");
	  printf("-a Use AWGN channel and not multipath\n");
	  printf("-n Number of frames to simulate\n");
	  printf("-s Starting SNR, runs from SNR0 to SNR0 + 5 dB.  If n_frames is 1 then just SNR is simulated\n");
	  printf("-S Ending SNR, runs from SNR0 to SNR1\n");
	  printf("-g [A,B,C,D,E,F,G,I,N] Use 3GPP SCM (A,B,C,D) or 36-101 (E-EPA,F-EVA,G-ETU) or Rayleigh1 (I) or Rayleigh1_800 (N) models (ignores delay spread and Ricean factor)\n");
	  printf("-z Number of RX antennas used in eNB\n");
	  printf("-N Nid_cell\n");
	  printf("-O oversampling factor (1,2,4,8,16)\n");
      //	  printf("-f PRACH format (0=1,1=2,2=3,3=4)\n");
	  printf("-d Channel delay \n");
	  printf("-v Starting UE velocity in km/h, runs from 'v' to 'v+50km/h'. If n_frames is 1 just 'v' is simulated \n");
	  printf("-V Ending UE velocity in km/h, runs from 'v' to 'V'");
	  printf("-L rootSequenceIndex (0-837)\n");
	  printf("-Z NCS_config (ZeroCorrelationZone) (0-15)\n");
	  printf("-H Run with High-Speed Flag enabled \n");
	  printf("-R Number of PRB (6,15,25,50,75,100)\n");
	  printf("-F Input filename (.txt format) for RX conformance testing\n");
	  exit (-1);
	  break;
	}
    }

  if (transmission_mode==2)
    n_tx=2;

  lte_param_init(n_tx,n_rx,transmission_mode,extended_prefix_flag,Nid_cell,N_RB_DL,osf);


  if (snr1set==0) {
    if (n_frames==1)
      snr1 = snr0+.1;
    else
      snr1 = snr0+5.0;
  }

  if (ue_speed1set==0) {
    if (n_frames==1)
      ue_speed1 = ue_speed0+10;
    else
      ue_speed1 = ue_speed0+50;
  }

  printf("SNR0 %f, SNR1 %f\n",snr0,snr1);

  frame_parms = &PHY_vars_eNB->lte_frame_parms;


  txdata = PHY_vars_UE->lte_ue_common_vars.txdata;
  printf("txdata %p\n",&txdata[0][subframe*frame_parms->samples_per_tti]);
  
  s_re = malloc(2*sizeof(double*));
  s_im = malloc(2*sizeof(double*));
  r_re = malloc(2*sizeof(double*));
  r_im = malloc(2*sizeof(double*));
  nsymb = (frame_parms->Ncp == 0) ? 14 : 12;

  printf("FFT Size %d, Extended Prefix %d, Samples per subframe %d, Symbols per subframe %d\n",NUMBER_OF_OFDM_CARRIERS,
	 frame_parms->Ncp,frame_parms->samples_per_tti,nsymb);


  
  msg("[SIM] Using SCM/101\n");
  UE2eNB = new_channel_desc_scm(PHY_vars_UE->lte_frame_parms.nb_antennas_tx,
				PHY_vars_eNB->lte_frame_parms.nb_antennas_rx,
				channel_model,
				BW,
				0.0,
				delay,
				0);

  if (UE2eNB==NULL) {
    msg("Problem generating channel model. Exiting.\n");
    exit(-1);
  }

  for (i=0;i<2;i++) {

    s_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));

    r_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
  }
 
  PHY_vars_UE->lte_frame_parms.prach_config_common.rootSequenceIndex=rootSequenceIndex; 
  PHY_vars_UE->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex=0; 
  PHY_vars_UE->lte_frame_parms.prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig=NCS_config;
  PHY_vars_UE->lte_frame_parms.prach_config_common.prach_ConfigInfo.highSpeedFlag=hs_flag;
  PHY_vars_UE->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset=0;


  PHY_vars_eNB->lte_frame_parms.prach_config_common.rootSequenceIndex=rootSequenceIndex; 
  PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex=0; 
  PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig=NCS_config;
  PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.highSpeedFlag=hs_flag;
  PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset=0;

  prach_fmt = get_prach_fmt(PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex,
			    PHY_vars_eNB->lte_frame_parms.frame_type);
  N_ZC = (prach_fmt <4)?839:139;
  
  compute_prach_seq(&PHY_vars_eNB->lte_frame_parms.prach_config_common,PHY_vars_eNB->lte_frame_parms.frame_type,PHY_vars_eNB->X_u);

  compute_prach_seq(&PHY_vars_UE->lte_frame_parms.prach_config_common,PHY_vars_UE->lte_frame_parms.frame_type,PHY_vars_UE->X_u);

  PHY_vars_UE->lte_ue_prach_vars[0]->amp = AMP;

  PHY_vars_UE->prach_resources[0] = &prach_resources;
  if (preamble_tx == 99)
    preamble_tx = (uint16_t)(taus()&0x3f);
  if (n_frames == 1)
     printf("raPreamble %d\n",preamble_tx);

  PHY_vars_UE->prach_resources[0]->ra_PreambleIndex = preamble_tx;
  PHY_vars_UE->prach_resources[0]->ra_TDD_map_index = 0;

  tx_lev = generate_prach(PHY_vars_UE,
			  0, //eNB_id,
			  subframe, 
			  0); //Nf

  tx_lev_dB = (unsigned int) dB_fixed(tx_lev);
    
  write_output("txsig0_new.m","txs0", &txdata[0][subframe*frame_parms->samples_per_tti],frame_parms->samples_per_tti,1,1);
    //write_output("txsig1.m","txs1", txdata[1],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);

    // multipath channel
  dump_prach_config(&PHY_vars_eNB->lte_frame_parms,subframe);

  for (i=0;i<2*frame_parms->samples_per_tti;i++) {
    for (aa=0;aa<1;aa++) {
      if (awgn_flag == 0) {
	s_re[aa][i] = ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)]);
	s_im[aa][i] = ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)+1]);
      }
      else {
	for (aarx=0;aarx<PHY_vars_eNB->lte_frame_parms.nb_antennas_rx;aarx++) {
	  if (aa==0) {
	    r_re[aarx][i] = ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)]);
	    r_im[aarx][i] = ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)+1]);
	  }
	  else {
	    r_re[aarx][i] += ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)]);
	    r_im[aarx][i] += ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)+1]);
	  }
	}
      }
    }
  }



  for (SNR=snr0;SNR<snr1;SNR+=.2) {
      for (ue_speed=ue_speed0;ue_speed<ue_speed1;ue_speed+=10) {
    delay_avg = 0.0;
    // max Doppler shift
    UE2eNB->max_Doppler = 1.9076e9*(ue_speed/3.6)/3e8;
    printf("n_frames %d SNR %f\n",n_frames,SNR);
    prach_errors=0;
    for (trial=0; trial<n_frames; trial++) {
      
      sigma2_dB = 10*log10((double)tx_lev) - SNR;
      if (n_frames==1)
	printf("sigma2_dB %f (SNR %f dB) tx_lev_dB %f\n",sigma2_dB,SNR,10*log10((double)tx_lev));
      //AWGN
      sigma2 = pow(10,sigma2_dB/10);
      //	printf("Sigma2 %f (sigma2_dB %f)\n",sigma2,sigma2_dB);
            

      if (awgn_flag == 0) {
	multipath_tv_channel(UE2eNB,s_re,s_im,r_re,r_im,
			  2*frame_parms->samples_per_tti,0);
      }
      if (n_frames==1) {
	printf("rx_level data symbol %f, tx_lev %f\n",
	       10*log10(signal_energy_fp(r_re,r_im,1,OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES,0)),
	       10*log10(tx_lev));
      }

      for (i=0; i<frame_parms->samples_per_tti; i++) {
	for (aa=0;aa<PHY_vars_eNB->lte_frame_parms.nb_antennas_rx;aa++) {
	
	  ((short*) &PHY_vars_eNB->lte_eNB_common_vars.rxdata[0][aa][subframe*frame_parms->samples_per_tti])[2*i] = (short) (.167*(r_re[aa][i] +sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
	  ((short*) &PHY_vars_eNB->lte_eNB_common_vars.rxdata[0][aa][subframe*frame_parms->samples_per_tti])[2*i+1] = (short) (.167*(r_im[aa][i] + (iqim*r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
	}
      }
	
      rx_prach(PHY_vars_eNB,
	       subframe,
	       preamble_energy_list,
	       preamble_delay_list,
	       0,   //Nf
	       0);    //tdd_mapindex

      preamble_energy_max = preamble_energy_list[0];
      preamble_max = 0;
      for (i=1;i<64;i++) {
	if (preamble_energy_max < preamble_energy_list[i]) {
	  //	  printf("preamble %d => %d\n",i,preamble_energy_list[i]);
	  preamble_energy_max = preamble_energy_list[i];
	  preamble_max = i;
	}
      }
      if (preamble_max!=preamble_tx)
	prach_errors++;
      else {
	delay_avg += (double)preamble_delay_list[preamble_max];
      }
      if (n_frames==1) {
	for (i=0;i<64;i++)
	  if (i==preamble_tx)
	    printf("****** preamble %d : energy %d, delay %d\n",i,preamble_energy_list[i],preamble_delay_list[i]);
	  else
	    printf("preamble %d : energy %d, delay %d\n",i,preamble_energy_list[i],preamble_delay_list[i]);
	write_output("prach0.m","prach0", &txdata[0][subframe*frame_parms->samples_per_tti],frame_parms->samples_per_tti,1,1);
	write_output("prachF0.m","prachF0", &PHY_vars_eNB->lte_eNB_prach_vars.prachF[0],24576,1,1);
	write_output("rxsig0.m","rxs0", 
		     &PHY_vars_eNB->lte_eNB_common_vars.rxdata[0][0][subframe*frame_parms->samples_per_tti],
		     frame_parms->samples_per_tti,1,1);
	write_output("rxsigF0.m","rxsF0", &PHY_vars_eNB->lte_eNB_common_vars.rxdataF[0][0][0],512*nsymb*2,2,1);
	write_output("prach_preamble.m","prachp",&PHY_vars_eNB->X_u[0],839,1,1);
      }
    }
    printf("SNR %f dB, UE Speed %f km/h: errors %d/%d (delay %f)\n",SNR,ue_speed,prach_errors,n_frames,delay_avg/(double)(n_frames-prach_errors));
    //printf("(%f,%f)\n",ue_speed,(double)prach_errors/(double)n_frames);
  } // UE Speed loop
      //printf("SNR %f dB, UE Speed %f km/h: errors %d/%d (delay %f)\n",SNR,ue_speed,prach_errors,n_frames,delay_avg/(double)(n_frames-prach_errors));
      //  printf("(%f,%f)\n",SNR,(double)prach_errors/(double)n_frames);
} //SNR loop
#ifdef IFFT_FPGA
  free(txdataF2[0]);
  free(txdataF2[1]);
  free(txdataF2);
  free(txdata[0]);
  free(txdata[1]);
  free(txdata);
#endif 

  for (i=0;i<2;i++) {
    free(s_re[i]);
    free(s_im[i]);
    free(r_re[i]);
    free(r_im[i]);
  }
  free(s_re);
  free(s_im);
  free(r_re);
  free(r_im);
  
  lte_sync_time_free();

  return(0);

}
Esempio n. 5
0
void lte_eNB_I0_measurements(PHY_VARS_eNB *phy_vars_eNb,
                             unsigned char eNB_id,
                             unsigned char clear) {
    LTE_eNB_COMMON *eNB_common_vars = &phy_vars_eNb->lte_eNB_common_vars;
    LTE_DL_FRAME_PARMS *frame_parms = &phy_vars_eNb->lte_frame_parms;
    PHY_MEASUREMENTS_eNB *phy_measurements = &phy_vars_eNb->PHY_measurements_eNB[eNB_id];


    unsigned int aarx,rx_power_correction;
    unsigned int rb;
    int *ul_ch;
    int n0_power_tot;
    int i;

    // noise measurements
    // for the moment we measure the noise on the 7th OFDM symbol (in S subframe)

    phy_measurements->n0_power_tot = 0;

    /*  printf("rxdataF0 %p, rxdataF1 %p\n",
     (&eNB_common_vars->rxdataF[0][0][(frame_parms->ofdm_symbol_size + frame_parms->first_carrier_offset)<<1 ]),
     (&eNB_common_vars->rxdataF[0][1][(frame_parms->ofdm_symbol_size + frame_parms->first_carrier_offset)<<1 ]));
    */
    /*
    for (i=0;i<512;i++)
      printf("sector 0 antenna 0 : %d,%d\n",((short *)&eNB_common_vars->rxdataF[0][0][(19*frame_parms->ofdm_symbol_size)<<1])[i<<1],
       ((short *)&eNB_common_vars->rxdataF[0][0][(19*frame_parms->ofdm_symbol_size)<<1])[1+(i<<1)]);

    for (i=0;i<12;i++)
      //    printf("sector 0 antenna 1 : %d,%d\n",((short *)&eNB_common_vars->rxdataF[0][1][(19*frame_parms->ofdm_symbol_size)<<1])[i<<1],
       ((short *)&eNB_common_vars->rxdataF[0][1][(19*frame_parms->ofdm_symbol_size)<<1])[1+(i<<1)]);
    */

    if ( (frame_parms->ofdm_symbol_size == 128) ||
            (frame_parms->ofdm_symbol_size == 512) )
        rx_power_correction = 2;
    else
        rx_power_correction = 1;

    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
        if (clear == 1)
            phy_measurements->n0_power[aarx]=0;
#ifdef USER_MODE
        phy_measurements->n0_power[aarx] = ((k1*signal_energy(&eNB_common_vars->rxdata[eNB_id][aarx][19*(frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples)],frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples)) + k2*phy_measurements->n0_power[aarx])>>10;
#else
        phy_measurements->n0_power[aarx] = ((k1*signal_energy(&eNB_common_vars->rxdata[eNB_id][aarx][19*frame_parms->ofdm_symbol_size],frame_parms->ofdm_symbol_size))+k2*phy_measurements->n0_power[aarx])>>10;
#endif
        phy_measurements->n0_power_dB[aarx] = (unsigned short) dB_fixed(phy_measurements->n0_power[aarx]);
        phy_measurements->n0_power_tot +=  phy_measurements->n0_power[aarx];
    }

    phy_measurements->n0_power_tot_dB = (unsigned short) dB_fixed(phy_measurements->n0_power_tot);

    phy_measurements->n0_power_tot_dBm = phy_measurements->n0_power_tot_dB - phy_vars_eNb->rx_total_gain_eNB_dB;
    //    printf("n0_power %d\n",phy_measurements->n0_avg_power_dB);


    for (rb=0; rb<frame_parms->N_RB_UL; rb++) {

        n0_power_tot=0;
        for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {


            if (rb < 12)
                //	ul_ch    = &eNB_common_vars->rxdataF[eNB_id][aarx][((19*(frame_parms->ofdm_symbol_size)) + frame_parms->first_carrier_offset + (rb*12))<<1];
                ul_ch    = &eNB_common_vars->rxdataF[eNB_id][aarx][((7*frame_parms->ofdm_symbol_size) + frame_parms->first_carrier_offset + (rb*12))<<1];
            else if (rb>12)
                ul_ch    = &eNB_common_vars->rxdataF[eNB_id][aarx][((7*frame_parms->ofdm_symbol_size) + 6 + (rb-13)*12)<<1];
            else {
                ul_ch = NULL;
            }

            if (clear == 1)
                phy_measurements->n0_subband_power[aarx][rb]=0;

            if (ul_ch) {
                //	for (i=0;i<24;i+=2)
                //	  printf("re %d => %d\n",i/2,ul_ch[i]);
                phy_measurements->n0_subband_power[aarx][rb] = ((k1*(signal_energy_nodc(ul_ch,24))*rx_power_correction) + (k2*phy_measurements->n0_subband_power[aarx][rb]))>>11;  // 11 and 24 to compensate for repeated signal format

                phy_measurements->n0_subband_power_dB[aarx][rb] = dB_fixed(phy_measurements->n0_subband_power[aarx][rb]);
                //	printf("eNb %d, aarx %d, rb %d : energy %d (%d dB)\n",eNB_id,aarx,rb,signal_energy_nodc(ul_ch,24),	phy_measurements->n0_subband_power_dB[aarx][rb]);
                n0_power_tot +=	phy_measurements->n0_subband_power[aarx][rb];
            }
            else {
                phy_measurements->n0_subband_power[aarx][rb] = 1;
                phy_measurements->n0_subband_power_dB[aarx][rb] = -99;
                n0_power_tot = 1;
            }
        }
        phy_measurements->n0_subband_power_tot_dB[rb] = dB_fixed(n0_power_tot);
        phy_measurements->n0_subband_power_tot_dBm[rb] = phy_measurements->n0_subband_power_tot_dB[rb] - phy_vars_eNb->rx_total_gain_eNB_dB - 14;

    }
Esempio n. 6
0
int main(int argc, char **argv)
{

  char c;

  int i,l,aa;
  double sigma2, sigma2_dB=0,SNR,snr0=-2.0,snr1;

  int **txdata;
  double s_re[2][30720*2],s_im[2][30720*2],r_re[2][30720*2],r_im[2][30720*2];
  double iqim=0.0;
  //  int subframe_offset;
  uint8_t subframe=0;
#ifdef XFORMS
  FD_lte_phy_scope_ue *form_ue;
  char title[255];
#endif
  int trial, n_errors_common=0,n_errors_ul=0,n_errors_dl=0,n_errors_cfi=0,n_errors_hi=0;
  unsigned char eNb_id = 0;

  uint8_t awgn_flag=0;
  int n_frames=1;
  channel_desc_t *eNB2UE;
  uint32_t nsymb,tx_lev,tx_lev_dB=0,num_pdcch_symbols=3;
  uint8_t extended_prefix_flag=0,transmission_mode=1,n_tx=1,n_rx=1;
  uint16_t Nid_cell=0;
  //  int8_t interf1=-128,interf2=-128;
  uint8_t dci_cnt=0;
  LTE_DL_FRAME_PARMS *frame_parms;
  uint8_t log2L=2, log2Lcommon=2;
  DCI_format_t format_selector[MAX_NUM_DCI];
  uint8_t num_dci=0;
  uint8_t numCCE,common_active=0,ul_active=0,dl_active=0;

  uint32_t n_trials_common=0,n_trials_ul=0,n_trials_dl=0,false_detection_cnt=0;
  uint8_t common_rx,ul_rx,dl_rx;
  uint8_t tdd_config=3;

  FILE *input_fd=NULL;
  char input_val_str[50],input_val_str2[50];
  uint16_t n_rnti=0x1234;
  uint8_t osf=1,N_RB_DL=25;

  SCM_t channel_model=Rayleigh1_anticorr;

  DCI_ALLOC_t dci_alloc_rx[8];

  int ret;

  uint8_t harq_pid;
  uint8_t phich_ACK;

  uint8_t num_phich_interf = 0;
  lte_frame_type_t frame_type=TDD;
  //  int re_offset;
  //  uint32_t *txptr;
  int aarx;
  int k;
  uint32_t perfect_ce = 0;
  int CCE_table[800];

  number_of_cards = 1;

  cpuf = get_cpu_freq_GHz();

  logInit();


  while ((c = getopt (argc, argv, "hapFg:R:c:n:s:x:y:z:L:M:N:I:f:i:S:P:Y")) != -1) {
    switch (c) {
    case 'a':
      printf("Running AWGN simulation\n");
      awgn_flag = 1;
      break;

    case 'R':
      N_RB_DL = atoi(optarg);
      break;

    case 'F':
      frame_type = FDD;
      break;

    case 'c':
      tdd_config=atoi(optarg);

      if (tdd_config>6) {
        printf("Illegal tdd_config %d (should be 0-6)\n",tdd_config);
        exit(-1);
      }

      break;

    case 'g':
      switch((char)*optarg) {
      case 'A':
        channel_model=SCM_A;
        break;

      case 'B':
        channel_model=SCM_B;
        break;

      case 'C':
        channel_model=SCM_C;
        break;

      case 'D':
        channel_model=SCM_D;
        break;

      case 'E':
        channel_model=EPA;
        break;

      case 'F':
        channel_model=EVA;
        break;

      case 'G':
        channel_model=ETU;
        break;

      default:
        printf("Unsupported channel model!\n");
        exit(-1);
      }

      break;

      /*
          case 'i':
      interf1=atoi(optarg);
      break;
          case 'j':
      interf2=atoi(optarg);
      break;
      */
    case 'n':
      n_frames = atoi(optarg);
      break;

    case 's':
      snr0 = atoi(optarg);
      break;

    case 'p':
      extended_prefix_flag=1;
      break;

    case 'x':
      transmission_mode=atoi(optarg);

      if ((transmission_mode!=1) &&
          (transmission_mode!=2) &&
          (transmission_mode!=6)) {
        printf("Unsupported transmission mode %d\n",transmission_mode);
        exit(-1);
      }

      break;

    case 'y':
      n_tx=atoi(optarg);

      if ((n_tx==0) || (n_tx>2)) {
        printf("Unsupported number of tx antennas %d\n",n_tx);
        exit(-1);
      }

      break;

    case 'z':
      n_rx=atoi(optarg);

      if ((n_rx==0) || (n_rx>2)) {
        printf("Unsupported number of rx antennas %d\n",n_rx);
        exit(-1);
      }

      break;

    case 'S':
      subframe=atoi(optarg);
      break;

    case 'L':
      log2L=atoi(optarg);

      if ((log2L!=0)&&
          (log2L!=1)&&
          (log2L!=2)&&
          (log2L!=3)) {
        printf("Unsupported DCI aggregation level %d (should be 0,1,2,3)\n",log2L);
        exit(-1);
      }

      break;

    case 'M':
      log2Lcommon=atoi(optarg);

      if ((log2Lcommon!=2)&&
          (log2Lcommon!=3)) {
        printf("Unsupported Common DCI aggregation level %d (should be 2 or 3)\n",log2Lcommon);
        exit(-1);
      }

      break;

    case 'N':
      format_selector[num_dci] = (DCI_format_t) atoi(optarg);
      if ((format_selector[num_dci]<format0) || (format_selector[num_dci] > format1A)) {
	printf("only formats 0, 1, and 1A supported for the moment\n");
	exit(-1);
      }
      if (format_selector[num_dci]==format0) ul_active=1;
      if (format_selector[num_dci]==format1A) common_active=1;
      if (format_selector[num_dci]==format1) dl_active=1;
      num_dci++;
      break;

    case 'O':
      osf = atoi(optarg);
      break;

    case 'I':
      Nid_cell = atoi(optarg);
      break;

    case 'f':
      input_fd = fopen(optarg,"r");

      if (input_fd==NULL) {
        printf("Problem with filename %s\n",optarg);
        exit(-1);
      }

      break;

    case 'i':
      n_rnti=atoi(optarg);
      break;

    case 'P':
      num_phich_interf=atoi(optarg);
      break;

    case 'Y':
      perfect_ce = 1;
      break;

    case 'h':
      printf("%s -h(elp) -a(wgn on) -c tdd_config -n n_frames -r RiceanFactor -s snr0 -t Delayspread -x transmission mode (1,2,6) -y TXant -z RXant -L AggregLevelUEspec -M AggregLevelCommonDCI -N DCIFormat\n\n",
             argv[0]);
      printf("-h This message\n");
      printf("-a Use AWGN channel and not multipath\n");
      printf("-c TDD config\n");
      printf("-S Subframe number (0..9)\n");
      printf("-R N_RB_DL\n");
      printf("-F use FDD frame\n");
      printf("-p Use extended prefix mode\n");
      printf("-n Number of frames to simulate\n");
      printf("-r Ricean factor (dB, 0 means Rayleigh, 100 is almost AWGN\n");
      printf("-s Starting SNR, runs from SNR to SNR + 5 dB.  If n_frames is 1 then just SNR is simulated\n");
      printf("-t Delay spread for multipath channel\n");
      printf("-x Transmission mode (1,2,6 for the moment)\n");
      printf("-y Number of TX antennas used in eNB\n");
      printf("-z Number of RX antennas used in UE\n");
      printf("-P Number of interfering PHICH\n");
      printf("-L log2 of Aggregation level for UE Specific DCI (0,1,2,3)\n");
      printf("-M log2 Aggregation level for Common DCI (4,8)\n");
      printf("-N Format for UE Spec DCI (0 - format0,\n");
      printf("                           1 - format1,\n");
      printf("                           2 - format1A,\n");
      printf("                           3 - format1B_2A,\n");
      printf("                           4 - format1B_4A,\n");
      printf("                           5 - format1C,\n");
      printf("                           6 - format1D_2A,\n");
      printf("                           7 - format1D_4A,\n");
      printf("                           8 - format2A_2A_L10PRB,\n");
      printf("                           9 - format2A_2A_M10PRB,\n");
      printf("                          10 - format2A_4A_L10PRB,\n");
      printf("                          11 - format2A_4A_M10PRB,\n");
      printf("                          12 - format2_2A_L10PRB,\n");
      printf("                          13 - format2_2A_M10PRB,\n");
      printf("                          14 - format2_4A_L10PRB,\n");
      printf("                          15 - format2_4A_M10PRB\n");
      printf("                          16 - format2_2D_M10PRB\n");
      printf("                          17 - format2_2D_L10PRB\n");
      printf("   can be called multiple times to add more than one DCI\n");
      printf("-O Oversampling factor\n");
      printf("-I Cell Id\n");
      printf("-F Input sample stream\n");
      exit(1);
      break;
    }
  }

  if ((transmission_mode>1) && (n_tx==1))
    n_tx=2;

  lte_param_init(n_tx,
                 n_tx,
                 n_rx,
                 transmission_mode,
                 extended_prefix_flag,
		 frame_type,
                 Nid_cell,
                 tdd_config,
                 N_RB_DL,
		 0,
                 osf,
                 perfect_ce);

#ifdef XFORMS
  fl_initialize (&argc, argv, NULL, 0, 0);
  form_ue = create_lte_phy_scope_ue();
  sprintf (title, "LTE PHY SCOPE UE");
  fl_show_form (form_ue->lte_phy_scope_ue, FL_PLACE_HOTSPOT, FL_FULLBORDER, title);
#endif


  mac_xface->computeRIV = computeRIV;
  mac_xface->frame_parms = &eNB->frame_parms;
  //  init_transport_channels(transmission_mode);

  if (n_frames==1)
    snr1 = snr0+.1;
  else
    snr1 = snr0+8.0;

  printf("SNR0 %f, SNR1 %f\n",snr0,snr1);

  frame_parms = &eNB->frame_parms;
  printf("Getting %d dcis\n",num_dci);

  get_dci(frame_parms, log2L, log2Lcommon, format_selector, num_dci, n_rnti);

  txdata = eNB->common_vars.txdata[eNb_id];

  nsymb = (eNB->frame_parms.Ncp == 0) ? 14 : 12;

  printf("Subframe %d, FFT Size %d, Extended Prefix %d, Samples per subframe %d, Symbols per subframe %d\n",
         subframe,NUMBER_OF_OFDM_CARRIERS,
         eNB->frame_parms.Ncp,eNB->frame_parms.samples_per_tti,nsymb);

  eNB2UE = new_channel_desc_scm(eNB->frame_parms.nb_antennas_tx,
                                UE->frame_parms.nb_antennas_rx,
                                channel_model,
				N_RB2sampling_rate(eNB->frame_parms.N_RB_DL),
				N_RB2channel_bandwidth(eNB->frame_parms.N_RB_DL),
                                0,
                                0,
                                0);

  eNB_rxtx_proc_t *proc_rxtx = &eNB->proc.proc_rxtx[subframe&1];

  eNB->ulsch[0] = new_eNB_ulsch(MAX_TURBO_ITERATIONS,N_RB_DL,0);
  UE->ulsch[0]   = new_ue_ulsch(N_RB_DL,0);


  proc_rxtx->frame_tx    = 0;
  proc_rxtx->subframe_tx = subframe;

  if (input_fd==NULL) {
    printf("No input file, so starting TX\n");
  } else {
    i=0;

    while (!feof(input_fd)) {
      ret=fscanf(input_fd,"%s %s",input_val_str,input_val_str2);//&input_val1,&input_val2);

      if (ret != 2) {
        printf("%s:%d:%s: fscanf error, exiting\n", __FILE__, __LINE__, __FUNCTION__);
        exit(1);
      }

      if ((i%4)==0) {
        ((short*)txdata[0])[i/2] = (short)((1<<15)*strtod(input_val_str,NULL));
        ((short*)txdata[0])[(i/2)+1] = (short)((1<<15)*strtod(input_val_str2,NULL));

        if ((i/4)<100)
          printf("sample %d => %e + j%e (%d +j%d)\n",i/4,strtod(input_val_str,NULL),strtod(input_val_str2,NULL),((short*)txdata[0])[i/4],((short*)txdata[0])[(i/4)+1]);//1,input_val2,);
      }

      i++;

      if (i>(4*FRAME_LENGTH_SAMPLES))
        break;
    }

    printf("Read in %d samples\n",i/4);
    write_output("txsig0.m","txs0", txdata[0],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);
    //    write_output("txsig1.m","txs1", txdata[1],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);
    tx_lev = signal_energy(&txdata[0][0],
                           OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES);
    tx_lev_dB = (unsigned int) dB_fixed(tx_lev);
  }


  UE->UE_mode[0] = PUSCH;

  //  nCCE_max = get_nCCE(3,&eNB->frame_parms,get_mi(&eNB->frame_parms,0));
  //printf("nCCE_max %d\n",nCCE_max);

  //printf("num_phich interferers %d\n",num_phich_interf);
  for (SNR=snr0; SNR<snr1; SNR+=0.2) {


    n_errors_common = 0;
    n_errors_ul     = 0;
    n_errors_dl     = 0;
    n_errors_cfi    = 0;
    n_errors_hi     = 0;
    n_trials_common=0;
    n_trials_ul=0;
    n_trials_dl=0;

    for (trial=0; trial<n_frames; trial++) {
      
      //    printf("DCI (SF %d): txdataF %p (0 %p)\n",subframe,&eNB->common_vars.txdataF[eNb_id][aa][512*14*subframe],&eNB->common_vars.txdataF[eNb_id][aa][0]);
      for (aa=0; aa<eNB->frame_parms.nb_antennas_tx; aa++) {
        memset(&eNB->common_vars.txdataF[eNb_id][aa][0],0,FRAME_LENGTH_COMPLEX_SAMPLES_NO_PREFIX*sizeof(int32_t));

      }


      generate_pilots_slot(eNB,
                           eNB->common_vars.txdataF[eNb_id],
                           AMP,    //1024,
                           (subframe*2),
                           0);
      generate_pilots_slot(eNB,
                           eNB->common_vars.txdataF[eNb_id],
                           AMP,    //1024,
                           (subframe*2)+1,
                           0);


      if (input_fd == NULL) {
        numCCE=0;
        n_trials_common++;
        common_active = 1;
	if (eNB->frame_parms.N_RB_DL >= 50) { 
	  if (ul_active==1) { 
	    n_trials_ul++;
	  }
	}
        if (eNB->frame_parms.N_RB_DL >= 25) { 
	  if (dl_active==1) { 
	    n_trials_dl++;
	  }
	}
        num_pdcch_symbols = get_num_pdcch_symbols(DCI_pdu.Num_dci,
                            DCI_pdu.dci_alloc, frame_parms, subframe);
	numCCE = get_nCCE(num_pdcch_symbols,&eNB->frame_parms,get_mi(&eNB->frame_parms,subframe));

        if (n_frames==1) {
          printf("num_dci %d, num_pddch_symbols %d, nCCE %d\n",
                 DCI_pdu.Num_dci,
                 num_pdcch_symbols,numCCE);
        }

        // apply RNTI-based nCCE allocation
	memset(CCE_table,0,800*sizeof(int));

        for (i = 0; i < DCI_pdu.Num_dci; i++) {
          // SI RNTI
          if (DCI_pdu.dci_alloc[i].rnti == SI_RNTI) {
            DCI_pdu.dci_alloc[i].firstCCE = get_nCCE_offset_l1(CCE_table,
							       1<<DCI_pdu.dci_alloc[i].L,
							       numCCE,
							       1,
							       SI_RNTI,
							       subframe);
          }
          // RA RNTI
          else if (DCI_pdu.dci_alloc[i].ra_flag == 1) {
            DCI_pdu.dci_alloc[i].firstCCE = get_nCCE_offset_l1(CCE_table,
							       1<<DCI_pdu.dci_alloc[i].L,
							       numCCE,
							       1,
							       DCI_pdu.dci_alloc[i].rnti,
							       subframe);
          }
          // C RNTI
          else {
            DCI_pdu.dci_alloc[i].firstCCE = get_nCCE_offset_l1(CCE_table,
							       1<<DCI_pdu.dci_alloc[i].L,
							       numCCE,
							       0,
							       DCI_pdu.dci_alloc[i].rnti,
							       subframe);
          }

          if (n_frames==1)
            printf("dci %d: rnti 0x%x, format %d, L %d (aggreg %d), nCCE %d/%d dci_length %d\n",i,DCI_pdu.dci_alloc[i].rnti, DCI_pdu.dci_alloc[i].format,
                   DCI_pdu.dci_alloc[i].L, 1<<DCI_pdu.dci_alloc[i].L, DCI_pdu.dci_alloc[i].firstCCE, numCCE, DCI_pdu.dci_alloc[i].dci_length);

          if (DCI_pdu.dci_alloc[i].firstCCE==-1)
            exit(-1);
        }

        num_pdcch_symbols = generate_dci_top(DCI_pdu.Num_dci,
                                             DCI_pdu.dci_alloc,
                                             0,
                                             AMP,
                                             &eNB->frame_parms,
                                             eNB->common_vars.txdataF[eNb_id],
                                             subframe);

        if (n_frames==1)
          printf("num_pdcch_symbols at TX %d\n",num_pdcch_symbols);

        if (is_phich_subframe(&eNB->frame_parms,subframe)) {
          if (n_frames==1)
            printf("generating PHICH\n");

          harq_pid = phich_subframe_to_harq_pid(&eNB->frame_parms, proc_rxtx->frame_tx, subframe);

          phich_ACK = taus()&1;
          eNB->ulsch[0]->harq_processes[harq_pid]->phich_active = 1;
          eNB->ulsch[0]->harq_processes[harq_pid]->first_rb     = 0;
          eNB->ulsch[0]->harq_processes[harq_pid]->n_DMRS       = 0;
          eNB->ulsch[0]->harq_processes[harq_pid]->phich_ACK    = phich_ACK;
          eNB->ulsch[0]->harq_processes[harq_pid]->dci_alloc    = 1;

          UE->ulsch[0]->harq_processes[harq_pid]->first_rb       = 0;
          UE->ulsch[0]->harq_processes[harq_pid]->n_DMRS         = 0;

          generate_phich_top(eNB,proc_rxtx,AMP,0);
          
          // generate 3 interfering PHICH
          if (num_phich_interf>0) {
            eNB->ulsch[0]->harq_processes[harq_pid]->first_rb = 4;
            generate_phich_top(eNB,proc_rxtx,1024,0);
          }

          if (num_phich_interf>1) {
            eNB->ulsch[0]->harq_processes[harq_pid]->first_rb = 8;
            eNB->ulsch[0]->harq_processes[harq_pid]->n_DMRS = 1;
            generate_phich_top(eNB,proc_rxtx,1024,0);
          }
          if (num_phich_interf>2) {
            eNB->ulsch[0]->harq_processes[harq_pid]->first_rb = 12;
            eNB->ulsch[0]->harq_processes[harq_pid]->n_DMRS = 1;
            generate_phich_top(eNB,proc_rxtx,1024,0);

          }

          eNB->ulsch[0]->harq_processes[harq_pid]->first_rb = 0;
          
        }

        //  write_output("pilotsF.m","rsF",txdataF[0],lte_eNB->frame_parms.ofdm_symbol_size,1,1);

        if (n_frames==1) {
          write_output("txsigF0.m","txsF0", eNB->common_vars.txdataF[eNb_id][0],4*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES_NO_PREFIX,1,1);

          if (eNB->frame_parms.nb_antenna_ports_eNB > 1)
            write_output("txsigF1.m","txsF1", eNB->common_vars.txdataF[eNb_id][1],4*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES_NO_PREFIX,1,1);
        }

        tx_lev = 0;


        for (aa=0; aa<eNB->frame_parms.nb_antenna_ports_eNB; aa++) {
          if (eNB->frame_parms.Ncp == 1)
            PHY_ofdm_mod(&eNB->common_vars.txdataF[eNb_id][aa][subframe*nsymb*eNB->frame_parms.ofdm_symbol_size],        // input,
                         &txdata[aa][subframe*eNB->frame_parms.samples_per_tti],         // output
                         eNB->frame_parms.ofdm_symbol_size,
                         2*nsymb,                 // number of symbols
                         eNB->frame_parms.nb_prefix_samples,               // number of prefix samples
                         CYCLIC_PREFIX);
          else {
            normal_prefix_mod(&eNB->common_vars.txdataF[eNb_id][aa][subframe*nsymb*eNB->frame_parms.ofdm_symbol_size],
                              &txdata[aa][subframe*eNB->frame_parms.samples_per_tti],
                              2*nsymb,
                              frame_parms);
          }

          tx_lev += signal_energy(&txdata[aa][subframe*eNB->frame_parms.samples_per_tti],
                                  eNB->frame_parms.ofdm_symbol_size);
        }

        tx_lev_dB = (unsigned int) dB_fixed(tx_lev);
      }

      for (i=0; i<2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; i++) {
        for (aa=0; aa<eNB->frame_parms.nb_antenna_ports_eNB; aa++) {
          if (awgn_flag == 0) {
            s_re[aa][i] = ((double)(((short *)txdata[aa]))[(2*subframe*UE->frame_parms.samples_per_tti) + (i<<1)]);
            s_im[aa][i] = ((double)(((short *)txdata[aa]))[(2*subframe*UE->frame_parms.samples_per_tti) + (i<<1)+1]);
          } else {
            for (aarx=0; aarx<UE->frame_parms.nb_antennas_rx; aarx++) {
              if (aa==0) {
                r_re[aarx][i] = ((double)(((short *)txdata[aa]))[(2*subframe*UE->frame_parms.samples_per_tti) + (i<<1)]);
                r_im[aarx][i] = ((double)(((short *)txdata[aa]))[(2*subframe*UE->frame_parms.samples_per_tti) + (i<<1)+1]);
              } else {
                r_re[aarx][i] += ((double)(((short *)txdata[aa]))[(2*subframe*UE->frame_parms.samples_per_tti) + (i<<1)]);
                r_im[aarx][i] += ((double)(((short *)txdata[aa]))[(2*subframe*UE->frame_parms.samples_per_tti) + (i<<1)+1]);
              }
            }
          }
        }
      }



      if (awgn_flag == 0) {
        multipath_channel(eNB2UE,s_re,s_im,r_re,r_im,
                          2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES,0);
      }

      //write_output("channel0.m","chan0",ch[0],channel_length,1,8);

      // scale by path_loss = NOW - P_noise
      //sigma2       = pow(10,sigma2_dB/10);
      //N0W          = -95.87;
      sigma2_dB = (double)tx_lev_dB +10*log10((double)eNB->frame_parms.ofdm_symbol_size/(double)(12*eNB->frame_parms.N_RB_DL)) - SNR;

      if (n_frames==1)
        printf("sigma2_dB %f (SNR %f dB) tx_lev_dB %d\n",sigma2_dB,SNR,tx_lev_dB);

      //AWGN
      sigma2 = pow(10,sigma2_dB/10);

      //  printf("Sigma2 %f (sigma2_dB %f)\n",sigma2,sigma2_dB);
      for (i=0; i<2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; i++) {
        for (aa=0; aa<UE->frame_parms.nb_antennas_rx; aa++) {

          ((short*) UE->common_vars.rxdata[aa])[(2*subframe*UE->frame_parms.samples_per_tti) + 2*i] = (short) (.667*(r_re[aa][i] + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
          ((short*) UE->common_vars.rxdata[aa])[(2*subframe*UE->frame_parms.samples_per_tti) + 2*i+1] = (short) (.667*(r_im[aa][i] + (iqim*r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(
                0.0,1.0)));
          /*
          ((short*)UE->common_vars.rxdata[aa])[(2*subframe*UE->frame_parms.samples_per_tti) + 2*i] =
            ((short*)txdata[aa])[(2*subframe*UE->frame_parms.samples_per_tti) + 2*i];
          ((short*)UE->common_vars.rxdata[aa])[(2*subframe*UE->frame_parms.samples_per_tti) + 2*i+1] =
            ((short*)txdata[aa])[(2*subframe*UE->frame_parms.samples_per_tti) + 2*i+1];
          */
        }
      }

      // UE receiver
      for (l=0; l<eNB->frame_parms.symbols_per_tti; l++) {

        //  subframe_offset = (l/eNB->frame_parms.symbols_per_tti)*eNB->frame_parms.samples_per_tti;
        //      printf("subframe_offset = %d\n",subframe_offset);

        slot_fep(UE,
                 l%(eNB->frame_parms.symbols_per_tti/2),
                 (2*subframe)+(l/(eNB->frame_parms.symbols_per_tti/2)),
                 0,
                 0,
		 0);

        if (UE->perfect_ce == 1) {
          if (awgn_flag==0) {
            // fill in perfect channel estimates
            freq_channel(eNB2UE,UE->frame_parms.N_RB_DL,12*UE->frame_parms.N_RB_DL + 1);

            //write_output("channel.m","ch",desc1->ch[0],desc1->channel_length,1,8);
            //write_output("channelF.m","chF",desc1->chF[0],nb_samples,1,8);
            for(k=0; k<NUMBER_OF_eNB_MAX; k++) {
              for(aa=0; aa<frame_parms->nb_antennas_tx; aa++) {
                for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
                  for (i=0; i<frame_parms->N_RB_DL*12; i++) {
                    ((int16_t *) UE->common_vars.common_vars_rx_data_per_thread[subframe&0x1].dl_ch_estimates[k][(aa<<1)+aarx])[2*i+(l*frame_parms->ofdm_symbol_size+LTE_CE_FILTER_LENGTH)*2]=(int16_t)(
                          eNB2UE->chF[aarx+(aa*frame_parms->nb_antennas_rx)][i].x*AMP);
                    ((int16_t *) UE->common_vars.common_vars_rx_data_per_thread[subframe&0x1].dl_ch_estimates[k][(aa<<1)+aarx])[2*i+1+(l*frame_parms->ofdm_symbol_size+LTE_CE_FILTER_LENGTH)*2]=(int16_t)(
                          eNB2UE->chF[aarx+(aa*frame_parms->nb_antennas_rx)][i].y*AMP);
                  }
                }
              }
            }
          } else {
            for(aa=0; aa<frame_parms->nb_antenna_ports_eNB; aa++) {
              for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
                for (i=0; i<frame_parms->N_RB_DL*12; i++) {
                  ((int16_t *) UE->common_vars.common_vars_rx_data_per_thread[subframe&0x1].dl_ch_estimates[0][(aa<<1)+aarx])[2*i+(l*frame_parms->ofdm_symbol_size+LTE_CE_FILTER_LENGTH)*2]=(short)(AMP);
                  ((int16_t *) UE->common_vars.common_vars_rx_data_per_thread[subframe&0x1].dl_ch_estimates[0][(aa<<1)+aarx])[2*i+1+(l*frame_parms->ofdm_symbol_size+LTE_CE_FILTER_LENGTH)*2]=0/2;
                }
              }
            }
          }
        }

        if (l==((eNB->frame_parms.Ncp==0)?4:3)) {

          //      write_output("H00.m","h00",&(UE->common_vars.dl_ch_estimates[0][0][0]),((frame_parms->Ncp==0)?7:6)*(eNB->frame_parms.ofdm_symbol_size),1,1);

          // do PDCCH procedures here
          UE->pdcch_vars[0][0]->crnti = n_rnti;

          //    printf("Doing RX : num_pdcch_symbols at TX %d\n",num_pdcch_symbols);
          rx_pdcch(UE,
                   trial,
                   subframe,
                   0,
                   (UE->frame_parms.mode1_flag == 1) ? SISO : ALAMOUTI,
                   UE->high_speed_flag,
                   UE->is_secondary_ue);

          if (is_phich_subframe(&UE->frame_parms,subframe)) {
            UE->ulsch[0]->harq_processes[phich_subframe_to_harq_pid(&UE->frame_parms,0,subframe)]->status = ACTIVE;
            //UE->ulsch[0]->harq_processes[phich_subframe_to_harq_pid(&UE->frame_parms,0,subframe)]->Ndi = 1;
            rx_phich(UE,
		     &UE->proc.proc_rxtx[subframe&1],
                     subframe,
                     0);
          }

          //    if (UE->pdcch_vars[0]->num_pdcch_symbols != num_pdcch_symbols)
          //      break;
          dci_cnt = dci_decoding_procedure(UE,
                                           dci_alloc_rx,1,
                                           0,subframe);

          common_rx=0;
          ul_rx=0;
          dl_rx=0;

          if (n_frames==1)  {
            numCCE = get_nCCE(UE->pdcch_vars[0][0]->num_pdcch_symbols, &UE->frame_parms, get_mi(&UE->frame_parms,subframe));

            for (i = 0; i < dci_cnt; i++)
              printf("dci %d: rnti 0x%x, format %d, L %d, nCCE %d/%d dci_length %d\n",i, dci_alloc_rx[i].rnti, dci_alloc_rx[i].format,
                     dci_alloc_rx[i].L, dci_alloc_rx[i].firstCCE, numCCE, dci_alloc_rx[i].dci_length);
          }

          for (i=0; i<dci_cnt; i++) {
            if (dci_alloc_rx[i].rnti == SI_RNTI) {
              if (n_frames==1)
                dump_dci(&UE->frame_parms, &dci_alloc_rx[i]);

              common_rx=1;
            }

            if ((dci_alloc_rx[i].rnti == n_rnti) && (dci_alloc_rx[i].format == format0)) {
              if (n_frames==1)
                dump_dci(&UE->frame_parms, &dci_alloc_rx[i]);

              ul_rx=1;
            }

            if ((dci_alloc_rx[i].rnti == n_rnti) && ((dci_alloc_rx[i].format == format1))) {
              if (n_frames==1)
                dump_dci(&UE->frame_parms, &dci_alloc_rx[i]);

              dl_rx=1;
            }

            if ((dci_alloc_rx[i].rnti != n_rnti) && (dci_alloc_rx[i].rnti != SI_RNTI))
              false_detection_cnt++;
          }

          if (n_frames==1)
            printf("RX DCI Num %d (Common DCI %d, DL DCI %d, UL DCI %d)\n", dci_cnt, common_rx, dl_rx, ul_rx);

          if ((common_rx==0)&&(common_active==1))
            n_errors_common++;

          if ((ul_rx==0)&&(ul_active==1)) {
            n_errors_ul++;
            //     exit(-1);
          }

          if ((dl_rx==0)&&(dl_active==1)) {
            n_errors_dl++;
            //   exit(-1);
          }

          if (UE->pdcch_vars[0][0]->num_pdcch_symbols != num_pdcch_symbols)
            n_errors_cfi++;

          /*
           if (is_phich_subframe(&UE->frame_parms,subframe))
             if (UE->ulsch[0]->harq_processes[phich_subframe_to_harq_pid(&UE->frame_parms, UE->frame, subframe)]->Ndi != phich_ACK)
               n_errors_hi++;
          */

          if (n_errors_cfi > 10)
            break;
        }

      } // symbol loop

      if (n_errors_cfi > 100)
        break;

      if ((n_errors_ul>1000) && (n_errors_dl>1000) && (n_errors_common>1000))
        break;

#ifdef XFORMS
      phy_scope_UE(form_ue,
                   UE,
                   eNb_id,0,subframe);
#endif

    } //trials
    
    if (common_active) printf("SNR %f : n_errors_common = %d/%d (%e)\n", SNR,n_errors_common,n_trials_common,(double)n_errors_common/n_trials_common);
    if (ul_active==1) printf("SNR %f : n_errors_ul = %d/%d (%e)\n", SNR,n_errors_ul,n_trials_ul,(double)n_errors_ul/n_trials_ul);
    if (dl_active==1) printf("SNR %f : n_errors_dl = %d/%d (%e)\n", SNR,n_errors_dl,n_trials_dl,(double)n_errors_dl/n_trials_dl);
    printf("SNR %f : n_errors_cfi = %d/%d (%e)\n", SNR,n_errors_cfi,trial,(double)n_errors_cfi/trial);
    printf("SNR %f : n_errors_hi = %d/%d (%e)\n", SNR,n_errors_hi,trial,(double)n_errors_hi/trial);
    
  } // SNR
 

  if (n_frames==1) {
    write_output("txsig0.m","txs0", txdata[0],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);

    if (n_tx>1)
      write_output("txsig1.m","txs1", txdata[1],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);

    write_output("rxsig0.m","rxs0", UE->common_vars.rxdata[0],10*frame_parms->samples_per_tti,1,1);
    write_output("rxsigF0.m","rxsF0", UE->common_vars.common_vars_rx_data_per_thread[subframe&0x1].rxdataF[0],NUMBER_OF_OFDM_CARRIERS*2*((frame_parms->Ncp==0)?14:12),2,1);

    if (n_rx>1) {
      write_output("rxsig1.m","rxs1", UE->common_vars.rxdata[1],10*frame_parms->samples_per_tti,1,1);
      write_output("rxsigF1.m","rxsF1", UE->common_vars.common_vars_rx_data_per_thread[subframe&0x1].rxdataF[1],NUMBER_OF_OFDM_CARRIERS*2*((frame_parms->Ncp==0)?14:12),2,1);
    }

    write_output("H00.m","h00",&(UE->common_vars.common_vars_rx_data_per_thread[subframe&0x1].dl_ch_estimates[0][0][0]),((frame_parms->Ncp==0)?7:6)*(eNB->frame_parms.ofdm_symbol_size),1,1);

    if (n_tx==2)
      write_output("H10.m","h10",&(UE->common_vars.common_vars_rx_data_per_thread[subframe&0x1].dl_ch_estimates[0][2][0]),((frame_parms->Ncp==0)?7:6)*(eNB->frame_parms.ofdm_symbol_size),1,1);

    write_output("pdcch_rxF_ext0.m","pdcch_rxF_ext0",UE->pdcch_vars[0][eNb_id]->rxdataF_ext[0],3*12*UE->frame_parms.N_RB_DL,1,1);
    write_output("pdcch_rxF_comp0.m","pdcch0_rxF_comp0",UE->pdcch_vars[0][eNb_id]->rxdataF_comp[0],4*12*UE->frame_parms.N_RB_DL,1,1);
    write_output("pdcch_rxF_llr.m","pdcch_llr",UE->pdcch_vars[0][eNb_id]->llr,2400,1,4);
  }

  lte_sync_time_free();

  return(n_errors_ul);

  }
Esempio n. 7
0
int main(int argc, char **argv)
{

  int i,l,aa,sector;
  double sigma2, sigma2_dB=0;
  mod_sym_t **txdataF;
#ifdef IFFT_FPGA
  int **txdataF2;
#endif
  int **txdata,**rxdata;
  double **s_re,**s_im,**r_re,**r_im;
  double amps[8] = {0.3868472 , 0.3094778 , 0.1547389 , 0.0773694 , 0.0386847 , 0.0193424 , 0.0096712 , 0.0038685};
  double aoa=.03,ricean_factor=1,Td=1.0;
  int channel_length;
  int amp;

  unsigned char pbch_pdu[6];
  int sync_pos, sync_pos_slot;
  FILE *rx_frame_file;
  int result;
  int freq_offset;
  int subframe_offset;
  char fname[40], vname[40];
  int trial, n_errors=0;
  unsigned int nb_rb = 25;
  unsigned int first_rb = 0;
  unsigned int eNb_id = 0;
  unsigned int slot_offset = 0;
  unsigned int sample_offset = 0;
  unsigned int channel_offset = 0;
  int n_frames;

  int slot=0,last_slot=0,next_slot=0;

  double nf[2] = {3.0,3.0}; //currently unused
  double ip =0.0;
  double N0W, path_loss, path_loss_dB, tx_pwr, rx_pwr;
  double rx_gain;
  int rx_pwr2, target_rx_pwr_dB;

  struct complex **ch;
  unsigned char first_call = 1;

  LTE_DL_FRAME_PARMS frame_parms;
  LTE_DL_FRAME_PARMS *lte_frame_parms = &frame_parms;

  if (argc==2)
    amp = atoi(argv[1]);
  else
    amp = 1024;

  // we normalize the tx power to 0dBm, assuming the amplitude of the signal is 1024
  // the SNR is this given by the difference of the path loss and the thermal noise (~-105dBm)
  // the rx_gain is adjusted automatically to achieve the target_rx_pwr_dB

  path_loss_dB = -90;
  path_loss    = pow(10,path_loss_dB/10);
  target_rx_pwr_dB = 60;

  lte_frame_parms->N_RB_DL            = 25;
  lte_frame_parms->N_RB_UL            = 25;
  lte_frame_parms->Ng_times6          = 1;
  lte_frame_parms->Ncp                = 1;
  lte_frame_parms->Nid_cell           = 0;
  lte_frame_parms->nushift            = 0;
  lte_frame_parms->nb_antennas_tx     = 2;
  lte_frame_parms->nb_antennas_rx     = 2;
  lte_frame_parms->first_dlsch_symbol = 4;
  lte_frame_parms->num_dlsch_symbols  = 6;
  lte_frame_parms->Csrs = 2;
  lte_frame_parms->Bsrs = 0;
  lte_frame_parms->kTC = 0;
  lte_frame_parms->n_RRC = 0;
  lte_frame_parms->mode1_flag = 1;
  lte_frame_parms->ofdm_symbol_size = 512;
  lte_frame_parms->log2_symbol_size = 9;
  lte_frame_parms->samples_per_tti = 7680;
  lte_frame_parms->first_carrier_offset = 362;
  lte_frame_parms->nb_prefix_samples>>=2;

#ifdef IFFT_FPGA
  txdata    = (int **)malloc16(2*sizeof(int*));
  txdata[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
  txdata[1] = (int *)malloc16(FRAME_LENGTH_BYTES);

  bzero(txdata[0],FRAME_LENGTH_BYTES);
  bzero(txdata[1],FRAME_LENGTH_BYTES);

  rxdata    = (int **)malloc16(2*sizeof(int*));
  rxdata[0] = (int *)malloc16(2*FRAME_LENGTH_BYTES);
  rxdata[1] = (int *)malloc16(2*FRAME_LENGTH_BYTES);

  bzero(rxdata[0],2*FRAME_LENGTH_BYTES);
  bzero(rxdata[1],2*FRAME_LENGTH_BYTES);

  txdataF2    = (int **)malloc16(2*sizeof(int*));
  txdataF2[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
  txdataF2[1] = (int *)malloc16(FRAME_LENGTH_BYTES);

  bzero(txdataF2[0],FRAME_LENGTH_BYTES);
  bzero(txdataF2[1],FRAME_LENGTH_BYTES);
#endif

  s_re = malloc(2*sizeof(double*));
  s_im = malloc(2*sizeof(double*));
  r_re = malloc(2*sizeof(double*));
  r_im = malloc(2*sizeof(double*));

  for (i=0; i<2; i++) {

    s_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
  }

  for (i=0; i<2; i++) {
    for (l=0; l<FRAME_LENGTH_COMPLEX_SAMPLES; l++) {
      ((short*) txdata[i])[2*l]   = amp * cos(M_PI/2*l);
      ((short*) txdata[i])[2*l+1] = amp * sin(M_PI/2*l);
    }
  }

  tx_pwr = signal_energy(txdata[0],lte_frame_parms->samples_per_tti>>1);
  printf("tx_pwr (DAC in) %d dB for slot %d (subframe %d)\n",dB_fixed(tx_pwr),next_slot,next_slot>>1);



  channel_length = (int) 11+2*BW*Td;

  ch = (struct complex**) malloc(4 * sizeof(struct complex*));

  for (i = 0; i<4; i++)
    ch[i] = (struct complex*) malloc(channel_length * sizeof(struct complex));

  randominit(0);
  set_taus_seed(0);

#ifdef RF
  tx_pwr = dac_fixed_gain(s_re,
                          s_im,
                          txdata,
                          lte_frame_parms->nb_antennas_tx,
                          lte_frame_parms->samples_per_tti>>1,
                          14,
                          18); //this should give 0dBm output level for input with amplitude 1024

  printf("tx_pwr (DAC out) %f dB for slot %d (subframe %d)\n",10*log10(tx_pwr),next_slot,next_slot>>1);
#else

  for (i=0; i<(lte_frame_parms->samples_per_tti>>1); i++) {
    for (aa=0; aa<lte_frame_parms->nb_antennas_tx; aa++) {
      s_re[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)]);
      s_im[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)+1]);
    }
  }

#endif

  //      printf("channel for slot %d (subframe %d)\n",next_slot,next_slot>>1);
  multipath_channel(ch,s_re,s_im,r_re,r_im,
                    amps,Td,BW,ricean_factor,aoa,
                    lte_frame_parms->nb_antennas_tx,
                    lte_frame_parms->nb_antennas_rx,
                    lte_frame_parms->samples_per_tti>>1,
                    channel_length,
                    0,
                    .9,
                    (first_call == 1) ? 1 : 0);

  if (first_call == 1)
    first_call = 0;

#ifdef RF

  //path_loss_dB = 0;
  //path_loss = 1;

  for (i=0; i<(lte_frame_parms->samples_per_tti>>1); i++) {
    for (aa=0; aa<lte_frame_parms->nb_antennas_rx; aa++) {
      r_re[aa][i]=r_re[aa][i]*sqrt(path_loss);
      r_im[aa][i]=r_im[aa][i]*sqrt(path_loss);

    }
  }

  rx_pwr = signal_energy_fp(r_re,r_im,lte_frame_parms->nb_antennas_rx,lte_frame_parms->samples_per_tti>>1,0);
  printf("rx_pwr (RF in) %f dB for slot %d (subframe %d)\n",10*log10(rx_pwr),next_slot,next_slot>>1);

  rx_gain = target_rx_pwr_dB - 10*log10(rx_pwr);

  // RF model
  rf_rx(r_re,
        r_im,
        NULL,
        NULL,
        0,
        lte_frame_parms->nb_antennas_rx,
        lte_frame_parms->samples_per_tti>>1,
        1.0/7.68e6 * 1e9,  // sampling time (ns)
        0.0,               // freq offset (Hz) (-20kHz..20kHz)
        0.0,               // drift (Hz) NOT YET IMPLEMENTED
        nf,                // noise_figure NOT YET IMPLEMENTED
        rx_gain-66.227,    // rx gain (66.227 = 20*log10(pow2(11)) = gain from the adc that will be applied later)
        200,               // IP3_dBm (dBm)
        &ip,               // initial phase
        30.0e3,            // pn_cutoff (kHz)
        -500.0,            // pn_amp (dBc) default: 50
        0.0,               // IQ imbalance (dB),
        0.0);              // IQ phase imbalance (rad)

  rx_pwr = signal_energy_fp(r_re,r_im,lte_frame_parms->nb_antennas_rx,lte_frame_parms->samples_per_tti>>1,0);

  printf("rx_pwr (ADC in) %f dB for slot %d (subframe %d)\n",10*log10(rx_pwr),next_slot,next_slot>>1);
#endif

#ifdef RF
  adc(r_re,
      r_im,
      0,
      slot_offset,
      rxdata,
      lte_frame_parms->nb_antennas_rx,
      lte_frame_parms->samples_per_tti>>1,
      12);

  rx_pwr2 = signal_energy(rxdata[0]+slot_offset,lte_frame_parms->samples_per_tti>>1);

  printf("rx_pwr (ADC out) %f dB (%d) for slot %d (subframe %d)\n",10*log10((double)rx_pwr2),rx_pwr2,next_slot,next_slot>>1);

#else

  for (i=0; i<(lte_frame_parms->samples_per_tti>>1); i++) {
    for (aa=0; aa<lte_frame_parms->nb_antennas_rx; aa++) {
      ((short*) rxdata[aa])[2*slot_offset + (2*i)]   = (short) ((r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0));
      ((short*) rxdata[aa])[2*slot_offset + (2*i)+1] = (short) ((r_im[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0));

    }
  }

#endif

  write_output("rxsig0.m","rxs0",rxdata[0],lte_frame_parms->samples_per_tti>>1,1,1);
  write_output("rxsig1.m","rxs1",rxdata[1],lte_frame_parms->samples_per_tti>>1,1,1);


#ifdef IFFT_FPGA
  free(txdataF2[0]);
  free(txdataF2[1]);
  free(txdataF2);
  free(txdata[0]);
  free(txdata[1]);
  free(txdata);
  free(rxdata[0]);
  free(rxdata[1]);
  free(rxdata);
#endif

  for (i=0; i<2; i++) {
    free(s_re[i]);
    free(s_im[i]);
    free(r_re[i]);
    free(r_im[i]);
  }

  free(s_re);
  free(s_im);
  free(r_re);
  free(r_im);

  return(0);
}
Esempio n. 8
0
int main(int argc, char **argv) {

  char c;

  int i,aa,aarx;
  double sigma2, sigma2_dB=0,SNR,snr0=-2.0,snr1=0.0;
  u8 snr1set=0;
  //mod_sym_t **txdataF;
#ifdef IFFT_FPGA
  int **txdataF2;
#endif
  int **txdata;
  double **s_re,**s_im,**r_re,**r_im;
  double ricean_factor=0.0000005,Td=.8,iqim=0.0;
  u8 channel_length;
  int trial, ntrials=1;
  u8 transmission_mode = 1,n_tx=1,n_rx=1;
  u16 Nid_cell=0;

  u8 awgn_flag=0;
  int n_frames=1;
  channel_desc_t *UE2eNB;
  u32 nsymb,tx_lev,tx_lev_dB;
  u8 extended_prefix_flag=0;
  s8 interf1=-19,interf2=-19;
  LTE_DL_FRAME_PARMS *frame_parms;
#ifdef EMOS
  fifo_dump_emos emos_dump;
#endif


  SCM_t channel_model=Rayleigh1_corr;

  u8 abstraction_flag=0,calibration_flag=0;
  //  double prach_sinr;
  u8 osf=1,N_RB_DL=25;
  u32 prach_errors=0;
  u8 subframe=3;
  u16 preamble_energy_list[64],preamble_tx=99,preamble_delay_list[64];
  u16 preamble_max,preamble_energy_max;
  PRACH_RESOURCES_t prach_resources;
  u8 prach_fmt;
  int N_ZC;

  channel_length = (int) 11+2*BW*Td;

//  number_of_cards = 1;
  openair_daq_vars.rx_rf_mode = 1;
  
  /*
    rxdataF    = (int **)malloc16(2*sizeof(int*));
    rxdataF[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
    rxdataF[1] = (int *)malloc16(FRAME_LENGTH_BYTES);
    
    rxdata    = (int **)malloc16(2*sizeof(int*));
    rxdata[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
    rxdata[1] = (int *)malloc16(FRAME_LENGTH_BYTES);
  */
  /*while ((c = getopt (argc, argv, "haA:Cr:p:g:i:j:n:s:S:t:x:y:z:N:F:")) != -1)
    {
      switch (c)
	{
	case 'a':
	  printf("Running AWGN simulation\n");
	  awgn_flag = 1;
	  ntrials=1;
	  break;
	case 'g':
	  switch((char)*optarg) {
	  case 'A': 
	    channel_model=SCM_A;
	    break;
	  case 'B': 
	    channel_model=SCM_B;
	    break;
	  case 'C': 
	    channel_model=SCM_C;
	    break;
	  case 'D': 
	    channel_model=SCM_D;
	    break;
	  case 'E': 
	    channel_model=EPA;
	    break;
	  case 'F': 
	    channel_model=EVA;
	    break;
	  case 'G': 
	    channel_model=ETU;
	    break;
	  case 'H':
	    channel_model=Rayleigh8;
	  case 'I':
	    channel_model=Rayleigh1;
	  case 'J':
	    channel_model=Rayleigh1_corr;
	  case 'K':
	    channel_model=Rayleigh1_anticorr;
	  case 'L':
	    channel_model=Rice8;
	  case 'M':
	    channel_model=Rice1;
	  break;
	  default:
	    msg("Unsupported channel model!\n");
	    exit(-1);
	  }
	break;
	case 'i':
	  interf1=atoi(optarg);
	  break;
	case 'j':
	  interf2=atoi(optarg);
	  break;
	case 'n':
	  n_frames = atoi(optarg);
	  break;
	case 's':
	  snr0 = atof(optarg);
	  msg("Setting SNR0 to %f\n",snr0);
	  break;
	case 'S':
	  snr1 = atof(optarg);
	  snr1set=1;
	  msg("Setting SNR1 to %f\n",snr1);
	  break;
	case 't':
	  Td= atof(optarg);
	  break;
	case 'p':
	  preamble_tx=atoi(optarg);
	  break;
	case 'r':
	  ricean_factor = pow(10,-.1*atof(optarg));
	  if (ricean_factor>1) {
	    printf("Ricean factor must be between 0 and 1\n");
	    exit(-1);
	  }
	  break;
	case 'x':
	  transmission_mode=atoi(optarg);
	  if ((transmission_mode!=1) &&
	      (transmission_mode!=2) &&
	      (transmission_mode!=6)) {
	    msg("Unsupported transmission mode %d\n",transmission_mode);
	    exit(-1);
	  }
	  break;
	case 'y':
	  n_tx=atoi(optarg);
	  if ((n_tx==0) || (n_tx>2)) {
	    msg("Unsupported number of tx antennas %d\n",n_tx);
	    exit(-1);
	  }
	  break;
	case 'z':
	  n_rx=atoi(optarg);
	  if ((n_rx==0) || (n_rx>2)) {
	    msg("Unsupported number of rx antennas %d\n",n_rx);
	    exit(-1);
	  }
	  break;
	case 'A':
	  abstraction_flag=1;
	  ntrials=10000;
	  msg("Running Abstraction test\n");
	  break;
	case 'C':
	  calibration_flag=1;
	  msg("Running Abstraction calibration for Bias removal\n");
	  break;
	case 'N':
	  Nid_cell = atoi(optarg);
	  break;
	case 'R':
	  N_RB_DL = atoi(optarg);
	  break;
	case 'O':
	  osf = atoi(optarg);
	  break;
	case 'F':
	  break;
	default:
	case 'h':
	  printf("%s -h(elp) -a(wgn on) -p(extended_prefix) -N cell_id -f output_filename -F input_filename -g channel_model -n n_frames -t Delayspread -r Ricean_FactordB -s snr0 -S snr1 -x transmission_mode -y TXant -z RXant -i Intefrence0 -j Interference1 -A interpolation_file -C(alibration offset dB) -N CellId\n",argv[0]);
	  printf("-h This message\n");
	  printf("-a Use AWGN channel and not multipath\n");
	  printf("-p Use extended prefix mode\n");
	  printf("-n Number of frames to simulate\n");
	  printf("-r Ricean factor (dB, 0 means Rayleigh, 100 is almost AWGN\n");
	  printf("-s Starting SNR, runs from SNR0 to SNR0 + 5 dB.  If n_frames is 1 then just SNR is simulated\n");
	  printf("-S Ending SNR, runs from SNR0 to SNR1\n");
	  printf("-t Delay spread for multipath channel\n");
	  printf("-g [A,B,C,D,E,F,G] Use 3GPP SCM (A,B,C,D) or 36-101 (E-EPA,F-EVA,G-ETU) models (ignores delay spread and Ricean factor)\n");
	  printf("-x Transmission mode (1,2,6 for the moment)\n");
	  printf("-y Number of TX antennas used in eNB\n");
	  printf("-z Number of RX antennas used in UE\n");
	  printf("-i Relative strength of first intefering eNB (in dB) - cell_id mod 3 = 1\n");
	  printf("-j Relative strength of second intefering eNB (in dB) - cell_id mod 3 = 2\n");
	  printf("-N Nid_cell\n");
	  printf("-R N_RB_DL\n");
	  printf("-O oversampling factor (1,2,4,8,16)\n");
	  printf("-A Interpolation_filname Run with Abstraction to generate Scatter plot using interpolation polynomial in file\n");
	  printf("-C Generate Calibration information for Abstraction (effective SNR adjustment to remove Pe bias w.r.t. AWGN)\n");
	  printf("-f PRACH format (0=1,1=2,2=3,3=4)\n");
	  printf("-F Input filename (.txt format) for RX conformance testing\n");
	  exit (-1);
	  break;
	}
    }*/

  if (transmission_mode==2)
    n_tx=2;

  lte_param_init(n_tx,n_rx,transmission_mode,extended_prefix_flag,Nid_cell,N_RB_DL,osf);


  if (snr1set==0) {
    if (n_frames==1)
      snr1 = snr0+.1;
    else
      snr1 = snr0+5.0;
  }

  printf("SNR0 %f, SNR1 %f\n",snr0,snr1);

  frame_parms = &PHY_vars_eNB->lte_frame_parms;


  txdata = PHY_vars_UE->lte_ue_common_vars.txdata;
  printf("txdata %p\n",&txdata[0][subframe*frame_parms->samples_per_tti]);
  
  s_re = (double **)malloc(2*sizeof(double*));
  s_im = (double **)malloc(2*sizeof(double*));
  r_re = (double **)malloc(2*sizeof(double*));
  r_im = (double **)malloc(2*sizeof(double*));
  nsymb = (frame_parms->Ncp == 0) ? 14 : 12;

  printf("FFT Size %d, Extended Prefix %d, Samples per subframe %d, Symbols per subframe %d\n",NUMBER_OF_OFDM_CARRIERS,
	 frame_parms->Ncp,frame_parms->samples_per_tti,nsymb);


  
  msg("[SIM] Using SCM/101\n");
  UE2eNB = new_channel_desc_scm(PHY_vars_eNB->lte_frame_parms.nb_antennas_tx,
				PHY_vars_UE->lte_frame_parms.nb_antennas_rx,
				channel_model,
				BW,
				0.0,
				0,
				0);
  

  if (UE2eNB==NULL) {
    msg("Problem generating channel model. Exiting.\n");
    exit(-1);
  }

  for (i=0;i<2;i++) {

    s_re[i] = (double *)malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im[i] = (double *)malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));

    r_re[i] = (double *)malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im[i] = (double *)malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
  }
 
  PHY_vars_UE->lte_frame_parms.prach_config_common.rootSequenceIndex=1; 
  PHY_vars_UE->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex=0; 
  PHY_vars_UE->lte_frame_parms.prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig=1;
  PHY_vars_UE->lte_frame_parms.prach_config_common.prach_ConfigInfo.highSpeedFlag=0;
  PHY_vars_UE->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset=0;


  PHY_vars_eNB->lte_frame_parms.prach_config_common.rootSequenceIndex=1; 
  PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex=0; 
  PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig=1;
  PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.highSpeedFlag=0;
  PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset=0;

  prach_fmt = get_prach_fmt(PHY_vars_eNB->lte_frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex,
			    PHY_vars_eNB->lte_frame_parms.frame_type);
  N_ZC = (prach_fmt <4)?839:139;
  
  compute_prach_seq(prach_root_sequence_map0_3[PHY_vars_eNB->lte_frame_parms.prach_config_common.rootSequenceIndex],N_ZC, PHY_vars_eNB->X_u[0]);

  compute_prach_seq(prach_root_sequence_map0_3[PHY_vars_UE->lte_frame_parms.prach_config_common.rootSequenceIndex],N_ZC, PHY_vars_UE->X_u[0]);

  PHY_vars_UE->lte_ue_prach_vars[0]->amp = (s32)scfdma_amps[6];

  PHY_vars_UE->prach_resources[0] = &prach_resources;
  if (preamble_tx == 99)
    preamble_tx = (u16)(taus()&0x3f);
  if (n_frames == 1)
     printf("raPreamble %d\n",preamble_tx);

  PHY_vars_UE->prach_resources[0]->ra_PreambleIndex = preamble_tx;
  PHY_vars_UE->prach_resources[0]->ra_TDD_map_index = 0;

  tx_lev = generate_prach(PHY_vars_UE,
			  0, //eNB_id,
			  subframe, 
			  0); //Nf

  tx_lev_dB = (unsigned int) dB_fixed(tx_lev);
    
  write_output("txsig0_new.m","txs0", &txdata[0][subframe*frame_parms->samples_per_tti],frame_parms->samples_per_tti,1,1);
    //write_output("txsig1.m","txs1", txdata[1],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);

    // multipath channel
  dump_prach_config(&PHY_vars_eNB->lte_frame_parms,subframe);

  for (i=0;i<2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES;i++) {
    for (aa=0;aa<1;aa++) {
      if (awgn_flag == 0) {
	s_re[aa][i] = ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)]);
	s_im[aa][i] = ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)+1]);
      }
      else {
	for (aarx=0;aarx<PHY_vars_eNB->lte_frame_parms.nb_antennas_rx;aarx++) {
	  if (aa==0) {
	    r_re[aarx][i] = ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)]);
	    r_im[aarx][i] = ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)+1]);
	  }
	  else {
	    r_re[aarx][i] += ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)]);
	    r_im[aarx][i] += ((double)(((short *)&txdata[aa][subframe*frame_parms->samples_per_tti]))[(i<<1)+1]);
	  }
	}
      }
    }
  }



  for (SNR=snr0;SNR<snr1;SNR+=.2) {

    printf("n_frames %d SNR %f\n",n_frames,SNR);
    prach_errors=0;
    for (trial=0; trial<n_frames; trial++) {
      
      sigma2_dB = 10*log10((double)tx_lev) - SNR;
      if (n_frames==1)
	printf("sigma2_dB %f (SNR %f dB) tx_lev_dB %f\n",sigma2_dB,SNR,10*log10((double)tx_lev));
      //AWGN
      sigma2 = pow(10,sigma2_dB/10);
      //	printf("Sigma2 %f (sigma2_dB %f)\n",sigma2,sigma2_dB);
            

      if (awgn_flag == 0) {
	multipath_channel(UE2eNB,s_re,s_im,r_re,r_im,
			  2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES,0);
      }
      if (n_frames==1) {
	printf("rx_level data symbol %f, tx_lev %f\n",
	       10*log10(signal_energy_fp(r_re,r_im,1,OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES,0)),
	       10*log10((double)tx_lev));
      }

      for (i=0; i<2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; i++) {
	for (aa=0;aa<PHY_vars_eNB->lte_frame_parms.nb_antennas_rx;aa++) {
	
	  ((short*) &PHY_vars_eNB->lte_eNB_common_vars.rxdata[0][aa][subframe*frame_parms->samples_per_tti])[2*i] = (short) (.167*(r_re[aa][i] +sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
	  ((short*) &PHY_vars_eNB->lte_eNB_common_vars.rxdata[0][aa][subframe*frame_parms->samples_per_tti])[2*i+1] = (short) (.167*(r_im[aa][i] + (iqim*r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
	}
      }
	
      rx_prach(PHY_vars_eNB,
	       subframe,
	       preamble_energy_list,
	       preamble_delay_list,
	       0,   //Nf
	       0);    //tdd_mapindex

      preamble_energy_max = preamble_energy_list[0];
      preamble_max = 0;
      for (i=1;i<64;i++) {
	if (preamble_energy_max < preamble_energy_list[i]) {
	  //	  printf("preamble %d => %d\n",i,preamble_energy_list[i]);
	
	  preamble_energy_max = preamble_energy_list[i];
	  preamble_max = i;
	}
      }
      if (preamble_max!=preamble_tx)
	prach_errors++;
      if (n_frames==1) {
	write_output("prach0.m","prach0", &txdata[0][subframe*frame_parms->samples_per_tti],frame_parms->samples_per_tti,1,1);
	write_output("prachF0.m","prachF0", &PHY_vars_UE->lte_ue_prach_vars[0]->prachF[0],6144,1,1);
	write_output("rxsig0.m","rxs0", 
		     &PHY_vars_eNB->lte_eNB_common_vars.rxdata[0][0][subframe*frame_parms->samples_per_tti],
		     frame_parms->samples_per_tti,1,1);
	write_output("rxsigF0.m","rxsF0", &PHY_vars_eNB->lte_eNB_common_vars.rxdataF[0][0][0],512*nsymb*2,2,1);
	write_output("prach_preamble.m","prachp",&PHY_vars_eNB->X_u[0],839,1,1);
      }
    }
    printf("SNR %f dB: errors %d/%d\n",SNR,prach_errors,n_frames);
  }
#ifdef IFFT_FPGA
  free(txdataF2[0]);
  free(txdataF2[1]);
  free(txdataF2);
  free(txdata[0]);
  free(txdata[1]);
  free(txdata);
#endif 

  for (i=0;i<2;i++) {
    free(s_re[i]);
    free(s_im[i]);
    free(r_re[i]);
    free(r_im[i]);
  }
  free(s_re);
  free(s_im);
  free(r_re);
  free(r_im);
  
 // lte_sync_time_free();
  system("PAUSE");
  return(0);

}
double aggregate_eNB_UE_localization_stats(PHY_VARS_eNB *phy_vars_eNB, int8_t UE_id, frame_t frame, sub_frame_t subframe, int32_t UE_tx_power_dB) {
    // parameters declaration
    int8_t Mod_id, CC_id;
    int32_t harq_pid, avg_power, avg_rssi, median_power, median_rssi, median_subcarrier_rss, median_TA, median_TA_update, ref_timestamp_ms, current_timestamp_ms;
    char cqis[100], sub_powers[2048];
    int len = 0, i;
    struct timeval ts;
    double sys_bw = 0;
    uint8_t N_RB_DL;
    LTE_DL_FRAME_PARMS *frame_parms = &phy_vars_eNB->lte_frame_parms;

    Mod_id = phy_vars_eNB->Mod_id;
    CC_id = phy_vars_eNB->CC_id;
    ref_timestamp_ms = phy_vars_eNB->ulsch_eNB[UE_id+1]->reference_timestamp_ms;

    for (i=0; i<13; i++) {
        len += sprintf(&cqis[len]," %d ", phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].DL_subband_cqi[0][i]);
    }
    len = 0;
    for (i=0; i<phy_vars_eNB->lte_eNB_pusch_vars[(uint32_t)UE_id]->active_subcarrier; i++) {
        len += sprintf(&sub_powers[len]," %d ", phy_vars_eNB->lte_eNB_pusch_vars[(uint32_t)UE_id]->subcarrier_power[i]);
    }

    gettimeofday(&ts, NULL);
    current_timestamp_ms = ts.tv_sec * 1000 + ts.tv_usec / 1000;


    LOG_F(LOCALIZE, "PHY: [UE %x/%d -> eNB %d], timestamp %d, "
          "frame %d, subframe %d"
          "UE Tx power %d dBm, "
          "RSSI ant1 %d dBm, "
          "RSSI ant2 %d dBm, "
          "pwr ant1 %d dBm, "
          "pwr ant2 %d dBm, "
          "Rx gain %d dBm, "
          "TA %d, "
          "TA update %d, "
          "DL_CQI (%d,%d), "
          "Wideband CQI (%d,%d), "
          "DL Subband CQI[13] %s \n"
          "timestamp %d, (%d active subcarrier) %s \n"
          ,phy_vars_eNB->dlsch_eNB[(uint32_t)UE_id][0]->rnti, UE_id, Mod_id, current_timestamp_ms,
          frame,subframe,
          UE_tx_power_dB,
          phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].UL_rssi[0],
          phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].UL_rssi[1],
          dB_fixed(phy_vars_eNB->lte_eNB_pusch_vars[(uint32_t)UE_id]->ulsch_power[0]),
          dB_fixed(phy_vars_eNB->lte_eNB_pusch_vars[(uint32_t)UE_id]->ulsch_power[1]),
          phy_vars_eNB->rx_total_gain_eNB_dB,
          phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].UE_timing_offset, // raw timing advance 1/sampling rate
          phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].timing_advance_update,
          phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].DL_cqi[0],phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].DL_cqi[1],
          phy_vars_eNB->PHY_measurements_eNB[Mod_id].wideband_cqi_dB[(uint32_t)UE_id][0],
          phy_vars_eNB->PHY_measurements_eNB[Mod_id].wideband_cqi_dB[(uint32_t)UE_id][1],
          cqis,
          current_timestamp_ms,
          phy_vars_eNB->lte_eNB_pusch_vars[(uint32_t)UE_id]->active_subcarrier,
          sub_powers);

    N_RB_DL = frame_parms->N_RB_DL;
    switch (N_RB_DL)
    {
    case 6:
        sys_bw = 1.92;
        break;
    case 25:
        sys_bw = 7.68;
        break;
    case 50:
        sys_bw = 15.36;
        break;
    case 100:
        sys_bw = 30.72;
        break;
    }

    if ((current_timestamp_ms - ref_timestamp_ms > phy_vars_eNB->ulsch_eNB[UE_id+1]->aggregation_period_ms) &&
            (phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_rss_list.size != 0)) {
        // check the size of one list to be sure there was a message transmitted during the defined aggregation period
        median_power = calculate_median(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_rss_list);
        del(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_rss_list);
        median_rssi = calculate_median(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_rssi_list);
        del(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_rssi_list);
        median_subcarrier_rss = calculate_median(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_subcarrier_rss_list);
        del(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_subcarrier_rss_list);
        median_TA = calculate_median(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_timing_advance_list);
        del(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_timing_advance_list);
        median_TA_update = calculate_median(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_timing_update_list);
        del(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_timing_update_list);

        double alpha = 2, power_distance, time_distance;
        power_distance = pow(10, ((UE_tx_power_dB - (median_subcarrier_rss - phy_vars_eNB->rx_total_gain_eNB_dB))/(20.0*alpha)));
        time_distance = (double) 299792458*(phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].UE_timing_offset)/(sys_bw*1000000);

        phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].distance.time_based = time_distance;
        phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].distance.power_based = power_distance;

        LOG_D(LOCALIZE, " PHY [UE %x/%d -> eNB %d], timestamp %d, "
              "frame %d, subframe %d "
              "UE Tx power %d dBm, "
              "median RSSI %d dBm, "
              "median Power %d dBm, "
              "Rx gain %d dBm, "
              "power estimated r = %0.3f, "
              " TA %d, update %d "
              "TA estimated r = %0.3f\n"
              ,phy_vars_eNB->dlsch_eNB[(uint32_t)UE_id][0]->rnti, UE_id, Mod_id, current_timestamp_ms,
              frame, subframe,
              UE_tx_power_dB,
              median_rssi,
              median_power,
              phy_vars_eNB->rx_total_gain_eNB_dB,
              power_distance,
              phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].UE_timing_offset, median_TA_update,
              time_distance);

        initialize(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_rss_list);
        initialize(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_subcarrier_rss_list);
        initialize(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_rssi_list);
        initialize(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_timing_advance_list);
        initialize(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_timing_update_list);

        // make the reference timestamp == current timestamp
        phy_vars_eNB->ulsch_eNB[UE_id+1]->reference_timestamp_ms = current_timestamp_ms;
        return 0;
    }
    else {
        avg_power = (dB_fixed(phy_vars_eNB->lte_eNB_pusch_vars[(uint32_t)UE_id]->ulsch_power[0]) + dB_fixed(phy_vars_eNB->lte_eNB_pusch_vars[(uint32_t)UE_id]->ulsch_power[1]))/2;
        avg_rssi = (phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].UL_rssi[0] + phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].UL_rssi[1])/2;

        push_front(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_rss_list,avg_power);
        push_front(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_rssi_list,avg_rssi);
        for (i=0; i<phy_vars_eNB->lte_eNB_pusch_vars[(uint32_t)UE_id]->active_subcarrier; i++) {
            push_front(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_subcarrier_rss_list, phy_vars_eNB->lte_eNB_pusch_vars[(uint32_t)UE_id]->subcarrier_power[i]);
        }
        push_front(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_timing_advance_list, phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].UE_timing_offset);
        push_front(&phy_vars_eNB->ulsch_eNB[UE_id+1]->loc_timing_update_list, phy_vars_eNB->eNB_UE_stats[(uint32_t)UE_id].timing_advance_update);
        return -1;
    }
}