pss_nr.c 28.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * 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
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * 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
 */

/**********************************************************************
*
* FILENAME    :  pss_nr.c
*
* MODULE      :  synchronisation signal
*
* DESCRIPTION :  generation of pss
*                3GPP TS 38.211 7.4.2.2 Primary synchronisation signal
*
************************************************************************/

#include <stdio.h>
#include <assert.h>
#include <errno.h>
36
#include <math.h>
37 38 39 40

#include "PHY/defs_nr_UE.h"

#include "PHY/NR_REFSIG/ss_pbch_nr.h"
Rakesh's avatar
Rakesh committed
41
#include "common/utils/LOG/vcd_signal_dumper.h"
42 43 44 45 46 47 48 49 50 51 52 53 54 55

#define DEFINE_VARIABLES_PSS_NR_H
#include "PHY/NR_REFSIG/pss_nr.h"
#undef DEFINE_VARIABLES_PSS_NR_H

#include "PHY/NR_REFSIG/sss_nr.h"
#include "PHY/NR_UE_TRANSPORT/cic_filter_nr.h"

/*******************************************************************
*
* NAME :         get_idft
*
* PARAMETERS :   size of ofdm symbol
*
frtabu's avatar
frtabu committed
56
* RETURN :       index pointing to the dft func in the dft library
57 58 59 60 61
*
* DESCRIPTION :  get idft function depending of ofdm size
*
*********************************************************************/

62
//#define DBG_PSS_NR
63

frtabu's avatar
frtabu committed
64
idft_size_idx_t get_idft(int ofdm_symbol_size)
65
{
frtabu's avatar
frtabu committed
66 67
  
 
68 69
  switch (ofdm_symbol_size) {
    case 128:
frtabu's avatar
frtabu committed
70
      return IDFT_128;
71 72 73
      break;

    case 256:
frtabu's avatar
frtabu committed
74
      return IDFT_256;
75 76 77
      break;

    case 512:
frtabu's avatar
frtabu committed
78
      return IDFT_512;
79 80 81
      break;

    case 1024:
frtabu's avatar
frtabu committed
82
      return IDFT_1024;
83 84 85
      break;

    case 1536:
frtabu's avatar
frtabu committed
86
      return IDFT_1536;
87 88 89
      break;

    case 2048:
frtabu's avatar
frtabu committed
90
      return IDFT_2048;
91 92
      break;

93
    case 3072:
frtabu's avatar
frtabu committed
94
      return IDFT_3072;
95 96
      break;

Raymond Knopp's avatar
Raymond Knopp committed
97
    case 4096:
frtabu's avatar
frtabu committed
98
      return IDFT_4096;
Raymond Knopp's avatar
Raymond Knopp committed
99 100 101
      break;

    case 8192:
frtabu's avatar
frtabu committed
102
      return IDFT_8192;
Raymond Knopp's avatar
Raymond Knopp committed
103 104
      break;

105 106 107 108 109
    default:
      printf("function get_idft : unsupported ofdm symbol size \n");
      assert(0);
      break;
 }
frtabu's avatar
frtabu committed
110
 return IDFT_SIZE_IDXTABLESIZE; // never reached and will trigger assertion in idft function
111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

/*******************************************************************
*
* NAME :         get_dft
*
* PARAMETERS :   size of ofdm symbol
*
* RETURN :       function for discrete fourier transform
*
* DESCRIPTION :  get dft function depending of ofdm size
*
*********************************************************************/

frtabu's avatar
frtabu committed
125
dft_size_idx_t get_dft(int ofdm_symbol_size)
126
{
frtabu's avatar
frtabu committed
127

128 129 130

  switch (ofdm_symbol_size) {
    case 128:
frtabu's avatar
frtabu committed
131
      return DFT_128;
132 133 134
      break;

    case 256:
frtabu's avatar
frtabu committed
135
      return DFT_256;
136 137 138
      break;

    case 512:
frtabu's avatar
frtabu committed
139
      return DFT_512;
140 141 142
      break;

    case 1024:
frtabu's avatar
frtabu committed
143
      return DFT_1024;
144 145 146
      break;

    case 1536:
frtabu's avatar
frtabu committed
147
      return DFT_1536;
148 149 150
      break;

    case 2048:
frtabu's avatar
frtabu committed
151
      return DFT_2048;
152 153
      break;

Raymond Knopp's avatar
Raymond Knopp committed
154
    case 4096:
frtabu's avatar
frtabu committed
155
      return DFT_4096;
Raymond Knopp's avatar
Raymond Knopp committed
156 157 158
      break;

    case 8192:
frtabu's avatar
frtabu committed
159
      return DFT_8192;
Raymond Knopp's avatar
Raymond Knopp committed
160 161
      break;

162 163 164 165 166
    default:
      printf("function get_dft : unsupported ofdm symbol size \n");
      assert(0);
      break;
 }
frtabu's avatar
frtabu committed
167
 return DFT_SIZE_IDXTABLESIZE; // never reached and will trigger assertion in idft function;
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

/*******************************************************************
*
* NAME :         generate_pss_nr
*
* PARAMETERS :   N_ID_2 : element 2 of physical layer cell identity
*                value : { 0, 1, 2}
*
* RETURN :       generate binary pss sequence (this is a m-sequence)
*
* DESCRIPTION :  3GPP TS 38.211 7.4.2.2 Primary synchronisation signal
*                Sequence generation
*
*********************************************************************/

Raymond Knopp's avatar
Raymond Knopp committed
184
void generate_pss_nr(NR_DL_FRAME_PARMS *fp,int N_ID_2)
185
{
Raymond Knopp's avatar
Raymond Knopp committed
186
  AssertFatal(fp->ofdm_symbol_size > 127,"Illegal ofdm_symbol_size %d\n",fp->ofdm_symbol_size);
Raymond Knopp's avatar
Raymond Knopp committed
187
  AssertFatal(N_ID_2>=0 && N_ID_2 <=2,"Illegal N_ID_2 %d\n",N_ID_2);
188 189 190
  int16_t d_pss[LENGTH_PSS_NR];
  int16_t x[LENGTH_PSS_NR];
  int16_t *primary_synchro_time = primary_synchro_time_nr[N_ID_2];
Raymond Knopp's avatar
Raymond Knopp committed
191
  unsigned int length = fp->ofdm_symbol_size;
192 193
  unsigned int size = length * IQ_SIZE; /* i & q */
  int16_t *primary_synchro = primary_synchro_nr[N_ID_2]; /* pss in complex with alternatively i then q */
Raymond Knopp's avatar
Raymond Knopp committed
194
  int16_t *primary_synchro2 = primary_synchro_nr2[N_ID_2]; /* pss in complex with alternatively i then q */
frtabu's avatar
frtabu committed
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

  #define INITIAL_PSS_NR    (7)
  const int x_initial[INITIAL_PSS_NR] = {0, 1, 1 , 0, 1, 1, 1};

  assert(N_ID_2 < NUMBER_PSS_SEQUENCE);
  assert(size <= SYNCF_TMP_SIZE);
  assert(size <= SYNC_TMP_SIZE);

  bzero(synchroF_tmp, size);
  bzero(synchro_tmp, size);

  for (int i=0; i < INITIAL_PSS_NR; i++) {
    x[i] = x_initial[i];
  }

  for (int i=0; i < (LENGTH_PSS_NR - INITIAL_PSS_NR); i++) {
    x[i+INITIAL_PSS_NR] = (x[i + 4] + x[i])%(2);
  }

  for (int n=0; n < LENGTH_PSS_NR; n++) {
	int m = (n + 43*N_ID_2)%(LENGTH_PSS_NR);
    d_pss[n] = 1 - 2*x[m];
  }

  /* PSS is directly mapped to subcarrier without modulation 38.211 */
  for (int i=0; i < LENGTH_PSS_NR; i++) {
#if 1
    primary_synchro[2*i] = (d_pss[i] * SHRT_MAX)>>SCALING_PSS_NR; /* Maximum value for type short int ie int16_t */
    primary_synchro[2*i+1] = 0;
Raymond Knopp's avatar
Raymond Knopp committed
225
    primary_synchro2[i] = d_pss[i];
226 227 228
#else
    primary_synchro[2*i] = d_pss[i] * AMP;
    primary_synchro[2*i+1] = 0;
Raymond Knopp's avatar
Raymond Knopp committed
229
    primary_synchro2[i] = d_pss[i];
230 231 232 233 234 235 236 237
#endif
  }

#ifdef DBG_PSS_NR

  if (N_ID_2 == 0) {
    char output_file[255];
    char sequence_name[255];
yilmazt's avatar
yilmazt committed
238 239
    sprintf(output_file, "pss_seq_%d_%u.m", N_ID_2, length);
    sprintf(sequence_name, "pss_seq_%d_%u", N_ID_2, length);
240 241
    printf("file %s sequence %s\n", output_file, sequence_name);

Raymond Knopp's avatar
Raymond Knopp committed
242
    LOG_M(output_file, sequence_name, primary_synchro, LENGTH_PSS_NR, 1, 1);
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
  }

#endif

  /* call of IDFT should be done with ordered input as below
  *
  *                n input samples
  *  <------------------------------------------------>
  *  0                                                n
  *  are written into input buffer for IFFT
  *   -------------------------------------------------
  *  |xxxxxxx                       N/2       xxxxxxxx|
  *  --------------------------------------------------
  *  ^      ^                 ^               ^          ^
  *  |      |                 |               |          |
  * n/2    end of            n=0            start of    n/2-1
  *         pss                               pss
  *
  *                   Frequencies
  *      positives                   negatives
  * 0                 (+N/2)(-N/2)
  * |-----------------------><-------------------------|
  *
  * sample 0 is for continuous frequency which is used here
  */

Raymond Knopp's avatar
Raymond Knopp committed
269 270 271 272
  unsigned int  k = fp->first_carrier_offset + fp->ssb_start_subcarrier + 56; //and
  if (k>= fp->ofdm_symbol_size) k-=fp->ofdm_symbol_size;


273 274 275 276 277 278 279

  for (int i=0; i < LENGTH_PSS_NR; i++) {
    synchroF_tmp[2*k] = primary_synchro[2*i];
    synchroF_tmp[2*k+1] = primary_synchro[2*i+1];

    k++;

Raymond Knopp's avatar
Raymond Knopp committed
280 281
    if (k == length) k=0;
    
282 283 284 285
  }

  /* IFFT will give temporal signal of Pss */

frtabu's avatar
frtabu committed
286 287 288 289
 
 
  idft((int16_t)get_idft(length),
  	   synchroF_tmp,          /* complex input */
290 291 292 293 294 295 296 297 298 299 300 301 302
       synchro_tmp,           /* complex output */
       1);                 /* scaling factor */

  /* then get final pss in time */
  for (unsigned int i=0; i<length; i++) {
    ((int32_t *)primary_synchro_time)[i] = ((int32_t *)synchro_tmp)[i];
  }

#ifdef DBG_PSS_NR

  if (N_ID_2 == 0) {
    char output_file[255];
    char sequence_name[255];
yilmazt's avatar
yilmazt committed
303 304
    sprintf(output_file, "%s%d_%u%s","pss_seq_t_", N_ID_2, length, ".m");
    sprintf(sequence_name, "%s%d_%u","pss_seq_t_", N_ID_2, length);
305 306 307

    printf("file %s sequence %s\n", output_file, sequence_name);

Raymond Knopp's avatar
Raymond Knopp committed
308
    LOG_M(output_file, sequence_name, primary_synchro_time, length, 1, 1);
yilmazt's avatar
yilmazt committed
309 310
    sprintf(output_file, "%s%d_%u%s","pss_seq_f_", N_ID_2, length, ".m");
    sprintf(sequence_name, "%s%d_%u","pss_seq_f_", N_ID_2, length);
Raymond Knopp's avatar
Raymond Knopp committed
311
    LOG_M(output_file, sequence_name, synchroF_tmp, length, 1, 1);
312 313 314 315 316
  }

#endif


frtabu's avatar
frtabu committed
317

318 319 320 321 322 323
#if 0

/* it allows checking that process of idft on a signal and then dft gives same signal with limited errors */

  if ((N_ID_2 == 0) && (length == 256)) {

Raymond Knopp's avatar
Raymond Knopp committed
324
    LOG_M("pss_f00.m","pss_f00",synchro_tmp,length,1,1);
325 326 327 328


    bzero(synchroF_tmp, size);

frtabu's avatar
frtabu committed
329
  
330 331

    /* get pss in the time domain by applying an inverse FFT */
frtabu's avatar
frtabu committed
332 333
    dft((int16_t)get_dft(length),
    	synchro_tmp,           /* complex input */
334 335 336 337
        synchroF_tmp,          /* complex output */
        1);                 /* scaling factor */

    if ((N_ID_2 == 0) && (length == 256)) {
Raymond Knopp's avatar
Raymond Knopp committed
338
      LOG_M("pss_f_0.m","pss_f_0",synchroF_tmp,length,1,1);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    }

    /* check Pss */
    k = length - (LENGTH_PSS_NR/2);

#define LIMIT_ERROR_FFT   (10)

    for (int i=0; i < LENGTH_PSS_NR; i++) {
      if (abs(synchroF_tmp[2*k] - primary_synchro[2*i]) > LIMIT_ERROR_FFT) {
      printf("Pss Error[%d] Compute %d Reference %d \n", k, synchroF_tmp[2*k], primary_synchro[2*i]);
      }
    
      if (abs(synchroF_tmp[2*k+1] - primary_synchro[2*i+1]) > LIMIT_ERROR_FFT) {
        printf("Pss Error[%d] Compute %d Reference %d\n", (2*k+1), synchroF_tmp[2*k+1], primary_synchro[2*i+1]);
      }

      k++;

      if (k >= length) {
        k-=length;
      }
    }
  }
#endif
}

/*******************************************************************
*
* NAME :         init_context_pss_nr
*
* PARAMETERS :   structure NR_DL_FRAME_PARMS give frame parameters
*
* RETURN :       generate binary pss sequences (this is a m-sequence)
*
* DESCRIPTION :  3GPP TS 38.211 7.4.2.2 Primary synchronisation signal
*                Sequence generation
*
*********************************************************************/

void init_context_pss_nr(NR_DL_FRAME_PARMS *frame_parms_ue)
{
  int ofdm_symbol_size = frame_parms_ue->ofdm_symbol_size;
  int sizePss = LENGTH_PSS_NR * IQ_SIZE;  /* complex value i & q signed 16 bits */
  int size = ofdm_symbol_size * IQ_SIZE; /* i and q samples signed 16 bits */
  int16_t *p = NULL;
Raymond Knopp's avatar
Raymond Knopp committed
384
  int64_t *q = NULL;
385

Raymond Knopp's avatar
Raymond Knopp committed
386
  AssertFatal(ofdm_symbol_size > 127, "illegal ofdm_symbol_size %d\n",ofdm_symbol_size);
387 388 389 390 391 392 393 394
  for (int i = 0; i < NUMBER_PSS_SEQUENCE; i++) {

    p = malloc16(sizePss); /* pss in complex with alternatively i then q */
    if (p != NULL) {
      primary_synchro_nr[i] = p;
      bzero( primary_synchro_nr[i], sizePss);
    }
    else {
395 396
      LOG_E(PHY,"Fatal memory allocation problem \n");
      assert(0);
397
    }
Raymond Knopp's avatar
Raymond Knopp committed
398 399 400 401 402
    p = malloc(LENGTH_PSS_NR*2);
    if (p != NULL) {
      primary_synchro_nr2[i] = p;
      bzero( primary_synchro_nr2[i],LENGTH_PSS_NR*2);
    }
403 404 405 406 407 408
    p = malloc16(size);
    if (p != NULL) {
      primary_synchro_time_nr[i] = p;
      bzero( primary_synchro_time_nr[i], size);
    }
    else {
409
      LOG_E(PHY,"Fatal memory allocation problem \n");
410 411 412
     assert(0);
    }

413
    size = sizeof(int64_t)*(frame_parms_ue->samples_per_frame + (2*ofdm_symbol_size));
Raymond Knopp's avatar
Raymond Knopp committed
414
    q = (int64_t*)malloc16(size);
415 416 417 418 419
    if (q != NULL) {
      pss_corr_ue[i] = q;
      bzero( pss_corr_ue[i], size);
    }
    else {
420
      LOG_E(PHY,"Fatal memory allocation problem \n");
421 422 423
      assert(0);
    }

Raymond Knopp's avatar
Raymond Knopp committed
424
    generate_pss_nr(frame_parms_ue,i);
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  }
}

/*******************************************************************
*
* NAME :         free_context_pss_nr
*
* PARAMETERS :   none
*
* RETURN :       none
*
* DESCRIPTION :  free context related to pss
*
*********************************************************************/

void free_context_pss_nr(void)
{
  for (int i = 0; i < NUMBER_PSS_SEQUENCE; i++) {

    if (primary_synchro_time_nr[i] != NULL) {
      free(primary_synchro_time_nr[i]);
      primary_synchro_time_nr[i] = NULL;
    }
    else {
449
      LOG_E(PHY,"Fatal memory deallocation problem \n");
450 451 452 453 454 455 456 457
      assert(0);
    }

    if (primary_synchro_nr[i] != NULL) {
      free(primary_synchro_nr[i]);
      primary_synchro_nr[i] = NULL;
    }
    else {
458
      LOG_E(PHY,"Fatal memory deallocation problem \n");
459 460 461 462 463 464 465 466
      assert(0);
    }

    if (pss_corr_ue[i] != NULL) {
      free(pss_corr_ue[i]);
      pss_corr_ue[i] = NULL;
    }
    else {
467
      LOG_E(PHY,"Fatal memory deallocation problem \n");
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
      assert(0);
    }
  }
}

/*******************************************************************
*
* NAME :         init_context_synchro_nr
*
* PARAMETERS :   none
*
* RETURN :       generate context for pss and sss
*
* DESCRIPTION :  initialise contexts and buffers for synchronisation
*
*********************************************************************/

void init_context_synchro_nr(NR_DL_FRAME_PARMS *frame_parms_ue)
{
#ifndef STATIC_SYNC_BUFFER

  /* initialise global buffers for synchronisation */
  synchroF_tmp = malloc16(SYNCF_TMP_SIZE);
  if (synchroF_tmp == NULL) {
492
    LOG_E(PHY,"Fatal memory allocation problem \n");
493 494 495 496 497
    assert(0);
  }

  synchro_tmp = malloc16(SYNC_TMP_SIZE);
  if (synchro_tmp == NULL) {
498
    LOG_E(PHY,"Fatal memory allocation problem \n");
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    assert(0);
  }

#endif

  init_context_pss_nr(frame_parms_ue);

  init_context_sss_nr(AMP);
}

/*******************************************************************
*
* NAME :         free_context_synchro_nr
*
* PARAMETERS :   none
*
* RETURN :       free context for pss and sss
*
* DESCRIPTION :  deallocate memory of synchronisation
*
*********************************************************************/

void free_context_synchro_nr(void)
{
#ifndef STATIC_SYNC_BUFFER

  if (synchroF_tmp != NULL) {
    free(synchroF_tmp);
    synchroF_tmp = NULL;
  }
  else {
530
    LOG_E(PHY,"Fatal memory deallocation problem \n");
531 532 533 534 535 536 537 538
    assert(0);
  }

  if (synchro_tmp != NULL) {
    free(synchro_tmp);
    synchro_tmp = NULL;
  }
  else {
539
    LOG_E(PHY,"Fatal memory deallocation problem \n");
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 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
    assert(0);
  }

#endif

  free_context_pss_nr();
}

/*******************************************************************
*
* NAME :         set_frame_context_pss_nr
*
* PARAMETERS :   configuration for UE with new FFT size
*
* RETURN :       0 if OK else error
*
* DESCRIPTION :  initialisation of UE contexts
*
*********************************************************************/

void set_frame_context_pss_nr(NR_DL_FRAME_PARMS *frame_parms_ue, int rate_change)
{
  /* set new value according to rate_change */
  frame_parms_ue->ofdm_symbol_size = (frame_parms_ue->ofdm_symbol_size / rate_change);
  frame_parms_ue->samples_per_tti = (frame_parms_ue->samples_per_tti / rate_change);
  frame_parms_ue->samples_per_subframe = (frame_parms_ue->samples_per_subframe / rate_change);

  free_context_pss_nr();

  /* pss reference have to be rebuild with new parameters ie ofdm symbol size */
  init_context_synchro_nr(frame_parms_ue);

#ifdef SYNCHRO_DECIMAT
  set_pss_nr(frame_parms_ue->ofdm_symbol_size);
#endif
}

/*******************************************************************
*
* NAME :         restore_frame_context_pss_nr
*
* PARAMETERS :   configuration for UE and eNB with new FFT size
*
* RETURN :       0 if OK else error
*
* DESCRIPTION :  initialisation of UE and eNode contexts
*
*********************************************************************/

void restore_frame_context_pss_nr(NR_DL_FRAME_PARMS *frame_parms_ue, int rate_change)
{
  frame_parms_ue->ofdm_symbol_size = frame_parms_ue->ofdm_symbol_size * rate_change;
  frame_parms_ue->samples_per_tti = frame_parms_ue->samples_per_tti * rate_change;
  frame_parms_ue->samples_per_subframe = frame_parms_ue->samples_per_subframe * rate_change;

  free_context_pss_nr();

  /* pss reference have to be rebuild with new parameters ie ofdm symbol size */
  init_context_synchro_nr(frame_parms_ue);
#ifdef SYNCHRO_DECIMAT
  set_pss_nr(frame_parms_ue->ofdm_symbol_size);
#endif
}

/********************************************************************
*
* NAME :         decimation_synchro_nr
*
* INPUT :        UE context
*                for first and second pss sequence
*                - position of pss in the received UE buffer
*                - number of pss sequence
*
* RETURN :      0 if OK else error
*
* DESCRIPTION :  detect pss sequences in the received UE buffer
*
********************************************************************/

619
void decimation_synchro_nr(PHY_VARS_NR_UE *PHY_vars_UE, int rate_change, int **rxdata)
620 621
{
  NR_DL_FRAME_PARMS *frame_parms = &(PHY_vars_UE->frame_parms);
622
  int samples_for_frame = 2*frame_parms->samples_per_frame;
623

Raymond Knopp's avatar
Raymond Knopp committed
624 625
  AssertFatal(frame_parms->samples_per_tti > 3839,"Illegal samples_per_tti %d\n",frame_parms->samples_per_tti);

626 627 628 629 630 631 632 633 634 635 636
#if TEST_SYNCHRO_TIMING_PSS

  opp_enabled = 1;

  start_meas(&generic_time[TIME_RATE_CHANGE]);

#endif

/* build with cic filter does not work properly. Performances are significantly deteriorated */
#ifdef CIC_DECIMATOR

637
  cic_decimator((int16_t *)&(PHY_vars_UE->common_vars.rxdata[0][0]), (int16_t *)&(rxdata[0][0]),
638 639 640
                            samples_for_frame, rate_change, CIC_FILTER_STAGE_NUMBER, 0, FIR_RATE_CHANGE);
#else

641
  fir_decimator((int16_t *)&(PHY_vars_UE->common_vars.rxdata[0][0]), (int16_t *)&(rxdata[0][0]),
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
                            samples_for_frame, rate_change, 0);

#endif

  set_frame_context_pss_nr(frame_parms, rate_change);

#if TEST_SYNCHRO_TIMING_PSS

  stop_meas(&generic_time[TIME_RATE_CHANGE]);

  printf("Rate change execution duration %5.2f \n", generic_time[TIME_RATE_CHANGE].p_time/(cpuf*1000.0));

#endif
}

/*******************************************************************
*
* NAME :         pss_synchro_nr
*
* PARAMETERS :   int rate_change
*
* RETURN :       position of detected pss
*
* DESCRIPTION :  pss search can be done with sampling decimation.*
*
*********************************************************************/

669
int pss_synchro_nr(PHY_VARS_NR_UE *PHY_vars_UE, int is, int rate_change)
670 671 672
{
  NR_DL_FRAME_PARMS *frame_parms = &(PHY_vars_UE->frame_parms);
  int synchro_position;
673
  int **rxdata = NULL;
674
  int fo_flag = PHY_vars_UE->UE_fo_compensation;  // flag to enable freq offset estimation and compensation
675

Rakesh's avatar
Rakesh committed
676
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PSS_SYNCHRO_NR, VCD_FUNCTION_IN);
677
#ifdef DBG_PSS_NR
678

679
  LOG_M("rxdata0_rand.m","rxd0_rand", &PHY_vars_UE->common_vars.rxdata[0][0], frame_parms->samples_per_frame, 1, 1);
680 681

#endif
682

683
  if (rate_change != 1) {
684

685 686 687 688 689
    rxdata = (int32_t**)malloc16(frame_parms->nb_antennas_rx*sizeof(int32_t*));

    for (int aa=0; aa < frame_parms->nb_antennas_rx; aa++) {
      rxdata[aa] = (int32_t*) malloc16_clear( (frame_parms->samples_per_frame+8192)*sizeof(int32_t));
    }
690 691
#ifdef SYNCHRO_DECIMAT

692
    decimation_synchro_nr(PHY_vars_UE, rate_change, rxdata);
693 694 695 696 697

#endif
  }
  else {

698
    rxdata = PHY_vars_UE->common_vars.rxdata;
699 700 701 702
  }

#ifdef DBG_PSS_NR

703
  LOG_M("rxdata0_des.m","rxd0_des", &rxdata[0][0], frame_parms->samples_per_frame,1,1);
704 705 706 707 708 709 710 711 712 713 714

#endif

#if TEST_SYNCHRO_TIMING_PSS

  opp_enabled = 1;

  start_meas(&generic_time[TIME_PSS]);

#endif

Rakesh's avatar
Rakesh committed
715
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PSS_SEARCH_TIME_NR, VCD_FUNCTION_IN);
716 717
  synchro_position = pss_search_time_nr(rxdata,
                                        frame_parms,
718
					fo_flag,
719
                                        is,
720 721 722
                                        (int *)&PHY_vars_UE->common_vars.eNb_id,
					(int *)&PHY_vars_UE->common_vars.freq_offset);

Rakesh's avatar
Rakesh committed
723
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PSS_SEARCH_TIME_NR, VCD_FUNCTION_OUT);
724 725 726 727 728

#if TEST_SYNCHRO_TIMING_PSS

  stop_meas(&generic_time[TIME_PSS]);

729 730 731 732 733 734 735
  int duration_ms = generic_time[TIME_PSS].p_time/(cpuf*1000.0);

  #ifndef NR_UNIT_TEST

    printf("PSS execution duration %4d microseconds \n", duration_ms);

  #endif
736 737 738 739 740

#endif

#ifdef SYNCHRO_DECIMAT

741 742 743 744 745
  if (rate_change != 1) {

    if (rxdata[0] != NULL) {

      for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++) {
746
        free(rxdata[aa]);
747
      }
748 749

      free(rxdata);
750 751 752
    }

    restore_frame_context_pss_nr(frame_parms, rate_change);  
753
  }
754
#endif
755

Rakesh's avatar
Rakesh committed
756
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PSS_SYNCHRO_NR, VCD_FUNCTION_OUT);
757 758 759
  return synchro_position;
}

760

761 762 763 764 765
static inline int abs32(int x)
{
  return (((int)((short*)&x)[0])*((int)((short*)&x)[0]) + ((int)((short*)&x)[1])*((int)((short*)&x)[1]));
}

Raymond Knopp's avatar
Raymond Knopp committed
766 767 768 769 770
static inline int64_t abs64(int64_t x)
{
  return (((int64_t)((int32_t*)&x)[0])*((int64_t)((int32_t*)&x)[0]) + ((int64_t)((int32_t*)&x)[1])*((int64_t)((int32_t*)&x)[1]));
}

771 772 773 774 775 776 777 778 779
static inline double angle64(int64_t x)
{

  double re=((int32_t*)&x)[0];
  double im=((int32_t*)&x)[1];
  return (atan2(im,re));
}


780 781 782 783 784 785
/*******************************************************************
*
* NAME :         pss_search_time_nr
*
* PARAMETERS :   received buffer
*                frame parameters
Francesco Mani's avatar
Francesco Mani committed
786
*
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
* RETURN :       position of detected pss
*
* DESCRIPTION :  Synchronisation on pss sequence is based on a time domain correlation between received samples and pss sequence
*                A maximum likelihood detector finds the timing offset (position) that corresponds to the maximum correlation
*                Length of received buffer should be a minimum of 2 frames (see TS 38.213 4.1 Cell search)
*                Search pss in the received buffer is done each 4 samples which ensures a memory alignment to 128 bits (32 bits x 4).
*                This is required by SIMD (single instruction Multiple Data) Extensions of Intel processors
*                Correlation computation is based on a a dot product which is realized thank to SIMS extensions
*
*                                    (x frames)
*     <--------------------------------------------------------------------------->
*
*
*     -----------------------------------------------------------------------------
*     |                      Received UE data buffer                              |
*     ----------------------------------------------------------------------------
*                -------------
*     <--------->|    pss    |
*      position  -------------
*                ^
*                |
*            peak position
*            given by maximum of correlation result
*            position matches beginning of first ofdm symbol of pss sequence
*
*     Remark: memory position should be aligned on a multiple of 4 due to I & Q samples of int16
*             An OFDM symbol is composed of x number of received samples depending of Rf front end sample rate.
*
*     I & Q storage in memory
*
*             First samples       Second  samples
*     ------------------------- -------------------------  ...
*     |     I1     |     Q1    |     I2     |     Q2    |
*     ---------------------------------------------------  ...
*     ^    16  bits   16 bits  ^
*     |                        |
*     ---------------------------------------------------  ...
*     |         sample 1       |    sample   2          |
*    ----------------------------------------------------  ...
*     ^
*
*********************************************************************/

#define DOT_PRODUCT_SCALING_SHIFT    (17)

int pss_search_time_nr(int **rxdata, ///rx data in time domain
                       NR_DL_FRAME_PARMS *frame_parms,
834
		       int fo_flag,
835
                       int is,
836 837
                       int *eNB_id,
		       int *f_off)
838
{
Raymond Knopp's avatar
Raymond Knopp committed
839 840
  unsigned int n, ar, peak_position, pss_source;
  int64_t peak_value;
841
  int64_t result;
Raymond Knopp's avatar
Raymond Knopp committed
842
  int64_t avg[NUMBER_PSS_SEQUENCE];
843
  double ffo_est=0;
844

845 846 847 848 849 850 851
  // performing the correlation on a frame length plus one symbol for the first of the two frame
  // to take into account the possibility of PSS in between the two frames 
  unsigned int length;
  if (is==0)
    length = frame_parms->samples_per_frame + (2*frame_parms->ofdm_symbol_size);
  else
    length = frame_parms->samples_per_frame;
Raymond Knopp's avatar
Raymond Knopp committed
852 853

  AssertFatal(length>0,"illegal length %d\n",length);
Raymond Knopp's avatar
Raymond Knopp committed
854
  for (int i = 0; i < NUMBER_PSS_SEQUENCE; i++) AssertFatal(pss_corr_ue[i] != NULL,"pss_corr_ue[%d] not yet allocated! Exiting.\n", i);
855 856 857 858 859

  peak_value = 0;
  peak_position = 0;
  pss_source = 0;

Raymond Knopp's avatar
Raymond Knopp committed
860 861 862 863 864 865 866 867 868 869 870
  int maxval=0;
  for (int i=0;i<2*(frame_parms->ofdm_symbol_size);i++) {
    maxval = max(maxval,primary_synchro_time_nr[0][i]);
    maxval = max(maxval,-primary_synchro_time_nr[0][i]);
    maxval = max(maxval,primary_synchro_time_nr[1][i]);
    maxval = max(maxval,-primary_synchro_time_nr[1][i]);
    maxval = max(maxval,primary_synchro_time_nr[2][i]);
    maxval = max(maxval,-primary_synchro_time_nr[2][i]);
  }
  int shift = log2_approx(maxval);//*(frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples)*2);

871 872 873 874
  /* Search pss in the received buffer each 4 samples which ensures a memory alignment on 128 bits (32 bits x 4 ) */
  /* This is required by SIMD (single instruction Multiple Data) Extensions of Intel processors. */
  /* Correlation computation is based on a a dot product which is realized thank to SIMS extensions */

875 876 877 878
  for (int pss_index = 0; pss_index < NUMBER_PSS_SEQUENCE; pss_index++) {
    avg[pss_index]=0;
    memset(pss_corr_ue[pss_index],0,length*sizeof(int64_t)); 
  }
879

880
  for (n=0; n < length; n+=4) { //
881 882 883 884 885 886 887 888 889

    for (int pss_index = 0; pss_index < NUMBER_PSS_SEQUENCE; pss_index++) {

      if ( n < (length - frame_parms->ofdm_symbol_size)) {

        /* calculate dot product of primary_synchro_time_nr and rxdata[ar][n] (ar=0..nb_ant_rx) and store the sum in temp[n]; */
        for (ar=0; ar<frame_parms->nb_antennas_rx; ar++) {

          /* perform correlation of rx data and pss sequence ie it is a dot product */
Raymond Knopp's avatar
Raymond Knopp committed
890
          result  = dot_product64((short*)primary_synchro_time_nr[pss_index], 
891
				  (short*) &(rxdata[ar][n+is*frame_parms->samples_per_frame]), 
Raymond Knopp's avatar
Raymond Knopp committed
892 893 894 895 896
				  frame_parms->ofdm_symbol_size, 
				  shift);
	  pss_corr_ue[pss_index][n] += abs64(result);
          //((short*)pss_corr_ue[pss_index])[2*n] += ((short*) &result)[0];   /* real part */
          //((short*)pss_corr_ue[pss_index])[2*n+1] += ((short*) &result)[1]; /* imaginary part */
897 898
          //((short*)&synchro_out)[0] += ((int*) &result)[0];               /* real part */
          //((short*)&synchro_out)[1] += ((int*) &result)[1];               /* imaginary part */
899 900 901

        }
      }
902
 
903
      /* calculate the absolute value of sync_corr[n] */
Raymond Knopp's avatar
Raymond Knopp committed
904 905 906
      avg[pss_index]+=pss_corr_ue[pss_index][n];
      if (pss_corr_ue[pss_index][n] > peak_value) {
        peak_value = pss_corr_ue[pss_index][n];
907 908 909
        peak_position = n;
        pss_source = pss_index;

910
#ifdef DEBUG_PSS_NR
yilmazt's avatar
yilmazt committed
911
        printf("pss_index %d: n %6u peak_value %15llu\n", pss_index, n, (unsigned long long)pss_corr_ue[pss_index][n]);
912
#endif
913 914 915 916
      }
    }
  }

917
  if (fo_flag){
918

Francesco Mani's avatar
Francesco Mani committed
919
	  // fractional frequency offset computation according to Cross-correlation Synchronization Algorithm Using PSS
920
	  // Shoujun Huang, Yongtao Su, Ying He and Shan Tang, "Joint time and frequency offset estimation in LTE downlink," 7th International Conference on Communications and Networking in China, 2012.
921

922 923 924
	  int64_t result1,result2;
	  // Computing cross-correlation at peak on half the symbol size for first half of data
	  result1  = dot_product64((short*)primary_synchro_time_nr[pss_source], 
925
				  (short*) &(rxdata[0][peak_position+is*frame_parms->samples_per_frame]), 
926 927
				  frame_parms->ofdm_symbol_size>>1, 
				  shift);
928 929 930
	  // Computing cross-correlation at peak on half the symbol size for data shifted by half symbol size 
	  // as it is real and complex it is necessary to shift by a value equal to symbol size to obtain such shift
	  result2  = dot_product64((short*)primary_synchro_time_nr[pss_source]+(frame_parms->ofdm_symbol_size), 
931
				  (short*) &(rxdata[0][peak_position+is*frame_parms->samples_per_frame])+frame_parms->ofdm_symbol_size, 
932 933 934
				  frame_parms->ofdm_symbol_size>>1, 
				  shift);

935 936 937 938 939
	  int64_t re1,re2,im1,im2;
	  re1=((int*) &result1)[0];
	  re2=((int*) &result2)[0];
	  im1=((int*) &result1)[1];
	  im2=((int*) &result2)[1];
940

941 942
 	  // estimation of fractional frequency offset: angle[(result1)'*(result2)]/pi
	  ffo_est=atan2(re1*im2-re2*im1,re1*re2+im1*im2)/M_PI;
943

944
#ifdef DBG_PSS_NR
945
	  printf("ffo %lf\n",ffo_est);
946
#endif
947
  }
948 949 950 951

  // computing absolute value of frequency offset
  *f_off = ffo_est*frame_parms->subcarrier_spacing;  

Raymond Knopp's avatar
Raymond Knopp committed
952
  for (int pss_index = 0; pss_index < NUMBER_PSS_SEQUENCE; pss_index++) avg[pss_index]/=(length/4);
953 954 955

  *eNB_id = pss_source;

956
  LOG_I(PHY,"[UE] nr_synchro_time: Sync source = %d, Peak found at pos %d, val = %llu (%d dB) avg %d dB, ffo %lf\n", pss_source, peak_position, (unsigned long long)peak_value, dB_fixed64(peak_value),dB_fixed64(avg[pss_source]),ffo_est);
957

958
  if (peak_value < 5*avg[pss_source])
Raymond Knopp's avatar
Raymond Knopp committed
959
    return(-1);
960 961


962 963
#ifdef DBG_PSS_NR

964
  static int debug_cnt = 0;
965 966

  if (debug_cnt == 0) {
967
    LOG_M("pss_corr_ue0.m","pss_corr_ue0",pss_corr_ue[0],length,1,6);
Raymond Knopp's avatar
Raymond Knopp committed
968 969
    LOG_M("pss_corr_ue1.m","pss_corr_ue1",pss_corr_ue[1],length,1,6);
    LOG_M("pss_corr_ue2.m","pss_corr_ue2",pss_corr_ue[2],length,1,6);
970 971 972 973
    if (is)
      LOG_M("rxdata1.m","rxd0",rxdata[frame_parms->samples_per_frame],length,1,1); 
    else
      LOG_M("rxdata0.m","rxd0",rxdata[0],length,1,1);
974 975 976 977 978 979 980 981 982
  } else {
    debug_cnt++;
  }

#endif

  return(peak_position);
}