prach.c 24 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
/*! \file PHY/LTE_TRANSPORT/prach.c
 * \brief Top-level routines for generating and decoding the PRACH physical channel V8.6 2009-03
 * \author R. Knopp
 * \date 2011
 * \version 0.1
 * \company Eurecom
 * \email: knopp@eurecom.fr
 * \note
 * \warning
 */
32
#include "PHY/sse_intrin.h"
33 34
#include "PHY/defs_eNB.h"
#include "PHY/phy_extern.h"
35
//#include "prach.h"
36
#include "PHY/LTE_TRANSPORT/if4_tools.h"
37
#include "SCHED/sched_eNB.h"
38
#include "common/utils/LOG/vcd_signal_dumper.h"
39
#include "prach_extern.h"
40

41
#if (LTE_RRC_VERSION < MAKE_VERSION(14, 0, 0))
42 43 44
#define rx_prach0 rx_prach
#endif

45 46
void rx_prach0(PHY_VARS_eNB *eNB,
	       RU_t *ru,
knopp's avatar
knopp committed
47 48 49
	       uint16_t *max_preamble,
	       uint16_t *max_preamble_energy,
	       uint16_t *max_preamble_delay,
50 51
	       uint16_t Nf, 
	       uint8_t tdd_mapindex
52
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
53 54 55 56
	       ,uint8_t br_flag,
	       uint8_t ce_level
#endif
	       )
57
{
58 59

  int i;
60 61 62 63 64 65 66

  LTE_DL_FRAME_PARMS *fp;
  lte_frame_type_t   frame_type;
  uint16_t           rootSequenceIndex;  
  uint8_t            prach_ConfigIndex;   
  uint8_t            Ncs_config;          
  uint8_t            restricted_set;      
67
  uint8_t            n_ra_prb;
68 69 70
  int                subframe;
  int16_t            *prachF=NULL;
  int16_t            **rxsigF=NULL;
71
  int                nb_rx;
72

73 74 75 76 77 78 79
  int16_t *prach2;
  uint8_t preamble_index;
  uint16_t NCS,NCS2;
  uint16_t preamble_offset=0,preamble_offset_old;
  int16_t preamble_shift=0;
  uint32_t preamble_shift2;
  uint16_t preamble_index0=0,n_shift_ra=0,n_shift_ra_bar;
80
  uint16_t d_start=0;
81 82 83
  uint16_t numshift=0;
  uint16_t *prach_root_sequence_map;
  uint8_t not_found;
84
  int k=0;
85
  uint16_t u;
86
  int16_t *Xu=0;
87 88
  uint16_t offset;
  int16_t Ncp;
89
  uint16_t first_nonzero_root_idx=0;
90 91 92
  uint8_t new_dft=0;
  uint8_t aa;
  int32_t lev;
knopp's avatar
knopp committed
93
  int16_t levdB;
94
  int fft_size,log2_ifft_size;
95
  int16_t prach_ifft_tmp[2048*2] __attribute__((aligned(32)));
knopp's avatar
knopp committed
96 97
  int32_t *prach_ifft=(int32_t*)NULL;
  int32_t **prach_ifftp=(int32_t **)NULL;
98
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
99 100
  int prach_ifft_cnt=0;
#endif
101

Cedric Roux's avatar
Cedric Roux committed
102

103 104 105 106 107 108 109 110 111 112 113 114
  if (ru) { 
    fp    = &ru->frame_parms;
    nb_rx = ru->nb_rx;
  }
  else if (eNB) {
    fp    = &eNB->frame_parms;
    nb_rx = fp->nb_antennas_rx;
  }
  else AssertFatal(1==0,"rx_prach called without valid RU or eNB descriptor\n");
  
  frame_type          = fp->frame_type;

115
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
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
  if (br_flag == 1) {
    AssertFatal(fp->prach_emtc_config_common.prach_Config_enabled==1,
		"emtc prach_Config is not enabled\n");
    AssertFatal(fp->prach_emtc_config_common.prach_ConfigInfo.prach_CElevel_enable[ce_level]==1,
		"ce_level %d is not active\n",ce_level);
    rootSequenceIndex   = fp->prach_emtc_config_common.rootSequenceIndex;
    prach_ConfigIndex   = fp->prach_emtc_config_common.prach_ConfigInfo.prach_ConfigIndex[ce_level];
    Ncs_config          = fp->prach_emtc_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig;
    restricted_set      = fp->prach_emtc_config_common.prach_ConfigInfo.highSpeedFlag;
    n_ra_prb            = get_prach_prb_offset(fp,prach_ConfigIndex,
					       fp->prach_emtc_config_common.prach_ConfigInfo.prach_FreqOffset[ce_level],
					       tdd_mapindex,Nf);
    // update pointers to results for ce_level
    max_preamble        += ce_level;
    max_preamble_energy += ce_level;
    max_preamble_delay  += ce_level;
  }
  else 
#endif
    {
      rootSequenceIndex   = fp->prach_config_common.rootSequenceIndex;
      prach_ConfigIndex   = fp->prach_config_common.prach_ConfigInfo.prach_ConfigIndex;
      Ncs_config          = fp->prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig;
      restricted_set      = fp->prach_config_common.prach_ConfigInfo.highSpeedFlag;
      n_ra_prb            = get_prach_prb_offset(fp,prach_ConfigIndex,
						 fp->prach_config_common.prach_ConfigInfo.prach_FreqOffset,
						 tdd_mapindex,Nf);
    }

  int16_t *prach[nb_rx];
knopp's avatar
knopp committed
146 147
  uint8_t prach_fmt = get_prach_fmt(prach_ConfigIndex,frame_type);
  uint16_t N_ZC = (prach_fmt <4)?839:139;
148 149
  
  if (eNB) {
150
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
151 152 153 154
    if (br_flag == 1) {
      prach_ifftp         = eNB->prach_vars_br.prach_ifft[ce_level];
      subframe            = eNB->proc.subframe_prach_br;
      prachF              = eNB->prach_vars_br.prachF;
155
      rxsigF              = eNB->prach_vars_br.rxsigF[ce_level];
156 157 158 159 160 161 162 163 164
      if (LOG_DEBUGFLAG(PRACH)){
        if (((ru->proc.frame_prach)&1023) < 20) LOG_I(PHY,"PRACH (eNB) : running rx_prach (br_flag %d, ce_level %d) for frame %d subframe %d, prach_FreqOffset %d, prach_ConfigIndex %d, rootSequenceIndex %d, repetition number %d,numRepetitionsPrePreambleAttempt %d\n",
			  	     br_flag,ce_level,ru->proc.frame_prach,subframe,
				     fp->prach_emtc_config_common.prach_ConfigInfo.prach_FreqOffset[ce_level],
				     prach_ConfigIndex,rootSequenceIndex,
				     eNB->prach_vars_br.repetition_number[ce_level],
				     fp->prach_emtc_config_common.prach_ConfigInfo.prach_numRepetitionPerPreambleAttempt[ce_level]);
      }
    } else
165 166 167 168 169
#endif
      {
        prach_ifftp       = eNB->prach_vars.prach_ifft[0];
        subframe          = eNB->proc.subframe_prach;
        prachF            = eNB->prach_vars.prachF;
170
        rxsigF            = eNB->prach_vars.rxsigF[0];
171 172 173
        if (LOG_DEBUGFLAG(PRACH)){
          if (((ru->proc.frame_prach)&1023) < 20) LOG_I(PHY,"PRACH (eNB) : running rx_prach for subframe %d, prach_FreqOffset %d, prach_ConfigIndex %d , rootSequenceIndex %d\n", subframe,fp->prach_config_common.prach_ConfigInfo.prach_FreqOffset,prach_ConfigIndex,rootSequenceIndex);
        }
174 175 176
      }
  }
  else {
177
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
178 179 180
    if (br_flag == 1) {
        subframe          = ru->proc.subframe_prach_br;
        rxsigF            = ru->prach_rxsigF_br[ce_level];
181 182 183 184 185
        if (LOG_DEBUGFLAG(PRACH)){
          if (((ru->proc.frame_prach)&1023) < 20) LOG_I(PHY,"PRACH (RU) : running rx_prach (br_flag %d, ce_level %d) for frame %d subframe %d, prach_FreqOffset %d, prach_ConfigIndex %d\n",
				       br_flag,ce_level,ru->proc.frame_prach,subframe,fp->prach_emtc_config_common.prach_ConfigInfo.prach_FreqOffset[ce_level],prach_ConfigIndex);
        }
    } else
186 187 188 189
#endif
      {
        subframe          = ru->proc.subframe_prach;
        rxsigF            = ru->prach_rxsigF;
190 191 192 193
        if (LOG_DEBUGFLAG(PRACH)){
          if (((ru->proc.frame_prach)&1023) < 20) LOG_I(PHY,"PRACH (RU) : running rx_prach for subframe %d, prach_FreqOffset %d, prach_ConfigIndex %d\n",
	        subframe,fp->prach_config_common.prach_ConfigInfo.prach_FreqOffset,prach_ConfigIndex);
        }
194 195 196 197
      }

  }

198 199
  AssertFatal(ru!=NULL,"ru is null\n");

200
  for (aa=0; aa<nb_rx; aa++) {
201
    if (ru->if_south == LOCAL_RF) { // set the time-domain signal if we have to use it in this node
202
      // DJP - indexing below in subframe zero takes us off the beginning of the array???
203
      prach[aa] = (int16_t*)&ru->common.rxdata[aa][(subframe*fp->samples_per_tti)-ru->N_TA_offset];
204

205
        if (LOG_DUMPFLAG(PRACH)){
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
          int32_t en0=signal_energy((int32_t*)prach[aa],fp->samples_per_tti);
          int8_t dbEn0 = dB_fixed(en0);
          int8_t rach_dBm = dbEn0 - ru->rx_total_gain_dB;
          char buffer[80];
          if (dbEn0>32 && prach[0]!= NULL) {
            static int counter=0;
            sprintf(buffer, "%s%d", "/tmp/prach_rx",counter);
            LOG_M(buffer,"prach_rx",prach[0],fp->samples_per_tti,1,13);
          }
        if (dB_fixed(en0)>32) {
          sprintf(buffer, "rach_dBm:%d",rach_dBm);
          if (prach[0]!= NULL) LOG_M("prach_rx","prach_rx",prach[0],fp->samples_per_tti,1,1);
            LOG_I(PHY,"RU %d, br_flag %d ce_level %d frame %d subframe %d per_tti:%d prach:%p (energy %d) TA:%d %s rxdata:%p index:%d\n",
                  ru->idx,br_flag,ce_level,ru->proc.frame_prach,subframe,fp->samples_per_tti,
                  prach[aa],dbEn0,ru->N_TA_offset,buffer,ru->common.rxdata[aa], 
                  (subframe*fp->samples_per_tti)-ru->N_TA_offset);
222
        }
223
      }
224
    }
225
  }
226

227 228
  // First compute physical root sequence
  if (restricted_set == 0) {
229 230
    AssertFatal(Ncs_config<=15,
		"Illegal Ncs_config for unrestricted format %d\n",Ncs_config);
231 232
    NCS = NCS_unrestricted[Ncs_config];
  } else {
233 234
    AssertFatal(Ncs_config<=14,
		"FATAL, Illegal Ncs_config for restricted format %d\n",Ncs_config);
235 236 237
    NCS = NCS_restricted[Ncs_config];
  }

238
  if (eNB) start_meas(&eNB->rx_prach);
239

240

241
  prach_root_sequence_map = (prach_fmt < 4) ? prach_root_sequence_map0_3 : prach_root_sequence_map4;
242

243 244 245
  // PDP is oversampled, e.g. 1024 sample instead of 839
  // Adapt the NCS (zero-correlation zones) with oversampling factor e.g. 1024/839
  NCS2 = (N_ZC==839) ? ((NCS<<10)/839) : ((NCS<<8)/139);
246

247
  if (NCS2==0)
248
    NCS2 = N_ZC;
249 250 251 252

  switch (prach_fmt) {
  case 0:
    Ncp = 3168;
253 254
    break;

255 256 257 258
  case 1:
  case 3:
    Ncp = 21024;
    break;
259

260 261 262
  case 2:
    Ncp = 6240;
    break;
263

264 265 266
  case 4:
    Ncp = 448;
    break;
267

268 269 270 271
  default:
    Ncp = 3168;
    break;
  }
272

273
  // Adjust CP length based on UL bandwidth
274
  switch (fp->N_RB_UL) {
275 276 277
  case 6:
    Ncp>>=4;
    break;
278

279 280 281
  case 15:
    Ncp>>=3;
    break;
282

283 284 285
  case 25:
    Ncp>>=2;
    break;
286

287 288 289
  case 50:
    Ncp>>=1;
    break;
290

291 292 293
  case 75:
    Ncp=(Ncp*3)>>2;
    break;
294 295

  case 100:
296
    if (fp->threequarter_fs == 1)
297 298
      Ncp=(Ncp*3)>>2;
    break;
299 300
  }

301

302 303
  if (((eNB!=NULL) && (ru->function != NGFI_RAU_IF4p5))||
      ((eNB==NULL) && (ru->function == NGFI_RRU_IF4p5))) { // compute the DFTs of the PRACH temporal resources
304
    // Do forward transform
305 306 307
    if (LOG_DEBUGFLAG(PRACH)) {
      LOG_D(PHY,"rx_prach: Doing FFT for N_RB_UL %d nb_rx:%d Ncp:%d\n",fp->N_RB_UL, nb_rx, Ncp);
    }
308
    for (aa=0; aa<nb_rx; aa++) {
309
      AssertFatal(prach[aa]!=NULL,"prach[%d] is null\n",aa);
310 311 312
      prach2 = prach[aa] + (Ncp<<1);
  
      // do DFT
313
      switch (fp->N_RB_UL) {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
      case 6:
	if (prach_fmt == 4) {
	  dft256(prach2,rxsigF[aa],1);
	} else {
	  dft1536(prach2,rxsigF[aa],1);
	  
	  if (prach_fmt>1)
	    dft1536(prach2+3072,rxsigF[aa]+3072,1);
	}
	
	break;
	
      case 15:
	if (prach_fmt == 4) {
	  dft256(prach2,rxsigF[aa],1);
	} else {
knopp's avatar
knopp committed
330
	  dft3072(prach2,rxsigF[aa],1);
331 332
	  
	  if (prach_fmt>1)
knopp's avatar
knopp committed
333
	    dft3072(prach2+6144,rxsigF[aa]+6144,1);
334 335 336 337 338 339 340 341 342 343
	}
	
	break;
	
      case 25:
      default:
	if (prach_fmt == 4) {
	  dft1024(prach2,rxsigF[aa],1);
	  fft_size = 1024;
	} else {
knopp's avatar
knopp committed
344
	  dft6144(prach2,rxsigF[aa],1);
345 346
	  
	  if (prach_fmt>1)
knopp's avatar
knopp committed
347
	    dft6144(prach2+12288,rxsigF[aa]+12288,1);
348 349 350 351 352 353 354 355 356 357
	  
	  fft_size = 6144;
	}
	
	break;
	
      case 50:
	if (prach_fmt == 4) {
	  dft2048(prach2,rxsigF[aa],1);
	} else {
knopp's avatar
knopp committed
358
	  dft12288(prach2,rxsigF[aa],1);
359 360
	  
	  if (prach_fmt>1)
knopp's avatar
knopp committed
361
	    dft12288(prach2+24576,rxsigF[aa]+24576,1);
362 363 364 365 366 367
	}
	
	break;
	
      case 75:
	if (prach_fmt == 4) {
knopp's avatar
knopp committed
368
	  dft3072(prach2,rxsigF[aa],1);
369
	} else {
knopp's avatar
knopp committed
370
	  dft18432(prach2,rxsigF[aa],1);
371 372
	  
	  if (prach_fmt>1)
knopp's avatar
knopp committed
373
	    dft18432(prach2+36864,rxsigF[aa]+36864,1);
374 375 376 377 378
	}
	
	break;
	
      case 100:
379
	if (fp->threequarter_fs==0) {
380 381 382
	  if (prach_fmt == 4) {
	    dft4096(prach2,rxsigF[aa],1);
	  } else {
knopp's avatar
knopp committed
383
	    dft24576(prach2,rxsigF[aa],1);
384 385
	    
	    if (prach_fmt>1)
knopp's avatar
knopp committed
386
	      dft24576(prach2+49152,rxsigF[aa]+49152,1);
387
	  }
388
	} else {
389
	  if (prach_fmt == 4) {
knopp's avatar
knopp committed
390
	    dft3072(prach2,rxsigF[aa],1);
391
	  } else {
knopp's avatar
knopp committed
392
	    dft18432(prach2,rxsigF[aa],1);
393 394
	    
	    if (prach_fmt>1)
knopp's avatar
knopp committed
395
	      dft18432(prach2+36864,rxsigF[aa]+36864,1);
396
	  }
397 398 399 400
	}
	
	break;
      }
401 402 403 404 405 406 407 408 409 410 411

      k = (12*n_ra_prb) - 6*fp->N_RB_UL;
      
      if (k<0) {
	k+=(fp->ofdm_symbol_size);
      }
      
      k*=12;
      k+=13; 
      k*=2;
      int dftsize_x2 = fp->ofdm_symbol_size*24;
412
      //LOG_D(PHY,"Shifting prach_rxF from %d to 0\n",k);
413 414 415 416 417 418 419 420

      if ((k+(839*2)) > dftsize_x2) { // PRACH signal is split around DC 
	memmove((void*)&rxsigF[aa][dftsize_x2-k],(void*)&rxsigF[aa][0],(k+(839*2)-dftsize_x2)*2);	
	memmove((void*)&rxsigF[aa][0],(void*)(&rxsigF[aa][k]),(dftsize_x2-k)*2);	
      }
      else  // PRACH signal is not split around DC
	memmove((void*)&rxsigF[aa][0],(void*)(&rxsigF[aa][k]),839*4);	
      
421
    }
422
	     
423 424
  }

425
  if ((eNB==NULL) && (ru!=NULL) && ru->function == NGFI_RRU_IF4p5) {
426

427
    /// **** send_IF4 of rxsigF to RAU **** ///
428
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
429 430 431 432 433
    if (br_flag == 1) send_IF4p5(ru, ru->proc.frame_prach, ru->proc.subframe_prach, IF4p5_PRACH+1+ce_level);      

    else
#endif
      send_IF4p5(ru, ru->proc.frame_prach, ru->proc.subframe_prach, IF4p5_PRACH);
434
    
435
    return;
436
  } else if (eNB!=NULL) {
437

438 439 440 441
    if ( LOG_DEBUGFLAG(PRACH)) {
      int en = dB_fixed(signal_energy((int32_t*)&rxsigF[0][0],840));
      if ((en > 60)&&(br_flag==1)) LOG_I(PHY,"PRACH (br_flag %d,ce_level %d, n_ra_prb %d, k %d): Frame %d, Subframe %d => %d dB\n",br_flag,ce_level,n_ra_prb,k,eNB->proc.frame_rx,eNB->proc.subframe_rx,en);
    }
442
  }
443
  
444
  // in case of RAU and prach received rx_thread wakes up prach
445

446
  // here onwards is for eNodeB_3GPP or NGFI_RAU_IF4p5
447

448 449
  preamble_offset_old = 99;

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  uint8_t update_TA  = 4;
  uint8_t update_TA2 = 1;
  switch (eNB->frame_parms.N_RB_DL) {
  case 6:
    update_TA = 16;
    break;
    
  case 25:
    update_TA = 4;
    break;
    
  case 50:
    update_TA = 2;
    break;
    
  case 75:
    update_TA  = 3;
    update_TA2 = 2;
  case 100:
    update_TA  = 1;
    break;
  }
  
  *max_preamble_energy=0;
474
  for (preamble_index=0 ; preamble_index<64 ; preamble_index++) {
475

476 477 478 479
    if (LOG_DEBUGFLAG(PRACH)){
      int en = dB_fixed(signal_energy((int32_t*)&rxsigF[0][0],840));
      if (en>60) LOG_I(PHY,"frame %d, subframe %d : Trying preamble %d (br_flag %d)\n",ru->proc.frame_prach,subframe,preamble_index,br_flag);
    }
480
    if (restricted_set == 0) {
481 482
      // This is the relative offset in the root sequence table (5.7.2-4 from 36.211) for the given preamble index
      preamble_offset = ((NCS==0)? preamble_index : (preamble_index/(N_ZC/NCS)));
483
      
484
      if (preamble_offset != preamble_offset_old) {
485 486 487 488
        preamble_offset_old = preamble_offset;
        new_dft = 1;
        // This is the \nu corresponding to the preamble index
        preamble_shift  = 0;
489
      }
490
      
491
      else {
492
        preamble_shift  -= NCS;
493
	
494 495
        if (preamble_shift < 0)
          preamble_shift+=N_ZC;
496
      }
497
    } else { // This is the high-speed case
498
      new_dft = 0;
499

500 501 502 503
      // set preamble_offset to initial rootSequenceIndex and look if we need more root sequences for this
      // preamble index and find the corresponding cyclic shift
      // Check if all shifts for that root have been processed
      if (preamble_index0 == numshift) {
504 505 506 507 508 509 510 511 512 513 514
        not_found = 1;
        new_dft   = 1;
        preamble_index0 -= numshift;
        (preamble_offset==0 && numshift==0) ? (preamble_offset) : (preamble_offset++);

        while (not_found == 1) {
          // current root depending on rootSequenceIndex
          int index = (rootSequenceIndex + preamble_offset) % N_ZC;

          if (prach_fmt<4) {
            // prach_root_sequence_map points to prach_root_sequence_map0_3
515
            DevAssert( index < sizeof(prach_root_sequence_map0_3) / sizeof(prach_root_sequence_map0_3[0]) );
516 517
          } else {
            // prach_root_sequence_map points to prach_root_sequence_map4
518
            DevAssert( index < sizeof(prach_root_sequence_map4) / sizeof(prach_root_sequence_map4[0]) );
519 520 521 522
          }

          u = prach_root_sequence_map[index];

523
          uint16_t n_group_ra = 0;
524

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
          if ( (du[u]<(N_ZC/3)) && (du[u]>=NCS) ) {
            n_shift_ra     = du[u]/NCS;
            d_start        = (du[u]<<1) + (n_shift_ra * NCS);
            n_group_ra     = N_ZC/d_start;
            n_shift_ra_bar = max(0,(N_ZC-(du[u]<<1)-(n_group_ra*d_start))/N_ZC);
          } else if  ( (du[u]>=(N_ZC/3)) && (du[u]<=((N_ZC - NCS)>>1)) ) {
            n_shift_ra     = (N_ZC - (du[u]<<1))/NCS;
            d_start        = N_ZC - (du[u]<<1) + (n_shift_ra * NCS);
            n_group_ra     = du[u]/d_start;
            n_shift_ra_bar = min(n_shift_ra,max(0,(du[u]- (n_group_ra*d_start))/NCS));
          } else {
            n_shift_ra     = 0;
            n_shift_ra_bar = 0;
          }

          // This is the number of cyclic shifts for the current root u
          numshift = (n_shift_ra*n_group_ra) + n_shift_ra_bar;
          // skip to next root and recompute parameters if numshift==0
          (numshift>0) ? (not_found = 0) : (preamble_offset++);
        }
545
      }
546 547 548

      if (n_shift_ra>0)
        preamble_shift = -((d_start * (preamble_index0/n_shift_ra)) + ((preamble_index0%n_shift_ra)*NCS)); // minus because the channel is h(t -\tau + Cv)
549
      else
550
        preamble_shift = 0;
551

552 553
      if (preamble_shift < 0)
        preamble_shift+=N_ZC;
554

555
      preamble_index0++;
556

557
      if (preamble_index == 0)
558
        first_nonzero_root_idx = preamble_offset;
559
    }
560

561
    // Compute DFT of RX signal (conjugate input, results in conjugate output) for each new rootSequenceIndex
562 563 564 565 566
    if (LOG_DEBUGFLAG(PRACH)) {
      int en = dB_fixed(signal_energy((int32_t*)&rxsigF[0][0],840));
      if (en>60) LOG_I(PHY,"frame %d, subframe %d : preamble index %d: offset %d, preamble shift %d (br_flag %d, en %d)\n",
		       ru->proc.frame_prach,subframe,preamble_index,preamble_offset,preamble_shift,br_flag,en);
    }
567 568
    log2_ifft_size = 10;
    fft_size = 6144;
569

570 571
    if (new_dft == 1) {
      new_dft = 0;
572

573
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
574
      if (br_flag == 1) {
575
	Xu=(int16_t*)eNB->X_u_br[ce_level][preamble_offset-first_nonzero_root_idx];
576 577 578 579 580 581
	prach_ifft = prach_ifftp[prach_ifft_cnt++];
	if (eNB->prach_vars_br.repetition_number[ce_level]==1) memset(prach_ifft,0,((N_ZC==839)?2048:256)*sizeof(int32_t));
      }
      else
#endif
	{
582
	  Xu=(int16_t*)eNB->X_u[preamble_offset-first_nonzero_root_idx];
583 584 585
	  prach_ifft = prach_ifftp[0];
          memset(prach_ifft,0,((N_ZC==839) ? 2048 : 256)*sizeof(int32_t));
	}
586

587
      memset(prachF, 0, sizeof(int16_t)*2*1024 );
588
      if (LOG_DUMPFLAG(PRACH)) {      
589 590 591 592 593 594
        if (prach[0]!= NULL) LOG_M("prach_rx0.m","prach_rx0",prach[0],6144+792,1,1);
         LOG_M("prach_rx1.m","prach_rx1",prach[1],6144+792,1,1);
         LOG_M("prach_rxF0.m","prach_rxF0",rxsigF[0],24576,1,1);
         LOG_M("prach_rxF1.m","prach_rxF1",rxsigF[1],6144,1,1);
      }
   
595
      for (aa=0;aa<nb_rx; aa++) {
596
      // Do componentwise product with Xu* on each antenna 
597 598

	k=0;	
599 600 601 602
	for (offset=0; offset<(N_ZC<<1); offset+=2) {
	  prachF[offset]   = (int16_t)(((int32_t)Xu[offset]*rxsigF[aa][k]   + (int32_t)Xu[offset+1]*rxsigF[aa][k+1])>>15);
	  prachF[offset+1] = (int16_t)(((int32_t)Xu[offset]*rxsigF[aa][k+1] - (int32_t)Xu[offset+1]*rxsigF[aa][k])>>15);
	  k+=2;
603
	  if (k==(12*2*fp->ofdm_symbol_size))
604 605 606 607 608 609
	    k=0;
	}
	
	// Now do IFFT of size 1024 (N_ZC=839) or 256 (N_ZC=139)
	if (N_ZC == 839) {
	  log2_ifft_size = 10;
610 611 612
	  idft1024(prachF,prach_ifft_tmp,1);
	  // compute energy and accumulate over receive antennas and repetitions for BR
	  for (i=0;i<2048;i++)
613
	    prach_ifft[i] += (prach_ifft_tmp[i<<1]*prach_ifft_tmp[i<<1] + prach_ifft_tmp[1+(i<<1)]*prach_ifft_tmp[1+(i<<1)])>>10;
614
	} else {
615
	  idft256(prachF,prach_ifft_tmp,1);
616
	  log2_ifft_size = 8;
617 618
	  // compute energy and accumulate over receive antennas and repetitions for BR
	  for (i=0;i<256;i++)
619
	    prach_ifft[i] += (prach_ifft_tmp[i<<1]*prach_ifft_tmp[(i<<1)] + prach_ifft_tmp[1+(i<<1)]*prach_ifft_tmp[1+(i<<1)])>>10;
620
	}
621

622
        if (LOG_DUMPFLAG(PRACH)) {	
623 624 625
	  if (aa==0) LOG_M("prach_rxF_comp0.m","prach_rxF_comp0",prachF,1024,1,1);
          if (aa==1) LOG_M("prach_rxF_comp1.m","prach_rxF_comp1",prachF,1024,1,1);
        }
626 627
      }// antennas_rx
    } // new dft
628
    
629
    // check energy in nth time shift, for 
630
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
631 632 633
    if ((br_flag==0) ||
	(eNB->prach_vars_br.repetition_number[ce_level]==
	 eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_numRepetitionPerPreambleAttempt[ce_level]))
634
#endif
635
      {
636
      if (LOG_DEBUGFLAG(PRACH)){
637 638
        int en = dB_fixed(signal_energy((int32_t*)&rxsigF[0][0],840));
	if (en>60) LOG_I(PHY,"frame %d, subframe %d: Checking for peak in time-domain (br_flag %d, en %d)\n",ru->proc.frame_prach,subframe,br_flag,en);
639
      }
640
	preamble_shift2 = ((preamble_shift==0) ? 0 : ((preamble_shift<<log2_ifft_size)/N_ZC));
641

642 643
    
	for (i=0; i<NCS2; i++) {
644
	  lev = (int32_t)prach_ifft[(preamble_shift2+i)];
645 646 647 648 649 650
	  levdB = dB_fixed_times10(lev);
	  
	  if (levdB>*max_preamble_energy) {
	    *max_preamble_energy  = levdB;
	    *max_preamble_delay   = ((i*fft_size)>>log2_ifft_size)*update_TA/update_TA2;
	    *max_preamble         = preamble_index;
651 652 653 654 655 656 657 658
            if (LOG_DEBUGFLAG(PRACH)){
              int en = dB_fixed(signal_energy((int32_t*)&rxsigF[0][0],840));
	      if ((en>60) && (br_flag==1)) 
                 LOG_D(PHY,"frame %d, subframe %d : max_preamble_energy %d, max_preamble_delay %d, max_preamble %d (br_flag %d,ce_level %d, levdB %d, lev %d)\n",
                       ru->proc.frame_prach,subframe,
                       *max_preamble_energy,*max_preamble_delay,
                       *max_preamble,br_flag,ce_level,levdB,lev);
            }
659 660
	  }
	}
661

662
      }
663
  }// preamble_index
664

665
  if (LOG_DUMPFLAG(PRACH)) {
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    int en = dB_fixed(signal_energy((int32_t*)&rxsigF[0][0],840));  
    if (en>60) {
      k = (12*n_ra_prb) - 6*fp->N_RB_UL;
      
      if (k<0) k+=fp->ofdm_symbol_size;
      
      k*=12;
      k+=13;
      k*=2;
      
      if (br_flag == 0) {
  	  LOG_M("rxsigF.m","prach_rxF",&rxsigF[0][0],12288,1,1);
  	  LOG_M("prach_rxF_comp0.m","prach_rxF_comp0",prachF,1024,1,1);
  	  LOG_M("Xu.m","xu",Xu,N_ZC,1,1);
  	  LOG_M("prach_ifft0.m","prach_t0",prach_ifft,1024,1,1);
      }
      else {
  	LOG_E(PHY,"Dumping prach (br_flag %d), k = %d (n_ra_prb %d)\n",br_flag,k,n_ra_prb);
  	LOG_M("rxsigF_br.m","prach_rxF_br",&rxsigF[0][0],12288,1,1);
  	LOG_M("prach_rxF_comp0_br.m","prach_rxF_comp0_br",prachF,1024,1,1);
  	LOG_M("Xu_br.m","xu_br",Xu,N_ZC,1,1);
  	LOG_M("prach_ifft0_br.m","prach_t0_br",prach_ifft,1024,1,1);
  	exit(-1);      
      }
690

691
    }
692
  } /* LOG_DUMPFLAG(PRACH) */
693
  if (eNB) stop_meas(&eNB->rx_prach);
694

695 696
}

697
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
698

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
void rx_prach(PHY_VARS_eNB *eNB,
	      RU_t *ru,
	      uint16_t *max_preamble,
	      uint16_t *max_preamble_energy,
	      uint16_t *max_preamble_delay,
	      uint16_t Nf, 
	      uint8_t tdd_mapindex,
	      uint8_t br_flag) {

  int i;
  int prach_mask=0;

  if (br_flag == 0) { 
    rx_prach0(eNB,ru,max_preamble,max_preamble_energy,max_preamble_delay,Nf,tdd_mapindex,0,0);
  }
  else { // This is procedure for eMTC, basically handling the repetitions
    prach_mask = is_prach_subframe(&eNB->frame_parms,eNB->proc.frame_prach_br,eNB->proc.subframe_prach_br);
    for (i=0;i<4;i++) {
      if ((eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_CElevel_enable[i]==1) &&
	  ((prach_mask&(1<<(i+1))) > 0)) { // check that prach CE level is active now

	// if first reception in group of repetitions store frame for later (in RA-RNTI for Msg2) 
	if (eNB->prach_vars_br.repetition_number[i]==0) eNB->prach_vars_br.first_frame[i]=eNB->proc.frame_prach_br;

	// increment repetition number
	eNB->prach_vars_br.repetition_number[i]++;

	// do basic PRACH reception
	rx_prach0(eNB,ru,max_preamble,max_preamble_energy,max_preamble_delay,Nf,tdd_mapindex,1,i);
	
	// if last repetition, clear counter
	if (eNB->prach_vars_br.repetition_number[i] == eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_numRepetitionPerPreambleAttempt[i]) {
	  eNB->prach_vars_br.repetition_number[i]=0;

	}
      }
    }
  }
}
738

739
#endif /* #if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0)) */