sss.c 11.6 KB
Newer Older
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3
    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
    You should have received a copy of the GNU General Public License
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19
   see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
ghaddab's avatar
ghaddab committed
22 23
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

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

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

/*! \file PHY/LTE_TRANSPORT/sss.c
* \brief Top-level routines for generating and decoding the secondary synchronization signal (SSS) V8.6 2009-03
* \author R. Knopp
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr
* \note
* \warning
*/
#include "PHY/defs.h"
#include "defs.h"
#include "PHY/extern.h"

//#define DEBUG_SSS


47
int generate_sss(int32_t **txdataF,
48 49 50 51 52
                 int16_t amp,
                 LTE_DL_FRAME_PARMS *frame_parms,
                 uint16_t symbol,
                 uint16_t slot_offset)
{
53

54
  uint8_t i,aa,Nsymb;
55
  int16_t *d,k;
56 57
  uint8_t Nid2;
  uint16_t Nid1;
58
  int16_t a;
59 60 61 62

  Nid2 = frame_parms->Nid_cell % 3;
  Nid1 = frame_parms->Nid_cell/3;

63
  if (slot_offset < 3)
64 65 66 67
    d = &d0_sss[62*(Nid2 + (Nid1*3))];
  else
    d = &d5_sss[62*(Nid2 + (Nid1*3))];

68
  Nsymb = (frame_parms->Ncp==NORMAL)?14:12;
69
  k = frame_parms->ofdm_symbol_size-3*12+5;
70
  a = (frame_parms->nb_antennas_tx == 1) ? amp : (amp*ONE_OVER_SQRT2_Q15)>>15;
71 72 73 74

  for (i=0; i<62; i++) {
    for (aa=0; aa<frame_parms->nb_antennas_tx; aa++) {

75
      ((int16_t*)txdataF[aa])[2*(slot_offset*Nsymb/2*frame_parms->ofdm_symbol_size +
76 77
                                 symbol*frame_parms->ofdm_symbol_size + k)] =
                                   (a * d[i]);
78
      ((int16_t*)txdataF[aa])[2*(slot_offset*Nsymb/2*frame_parms->ofdm_symbol_size +
79
                                 symbol*frame_parms->ofdm_symbol_size + k)+1] = 0;
80
    }
81

82
    k+=1;
83

84 85 86 87 88
    if (k >= frame_parms->ofdm_symbol_size) {
      k++;
      k-=frame_parms->ofdm_symbol_size;
    }
  }
89

90 91 92 93
  return(0);
}

int pss_ch_est(PHY_VARS_UE *phy_vars_ue,
94 95 96
               int32_t pss_ext[4][72],
               int32_t sss_ext[4][72])
{
97

98 99
  int16_t *pss;
  int16_t *pss_ext2,*sss_ext2,*sss_ext3,tmp_re,tmp_im,tmp_re2,tmp_im2;
100
  uint8_t aarx,i;
101 102 103
  LTE_DL_FRAME_PARMS *frame_parms = &phy_vars_ue->lte_frame_parms;

  switch (phy_vars_ue->lte_ue_common_vars.eNb_id) {
104

105 106 107
  case 0:
    pss = &primary_synch0[10];
    break;
108

109 110 111
  case 1:
    pss = &primary_synch1[10];
    break;
112

113 114 115
  case 2:
    pss = &primary_synch2[10];
    break;
116

117 118 119 120 121
  default:
    pss = &primary_synch0[10];
    break;
  }

122
  sss_ext3 = (int16_t*)&sss_ext[0][5];
123 124

  for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
125

126 127
    sss_ext2 = (int16_t*)&sss_ext[aarx][5];
    pss_ext2 = (int16_t*)&pss_ext[aarx][5];
128 129

    for (i=0; i<62; i++) {
130 131

      // This is H*(PSS) = R* \cdot PSS
132 133
      tmp_re = (int16_t)(((pss_ext2[i<<1] * (int32_t)pss[i<<1])>>15)     + ((pss_ext2[1+(i<<1)] * (int32_t)pss[1+(i<<1)])>>15));
      tmp_im = (int16_t)(((pss_ext2[i<<1] * (int32_t)pss[1+(i<<1)])>>15) - ((pss_ext2[1+(i<<1)] * (int32_t)pss[(i<<1)])>>15));
134 135
      //      printf("H*(%d,%d) : (%d,%d)\n",aarx,i,tmp_re,tmp_im);
      // This is R(SSS) \cdot H*(PSS)
136
      tmp_re2 = (int16_t)(((tmp_re * (int32_t)sss_ext2[i<<1])>>15)     - ((tmp_im * (int32_t)sss_ext2[1+(i<<1)]>>15)));
137
      tmp_im2 = (int16_t)(((tmp_re * (int32_t)sss_ext2[1+(i<<1)])>>15) + ((tmp_im * (int32_t)sss_ext2[(i<<1)]>>15)));
138 139

      //      printf("SSSi(%d,%d) : (%d,%d)\n",aarx,i,sss_ext2[i<<1],sss_ext2[1+(i<<1)]);
140 141 142
      //      printf("SSSo(%d,%d) : (%d,%d)\n",aarx,i,tmp_re2,tmp_im2);
      // MRC on RX antennas
      if (aarx==0) {
143 144 145 146 147
        sss_ext3[i<<1]      = tmp_re2;
        sss_ext3[1+(i<<1)]  = tmp_im2;
      } else {
        sss_ext3[i<<1]      += tmp_re2;
        sss_ext3[1+(i<<1)]  += tmp_im2;
148 149 150
      }
    }
  }
151

152 153 154 155 156 157
  // sss_ext now contains the compensated SSS
  return(0);
}


int pss_sss_extract(PHY_VARS_UE *phy_vars_ue,
158 159 160 161 162
                    int32_t pss_ext[4][72],
                    int32_t sss_ext[4][72])
{


163

164 165 166 167
  uint16_t rb,nb_rb=6;
  uint8_t i,aarx;
  int32_t *pss_rxF,*pss_rxF_ext;
  int32_t *sss_rxF,*sss_rxF_ext;
168 169 170
  LTE_DL_FRAME_PARMS *frame_parms = &phy_vars_ue->lte_frame_parms;

  int rx_offset = frame_parms->ofdm_symbol_size-3*12;
171
  uint8_t pss_symb,sss_symb;
172

173
  int32_t **rxdataF =  phy_vars_ue->lte_ue_common_vars.rxdataF;
174

175
  if (frame_parms->frame_type == FDD) {
176 177
    pss_symb = 6-frame_parms->Ncp;
    sss_symb = pss_symb-1;
178
  } else {
179 180 181 182
    pss_symb = 2;
    sss_symb = frame_parms->symbols_per_tti-1;
  }

183 184
  for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {

185 186 187 188 189 190 191 192 193 194 195 196
    //printf("extract_rbs: symbol_mod=%d, rx_offset=%d, ch_offset=%d\n",symbol_mod,
    //   (rx_offset + (symbol*(frame_parms->ofdm_symbol_size)))*2,
    //   LTE_CE_OFFSET+ch_offset+(symbol_mod*(frame_parms->ofdm_symbol_size)));

    pss_rxF        = &rxdataF[aarx][(rx_offset + (pss_symb*(frame_parms->ofdm_symbol_size)))];
    sss_rxF        = &rxdataF[aarx][(rx_offset + (sss_symb*(frame_parms->ofdm_symbol_size)))];
    pss_rxF_ext    = &pss_ext[aarx][0];
    sss_rxF_ext    = &sss_ext[aarx][0];

    for (rb=0; rb<nb_rb; rb++) {
      // skip DC carrier
      if (rb==3) {
197 198
        sss_rxF       = &rxdataF[aarx][(1 + (sss_symb*(frame_parms->ofdm_symbol_size)))];
        pss_rxF       = &rxdataF[aarx][(1 + (pss_symb*(frame_parms->ofdm_symbol_size)))];
199
      }
200 201 202 203

      for (i=0; i<12; i++) {
        pss_rxF_ext[i]=pss_rxF[i];
        sss_rxF_ext[i]=sss_rxF[i];
204
      }
205

206 207 208 209 210 211 212
      pss_rxF+=12;
      sss_rxF+=12;
      pss_rxF_ext+=12;
      sss_rxF_ext+=12;
    }

  }
213

214 215 216 217
  return(0);
}


218 219
int16_t phase_re[7] = {16383, 25101, 30791, 32767, 30791, 25101, 16383};
int16_t phase_im[7] = {-28378, -21063, -11208, 0, 11207, 21062, 28377};
220 221


222 223 224
int rx_sss(PHY_VARS_UE *phy_vars_ue,int32_t *tot_metric,uint8_t *flip_max,uint8_t *phase_max)
{

225 226 227 228 229 230 231
  uint8_t i;
  int32_t pss_ext[4][72];
  int32_t sss0_ext[4][72],sss5_ext[4][72];
  uint8_t Nid2 = phy_vars_ue->lte_ue_common_vars.eNb_id;
  uint8_t flip,phase;
  uint16_t Nid1;
  int16_t *sss0,*sss5;
232
  LTE_DL_FRAME_PARMS *frame_parms=&phy_vars_ue->lte_frame_parms;
233 234
  int32_t metric;
  int16_t *d0,*d5;
235

236
  if (phy_vars_ue->lte_frame_parms.frame_type == FDD) {
237
#ifdef DEBUG_SSS
238

239
    if (phy_vars_ue->lte_frame_parms.Ncp == NORMAL)
240 241 242
      msg("[PHY][UE%d] Doing SSS for FDD Normal Prefix\n",phy_vars_ue->Mod_id);
    else
      msg("[PHY][UE%d] Doing SSS for FDD Extended Prefix\n",phy_vars_ue->Mod_id);
243

244 245 246 247
#endif
    // Do FFTs for SSS/PSS
    // SSS
    slot_fep(phy_vars_ue,
248 249 250
             (frame_parms->symbols_per_tti/2)-2, // second to last symbol of
             0,                                  // slot 0
             phy_vars_ue->rx_offset,
251 252
             0,
	     1);
253 254
    // PSS
    slot_fep(phy_vars_ue,
255 256 257
             (frame_parms->symbols_per_tti/2)-1, // last symbol of
             0,                                  // slot 0
             phy_vars_ue->rx_offset,
258 259
             0,
	     1);
260
  } else { // TDD
261
#ifdef DEBUG_SSS
262
    if (phy_vars_ue->lte_frame_parms.Ncp == NORMAL)
263 264 265
      msg("[PHY][UE%d] Doing SSS for TDD Normal Prefix\n",phy_vars_ue->Mod_id);
    else
      msg("[PHY][UE%d] Doing SSS for TDD Extended Prefix\n",phy_vars_ue->Mod_id);
266

267 268 269
#endif
    // SSS
    slot_fep(phy_vars_ue,
270 271 272
             (frame_parms->symbols_per_tti>>1)-1,  // last symbol of
             1,                                    // slot 1
             phy_vars_ue->rx_offset,
273 274
             0,
	     1);
275 276
    // PSS
    slot_fep(phy_vars_ue,
277 278 279
             2,                                   // symbol 2 of
             2,                                   // slot 2
             phy_vars_ue->rx_offset,
280 281
             0,
	     1);
282 283 284
  }

  pss_sss_extract(phy_vars_ue,
285 286 287
                  pss_ext,
                  sss0_ext);
  /*
288 289 290 291 292 293
  write_output("rxsig0.m","rxs0",&phy_vars_ue->lte_ue_common_vars.rxdata[0][0],phy_vars_ue->lte_frame_parms.samples_per_tti,1,1);
  write_output("rxdataF0.m","rxF0",&phy_vars_ue->lte_ue_common_vars.rxdataF[0][0],2*14*phy_vars_ue->lte_frame_parms.ofdm_symbol_size,2,1);
  write_output("pss_ext0.m","pssext0",pss_ext,72,1,1);
  write_output("sss0_ext0.m","sss0ext0",sss0_ext,72,1,1);
  */

294 295
  // get conjugated channel estimate from PSS (symbol 6), H* = R* \cdot PSS
  // and do channel estimation and compensation based on PSS
296 297

  pss_ch_est(phy_vars_ue,
298 299 300
             pss_ext,
             sss0_ext);

301 302
  //  write_output("sss0_comp0.m","sss0comp0",sss0_ext,72,1,1);

303
  if (phy_vars_ue->lte_frame_parms.frame_type == FDD) { // FDD
304 305 306

    // SSS
    slot_fep(phy_vars_ue,
307 308 309
             (frame_parms->symbols_per_tti/2)-2,
             10,
             phy_vars_ue->rx_offset,
310
             0,1);
311 312
    // PSS
    slot_fep(phy_vars_ue,
313 314 315
             (frame_parms->symbols_per_tti/2)-1,
             10,
             phy_vars_ue->rx_offset,
316
             0,1);
317
  } else { // TDD
318 319
    // SSS
    slot_fep(phy_vars_ue,
320 321 322
             (frame_parms->symbols_per_tti>>1)-1,
             11,
             phy_vars_ue->rx_offset,
323 324
             0,
	     1);
325 326
    // PSS
    slot_fep(phy_vars_ue,
327 328 329
             2,
             12,
             phy_vars_ue->rx_offset,
330 331
             0,
	     1);
332
  }
333

334
  pss_sss_extract(phy_vars_ue,
335 336
                  pss_ext,
                  sss5_ext);
337 338 339 340

  //  write_output("sss5_ext0.m","sss5ext0",sss5_ext,72,1,1);
  // get conjugated channel estimate from PSS (symbol 6), H* = R* \cdot PSS
  // and do channel estimation and compensation based on PSS
341

342
  pss_ch_est(phy_vars_ue,
343 344 345 346 347
             pss_ext,
             sss5_ext);



348
  // now do the SSS detection based on the precomputed sequences in PHY/LTE_TRANSPORT/sss.h
349

350
  *tot_metric = -99999999;
351 352


353 354
  sss0 = (int16_t*)&sss0_ext[0][5];
  sss5 = (int16_t*)&sss5_ext[0][5];
355 356 357

  for (flip=0; flip<2; flip++) {      //  d0/d5 flip in RX frame
    for (phase=0; phase<7; phase++) { // phase offset between PSS and SSS
358
      for (Nid1 = 0 ; Nid1 <= 167; Nid1++) {  // 168 possible Nid1 values
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        metric = 0;

        if (flip==0) {
          d0 = &d0_sss[62*(Nid2 + (Nid1*3))];
          d5 = &d5_sss[62*(Nid2 + (Nid1*3))];
        } else {
          d5 = &d0_sss[62*(Nid2 + (Nid1*3))];
          d0 = &d5_sss[62*(Nid2 + (Nid1*3))];
        }

        // This is the inner product using one particular value of each unknown parameter
        for (i=0; i<62; i++) {
          metric += (int16_t)(((d0[i]*((((phase_re[phase]*(int32_t)sss0[i<<1])>>19)-((phase_im[phase]*(int32_t)sss0[1+(i<<1)])>>19)))) +
                               (d5[i]*((((phase_re[phase]*(int32_t)sss5[i<<1])>>19)-((phase_im[phase]*(int32_t)sss5[1+(i<<1)])>>19))))));
        }

        // if the current metric is better than the last save it
        if (metric > *tot_metric) {
          *tot_metric = metric;
          phy_vars_ue->lte_frame_parms.Nid_cell = Nid2+(3*Nid1);
          *phase_max = phase;
          *flip_max=flip;
381
#ifdef DEBUG_SSS
382
          msg("(flip,phase,Nid1) (%d,%d,%d), metric_phase %d tot_metric %d, phase_max %d, flip_max %d\n",flip,phase,Nid1,metric,*tot_metric,*phase_max,*flip_max);
383
#endif
384 385

        }
386
      }
387
    }
388 389 390 391 392
  }

  return(0);
}