Ejemplo n.º 1
0
int gauss(unsigned int *gauss_LUT,
          unsigned char Nbits
         )
{

  unsigned int search_pos,step_size,u,tmp,tmpm1,tmpp1,s;

  // Get a 32-bit uniform random-variable
  u = taus();

#ifdef DEBUG
  printf("u = %u\n",u);
#endif //DEBUG

  // if it is larger than 2^31 (here negative), save the sign and rescale down to 31-bits.

  s = u & 0x80000000;
  u &= 0x7fffffff;


#ifdef DEBUG
  printf("u = %x,s=%d\n",u,s);
#endif //DEBUG

  search_pos = (1<<(Nbits-2));   // starting position of the binary search
  step_size  = search_pos;

  do {

    step_size >>= 1;

    tmp = gauss_LUT[search_pos];
    tmpm1 = gauss_LUT[search_pos-1];
    tmpp1 = gauss_LUT[search_pos+1];
#ifdef DEBUG
    printf("search_pos %d, step_size %d: t %x tm %x,tp %x\n",search_pos,step_size,tmp,tmpm1,tmpp1);
#endif //DEBUG

    if (u <= tmp)
      if (u >tmpm1)
        return s==0 ? (search_pos-1) : 1-search_pos;
      else
        search_pos -= step_size;
    else if (u <= tmpp1)
      return s==0 ? search_pos : - search_pos;
    else
      search_pos += step_size;

  } while (step_size > 0);

  // If it gets here we're beyond the positive edge  so return max
  return s==0 ? (1<<(Nbits-1))-1 : 1-((1<<(Nbits-1)));

}
Ejemplo n.º 2
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);

}
Ejemplo n.º 3
0
int main(int argc, char* argv[])
{

  // Fill input vector with complex sinusoid

  int nb_antennas = 1;
  int length      = 100000;
  int i,j,n;
  double input_amp = pow(10.0,(.05*INPUT_dBm))/sqrt(2);
  double nf[2] = {3.0,3.0};
  double ip =0.0;
  double path_loss_dB = -90, rx_gain_dB = 125;
  double tx_pwr, rx_pwr;

  uint32_t **input = malloc(nb_antennas*sizeof(uint32_t*));
  uint32_t **output = malloc(nb_antennas*sizeof(uint32_t*));

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

  channel_desc_t *channel;

  srand(0);
  randominit(0);
  set_taus_seed(0);

  channel = new_channel_desc_scm(nb_antennas,
                                 nb_antennas,
                                 AWGN,
                                 7.68e6,
                                 0,
                                 0,
                                 path_loss_dB);

  for (i=0; i<nb_antennas; i++) {
    s_re[i] = (double *)malloc(length * sizeof (double ));
    s_im[i] = (double *)malloc(length * sizeof (double ));
    r_re[i] = (double *)malloc(length * sizeof (double ));
    r_im[i] = (double *)malloc(length * sizeof (double ));
    input[i] = (uint32_t*)malloc(length * sizeof(uint32_t));
    output[i] = (uint32_t*)malloc(length * sizeof(uint32_t));
  }

  for (i=0; i<nb_antennas; i++) {
    // generate a random QPSK signal
    for (j=0; j<length/2; j++) {
      input[i][j] = QPSK[taus()&3];
    }
  }

  tx_pwr = dac_fixed_gain(s_re,
                          s_im,
                          input,
                          0,
                          nb_antennas,
                          length,
                          0,
                          512,
                          14,
                          15.0);

  multipath_channel(channel,s_re,s_im,r_re,r_im,
                    length,0);

  rf_rx_simple(r_re,
               r_im,
               nb_antennas,
               length,
               1.0/7.68e6 * 1e9,// sampling time (ns)
               rx_gain_dB - 66.227);

  /*
  rf_rx(r_re,
  r_im,
  nb_antennas,
  length,
  1.0/6.5e6 * 1e9,// sampling time (ns)
  1000.0  ,          //freq offset (Hz)
  0.0,            //drift (Hz) NOT YET IMPLEMENTED
  nf,             //noise_figure NOT YET IMPLEMENTED
  -INPUT_dBm,           //rx_gain (dB)
  200,            // IP3_dBm (dBm)
  &ip,            // initial phase
  30.0e3,         // pn_cutoff (kHz)
  -500.0,          // pn_amp (dBc)
  -0.0,              // IQ imbalance (dB),
  0.0);           // IQ phase imbalance (rad)
  */

  adc(r_re,
      r_im,
      0,
      0,
      output,
      nb_antennas,
      length,
      12);

  write_output("s_im.m","s_im",s_im[0],length,1,7);
  write_output("s_re.m","s_re",s_re[0],length,1,7);
  write_output("r_im.m","r_im",r_im[0],length,1,7);
  write_output("r_re.m","r_re",r_re[0],length,1,7);
  write_output("input.m","rfin",input[0],length,1,1);
  write_output("output.m","rfout",output[0],length,1,1);
}
Ejemplo n.º 4
0
int main(int argc, char **argv) {

  char c;

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

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


  SCM_t channel_model=Rayleigh1;

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

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

  if (transmission_mode==2)
    n_tx=2;

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


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

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

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

  frame_parms = &PHY_vars_eNB->lte_frame_parms;


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

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


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

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

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

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

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


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

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

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

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

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

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

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

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

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

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



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

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

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

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

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

  return(0);

}
Ejemplo n.º 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);

  }
Ejemplo n.º 6
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);
}
Ejemplo n.º 7
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);

}
Ejemplo n.º 8
0
Archivo: dlsim.c Proyecto: a4a881d4/oai
int main(int argc, char **argv) {

  char c;
  int i,aa,s,ind,Kr,Kr_bytes;;
  double sigma2, sigma2_dB=10,SNR,snr0=-2.0,snr1,SNRmeas;
  //int **txdataF, **txdata;
  int **txdata;
#ifdef IFFT_FPGA
  int **txdataF2;
#endif
  //LTE_DL_FRAME_PARMS *frame_parms = (LTE_DL_FRAME_PARMS *)malloc(sizeof(LTE_DL_FRAME_PARMS));
  //LTE_UE_COMMON      *lte_ue_common_vars = (LTE_UE_COMMON *)malloc(sizeof(LTE_UE_COMMON));
  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; //0.0000005;
  int channel_length;
  struct complex **ch;

  int eNb_id = 0, eNb_id_i = 1;
  unsigned char mcs,dual_stream_UE = 0;
  unsigned short NB_RB=conv_nprb(0,DLSCH_RB_ALLOC);
  unsigned char Ns,l,m;


  unsigned char *input_data,*decoded_output;

  unsigned char *input_buffer;
  unsigned short input_buffer_length;
  unsigned int ret;
  unsigned int coded_bits_per_codeword,nsymb,dci_cnt;

  unsigned int tx_lev,tx_lev_dB,trials,errs=0,dci_errors=0,dlsch_active=0,num_layers;
  int re_allocated;
  FILE *bler_fd;
  FILE *csv_fd;
  char bler_fname[20];
  char csv_fname[20];

  unsigned char pbch_pdu[6];

  DCI_ALLOC_t dci_alloc[8],dci_alloc_rx[8];

  FILE *rx_frame_file;
  int result;

  int n_frames;
  int cnt=0; 
  int rx_lev_data_sym;
  int rx_lev_null_sym;
  int rx_snr_dB;
  void *data;
  int ii;
  int bler;
  double blerr;
  int ch_realization;
  channel_length = (int) 11+2*BW*Td;

  lte_param_init(1,1,1);

  num_layers = 1;
  //int cont=0;
  // default parameters
  //for (cont =0;cont<29;cont++){

  mcs = 0;
  n_frames = 1000;
  snr0 = 2;
  //if(snr0>0)
  // snr0 = 0;
  while ((c = getopt (argc, argv, "hm:n:s:")) != -1)
    {
      switch (c)
	{
	case 'h':
	  printf("%s -h(elp) -m mcs -n n_frames -s snr0\n",argv[0]);
	  exit(1);
	case 'm':
	  mcs = atoi(optarg);
	  break;
	case 'n':
	  n_frames = atoi(optarg);
	  break;
	case 's':
	  snr0 = atoi(optarg);
	  break;
	default:
	  printf("%s -h(elp) -m mcs -n n_frames -s snr0\n",argv[0]);
	  exit (-1);
	  break;
	}
    }
  
  printf("Setting mcs = %d\n",mcs);
  printf("NPRB = %d\n",NB_RB);
  printf("n_frames = %d\n",n_frames);

  /*
      snr0 = -7 + mcs;
      if(snr0>0)
      snr0 = ;
  */

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

  /*
    txdataF    = (int **)malloc16(2*sizeof(int*));
    txdataF[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
    txdataF[1] = (int *)malloc16(FRAME_LENGTH_BYTES);
  
    txdata    = (int **)malloc16(2*sizeof(int*));
    txdata[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
    txdata[1] = (int *)malloc16(FRAME_LENGTH_BYTES);
  */


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

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

  bzero(txdataF2[0],FRAME_LENGTH_BYTES_NO_PREFIX);
  bzero(txdataF2[1],FRAME_LENGTH_BYTES_NO_PREFIX);
#else
  txdata = PHY_vars_eNb->lte_eNB_common_vars.txdata[eNb_id];
#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*));

  nsymb = (lte_frame_parms->Ncp == 0) ? 14 : 12;

  coded_bits_per_codeword = NB_RB * (12 * get_Qm(mcs)) * (lte_frame_parms->num_dlsch_symbols);
  printf("Rate = %f (mod %d)\n",(((double)dlsch_tbs25[get_I_TBS(mcs)][NB_RB-1])*3/4)/coded_bits_per_codeword,
	 get_Qm(mcs));
  sprintf(bler_fname,"bler_%d.m",mcs);
  bler_fd = fopen(bler_fname,"w");
  fprintf(bler_fd,"bler = [");
  // CSV file 
  sprintf(csv_fname,"data_out%d.m",mcs);
  csv_fd = fopen(csv_fname,"w");
  fprintf(csv_fd,"data_all=[");

  for (i=0;i<2;i++) {
    s_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
  }


  PHY_vars_UE->lte_ue_pdcch_vars[0]->crnti = 0x1234;

  // Fill in UL_alloc
  UL_alloc_pdu.type    = 0;
  UL_alloc_pdu.hopping = 0;
  UL_alloc_pdu.rballoc = UL_RB_ALLOC;
  UL_alloc_pdu.mcs     = 1;
  UL_alloc_pdu.ndi     = 1;
  UL_alloc_pdu.TPC     = 0;
  UL_alloc_pdu.cqi_req = 1;

  CCCH_alloc_pdu.type               = 0;
  CCCH_alloc_pdu.vrb_type           = 0;
  CCCH_alloc_pdu.rballoc            = CCCH_RB_ALLOC;
  CCCH_alloc_pdu.ndi      = 1;
  CCCH_alloc_pdu.mcs      = 1;
  CCCH_alloc_pdu.harq_pid = 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             = mcs;  
  DLSCH_alloc_pdu2.ndi1             = 1;
  DLSCH_alloc_pdu2.rv1              = 0;
  // Forget second codeword
  DLSCH_alloc_pdu2.tpmi             = 5 ;  // precoding

  // Create transport channel structures for SI pdus
  PHY_vars_eNb->dlsch_eNb_cntl = new_eNb_dlsch(1,1);
  PHY_vars_UE->dlsch_ue_cntl  = new_ue_dlsch(1,1);

  
  // Create transport channel structures for 2 transport blocks (MIMO)
  PHY_vars_eNb->dlsch_eNb = (LTE_eNb_DLSCH_t**) malloc16(2*sizeof(LTE_eNb_DLSCH_t*));
  PHY_vars_UE->dlsch_ue = (LTE_UE_DLSCH_t**) malloc16(2*sizeof(LTE_UE_DLSCH_t*));
  for (i=0;i<2;i++) {
    PHY_vars_eNb->dlsch_eNb[i] = new_eNb_dlsch(1,8);
    PHY_vars_UE->dlsch_ue[i]  = new_ue_dlsch(1,8);
  
    if (!PHY_vars_eNb->dlsch_eNb[i]) {
      printf("Can't get eNb dlsch structures\n");
      exit(-1);
    }
    
    if (!PHY_vars_UE->dlsch_ue[i]) {
      printf("Can't get ue dlsch structures\n");
      exit(-1);
    }
  }
  

  if (DLSCH_alloc_pdu2.tpmi == 5) {
    PHY_vars_eNb->dlsch_eNb[0]->pmi_alloc = (unsigned short)(taus()&0xffff);
    PHY_vars_UE->dlsch_ue[0]->pmi_alloc = PHY_vars_eNb->dlsch_eNb[0]->pmi_alloc;
    PHY_vars_eNb->eNB_UE_stats[0].DL_pmi_single[0] = PHY_vars_eNb->dlsch_eNb[0]->pmi_alloc;
  }
  
  generate_eNb_dlsch_params_from_dci(0,
                                     &DLSCH_alloc_pdu2,
				     0x1234,
				     format2_2A_M10PRB,
				     PHY_vars_eNb->dlsch_eNb,
				     lte_frame_parms,
				     SI_RNTI,
				     RA_RNTI,
				     P_RNTI,
				     0); //change this later

				     
  /*
    generate_eNb_dlsch_params_from_dci(0,
    &CCCH_alloc_pdu,
    SI_RNTI,
    format1A,
    &dlsch_eNb_cntl,
    lte_frame_parms,
    SI_RNTI,
    RA_RNTI,
    P_RNTI);
  */
  
  
  //  input_data     = (unsigned char*) malloc(block_length/8);
  //  decoded_output = (unsigned char*) malloc(block_length/8);

  // DCI
  
  memcpy(&dci_alloc[0].dci_pdu[0],&DLSCH_alloc_pdu2,sizeof(DCI2_5MHz_2A_M10PRB_TDD_t));
  dci_alloc[0].dci_length = sizeof_DCI2_5MHz_2A_M10PRB_TDD_t;
  dci_alloc[0].L          = 3;
  dci_alloc[0].rnti       = 0x1234;
  /*
    memcpy(&dci_alloc[0].dci_pdu[0],&CCCH_alloc_pdu,sizeof(DCI1A_5MHz_TDD_1_6_t));
    dci_alloc[0].dci_length = sizeof_DCI1A_5MHz_TDD_1_6_t;
    dci_alloc[0].L          = 3;
    dci_alloc[0].rnti       = SI_RNTI;
  */

  memcpy(&dci_alloc[1].dci_pdu[0],&UL_alloc_pdu,sizeof(DCI0_5MHz_TDD0_t));
  dci_alloc[1].dci_length = sizeof_DCI0_5MHz_TDD_0_t;
  dci_alloc[1].L          = 3;
  dci_alloc[1].rnti       = 0x1234;




  // DLSCH
  if (1) {
    input_buffer_length = PHY_vars_eNb->dlsch_eNb[0]->harq_processes[0]->TBS/8;
    
    printf("dlsch0: TBS      %d\n",PHY_vars_eNb->dlsch_eNb[0]->harq_processes[0]->TBS);
    
    printf("Input buffer size %d bytes\n",input_buffer_length);
    
    
    input_buffer = (unsigned char *)malloc(input_buffer_length+4);
    
    for (i=0;i<input_buffer_length;i++)
      input_buffer[i]= (unsigned char)(taus()&0xff);
    
    dlsch_encoding(input_buffer,
		   lte_frame_parms,
		   PHY_vars_eNb->dlsch_eNb[0]);
    
#ifdef OUTPUT_DEBUG
    for (s=0;s<PHY_vars_eNb->dlsch_eNb[0]->harq_processes[0]->C;s++) {
      if (s<PHY_vars_eNb->dlsch_eNb[0]->harq_processes[0]->Cminus)
	Kr = PHY_vars_eNb->dlsch_eNb[0]->harq_processes[0]->Kminus;
      else
	Kr = PHY_vars_eNb->dlsch_eNb[0]->harq_processes[0]->Kplus;
      
      Kr_bytes = Kr>>3;
      
      for (i=0;i<Kr_bytes;i++)
	printf("%d : (%x)\n",i,PHY_vars_eNb->dlsch_eNb[0]->harq_processes[0]->c[s][i]);
    }
    
#endif 

    re_allocated = dlsch_modulation(PHY_vars_eNb->lte_eNB_common_vars.txdataF[eNb_id],
				    1024,
				    0,
				    &PHY_vars_eNb->lte_frame_parms,
				    PHY_vars_eNb->dlsch_eNb[0]);
    
    
    printf("RB count %d (%d,%d)\n",re_allocated,re_allocated/lte_frame_parms->num_dlsch_symbols/12,lte_frame_parms->num_dlsch_symbols);
    
    
      
    if (num_layers>1)
      re_allocated = dlsch_modulation(PHY_vars_eNb->lte_eNB_common_vars.txdataF[eNb_id],
				      1024,
				      0,
				      &PHY_vars_eNb->lte_frame_parms,
				      PHY_vars_eNb->dlsch_eNb[1]);
  }