prachsim.c 16.5 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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
 */

22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "SIMULATION/TOOLS/defs.h"
#include "SIMULATION/RF/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"

36 37
#include "unitary_defs.h"

38
int current_dlsch_cqi; //FIXME!
39

40 41
PHY_VARS_eNB *eNB;
PHY_VARS_UE *UE;
42 43 44

#define DLSCH_RB_ALLOC 0x1fbf // igore DC component,RB13

45
double cpuf;
46

47
extern uint16_t prach_root_sequence_map0_3[838];
48

Cedric Roux's avatar
Cedric Roux committed
49 50
void dump_prach_config(LTE_DL_FRAME_PARMS *frame_parms,uint8_t subframe);

51 52
int main(int argc, char **argv)
{
53 54 55 56 57

  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;
58 59
  uint8_t snr1set=0;
  uint8_t ue_speed1set=0;
60 61 62 63
  //mod_sym_t **txdataF;
  int **txdata;
  double **s_re,**s_im,**r_re,**r_im;
  double iqim=0.0;
Cedric Roux's avatar
Cedric Roux committed
64
  int trial; //, ntrials=1;
65 66
  uint8_t transmission_mode = 1,n_tx=1,n_rx=1;
  uint16_t Nid_cell=0;
67

68 69
  uint8_t awgn_flag=0;
  uint8_t hs_flag=0;
70 71
  int n_frames=1;
  channel_desc_t *UE2eNB;
Cedric Roux's avatar
Cedric Roux committed
72
  uint32_t nsymb,tx_lev; //,tx_lev_dB;
73 74
  uint8_t extended_prefix_flag=0;
  //  int8_t interf1=-19,interf2=-19;
75 76 77 78
  LTE_DL_FRAME_PARMS *frame_parms;

  SCM_t channel_model=Rayleigh1;

79
  //  uint8_t abstraction_flag=0,calibration_flag=0;
80
  //  double prach_sinr;
81 82 83 84 85
  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;
86
  PRACH_RESOURCES_t prach_resources;
Cedric Roux's avatar
Cedric Roux committed
87 88
  //uint8_t prach_fmt;
  //int N_ZC;
89 90 91 92
  int delay = 0;
  double delay_avg=0;
  double ue_speed = 0;
  int NCS_config = 1,rootSequenceIndex=0;
93 94
  int threequarter_fs = 0;

95 96
  cpuf = get_cpu_freq_GHz();

97 98 99
  logInit();

  number_of_cards = 1;
100 101


102

103
  while ((c = getopt (argc, argv, "hHaA:Cr:p:g:n:s:S:t:x:y:v:V:z:N:F:d:Z:L:R:E")) != -1) {
104 105 106 107
    switch (c) {
    case 'a':
      printf("Running AWGN simulation\n");
      awgn_flag = 1;
Cedric Roux's avatar
Cedric Roux committed
108 109
      /* ntrials not used later, no need to set */
      //ntrials=1;
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 166 167 168 169 170 171 172 173 174
      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;

175 176 177 178
    case 'E':
      threequarter_fs=1;
      break;

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    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);
204
      ue_speed1set = 1;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
      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;
299
    }
300
  }
301 302 303 304

  if (transmission_mode==2)
    n_tx=2;

305
  lte_param_init(n_tx,
306
                 n_tx,
307 308 309 310 311 312 313
		 n_rx,
		 transmission_mode,
		 extended_prefix_flag,
		 FDD,
		 Nid_cell,
		 3,
		 N_RB_DL,
314
		 threequarter_fs,
315 316
		 osf,
		 0);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334


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

335
  frame_parms = &eNB->frame_parms;
336 337


338
  txdata = UE->common_vars.txdata;
339
  printf("txdata %p\n",&txdata[0][subframe*frame_parms->samples_per_tti]);
340

341 342 343 344 345 346 347
  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,
348 349
         frame_parms->Ncp,frame_parms->samples_per_tti,nsymb);

350 351 352


  msg("[SIM] Using SCM/101\n");
353 354
  UE2eNB = new_channel_desc_scm(UE->frame_parms.nb_antennas_tx,
                                eNB->frame_parms.nb_antennas_rx,
355
                                channel_model,
356 357
				N_RB2sampling_rate(eNB->frame_parms.N_RB_UL),
				N_RB2channel_bandwidth(eNB->frame_parms.N_RB_UL),
358 359 360
                                0.0,
                                delay,
                                0);
361 362 363 364 365 366

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

367
  for (i=0; i<2; i++) {
368 369 370 371 372 373 374 375 376 377 378

    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));
  }
379

380 381 382 383 384 385
  UE->frame_parms.prach_config_common.rootSequenceIndex=rootSequenceIndex;
  UE->frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex=0;
  UE->frame_parms.prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig=NCS_config;
  UE->frame_parms.prach_config_common.prach_ConfigInfo.highSpeedFlag=hs_flag;
  UE->frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset=0;

386

387 388 389 390 391
  eNB->frame_parms.prach_config_common.rootSequenceIndex=rootSequenceIndex;
  eNB->frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex=0;
  eNB->frame_parms.prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig=NCS_config;
  eNB->frame_parms.prach_config_common.prach_ConfigInfo.highSpeedFlag=hs_flag;
  eNB->frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset=0;
392

393 394 395
  eNB->node_function       = eNodeB_3GPP;
  eNB->proc.subframe_rx    = subframe;
  eNB->proc.subframe_prach = subframe;
396

Cedric Roux's avatar
Cedric Roux committed
397
  /* N_ZC not used later, so prach_fmt is also useless, don't set */
398 399
  //prach_fmt = get_prach_fmt(eNB->frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex,
  //                          eNB->frame_parms.frame_type);
Cedric Roux's avatar
Cedric Roux committed
400 401
  /* N_ZC not used later, no need to set */
  //N_ZC = (prach_fmt <4)?839:139;
402

403
  compute_prach_seq(&eNB->frame_parms.prach_config_common,eNB->frame_parms.frame_type,eNB->X_u);
404

405
  compute_prach_seq(&UE->frame_parms.prach_config_common,UE->frame_parms.frame_type,UE->X_u);
406

407
  UE->prach_vars[0]->amp = AMP;
408

409
  UE->prach_resources[0] = &prach_resources;
410

411
  if (preamble_tx == 99)
412
    preamble_tx = (uint16_t)(taus()&0x3f);
413

414
  if (n_frames == 1)
415
    printf("raPreamble %d\n",preamble_tx);
416

417 418
  UE->prach_resources[0]->ra_PreambleIndex = preamble_tx;
  UE->prach_resources[0]->ra_TDD_map_index = 0;
419

420
  tx_lev = generate_prach(UE,
421 422 423
                          0, //eNB_id,
                          subframe,
                          0); //Nf
424

Cedric Roux's avatar
Cedric Roux committed
425 426
  /* tx_lev_dB not used later, no need to set */
  //tx_lev_dB = (unsigned int) dB_fixed(tx_lev);
427

Mongazon's avatar
Mongazon committed
428 429
  LOG_M("txsig0_new.m","txs0", &txdata[0][subframe*frame_parms->samples_per_tti],frame_parms->samples_per_tti,1,1);
  //LOG_M("txsig1.m","txs1", txdata[1],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);
430

431
  // multipath channel
432
  dump_prach_config(&eNB->frame_parms,subframe);
433

434 435
  for (i=0; i<2*frame_parms->samples_per_tti; i++) {
    for (aa=0; aa<1; aa++) {
436
      if (awgn_flag == 0) {
437 438 439
        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 {
440
        for (aarx=0; aarx<eNB->frame_parms.nb_antennas_rx; aarx++) {
441 442 443 444 445 446 447 448
          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]);
          }
        }
449 450 451 452 453 454
      }
    }
  }



455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
  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++) {
487
          for (aa=0; aa<eNB->frame_parms.nb_antennas_rx; aa++) {
488

489 490
            ((short*) &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*) &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)));
491 492 493
          }
        }

494
        rx_prach(eNB,
495 496 497 498 499 500 501 502 503 504
                 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]) {
505
            //   printf("preamble %d => %d\n",i,preamble_energy_list[i]);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
            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]);

Mongazon's avatar
Mongazon committed
524 525 526
          LOG_M("prach0.m","prach0", &txdata[0][subframe*frame_parms->samples_per_tti],frame_parms->samples_per_tti,1,1);
          LOG_M("prachF0.m","prachF0", &eNB->prach_vars.prachF[0],24576,1,1);
          LOG_M("rxsig0.m","rxs0",
527
                       &eNB->common_vars.rxdata[0][0][subframe*frame_parms->samples_per_tti],
528
                       frame_parms->samples_per_tti,1,1);
Mongazon's avatar
Mongazon committed
529 530
          LOG_M("rxsigF0.m","rxsF0", eNB->prach_vars.rxsigF[0],6144,1,1);
          LOG_M("prach_preamble.m","prachp",&eNB->X_u[0],839,1,1);
531
        }
532 533
      }

534 535 536 537 538 539 540 541
      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

542

543
  for (i=0; i<2; i++) {
544 545 546 547 548
    free(s_re[i]);
    free(s_im[i]);
    free(r_re[i]);
    free(r_im[i]);
  }
549

550 551 552 553
  free(s_re);
  free(s_im);
  free(r_re);
  free(r_im);
554

555 556 557 558 559 560 561
  lte_sync_time_free();

  return(0);

}


562 563

/*
564 565
  for (i=1;i<4;i++)
    memcpy((void *)&PHY_vars->tx_vars[0].TX_DMA_BUFFER[i*12*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES_NO_PREFIX*2],
566 567
     (void *)&PHY_vars->tx_vars[0].TX_DMA_BUFFER[0],
     12*OFDM_SYMBOL_SIZE_SAMPLES_NO_PREFIX*2);
568 569
*/