pbch.c 27.4 KB
Newer Older
1
/*******************************************************************************
ghaddab's avatar
ghaddab committed
2 3
    OpenAirInterface 
    Copyright(c) 1999 - 2014 Eurecom
4

ghaddab's avatar
ghaddab committed
5 6 7 8
    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.
9 10


ghaddab's avatar
ghaddab committed
11 12 13 14
    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.
15

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

  Contact Information
ghaddab's avatar
ghaddab committed
22 23 24 25
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
  
ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27

ghaddab's avatar
ghaddab committed
28
 *******************************************************************************/
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

/*! \file PHY/LTE_TRANSPORT/pbch.c
* \brief Top-level routines for generating and decoding  the PBCH/BCH physical/transport channel V8.6 2009-03
* \author R. Knopp, F. Kaltenberger
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr,florian.kaltenberger.fr
* \note
* \warning
*/
#include "PHY/defs.h"
#include "PHY/CODING/extern.h"
#include "PHY/CODING/lte_interleaver_inline.h"
#include "defs.h"
#include "extern.h"
#include "PHY/extern.h"
46
#include "PHY/sse_intrin.h"
47

48 49 50 51
#ifdef PHY_ABSTRACTION
#include "SIMULATION/TOOLS/defs.h"
#endif 

52 53 54
  
//#define DEBUG_PBCH 1
//#define DEBUG_PBCH_ENCODING
xuhl's avatar
xuhl committed
55
//#define INTERFERENCE_MITIGATION 1
56 57 58 59 60 61 62

#ifdef OPENAIR2
#include "PHY_INTERFACE/defs.h"
#endif

#define PBCH_A 24

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
int allocate_pbch_REs_in_RB(LTE_DL_FRAME_PARMS *frame_parms,
			    mod_sym_t **txdataF,
			    uint32_t *jj,
			    uint16_t re_offset,
			    uint32_t symbol_offset,
			    uint8_t *x0,
			    uint8_t pilots,
			    int16_t amp,
			    uint32_t *re_allocated) {

  MIMO_mode_t mimo_mode   = (frame_parms->mode1_flag==1)?SISO:ALAMOUTI;


  uint32_t tti_offset,aa;
  uint8_t re;
  int16_t gain_lin_QPSK;
  int16_t re_off=re_offset;

  uint8_t first_re,last_re;
  int32_t tmp_sample1,tmp_sample2;

  gain_lin_QPSK = (int16_t)((amp*ONE_OVER_SQRT2_Q15)>>15);

  first_re=0;
  last_re=12;

  for (re=first_re;re<last_re;re++) {
    
    tti_offset = symbol_offset + re_off + re;
    
    // check that RE is not from Cell-specific RS

    if (is_not_pilot(pilots,re,frame_parms->nushift,0)==1) { 
      //      printf("re %d (jj %d)\n",re,*jj);
      if (mimo_mode == SISO) {  //SISO mapping
	*re_allocated = *re_allocated + 1;	
	//	  printf("%d(%d) : %d,%d => ",tti_offset,*jj,((int16_t*)&txdataF[0][tti_offset])[0],((int16_t*)&txdataF[0][tti_offset])[1]);
	for (aa=0; aa<frame_parms->nb_antennas_tx; aa++) {
	  ((int16_t*)&txdataF[aa][tti_offset])[0] += (x0[*jj]==1) ? (-gain_lin_QPSK) : gain_lin_QPSK; //I //b_i
	}
	*jj = *jj + 1;
	for (aa=0; aa<frame_parms->nb_antennas_tx; aa++) {
	  ((int16_t*)&txdataF[aa][tti_offset])[1] += (x0[*jj]==1) ? (-gain_lin_QPSK) : gain_lin_QPSK; //Q //b_{i+1}
	}
	*jj = *jj + 1;
      }
      else if (mimo_mode == ALAMOUTI){
	*re_allocated = *re_allocated + 1;	

	((int16_t*)&tmp_sample1)[0] = (x0[*jj]==1) ? (-gain_lin_QPSK) : gain_lin_QPSK;
	*jj=*jj+1;
	((int16_t*)&tmp_sample1)[1] = (x0[*jj]==1) ? (-gain_lin_QPSK) : gain_lin_QPSK;
	*jj=*jj+1;

	// second antenna position n -> -x1*
	
	((int16_t*)&tmp_sample2)[0] = (x0[*jj]==1) ? (gain_lin_QPSK) : -gain_lin_QPSK;
	*jj=*jj+1;
	((int16_t*)&tmp_sample2)[1] = (x0[*jj]==1) ? (-gain_lin_QPSK) : gain_lin_QPSK;
	*jj=*jj+1;
	
	// normalization for 2 tx antennas
	((int16_t*)&txdataF[0][tti_offset])[0] += (int16_t)((((int16_t*)&tmp_sample1)[0]*ONE_OVER_SQRT2_Q15)>>15);
	((int16_t*)&txdataF[0][tti_offset])[1] += (int16_t)((((int16_t*)&tmp_sample1)[1]*ONE_OVER_SQRT2_Q15)>>15);
	((int16_t*)&txdataF[1][tti_offset])[0] += (int16_t)((((int16_t*)&tmp_sample2)[0]*ONE_OVER_SQRT2_Q15)>>15);
	((int16_t*)&txdataF[1][tti_offset])[1] += (int16_t)((((int16_t*)&tmp_sample2)[1]*ONE_OVER_SQRT2_Q15)>>15);
	
	// fill in the rest of the ALAMOUTI precoding
	if (is_not_pilot(pilots,re + 1,frame_parms->nushift,0)==1) {
	  ((int16_t *)&txdataF[0][tti_offset+1])[0] += -((int16_t *)&txdataF[1][tti_offset])[0]; //x1
	  ((int16_t *)&txdataF[0][tti_offset+1])[1] += ((int16_t *)&txdataF[1][tti_offset])[1];
	  ((int16_t *)&txdataF[1][tti_offset+1])[0] += ((int16_t *)&txdataF[0][tti_offset])[0];  //x0*
	  ((int16_t *)&txdataF[1][tti_offset+1])[1] += -((int16_t *)&txdataF[0][tti_offset])[1];
	}
	else {
	  ((int16_t *)&txdataF[0][tti_offset+2])[0] += -((int16_t *)&txdataF[1][tti_offset])[0]; //x1
	  ((int16_t *)&txdataF[0][tti_offset+2])[1] += ((int16_t *)&txdataF[1][tti_offset])[1];
	  ((int16_t *)&txdataF[1][tti_offset+2])[0] += ((int16_t *)&txdataF[0][tti_offset])[0];  //x0*
	  ((int16_t *)&txdataF[1][tti_offset+2])[1] += -((int16_t *)&txdataF[0][tti_offset])[1];
	}
	re++;  // adjacent carriers are taken care of by precoding
	*re_allocated = *re_allocated + 1;
	if (is_not_pilot(pilots,re,frame_parms->nushift,0)==0) { // skip pilots
	  re++;  
	  *re_allocated = *re_allocated + 1;
	}
      }
    }
  }
  return(0);
}
 
gauthier's avatar
gauthier committed
155
//uint8_t pbch_d[96+(3*(16+PBCH_A))], pbch_w[3*3*(16+PBCH_A)],pbch_e[1920];  //one bit per byte
156 157 158 159
int generate_pbch(LTE_eNB_PBCH *eNB_pbch,
		  mod_sym_t **txdataF,
		  int amp,
		  LTE_DL_FRAME_PARMS *frame_parms,
gauthier's avatar
gauthier committed
160 161
		  uint8_t *pbch_pdu,
		  uint8_t frame_mod4) {
162 163 164

  int i, l;

gauthier's avatar
gauthier committed
165 166 167
  uint32_t  pbch_D,pbch_E;//,pbch_coded_bytes;
  uint8_t pbch_a[PBCH_A>>3];
  uint8_t RCC;
168

gauthier's avatar
gauthier committed
169 170
  uint32_t nsymb = (frame_parms->Ncp==NORMAL) ? 14:12;
  uint32_t pilots;
171
#ifdef INTERFERENCE_MITIGATION
gauthier's avatar
gauthier committed
172
  uint32_t pilots_2;
173
#endif
gauthier's avatar
gauthier committed
174 175 176 177 178
  uint32_t second_pilot = (frame_parms->Ncp==NORMAL) ? 4 : 3;
  uint32_t jj=0;
  uint32_t re_allocated=0;
  uint32_t rb, re_offset, symbol_offset;
  uint16_t amask=0;
179 180 181 182 183 184 185 186 187 188 189 190 191

  pbch_D    = 16+PBCH_A;

  pbch_E  = (frame_parms->Ncp==NORMAL) ? 1920 : 1728; //RE/RB * #RB * bits/RB (QPSK)
  //  pbch_E_bytes = pbch_coded_bits>>3;

  if (frame_mod4==0) {
    bzero(pbch_a,PBCH_A>>3);
    bzero(eNB_pbch->pbch_e,pbch_E);
    memset(eNB_pbch->pbch_d,LTE_NULL,96);
    // Encode data
    
    // CRC attachment
gauthier's avatar
gauthier committed
192
    //  crc = (uint16_t) (crc16(pbch_pdu, pbch_crc_bits-16) >> 16); 
193 194 195 196 197
    
    /*
    // scramble crc with PBCH CRC mask (Table 5.3.1.1-1 of 3GPP 36.212-860)
    switch (frame_parms->nb_antennas_tx_eNB) {
    case 1:
gauthier's avatar
gauthier committed
198
    crc = crc ^ (uint16_t) 0;
199 200
    break;
    case 2:
gauthier's avatar
gauthier committed
201
    crc = crc ^ (uint16_t) 0xFFFF;
202 203
    break;
    case 4:
gauthier's avatar
gauthier committed
204
    crc = crc ^ (uint16_t) 0xAAAA;
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
    break;
    default:
    msg("[PBCH] Unknown number of TX antennas!\n");
    break;
    }
    */
    
    // Fix byte endian of PBCH (bit 23 goes in first)
    for (i=0;i<(PBCH_A>>3);i++) 
      pbch_a[(PBCH_A>>3)-i-1] = pbch_pdu[i];
    //  pbch_data[i] = ((char*) &crc)[0];
    //  pbch_data[i+1] = ((char*) &crc)[1];
#ifdef DEBUG_PBCH
    for (i=0;i<(PBCH_A>>3);i++) 
      msg("[PBCH] pbch_data[%d] = %x\n",i,pbch_a[i]);
#endif
    if (frame_parms->mode1_flag == 1)
      amask = 0x0000;
    else {
      switch (frame_parms->nb_antennas_tx_eNB) {
      case 1:
	amask = 0x0000;
	break;
      case 2:
	amask = 0xffff;
	break;
      case 4:
	amask = 0x5555;
      }
    }
    ccodelte_encode(PBCH_A,2,pbch_a,eNB_pbch->pbch_d+96,amask);

     
#ifdef DEBUG_PBCH_ENCODING
    for (i=0;i<16+PBCH_A;i++)
      msg("%d : (%d,%d,%d)\n",i,*(eNB_pbch->pbch_d+96+(3*i)),*(eNB_pbch->pbch_d+97+(3*i)),*(eNB_pbch->pbch_d+98+(3*i)));
#endif //DEBUG_PBCH_ENCODING
    
    // Bit collection
    /*
      j2=0;
      for (j=0;j<pbch_crc_bits*3+12;j++) {
      if ((pbch_coded_data[j]&0x80) > 0) { // bit is to be transmitted
      pbch_coded_data2[j2++] = pbch_coded_data[j]&1;
      //Bit is repeated
      if ((pbch_coded_data[j]&0x40)>0)
      pbch_coded_data2[j2++] = pbch_coded_data[j]&1;
      }
      }
      
      #ifdef DEBUG_PBCH			
      msg("[PBCH] rate matched bits=%d, pbch_coded_bits=%d, pbch_crc_bits=%d\n",j2,pbch_coded_bits,pbch_crc_bits);
      #endif
      
      #ifdef DEBUG_PBCH
      #ifdef USER_MODE
      write_output("pbch_encoded_output2.m","pbch_encoded_out2",
      pbch_coded_data2,
      pbch_coded_bits,
      1,
      4);
      #endif //USER_MODE
      #endif //DEBUG_PBCH
    */
#ifdef DEBUG_PBCH_ENCODING
    msg("Doing PBCH interleaving for %d coded bits, e %p\n",pbch_D,eNB_pbch->pbch_e);
#endif
    RCC = sub_block_interleaving_cc(pbch_D,eNB_pbch->pbch_d+96,eNB_pbch->pbch_w);
    
    lte_rate_matching_cc(RCC,pbch_E,eNB_pbch->pbch_w,eNB_pbch->pbch_e);

#ifdef DEBUG_PBCH_ENCODING
    msg("PBCH_e:\n");
    for (i=0;i<pbch_E;i++)
      msg("%d %d\n",i,*(eNB_pbch->pbch_e+i));
    msg("\n");
#endif


   
#ifdef DEBUG_PBCH
#ifdef USER_MODE
    if (frame_mod4==0) {
      write_output("pbch_e.m","pbch_e",
		   eNB_pbch->pbch_e,
		   pbch_E,
		   1,
		   4);
      for (i=0;i<16;i++)
	printf("e[%d] %d\n",i,eNB_pbch->pbch_e[i]);
    }
#endif //USER_MODE
#endif //DEBUG_PBCH
    // scrambling

    pbch_scrambling(frame_parms,
		    eNB_pbch->pbch_e,
		    pbch_E);
#ifdef DEBUG_PBCH
#ifdef USER_MODE
    if (frame_mod4==0) {
      write_output("pbch_e_s.m","pbch_e_s",
		   eNB_pbch->pbch_e,
		   pbch_E,
		   1,
		   4);
      for (i=0;i<16;i++)
	printf("e_s[%d] %d\n",i,eNB_pbch->pbch_e[i]);
    }
#endif //USER_MODE
#endif //DEBUG_PBCH 
  } // frame_mod4==0

  // modulation and mapping (slot 1, symbols 0..3)
  for (l=(nsymb>>1);l<(nsymb>>1)+4;l++) {
    
    pilots=0;
322
#ifdef INTERFERENCE_MITIGATION
323
    pilots_2 = 0;
324
#endif
325 326
    if ((l==0) || (l==(nsymb>>1))){
      pilots=1;
327
#ifdef INTERFERENCE_MITIGATION
328
      pilots_2=1;
329
#endif
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    }

    if ((l==1) || (l==(nsymb>>1)+1)){
      pilots=1;
    }

    if ((l==second_pilot)||(l==(second_pilot+(nsymb>>1)))) {
      pilots=1;
    }

#ifdef DEBUG_PBCH
    msg("[PBCH] l=%d, pilots=%d\n",l,pilots);
#endif

    
    re_offset = frame_parms->ofdm_symbol_size-3*12;
    symbol_offset = frame_parms->ofdm_symbol_size*l;
    
    for (rb=0;rb<6;rb++) {

#ifdef DEBUG_PBCH
      msg("RB %d, jj %d, re_offset %d, symbol_offset %d, pilots %d, nushift %d\n",rb,jj,re_offset, symbol_offset, pilots,frame_parms->nushift);
#endif
353 354 355 356 357 358 359
      allocate_pbch_REs_in_RB(frame_parms,
			      txdataF,
			      &jj,
			      re_offset,
			      symbol_offset,
			      &eNB_pbch->pbch_e[frame_mod4*(pbch_E>>2)],
			      pilots,
360
#ifdef INTERFERENCE_MITIGATION
361
			      (pilots_2==1)?(amp/3):amp,
362
#else
363
			      amp,
364
#endif
365
			      &re_allocated);
366 367 368 369
      
      re_offset+=12; // go to next RB
      
      // check if we crossed the symbol boundary and skip DC
370

371 372 373 374 375 376 377
      if (re_offset >= frame_parms->ofdm_symbol_size)
	re_offset=1;
    }
    
    //    }
  }
#ifdef DEBUG_PBCH
378 379 380 381 382 383 384 385 386 387 388 389
  printf("[PBCH] txdataF=\n");
  for (i=0;i<frame_parms->ofdm_symbol_size;i++) { 
    printf("%d=>(%d,%d)",i,((short*)&txdataF[0][frame_parms->ofdm_symbol_size*(nsymb>>1)+i])[0],
	   ((short*)&txdataF[0][frame_parms->ofdm_symbol_size*(nsymb>>1)+i])[1]);
    if (frame_parms->mode1_flag==0) {
      printf("(%d,%d)\n",((short*)&txdataF[1][frame_parms->ofdm_symbol_size*(nsymb>>1)+i])[0],
	     ((short*)&txdataF[1][frame_parms->ofdm_symbol_size*(nsymb>>1)+i])[1]);
    }
    else {
      printf("\n");
    }
  }
390 391 392 393 394 395
#endif
  
  
  return(0);
}

gauthier's avatar
gauthier committed
396
int32_t generate_pbch_emul(PHY_VARS_eNB *phy_vars_eNB,uint8_t *pbch_pdu) {
397 398
  
  LOG_D(PHY,"[eNB %d] generate_pbch_emul \n",phy_vars_eNB->Mod_id);
knopp's avatar
 
knopp committed
399
  eNB_transport_info[phy_vars_eNB->Mod_id][phy_vars_eNB->CC_id].cntl.pbch_flag=1;
400
  // Copy PBCH payload 
knopp's avatar
 
knopp committed
401
  eNB_transport_info[phy_vars_eNB->Mod_id][phy_vars_eNB->CC_id].cntl.pbch_payload=*(uint32_t *)pbch_pdu;
402 403 404
  return(0);
}

gauthier's avatar
gauthier committed
405
uint16_t pbch_extract(int **rxdataF,
406 407 408 409 410 411
		      int **dl_ch_estimates,
		      int **rxdataF_ext,
		      int **dl_ch_estimates_ext,
		      uint32_t symbol,
		      uint32_t high_speed_flag,
		      LTE_DL_FRAME_PARMS *frame_parms) {
412 413
  

gauthier's avatar
gauthier committed
414 415
  uint16_t rb,nb_rb=6;
  uint8_t i,j,aarx,aatx;
416 417
  int *dl_ch0,*dl_ch0_ext,*rxF,*rxF_ext;
 
gauthier's avatar
gauthier committed
418 419
  uint32_t nsymb = (frame_parms->Ncp==0) ? 7:6;
  uint32_t symbol_mod = symbol % nsymb;
420 421 422 423 424 425 426 427 428 429 430

  int rx_offset = frame_parms->ofdm_symbol_size-3*12;
  int ch_offset = frame_parms->N_RB_DL*6-3*12;
  int nushiftmod3 = frame_parms->nushift%3;

  for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {
    /*
    printf("extract_rbs (nushift %d): symbol_mod=%d, rx_offset=%d, ch_offset=%d\n",frame_parms->nushift,symbol_mod,
	   (rx_offset + (symbol*(frame_parms->ofdm_symbol_size)))*2,
	   LTE_CE_OFFSET+ch_offset+(symbol_mod*(frame_parms->ofdm_symbol_size)));
    */
431

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    rxF        = &rxdataF[aarx][(rx_offset + (symbol*(frame_parms->ofdm_symbol_size)))];
    rxF_ext    = &rxdataF_ext[aarx][symbol_mod*(6*12)];

    for (rb=0; rb<nb_rb; rb++) {
      // skip DC carrier
      if (rb==3) {
	rxF       = &rxdataF[aarx][(1 + (symbol*(frame_parms->ofdm_symbol_size)))];
      }
      if ((symbol_mod==0) || (symbol_mod==1)) {
	j=0;
	for (i=0;i<12;i++) {
	  if ((i!=nushiftmod3) && 
	      (i!=(nushiftmod3+3)) && 
	      (i!=(nushiftmod3+6)) && 
	      (i!=(nushiftmod3+9))) {
	    rxF_ext[j++]=rxF[i];
	  }
	}
	rxF+=12;
	rxF_ext+=8;
      }
      else {
	for (i=0;i<12;i++) {
	  rxF_ext[i]=rxF[i];
	}
	rxF+=12;
	rxF_ext+=12;
      }
    }

    for (aatx=0;aatx<4;aatx++) {//frame_parms->nb_antennas_tx_eNB;aatx++) {
463 464 465 466
      if (high_speed_flag == 1)
	dl_ch0     = &dl_ch_estimates[(aatx<<1)+aarx][LTE_CE_OFFSET+ch_offset+(symbol*(frame_parms->ofdm_symbol_size))];
      else
	dl_ch0     = &dl_ch_estimates[(aatx<<1)+aarx][LTE_CE_OFFSET+ch_offset];
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
      dl_ch0_ext = &dl_ch_estimates_ext[(aatx<<1)+aarx][symbol_mod*(6*12)];

      for (rb=0; rb<nb_rb; rb++) {
	// skip DC carrier
	// if (rb==3) dl_ch0++;
	if (symbol_mod>1) {
	  memcpy(dl_ch0_ext,dl_ch0,12*sizeof(int));
	  dl_ch0+=12;
	  dl_ch0_ext+=12;
	}
	else {
	  j=0;
	  for (i=0;i<12;i++) {
	    if ((i!=nushiftmod3) && 
		(i!=(nushiftmod3+3)) && 
		(i!=(nushiftmod3+6)) && 
		(i!=(nushiftmod3+9))){
	      //	      printf("PBCH extract i %d j %d => (%d,%d)\n",i,j,*(short *)&dl_ch0[i],*(1+(short*)&dl_ch0[i]));
	      dl_ch0_ext[j++]=dl_ch0[i];
	    }
	  }	    
	  dl_ch0+=12;
	  dl_ch0_ext+=8;
	}
     }
    }  //tx antenna loop

  }

  return(0);
}

__m128i avg128;

//compute average channel_level on each (TX,RX) antenna pair
int pbch_channel_level(int **dl_ch_estimates_ext,
		       LTE_DL_FRAME_PARMS *frame_parms,
gauthier's avatar
gauthier committed
504
		       uint32_t symbol) {
505

gauthier's avatar
gauthier committed
506 507
  int16_t rb, nb_rb=6;
  uint8_t aatx,aarx;
508 509 510
  __m128i *dl_ch128;
  int avg1=0,avg2=0;

gauthier's avatar
gauthier committed
511 512
  uint32_t nsymb = (frame_parms->Ncp==0) ? 7:6;
  uint32_t symbol_mod = symbol % nsymb;
513 514 515 516

  for (aatx=0;aatx<4;aatx++) //frame_parms->nb_antennas_tx_eNB;aatx++)
    for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {
      //clear average level
517
      avg128 = _mm_setzero_si128();
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
      dl_ch128=(__m128i *)&dl_ch_estimates_ext[(aatx<<1)+aarx][symbol_mod*6*12];

      for (rb=0;rb<nb_rb;rb++) {
    
	avg128 = _mm_add_epi32(avg128,_mm_madd_epi16(dl_ch128[0],dl_ch128[0]));
	avg128 = _mm_add_epi32(avg128,_mm_madd_epi16(dl_ch128[1],dl_ch128[1]));
	avg128 = _mm_add_epi32(avg128,_mm_madd_epi16(dl_ch128[2],dl_ch128[2]));

	dl_ch128+=3;	
	/*
	  if (rb==0) {
	  print_shorts("dl_ch128",&dl_ch128[0]);
	  print_shorts("dl_ch128",&dl_ch128[1]);
	  print_shorts("dl_ch128",&dl_ch128[2]);
	  }
	*/
      }

      avg1 = (((int*)&avg128)[0] + 
	      ((int*)&avg128)[1] + 
	      ((int*)&avg128)[2] + 
	      ((int*)&avg128)[3])/(nb_rb*12);

      if (avg1>avg2) 
	avg2 = avg1;

      //msg("Channel level : %d, %d\n",avg1, avg2);
    }

  _mm_empty();
  _m_empty();

  return(avg2);

}

__m128i mmtmpP0,mmtmpP1,mmtmpP2,mmtmpP3;

void pbch_channel_compensation(int **rxdataF_ext,
			       int **dl_ch_estimates_ext,
			       int **rxdataF_comp,
			       LTE_DL_FRAME_PARMS *frame_parms,
gauthier's avatar
gauthier committed
560 561
			       uint8_t symbol,
			       uint8_t output_shift) {
562

gauthier's avatar
gauthier committed
563 564
  uint16_t rb,nb_rb=6;
  uint8_t aatx,aarx,symbol_mod;
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
  __m128i *dl_ch128,*rxdataF128,*rxdataF_comp128;

  symbol_mod = (symbol>=(7-frame_parms->Ncp)) ? symbol-(7-frame_parms->Ncp) : symbol;
  
  for (aatx=0;aatx<4;aatx++) //frame_parms->nb_antennas_tx_eNB;aatx++)
    for (aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {

      dl_ch128          = (__m128i *)&dl_ch_estimates_ext[(aatx<<1)+aarx][symbol_mod*6*12];
      rxdataF128        = (__m128i *)&rxdataF_ext[aarx][symbol_mod*6*12];
      rxdataF_comp128   = (__m128i *)&rxdataF_comp[(aatx<<1)+aarx][symbol_mod*6*12];


      for (rb=0;rb<nb_rb;rb++) {
	//printf("rb %d\n",rb);
	
	// multiply by conjugated channel
	mmtmpP0 = _mm_madd_epi16(dl_ch128[0],rxdataF128[0]);
	//	print_ints("re",&mmtmpP0);
	// mmtmpP0 contains real part of 4 consecutive outputs (32-bit)
	mmtmpP1 = _mm_shufflelo_epi16(dl_ch128[0],_MM_SHUFFLE(2,3,0,1));
	mmtmpP1 = _mm_shufflehi_epi16(mmtmpP1,_MM_SHUFFLE(2,3,0,1));
	mmtmpP1 = _mm_sign_epi16(mmtmpP1,*(__m128i*)&conjugate[0]);
	//	print_ints("im",&mmtmpP1);
	mmtmpP1 = _mm_madd_epi16(mmtmpP1,rxdataF128[0]);
	// mmtmpP1 contains imag part of 4 consecutive outputs (32-bit)
	mmtmpP0 = _mm_srai_epi32(mmtmpP0,output_shift);
	//	print_ints("re(shift)",&mmtmpP0);
	mmtmpP1 = _mm_srai_epi32(mmtmpP1,output_shift);
	//	print_ints("im(shift)",&mmtmpP1);
	mmtmpP2 = _mm_unpacklo_epi32(mmtmpP0,mmtmpP1);
	mmtmpP3 = _mm_unpackhi_epi32(mmtmpP0,mmtmpP1);
	//      print_ints("c0",&mmtmpP2);
	//	print_ints("c1",&mmtmpP3);
	rxdataF_comp128[0] = _mm_packs_epi32(mmtmpP2,mmtmpP3);
	//	print_shorts("rx:",rxdataF128);
	//	print_shorts("ch:",dl_ch128);
	//	print_shorts("pack:",rxdataF_comp128);

	// multiply by conjugated channel
	mmtmpP0 = _mm_madd_epi16(dl_ch128[1],rxdataF128[1]);
	// mmtmpP0 contains real part of 4 consecutive outputs (32-bit)
	mmtmpP1 = _mm_shufflelo_epi16(dl_ch128[1],_MM_SHUFFLE(2,3,0,1));
	mmtmpP1 = _mm_shufflehi_epi16(mmtmpP1,_MM_SHUFFLE(2,3,0,1));
	mmtmpP1 = _mm_sign_epi16(mmtmpP1,*(__m128i*)&conjugate[0]);
	mmtmpP1 = _mm_madd_epi16(mmtmpP1,rxdataF128[1]);
	// mmtmpP1 contains imag part of 4 consecutive outputs (32-bit)
	mmtmpP0 = _mm_srai_epi32(mmtmpP0,output_shift);
	mmtmpP1 = _mm_srai_epi32(mmtmpP1,output_shift);
	mmtmpP2 = _mm_unpacklo_epi32(mmtmpP0,mmtmpP1);
	mmtmpP3 = _mm_unpackhi_epi32(mmtmpP0,mmtmpP1);
	rxdataF_comp128[1] = _mm_packs_epi32(mmtmpP2,mmtmpP3);
	//	print_shorts("rx:",rxdataF128+1);
	//	print_shorts("ch:",dl_ch128+1);
	//	print_shorts("pack:",rxdataF_comp128+1);	

	if (symbol_mod>1) {
	  // multiply by conjugated channel
	  mmtmpP0 = _mm_madd_epi16(dl_ch128[2],rxdataF128[2]);
	  // mmtmpP0 contains real part of 4 consecutive outputs (32-bit)
	  mmtmpP1 = _mm_shufflelo_epi16(dl_ch128[2],_MM_SHUFFLE(2,3,0,1));
	  mmtmpP1 = _mm_shufflehi_epi16(mmtmpP1,_MM_SHUFFLE(2,3,0,1));
	  mmtmpP1 = _mm_sign_epi16(mmtmpP1,*(__m128i*)&conjugate[0]);
	  mmtmpP1 = _mm_madd_epi16(mmtmpP1,rxdataF128[2]);
	  // mmtmpP1 contains imag part of 4 consecutive outputs (32-bit)
	  mmtmpP0 = _mm_srai_epi32(mmtmpP0,output_shift);
	  mmtmpP1 = _mm_srai_epi32(mmtmpP1,output_shift);
	  mmtmpP2 = _mm_unpacklo_epi32(mmtmpP0,mmtmpP1);
	  mmtmpP3 = _mm_unpackhi_epi32(mmtmpP0,mmtmpP1);
	  rxdataF_comp128[2] = _mm_packs_epi32(mmtmpP2,mmtmpP3);
	  //	print_shorts("rx:",rxdataF128+2);
	  //	print_shorts("ch:",dl_ch128+2);
	  //      print_shorts("pack:",rxdataF_comp128+2);
	  
	  dl_ch128+=3;
	  rxdataF128+=3;
	  rxdataF_comp128+=3;
	}
	else {
	  dl_ch128+=2;
	  rxdataF128+=2;
	  rxdataF_comp128+=2;
	}
      }
    }
  _mm_empty();
  _m_empty();
}     

void pbch_detection_mrc(LTE_DL_FRAME_PARMS *frame_parms,
			int **rxdataF_comp,
gauthier's avatar
gauthier committed
655
			uint8_t symbol) {
656

gauthier's avatar
gauthier committed
657
  uint8_t aatx, symbol_mod;
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
  int i, nb_rb=6;
  __m128i *rxdataF_comp128_0,*rxdataF_comp128_1;

  symbol_mod = (symbol>=(7-frame_parms->Ncp)) ? symbol-(7-frame_parms->Ncp) : symbol;

  if (frame_parms->nb_antennas_rx>1) {
    for (aatx=0;aatx<4;aatx++) {//frame_parms->nb_antennas_tx_eNB;aatx++) {
      rxdataF_comp128_0   = (__m128i *)&rxdataF_comp[(aatx<<1)][symbol_mod*6*12];  
      rxdataF_comp128_1   = (__m128i *)&rxdataF_comp[(aatx<<1)+1][symbol_mod*6*12];  
      // MRC on each re of rb, both on MF output and magnitude (for 16QAM/64QAM llr computation)
      for (i=0;i<nb_rb*3;i++) {
	rxdataF_comp128_0[i] = _mm_adds_epi16(_mm_srai_epi16(rxdataF_comp128_0[i],1),_mm_srai_epi16(rxdataF_comp128_1[i],1));
      }
    }
  }
  _mm_empty();
  _m_empty();
}

void pbch_scrambling(LTE_DL_FRAME_PARMS *frame_parms,
gauthier's avatar
gauthier committed
678 679
		     uint8_t *pbch_e,
		     uint32_t length) {
680
  int i;
gauthier's avatar
gauthier committed
681 682
  uint8_t reset;
  uint32_t x1, x2, s=0;
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701

  reset = 1;
  // x1 is set in lte_gold_generic
  x2 = frame_parms->Nid_cell; //this is c_init in 36.211 Sec 6.6.1
  //  msg("pbch_scrambling: Nid_cell = %d\n",x2);

  for (i=0; i<length; i++) {
    if ((i&0x1f)==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      //      printf("lte_gold[%d]=%x\n",i,s);
      reset = 0;
    }

    pbch_e[i] = (pbch_e[i]&1) ^ ((s>>(i&0x1f))&1);

  }
}

void pbch_unscrambling(LTE_DL_FRAME_PARMS *frame_parms,
gauthier's avatar
gauthier committed
702 703 704
		       int8_t* llr,
		       uint32_t length,
		       uint8_t frame_mod4) {
705
  int i;
gauthier's avatar
gauthier committed
706 707
  uint8_t reset;
  uint32_t x1, x2, s=0;
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731

  reset = 1;
  // x1 is set in first call to lte_gold_generic
  x2 = frame_parms->Nid_cell; //this is c_init in 36.211 Sec 6.6.1
  //  msg("pbch_unscrambling: Nid_cell = %d\n",x2);

  for (i=0; i<length; i++) {
    if (i%32==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      //      printf("lte_gold[%d]=%x\n",i,s);
      reset = 0;
    } 
    // take the quarter of the PBCH that corresponds to this frame
    if ((i>=(frame_mod4*(length>>2))) && (i<((1+frame_mod4)*(length>>2)))) {
      //      if (((s>>(i%32))&1)==1)

      if (((s>>(i%32))&1)==0)
	llr[i] = -llr[i];
    }
  }
}

void pbch_alamouti(LTE_DL_FRAME_PARMS *frame_parms,
		   int **rxdataF_comp,
gauthier's avatar
gauthier committed
732
		   uint8_t symbol) {
733 734


gauthier's avatar
gauthier committed
735
  int16_t *rxF0,*rxF1;
736
  //  __m128i *ch_mag0,*ch_mag1,*ch_mag0b,*ch_mag1b;
gauthier's avatar
gauthier committed
737
  uint8_t rb,re,symbol_mod;
738 739 740 741 742 743
  int jj;

  //  printf("Doing alamouti\n");
  symbol_mod = (symbol>=(7-frame_parms->Ncp)) ? symbol-(7-frame_parms->Ncp) : symbol;
  jj         = (symbol_mod*6*12);

gauthier's avatar
gauthier committed
744 745
  rxF0     = (int16_t*)&rxdataF_comp[0][jj];  //tx antenna 0  h0*y
  rxF1     = (int16_t*)&rxdataF_comp[2][jj];  //tx antenna 1  h1*y
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

  for (rb=0;rb<6;rb++) {

    for (re=0;re<12;re+=2) {

      // Alamouti RX combining
      
      rxF0[0] = rxF0[0] + rxF1[2];
      rxF0[1] = rxF0[1] - rxF1[3];

      rxF0[2] = rxF0[2] - rxF1[0];
      rxF0[3] = rxF0[3] + rxF1[1];
 
      rxF0+=4;
      rxF1+=4;
    }

  }

  _mm_empty();
  _m_empty();
  
}

gauthier's avatar
gauthier committed
770 771 772
void pbch_quantize(int8_t *pbch_llr8,
		   int16_t *pbch_llr,
		   uint16_t len) {
773

gauthier's avatar
gauthier committed
774
  uint16_t i;
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

  for (i=0;i<len;i++) { 
    if (pbch_llr[i]>7)
      pbch_llr8[i]=7;
    else if (pbch_llr[i]<-8)
      pbch_llr8[i]=-8;
    else
      pbch_llr8[i] = (char)(pbch_llr[i]);

  }
}

static unsigned char dummy_w_rx[3*3*(16+PBCH_A)];
static int8_t pbch_w_rx[3*3*(16+PBCH_A)],pbch_d_rx[96+(3*(16+PBCH_A))];


gauthier's avatar
gauthier committed
791
uint16_t rx_pbch(LTE_UE_COMMON *lte_ue_common_vars,
792 793 794 795 796 797
		 LTE_UE_PBCH *lte_ue_pbch_vars,
		 LTE_DL_FRAME_PARMS *frame_parms,
		 uint8_t eNB_id,
		 MIMO_mode_t mimo_mode,
		 uint32_t high_speed_flag,
		 uint8_t frame_mod4) {
798

gauthier's avatar
gauthier committed
799
  uint8_t log2_maxh;//,aatx,aarx;
800 801 802
  int max_h=0;

  int symbol,i;
gauthier's avatar
gauthier committed
803 804 805 806
  uint32_t nsymb = (frame_parms->Ncp==0) ? 14:12;
  uint16_t  pbch_E;
  uint8_t pbch_a[8];
  uint8_t RCC;
807

gauthier's avatar
gauthier committed
808 809 810
  int8_t *pbch_e_rx;
  uint8_t *decoded_output = lte_ue_pbch_vars->decoded_output;
  uint16_t crc;
811 812 813 814 815 816 817


  //  pbch_D    = 16+PBCH_A;

  pbch_E  = (frame_parms->Ncp==0) ? 1920 : 1728; //RE/RB * #RB * bits/RB (QPSK)
  pbch_e_rx = &lte_ue_pbch_vars->llr[frame_mod4*(pbch_E>>2)];
#ifdef DEBUG_PBCH
knopp's avatar
 
knopp committed
818
  msg("[PBCH] starting symbol loop (Ncp %d, frame_mod4 %d,mimo_mode %d\n",frame_parms->Ncp,frame_mod4,mimo_mode);
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
#endif

  // clear LLR buffer
  memset(lte_ue_pbch_vars->llr,0,pbch_E);

  for (symbol=(nsymb>>1);symbol<(nsymb>>1)+4;symbol++) {

#ifdef DEBUG_PBCH
    msg("[PBCH] starting extract\n");
#endif
    pbch_extract(lte_ue_common_vars->rxdataF,
		 lte_ue_common_vars->dl_ch_estimates[eNB_id],
		 lte_ue_pbch_vars->rxdataF_ext,
		 lte_ue_pbch_vars->dl_ch_estimates_ext,
		 symbol,
834
		 high_speed_flag,
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
		 frame_parms);
#ifdef DEBUG_PBCH    
    msg("[PHY] PBCH Symbol %d\n",symbol);
    msg("[PHY] PBCH starting channel_level\n");
#endif
    
    max_h = pbch_channel_level(lte_ue_pbch_vars->dl_ch_estimates_ext,
			       frame_parms,
			       symbol);
    log2_maxh = 3+(log2_approx(max_h)/2);
    
#ifdef DEBUG_PBCH
    msg("[PHY] PBCH log2_maxh = %d (%d)\n",log2_maxh,max_h);
#endif
    
    pbch_channel_compensation(lte_ue_pbch_vars->rxdataF_ext,
			      lte_ue_pbch_vars->dl_ch_estimates_ext,
			      lte_ue_pbch_vars->rxdataF_comp,
			      frame_parms,
			      symbol,
			      log2_maxh); // log2_maxh+I0_shift
    
    if (frame_parms->nb_antennas_rx > 1)
      pbch_detection_mrc(frame_parms,
			 lte_ue_pbch_vars->rxdataF_comp,
			 symbol);
    
    
    if (mimo_mode == ALAMOUTI) {
      pbch_alamouti(frame_parms,lte_ue_pbch_vars->rxdataF_comp,symbol);
      //	msg("[PBCH][RX] Alamouti receiver not yet implemented!\n");
      //	return(-1);
    }
868
    else if (mimo_mode != SISO) {
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 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 933 934 935 936 937 938 939 940 941 942 943 944
      msg("[PBCH][RX] Unsupported MIMO mode\n");
      return(-1);
    }
  
    if (symbol>(nsymb>>1)+1) {
      pbch_quantize(pbch_e_rx,
		    (short*)&(lte_ue_pbch_vars->rxdataF_comp[0][(symbol%(nsymb>>1))*72]),
		    144);
      
      pbch_e_rx+=144;
    }
    else {
      pbch_quantize(pbch_e_rx,
		    (short*)&(lte_ue_pbch_vars->rxdataF_comp[0][(symbol%(nsymb>>1))*72]),
		    96);

      pbch_e_rx+=96;
    }


  }

  pbch_e_rx = lte_ue_pbch_vars->llr;



  //un-scrambling
#ifdef DEBUG_PBCH
  msg("[PBCH] doing unscrambling\n");
#endif

  
  pbch_unscrambling(frame_parms,
		    pbch_e_rx,
		    pbch_E,
		    frame_mod4);
  


  //un-rate matching
#ifdef DEBUG_PBCH
  msg("[PBCH] doing un-rate-matching\n");
#endif


  memset(dummy_w_rx,0,3*3*(16+PBCH_A));
  RCC = generate_dummy_w_cc(16+PBCH_A,
			    dummy_w_rx);


  lte_rate_matching_cc_rx(RCC,pbch_E,pbch_w_rx,dummy_w_rx,pbch_e_rx);

  sub_block_deinterleaving_cc((unsigned int)(PBCH_A+16), 
			      &pbch_d_rx[96], 
			      &pbch_w_rx[0]); 

  memset(pbch_a,0,((16+PBCH_A)>>3));




  phy_viterbi_lte_sse2(pbch_d_rx+96,pbch_a,16+PBCH_A);
  
  // Fix byte endian of PBCH (bit 23 goes in first)
  for (i=0;i<(PBCH_A>>3);i++) 
    decoded_output[(PBCH_A>>3)-i-1] = pbch_a[i];

#ifdef DEBUG_PBCH
  for (i=0;i<(PBCH_A>>3);i++) 
    msg("[PBCH] pbch_a[%d] = %x\n",i,decoded_output[i]);

#endif //DEBUG_PBCH

#ifdef DEBUG_PBCH
  msg("PBCH CRC %x : %x\n",
      crc16(pbch_a,PBCH_A),
gauthier's avatar
gauthier committed
945
      ((uint16_t)pbch_a[PBCH_A>>3]<<8)+pbch_a[(PBCH_A>>3)+1]);
946 947 948
#endif

  crc = (crc16(pbch_a,PBCH_A)>>16) ^ 
gauthier's avatar
gauthier committed
949
    (((uint16_t)pbch_a[PBCH_A>>3]<<8)+pbch_a[(PBCH_A>>3)+1]);
950 951 952 953 954 955 956 957 958 959 960 961 962

  if (crc == 0x0000)
    return(1);
  else if (crc == 0xffff)
    return(2);
  else if (crc == 0x5555)
    return(4);
  else 
    return(-1);
  
  
}

963
#ifdef PHY_ABSTRACTION
gauthier's avatar
gauthier committed
964 965 966
uint16_t rx_pbch_emul(PHY_VARS_UE *phy_vars_ue,
		 uint8_t eNB_id,
		 uint8_t pbch_phase) {
967

968
  double bler=0.0;//, x=0.0;
969
  double sinr=0.0;
gauthier's avatar
gauthier committed
970 971
  uint16_t nb_rb = phy_vars_ue->lte_frame_parms.N_RB_DL;
  int16_t f;
knopp's avatar
 
knopp committed
972 973
  uint8_t CC_id=phy_vars_ue->CC_id;

974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
  // compute effective sinr
  // TODO: adapt this to varible bandwidth
  for (f=(nb_rb*6-3*12);f<(nb_rb*6+3*12);f++) {
    if (f!=0) //skip DC
      sinr += pow(10, 0.1*(phy_vars_ue->sinr_dB[f]));
  }
  sinr = 10*log10(sinr/(6*12));
 
  bler = pbch_bler(sinr);
 
  LOG_D(PHY,"EMUL UE rx_pbch_emul: eNB_id %d, pbch_phase %d, sinr %f dB, bler %f \n",
	eNB_id,
	pbch_phase,
	sinr,
	bler);
989

knopp's avatar
 
knopp committed
990
  if (pbch_phase == (phy_vars_ue->frame_rx % 4)) {
991
    if (uniformrandom() >= bler) {
knopp's avatar
 
knopp committed
992 993
      memcpy(phy_vars_ue->lte_ue_pbch_vars[eNB_id]->decoded_output,PHY_vars_eNB_g[eNB_id][CC_id]->pbch_pdu,PBCH_PDU_SIZE);    
      return(PHY_vars_eNB_g[eNB_id][CC_id]->lte_frame_parms.nb_antennas_tx_eNB);
994 995 996 997 998 999 1000
    }
    else
      return(-1);
  }
  else
    return(-1);
}
1001
#endif