prachsim.c 16.7 KB
Newer Older
ghaddab's avatar
ghaddab committed
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19 20 21 22 23
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
ghaddab's avatar
ghaddab committed
27 28

 *******************************************************************************/
29 30 31 32 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 "SIMULATION/RF/defs.h"
#include "PHY/types.h"
#include "PHY/defs.h"
#include "PHY/vars.h"
#ifdef EMOS
#include "SCHED/phy_procedures_emos.h"
#endif
#include "SCHED/defs.h"
#include "SCHED/vars.h"
#include "LAYER2/MAC/vars.h"

#include "OCG_vars.h"

46 47
#include "unitary_defs.h"

48
int current_dlsch_cqi; //FIXME!
49

50 51
PHY_VARS_eNB *eNB;
PHY_VARS_UE *UE;
52 53 54 55 56

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



57
extern uint16_t prach_root_sequence_map0_3[838];
58

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

61 62
int main(int argc, char **argv)
{
63 64 65 66 67

  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;
68 69
  uint8_t snr1set=0;
  uint8_t ue_speed1set=0;
70 71 72 73
  //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
74
  int trial; //, ntrials=1;
75 76
  uint8_t transmission_mode = 1,n_tx=1,n_rx=1;
  uint16_t Nid_cell=0;
77

78 79
  uint8_t awgn_flag=0;
  uint8_t hs_flag=0;
80 81
  int n_frames=1;
  channel_desc_t *UE2eNB;
Cedric Roux's avatar
Cedric Roux committed
82
  uint32_t nsymb,tx_lev; //,tx_lev_dB;
83 84
  uint8_t extended_prefix_flag=0;
  //  int8_t interf1=-19,interf2=-19;
85 86 87 88 89 90 91 92
  LTE_DL_FRAME_PARMS *frame_parms;
#ifdef EMOS
  fifo_dump_emos emos_dump;
#endif


  SCM_t channel_model=Rayleigh1;

93
  //  uint8_t abstraction_flag=0,calibration_flag=0;
94
  //  double prach_sinr;
95 96 97 98 99
  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;
100
  PRACH_RESOURCES_t prach_resources;
Cedric Roux's avatar
Cedric Roux committed
101 102
  //uint8_t prach_fmt;
  //int N_ZC;
103 104 105 106 107 108 109
  int delay = 0;
  double delay_avg=0;
  double ue_speed = 0;
  int NCS_config = 1,rootSequenceIndex=0;
  logInit();

  number_of_cards = 1;
110 111


112

113 114 115 116 117
  while ((c = getopt (argc, argv, "hHaA:Cr:p:g:n:s:S:t:x:y:v:V:z:N:F:d:Z:L:R:")) != -1) {
    switch (c) {
    case 'a':
      printf("Running AWGN simulation\n");
      awgn_flag = 1;
Cedric Roux's avatar
Cedric Roux committed
118 119
      /* ntrials not used later, no need to set */
      //ntrials=1;
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 175 176 177 178 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 204 205 206 207 208 209
      break;

    case 'd':
      delay = atoi(optarg);
      break;

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

      case 'B':
        channel_model=SCM_B;
        break;

      case 'C':
        channel_model=SCM_C;
        break;

      case 'D':
        channel_model=SCM_D;
        break;

      case 'E':
        channel_model=EPA;
        break;

      case 'F':
        channel_model=EVA;
        break;

      case 'G':
        channel_model=ETU;
        break;

      case 'H':
        channel_model=Rayleigh8;

      case 'I':
        channel_model=Rayleigh1;

      case 'J':
        channel_model=Rayleigh1_corr;

      case 'K':
        channel_model=Rayleigh1_anticorr;

      case 'L':
        channel_model=Rice8;

      case 'M':
        channel_model=Rice1;

      case 'N':
        channel_model=Rayleigh1_800;
        break;

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

      break;

    case 'n':
      n_frames = atoi(optarg);
      break;

    case 's':
      snr0 = atof(optarg);
      msg("Setting SNR0 to %f\n",snr0);
      break;

    case 'S':
      snr1 = atof(optarg);
      snr1set=1;
      msg("Setting SNR1 to %f\n",snr1);
      break;

    case 'p':
      preamble_tx=atoi(optarg);
      break;

    case 'v':
      ue_speed0 = atoi(optarg);
      break;

    case 'V':
      ue_speed1 = atoi(optarg);
210
      ue_speed1set = 1;
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 299 300 301 302 303 304
      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;
305
    }
306
  }
307 308 309 310

  if (transmission_mode==2)
    n_tx=2;

311 312 313 314 315 316 317 318
  lte_param_init(n_tx,
		 n_rx,
		 transmission_mode,
		 extended_prefix_flag,
		 FDD,
		 Nid_cell,
		 3,
		 N_RB_DL,
319
		 0,
320 321
		 osf,
		 0);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339


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

340
  frame_parms = &eNB->frame_parms;
341 342


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

346 347 348 349 350 351 352
  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,
353 354
         frame_parms->Ncp,frame_parms->samples_per_tti,nsymb);

355 356 357


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

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

372
  for (i=0; i<2; i++) {
373 374 375 376 377 378 379 380 381 382 383

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

385 386 387 388 389 390
  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;

391

392 393 394 395 396
  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;
397

398 399
  eNB->node_function = eNodeB_3GPP;
  eNB->proc.subframe_rx = subframe;
400

Cedric Roux's avatar
Cedric Roux committed
401
  /* N_ZC not used later, so prach_fmt is also useless, don't set */
402 403
  //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
404 405
  /* N_ZC not used later, no need to set */
  //N_ZC = (prach_fmt <4)?839:139;
406

407
  compute_prach_seq(&eNB->frame_parms.prach_config_common,eNB->frame_parms.frame_type,eNB->X_u);
408

409
  compute_prach_seq(&UE->frame_parms.prach_config_common,UE->frame_parms.frame_type,UE->X_u);
410

411
  UE->prach_vars[0]->amp = AMP;
412

413
  UE->prach_resources[0] = &prach_resources;
414

415
  if (preamble_tx == 99)
416
    preamble_tx = (uint16_t)(taus()&0x3f);
417

418
  if (n_frames == 1)
419
    printf("raPreamble %d\n",preamble_tx);
420

421 422
  UE->prach_resources[0]->ra_PreambleIndex = preamble_tx;
  UE->prach_resources[0]->ra_TDD_map_index = 0;
423

424
  tx_lev = generate_prach(UE,
425 426 427
                          0, //eNB_id,
                          subframe,
                          0); //Nf
428

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

432
  write_output("txsig0_new.m","txs0", &txdata[0][subframe*frame_parms->samples_per_tti],frame_parms->samples_per_tti,1,1);
433
  //write_output("txsig1.m","txs1", txdata[1],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);
434

435
  // multipath channel
436
  dump_prach_config(&eNB->frame_parms,subframe);
437

438 439
  for (i=0; i<2*frame_parms->samples_per_tti; i++) {
    for (aa=0; aa<1; aa++) {
440
      if (awgn_flag == 0) {
441 442 443
        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 {
444
        for (aarx=0; aarx<eNB->frame_parms.nb_antennas_rx; aarx++) {
445 446 447 448 449 450 451 452
          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]);
          }
        }
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 487 488 489 490
  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++) {
491
          for (aa=0; aa<eNB->frame_parms.nb_antennas_rx; aa++) {
492

493 494
            ((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)));
495 496 497
          }
        }

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

        if (preamble_max!=preamble_tx)
          prach_errors++;
        else {
          delay_avg += (double)preamble_delay_list[preamble_max];
        }

        if (n_frames==1) {
          for (i=0; i<64; i++)
            if (i==preamble_tx)
              printf("****** preamble %d : energy %d, delay %d\n",i,preamble_energy_list[i],preamble_delay_list[i]);
            else
              printf("preamble %d : energy %d, delay %d\n",i,preamble_energy_list[i],preamble_delay_list[i]);

          write_output("prach0.m","prach0", &txdata[0][subframe*frame_parms->samples_per_tti],frame_parms->samples_per_tti,1,1);
529
          write_output("prachF0.m","prachF0", &eNB->prach_vars.prachF[0],24576,1,1);
530
          write_output("rxsig0.m","rxs0",
531
                       &eNB->common_vars.rxdata[0][0][subframe*frame_parms->samples_per_tti],
532
                       frame_parms->samples_per_tti,1,1);
533 534
          write_output("rxsigF0.m","rxsF0", &eNB->common_vars.rxdataF[0][0][0],512*nsymb*2,2,1);
          write_output("prach_preamble.m","prachp",&eNB->X_u[0],839,1,1);
535
        }
536 537
      }

538 539 540 541 542 543 544 545
      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

546

547
  for (i=0; i<2; i++) {
548 549 550 551 552
    free(s_re[i]);
    free(s_im[i]);
    free(r_re[i]);
    free(r_im[i]);
  }
553

554 555 556 557
  free(s_re);
  free(s_im);
  free(r_re);
  free(r_im);
558

559 560 561 562 563 564 565
  lte_sync_time_free();

  return(0);

}


566 567

/*
568 569
  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],
570 571
     (void *)&PHY_vars->tx_vars[0].TX_DMA_BUFFER[0],
     12*OFDM_SYMBOL_SIZE_SAMPLES_NO_PREFIX*2);
572 573
*/