pbchsim.c 36.6 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 46 47 48 49 50 51 52 53 54
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.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"

#ifdef XFORMS
#include "PHY/TOOLS/lte_phy_scope.h"
#endif

#include "OCG_vars.h"

55
PHY_VARS_eNB *PHY_vars_eNB,*PHY_vars_eNB1,*PHY_vars_eNB2;
56 57 58 59
PHY_VARS_UE *PHY_vars_UE;

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

60 61 62 63 64
int32_t *dummybuf[4];
int32_t dummy0[2048*14];
int32_t dummy1[2048*14];
int32_t dummy2[2048*14];
int32_t dummy3[2048*14];
65 66


67 68
int main(int argc, char **argv)
{
69 70 71 72 73

  char c;

  int i,l,aa;
  double sigma2, sigma2_dB=0,SNR,snr0=-2.0,snr1;
74
  uint8_t snr1set=0;
75 76 77 78 79 80
  int **txdata,**txdata1,**txdata2;
  double **s_re,**s_im,**s_re1,**s_im1,**s_re2,**s_im2,**r_re,**r_im,**r_re1,**r_im1,**r_re2,**r_im2;
  double iqim = 0.0;
  unsigned char pbch_pdu[6];
  //  int sync_pos, sync_pos_slot;
  //  FILE *rx_frame_file;
Cedric Roux's avatar
Cedric Roux committed
81
  FILE *output_fd = NULL;
82
  uint8_t write_output_file=0;
Cedric Roux's avatar
Cedric Roux committed
83
  //int result;
84 85 86 87
  int freq_offset;
  //  int subframe_offset;
  //  char fname[40], vname[40];
  int trial, n_trials, ntrials=1, n_errors,n_errors2,n_alamouti;
88 89
  uint8_t transmission_mode = 1,n_tx=1,n_rx=1;
  uint16_t Nid_cell=0;
90 91

  int n_frames=1;
Cedric Roux's avatar
Cedric Roux committed
92 93
  channel_desc_t *eNB2UE,*eNB2UE1 = NULL,*eNB2UE2 = NULL;
  uint32_t nsymb,tx_lev,tx_lev1 = 0,tx_lev2 = 0;
94 95
  uint8_t extended_prefix_flag=0;
  int8_t interf1=-21,interf2=-21;
96 97 98 99 100 101 102 103
  LTE_DL_FRAME_PARMS *frame_parms;
#ifdef EMOS
  fifo_dump_emos emos_dump;
#endif

  FILE *input_fd=NULL,*pbch_file_fd=NULL;
  char input_val_str[50],input_val_str2[50];
  //  double input_val1,input_val2;
104
  //  uint16_t amask=0;
Cedric Roux's avatar
Cedric Roux committed
105
  uint8_t frame_mod4,num_pdcch_symbols = 0;
106
  uint16_t NB_RB=25;
107 108 109

  SCM_t channel_model=AWGN;//Rayleigh1_anticorr;

Cedric Roux's avatar
Cedric Roux committed
110
  //DCI_ALLOC_t dci_alloc[8];
111
  uint8_t abstraction_flag=0;//,calibration_flag=0;
112 113
  double pbch_sinr;
  int pbch_tx_ant;
114
  uint8_t N_RB_DL=25,osf=1;
115 116 117 118 119 120 121 122 123 124 125 126

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

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

  logInit();
  number_of_cards = 1;
  openair_daq_vars.rx_rf_mode = 1;
127

128 129 130 131
  /*
    rxdataF    = (int **)malloc16(2*sizeof(int*));
    rxdataF[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
    rxdataF[1] = (int *)malloc16(FRAME_LENGTH_BYTES);
132

133 134 135 136
    rxdata    = (int **)malloc16(2*sizeof(int*));
    rxdata[0] = (int *)malloc16(FRAME_LENGTH_BYTES);
    rxdata[1] = (int *)malloc16(FRAME_LENGTH_BYTES);
  */
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 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  while ((c = getopt (argc, argv, "f:hA:pf:g:i:j:n:s:S:t:x:y:z:N:F:GR:O:dP:")) != -1) {
    switch (c) {
    case 'f':
      write_output_file=1;
      output_fd = fopen(optarg,"w");

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

      break;

    case 'd':
      frame_type = 1;
      break;

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

      case 'B':
        channel_model=SCM_B;
        break;

      case 'C':
        channel_model=SCM_C;
        break;

      case 'D':
        channel_model=SCM_D;
        break;

      case 'E':
        channel_model=EPA;
        break;

      case 'F':
        channel_model=EVA;
        break;

      case 'G':
        channel_model=ETU;
        break;

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

      break;

    case 'i':
      interf1=atoi(optarg);
      break;

    case 'j':
      interf2=atoi(optarg);
      break;

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

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

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

      /*
      case 't':
      Td= atof(optarg);
      break;
      */
    case 'p':
      extended_prefix_flag=1;
      break;

      /*
      case 'r':
      ricean_factor = pow(10,-.1*atof(optarg));
      if (ricean_factor>1) {
        printf("Ricean factor must be between 0 and 1\n");
        exit(-1);
      }
      break;
      */
    case 'x':
      transmission_mode=atoi(optarg);

      if ((transmission_mode!=1) &&
          (transmission_mode!=2) &&
          (transmission_mode!=6)) {
        msg("Unsupported transmission mode %d\n",transmission_mode);
        exit(-1);
      }

      break;

    case 'y':
      n_tx=atoi(optarg);

      if ((n_tx==0) || (n_tx>2)) {
        msg("Unsupported number of tx antennas %d\n",n_tx);
        exit(-1);
      }

      break;

    case 'z':
      n_rx=atoi(optarg);

      if ((n_rx==0) || (n_rx>2)) {
        msg("Unsupported number of rx antennas %d\n",n_rx);
        exit(-1);
      }

      break;

    case 'A':
      abstraction_flag=1;
      ntrials=10000;
      msg("Running Abstraction test\n");
      pbch_file_fd=fopen(optarg,"r");

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

      break;

      //  case 'C':
      //    calibration_flag=1;
      //    msg("Running Abstraction calibration for Bias removal\n");
      //    break;
    case 'N':
      Nid_cell = atoi(optarg);
      break;

    case 'R':
      N_RB_DL = atoi(optarg);
      break;

    case 'O':
      osf = atoi(optarg);
      break;

    case 'F':
      input_fd = fopen(optarg,"r");

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

      break;

    case 'P':
      pbch_phase = atoi(optarg);

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

      break;

    default:
    case 'h':
      printf("%s -h(elp) -p(extended_prefix) -N cell_id -f output_filename -F input_filename -g channel_model -n n_frames -t Delayspread -s snr0 -S snr1 -x transmission_mode -y TXant -z RXant -i Intefrence0 -j Interference1 -A interpolation_file -C(alibration offset dB) -N CellId\n",
             argv[0]);
      printf("-h This message\n");
      printf("-p Use extended prefix mode\n");
      printf("-d Use TDD\n");
      printf("-n Number of frames to simulate\n");
      printf("-s Starting SNR, runs from SNR0 to SNR0 + 5 dB.  If n_frames is 1 then just SNR is simulated\n");
      printf("-S Ending SNR, runs from SNR0 to SNR1\n");
      printf("-t Delay spread for multipath channel\n");
      printf("-g [A,B,C,D,E,F,G] Use 3GPP SCM (A,B,C,D) or 36-101 (E-EPA,F-EVA,G-ETU) models (ignores delay spread and Ricean factor)\n");
      printf("-x Transmission mode (1,2,6 for the moment)\n");
      printf("-y Number of TX antennas used in eNB\n");
      printf("-z Number of RX antennas used in UE\n");
      printf("-i Relative strength of first intefering eNB (in dB) - cell_id mod 3 = 1\n");
      printf("-j Relative strength of second intefering eNB (in dB) - cell_id mod 3 = 2\n");
      printf("-N Nid_cell\n");
      printf("-R N_RB_DL\n");
      printf("-O oversampling factor (1,2,4,8,16)\n");
      printf("-A Interpolation_filname Run with Abstraction to generate Scatter plot using interpolation polynomial in file\n");
      //    printf("-C Generate Calibration information for Abstraction (effective SNR adjustment to remove Pe bias w.r.t. AWGN)\n");
      printf("-f Output filename (.txt format) for Pe/SNR results\n");
      printf("-F Input filename (.txt format) for RX conformance testing\n");
      exit (-1);
      break;
337
    }
338
  }
339 340 341 342 343 344

  if (transmission_mode>=2)
    n_tx=2;

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

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  PHY_vars_eNB1 = malloc(sizeof(PHY_VARS_eNB));
  PHY_vars_eNB2 = malloc(sizeof(PHY_VARS_eNB));
  PHY_vars_UE->PHY_measurements.n_adj_cells=2;
  PHY_vars_UE->PHY_measurements.adj_cell_id[0] = Nid_cell+1;
  PHY_vars_UE->PHY_measurements.adj_cell_id[1] = Nid_cell+2;

  for (i=0; i<3; i++)
    lte_gold(&PHY_vars_eNB->lte_frame_parms,PHY_vars_UE->lte_gold_table[i],Nid_cell+i);

  memcpy((void*)&PHY_vars_eNB1->lte_frame_parms,(void*)&PHY_vars_eNB->lte_frame_parms,sizeof(LTE_DL_FRAME_PARMS));
  PHY_vars_eNB1->lte_frame_parms.Nid_cell=Nid_cell+1;
  PHY_vars_eNB1->lte_frame_parms.nushift=(Nid_cell+1)%6;
  PHY_vars_eNB1->Mod_id=1;

  memcpy((void*)&PHY_vars_eNB2->lte_frame_parms,(void*)&PHY_vars_eNB->lte_frame_parms,sizeof(LTE_DL_FRAME_PARMS));
  PHY_vars_eNB2->lte_frame_parms.Nid_cell=Nid_cell+2;
  PHY_vars_eNB2->lte_frame_parms.nushift=(Nid_cell+2)%6;
  PHY_vars_eNB2->Mod_id=2;

  phy_init_lte_eNB(PHY_vars_eNB1,0,0,0);
  phy_init_lte_eNB(PHY_vars_eNB2,0,0,0);

367 368 369 370
#ifdef XFORMS
  fl_initialize (&argc, argv, NULL, 0, 0);
  form_ue = create_lte_phy_scope_ue();
  sprintf (title, "LTE PHY SCOPE UE");
371
  fl_show_form (form_ue->lte_phy_scope_ue, FL_PLACE_HOTSPOT, FL_FULLBORDER, title);
372 373 374 375 376 377 378 379 380 381 382
#endif

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

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

383
  frame_parms = &PHY_vars_eNB->lte_frame_parms;
384 385 386



387 388 389
  txdata = PHY_vars_eNB->lte_eNB_common_vars.txdata[0];
  txdata1 = PHY_vars_eNB1->lte_eNB_common_vars.txdata[0];
  txdata2 = PHY_vars_eNB2->lte_eNB_common_vars.txdata[0];
390

391

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  s_re = malloc(2*sizeof(double*));
  s_im = malloc(2*sizeof(double*));
  s_re1 = malloc(2*sizeof(double*));
  s_im1 = malloc(2*sizeof(double*));
  s_re2 = malloc(2*sizeof(double*));
  s_im2 = malloc(2*sizeof(double*));
  r_re = malloc(2*sizeof(double*));
  r_im = malloc(2*sizeof(double*));
  r_re1 = malloc(2*sizeof(double*));
  r_im1 = malloc(2*sizeof(double*));
  r_re2 = malloc(2*sizeof(double*));
  r_im2 = malloc(2*sizeof(double*));

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

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

410 411
  printf("PHY_vars_eNB1->lte_eNB_common_vars.txdataF[0][0] = %p\n",
         PHY_vars_eNB1->lte_eNB_common_vars.txdataF[0][0]);
412 413 414 415 416 417 418 419


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

426
  eNB2UE = new_channel_desc_scm(PHY_vars_eNB->lte_frame_parms.nb_antennas_tx,
427 428
                                PHY_vars_UE->lte_frame_parms.nb_antennas_rx,
                                channel_model,
429 430
 				N_RB2sampling_rate(PHY_vars_eNB->lte_frame_parms.N_RB_DL),
				N_RB2channel_bandwidth(PHY_vars_eNB->lte_frame_parms.N_RB_DL),
431 432 433
                                0,
                                0,
                                0);
434 435

  if (interf1>-20)
436
    eNB2UE1 = new_channel_desc_scm(PHY_vars_eNB->lte_frame_parms.nb_antennas_tx,
437 438
                                   PHY_vars_UE->lte_frame_parms.nb_antennas_rx,
                                   channel_model,
439 440
				   N_RB2sampling_rate(PHY_vars_eNB->lte_frame_parms.N_RB_DL),
				   N_RB2channel_bandwidth(PHY_vars_eNB->lte_frame_parms.N_RB_DL),
441 442 443 444
                                   0,
                                   4,
                                   0);

445
  if (interf2>-20)
446
    eNB2UE2 = new_channel_desc_scm(PHY_vars_eNB->lte_frame_parms.nb_antennas_tx,
447 448
                                   PHY_vars_UE->lte_frame_parms.nb_antennas_rx,
                                   channel_model,
449 450
				   N_RB2sampling_rate(PHY_vars_eNB->lte_frame_parms.N_RB_DL),
				   N_RB2channel_bandwidth(PHY_vars_eNB->lte_frame_parms.N_RB_DL),
451 452 453 454
                                   0,
                                   8,
                                   0);

455 456 457 458 459 460

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

461
  for (i=0; i<2; i++) {
462 463 464 465 466 467 468 469

    s_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_re1[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_re1[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im1[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
470
    bzero(s_im1[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    s_re2[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_re2[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    s_im2[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(s_im2[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));

    r_re[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_re[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_im[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_re1[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_re1[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im1[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_im1[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_re2[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_re2[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    r_im2[i] = malloc(FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
    bzero(r_im2[i],FRAME_LENGTH_COMPLEX_SAMPLES*sizeof(double));
  }
489

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
  /*
  switch (frame_parms->nb_antennas_tx) {
  case 1:
    amask = 0x0000;
    break;
  case 2:
    amask = 0xffff;
    break;
  case 4:
    amask = 0x5555;
  }
  */
  if (pbch_file_fd!=NULL) {
    load_pbch_desc(pbch_file_fd);
  }

  if (input_fd==NULL) {

    //    for (i=0;i<6;i++)
    //      pbch_pdu[i] = i;
    pbch_pdu[0]=100;
    pbch_pdu[1]=1;
    pbch_pdu[2]=0;
513

514 515
    if (PHY_vars_eNB->lte_frame_parms.frame_type == 0) {
      generate_pss(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
516
                   AMP,
517 518
                   &PHY_vars_eNB->lte_frame_parms,
                   (PHY_vars_eNB->lte_frame_parms.Ncp==0) ? 6 : 5,
519
                   0);
520
      generate_sss(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
521
                   AMP,
522 523
                   &PHY_vars_eNB->lte_frame_parms,
                   (PHY_vars_eNB->lte_frame_parms.Ncp==0) ? 5 : 4,
524
                   0);
525
      generate_pss(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
526
                   AMP,
527 528
                   &PHY_vars_eNB->lte_frame_parms,
                   (PHY_vars_eNB->lte_frame_parms.Ncp==0) ? 6 : 5,
529
                   10);
530
      generate_sss(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
531
                   AMP,
532 533
                   &PHY_vars_eNB->lte_frame_parms,
                   (PHY_vars_eNB->lte_frame_parms.Ncp==0) ? 5 : 4,
534
                   10);
535

536
    } else {
537
      generate_sss(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
538
                   AMP,
539 540
                   &PHY_vars_eNB->lte_frame_parms,
                   (PHY_vars_eNB->lte_frame_parms.Ncp==0) ? 6 : 5,
541
                   1);
542
      generate_pss(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
543
                   AMP,
544
                   &PHY_vars_eNB->lte_frame_parms,
545 546
                   2,
                   2);
547
      generate_sss(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
548
                   AMP,
549 550
                   &PHY_vars_eNB->lte_frame_parms,
                   (PHY_vars_eNB->lte_frame_parms.Ncp==0) ? 6 : 5,
551
                   11);
552
      generate_pss(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
553
                   AMP,
554
                   &PHY_vars_eNB->lte_frame_parms,
555 556
                   2,
                   12);
557 558 559


    }
560 561

    /*
562 563 564 565 566 567 568 569
    memcpy(&dci_alloc[0].dci_pdu[0],&DLSCH_alloc_pdu2,sizeof(DCI2_5MHz_2A_M10PRB_TDD_t));
    dci_alloc[0].dci_length = sizeof_DCI2_5MHz_2A_M10PRB_TDD_t;
    dci_alloc[0].L          = 2;
    dci_alloc[0].rnti       = 0x1234;
    dci_alloc[0].nCCE       = 0;
    memcpy(&dci_alloc[1].dci_pdu[1],&DLSCH_alloc_pdu2,sizeof(DCI2_5MHz_2A_M10PRB_TDD_t));
    dci_alloc[1].dci_length = sizeof_DCI2_5MHz_2A_M10PRB_TDD_t;
    dci_alloc[1].L          = 3;
570
    dci_alloc[1].rnti       = 0x1234;
571 572
    */

573
    printf("Generating PBCH for mode1_flag = %d\n", PHY_vars_eNB->lte_frame_parms.mode1_flag);
574 575


576 577
    generate_pilots(PHY_vars_eNB,
                    PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
578 579
                    AMP,
                    LTE_NUMBER_OF_SUBFRAMES_PER_FRAME);
580 581


582
    /*
583
    num_pdcch_symbols = generate_dci_top(1,
584 585 586 587
           0,
           dci_alloc,
           0,
           1024,
588 589
           &PHY_vars_eNB->lte_frame_parms,
           PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
590
           0);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
591
    */
592

593

594 595 596 597
    if (num_pdcch_symbols<3) {
      printf("Less than 3 pdcch symbols\n");
      //  exit(-1);
    }
598

599 600 601 602 603
    if (pbch_phase>0) {
      dummybuf[0] = dummy0;
      dummybuf[1] = dummy1;
      dummybuf[2] = dummy2;
      dummybuf[3] = dummy3;
604
      generate_pbch(&PHY_vars_eNB->lte_eNB_pbch,
605
                    (int32_t**)dummybuf,
606
                    AMP,
607
                    &PHY_vars_eNB->lte_frame_parms,
608 609
                    pbch_pdu,
                    0);
610 611
    }

612 613
    generate_pbch(&PHY_vars_eNB->lte_eNB_pbch,
                  PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
614
                  AMP,
615
                  &PHY_vars_eNB->lte_frame_parms,
616 617
                  pbch_pdu,
                  pbch_phase);
618
    /*
619 620
    generate_pbch(&PHY_vars_eNB->lte_eNB_pbch,
      PHY_vars_eNB->lte_eNB_common_vars.txdataF[0],
621
      AMP,
622
      &PHY_vars_eNB->lte_frame_parms,
623 624
      pbch_pdu,
      1);
625 626 627 628
    */

    if (interf1>-20) {
      /*
629
      generate_pss(PHY_vars_eNB1->lte_eNB_common_vars.txdataF[0],
630
       AMP,
631 632
       &PHY_vars_eNB1->lte_frame_parms,
       (PHY_vars_eNB1->lte_frame_parms.Ncp==0)?6:5,
633
       0);
634
      */
635 636
      generate_pilots(PHY_vars_eNB1,
                      PHY_vars_eNB1->lte_eNB_common_vars.txdataF[0],
637 638 639 640
                      AMP,
                      LTE_NUMBER_OF_SUBFRAMES_PER_FRAME);


641 642
      generate_pbch(&PHY_vars_eNB1->lte_eNB_pbch,
                    PHY_vars_eNB1->lte_eNB_common_vars.txdataF[0],
643
                    AMP,
644
                    &PHY_vars_eNB1->lte_frame_parms,
645 646 647
                    pbch_pdu,
                    0);

648
    }
649

650 651
    if (interf2>-20) {
      /*
652
      generate_pss(PHY_vars_eNB2->lte_eNB_common_vars.txdataF[0],
653
       AMP,
654 655
       &PHY_vars_eNB2->lte_frame_parms,
       (PHY_vars_eNB2->lte_frame_parms.Ncp==0)?6:5,
656 657
       0);

658
      */
659 660 661



662 663
      generate_pilots(PHY_vars_eNB2,
                      PHY_vars_eNB2->lte_eNB_common_vars.txdataF[0],
664 665 666 667
                      AMP,
                      LTE_NUMBER_OF_SUBFRAMES_PER_FRAME);


668 669
      generate_pbch(&PHY_vars_eNB2->lte_eNB_pbch,
                    PHY_vars_eNB2->lte_eNB_common_vars.txdataF[0],
670
                    AMP,
671
                    &PHY_vars_eNB2->lte_frame_parms,
672 673 674
                    pbch_pdu,
                    0);

675
    }
676 677


678 679
    //  write_output("pilotsF.m","rsF",txdataF[0],lte_frame_parms->ofdm_symbol_size,1,1);

680
    write_output("txsigF0.m","txsF0", PHY_vars_eNB->lte_eNB_common_vars.txdataF[0][0],FRAME_LENGTH_COMPLEX_SAMPLES_NO_PREFIX,1,1);
681

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

685 686 687 688
    tx_lev = 0;
    tx_lev1 = 0;
    tx_lev2 = 0;

689 690 691



692
    for (aa=0; aa<PHY_vars_eNB->lte_frame_parms.nb_antennas_tx; aa++) {
693
      if (frame_parms->Ncp == 1)
694
        PHY_ofdm_mod(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0][aa],        // input,
695
                     txdata[aa],         // output
696
                     frame_parms->ofdm_symbol_size,
697 698 699
                     LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*nsymb,                 // number of symbols
                     frame_parms->nb_prefix_samples,               // number of prefix samples
                     CYCLIC_PREFIX);
700
      else {
701
        normal_prefix_mod(PHY_vars_eNB->lte_eNB_common_vars.txdataF[0][aa],
702 703 704
                          txdata[aa],
                          LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*nsymb,
                          frame_parms);
705
      }
706

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

    if (interf1>-20) {
712
      for (aa=0; aa<PHY_vars_eNB1->lte_frame_parms.nb_antennas_tx; aa++) {
713
        if (frame_parms->Ncp == 1)
714 715 716
          PHY_ofdm_mod(PHY_vars_eNB1->lte_eNB_common_vars.txdataF[0][aa],        // input,
                       PHY_vars_eNB1->lte_eNB_common_vars.txdata[0][aa],         // output
                       frame_parms->ofdm_symbol_size,
717 718 719 720
                       2*nsymb,                 // number of symbols
                       frame_parms->nb_prefix_samples,               // number of prefix samples
                       CYCLIC_PREFIX);
        else {
721 722
          normal_prefix_mod(PHY_vars_eNB1->lte_eNB_common_vars.txdataF[0][aa],
                            PHY_vars_eNB1->lte_eNB_common_vars.txdata[0][aa],
723 724 725 726 727 728
                            2*nsymb,
                            frame_parms);
        }

        tx_lev1 += signal_energy(&txdata1[aa][frame_parms->samples_per_tti/2],         // output
                                 OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES);
729
      }
730

731
      write_output("txsigF0_1.m","txsF0_1", PHY_vars_eNB1->lte_eNB_common_vars.txdataF[0][0],FRAME_LENGTH_COMPLEX_SAMPLES_NO_PREFIX,1,1);
732

733 734
      if (PHY_vars_eNB1->lte_frame_parms.nb_antennas_tx>1)
        write_output("txsigF1_1.m","txsF1_1", PHY_vars_eNB1->lte_eNB_common_vars.txdataF[0][1],FRAME_LENGTH_COMPLEX_SAMPLES_NO_PREFIX,1,1);
735
    }
736

737
    if (interf2>-20) {
738
      for (aa=0; aa<PHY_vars_eNB2->lte_frame_parms.nb_antennas_tx; aa++) {
739
        if (frame_parms->Ncp == 1)
740 741 742
          PHY_ofdm_mod(PHY_vars_eNB2->lte_eNB_common_vars.txdataF[0][aa],        // input,
                       PHY_vars_eNB2->lte_eNB_common_vars.txdata[0][aa],         // output
                       frame_parms->ofdm_symbol_size,
743 744 745 746
                       2*nsymb,                 // number of symbols
                       frame_parms->nb_prefix_samples,               // number of prefix samples
                       CYCLIC_PREFIX);
        else {
747 748
          normal_prefix_mod(PHY_vars_eNB2->lte_eNB_common_vars.txdataF[0][aa],
                            PHY_vars_eNB2->lte_eNB_common_vars.txdata[0][aa],
749 750 751 752 753 754
                            2*nsymb,
                            frame_parms);
        }

        tx_lev2 += signal_energy(&txdata2[aa][frame_parms->samples_per_tti/2],         // output
                                 OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES);
755
      }
756

757
      write_output("txsigF0_2.m","txsF0_2", PHY_vars_eNB2->lte_eNB_common_vars.txdataF[0][0],FRAME_LENGTH_COMPLEX_SAMPLES_NO_PREFIX,1,1);
758

759 760
      if (PHY_vars_eNB2->lte_frame_parms.nb_antennas_tx>1)
        write_output("txsigF1_2.m","txsF1_2", PHY_vars_eNB2->lte_eNB_common_vars.txdataF[0][1],FRAME_LENGTH_COMPLEX_SAMPLES_NO_PREFIX,1,1);
761 762 763
    }

    //    tx_lev_dB = (unsigned int) dB_fixed(tx_lev);
764

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

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

770
    write_output("txsig0_1.m","txs0_1", PHY_vars_eNB1->lte_eNB_common_vars.txdata[0][0],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);
771

772
    if (frame_parms->nb_antennas_tx>1)
773
      write_output("txsig1_1.m","txs1_1", PHY_vars_eNB1->lte_eNB_common_vars.txdata[0][1],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);
774

775
    write_output("txsig0_2.m","txs0_2", PHY_vars_eNB2->lte_eNB_common_vars.txdata[0][0],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);
776

777
    if (frame_parms->nb_antennas_tx>1)
778
      write_output("txsig1_2.m","txs1_2", PHY_vars_eNB2->lte_eNB_common_vars.txdata[0][1],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);
779
  } else { //read in from file
780
    i=0;
781

782
    while (!feof(input_fd)) {
Cedric Roux's avatar
Cedric Roux committed
783 784 785 786
      if (fscanf(input_fd,"%s %s",input_val_str,input_val_str2) != 2) { //&input_val1,&input_val2);
        printf("%s:%d:%s: error with fscanf, exiting\n", __FILE__, __LINE__, __FUNCTION__);
        exit(1);
      }
787

788
      if ((i%4)==0) {
789 790 791 792 793
        ((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,);
794
      }
795

796
      i++;
797

798
      if (i>(FRAME_LENGTH_SAMPLES))
799
        break;
800
    }
801

802 803 804 805
    printf("Read in %d samples\n",i/4);
    write_output("txsig0.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],
806
                           OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES);
807 808 809 810
    //    tx_lev_dB = (unsigned int) dB_fixed(tx_lev);
  }


811 812 813
  // multipath channel

  for (i=0; i<2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; i++) {
814
    for (aa=0; aa<PHY_vars_eNB->lte_frame_parms.nb_antennas_tx; aa++) {
815 816
      s_re[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)]);
      s_im[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)+1]);
817

818
      if (interf1>-20) {
819 820
        s_re1[aa][i] = ((double)(((short *)txdata1[aa]))[(i<<1)]);
        s_im1[aa][i] = ((double)(((short *)txdata1[aa]))[(i<<1)+1]);
821
      }
822

823
      if (interf2>-20) {
824 825
        s_re2[aa][i] = ((double)(((short *)txdata2[aa]))[(i<<1)]);
        s_im2[aa][i] = ((double)(((short *)txdata2[aa]))[(i<<1)+1]);
826 827 828 829 830
      }
    }
  }


831
  for (SNR=snr0; SNR<snr1; SNR+=.2) {
832 833 834 835 836


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

838 839
    for (trial=0; trial<n_frames; trial++) {
      pbch_sinr=0.0;
840

841
      if (abstraction_flag==1)
842
        printf("*********************** trial %d ***************************\n",trial);
843 844

      while (pbch_sinr>-2.0) {
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872


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

        if (interf1>-20)
          multipath_channel(eNB2UE1,s_re1,s_im1,r_re1,r_im1,
                            2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES,0);

        if (interf2>-20)
          multipath_channel(eNB2UE2,s_re2,s_im2,r_re2,r_im2,
                            2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES,0);

        if (abstraction_flag == 1) {
          freq_channel(eNB2UE,25,51);

          if (interf1>-20)
            freq_channel(eNB2UE1,25,51);

          if (interf2>-20)
            freq_channel(eNB2UE2,25,51);

          pbch_sinr = compute_pbch_sinr(eNB2UE,eNB2UE1,eNB2UE2,SNR,SNR+interf1,SNR+interf2,25);
          printf("total_sinr %f\n",compute_sinr(eNB2UE,eNB2UE1,eNB2UE2,SNR,SNR+interf1,SNR+interf2,25));
          printf("pbch_sinr %f => BLER %f\n",pbch_sinr,pbch_bler(pbch_sinr));
        } else {
          pbch_sinr = -3.0;
        }
873
      }
874

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

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

883 884
      //AWGN
      sigma2 = pow(10,sigma2_dB/10);
885 886

      /*
887
      if (n_frames==1) {
888 889 890
      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));
891 892
      }
      */
893 894 895 896

      for (n_trials=0; n_trials<ntrials; n_trials++) {
        //printf("n_trial %d\n",n_trials);
        for (i=0; i<2*nsymb*OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; i++) {
897
          for (aa=0; aa<PHY_vars_eNB->lte_frame_parms.nb_antennas_rx; aa++) {
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
            if (n_trials==0) {
              r_re[aa][i] += (pow(10.0,.05*interf1)*r_re1[aa][i] + pow(10.0,.05*interf2)*r_re2[aa][i]);
              r_im[aa][i] += (pow(10.0,.05*interf1)*r_im1[aa][i] + pow(10.0,.05*interf2)*r_im2[aa][i]);
            }

            ((short*) PHY_vars_UE->lte_ue_common_vars.rxdata[aa])[2*i] = (short) (.167*(r_re[aa][i] +sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
            ((short*) PHY_vars_UE->lte_ue_common_vars.rxdata[aa])[2*i+1] = (short) (.167*(r_im[aa][i] + (iqim*r_re[aa][i]) + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
          }
        }

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


        /*
        // optional: read rx_frame from file
        if ((rx_frame_file = fopen("rx_frame.dat","r")) == NULL)
        {
          printf("[openair][CHBCH_TEST][INFO] Cannot open rx_frame.m data file\n");
          exit(0);
        }

        result = fread((void *)PHY_vars_UE->lte_ue_common_vars.rxdata[0],4,FRAME_LENGTH_COMPLEX_SAMPLES,rx_frame_file);
        printf("Read %d bytes\n",result);
        if (PHY_vars_UE->lte_frame_parms.nb_antennas_rx>1) {
          result = fread((void *)PHY_vars_UE->lte_ue_common_vars.rxdata[1],4,FRAME_LENGTH_COMPLEX_SAMPLES,rx_frame_file);
          printf("Read %d bytes\n",result);
        }

        fclose(rx_frame_file);
        */


933
        for (l=0; l<PHY_vars_eNB->lte_frame_parms.symbols_per_tti; l++) {
934

935
          //    subframe_offset = (l/PHY_vars_eNB->lte_frame_parms.symbols_per_tti)*PHY_vars_eNB->lte_frame_parms.samples_per_tti;
936 937 938
          //      printf("subframe_offset = %d\n",subframe_offset);

          slot_fep(PHY_vars_UE,
939 940
                   l%(PHY_vars_eNB->lte_frame_parms.symbols_per_tti/2),
                   l/(PHY_vars_eNB->lte_frame_parms.symbols_per_tti/2),
941
                   0,
942 943
                   0,
		   0);
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975


          if (l==0) {
            lte_ue_measurements(PHY_vars_UE,
                                0,
                                1,
                                0);
            /*
             if (trial%100 == 0) {
               msg("[PHY_PROCEDURES_LTE] frame %d, RX RSSI %d dBm, digital (%d, %d) dB, linear (%d, %d), RX gain %d dB\n",
             trial,
             PHY_vars_UE->PHY_measurements.rx_rssi_dBm[0],
             PHY_vars_UE->PHY_measurements.rx_power_dB[0][0],
             PHY_vars_UE->PHY_measurements.rx_power_dB[0][1],
             PHY_vars_UE->PHY_measurements.rx_power[0][0],
             PHY_vars_UE->PHY_measurements.rx_power[0][1],
             PHY_vars_UE->rx_total_gain_dB);

               msg("[PHY_PROCEDURES_LTE] frame %d, N0 digital (%d, %d) dB, linear (%d, %d)\n",
             trial,
             PHY_vars_UE->PHY_measurements.n0_power_dB[0],
             PHY_vars_UE->PHY_measurements.n0_power_dB[1],
             PHY_vars_UE->PHY_measurements.n0_power[0],
             PHY_vars_UE->PHY_measurements.n0_power[1]);

               msg("[PHY_PROCEDURES_LTE] frame %d, freq_offset_filt = %d\n",
             trial, freq_offset);

             }
            */
          }

976
          if (l==((PHY_vars_eNB->lte_frame_parms.Ncp==0)?4:3)) {
977 978 979 980 981 982 983
            //sprintf(fname,"dl_ch00_%d.m",l);
            //sprintf(vname,"dl_ch00_%d",l);
            //write_output(fname,vname,&(lte_ue_common_vars->dl_ch_estimates[0][lte_frame_parms->ofdm_symbol_size*(l%6)]),lte_frame_parms->ofdm_symbol_size,1,1);

            lte_est_freq_offset(PHY_vars_UE->lte_ue_common_vars.dl_ch_estimates[0],
                                &PHY_vars_UE->lte_frame_parms,
                                l,
984 985
                                &freq_offset,
				1);
986 987
          }

988
          if (l==((PHY_vars_eNB->lte_frame_parms.Ncp==0)?10:9)) {
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

            for (frame_mod4=0; frame_mod4<4; frame_mod4++) {
              pbch_tx_ant = rx_pbch(&PHY_vars_UE->lte_ue_common_vars,
                                    PHY_vars_UE->lte_ue_pbch_vars[0],
                                    &PHY_vars_UE->lte_frame_parms,
                                    0,
                                    SISO,
                                    PHY_vars_UE->high_speed_flag,
                                    frame_mod4);

              if ((pbch_tx_ant>0) && (pbch_tx_ant<4)) {
                PHY_vars_UE->lte_frame_parms.mode1_flag = 1;
                break;

                if (pbch_phase != frame_mod4)
                  printf("pbch_phase different!!!\n");
              }

              pbch_tx_ant = rx_pbch(&PHY_vars_UE->lte_ue_common_vars,
                                    PHY_vars_UE->lte_ue_pbch_vars[0],
1009
                                    &PHY_vars_eNB->lte_frame_parms,
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
                                    0,
                                    ALAMOUTI,
                                    PHY_vars_UE->high_speed_flag,
                                    frame_mod4);

              if ((pbch_tx_ant>0) && (pbch_tx_ant<4)) {
                PHY_vars_UE->lte_frame_parms.mode1_flag = 0;
                n_alamouti++;
                break;
              }
            }


            if ((pbch_tx_ant>0) && (pbch_tx_ant<4)) {
              if (n_frames==1)
                msg("pbch decoded sucessfully mode1_flag %d, frame_mod4 %d, tx_ant %d!\n",
                    PHY_vars_UE->lte_frame_parms.mode1_flag,frame_mod4,pbch_tx_ant);
            } else {
              n_errors++;
              n_errors2++;

              if (n_frames==1)
                msg("pbch error (%d)\n",pbch_tx_ant);
            }
          }
        }

1037
#ifdef XFORMS
1038 1039 1040
        phy_scope_UE(form_ue,
                     PHY_vars_UE,
                     0,0,1);
1041
#endif
1042

1043
      } //noise trials
1044

1045
      if (abstraction_flag==1) {
1046 1047 1048 1049
        printf("SNR %f : n_errors = %d/%d, n_alamouti %d\n", SNR,n_errors,ntrials,n_alamouti);

        if (write_output_file==1)
          fprintf(output_fd,"%f %f %e %e\n",SNR,pbch_sinr,(double)n_errors/ntrials,pbch_bler(pbch_sinr));
1050
      }
1051

1052
      n_errors=0;
1053

1054
      if ((abstraction_flag==0) && (n_errors2>1000) && (trial>5000))
1055
        break;
1056
    } // trials
1057

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

1062
      if (write_output_file==1)
1063
        fprintf(output_fd,"%f %e\n",SNR,(double)n_errors2/(ntrials*(1+trial)));
1064 1065 1066 1067 1068
    }
  } // NSR

  if (n_frames==1) {

1069
    write_output("H00.m","h00",&(PHY_vars_UE->lte_ue_common_vars.dl_ch_estimates[0][0][0]),((frame_parms->Ncp==0)?7:6)*(PHY_vars_eNB->lte_frame_parms.ofdm_symbol_size),1,1);
1070

1071
    if (n_tx==2)
1072
      write_output("H10.m","h10",&(PHY_vars_UE->lte_ue_common_vars.dl_ch_estimates[0][2][0]),((frame_parms->Ncp==0)?7:6)*(PHY_vars_eNB->lte_frame_parms.ofdm_symbol_size),1,1);
1073

1074
    write_output("rxsig0.m","rxs0", PHY_vars_UE->lte_ue_common_vars.rxdata[0],FRAME_LENGTH_COMPLEX_SAMPLES,1,1);
1075
    write_output("rxsigF0.m","rxsF0", PHY_vars_UE->lte_ue_common_vars.rxdataF[0],NUMBER_OF_OFDM_CARRIERS*2*((frame_parms->Ncp==0)?14:12),2,1);
1076 1077 1078 1079 1080 1081
    write_output("PBCH_rxF0_ext.m","pbch0_ext",PHY_vars_UE->lte_ue_pbch_vars[0]->rxdataF_ext[0],12*4*6,1,1);
    write_output("PBCH_rxF0_comp.m","pbch0_comp",PHY_vars_UE->lte_ue_pbch_vars[0]->rxdataF_comp[0],12*4*6,1,1);
    write_output("PBCH_rxF_llr.m","pbch_llr",PHY_vars_UE->lte_ue_pbch_vars[0]->llr,(frame_parms->Ncp==0) ? 1920 : 1728,1,4);
  }


1082
  for (i=0; i<2; i++) {
1083 1084 1085 1086 1087
    free(s_re[i]);
    free(s_im[i]);
    free(r_re[i]);
    free(r_im[i]);
  }
1088

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
  free(s_re);
  free(s_im);
  free(r_re);
  free(r_im);


  lte_sync_time_free();

  if (write_output_file)
    fclose(output_fd);

  return(n_errors);

}


1105 1106

/*
1107 1108
  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],
1109 1110
     (void *)&PHY_vars->tx_vars[0].TX_DMA_BUFFER[0],
     12*OFDM_SYMBOL_SIZE_SAMPLES_NO_PREFIX*2);
1111 1112
*/