pcfich.c 9.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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.0  (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
 */

22 23 24 25 26 27 28 29 30 31 32 33
/*! \file PHY/LTE_TRANSPORT/pcfich.c
* \brief Top-level routines for generating and decoding  the PCFICH/CFI physical/transport channel V8.6 2009-03
* \author R. Knopp
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr
* \note
* \warning
*/
#include "PHY/defs.h"

34 35
//uint16_t pcfich_reg[4];
//uint8_t pcfich_first_reg_idx = 0;
36 37 38

//#define DEBUG_PCFICH

39 40
void generate_pcfich_reg_mapping(LTE_DL_FRAME_PARMS *frame_parms)
{
41

42 43 44
  uint16_t kbar = 6 * (frame_parms->Nid_cell %(2*frame_parms->N_RB_DL));
  uint16_t first_reg;
  uint16_t *pcfich_reg = frame_parms->pcfich_reg;
45

46 47 48 49 50 51
  pcfich_reg[0] = kbar/6;
  first_reg = pcfich_reg[0];

  frame_parms->pcfich_first_reg_idx=0;

  pcfich_reg[1] = ((kbar + (frame_parms->N_RB_DL>>1)*6)%(frame_parms->N_RB_DL*12))/6;
52

53 54 55
  if (pcfich_reg[1] < pcfich_reg[0]) {
    frame_parms->pcfich_first_reg_idx = 1;
    first_reg = pcfich_reg[1];
56 57
  }

58
  pcfich_reg[2] = ((kbar + (frame_parms->N_RB_DL)*6)%(frame_parms->N_RB_DL*12))/6;
59

60 61 62 63
  if (pcfich_reg[2] < first_reg) {
    frame_parms->pcfich_first_reg_idx = 2;
    first_reg = pcfich_reg[2];
  }
64

65
  pcfich_reg[3] = ((kbar + ((3*frame_parms->N_RB_DL)>>1)*6)%(frame_parms->N_RB_DL*12))/6;
66

67 68 69 70
  if (pcfich_reg[3] < first_reg) {
    frame_parms->pcfich_first_reg_idx = 3;
    first_reg = pcfich_reg[3];
  }
71

72 73 74
  //#ifdef DEBUG_PCFICH
  printf("pcfich_reg : %d,%d,%d,%d\n",pcfich_reg[0],pcfich_reg[1],pcfich_reg[2],pcfich_reg[3]);
  //#endif
75 76 77
}

void pcfich_scrambling(LTE_DL_FRAME_PARMS *frame_parms,
78 79 80 81
                       uint8_t subframe,
                       uint8_t *b,
                       uint8_t *bt)
{
82 83 84
  uint32_t i;
  uint8_t reset;
  uint32_t x1, x2, s=0;
85 86 87 88

  reset = 1;
  // x1 is set in lte_gold_generic
  x2 = ((((2*frame_parms->Nid_cell)+1)*(1+subframe))<<9) + frame_parms->Nid_cell; //this is c_init in 36.211 Sec 6.7.1
89

90 91 92 93 94 95 96 97 98 99 100 101 102
  for (i=0; i<32; i++) {
    if ((i&0x1f)==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      //printf("lte_gold[%d]=%x\n",i,s);
      reset = 0;
    }

    bt[i] = (b[i]&1) ^ ((s>>(i&0x1f))&1);
    //    printf("scrambling %d : b %d => bt %d, c %d\n",i,b[i],bt[i],((s>>(i&0x1f))&1));
  }
}

void pcfich_unscrambling(LTE_DL_FRAME_PARMS *frame_parms,
103 104 105
                         uint8_t subframe,
                         int16_t *d)
{
106

107 108 109
  uint32_t i;
  uint8_t reset;
  uint32_t x1, x2, s=0;
110 111 112 113 114 115 116 117 118 119 120 121

  reset = 1;
  // x1 is set in lte_gold_generic
  x2 = ((((2*frame_parms->Nid_cell)+1)*(1+subframe))<<9) + frame_parms->Nid_cell; //this is c_init in 36.211 Sec 6.7.1

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

122
    if (((s>>(i&0x1f))&1) == 1)
123 124 125 126 127 128
      d[i]=-d[i];

    //    printf("scrambling %d : b %d => bt %d, c %d\n",i,b[i],bt[i],((s>>(i&0x1f))&1));
  }
}

129 130 131 132 133
uint8_t pcfich_b[4][32]= {{0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1},
  {1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0},
  {1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1},
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
134

135
void generate_pcfich(uint8_t num_pdcch_symbols,
136 137
                     int16_t amp,
                     LTE_DL_FRAME_PARMS *frame_parms,
138
                     int32_t **txdataF,
139 140
                     uint8_t subframe)
{
141

142
  uint8_t pcfich_bt[32],nsymb,pcfich_quad;
143
  int32_t pcfich_d[2][16];
144 145 146 147
  uint8_t i;
  uint32_t symbol_offset,m,re_offset,reg_offset;
  int16_t gain_lin_QPSK;
  uint16_t *pcfich_reg = frame_parms->pcfich_reg;
148 149 150 151 152 153 154

  int nushiftmod3 = frame_parms->nushift%3;
#ifdef DEBUG_PCFICH
  msg("[PHY] Generating PCFICH for %d PDCCH symbols, AMP %d\n",num_pdcch_symbols,amp);
#endif

  // scrambling
155
  if ((num_pdcch_symbols>0) && (num_pdcch_symbols<4))
156 157 158
    pcfich_scrambling(frame_parms,subframe,pcfich_b[num_pdcch_symbols-1],pcfich_bt);

  // modulation
159 160
  if (frame_parms->mode1_flag==1)
    gain_lin_QPSK = (int16_t)((amp*ONE_OVER_SQRT2_Q15)>>15);
161
  else
162
    gain_lin_QPSK = amp/2;
163 164

  if (frame_parms->mode1_flag) { // SISO
Raymond Knopp's avatar
 
Raymond Knopp committed
165

166
    for (i=0; i<16; i++) {
167 168 169 170
      ((int16_t*)(&(pcfich_d[0][i])))[0]   = ((pcfich_bt[2*i] == 1) ? -gain_lin_QPSK : gain_lin_QPSK);
      ((int16_t*)(&(pcfich_d[1][i])))[0]   = ((pcfich_bt[2*i] == 1) ? -gain_lin_QPSK : gain_lin_QPSK);
      ((int16_t*)(&(pcfich_d[0][i])))[1]   = ((pcfich_bt[2*i+1] == 1) ? -gain_lin_QPSK : gain_lin_QPSK);
      ((int16_t*)(&(pcfich_d[1][i])))[1]   = ((pcfich_bt[2*i+1] == 1) ? -gain_lin_QPSK : gain_lin_QPSK);
171
    }
172 173
  } else { // ALAMOUTI
    for (i=0; i<16; i+=2) {
174
      // first antenna position n -> x0
175 176
      ((int16_t*)(&(pcfich_d[0][i])))[0]   = ((pcfich_bt[2*i] == 1) ? -gain_lin_QPSK : gain_lin_QPSK);
      ((int16_t*)(&(pcfich_d[0][i])))[1]   = ((pcfich_bt[2*i+1] == 1) ? -gain_lin_QPSK : gain_lin_QPSK);
177
      // second antenna position n -> -x1*
178 179
      ((int16_t*)(&(pcfich_d[1][i])))[0]   = ((pcfich_bt[2*i+2] == 1) ? gain_lin_QPSK : -gain_lin_QPSK);
      ((int16_t*)(&(pcfich_d[1][i])))[1]   = ((pcfich_bt[2*i+3] == 1) ? -gain_lin_QPSK : gain_lin_QPSK);
180
      // fill in the rest of the ALAMOUTI precoding
181 182 183 184
      ((int16_t*)&pcfich_d[0][i+1])[0] = -((int16_t*)&pcfich_d[1][i])[0];
      ((int16_t*)&pcfich_d[0][i+1])[1] =  ((int16_t*)&pcfich_d[1][i])[1];
      ((int16_t*)&pcfich_d[1][i+1])[0] =  ((int16_t*)&pcfich_d[0][i])[0];
      ((int16_t*)&pcfich_d[1][i+1])[1] = -((int16_t*)&pcfich_d[0][i])[1];
185 186 187


    }
188 189 190 191 192
  }


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

194
  symbol_offset = (uint32_t)frame_parms->ofdm_symbol_size*(subframe*nsymb);
195 196 197 198
  re_offset = frame_parms->first_carrier_offset;

  // loop over 4 quadruplets and lookup REGs
  m=0;
199 200

  for (pcfich_quad=0; pcfich_quad<4; pcfich_quad++) {
201
    reg_offset = re_offset+((uint16_t)pcfich_reg[pcfich_quad]*6);
202

203 204
    if (reg_offset>=frame_parms->ofdm_symbol_size)
      reg_offset=1 + reg_offset-frame_parms->ofdm_symbol_size;
205

206
    //    printf("mapping pcfich reg_offset %d\n",reg_offset);
207
    for (i=0; i<6; i++) {
208
      if ((i!=nushiftmod3)&&(i!=(nushiftmod3+3))) {
209 210
        txdataF[0][symbol_offset+reg_offset+i] = pcfich_d[0][m];

Xiwen JIANG's avatar
Xiwen JIANG committed
211
        if (frame_parms->nb_antenna_ports_eNB>1)
212 213 214
          txdataF[1][symbol_offset+reg_offset+i] = pcfich_d[1][m];

        m++;
215 216 217 218 219 220 221
      }
    }
  }

}


222
uint8_t rx_pcfich(LTE_DL_FRAME_PARMS *frame_parms,
223 224 225 226
                  uint8_t subframe,
                  LTE_UE_PDCCH *lte_ue_pdcch_vars,
                  MIMO_mode_t mimo_mode)
{
227

228 229 230
  uint8_t pcfich_quad;
  uint8_t i,j;
  uint16_t reg_offset;
231

232 233 234 235 236
  int32_t **rxdataF_comp = lte_ue_pdcch_vars->rxdataF_comp;
  int16_t pcfich_d[32],*pcfich_d_ptr;
  int32_t metric,old_metric=-16384;
  uint8_t num_pdcch_symbols=3;
  uint16_t *pcfich_reg = frame_parms->pcfich_reg;
237 238 239 240 241 242

  // demapping
  // loop over 4 quadruplets and lookup REGs
  //  m=0;
  pcfich_d_ptr = pcfich_d;

243
  for (pcfich_quad=0; pcfich_quad<4; pcfich_quad++) {
244 245 246
    reg_offset = (pcfich_reg[pcfich_quad]*4);

    //    if (frame_parms->mode1_flag==1) {  // SISO
247 248 249 250
    for (i=0; i<4; i++) {

      pcfich_d_ptr[0] = ((int16_t*)&rxdataF_comp[0][reg_offset+i])[0]; // RE component
      pcfich_d_ptr[1] = ((int16_t*)&rxdataF_comp[0][reg_offset+i])[1]; // IM component
251
      /*
252 253 254 255 256 257 258 259 260
          printf("rx_pcfich: quad %d, i %d, offset %d => m%d (%d,%d) => pcfich_d_ptr[0] %d \n",pcfich_quad,i,reg_offset+i,m,
             ((int16_t*)&rxdataF_comp[0][reg_offset+i])[0],
             ((int16_t*)&rxdataF_comp[0][reg_offset+i])[1],
             pcfich_d_ptr[0]);
      */
      pcfich_d_ptr+=2;
    }

    /*
261 262
    }
    else { // ALAMOUTI
263 264 265 266 267 268
    for (i=0;i<4;i+=2) {
    pcfich_d_ptr[0] = 0;
    pcfich_d_ptr[1] = 0;
    pcfich_d_ptr[2] = 0;
    pcfich_d_ptr[3] = 0;
    for (j=0;j<frame_parms->nb_antennas_rx;j++) {
269

270 271 272 273
    pcfich_d_ptr[0] += (((int16_t*)&rxdataF_comp[j][reg_offset+i])[0]+
         ((int16_t*)&rxdataF_comp[j+2][reg_offset+i+1])[0]); // RE component
    pcfich_d_ptr[1] += (((int16_t*)&rxdataF_comp[j][reg_offset+i])[1] -
         ((int16_t*)&rxdataF_comp[j+2][reg_offset+i+1])[1]);// IM component
274

275 276 277 278
    pcfich_d_ptr[2] += (((int16_t*)&rxdataF_comp[j][reg_offset+i+1])[0]-
         ((int16_t*)&rxdataF_comp[j+2][reg_offset+i])[0]); // RE component
    pcfich_d_ptr[3] += (((int16_t*)&rxdataF_comp[j][reg_offset+i+1])[1] +
         ((int16_t*)&rxdataF_comp[j+2][reg_offset+i])[1]);// IM component
279 280


281
    }
282

283 284 285 286
    pcfich_d_ptr+=4;

    }
    */
287 288 289 290 291 292 293 294
  }

  // pcfhich unscrambling

  pcfich_unscrambling(frame_parms,subframe,pcfich_d);

  // pcfich detection

295
  for (i=0; i<3; i++) {
296
    metric = 0;
297 298

    for (j=0; j<32; j++) {
299
      //printf("pcfich_b[%d][%d] %d => pcfich_d[%d] %d\n",i,j,pcfich_b[i][j],j,pcfich_d[j]);
300
      metric += (int32_t)(((pcfich_b[i][j]==0) ? (pcfich_d[j]) : (-pcfich_d[j])));
301
    }
302

303 304 305
#ifdef DEBUG_PCFICH
    msg("metric %d : %d\n",i,metric);
#endif
306

307 308 309 310 311 312 313 314 315 316 317
    if (metric > old_metric) {
      num_pdcch_symbols = 1+i;
      old_metric = metric;
    }
  }

#ifdef DEBUG_PCFICH
  msg("[PHY] PCFICH detected for %d PDCCH symbols\n",num_pdcch_symbols);
#endif
  return(num_pdcch_symbols);
}