ulsim.c 64.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
21 22

/*! \file ulsim.c
Xiwen JIANG's avatar
Xiwen JIANG committed
23
 \brief Top-level UL simulator
24
 \author R. Knopp
25
 \date 2011 - 2014
26 27 28 29 30 31 32
 \version 0.1
 \company Eurecom
 \email: knopp@eurecom.fr
 \note
 \warning
*/

33 34 35 36 37 38 39 40 41 42 43 44 45
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "SIMULATION/TOOLS/defs.h"
#include "PHY/types.h"
#include "PHY/defs.h"
#include "PHY/vars.h"

#include "SCHED/defs.h"
#include "SCHED/vars.h"
#include "LAYER2/MAC/vars.h"
#include "OCG_vars.h"

46 47
#include "unitary_defs.h"

48 49
#include "PHY/TOOLS/lte_phy_scope.h"

50 51 52 53
PHY_VARS_eNB *eNB;
PHY_VARS_UE *UE;


Florian Kaltenberger's avatar
Florian Kaltenberger committed
54

55

Florian Kaltenberger's avatar
Florian Kaltenberger committed
56
//#define MCS_COUNT 23//added for PHY abstraction
57 58 59 60

channel_desc_t *eNB2UE[NUMBER_OF_eNB_MAX][NUMBER_OF_UE_MAX];
channel_desc_t *UE2eNB[NUMBER_OF_UE_MAX][NUMBER_OF_eNB_MAX];
//Added for PHY abstraction
61
node_desc_t *enb_data[NUMBER_OF_eNB_MAX];
62 63 64
node_desc_t *ue_data[NUMBER_OF_UE_MAX];
//double sinr_bler_map[MCS_COUNT][2][16];

65
extern uint16_t beta_ack[16],beta_ri[16],beta_cqi[16];
66 67
//extern  char* namepointer_chMag ;

68
int xforms=0;
69 70 71
FD_lte_phy_scope_enb *form_enb;
char title[255];

72 73 74 75 76 77 78 79
/*the following parameters are used to control the processing times*/
double t_tx_max = -1000000000; /*!< \brief initial max process time for tx */
double t_rx_max = -1000000000; /*!< \brief initial max process time for rx */
double t_tx_min = 1000000000; /*!< \brief initial min process time for tx */
double t_rx_min = 1000000000; /*!< \brief initial min process time for tx */
int n_tx_dropped = 0; /*!< \brief initial max process time for tx */
int n_rx_dropped = 0; /*!< \brief initial max process time for rx */

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

void fill_ulsch_dci(PHY_VARS_eNB *eNB,void *UL_dci,int first_rb,int nb_rb,int mcs,int ndi,int cqi_flag) {

  switch (eNB->frame_parms.N_RB_UL) {
  case 6:
    break;

  case 25:
    if (eNB->frame_parms.frame_type == TDD) {
      ((DCI0_5MHz_TDD_1_6_t*)UL_dci)->type    = 0;
      ((DCI0_5MHz_TDD_1_6_t*)UL_dci)->rballoc = computeRIV(eNB->frame_parms.N_RB_UL,first_rb,nb_rb);// 12 RBs from position 8
      //printf("nb_rb %d/%d, rballoc %d (dci %x)\n",nb_rb,eNB->frame_parms.N_RB_UL,((DCI0_5MHz_TDD_1_6_t*)UL_dci)->rballoc,*(uint32_t *)UL_dci);
      ((DCI0_5MHz_TDD_1_6_t*)UL_dci)->mcs     = mcs;
      ((DCI0_5MHz_TDD_1_6_t*)UL_dci)->ndi     = ndi;
      ((DCI0_5MHz_TDD_1_6_t*)UL_dci)->TPC     = 0;
      ((DCI0_5MHz_TDD_1_6_t*)UL_dci)->cqi_req = cqi_flag&1;
      ((DCI0_5MHz_TDD_1_6_t*)UL_dci)->cshift  = 0;
      ((DCI0_5MHz_TDD_1_6_t*)UL_dci)->dai     = 1;
    } else {
      ((DCI0_5MHz_FDD_t*)UL_dci)->type    = 0;
      ((DCI0_5MHz_FDD_t*)UL_dci)->rballoc = computeRIV(eNB->frame_parms.N_RB_UL,first_rb,nb_rb);// 12 RBs from position 8
      //      printf("nb_rb %d/%d, rballoc %d (dci %x) (dcip %p)\n",nb_rb,eNB->frame_parms.N_RB_UL,((DCI0_5MHz_FDD_t*)UL_dci)->rballoc,*(uint32_t *)UL_dci,UL_dci);
      ((DCI0_5MHz_FDD_t*)UL_dci)->mcs     = mcs;
      ((DCI0_5MHz_FDD_t*)UL_dci)->ndi     = ndi;
      ((DCI0_5MHz_FDD_t*)UL_dci)->TPC     = 0;
      ((DCI0_5MHz_FDD_t*)UL_dci)->cqi_req = cqi_flag&1;
      ((DCI0_5MHz_FDD_t*)UL_dci)->cshift  = 0;
    }

    break;

  case 50:
    if (eNB->frame_parms.frame_type == TDD) {
      ((DCI0_10MHz_TDD_1_6_t*)UL_dci)->type    = 0;
      ((DCI0_10MHz_TDD_1_6_t*)UL_dci)->rballoc = computeRIV(eNB->frame_parms.N_RB_UL,first_rb,nb_rb);// 12 RBs from position 8
      //      printf("nb_rb %d/%d, rballoc %d (dci %x)\n",nb_rb,eNB->frame_parms.N_RB_UL,((DCI0_10MHz_TDD_1_6_t*)UL_dci)->rballoc,*(uint32_t *)UL_dci);
      ((DCI0_10MHz_TDD_1_6_t*)UL_dci)->mcs     = mcs;
      ((DCI0_10MHz_TDD_1_6_t*)UL_dci)->ndi     = ndi;
      ((DCI0_10MHz_TDD_1_6_t*)UL_dci)->TPC     = 0;
      ((DCI0_10MHz_TDD_1_6_t*)UL_dci)->cqi_req = cqi_flag&1;
      ((DCI0_10MHz_TDD_1_6_t*)UL_dci)->cshift  = 0;
      ((DCI0_10MHz_TDD_1_6_t*)UL_dci)->dai     = 1;
    } else {
      ((DCI0_10MHz_FDD_t*)UL_dci)->type    = 0;
      ((DCI0_10MHz_FDD_t*)UL_dci)->rballoc = computeRIV(eNB->frame_parms.N_RB_UL,first_rb,nb_rb);// 12 RBs from position 8
      //printf("nb_rb %d/%d, rballoc %d (dci %x)\n",nb_rb,eNB->frame_parms.N_RB_UL,((DCI0_10MHz_FDD_t*)UL_dci)->rballoc,*(uint32_t *)UL_dci);
      ((DCI0_10MHz_FDD_t*)UL_dci)->mcs     = mcs;
      ((DCI0_10MHz_FDD_t*)UL_dci)->ndi     = ndi;
      ((DCI0_10MHz_FDD_t*)UL_dci)->TPC     = 0;
      ((DCI0_10MHz_FDD_t*)UL_dci)->cqi_req = cqi_flag&1;
      ((DCI0_10MHz_FDD_t*)UL_dci)->cshift  = 0;
    }

    break;

  case 100:
    if (eNB->frame_parms.frame_type == TDD) {
      ((DCI0_20MHz_TDD_1_6_t*)UL_dci)->type    = 0;
      ((DCI0_20MHz_TDD_1_6_t*)UL_dci)->rballoc = computeRIV(eNB->frame_parms.N_RB_UL,first_rb,nb_rb);// 12 RBs from position 8
      //      printf("nb_rb %d/%d, rballoc %d (dci %x)\n",nb_rb,eNB->frame_parms.N_RB_UL,((DCI0_20MHz_TDD_1_6_t*)UL_dci)->rballoc,*(uint32_t *)UL_dci);
      ((DCI0_20MHz_TDD_1_6_t*)UL_dci)->mcs     = mcs;
      ((DCI0_20MHz_TDD_1_6_t*)UL_dci)->ndi     = ndi;
      ((DCI0_20MHz_TDD_1_6_t*)UL_dci)->TPC     = 0;
      ((DCI0_20MHz_TDD_1_6_t*)UL_dci)->cqi_req = cqi_flag&1;
      ((DCI0_20MHz_TDD_1_6_t*)UL_dci)->cshift  = 0;
      ((DCI0_20MHz_TDD_1_6_t*)UL_dci)->dai     = 1;
    } else {
      ((DCI0_20MHz_FDD_t*)UL_dci)->type    = 0;
      ((DCI0_20MHz_FDD_t*)UL_dci)->rballoc = computeRIV(eNB->frame_parms.N_RB_UL,first_rb,nb_rb);// 12 RBs from position 8
      //   printf("nb_rb %d/%d, rballoc %d (dci %x) (UL_dci %p)\n",nb_rb,eNB->frame_parms.N_RB_UL,((DCI0_20MHz_FDD_t*)UL_dci)->rballoc,*(uint32_t *)UL_dci,(void*)UL_dci);
      ((DCI0_20MHz_FDD_t*)UL_dci)->mcs     = mcs;
      ((DCI0_20MHz_FDD_t*)UL_dci)->ndi     = ndi;
      ((DCI0_20MHz_FDD_t*)UL_dci)->TPC     = 0;
      ((DCI0_20MHz_FDD_t*)UL_dci)->cqi_req = cqi_flag&1;
      ((DCI0_20MHz_FDD_t*)UL_dci)->cshift  = 0;
    }

    break;

  default:
    break;
  }

}

extern void eNB_fep_full(PHY_VARS_eNB *eNB);
166
extern void eNB_fep_full_2thread(PHY_VARS_eNB *eNB);
167

168 169
int main(int argc, char **argv)
{
170 171 172 173 174 175

  char c;
  int i,j,aa,u;

  int aarx,aatx;
  double channelx,channely;
176
  double sigma2, sigma2_dB=10,SNR,SNR2,snr0=-2.0,snr1,SNRmeas,rate,saving_bler=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
177
  double input_snr_step=.2,snr_int=30;
178 179 180 181 182
  double blerr;

  int **txdata;

  LTE_DL_FRAME_PARMS *frame_parms;
183 184 185 186 187 188
  double s_re0[30720],s_im0[30720],r_re0[30720],r_im0[30720];
  double s_re1[30720],s_im1[30720],r_re1[30720],r_im1[30720];
  double *s_re[2]={s_re0,s_re1};
  double *s_im[2]={s_im0,s_im1};
  double *r_re[2]={r_re0,r_re1};
  double *r_im[2]={r_im0,r_im1};
189 190
  double forgetting_factor=0.0; //in [0,1] 0 means a new channel every time, 1 means keep the same channel
  double iqim=0.0;
191
  uint8_t extended_prefix_flag=0;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  int cqi_flag=0,cqi_error,cqi_errors,ack_errors,cqi_crc_falsepositives,cqi_crc_falsenegatives;
  int ch_realization;
  int eNB_id = 0;
  int chMod = 0 ;
  int UE_id = 0;
  unsigned char nb_rb=25,first_rb=0,mcs=0,round=0,bundling_flag=1;
  unsigned char l;

  unsigned char awgn_flag = 0 ;
  SCM_t channel_model=Rice1;


  unsigned char *input_buffer,harq_pid;
  unsigned short input_buffer_length;
  unsigned int ret;
  unsigned int coded_bits_per_codeword,nsymb;
  int subframe=3;
209
  unsigned int tx_lev=0,tx_lev_dB,trials,errs[4]= {0,0,0,0},round_trials[4]= {0,0,0,0};
210
  uint8_t transmission_mode=1,n_rx=1;
211

212
  FILE *bler_fd=NULL;
213
  char bler_fname[512];
214

215 216
  FILE *time_meas_fd=NULL;
  char time_meas_fname[256];
217

218 219 220
  FILE *input_fdUL=NULL,*trch_out_fdUL=NULL;
  //  unsigned char input_file=0;
  char input_val_str[50],input_val_str2[50];
221

222
  //  FILE *rx_frame_file;
223
  FILE *csv_fdUL=NULL;
224

225
  /*
226
  FILE *fperen=NULL;
227 228
  char fperen_name[512];

229
  FILE *fmageren=NULL;
230
  char fmageren_name[512];
231

232
  FILE *flogeren=NULL;
233
  char flogeren_name[512];
234
  */
235 236 237 238

  /* FILE *ftxlev;
     char ftxlev_name[512];
  */
239

240 241 242
  char csv_fname[512];
  int n_frames=5000;
  int n_ch_rlz = 1;
243 244
  int abstx = 0;
  int hold_channel=0;
245 246
  channel_desc_t *UE2eNB;

247
  uint8_t control_only_flag = 0;
248 249
  int delay = 0;
  double maxDoppler = 0.0;
250
  uint8_t srs_flag = 0;
251

252
  uint8_t N_RB_DL=25,osf=1;
253

254 255 256 257
  uint8_t cyclic_shift = 0;
  uint8_t cooperation_flag = 0; //0 no cooperation, 1 delay diversity, 2 Alamouti
  uint8_t beta_ACK=0,beta_RI=0,beta_CQI=2;
  uint8_t tdd_config=3,frame_type=FDD;
258

259
  uint8_t N0=30;
260 261 262 263 264 265 266
  double tx_gain=1.0;
  double cpu_freq_GHz;
  int avg_iter,iter_trials;

  uint32_t UL_alloc_pdu;
  int s,Kr,Kr_bytes;
  int dump_perf=0;
267
  int test_perf=0;
268 269
  int dump_table =0;

270 271
  double effective_rate=0.0;
  char channel_model_input[10];
272

273
  uint8_t max_turbo_iterations=4;
274
  uint8_t parallel_flag=0;
275
  int nb_rb_set = 0;
276

277
  int threequarter_fs=0;
278 279
  int ndi;

Raymond Knopp's avatar
 
Raymond Knopp committed
280 281
  opp_enabled=1; // to enable the time meas

282
  cpu_freq_GHz = (double)get_cpu_freq_GHz();
283 284 285 286 287 288

  printf("Detected cpu_freq %f GHz\n",cpu_freq_GHz);


  logInit();

289
  while ((c = getopt (argc, argv, "hapZEbm:n:Y:X:x:s:w:e:q:d:D:O:c:r:i:f:y:c:oA:C:R:g:N:l:S:T:QB:PI:LF")) != -1) {
290 291 292 293 294
    switch (c) {
    case 'a':
      channel_model = AWGN;
      chMod = 1;
      break;
295

296 297 298
    case 'b':
      bundling_flag = 0;
      break;
299

300 301 302
    case 'd':
      delay = atoi(optarg);
      break;
303

304 305 306
    case 'D':
      maxDoppler = atoi(optarg);
      break;
307

308 309 310
    case 'm':
      mcs = atoi(optarg);
      break;
311

312 313 314
    case 'n':
      n_frames = atoi(optarg);
      break;
315

316 317
    case 'Y':
      n_ch_rlz = atoi(optarg);
318 319
      break;

320 321
    case 'X':
      abstx= atoi(optarg);
322 323
      break;

324
    case 'g':
325
      sprintf(channel_model_input,optarg,10);
326

327
      switch((char)*optarg) {
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
      case 'A':
        channel_model=SCM_A;
        chMod = 2;
        break;

      case 'B':
        channel_model=SCM_B;
        chMod = 3;
        break;

      case 'C':
        channel_model=SCM_C;
        chMod = 4;
        break;

      case 'D':
        channel_model=SCM_D;
        chMod = 5;
        break;

      case 'E':
        channel_model=EPA;
        chMod = 6;
        break;

      case 'F':
        channel_model=EVA;
        chMod = 7;
        break;

      case 'G':
        channel_model=ETU;
        chMod = 8;
        break;

363
      case 'H':
364 365 366 367
        channel_model=Rayleigh8;
        chMod = 9;
        break;

368
      case 'I':
369 370 371 372
        channel_model=Rayleigh1;
        chMod = 10;
        break;

373
      case 'J':
374 375 376 377
        channel_model=Rayleigh1_corr;
        chMod = 11;
        break;

378
      case 'K':
379 380 381 382
        channel_model=Rayleigh1_anticorr;
        chMod = 12;
        break;

383
      case 'L':
384 385 386 387
        channel_model=Rice8;
        chMod = 13;
        break;

388
      case 'M':
389 390 391 392
        channel_model=Rice1;
        chMod = 14;
        break;

393
      case 'N':
394 395 396 397
        channel_model=AWGN;
        chMod = 1;
        break;

398
      default:
399
        printf("Unsupported channel model!\n");
400 401
        exit(-1);
        break;
402
      }
403

404
      break;
405

406
    case 's':
Raymond Knopp's avatar
 
Raymond Knopp committed
407 408
      snr0 = atof(optarg);
      break;
409

Raymond Knopp's avatar
 
Raymond Knopp committed
410 411 412
    case 'w':
      snr_int = atof(optarg);
      break;
413

Raymond Knopp's avatar
 
Raymond Knopp committed
414 415
    case 'e':
      input_snr_step= atof(optarg);
416
      break;
417

418 419
    case 'x':
      transmission_mode=atoi(optarg);
420

421
      if ((transmission_mode!=1) &&
422
          (transmission_mode!=2)) {
423
        printf("Unsupported transmission mode %d\n",transmission_mode);
424
        exit(-1);
425
      }
426

427
      break;
428

429 430 431
    case 'y':
      n_rx = atoi(optarg);
      break;
432

433 434 435
    case 'S':
      subframe = atoi(optarg);
      break;
436

437 438 439 440
    case 'T':
      tdd_config=atoi(optarg);
      frame_type=TDD;
      break;
441

442 443 444
    case 'p':
      extended_prefix_flag=1;
      break;
445

446 447
    case 'r':
      nb_rb = atoi(optarg);
448
      nb_rb_set = 1;
449
      break;
450

451 452 453
    case 'f':
      first_rb = atoi(optarg);
      break;
454

455 456 457
    case 'c':
      cyclic_shift = atoi(optarg);
      break;
458

459 460 461 462
    case 'E':
      threequarter_fs=1;
      break;

463 464 465
    case 'N':
      N0 = atoi(optarg);
      break;
466

467 468 469 470 471 472
    case 'o':
      srs_flag = 1;
      break;

    case 'i':
      input_fdUL = fopen(optarg,"r");
473
      printf("Reading in %s (%p)\n",optarg,input_fdUL);
474

475
      if (input_fdUL == (FILE*)NULL) {
476
        printf("Unknown file %s\n",optarg);
477
        exit(-1);
478
      }
479

480 481
      //      input_file=1;
      break;
482

483 484
    case 'A':
      beta_ACK = atoi(optarg);
485

486
      if (beta_ACK>15) {
487 488
        printf("beta_ack must be in (0..15)\n");
        exit(-1);
489
      }
490

491
      break;
492

493 494
    case 'C':
      beta_CQI = atoi(optarg);
495

496
      if ((beta_CQI>15)||(beta_CQI<2)) {
497 498
        printf("beta_cqi must be in (2..15)\n");
        exit(-1);
499
      }
500

501
      break;
502

503 504
    case 'R':
      beta_RI = atoi(optarg);
505

506
      if ((beta_RI>15)||(beta_RI<2)) {
507 508
        printf("beta_ri must be in (0..13)\n");
        exit(-1);
509
      }
510

511
      break;
512

513 514 515
    case 'Q':
      cqi_flag=1;
      break;
516

517 518 519
    case 'B':
      N_RB_DL=atoi(optarg);
      break;
520

521 522
    case 'P':
      dump_perf=1;
523
      opp_enabled=1;
524
      break;
525

526 527 528 529
    case 'O':
      test_perf=atoi(optarg);
      //print_perf =1;
      break;
530

531
    case 'L':
532
      parallel_flag=1;
533
      break;
534

535 536 537
    case 'I':
      max_turbo_iterations=atoi(optarg);
      break;
538

539 540 541 542
    case 'F':
      xforms=1;
      break;

543 544 545
    case 'Z':
      dump_table = 1;
      break;
546

547 548
    case 'h':
    default:
549 550
      printf("%s -h(elp) -a(wgn on) -m mcs -n n_frames -s snr0 -t delay_spread -p (extended prefix on) -r nb_rb -f first_rb -c cyclic_shift -o (srs on) -g channel_model [A:M] Use 3GPP 25.814 SCM-A/B/C/D('A','B','C','D') or 36-101 EPA('E'), EVA ('F'),ETU('G') models (ignores delay spread and Ricean factor), Rayghleigh8 ('H'), Rayleigh1('I'), Rayleigh1_corr('J'), Rayleigh1_anticorr ('K'), Rice8('L'), Rice1('M'), -d Channel delay, -D maximum Doppler shift \n",
             argv[0]);
551 552 553 554
      exit(1);
      break;
    }
  }
555

556
  lte_param_init(1,
Xiwen JIANG's avatar
Xiwen JIANG committed
557
                 1, 
558 559 560 561 562 563 564
		 n_rx,
		 1,
		 extended_prefix_flag,
		 frame_type,
		 0,
		 tdd_config,
		 N_RB_DL,
565
		 threequarter_fs,
566 567
		 osf,
		 0);
568

569
  if (nb_rb_set == 0)
570
    nb_rb = eNB->frame_parms.N_RB_UL;
571

572
  printf("1 . rxdataF_comp[0] %p\n",eNB->pusch_vars[0]->rxdataF_comp[0][0]);
573
  printf("Setting mcs = %d\n",mcs);
574
  printf("n_frames = %d\n", n_frames);
575

Raymond Knopp's avatar
 
Raymond Knopp committed
576
  snr1 = snr0+snr_int;
577 578
  printf("SNR0 %f, SNR1 %f\n",snr0,snr1);

579
  frame_parms = &eNB->frame_parms;
580

581
  txdata = UE->common_vars.txdata;
582

583

584

585
  nsymb = (eNB->frame_parms.Ncp == NORMAL) ? 14 : 12;
586 587


588
  sprintf(bler_fname,"ULbler_mcs%d_nrb%d_ChannelModel%d_nsim%d.csv",mcs,nb_rb,chMod,n_frames);
589
  bler_fd = fopen(bler_fname,"w");
590 591 592 593
  if (bler_fd==NULL) {
    fprintf(stderr,"Problem creating file %s\n",bler_fname);
    exit(-1);
  }
594

595
  fprintf(bler_fd,"#SNR;mcs;nb_rb;TBS;rate;errors[0];trials[0];errors[1];trials[1];errors[2];trials[2];errors[3];trials[3]\n");
596 597

  if (test_perf != 0) {
598 599 600 601
    char hostname[1024];
    hostname[1023] = '\0';
    gethostname(hostname, 1023);
    printf("Hostname: %s\n", hostname);
602 603 604 605
    //char dirname[FILENAME_MAX];
    //sprintf(dirname, "%s//SIMU/USER/pre-ci-logs-%s", getenv("OPENAIR_TARGETS"),hostname);
    //mkdir(dirname, 0777);
    sprintf(time_meas_fname,"time_meas_prb%d_mcs%d_antrx%d_channel%s_tx%d.csv",
606
            N_RB_DL,mcs,n_rx,channel_model_input,transmission_mode);
607
    time_meas_fd = fopen(time_meas_fname,"w");
608 609 610 611
    if (time_meas_fd==NULL) {
      fprintf(stderr,"Cannot create file %s!\n",time_meas_fname);
      exit(-1);
    }
612
  }
613 614 615

  if(abstx) {
    // CSV file
616 617
    sprintf(csv_fname,"EULdataout_tx%d_mcs%d_nbrb%d_chan%d_nsimus%d_eren.m",transmission_mode,mcs,nb_rb,chMod,n_frames);
    csv_fdUL = fopen(csv_fname,"w");
618 619 620 621
    if (csv_fdUL == NULL) {
      fprintf(stderr,"Problem opening file %s\n",csv_fname);
      exit(-1);
    }
622 623
    fprintf(csv_fdUL,"data_all%d=[",mcs);
  }
624

625

626 627 628 629 630 631
  if (xforms==1) {
    fl_initialize (&argc, argv, NULL, 0, 0);
    form_enb = create_lte_phy_scope_enb();
    sprintf (title, "LTE PHY SCOPE eNB");
    fl_show_form (form_enb->lte_phy_scope_enb, FL_PLACE_HOTSPOT, FL_FULLBORDER, title);
  }
632

633
  UE->pdcch_vars[0]->crnti = 14;
634

635
  UE->frame_parms.soundingrs_ul_config_common.enabled_flag = srs_flag;
636 637 638 639 640
  UE->frame_parms.soundingrs_ul_config_common.srs_BandwidthConfig = 2;
  UE->frame_parms.soundingrs_ul_config_common.srs_SubframeConfig = 7;
  UE->soundingrs_ul_config_dedicated[eNB_id].srs_Bandwidth = 0;
  UE->soundingrs_ul_config_dedicated[eNB_id].transmissionComb = 0;
  UE->soundingrs_ul_config_dedicated[eNB_id].freqDomainPosition = 0;
641

642
  eNB->frame_parms.soundingrs_ul_config_common.enabled_flag = srs_flag;
643 644
  eNB->frame_parms.soundingrs_ul_config_common.srs_BandwidthConfig = 2;
  eNB->frame_parms.soundingrs_ul_config_common.srs_SubframeConfig = 7;
645

646 647 648 649 650 651
  eNB->soundingrs_ul_config_dedicated[UE_id].srs_ConfigIndex = 1;
  eNB->soundingrs_ul_config_dedicated[UE_id].srs_Bandwidth = 0;
  eNB->soundingrs_ul_config_dedicated[UE_id].transmissionComb = 0;
  eNB->soundingrs_ul_config_dedicated[UE_id].freqDomainPosition = 0;
  eNB->cooperation_flag = cooperation_flag;
  //  eNB->eNB_UE_stats[0].SRS_parameters = UE->SRS_parameters;
652

653 654 655 656 657 658
  eNB->pusch_config_dedicated[UE_id].betaOffset_ACK_Index = beta_ACK;
  eNB->pusch_config_dedicated[UE_id].betaOffset_RI_Index  = beta_RI;
  eNB->pusch_config_dedicated[UE_id].betaOffset_CQI_Index = beta_CQI;
  UE->pusch_config_dedicated[eNB_id].betaOffset_ACK_Index = beta_ACK;
  UE->pusch_config_dedicated[eNB_id].betaOffset_RI_Index  = beta_RI;
  UE->pusch_config_dedicated[eNB_id].betaOffset_CQI_Index = beta_CQI;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
659

660
  UE->ul_power_control_dedicated[eNB_id].deltaMCS_Enabled = 1;
661

662 663
  printf("PUSCH Beta : ACK %f, RI %f, CQI %f\n",(double)beta_ack[beta_ACK]/8,(double)beta_ri[beta_RI]/8,(double)beta_cqi[beta_CQI]/8);

664 665
  UE2eNB = new_channel_desc_scm(eNB->frame_parms.nb_antennas_tx,
                                UE->frame_parms.nb_antennas_rx,
666
                                channel_model,
667 668
				N_RB2sampling_rate(eNB->frame_parms.N_RB_UL),
				N_RB2channel_bandwidth(eNB->frame_parms.N_RB_UL),
669 670 671 672 673 674 675
                                forgetting_factor,
                                delay,
                                0);
  // set Doppler
  UE2eNB->max_Doppler = maxDoppler;

  // NN: N_RB_UL has to be defined in ulsim
676 677
  eNB->ulsch[0] = new_eNB_ulsch(max_turbo_iterations,N_RB_DL,0);
  UE->ulsch[0]   = new_ue_ulsch(N_RB_DL,0);
678

679 680 681 682
  if (parallel_flag == 1) {
    init_fep_thread(eNB,NULL);
    init_td_thread(eNB,NULL);
  }
683
  // Create transport channel structures for 2 transport blocks (MIMO)
684
  for (i=0; i<2; i++) {
685
    eNB->dlsch[0][i] = new_eNB_dlsch(1,8,1827072,N_RB_DL,0,&eNB->frame_parms);
686
    UE->dlsch[0][i]  = new_ue_dlsch(1,8,1827072,MAX_TURBO_ITERATIONS,N_RB_DL,0);
687

688
    if (!eNB->dlsch[0][i]) {
689 690 691
      printf("Can't get eNB dlsch structures\n");
      exit(-1);
    }
692

693
    if (!UE->dlsch[0][i]) {
694 695 696
      printf("Can't get ue dlsch structures\n");
      exit(-1);
    }
697

698 699
    eNB->dlsch[0][i]->rnti = 14;
    UE->dlsch[0][i]->rnti   = 14;
700

701
  } 
702

703

704 705


706 707 708 709 710 711 712 713 714 715 716
  UE->measurements.rank[0] = 0;
  UE->transmission_mode[0] = 2;
  UE->pucch_config_dedicated[0].tdd_AckNackFeedbackMode = bundling_flag == 1 ? bundling : multiplexing;
  eNB->transmission_mode[0] = 2;
  eNB->pucch_config_dedicated[0].tdd_AckNackFeedbackMode = bundling_flag == 1 ? bundling : multiplexing;
  UE->frame_parms.pusch_config_common.ul_ReferenceSignalsPUSCH.groupHoppingEnabled = 1;
  eNB->frame_parms.pusch_config_common.ul_ReferenceSignalsPUSCH.groupHoppingEnabled = 1;
  UE->frame_parms.pusch_config_common.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled = 0;
  eNB->frame_parms.pusch_config_common.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled = 0;
  UE->frame_parms.pusch_config_common.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH = 0;
  eNB->frame_parms.pusch_config_common.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH = 0;
717 718


719
  UE->mac_enabled=0;
720
  
721 722
  eNB_rxtx_proc_t *proc_rxtx   = &eNB->proc.proc_rxtx[subframe&1];
  UE_rxtx_proc_t *proc_rxtx_ue = &UE->proc.proc_rxtx[subframe&1];
723 724
  proc_rxtx->frame_rx=1;
  proc_rxtx->subframe_rx=subframe;
725

726 727
  proc_rxtx->frame_tx=pdcch_alloc2ul_frame(&eNB->frame_parms,1,subframe);
  proc_rxtx->subframe_tx=pdcch_alloc2ul_subframe(&eNB->frame_parms,subframe);
728

729 730 731 732
  proc_rxtx_ue->frame_tx = proc_rxtx->frame_rx;
  proc_rxtx_ue->frame_rx = proc_rxtx->frame_tx;
  proc_rxtx_ue->subframe_tx = proc_rxtx->subframe_rx;
  proc_rxtx_ue->subframe_rx = proc_rxtx->subframe_tx;
Raymond Knopp's avatar
 
Raymond Knopp committed
733

734
  printf("Init UL hopping UE\n");
735
  init_ul_hopping(&UE->frame_parms);
736
  printf("Init UL hopping eNB\n");
737
  init_ul_hopping(&eNB->frame_parms);
738 739


740
  UE->dlsch[0][0]->harq_ack[ul_subframe2pdcch_alloc_subframe(&eNB->frame_parms,subframe)].send_harq_status = 1;
741

742 743
  UE->ulsch_Msg3_active[eNB_id] = 0;
  UE->ul_power_control_dedicated[eNB_id].accumulationEnabled=1;
744
  /*
745
  generate_ue_ulsch_params_from_dci((void *)&UL_alloc_pdu,
746
                                    14,
747
                                    proc_rxtx->subframe_tx,
748
                                    format0,
749
                                    UE,
750
				    proc_rxtx_ue,
751 752 753 754 755 756
                                    SI_RNTI,
                                    0,
                                    P_RNTI,
                                    CBA_RNTI,
                                    0,
                                    srs_flag);
757 758 759

  //  printf("RIV %d\n",UL_alloc_pdu.rballoc);

760
  generate_eNB_ulsch_params_from_dci(eNB,proc_rxtx,
761
				     (void *)&UL_alloc_pdu,
762 763 764
                                     14,
                                     format0,
                                     0,
765
				     SI_RNTI,
766 767 768 769
                                     0,
                                     P_RNTI,
                                     CBA_RNTI,
                                     srs_flag);
770
  */
771

772 773
  coded_bits_per_codeword = nb_rb * (12 * get_Qm_ul(mcs)) * nsymb;

774
  if (cqi_flag == 1) coded_bits_per_codeword-=UE->ulsch[0]->O;
775 776 777 778

  rate = (double)dlsch_tbs25[get_I_TBS(mcs)][nb_rb-1]/(coded_bits_per_codeword);

  printf("Rate = %f (mod %d), coded bits %d\n",rate,get_Qm_ul(mcs),coded_bits_per_codeword);
779 780


781 782 783

  for (ch_realization=0; ch_realization<n_ch_rlz; ch_realization++) {

784 785 786 787 788 789
    /*
      if(abstx){
      int ulchestim_f[300*12];
      int ulchestim_t[2*(frame_parms->ofdm_symbol_size)];
      }
    */
790 791

    if(abstx) {
792 793 794
      printf("**********************Channel Realization Index = %d **************************\n", ch_realization);
      saving_bler=1;
    }
795

796

Raymond Knopp's avatar
 
Raymond Knopp committed
797
    //    if ((subframe>5) || (subframe < 4))
798
    //      UE->frame++;
799 800

    for (SNR=snr0; SNR<snr1; SNR+=input_snr_step) {
801 802 803 804 805 806 807 808 809 810 811 812 813
      errs[0]=0;
      errs[1]=0;
      errs[2]=0;
      errs[3]=0;
      round_trials[0] = 0;
      round_trials[1] = 0;
      round_trials[2] = 0;
      round_trials[3] = 0;
      cqi_errors=0;
      ack_errors=0;
      cqi_crc_falsepositives=0;
      cqi_crc_falsenegatives=0;
      round=0;
814

815 816
      //randominit(0);

Raymond Knopp's avatar
 
Raymond Knopp committed
817

818
      harq_pid = subframe2harq_pid(&UE->frame_parms,proc_rxtx_ue->frame_tx,subframe);
819
      input_buffer_length = UE->ulsch[0]->harq_processes[harq_pid]->TBS/8;
820
      input_buffer = (unsigned char *)memalign(32,input_buffer_length+64);
821
      //      printf("UL frame %d/subframe %d, harq_pid %d\n",UE->frame,subframe,harq_pid);
822
      if (input_fdUL == NULL) {
823

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

        if (n_frames == 1) {
          trch_out_fdUL= fopen("ulsch_trchUL.txt","w");

          for (i=0; i<input_buffer_length; i++) {
            input_buffer[i] = taus()&0xff;

            for (j=0; j<8; j++)
              fprintf(trch_out_fdUL,"%d\n",(input_buffer[i]>>(7-j))&1);
          }

          fclose(trch_out_fdUL);
        } else {
          for (i=0; i<input_buffer_length; i++)
            input_buffer[i] = taus()&0xff;
        }
      } else {
        n_frames=1;
        i=0;

        while (!feof(input_fdUL)) {
845
          ret=fscanf(input_fdUL,"%s %s",input_val_str,input_val_str2);//&input_val1,&input_val2);
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867

          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>(FRAME_LENGTH_SAMPLES))
            break;
        }

        printf("Read in %d samples\n",i/4);
        //      write_output("txsig0UL.m","txs0", txdata[0],2*frame_parms->samples_per_tti,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);

868 869
      }

870 871
      avg_iter = 0;
      iter_trials=0;
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
      reset_meas(&UE->phy_proc_tx);
      reset_meas(&UE->ofdm_mod_stats);
      reset_meas(&UE->ulsch_modulation_stats);
      reset_meas(&UE->ulsch_encoding_stats);
      reset_meas(&UE->ulsch_interleaving_stats);
      reset_meas(&UE->ulsch_rate_matching_stats);
      reset_meas(&UE->ulsch_turbo_encoding_stats);
      reset_meas(&UE->ulsch_segmentation_stats);
      reset_meas(&UE->ulsch_multiplexing_stats);

      reset_meas(&eNB->phy_proc_rx);
      reset_meas(&eNB->ofdm_demod_stats);
      reset_meas(&eNB->ulsch_channel_estimation_stats);
      reset_meas(&eNB->ulsch_freq_offset_estimation_stats);
      reset_meas(&eNB->rx_dft_stats);
      reset_meas(&eNB->ulsch_decoding_stats);
      reset_meas(&eNB->ulsch_turbo_decoding_stats);
      reset_meas(&eNB->ulsch_deinterleaving_stats);
      reset_meas(&eNB->ulsch_demultiplexing_stats);
      reset_meas(&eNB->ulsch_rate_unmatching_stats);
      reset_meas(&eNB->ulsch_tc_init_stats);
      reset_meas(&eNB->ulsch_tc_alpha_stats);
      reset_meas(&eNB->ulsch_tc_beta_stats);
      reset_meas(&eNB->ulsch_tc_gamma_stats);
      reset_meas(&eNB->ulsch_tc_ext_stats);
      reset_meas(&eNB->ulsch_tc_intl1_stats);
      reset_meas(&eNB->ulsch_tc_intl2_stats);
899

900
      // initialization 
901 902
      struct list time_vector_tx;
      initialize(&time_vector_tx);
903 904 905 906 907 908 909
      struct list time_vector_tx_ifft;
      initialize(&time_vector_tx_ifft);
      struct list time_vector_tx_mod;
      initialize(&time_vector_tx_mod);
      struct list time_vector_tx_enc;
      initialize(&time_vector_tx_enc);

910 911
      struct list time_vector_rx;
      initialize(&time_vector_rx);
912 913 914 915 916 917
      struct list time_vector_rx_fft;
      initialize(&time_vector_rx_fft);
      struct list time_vector_rx_demod;
      initialize(&time_vector_rx_demod);
      struct list time_vector_rx_dec;
      initialize(&time_vector_rx_dec);
918

919
      ndi=0;
920 921
      for (trials = 0; trials<n_frames; trials++) {
        //      printf("*");
922 923
        //        UE->frame++;
        //        eNB->frame++;
924
	ndi = (1-ndi);
925 926 927 928
        fflush(stdout);
        round=0;

        while (round < 4) {
929 930
          eNB->ulsch[0]->harq_processes[harq_pid]->round=round;
          UE->ulsch[0]->harq_processes[harq_pid]->round=round;
931
	  //	  printf("Trial %d : Round %d (subframe %d, frame %d)\n",trials,round,proc_rxtx_ue->subframe_rx,proc_rxtx_ue->frame_rx);
932 933 934
          round_trials[round]++;


935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	  fill_ulsch_dci(eNB,(void*)&UL_alloc_pdu,first_rb,nb_rb,mcs,ndi,cqi_flag);

	  UE->ulsch_Msg3_active[eNB_id] = 0;
	  UE->ul_power_control_dedicated[eNB_id].accumulationEnabled=1;
	  generate_ue_ulsch_params_from_dci((void *)&UL_alloc_pdu,
					    14,
					    proc_rxtx->subframe_tx,
					    format0,
					    UE,
					    proc_rxtx_ue,
					    SI_RNTI,
					    0,
					    P_RNTI,
					    CBA_RNTI,
					    0,
					    srs_flag);

	  generate_eNB_ulsch_params_from_dci(eNB,proc_rxtx,
					     (void *)&UL_alloc_pdu,
					     14,
					     format0,
					     0,
					     SI_RNTI,
					     0,
					     P_RNTI,
					     CBA_RNTI,
					     srs_flag);
	  eNB->ulsch[0]->harq_processes[harq_pid]->subframe_scheduling_flag = 1;
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981

          /////////////////////
          if (abstx) {
            if (trials==0 && round==0 && SNR==snr0) { //generate a new channel
              hold_channel = 0;
              flagMag=0;
            } else {
              hold_channel = 1;
              flagMag = 1;
            }
          } else {
            hold_channel = 0;
            flagMag=1;
          }

          ///////////////////////////////////////

          if (input_fdUL == NULL) {

982 983 984 985 986 987
	    eNB->proc.frame_rx = 1;
	    eNB->proc.subframe_rx = subframe;
	    proc_rxtx_ue->frame_tx = proc_rxtx->frame_rx;
	    proc_rxtx_ue->frame_rx = proc_rxtx->frame_tx;
	    proc_rxtx_ue->subframe_tx = proc_rxtx->subframe_rx;
	    proc_rxtx_ue->subframe_rx = proc_rxtx->subframe_tx;
988

989
	    phy_procedures_UE_TX(UE,proc_rxtx_ue,0,0,normal_txrx,no_relay);
990

991
	    /*
992
            if (srs_flag)
993
              generate_srs_tx(UE,0,AMP,subframe);
994

995
            generate_drs_pusch(UE,proc_rxtx_ue,0,
996
                               AMP,subframe,
997 998
                               UE->ulsch[0]->harq_processes[harq_pid]->first_rb,
                               UE->ulsch[0]->harq_processes[harq_pid]->nb_rb,
999 1000 1001
                               0);

            if ((cqi_flag == 1) && (n_frames == 1) ) {
1002 1003 1004
              printf("CQI information (O %d) %d %d\n",UE->ulsch[0]->O,
                     UE->ulsch[0]->o[0],UE->ulsch[0]->o[1]);
              print_CQI(UE->ulsch[0]->o,UE->ulsch[0]->uci_format,UE->frame_parms.N_RB_DL,0);
1005 1006
            }

1007
            UE->ulsch[0]->o_ACK[0] = taus()&1;
1008

1009
            start_meas(&UE->ulsch_encoding_stats);
1010 1011

            if (ulsch_encoding(input_buffer,
1012
                               UE,
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
                               harq_pid,
                               eNB_id,
                               2, // transmission mode
                               control_only_flag,
                               1// Nbundled
                              )==-1) {
              printf("ulsim.c Problem with ulsch_encoding\n");
              exit(-1);
            }

1023
            stop_meas(&UE->ulsch_encoding_stats);
1024

1025 1026
            start_meas(&UE->ulsch_modulation_stats);
            ulsch_modulation(UE->common_vars.txdataF,AMP,
1027
                             proc_rxtx_ue->frame_tx,subframe,&UE->frame_parms,
1028 1029
                             UE->ulsch[0]);
            stop_meas(&UE->ulsch_modulation_stats);
1030
	    */
1031 1032 1033



1034 1035 1036

	    /*
	    for (aa=0; aa<1; aa++) {
1037
              if (frame_parms->Ncp == EXTENDED)
1038 1039 1040
                PHY_ofdm_mod(&UE->common_vars.txdataF[aa][subframe*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES_NO_PREFIX],        // input
                             &txdata[aa][eNB->frame_parms.samples_per_tti*subframe],         // output
                             UE->frame_parms.ofdm_symbol_size,
1041
                             nsymb,                 // number of symbols
1042
                             UE->frame_parms.nb_prefix_samples,               // number of prefix samples
1043 1044
                             CYCLIC_PREFIX);
              else
1045 1046
                normal_prefix_mod(&UE->common_vars.txdataF[aa][subframe*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES_NO_PREFIX],
                                  &txdata[aa][eNB->frame_parms.samples_per_tti*subframe],
1047 1048 1049 1050
                                  nsymb,
                                  frame_parms);


1051 1052
              apply_7_5_kHz(UE,UE->common_vars.txdata[aa],subframe<<1);
              apply_7_5_kHz(UE,UE->common_vars.txdata[aa],1+(subframe<<1));
1053

1054
*/
1055

1056 1057 1058 1059 1060 1061 1062 1063
	    tx_lev = signal_energy(&UE->common_vars.txdata[0][eNB->frame_parms.samples_per_tti*subframe],
				   eNB->frame_parms.samples_per_tti);
	    
	    
            if (n_frames==1) {
              write_output("txsigF0UL.m","txsF0", &UE->common_vars.txdataF[0][eNB->frame_parms.ofdm_symbol_size*nsymb*subframe],eNB->frame_parms.ofdm_symbol_size*nsymb,1,
                           1);
              //write_output("txsigF1.m","txsF1", UE->common_vars.txdataF[0],FRAME_LENGTH_COMPLEX_SAMPLES_NO_PREFIX,1,1);
1064
            }
1065 1066 1067
	    
	  }  // input_fd == NULL
	  
1068 1069 1070
          tx_lev_dB = (unsigned int) dB_fixed_times10(tx_lev);

          if (n_frames==1) {
1071 1072
            write_output("txsig0UL.m","txs0", &txdata[0][eNB->frame_parms.samples_per_tti*subframe],2*frame_parms->samples_per_tti,1,1);
            //        write_output("txsig1UL.m","txs1", &txdata[1][eNB->frame_parms.samples_per_tti*subframe],2*frame_parms->samples_per_tti,1,1);
1073 1074 1075 1076
          }

          //AWGN
          //Set target wideband RX noise level to N0
1077
          sigma2_dB = N0;//10*log10((double)tx_lev)  +10*log10(UE->frame_parms.ofdm_symbol_size/(UE->frame_parms.N_RB_DL*12)) - SNR;
1078 1079 1080
          sigma2 = pow(10,sigma2_dB/10);

          // compute tx_gain to achieve target SNR (per resource element!)
1081
          tx_gain = sqrt(pow(10.0,.1*(N0+SNR))*(nb_rb*12/(double)UE->frame_parms.ofdm_symbol_size)/(double)tx_lev);
1082

1083 1084

	  if (n_frames==1)
1085 1086 1087 1088 1089
            printf("tx_lev = %d (%d.%d dB,%f), gain %f\n",tx_lev,tx_lev_dB/10,tx_lev_dB,10*log10((double)tx_lev),10*log10(tx_gain));


          // fill measurement symbol (19) with noise
          for (i=0; i<OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; i++) {
1090
            for (aa=0; aa<eNB->frame_parms.nb_antennas_rx; aa++) {
1091

1092 1093
              ((short*) &eNB->common_vars.rxdata[0][aa][(frame_parms->samples_per_tti<<1) -frame_parms->ofdm_symbol_size])[2*i] = (short) ((sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
              ((short*) &eNB->common_vars.rxdata[0][aa][(frame_parms->samples_per_tti<<1) -frame_parms->ofdm_symbol_size])[2*i+1] = (short) ((sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
1094 1095 1096 1097 1098
            }
          }

          // multipath channel

1099
          for (i=0; i<eNB->frame_parms.samples_per_tti; i++) {
1100
            for (aa=0; aa<1; aa++) {
1101 1102
              s_re[aa][i] = ((double)(((short *)&UE->common_vars.txdata[aa][eNB->frame_parms.samples_per_tti*subframe]))[(i<<1)]);
              s_im[aa][i] = ((double)(((short *)&UE->common_vars.txdata[aa][eNB->frame_parms.samples_per_tti*subframe]))[(i<<1)+1]);
1103 1104 1105 1106 1107 1108
            }
          }

          if (awgn_flag == 0) {
            if (UE2eNB->max_Doppler == 0) {
              multipath_channel(UE2eNB,s_re,s_im,r_re,r_im,
1109
                                eNB->frame_parms.samples_per_tti,hold_channel);
1110 1111
            } else {
              multipath_tv_channel(UE2eNB,s_re,s_im,r_re,r_im,
1112
                                   2*eNB->frame_parms.samples_per_tti,hold_channel);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
            }
          }

          if(abstx) {
            if(saving_bler==0)
              if (trials==0 && round==0) {
                // calculate freq domain representation to compute SINR
                freq_channel(UE2eNB, N_RB_DL,12*N_RB_DL + 1);

                // snr=pow(10.0,.1*SNR);
                fprintf(csv_fdUL,"%f,%d,%d,%f,%f,%f,",SNR,tx_lev,tx_lev_dB,sigma2_dB,tx_gain,SNR2);

                //fprintf(csv_fdUL,"%f,",SNR);
                for (u=0; u<12*nb_rb; u++) {
                  for (aarx=0; aarx<UE2eNB->nb_rx; aarx++) {
                    for (aatx=0; aatx<UE2eNB->nb_tx; aatx++) {
                      // abs_channel = (eNB2UE->chF[aarx+(aatx*eNB2UE->nb_rx)][u].x*eNB2UE->chF[aarx+(aatx*eNB2UE->nb_rx)][u].x + eNB2UE->chF[aarx+(aatx*eNB2UE->nb_rx)][u].y*eNB2UE->chF[aarx+(aatx*eNB2UE->nb_rx)][u].y);
                      channelx = UE2eNB->chF[aarx+(aatx*UE2eNB->nb_rx)][u].x;
                      channely = UE2eNB->chF[aarx+(aatx*UE2eNB->nb_rx)][u].y;
                      // if(transmission_mode==5){
                      fprintf(csv_fdUL,"%e+i*(%e),",channelx,channely);
                      // }
                      // else{
                      //  pilot_sinr = 10*log10(snr*abs_channel);
                      //  fprintf(csv_fd,"%e,",pilot_sinr);
                      // }
                    }
                  }
                }
              }
          }

          if (n_frames==1)
            printf("Sigma2 %f (sigma2_dB %f), tx_gain %f (%f dB)\n",sigma2,sigma2_dB,tx_gain,20*log10(tx_gain));

1148 1149 1150 1151
          for (i=0; i<eNB->frame_parms.samples_per_tti; i++) {
            for (aa=0; aa<eNB->frame_parms.nb_antennas_rx; aa++) {
              ((short*) &eNB->common_vars.rxdata[0][aa][eNB->frame_parms.samples_per_tti*subframe])[2*i] = (short) ((tx_gain*r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0));
              ((short*) &eNB->common_vars.rxdata[0][aa][eNB->frame_parms.samples_per_tti*subframe])[2*i+1] = (short) ((tx_gain*r_im[aa][i]) + (iqim*tx_gain*r_re[aa][i]) + sqrt(
1152 1153 1154 1155 1156 1157
                    sigma2/2)*gaussdouble(0.0,1.0));
            }
          }

          if (n_frames==1) {
            printf("rx_level Null symbol %f\n",10*log10((double)signal_energy((int*)
1158 1159
                   &eNB->common_vars.rxdata[0][0][(eNB->frame_parms.samples_per_tti<<1) -eNB->frame_parms.ofdm_symbol_size],OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES/2)));
            printf("rx_level data symbol %f\n",10*log10(signal_energy((int*)&eNB->common_vars.rxdata[0][0][160+(eNB->frame_parms.samples_per_tti*subframe)],
1160 1161 1162
                   OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES/2)));
          }

1163
          SNRmeas = 10*log10(((double)signal_energy((int*)&eNB->common_vars.rxdata[0][0][160+(eNB->frame_parms.samples_per_tti*subframe)],
1164
                              OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES/2))/((double)signal_energy((int*)
1165 1166
                                  &eNB->common_vars.rxdata[0][0][(eNB->frame_parms.samples_per_tti<<1) -eNB->frame_parms.ofdm_symbol_size],
                                  OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES/2)) - 1)+10*log10(eNB->frame_parms.N_RB_UL/nb_rb);
1167 1168 1169 1170

          if (n_frames==1) {
            printf("SNRmeas %f\n",SNRmeas);

1171 1172
            //    write_output("rxsig0UL.m","rxs0", &eNB->common_vars.rxdata[0][0][eNB->frame_parms.samples_per_tti*subframe],eNB->frame_parms.samples_per_tti,1,1);
            //write_output("rxsig1UL.m","rxs1", &eNB->common_vars.rxdata[0][0][eNB->frame_parms.samples_per_tti*subframe],eNB->frame_parms.samples_per_tti,1,1);
1173 1174 1175
          }


1176 1177
	  eNB->fep = (parallel_flag == 1) ? eNB_fep_full_2thread        : eNB_fep_full;
	  eNB->td  = (parallel_flag == 1) ? ulsch_decoding_data_2thread : ulsch_decoding_data;
1178
	  eNB->do_prach = NULL;
1179

1180
	  phy_procedures_eNB_common_RX(eNB,proc_rxtx);
1181
	  phy_procedures_eNB_uespec_RX(eNB,proc_rxtx,no_relay);
1182

1183 1184 1185 1186

          if (cqi_flag > 0) {
            cqi_error = 0;

1187
            if (eNB->ulsch[0]->harq_processes[harq_pid]->Or1 < 32) {
1188
              for (i=2; i<4; i++) {
1189 1190
                //                printf("cqi %d : %d (%d)\n",i,eNB->ulsch[0]->o[i],UE->ulsch[0]->o[i]);
                if (eNB->ulsch[0]->harq_processes[harq_pid]->o[i] != UE->ulsch[0]->o[i])
1191 1192 1193 1194 1195 1196 1197 1198 1199
                  cqi_error = 1;
              }
            } else {

            }

            if (cqi_error == 1) {
              cqi_errors++;

1200
              if (eNB->ulsch[0]->harq_processes[harq_pid]->cqi_crc_status == 1)
1201 1202
                cqi_crc_falsepositives++;
            } else {
1203
              if (eNB->ulsch[0]->harq_processes[harq_pid]->cqi_crc_status == 0)
1204 1205 1206 1207
                cqi_crc_falsenegatives++;
            }
          }

1208
          if (eNB->ulsch[0]->harq_processes[harq_pid]->o_ACK[0] != UE->ulsch[0]->o_ACK[0])
1209 1210
            ack_errors++;

1211
          //    printf("ulsch_coding: O[%d] %d\n",i,o_flip[i]);
1212

1213 1214 1215 1216
	  
	  //          if (ret <= eNB->ulsch[0]->max_turbo_iterations) {
	  
	  if (eNB->ulsch[0]->harq_processes[harq_pid]->round == 0) {
1217

1218 1219
	    //  avg_iter += ret;
	    iter_trials++;
1220 1221

            if (n_frames==1) {
1222
              printf("No ULSCH errors found, o_ACK[0]= %d, cqi_crc_status=%d\n",eNB->ulsch[0]->harq_processes[harq_pid]->o_ACK[0],eNB->ulsch[0]->harq_processes[harq_pid]->cqi_crc_status);
1223

1224 1225 1226
              if (eNB->ulsch[0]->harq_processes[harq_pid]->cqi_crc_status==1)
                print_CQI(eNB->ulsch[0]->harq_processes[harq_pid]->o,
                          eNB->ulsch[0]->harq_processes[harq_pid]->uci_format,0,eNB->frame_parms.N_RB_DL);
1227

1228
              dump_ulsch(eNB,proc_rxtx,0);
1229 1230 1231 1232 1233
              exit(-1);
            }

            round=5;
          } else {
1234
	    //            avg_iter += ret-1;
1235 1236 1237 1238 1239
            iter_trials++;

            errs[round]++;

            if (n_frames==1) {
1240
              printf("ULSCH errors found o_ACK[0]= %d\n",eNB->ulsch[0]->harq_processes[harq_pid]->o_ACK[0]);
1241

1242 1243 1244
              for (s=0; s<eNB->ulsch[0]->harq_processes[harq_pid]->C; s++) {
                if (s<eNB->ulsch[0]->harq_processes[harq_pid]->Cminus)
                  Kr = eNB->ulsch[0]->harq_processes[harq_pid]->Kminus;
1245
                else
1246
                  Kr = eNB->ulsch[0]->harq_processes[harq_pid]->Kplus;
1247 1248 1249 1250 1251 1252

                Kr_bytes = Kr>>3;

                printf("Decoded_output (Segment %d):\n",s);

                for (i=0; i<Kr_bytes; i++)
1253 1254
                  printf("%d : %x (%x)\n",i,eNB->ulsch[0]->harq_processes[harq_pid]->c[s][i],
                         eNB->ulsch[0]->harq_processes[harq_pid]->c[s][i]^UE->ulsch[0]->harq_processes[harq_pid]->c[s][i]);
1255 1256
              }

1257
              dump_ulsch(eNB,proc_rxtx,0);
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
              exit(-1);
            }

            //      printf("round %d errors %d/%d\n",round,errs[round],trials);
            round++;

            if (n_frames==1) {
              printf("ULSCH in error in round %d\n",round);
            }
          }  // ulsch error
1268 1269
	  

1270 1271 1272 1273 1274 1275
        } // round

        //      printf("\n");
        if ((errs[0]>=100) && (trials>(n_frames/2)))
          break;

1276
	if (xforms==1)
1277
	  phy_scope_eNB(form_enb,eNB,0);
1278

1279
        /*calculate the total processing time for each packet, get the max, min, and number of packets that exceed t>3000us*/
1280

1281 1282 1283 1284
        double t_tx = (double)UE->phy_proc_tx.p_time/cpu_freq_GHz/1000.0;
        double t_tx_ifft = (double)UE->ofdm_mod_stats.p_time/cpu_freq_GHz/1000.0;
        double t_tx_mod = (double)UE->ulsch_modulation_stats.p_time/cpu_freq_GHz/1000.0;
        double t_tx_enc = (double)UE->ulsch_encoding_stats.p_time/cpu_freq_GHz/1000.0;
1285 1286


1287 1288 1289 1290
        double t_rx = (double)eNB->phy_proc_rx.p_time/cpu_freq_GHz/1000.0;
        double t_rx_fft = (double)eNB->ofdm_demod_stats.p_time/cpu_freq_GHz/1000.0;
        double t_rx_demod = (double)eNB->ulsch_demodulation_stats.p_time/cpu_freq_GHz/1000.0;
        double t_rx_dec = (double)eNB->ulsch_decoding_stats.p_time/cpu_freq_GHz/1000.0;
1291

1292
        if (t_tx > t_tx_max)
1293 1294
          t_tx_max = t_tx;

1295
        if (t_tx < t_tx_min)
1296 1297
          t_tx_min = t_tx;

1298
        if (t_rx > t_rx_max)
1299 1300
          t_rx_max = t_rx;

1301
        if (t_rx < t_rx_min)
1302 1303
          t_rx_min = t_rx;

1304
        if (t_tx > 2000)
1305 1306
          n_tx_dropped++;

1307
        if (t_rx > 2000)
1308 1309
          n_rx_dropped++;

1310
        push_front(&time_vector_tx, t_tx);
1311 1312 1313 1314
        push_front(&time_vector_tx_ifft, t_tx_ifft);
        push_front(&time_vector_tx_mod, t_tx_mod);
        push_front(&time_vector_tx_enc, t_tx_enc);

1315
        push_front(&time_vector_rx, t_rx);
1316 1317 1318
        push_front(&time_vector_rx_fft, t_rx_fft);
        push_front(&time_vector_rx_demod, t_rx_demod);
        push_front(&time_vector_rx_dec, t_rx_dec);
1319 1320


1321
      }   //trials
1322

1323 1324
      double table_tx[time_vector_tx.size];
      totable(table_tx, &time_vector_tx);
1325 1326 1327 1328 1329 1330
      double table_tx_ifft[time_vector_tx_ifft.size];
      totable(table_tx_ifft, &time_vector_tx_ifft);
      double table_tx_mod[time_vector_tx_mod.size];
      totable(table_tx_mod, &time_vector_tx_mod);
      double table_tx_enc[time_vector_tx_enc.size];
      totable(table_tx_enc, &time_vector_tx_enc);
1331

1332 1333
      double table_rx[time_vector_rx.size];
      totable(table_rx, &time_vector_rx);
1334 1335 1336 1337 1338
      double table_rx_fft[time_vector_rx_fft.size];
      totable(table_rx_fft, &time_vector_rx_fft);
      double table_rx_demod[time_vector_rx_demod.size];
      totable(table_rx_demod, &time_vector_rx_demod);
      double table_rx_dec[time_vector_rx_dec.size];
1339 1340
      totable(table_rx_dec, &time_vector_rx_dec);

1341 1342 1343
      // sort table
      qsort (table_tx, time_vector_tx.size, sizeof(double), &compare);
      qsort (table_rx, time_vector_rx.size, sizeof(double), &compare);
1344

Navid Nikaein's avatar
Navid Nikaein committed
1345
      if (dump_table == 1 ) {
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
        int n;
        set_component_filelog(USIM); // file located in /tmp/usim.txt
        LOG_F(USIM,"The transmitter raw data: \n");

        for (n=0; n< time_vector_tx.size; n++) {
          //   printf("%f ", table_tx[n]);
          LOG_F(USIM,"%f ", table_tx[n]);
        }

        LOG_F(USIM,"\n");
        LOG_F(USIM,"The receiver raw data: \n");

        for (n=0; n< time_vector_rx.size; n++) {
          // printf("%f ", table_rx[n]);
          LOG_F(USIM,"%f ", table_rx[n]);
        }

        LOG_F(USIM,"\n");
      }

1366 1367 1368
      double tx_median = table_tx[time_vector_tx.size/2];
      double tx_q1 = table_tx[time_vector_tx.size/4];
      double tx_q3 = table_tx[3*time_vector_tx.size/4];
1369

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
      double tx_ifft_median = table_tx_ifft[time_vector_tx_ifft.size/2];
      double tx_ifft_q1 = table_tx_ifft[time_vector_tx_ifft.size/4];
      double tx_ifft_q3 = table_tx_ifft[3*time_vector_tx_ifft.size/4];

      double tx_mod_median = table_tx_mod[time_vector_tx_mod.size/2];
      double tx_mod_q1 = table_tx_mod[time_vector_tx_mod.size/4];
      double tx_mod_q3 = table_tx_mod[3*time_vector_tx_mod.size/4];

      double tx_enc_median = table_tx_enc[time_vector_tx_enc.size/2];
      double tx_enc_q1 = table_tx_enc[time_vector_tx_enc.size/4];
      double tx_enc_q3 = table_tx_enc[3*time_vector_tx_enc.size/4];

1382 1383 1384
      double rx_median = table_rx[time_vector_rx.size/2];
      double rx_q1 = table_rx[time_vector_rx.size/4];
      double rx_q3 = table_rx[3*time_vector_rx.size/4];
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397

      double rx_fft_median = table_rx_fft[time_vector_rx_fft.size/2];
      double rx_fft_q1 = table_rx_fft[time_vector_rx_fft.size/4];
      double rx_fft_q3 = table_rx_fft[3*time_vector_rx_fft.size/4];

      double rx_demod_median = table_rx_demod[time_vector_rx_demod.size/2];
      double rx_demod_q1 = table_rx_demod[time_vector_rx_demod.size/4];
      double rx_demod_q3 = table_rx_demod[3*time_vector_rx_demod.size/4];

      double rx_dec_median = table_rx_dec[time_vector_rx_dec.size/2];
      double rx_dec_q1 = table_rx_dec[time_vector_rx_dec.size/4];
      double rx_dec_q3 = table_rx_dec[3*time_vector_rx_dec.size/4];

1398
      double std_phy_proc_tx=0;
1399 1400 1401 1402
      double std_phy_proc_tx_ifft=0;
      double std_phy_proc_tx_mod=0;
      double std_phy_proc_tx_enc=0;

1403
      double std_phy_proc_rx=0;
1404 1405 1406
      double std_phy_proc_rx_fft=0;
      double std_phy_proc_rx_demod=0;
      double std_phy_proc_rx_dec=0;
1407

Florian Kaltenberger's avatar
Florian Kaltenberger committed
1408
      printf("\n**********rb: %d ***mcs : %d  *********SNR = %f dB (%f): TX %d dB (gain %f dB), N0W %f dB, I0 %d dB, delta_IF %d [ (%d,%d) dB / (%d,%d) dB ]**************************\n",
1409 1410 1411 1412
             nb_rb,mcs,SNR,SNR2,
             tx_lev_dB,
             20*log10(tx_gain),
             (double)N0,
1413 1414 1415 1416 1417 1418
             eNB->measurements[0].n0_power_tot_dB,
             get_hundred_times_delta_IF(UE,eNB_id,harq_pid) ,
             dB_fixed(eNB->pusch_vars[0]->ulsch_power[0]),
             dB_fixed(eNB->pusch_vars[0]->ulsch_power[1]),
             eNB->measurements->n0_power_dB[0],
             eNB->measurements->n0_power_dB[1]);
1419 1420

      effective_rate = ((double)(round_trials[0])/((double)round_trials[0] + round_trials[1] + round_trials[2] + round_trials[3]));
1421

1422
      printf("Errors (%d/%d %d/%d %d/%d %d/%d), Pe = (%e,%e,%e,%e) => effective rate %f (%3.1f%%,%f,%f), normalized delay %f (%f)\n",
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
             errs[0],
             round_trials[0],
             errs[1],
             round_trials[1],
             errs[2],
             round_trials[2],
             errs[3],
             round_trials[3],
             (double)errs[0]/(round_trials[0]),
             (double)errs[1]/(round_trials[0]),
             (double)errs[2]/(round_trials[0]),
             (double)errs[3]/(round_trials[0]),
             rate*effective_rate,
             100*effective_rate,
             rate,
1438
             rate*get_Qm_ul(mcs),
1439
             (1.0*(round_trials[0]-errs[0])+2.0*(round_trials[1]-errs[1])+3.0*(round_trials[2]-errs[2])+4.0*(round_trials[3]-errs[3]))/((double)round_trials[0])/
1440
             (double)eNB->ulsch[0]->harq_processes[harq_pid]->TBS,
1441 1442
             (1.0*(round_trials[0]-errs[0])+2.0*(round_trials[1]-errs[1])+3.0*(round_trials[2]-errs[2])+4.0*(round_trials[3]-errs[3]))/((double)round_trials[0]));

1443
      if (cqi_flag >0) {
1444 1445 1446 1447
        printf("CQI errors %d/%d,false positives %d/%d, CQI false negatives %d/%d\n",
               cqi_errors,round_trials[0]+round_trials[1]+round_trials[2]+round_trials[3],
               cqi_crc_falsepositives,round_trials[0]+round_trials[1]+round_trials[2]+round_trials[3],
               cqi_crc_falsenegatives,round_trials[0]+round_trials[1]+round_trials[2]+round_trials[3]);
1448
      }
1449

1450
      if (eNB->ulsch[0]->harq_processes[harq_pid]->o_ACK[0] > 0)
1451
        printf("ACK/NAK errors %d/%d\n",ack_errors,round_trials[0]+round_trials[1]+round_trials[2]+round_trials[3]);
1452

1453 1454

      fprintf(bler_fd,"%f;%d;%d;%d;%f;%d;%d;%d;%d;%d;%d;%d;%d\n",
1455 1456 1457
              SNR,
              mcs,
              nb_rb,
1458
              eNB->ulsch[0]->harq_processes[harq_pid]->TBS,
1459 1460 1461 1462 1463 1464 1465 1466 1467
              rate,
              errs[0],
              round_trials[0],
              errs[1],
              round_trials[1],
              errs[2],
              round_trials[2],
              errs[3],
              round_trials[3]);
1468

1469 1470

      if (dump_perf==1) {
1471
        printf("UE TX function statistics (per 1ms subframe)\n\n");
1472 1473 1474
        std_phy_proc_tx = sqrt((double)UE->phy_proc_tx.diff_square/pow(cpu_freq_GHz,2)/pow(1000,
                               2)/UE->phy_proc_tx.trials - pow((double)UE->phy_proc_tx.diff/UE->phy_proc_tx.trials/cpu_freq_GHz/1000,2));
        printf("Total PHY proc tx                 :%f us (%d trials)\n",(double)UE->phy_proc_tx.diff/UE->phy_proc_tx.trials/cpu_freq_GHz/1000.0,UE->phy_proc_tx.trials);
1475 1476
        printf("|__ Statistics                         std: %f us max: %fus min: %fus median %fus q1 %fus q3 %fus n_dropped: %d packet \n",std_phy_proc_tx, t_tx_max, t_tx_min, tx_median, tx_q1, tx_q3,
               n_tx_dropped);
1477 1478 1479
        std_phy_proc_tx_ifft = sqrt((double)UE->ofdm_mod_stats.diff_square/pow(cpu_freq_GHz,2)/pow(1000,
                                    2)/UE->ofdm_mod_stats.trials - pow((double)UE->ofdm_mod_stats.diff/UE->ofdm_mod_stats.trials/cpu_freq_GHz/1000,2));
        printf("OFDM_mod time                     :%f us (%d trials)\n",(double)UE->ofdm_mod_stats.diff/UE->ofdm_mod_stats.trials/cpu_freq_GHz/1000.0,UE->ofdm_mod_stats.trials);
1480
        printf("|__ Statistics                         std: %f us median %fus q1 %fus q3 %fus \n",std_phy_proc_tx_ifft, tx_ifft_median, tx_ifft_q1, tx_ifft_q3);
1481 1482 1483 1484
        std_phy_proc_tx_mod = sqrt((double)UE->ulsch_modulation_stats.diff_square/pow(cpu_freq_GHz,2)/pow(1000,
                                   2)/UE->ulsch_modulation_stats.trials - pow((double)UE->ulsch_modulation_stats.diff/UE->ulsch_modulation_stats.trials/cpu_freq_GHz/1000,2));
        printf("ULSCH modulation time             :%f us (%d trials)\n",(double)UE->ulsch_modulation_stats.diff/UE->ulsch_modulation_stats.trials/cpu_freq_GHz/1000.0,
               UE->ulsch_modulation_stats.trials);
1485
        printf("|__ Statistics                         std: %f us median %fus q1 %fus q3 %fus \n",std_phy_proc_tx_mod, tx_mod_median, tx_mod_q1, tx_mod_q3);
1486 1487 1488 1489
        std_phy_proc_tx_enc = sqrt((double)UE->ulsch_encoding_stats.diff_square/pow(cpu_freq_GHz,2)/pow(1000,
                                   2)/UE->ulsch_encoding_stats.trials - pow((double)UE->ulsch_encoding_stats.diff/UE->ulsch_encoding_stats.trials/cpu_freq_GHz/1000,2));
        printf("ULSCH encoding time               :%f us (%d trials)\n",(double)UE->ulsch_encoding_stats.diff/UE->ulsch_encoding_stats.trials/cpu_freq_GHz/1000.0,
               UE->ulsch_encoding_stats.trials);
1490
        printf("|__ Statistics                         std: %f us median %fus q1 %fus q3 %fus \n",std_phy_proc_tx_enc, tx_enc_median, tx_enc_q1, tx_enc_q3);
1491 1492
        printf("|__ ULSCH segmentation time           :%f us (%d trials)\n",(double)UE->ulsch_segmentation_stats.diff/UE->ulsch_segmentation_stats.trials/cpu_freq_GHz/1000.0,
               UE->ulsch_segmentation_stats.trials);
1493
        printf("|__ ULSCH turbo encoding time         :%f us (%d trials)\n",
1494 1495
               ((double)UE->ulsch_turbo_encoding_stats.trials/UE->ulsch_encoding_stats.trials)*(double)
               UE->ulsch_turbo_encoding_stats.diff/UE->ulsch_turbo_encoding_stats.trials/cpu_freq_GHz/1000.0,UE->ulsch_turbo_encoding_stats.trials);
1496
        printf("|__ ULSCH rate-matching time          :%f us (%d trials)\n",
1497 1498
               ((double)UE->ulsch_rate_matching_stats.trials/UE->ulsch_encoding_stats.trials)*(double)
               UE->ulsch_rate_matching_stats.diff/UE->ulsch_rate_matching_stats.trials/cpu_freq_GHz/1000.0,UE->ulsch_rate_matching_stats.trials);
1499
        printf("|__ ULSCH sub-block interleaving time :%f us (%d trials)\n",
1500 1501
               ((double)UE->ulsch_interleaving_stats.trials/UE->ulsch_encoding_stats.trials)*(double)
               UE->ulsch_interleaving_stats.diff/UE->ulsch_interleaving_stats.trials/cpu_freq_GHz/1000.0,UE->ulsch_interleaving_stats.trials);
1502
        printf("|__ ULSCH multiplexing time           :%f us (%d trials)\n",
1503 1504
               ((double)UE->ulsch_multiplexing_stats.trials/UE->ulsch_encoding_stats.trials)*(double)
               UE->ulsch_multiplexing_stats.diff/UE->ulsch_multiplexing_stats.trials/cpu_freq_GHz/1000.0,UE->ulsch_multiplexing_stats.trials);
1505 1506

        printf("\n\neNB RX function statistics (per 1ms subframe)\n\n");
1507 1508 1509
        std_phy_proc_rx = sqrt((double)eNB->phy_proc_rx.diff_square/pow(cpu_freq_GHz,2)/pow(1000,
                               2)/eNB->phy_proc_rx.trials - pow((double)eNB->phy_proc_rx.diff/eNB->phy_proc_rx.trials/cpu_freq_GHz/1000,2));
        printf("Total PHY proc rx                  :%f us (%d trials)\n",(double)eNB->phy_proc_rx.diff/eNB->phy_proc_rx.trials/cpu_freq_GHz/1000.0,eNB->phy_proc_rx.trials);
1510 1511
        printf("|__ Statistcs                           std: %fus max: %fus min: %fus median %fus q1 %fus q3 %fus n_dropped: %d packet \n", std_phy_proc_rx, t_rx_max, t_rx_min, rx_median, rx_q1, rx_q3,
               n_rx_dropped);
1512 1513 1514 1515
        std_phy_proc_rx_fft = sqrt((double)eNB->ofdm_demod_stats.diff_square/pow(cpu_freq_GHz,2)/pow(1000,
                                   2)/eNB->ofdm_demod_stats.trials - pow((double)eNB->ofdm_demod_stats.diff/eNB->ofdm_demod_stats.trials/cpu_freq_GHz/1000,2));
        printf("OFDM_demod time                   :%f us (%d trials)\n",(double)eNB->ofdm_demod_stats.diff/eNB->ofdm_demod_stats.trials/cpu_freq_GHz/1000.0,
               eNB->ofdm_demod_stats.trials);
1516
        printf("|__ Statistcs                           std: %fus median %fus q1 %fus q3 %fus \n", std_phy_proc_rx_fft, rx_fft_median, rx_fft_q1, rx_fft_q3);
1517 1518 1519 1520
        std_phy_proc_rx_demod = sqrt((double)eNB->ulsch_demodulation_stats.diff_square/pow(cpu_freq_GHz,2)/pow(1000,
                                     2)/eNB->ulsch_demodulation_stats.trials - pow((double)eNB->ulsch_demodulation_stats.diff/eNB->ulsch_demodulation_stats.trials/cpu_freq_GHz/1000,2));
        printf("ULSCH demodulation time           :%f us (%d trials)\n",(double)eNB->ulsch_demodulation_stats.diff/eNB->ulsch_demodulation_stats.trials/cpu_freq_GHz/1000.0,
               eNB->ulsch_demodulation_stats.trials);
1521
        printf("|__ Statistcs                           std: %fus median %fus q1 %fus q3 %fus \n", std_phy_proc_rx_demod, rx_demod_median, rx_demod_q1, rx_demod_q3);
1522 1523
        std_phy_proc_rx_dec = sqrt((double)eNB->ulsch_decoding_stats.diff_square/pow(cpu_freq_GHz,2)/pow(1000,
                                   2)/eNB->ulsch_decoding_stats.trials - pow((double)eNB->ulsch_decoding_stats.diff/eNB->ulsch_decoding_stats.trials/cpu_freq_GHz/1000,2));
1524
        printf("ULSCH Decoding time (%.2f Mbit/s, avg iter %f)      :%f us (%d trials, max %f)\n",
1525 1526 1527
               UE->ulsch[0]->harq_processes[harq_pid]->TBS/1000.0,(double)avg_iter/iter_trials,
               (double)eNB->ulsch_decoding_stats.diff/eNB->ulsch_decoding_stats.trials/cpu_freq_GHz/1000.0,eNB->ulsch_decoding_stats.trials,
               (double)eNB->ulsch_decoding_stats.max/cpu_freq_GHz/1000.0);
1528 1529
        printf("|__ Statistcs                           std: %fus median %fus q1 %fus q3 %fus \n", std_phy_proc_rx_dec, rx_dec_median, rx_dec_q1, rx_dec_q3);
        printf("|__ sub-block interleaving                          %f us (%d trials)\n",
1530
               (double)eNB->ulsch_deinterleaving_stats.diff/eNB->ulsch_deinterleaving_stats.trials/cpu_freq_GHz/1000.0,eNB->ulsch_deinterleaving_stats.trials);
1531
        printf("|__ demultiplexing                                  %f us (%d trials)\n",
1532
               (double)eNB->ulsch_demultiplexing_stats.diff/eNB->ulsch_demultiplexing_stats.trials/cpu_freq_GHz/1000.0,eNB->ulsch_demultiplexing_stats.trials);
1533
        printf("|__ rate-matching                                   %f us (%d trials)\n",
1534
               (double)eNB->ulsch_rate_unmatching_stats.diff/eNB->ulsch_rate_unmatching_stats.trials/cpu_freq_GHz/1000.0,eNB->ulsch_rate_unmatching_stats.trials);
1535
        printf("|__ turbo_decoder(%d bits)                              %f us (%d cycles, %d trials)\n",
1536 1537 1538
               eNB->ulsch[0]->harq_processes[harq_pid]->Cminus ? eNB->ulsch[0]->harq_processes[harq_pid]->Kminus : eNB->ulsch[0]->harq_processes[harq_pid]->Kplus,
               (double)eNB->ulsch_turbo_decoding_stats.diff/eNB->ulsch_turbo_decoding_stats.trials/cpu_freq_GHz/1000.0,
               (int)((double)eNB->ulsch_turbo_decoding_stats.diff/eNB->ulsch_turbo_decoding_stats.trials),eNB->ulsch_turbo_decoding_stats.trials);
1539
        printf("    |__ init                                            %f us (cycles/iter %f, %d trials)\n",
1540 1541 1542
               (double)eNB->ulsch_tc_init_stats.diff/eNB->ulsch_tc_init_stats.trials/cpu_freq_GHz/1000.0,
               (double)eNB->ulsch_tc_init_stats.diff/eNB->ulsch_tc_init_stats.trials/((double)avg_iter/iter_trials),
               eNB->ulsch_tc_init_stats.trials);
1543
        printf("    |__ alpha                                           %f us (cycles/iter %f, %d trials)\n",
1544 1545 1546
               (double)eNB->ulsch_tc_alpha_stats.diff/eNB->ulsch_tc_alpha_stats.trials/cpu_freq_GHz/1000.0,
               (double)eNB->ulsch_tc_alpha_stats.diff/eNB->ulsch_tc_alpha_stats.trials*2,
               eNB->ulsch_tc_alpha_stats.trials);
1547
        printf("    |__ beta                                            %f us (cycles/iter %f,%d trials)\n",
1548 1549 1550
               (double)eNB->ulsch_tc_beta_stats.diff/eNB->ulsch_tc_beta_stats.trials/cpu_freq_GHz/1000.0,
               (double)eNB->ulsch_tc_beta_stats.diff/eNB->ulsch_tc_beta_stats.trials*2,
               eNB->ulsch_tc_beta_stats.trials);
1551
        printf("    |__ gamma                                           %f us (cycles/iter %f,%d trials)\n",
1552 1553 1554
               (double)eNB->ulsch_tc_gamma_stats.diff/eNB->ulsch_tc_gamma_stats.trials/cpu_freq_GHz/1000.0,
               (double)eNB->ulsch_tc_gamma_stats.diff/eNB->ulsch_tc_gamma_stats.trials*2,
               eNB->ulsch_tc_gamma_stats.trials);
1555
        printf("    |__ ext                                             %f us (cycles/iter %f,%d trials)\n",
1556 1557 1558
               (double)eNB->ulsch_tc_ext_stats.diff/eNB->ulsch_tc_ext_stats.trials/cpu_freq_GHz/1000.0,
               (double)eNB->ulsch_tc_ext_stats.diff/eNB->ulsch_tc_ext_stats.trials*2,
               eNB->ulsch_tc_ext_stats.trials);
1559
        printf("    |__ intl1                                           %f us (cycles/iter %f,%d trials)\n",
1560 1561 1562
               (double)eNB->ulsch_tc_intl1_stats.diff/eNB->ulsch_tc_intl1_stats.trials/cpu_freq_GHz/1000.0,
               (double)eNB->ulsch_tc_intl1_stats.diff/eNB->ulsch_tc_intl1_stats.trials,
               eNB->ulsch_tc_intl1_stats.trials);
1563
        printf("    |__ intl2+HD+CRC                                    %f us (cycles/iter %f,%d trials)\n",
1564 1565 1566
               (double)eNB->ulsch_tc_intl2_stats.diff/eNB->ulsch_tc_intl2_stats.trials/cpu_freq_GHz/1000.0,
               (double)eNB->ulsch_tc_intl2_stats.diff/eNB->ulsch_tc_intl2_stats.trials,
               eNB->ulsch_tc_intl2_stats.trials);
1567 1568
      }

1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
      if(abstx) { //ABSTRACTION
        blerr= (double)errs[1]/(round_trials[1]);
        //printf("hata yok XX,");


        blerr = (double)errs[0]/(round_trials[0]);

        if(saving_bler==0)
          fprintf(csv_fdUL,"%e;\n",blerr);

        //    printf("hata yok XX,");
1580 1581


1582 1583 1584
        if(blerr<1)
          saving_bler = 0;
        else saving_bler =1;
1585 1586 1587


      } //ABStraction
1588

1589
      if ( (test_perf != 0) && (100 * effective_rate > test_perf )) {
1590 1591 1592 1593
        //fprintf(time_meas_fd,"SNR; MCS; TBS; rate; err0; trials0; err1; trials1; err2; trials2; err3; trials3\n");
        fprintf(time_meas_fd,"%f;%d;%d;%f;%d;%d;%d;%d;%d;%d;%d;%d;",
                SNR,
                mcs,
1594
                eNB->ulsch[0]->harq_processes[harq_pid]->TBS,
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
                rate,
                errs[0],
                round_trials[0],
                errs[1],
                round_trials[1],
                errs[2],
                round_trials[2],
                errs[3],
                round_trials[3]);

        //fprintf(time_meas_fd,"SNR; MCS; TBS; rate; err0; trials0; err1; trials1; err2; trials2; err3; trials3;ND;\n");
        fprintf(time_meas_fd,"%f;%d;%d;%f;%2.1f;%f;%d;%d;%d;%d;%d;%d;%d;%d;%e;%e;%e;%e;%f;%f;",
                SNR,
                mcs,
1609
                eNB->ulsch[0]->harq_processes[harq_pid]->TBS,
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
                rate*effective_rate,
                100*effective_rate,
                rate,
                errs[0],
                round_trials[0],
                errs[1],
                round_trials[1],
                errs[2],
                round_trials[2],
                errs[3],
                round_trials[3],
                (double)errs[0]/(round_trials[0]),
                (double)errs[1]/(round_trials[0]),
                (double)errs[2]/(round_trials[0]),
                (double)errs[3]/(round_trials[0]),
                (1.0*(round_trials[0]-errs[0])+2.0*(round_trials[1]-errs[1])+3.0*(round_trials[2]-errs[2])+4.0*(round_trials[3]-errs[3]))/((double)round_trials[0])/
1626
                (double)eNB->ulsch[0]->harq_processes[harq_pid]->TBS,
1627 1628 1629 1630
                (1.0*(round_trials[0]-errs[0])+2.0*(round_trials[1]-errs[1])+3.0*(round_trials[2]-errs[2])+4.0*(round_trials[3]-errs[3]))/((double)round_trials[0]));

        //fprintf(time_meas_fd,"UE_PROC_TX(%d); OFDM_MOD(%d); UL_MOD(%d); UL_ENC(%d); eNB_PROC_RX(%d); OFDM_DEMOD(%d); UL_DEMOD(%d); UL_DECOD(%d);\n",
        fprintf(time_meas_fd,"%d; %d; %d; %d; %d; %d; %d; %d;",
1631 1632 1633 1634 1635 1636 1637 1638
                UE->phy_proc_tx.trials,
                UE->ofdm_mod_stats.trials,
                UE->ulsch_modulation_stats.trials,
                UE->ulsch_encoding_stats.trials,
                eNB->phy_proc_rx.trials,
                eNB->ofdm_demod_stats.trials,
                eNB->ulsch_demodulation_stats.trials,
                eNB->ulsch_decoding_stats.trials
1639 1640
               );
        fprintf(time_meas_fd,"%f;%f;%f;%f;%f;%f;%f;%f;",
1641 1642 1643 1644 1645 1646 1647 1648
                get_time_meas_us(&UE->phy_proc_tx),
                get_time_meas_us(&UE->ofdm_mod_stats),
                get_time_meas_us(&UE->ulsch_modulation_stats),
                get_time_meas_us(&UE->ulsch_encoding_stats),
                get_time_meas_us(&eNB->phy_proc_rx),
                get_time_meas_us(&eNB->ofdm_demod_stats),
                get_time_meas_us(&eNB->ulsch_demodulation_stats),
                get_time_meas_us(&eNB->ulsch_decoding_stats)
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
               );

        //fprintf(time_meas_fd,"UE_PROC_TX_STD;UE_PROC_TX_MAX;UE_PROC_TX_MIN;UE_PROC_TX_MED;UE_PROC_TX_Q1;UE_PROC_TX_Q3;UE_PROC_TX_DROPPED;\n");
        fprintf(time_meas_fd,"%f;%f;%f;%f;%f;%f;%d;", std_phy_proc_tx, t_tx_max, t_tx_min, tx_median, tx_q1, tx_q3, n_tx_dropped);

        //fprintf(time_meas_fd,"IFFT;\n");
        fprintf(time_meas_fd,"%f;%f;%f;%f;", std_phy_proc_tx_ifft, tx_ifft_median, tx_ifft_q1, tx_ifft_q3);

        //fprintf(time_meas_fd,"MOD;\n");
        fprintf(time_meas_fd,"%f;%f;%f;%f;", std_phy_proc_tx_mod, tx_mod_median, tx_mod_q1, tx_mod_q3);

        //fprintf(time_meas_fd,"ENC;\n");
        fprintf(time_meas_fd,"%f;%f;%f;%f;", std_phy_proc_tx_enc, tx_enc_median, tx_enc_q1, tx_enc_q3);

        //fprintf(time_meas_fd,"eNB_PROC_RX_STD;eNB_PROC_RX_MAX;eNB_PROC_RX_MIN;eNB_PROC_RX_MED;eNB_PROC_RX_Q1;eNB_PROC_RX_Q3;eNB_PROC_RX_DROPPED;\n");
        fprintf(time_meas_fd,"%f;%f;%f;%f;%f;%f;%d;", std_phy_proc_rx, t_rx_max, t_rx_min, rx_median, rx_q1, rx_q3, n_rx_dropped);

        //fprintf(time_meas_fd,"FFT;\n");
        fprintf(time_meas_fd,"%f;%f;%f;%f;", std_phy_proc_rx_fft, rx_fft_median, rx_fft_q1, rx_fft_q3);

        //fprintf(time_meas_fd,"DEMOD;\n");
        fprintf(time_meas_fd,"%f;%f;%f;%f;", std_phy_proc_rx_demod,rx_demod_median, rx_demod_q1, rx_demod_q3);

        //fprintf(time_meas_fd,"DEC;\n");
        fprintf(time_meas_fd,"%f;%f;%f;%f\n", std_phy_proc_rx_dec, rx_dec_median, rx_dec_q1, rx_dec_q3);


        printf("[passed] effective rate : %f  (%2.1f%%,%f)): log and break \n",rate*effective_rate, 100*effective_rate, rate );
        break;
      } else if (test_perf !=0 ) {
        printf("[continue] effective rate : %f  (%2.1f%%,%f)): increase snr \n",rate*effective_rate, 100*effective_rate, rate);
1680 1681 1682
      }


1683 1684
      if (((double)errs[0]/(round_trials[0]))<1e-2)
        break;
1685
  } // SNR
1686 1687 1688

    //

1689

1690 1691
    //write_output("chestim_f.m","chestf",eNB->pusch_vars[0]->drs_ch_estimates[0][0],300*12,2,1);
    // write_output("chestim_t.m","chestt",eNB->pusch_vars[0]->drs_ch_estimates_time[0][0], (frame_parms->ofdm_symbol_size)*2,2,1);
1692 1693 1694 1695

  }//ch realization


1696 1697
  oai_exit=1;
  pthread_cond_signal(&eNB->proc.cond_fep);
1698

1699
  if (abstx) { // ABSTRACTION
1700 1701 1702
    fprintf(csv_fdUL,"];");
    fclose(csv_fdUL);
  }
1703

1704
  fclose(bler_fd);
1705

1706 1707
  if (test_perf !=0)
    fclose (time_meas_fd);
1708

1709 1710 1711
  return(0);

}
1712

1713 1714