void rf_rx_simple(double **r_re,
                  double **r_im,
                  unsigned int nb_rx_antennas,
                  unsigned int length,
                  double s_time,
                  double rx_gain_dB)
{

  int i,a;
  double rx_gain_lin = pow(10.0,.05*rx_gain_dB);
  //double rx_gain_lin = 1.0;
  double N0W         = pow(10.0,.1*(-174.0 - 10*log10(s_time*1e-9)));
  //double N0W = 0.0;

  //  printf("s_time=%f, N0W=%g\n",s_time,10*log10(N0W));

  //Loop over input
#ifdef DEBUG_RF
  printf("N0W = %f dBm\n",10*log10(N0W));
  printf("rx_gain = %f dB(%f)\n",rx_gain_dB,rx_gain_lin);
#endif

  for (i=0; i<length; i++) {
    for (a=0; a<nb_rx_antennas; a++) {
      // Amplify by receiver gain and apply 3rd order non-linearity
      r_re[a][i] = rx_gain_lin*(r_re[a][i] + sqrt(.5*N0W)*gaussdouble(0.0,1.0));
      r_im[a][i] = rx_gain_lin*(r_im[a][i] + sqrt(.5*N0W)*gaussdouble(0.0,1.0));
    }
  }
}
int test_logmap8(LTE_eNB_DLSCH_t *dlsch_eNB,
                 LTE_UE_DLSCH_t *dlsch_ue,
                 unsigned int coded_bits,
                 unsigned char NB_RB,
                 double sigma,
                 unsigned char qbits,
                 unsigned int block_length,
                 unsigned int ntrials,
                 unsigned int *errors,
                 unsigned int *trials,
                 unsigned int *uerrors,
                 unsigned int *crc_misses,
                 unsigned int *iterations,
                 unsigned int num_pdcch_symbols,
                 unsigned int subframe)
{

  unsigned char test_input[block_length+1];

  short *channel_output;


  unsigned char decoded_output[block_length];
  unsigned int i,trial=0;
  unsigned int crc=0;
  unsigned char ret;
  unsigned char uerr;
  unsigned char crc_type;


  channel_output = (short *)malloc(coded_bits*sizeof(short));

  *iterations=0;
  *errors=0;
  *crc_misses=0;
  *uerrors=0;



  //  printf("dlsch_eNB->TBS= %d, block_length %d\n",dlsch_eNB->harq_processes[0]->TBS,block_length);

  while (trial++ < ntrials) {

    //    printf("encoding\n");
    //    test_input[0] = 0x80;
    for (i=0; i<block_length; i++) {

      test_input[i] = i&0xff;//(unsigned char)(taus()&0xff);
    }

    dlsch_encoding(test_input,
                   &PHY_vars_eNB->lte_frame_parms,
                   num_pdcch_symbols,
                   PHY_vars_eNB->dlsch_eNB[0][0],
                   0,
                   subframe,
                   &PHY_vars_eNB->dlsch_rate_matching_stats,
                   &PHY_vars_eNB->dlsch_turbo_encoding_stats,
                   &PHY_vars_eNB->dlsch_interleaving_stats);

    uerr=0;


    for (i = 0; i < coded_bits; i++) {
#ifdef DEBUG_CODER

      if ((i&0xf)==0)
        printf("\ne %d..%d:    ",i,i+15);

      printf("%d.",PHY_vars_eNB->dlsch_eNB[0][0]->harq_processes[0]->e[i]);
#endif
      channel_output[i] = (short)quantize(sigma/4.0,(2.0*PHY_vars_eNB->dlsch_eNB[0][0]->harq_processes[0]->e[i]) - 1.0 + sigma*gaussdouble(0.0,1.0),qbits);
      //            printf("input %d, output %f\n",(2*PHY_vars_eNB->dlsch_eNB[0][0]->e[i]) - 1,
      //             (2.0*PHY_vars_eNB->dlsch_eNB[0][0]->e[i]) - 1.0 + sigma*gaussdouble(0.0,1.0));
    }

#ifdef DEBUG_CODER
    printf("\n");
    exit(-1);
#endif



    PHY_vars_UE->dlsch_ue[0][0]->harq_processes[PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid]->G = coded_bits;
    ret = dlsch_decoding(PHY_vars_UE,
                         channel_output,
                         &PHY_vars_UE->lte_frame_parms,
                         PHY_vars_UE->dlsch_ue[0][0],
                         PHY_vars_UE->dlsch_ue[0][0]->harq_processes[PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid],
                         subframe,
                         PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid,
                         num_pdcch_symbols,1);

    /*    int diffs = 0,puncts=0;
    for (i=0;i<dlsch_ue->harq_processes[0]->Kplus*3;i++) {
      if (dlsch_ue->harq_processes[0]->d[0][96+i] == 0) {
    printf("%d punct (%d,%d)\n",i,dlsch_ue->harq_processes[0]->d[0][96+i],dlsch_eNb->harq_processes[0]->d[0][96+i]);
    puncts++;
      }
      else if (sgn(dlsch_ue->harq_processes[0]->d[0][96+i]) != dlsch_eNb->harq_processes[0]->d[0][96+i]) {
    printf("%d differs (%d,%d)\n",i,dlsch_ue->harq_processes[0]->d[0][96+i],dlsch_eNb->harq_processes[0]->d[0][96+i]);
    diffs++;
      }
      else
    printf("%d same (%d,%d)\n",i,dlsch_ue->harq_processes[0]->d[0][96+i],dlsch_eNb->harq_processes[0]->d[0][96+i]);
    }
    printf("diffs %d puncts %d(%d,%d,%d,%d,%d)\n",diffs,puncts,dlsch_ue->harq_processes[0]->F,coded_bits,3*(block_length<<3),3*dlsch_ue->harq_processes[0]->Kplus,3*dlsch_ue->harq_processes[0]->F+3*(block_length<<3)-coded_bits);


    printf("ret %d (max %d)\n",ret,dlsch_ue->max_turbo_iterations);
        printf("trial %d : i %d/%d : Input %x, Output %x (%x, F %d)\n",trial,0,block_length,test_input[0],
         dlsch_ue->harq_processes[PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid]->b[0],
         dlsch_ue->harq_processes[PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid]->c[0][0],
         (dlsch_ue->harq_processes[PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid]->F>>3));
    */
    if (ret < dlsch_ue->max_turbo_iterations+1) {
      *iterations = (*iterations) + ret;
      //      if (ret>1)
      //  printf("ret %d\n",ret);
    } else
      *iterations = (*iterations) + (ret-1);

    if (uerr==1)
      *uerrors = (*uerrors) + 1;

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

      if (dlsch_ue->harq_processes[PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid]->c[0][i] != test_input[i]) {
        /*
          printf("i %d/%d : Input %x, Output %x (%x, F %d)\n",i,block_length,test_input[i],
                 dlsch_ue->harq_processes[PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid]->b[i],
                 dlsch_ue->harq_processes[PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid]->c[0][i],
                 (dlsch_ue->harq_processes[PHY_vars_UE->dlsch_ue[0][0]->current_harq_pid]->F>>3));
        */
        *errors = (*errors) + 1;
        //  printf("*%d, ret %d\n",*errors,ret);



        if (ret < dlsch_ue->max_turbo_iterations+1)
          *crc_misses = (*crc_misses)+1;

        break;

      }


    }

    if (ret == dlsch_ue->max_turbo_iterations+1) {
      //      exit(-1);
    }

    /*
    else {

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

    if (dlsch_ue->harq_processes[0]->b[i] != test_input[i]) {
    printf("i %d/%d : Input %x, Output %x (%x, F %d)\n",i,block_length,test_input[i],
     dlsch_ue->harq_processes[0]->b[i],
     dlsch_ue->harq_processes[0]->c[0][i],
     (dlsch_ue->harq_processes[0]->F>>3));

    }

      }
      }*/

    if (*errors == 100) {
      printf("trials %d\n",trial);
      break;
    }
  }

  *trials = trial;
  //  printf("lte: trials %d, errors %d\n",trial,*errors);
  return(0);
}
//free(input_data);
void rf_rx(double **r_re,
           double **r_im,
           double **r_re_i1,
           double **r_im_i1,
           double I0_dB,
           unsigned int nb_rx_antennas,
           unsigned int length,
           double s_time,
           double f_off,
           double drift,
           double noise_figure,
           double rx_gain_dB,
           int IP3_dBm,
           double *initial_phase,
           double pn_cutoff,
           double pn_amp_dBc,
           double IQ_imb_dB,
           double IQ_phase)
{

  double phase       = *initial_phase;
  double phase2      = *initial_phase;
  double phase_inc   = 2*M_PI*f_off*s_time*1e-9;
  double IQ_imb_lin  = pow(10.0,-.05*IQ_imb_dB);
  double rx_gain_lin = pow(10.0,.05*rx_gain_dB);
  double IP3_lin     = pow(10.0,-.1*IP3_dBm);
  double p_noise     = 0.0;
  double tmp_re,tmp_im;
  double N0W         = pow(10.0,.1*(-174.0 - 10*log10(s_time*1e-9)));
  //  printf("s_time=%f, N0W=%g\n",s_time,10*log10(N0W));

  // phase-noise filter coefficients (2nd order digital Butterworth)
  double pn_cutoff_d = tan(M_PI*s_time*1e-9*pn_cutoff);
  double pn_c        = 1+2*cos(M_PI/4)*pn_cutoff_d + (pn_cutoff_d*pn_cutoff_d);
  double pn_a0       = pn_cutoff_d*pn_cutoff_d/pn_c;
  double pn_b1       = 2*((pn_cutoff_d*pn_cutoff_d) - 1)/pn_c;
  double pn_b2       = (4*pn_a0) - pn_b1 - 1;
  double x_n=0.0,x_n1=0.0,x_n2=0.0,y_n1=0.0,y_n2=0.0;

  double pn_amp      = pow(10.0,.1*pn_amp_dBc);
  double I0 = pow(10.0,.05*I0_dB);
  //  double dummy;

  int i,a,have_interference=0;


  if (pn_amp_dBc > -20.0) {
    printf("rf.c: Illegal pn_amp_dBc %f\n",pn_amp_dBc);
    exit(-1);
  }

  if ((pn_cutoff > 1e6) || (pn_cutoff<1e3)) {
    printf("rf.c: Illegal pn_cutoff %f\n",pn_cutoff);
    exit(-1);
  }

  if (fabs(IQ_imb_dB) > 1.0) {
    printf("rf.c: Illegal IQ_imb %f\n",IQ_imb_dB);
    exit(-1);
  }

  if (fabs(IQ_phase) > 0.1) {
    printf("rf.c: Illegal IQ_phase %f\n",IQ_phase);
    exit(-1);
  }

  if (fabs(f_off) > 10000.0) {
    printf("rf.c: Illegal f_off %f\n",f_off);
    exit(-1);
  }

  if (fabs(drift) > 1000.0) {
    printf("rf.c: Illegal drift %f\n",drift);
    exit(-1);
  }

#ifdef DEBUG_RF
  printf("pn_a0 = %f, pn_b1=%f,pn_b2=%f\n",pn_a0,pn_b1,pn_b2);
#endif

  /*
  for (i=0;i<nb_rx_antennas;i++)
    if (noise_figure[i] < 1.0) {
      printf("rf.c: Illegal noise_figure %d %f\n",i,noise_figure[i]);
      exit(-1);
    }
  */

  //Loop over input
#ifdef DEBUG_RF
  printf("N0W = %f dBm\n",10*log10(N0W));
  printf("rx_gain = %f dB(%f)\n",rx_gain_dB,rx_gain_lin);
  printf("IQ_imb = %f dB(%f)\n",IQ_imb_dB,IQ_imb_lin);
#endif
  p_noise=0.0;



  if ((r_re_i1) && (r_im_i1) )
    have_interference=1;

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


    for (a=0; a<nb_rx_antennas; a++) {

      if (have_interference==1) {
        r_re[a][i] = r_re[a][i] + (I0 * r_re_i1[a][i]);
        r_im[a][i] = r_im[a][i] + (I0 * r_im_i1[a][i]);
      }

      // Amplify by receiver gain and apply 3rd order non-linearity
      r_re[a][i] = rx_gain_lin*(r_re[a][i] + IP3_lin*(pow(r_re[a][i],3.0) + 3.0*r_re[a][i]*r_im[a][i]*r_im[a][i])) + rx_gain_lin*(sqrt(.5*N0W)*gaussdouble(0.0,1.0));
      r_im[a][i] = rx_gain_lin*(r_im[a][i] + IP3_lin*(pow(r_im[a][i],3.0) + 3.0*r_im[a][i]*r_re[a][i]*r_re[a][i])) + rx_gain_lin*(sqrt(.5*N0W)*gaussdouble(0.0,1.0));



      // Apply phase offsets
      tmp_re = r_re[a][i]*cos(phase2) - r_im[a][i]*sin(phase2);
      tmp_im = IQ_imb_lin*(r_re[a][i]*sin(phase2+IQ_phase) + r_im[a][i]*cos(phase2+IQ_phase));

      r_re[a][i] = tmp_re;
      r_im[a][i] = tmp_im;

    }


    //    if (nb_rx_antennas == 1) {
    //      dummy = gaussdouble(0.0,1.0);
    //      dummy = gaussdouble(0.0,1.0);
    //    }
    // First apply frequency/phase offsets + phase noise
    //    U[i%pn_len]=uniformrandom()*pn_amp_lin;
    //    p_noise = 0;
    //    for (j=0;j<pn_len;j++)
    //      p_noise += h_pn[j] * U[(i-j)%pn_len];

    // recompute phase offsets for next sample
    phase += phase_inc;
    phase2 = phase + sqrt(pn_amp)*p_noise;

    //    printf("phase = %f, phase2=%f\n",phase,phase2);

    //*initial_phase = phase2;

    //compute next realization of phase-noise process
    x_n2 = x_n1;
    x_n1 = x_n;
    x_n = gaussdouble(0.0,1.0);
    y_n1 = p_noise;
    y_n2 = y_n1;
    p_noise = pn_a0*x_n + 2*pn_a0*x_n1 + pn_a0*x_n2 - pn_b1*y_n1 - pn_b2*y_n2;

    //    pn[i] = p_noise;
  }
}
Beispiel #4
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);

}
Beispiel #5
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);

  }
Beispiel #6
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);

}
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);
}
Beispiel #8
0
int test_logmap8(LTE_eNB_DLSCH_t *dlsch_eNB,
		 LTE_UE_DLSCH_t *dlsch_ue,
		 unsigned int coded_bits,
		 unsigned char NB_RB,
		 double sigma,
		 unsigned char qbits,
		 unsigned int block_length,
		 unsigned int ntrials,
		 unsigned int *errors,
		 unsigned int *trials,
		 unsigned int *uerrors,
		 unsigned int *crc_misses,
		 unsigned int *iterations,
		 unsigned int num_pdcch_symbols,
		 unsigned int subframe) {

  unsigned char test_input[block_length+1];
  //_declspec(align(16))  char channel_output[512];
  //_declspec(align(16))  unsigned char output[512],decoded_output[16], *inPtr, *outPtr;

  short *channel_output;


  unsigned char decoded_output[block_length];
  unsigned int i,trial=0;
  unsigned int crc=0;
  unsigned char ret;
  unsigned char uerr;
  unsigned char crc_type;


  channel_output = (short *)malloc(coded_bits*sizeof(short));

  *iterations=0;
  *errors=0;
  *crc_misses=0;
  *uerrors=0;



  //  printf("dlsch_eNB->TBS= %d\n",dlsch_eNB->harq_processes[0]->TBS);

  while (trial++ < ntrials) {

    //    printf("encoding\n");
    for (i=0;i<block_length;i++) {
      
      test_input[i] = (unsigned char)(taus()&0xff);
    }

    dlsch_encoding(test_input,
		   &PHY_vars_eNB->lte_frame_parms,
		   num_pdcch_symbols,
		   PHY_vars_eNB->dlsch_eNB[0][0],
		   subframe);

    uerr=0;


    for (i = 0; i < coded_bits; i++){
      channel_output[i] = (short)quantize(sigma/4.0,(2.0*PHY_vars_eNB->dlsch_eNB[0][0]->e[i]) - 1.0 + sigma*gaussdouble(0.0,1.0),qbits);
    }


  
    
    //    memset(decoded_output,0,16);
    //    printf("decoding\n");
    ret = dlsch_decoding(channel_output,
			 &PHY_vars_UE->lte_frame_parms,
			 PHY_vars_UE->dlsch_ue[0][0],
			 subframe,
			 num_pdcch_symbols);

    /*    int diffs = 0,puncts=0;
    for (i=0;i<dlsch_ue->harq_processes[0]->Kplus*3;i++) {
      if (dlsch_ue->harq_processes[0]->d[0][96+i] == 0) {
	printf("%d punct (%d,%d)\n",i,dlsch_ue->harq_processes[0]->d[0][96+i],dlsch_eNb->harq_processes[0]->d[0][96+i]);
	puncts++;
      }
      else if (sgn(dlsch_ue->harq_processes[0]->d[0][96+i]) != dlsch_eNb->harq_processes[0]->d[0][96+i]) {
	printf("%d differs (%d,%d)\n",i,dlsch_ue->harq_processes[0]->d[0][96+i],dlsch_eNb->harq_processes[0]->d[0][96+i]);
	diffs++;
      }
      else
	printf("%d same (%d,%d)\n",i,dlsch_ue->harq_processes[0]->d[0][96+i],dlsch_eNb->harq_processes[0]->d[0][96+i]);
    }
    printf("diffs %d puncts %d(%d,%d,%d,%d,%d)\n",diffs,puncts,dlsch_ue->harq_processes[0]->F,coded_bits,3*(block_length<<3),3*dlsch_ue->harq_processes[0]->Kplus,3*dlsch_ue->harq_processes[0]->F+3*(block_length<<3)-coded_bits);
    */
    
    //    printf("ret %d\n",ret);
    //    printf("trial %d : i %d/%d : Input %x, Output %x (%x, F %d)\n",trial,0,block_length,test_input[0],
    //	   dlsch_ue->harq_processes[0]->b[0],
    //	   dlsch_ue->harq_processes[0]->c[0][0],
    //	   (dlsch_ue->harq_processes[0]->F>>3));
    
    if (ret < MAX_TURBO_ITERATIONS+1)
      *iterations = (*iterations) + ret;
    else
      *iterations = (*iterations) + (ret-1);

    if (uerr==1)
      *uerrors = (*uerrors) + 1;
    
    for (i=0;i<block_length;i++) {
            
      if (dlsch_ue->harq_processes[0]->b[i] != test_input[i]) {
	//	printf("i %d/%d : Input %x, Output %x (%x, F %d)\n",i,block_length,test_input[i],
	//	       dlsch_ue->harq_processes[0]->b[i],
	//	       dlsch_ue->harq_processes[0]->c[0][i],
	//	       (dlsch_ue->harq_processes[0]->F>>3));

	*errors = (*errors) + 1;
//	printf("*%d\n",*errors);	


	
	if (ret < MAX_TURBO_ITERATIONS+1)
	  *crc_misses = (*crc_misses)+1;
	break;
	
      }
    }
    if (*errors == 100) {
      //printf("\n");
      break;
    }
  }

  *trials = trial;
  //  printf("lte: trials %d, errors %d\n",trial,*errors);
  return(0);
}
Beispiel #9
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);

}
Beispiel #10
0
// Leosam 08/08: What are the arguments?
int
main (int argc, char **argv)
{

	// Command line parsing
	int input_val = 1000;	// Value serving as an input relatad to the SNR
	int max_num_ofdm_sym = 64;	// Index of the last frame for the channel estimation
	int be_verbose = 0;	// Operate in verbose mode
	int index;
	int c;
	unsigned int rx_energy[NB_ANTENNAS], n0_energy[NB_ANTENNAS];

	if (argc == 1)
	{
		error ();
		exit (-1);
	}

	while ((c = getopt (argc, argv, "hVv:n:")) != -1)
	{
		switch (c)
		{
		case 'h':
			help ();
			exit (1);
		case 'V':
			be_verbose = 1;
			break;
		case 'v':
			input_val = atoi (optarg);
			break;
			return 1;
		case 'n':
			max_num_ofdm_sym = atoi (optarg);
			break;
			return 1;
		default:
			error ();
			exit (-1);
		}
	}

	//
	// Initialization stuff
	//

	int i, ii, j, ret, delay, l;
	short seed[3];

	// Leosam 08/08: What is this amps?
	double amps[8] = { 1.0, .8, .4, .2, .1, .05, .025, .01 };
	struct complex ch[NB_ANTENNAS * NB_ANTENNAS][10 + (int) (1 + 2 * BW * Td)];
	struct complex rx_tmp, tx, n, phase;

	char *chbch_pdu;
	int chbch_size;
	int extension;
	unsigned char dummy_mac_pdu[120];

#ifdef USER_MODE
  char fname[40], vname[40];
#endif // USER_MODE

	if (be_verbose)
		printf ("Allocating memory for PHY_VARS\n");

	// Memory allocation for PHY and MAC structures
	PHY_vars = malloc (sizeof (PHY_VARS));
	PHY_config = malloc (sizeof (PHY_CONFIG));
	mac_xface = malloc (sizeof (MAC_xface));

	// Loading of the configuration data
	if ((config = fopen ("config.cfg", "r")) == NULL)	// this can be configured
	{
		if (be_verbose)
			printf ("[Main USER] The openair configuration file <config.cfg> could not be found!\n");
		exit (0);
	}

	if ((scenario = fopen ("scenario.scn", "r")) == NULL)
	{
		if (be_verbose)
			printf ("[Main USER] The openair scenario file <scenario.scn> could not be found!\n");
		exit (0);
	}

	if (be_verbose)
		printf ("Opened configuration files\n");

	reconfigure_MACPHY (scenario);

	if (be_verbose)
		dump_config ();

	//  Leosam 08/08: This is repeated bellow. Is it necessary?
	mac_xface->is_cluster_head = 0;

	// Initialize the PHY and MAC variables
	phy_init (NB_ANTENNAS_TX);
	if (be_verbose)
		printf ("Initialized PHY variables\n");


	// Fill MAC PDU buffer for CHBCH
	seed[0] = (short) time (NULL);
	seed[1] = (short) time (NULL);
	seed[2] = (short) time (NULL);
	seed48 (&seed[0]);

	randominit ();

	/*
	 * for (i=0;i<mac_xface->mac_tch->bch_tx[0].size-4;i++) {
	 * mac_xface->mac_tch->bch_tx[0].data[i] = i;//(u8)lrand48();
	 * }
	 * 
	 * 
	 * printf("Filled CHBCH PDU with random data\n");
	 * 
	 * // Generate one CHBCH
	 * phy_generate_chbch(0);
	 * 
	 */

	//
	//  Preparation for the TX procedure
	// 

	// Creation of the CHBCH
	chbch_size = (NUMBER_OF_CARRIERS_PER_GROUP * (NUMBER_OF_CHBCH_SYMBOLS) * 16) >> 3;
	if (be_verbose)
		printf ("chbch_size = %d\n", chbch_size);
	chbch_pdu = malloc (chbch_size);

	for (i = 0; i < chbch_size - 4; i++)
	{
		chbch_pdu[i] = i;
	}

	if (be_verbose)
		printf ("Filled CHBCH PDU (%d bytes) with data\n", chbch_size);

	// Leosam 08/08: O.o
	delay = 1032;
	//	delay = 0;

	// Generation of the CHBCH
	phy_generate_chbch (0, 1, NB_ANTENNAS_TX, chbch_pdu);

	// Generation of the pilot symbols
	for (i = 16; i < max_num_ofdm_sym; i++)
	{
		phy_generate_sch (0, i, 0xFFFF, 1, NB_ANTENNAS_TX);
	}
	
	mac_xface->is_cluster_head = 0;
	
	if (be_verbose)
	{
		for (ii=0; ii<NB_ANTENNAS; ii++)
		{
			sprintf (fname, "txsig%d.m", ii);
			sprintf (vname, "txs%d", ii);

			write_output (fname, vname,
										(s16 *) PHY_vars->tx_vars[ii].TX_DMA_BUFFER,
										NUMBER_OF_SYMBOLS_PER_FRAME * OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES, 1, 1);
		}
	}


	// generate channels
	if (be_verbose)
		printf ("Generating MIMO Channels\n");

	phase.r = 0;
	phase.i = 0;
	
	printf("Phase pointer %0x\n",&phase);

	for (i = 0; i < NB_ANTENNAS; i++)
	{
		for (j = 0; j < NB_ANTENNAS; j++)
		{
			random_channel (amps, Td, 8, BW, ch[j + (i * NB_ANTENNAS)],0.0,&phase);
		}
	}

	if (be_verbose)
		printf ("chbch_pdu %x (%d)\n", PHY_vars->chbch_data[0].demod_pdu, chbch_size);

	if (be_verbose)
	{
		for (l = 0; l < (1 + 2 * BW * Td); l++)
		{
			printf ("(%f,%f)\n", ch[0][l]);
		}
	}

	//
	// TX procedure
	//

	// Transmission 
	//
	//////////////////////////
	
	// Foreach symbol
	for (i = 0; i < (max_num_ofdm_sym) * OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; i++)
	{
		// Foreach RX antenna
		for (ii = 0; ii < NB_ANTENNAS; ii++)
		{
			rx_tmp.r = 0;
			rx_tmp.i = 0;
			n.r = gaussdouble (0.0, 10.0);
			n.i = gaussdouble (0.0, 10.0);
			
			// Foreach TX antenna
			for (j = 0; j < NB_ANTENNAS; j++)
			{
				
				// Foreach symbol
				for (l = 0; l < (1 + 2 * BW * Td); l++)
				{

					tx.r = (double) (((s16 *) & PHY_vars->tx_vars[j].
							  TX_DMA_BUFFER[0 *
									OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES])
							 [2 * (i - l)]) / sqrt (1.0 * input_val);
					tx.i = (double) (((s16 *) & PHY_vars->tx_vars[j].
							  TX_DMA_BUFFER[0 *
									OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES])
							 [1 +
							  (2 * (i - l))]) / sqrt (1.0 * input_val);

					rx_tmp.r +=
						(tx.r * ch[j + (ii * NB_ANTENNAS)][l].r) -
						(tx.i * ch[j + (ii * NB_ANTENNAS)][l].i);
					rx_tmp.i +=
						(tx.i * ch[j + (ii * NB_ANTENNAS)][l].r) +
						(tx.r * ch[j + (ii * NB_ANTENNAS)][l].i);

				}
			}
 
			((s16 *) & PHY_vars->rx_vars[ii].
			 RX_DMA_BUFFER[delay + (0 * OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES)])[2 * i] =
					(short) (rx_tmp.r + n.r);
			((s16 *) & PHY_vars->rx_vars[ii].
			 RX_DMA_BUFFER[delay + (0 * OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES)])[1 +
											(2 * i)] =
					(short) (rx_tmp.i + n.i);
		}
	}

	if (be_verbose)
	{
		for (ii=0; ii<NB_ANTENNAS; ii++)
		{
			sprintf (fname, "rxsig%d.m", ii);
			sprintf (vname, "rxs%d", ii);

			write_output (fname, vname,
			      (s16 *) PHY_vars->rx_vars[ii].RX_DMA_BUFFER,
						NUMBER_OF_SYMBOLS_PER_FRAME * OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES, 1, 1);
		}
	}

	//
	// RX procedure
	//  

	if (be_verbose)
		printf ("Starting RX\n");

	// Sync
	phy_synch_time (PHY_vars->rx_vars[0].RX_DMA_BUFFER,
			&sync_pos, 
			FRAME_LENGTH_COMPLEX_SAMPLES, 
			768, 
			CHSCH, 
			0);

	// Forcing sync to 0 since were running offline
	if (be_verbose)
		msg ("sync_pos = %d\n", sync_pos);
	PHY_vars->rx_vars[0].offset = 0;	//sync_pos;
	
	// estamte the signal and noise energy
	for (ii=0; ii < NB_ANTENNAS; ii++)
	{
		rx_energy[ii] = signal_energy((int *)&PHY_vars->rx_vars[ii].RX_DMA_BUFFER[PHY_vars->rx_vars[0].offset+CYCLIC_PREFIX_LENGTH],
																	OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES);
		n0_energy[ii] = signal_energy((int *)&PHY_vars->rx_vars[ii].RX_DMA_BUFFER[PHY_vars->rx_vars[0].offset+CYCLIC_PREFIX_LENGTH + 
																	(NUMBER_OF_CHSCH_SYMBOLS+NUMBER_OF_CHBCH_SYMBOLS+1) * OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES],
																	OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES);
		//msg("CYCLIC_PREFIX_LENGTH=%d, NUMBER_OF_CHSCH_SYMBOLS=%d, NUMBER_OF_CHBCH_SYMBOLS=%d, OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES=%d\n", CYCLIC_PREFIX_LENGTH, NUMBER_OF_CHSCH_SYMBOLS, NUMBER_OF_CHBCH_SYMBOLS, OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES);
		msg("symbol = %d\n", PHY_vars->rx_vars[ii].RX_DMA_BUFFER[PHY_vars->rx_vars[0].offset+CYCLIC_PREFIX_LENGTH + 
				(NUMBER_OF_CHSCH_SYMBOLS+NUMBER_OF_CHBCH_SYMBOLS) * OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES]);
		msg("SNR Ant %d = %d / %d \n", ii, rx_energy[ii], n0_energy[ii]);
	}	

	// CHBCH channel estimation
	phy_channel_estimation_top(PHY_vars->rx_vars[0].offset,0,0,0,NB_ANTENNAS,0);
	//phy_channel_estimation_top (PHY_vars->rx_vars[0].offset, 0, 0, 0);

	phy_decode_chbch (0, &dummy_mac_pdu[0], NB_ANTENNAS, NB_ANTENNAS_TX, 120);

	if (be_verbose)
	{
		for (i = 0; i < chbch_size; i++)
		{
			msg ("Data %x : %x\n", i, PHY_vars->chbch_data[0].demod_pdu[i]);
		}
	}
	
	printf("PERROR_SHIFT: %d\n", PERROR_SHIFT);
	
	//
	// Channel estimation procedure
	//
// 	for (i = 0; i < max_num_ofdm_sym; i++)
// 	{
// 		phy_channel_estimation_top(PHY_vars->rx_vars[0].offset,i,0,0,1);
// 	}
	  
	phy_channel_est_emos(16, 16, max_num_ofdm_sym-1, TRUE, 0);

	phy_cleanup ();
	if (be_verbose)
		printf ("Exiting\n");

}
Beispiel #11
0
int main(int argc, char **argv)
{

  char c;

  int i,l,aa;
  double sigma2, sigma2_dB=0,SNR,snr0=-2.0,snr1;
  uint8_t snr1set=0;
  //mod_sym_t **txdataF;
  int **txdata,**txdata1,**txdata2;
  double **s_re,**s_im,**s_re1,**s_im1,**s_re2,**s_im2,**r_re,**r_im,**r_re1,**r_im1,**r_re2,**r_im2;
  double iqim = 0.0;
  unsigned char pbch_pdu[6];
  //  int sync_pos, sync_pos_slot;
  //  FILE *rx_frame_file;
  FILE *output_fd;
  uint8_t write_output_file=0;
  int result;
  int freq_offset;
  //  int subframe_offset;
  //  char fname[40], vname[40];
  int trial, n_trials, ntrials=1, n_errors,n_errors2,n_alamouti;
  uint8_t transmission_mode = 1,n_tx=1,n_rx=1;
  uint16_t Nid_cell=0;

  int n_frames=1;
  channel_desc_t *eNB2UE,*eNB2UE1,*eNB2UE2;
  uint32_t nsymb,tx_lev,tx_lev1,tx_lev2;
  uint8_t extended_prefix_flag=0;
  LTE_DL_FRAME_PARMS *frame_parms;
#ifdef EMOS
  fifo_dump_emos emos_dump;
#endif

  FILE *input_fd=NULL,*pbch_file_fd=NULL;
  char input_val_str[50],input_val_str2[50];
  //  double input_val1,input_val2;
  //  uint16_t amask=0;
  uint8_t frame_mod4,num_pdcch_symbols;
  uint16_t NB_RB=25;

  SCM_t channel_model=AWGN;//Rayleigh1_anticorr;

  DCI_ALLOC_t dci_alloc[8];
  uint8_t abstraction_flag=0;//,calibration_flag=0;
  int pbch_tx_ant;
  uint8_t N_RB_DL=100,osf=1;

  unsigned char frame_type = FDD;
  unsigned char pbch_phase = 0;

#ifdef XFORMS
  FD_lte_phy_scope_ue *form_ue;
  char title[255];
#endif

  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, "f:hpf:g:n:s:S:t:x:y:z:N:F:GdP:")) != -1) {
    switch (c) {
    case 'f':
      write_output_file=1;
      output_fd = fopen(optarg,"w");

      if (output_fd==NULL) {
        printf("Error opening %s\n",optarg);
        exit(-1);
      }

      break;

    case 'd':
      frame_type = TDD;
      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:
        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 't':
        Td= atof(optarg);
        break;
      */
    case 'p':
      extended_prefix_flag=1;
      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");
      pbch_file_fd=fopen(optarg,"r");

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

      break;

      //  case 'C':
      //    calibration_flag=1;
      //    msg("Running Abstraction calibration for Bias removal\n");
      //    break;
    case 'N':
      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 'P':
      pbch_phase = atoi(optarg);

      if (pbch_phase>3)
        printf("Illegal PBCH phase (0-3) got %d\n",pbch_phase);

      break;

    default:
    case 'h':
      printf("%s -h(elp) -p(extended_prefix) -N cell_id -f output_filename -F input_filename -g channel_model -n n_frames -t Delayspread -s snr0 -S snr1 -x transmission_mode -y TXant -z RXant -N CellId\n",
             argv[0]);
      printf("-h This message\n");
      printf("-p Use extended prefix mode\n");
      printf("-d Use TDD\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("-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("-N Nid_cell\n");
      printf("-f Output filename (.txt format) for Pe/SNR results\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,frame_type,Nid_cell,N_RB_DL,osf);

#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

  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_eNb->lte_eNB_common_vars.txdata[0];
  txdata1 = PHY_vars_eNb1->lte_eNB_common_vars.txdata[0];
  txdata2 = PHY_vars_eNb2->lte_eNB_common_vars.txdata[0];


  s_re = malloc(2*sizeof(double*));
  s_im = malloc(2*sizeof(double*));
  s_re1 = malloc(2*sizeof(double*));
  s_im1 = malloc(2*sizeof(double*));
  s_re2 = malloc(2*sizeof(double*));
  s_im2 = malloc(2*sizeof(double*));
  r_re = malloc(2*sizeof(double*));
  r_im = malloc(2*sizeof(double*));
  r_re1 = malloc(2*sizeof(double*));
  r_im1 = malloc(2*sizeof(double*));
  r_re2 = malloc(2*sizeof(double*));
  r_im2 = 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);

  printf("PHY_vars_eNb1->lte_eNB_common_vars.txdataF[0][0] = %p\n",
         PHY_vars_eNb1->lte_eNB_common_vars.txdataF[0][0]);


  DLSCH_alloc_pdu2.rah              = 0;
  DLSCH_alloc_pdu2.rballoc          = DLSCH_RB_ALLOC;
  DLSCH_alloc_pdu2.TPC              = 0;
  DLSCH_alloc_pdu2.dai              = 0;
  DLSCH_alloc_pdu2.harq_pid         = 0;
  DLSCH_alloc_pdu2.tb_swap          = 0;
  DLSCH_alloc_pdu2.mcs1             = 0;
  DLSCH_alloc_pdu2.ndi1             = 1;
  DLSCH_alloc_pdu2.rv1              = 0;
  // Forget second codeword
  DLSCH_alloc_pdu2.tpmi             = (transmission_mode==6 ? 5 : 0) ;  // precoding

  eNB2UE = 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);


  if (eNB2UE==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));
    s_re1[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_re1[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im1[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_im1[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_re2[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_re2[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im2[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_im2[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));
    r_re1[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_re1[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im1[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_im1[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_re2[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_re2[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im2[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_im2[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
  }

  pbch_pdu[0]=100;
  pbch_pdu[1]=1;
  pbch_pdu[2]=0;

  if (PHY_vars_eNb->lte_frame_parms.frame_type == FDD) {
    generate_pss(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
                 AMP,
                 &PHY_vars_eNb->lte_frame_parms,
                 (PHY_vars_eNb->lte_frame_parms.Ncp==NORMAL) ? 6 : 5,
                 0);
    /*
    generate_sss(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
                 AMP,
                 &PHY_vars_eNb->lte_frame_parms,
                 (PHY_vars_eNb->lte_frame_parms.Ncp==0) ? 5 : 4,
                 0);*/
    generate_pss(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
                 AMP,
                 &PHY_vars_eNb->lte_frame_parms,
                 (PHY_vars_eNb->lte_frame_parms.Ncp==0) ? 6 : 5,
                 10);
    /*
    generate_sss(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
                 AMP,
                 &PHY_vars_eNb->lte_frame_parms,
                 (PHY_vars_eNb->lte_frame_parms.Ncp==0) ? 5 : 4,
                 10);
    */

  } else {

    generate_sss(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
                 AMP,
                 &PHY_vars_eNb->lte_frame_parms,
                 (PHY_vars_eNb->lte_frame_parms.Ncp==0) ? 6 : 5,
                 1);
    generate_pss(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
                 AMP,
                 &PHY_vars_eNb->lte_frame_parms,
                 2,
                 2);
    generate_sss(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
                 AMP,
                 &PHY_vars_eNb->lte_frame_parms,
                 (PHY_vars_eNb->lte_frame_parms.Ncp==0) ? 6 : 5,
                 11);
    generate_pss(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
                 AMP,
                 &PHY_vars_eNb->lte_frame_parms,
                 2,
                 12);


  }


  /*
  generate_pilots(PHY_vars_eNb,
      PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
      AMP,
      LTE_NUMBER_OF_SUBFRAMES_PER_FRAME);



     num_pdcch_symbols = generate_dci_top(1,
     0,
     dci_alloc,
     0,
     1024,
     &PHY_vars_eNb->lte_frame_parms,
     PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
     0);
  */

  /*
  if (num_pdcch_symbols<3) {
    printf("Less than 3 pdcch symbols\n");
    //  exit(-1);
  }

  if (pbch_phase>0) {
    dummybuf[0] = dummy0;
    dummybuf[1] = dummy1;
    dummybuf[2] = dummy2;
    dummybuf[3] = dummy3;
    generate_pbch(&PHY_vars_eNb->lte_eNB_pbch,
      (mod_sym_t**)dummybuf,
      AMP,
      &PHY_vars_eNb->lte_frame_parms,
      pbch_pdu,
      0);
  }

  generate_pbch(&PHY_vars_eNb->lte_eNB_pbch,
    PHY_vars_eNb->lte_eNB_common_vars.txdataF[0],
    AMP,
    &PHY_vars_eNb->lte_frame_parms,
    pbch_pdu,
    pbch_phase);
  */
  write_output("txsigF0.m","txsF0", PHY_vars_eNb->lte_eNB_common_vars.txdataF[0][0],FRAME_LENGTH_COMPLEX_SAMPLES_NO_PREFIX,1,1);

  if (PHY_vars_eNb->lte_frame_parms.nb_antennas_tx>1)
    write_output("txsigF1.m","txsF1", PHY_vars_eNb->lte_eNB_common_vars.txdataF[0][1],FRAME_LENGTH_COMPLEX_SAMPLES_NO_PREFIX,1,1);

  tx_lev = 0;
  tx_lev1 = 0;
  tx_lev2 = 0;




  for (aa=0; aa<PHY_vars_eNb->lte_frame_parms.nb_antennas_tx; aa++) {
    if (frame_parms->Ncp == 1)
      PHY_ofdm_mod(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0][aa],        // input,
                   txdata[aa],         // output
                   frame_parms->log2_symbol_size,                // log2_fft_size
                   LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*nsymb,                 // number of symbols
                   frame_parms->nb_prefix_samples,               // number of prefix samples
                   CYCLIC_PREFIX);
    else {
      normal_prefix_mod(PHY_vars_eNb->lte_eNB_common_vars.txdataF[0][aa],
                        txdata[aa],
                        LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*nsymb,
                        frame_parms);
    }

    tx_lev += signal_energy(&txdata[aa][frame_parms->samples_per_tti/2],
                            OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES);
  }


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

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

  // multipath channel

  for (i=0; i<2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; i++) {
    for (aa=0; aa<PHY_vars_eNb->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]);
    }
  }


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


    n_errors = 0;
    n_errors2 = 0;
    n_alamouti = 0;

    for (trial=0; trial<n_frames; trial++) {

      multipath_channel(eNB2UE,s_re,s_im,r_re,r_im,
                        2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES,0);

      sigma2_dB = 10*log10((double)tx_lev) +10*log10((double)PHY_vars_eNb->lte_frame_parms.ofdm_symbol_size/(double)(12*NB_RB)) - SNR;

      if (n_frames==1)
        printf("sigma2_dB %f (SNR %f dB) tx_lev_dB %f,%f,%f\n",sigma2_dB,SNR,
               10*log10((double)tx_lev),
               10*log10((double)tx_lev1),
               10*log10((double)tx_lev2));

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

      /*
      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 (n_trials=0; n_trials<ntrials; n_trials++) {
        //printf("n_trial %d\n",n_trials);
        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_UE->lte_ue_common_vars.rxdata[aa])[2*i] = (short) ((r_re[aa][i] +sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
            ((short*) PHY_vars_UE->lte_ue_common_vars.rxdata[aa])[2*i+1] = (short) ((r_im[aa][i] + (iqim*r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
          }
        }

        lte_sync_timefreq(PHY_vars_UE,0,2680000000);

        if (n_frames==1) {
          printf("rx_level data symbol %f\n",
                 10*log10(signal_energy(&PHY_vars_UE->lte_ue_common_vars.rxdata[0][frame_parms->samples_per_tti/2],4*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES)));
        }


      } //noise trials
    } // trials

    if (abstraction_flag==0) {
      printf("SNR %f : n_errors2 = %d/%d (BLER %e,40ms BLER %e,%d,%d), n_alamouti %d\n", SNR,n_errors2,ntrials*(1+trial),(double)n_errors2/(ntrials*(1+trial)),pow((double)n_errors2/(ntrials*(1+trial)),4),
             ntrials,trial,n_alamouti);

      if (write_output_file==1)
        fprintf(output_fd,"%f %e\n",SNR,(double)n_errors2/(ntrials*(1+trial)));
    }
  } // NSR

  if (n_frames==1) {

  }


  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();

  if (write_output_file)
    fclose(output_fd);

  return(n_errors);

}