pucch.c 66.8 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

22 23 24 25 26 27 28 29 30 31
/*! \file PHY/LTE_TRANSPORT/pucch.c
* \brief Top-level routines for generating and decoding the PUCCH physical channel V8.6 2009-03
* \author R. Knopp
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr
* \note
* \warning
*/
32
#include "PHY/defs_eNB.h"
33
#include "PHY/phy_extern.h"
34
#include "LAYER2/MAC/mac.h"
35
#include "PHY/LTE_REFSIG/lte_refsig.h"
36

37 38
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
39
#include "executables/softmodem-common.h"
40

41
//uint8_t ncs_cell[20][7];
42
//#define DEBUG_PUCCH_TXS
43
//#define DEBUG_PUCCH_RX
44

45
#include "pucch_extern.h"
46

47
int16_t cfo_pucch_np[24*7] = {20787,-25330,27244,-18205,31356,-9512,32767,0,31356,9511,27244,18204,20787,25329,
48 49 50 51 52 53 54
                              27244,-18205,30272,-12540,32137,-6393,32767,0,32137,6392,30272,12539,27244,18204,
                              31356,-9512,32137,-6393,32609,-3212,32767,0,32609,3211,32137,6392,31356,9511,
                              32767,0,32767,0,32767,0,32767,0,32767,0,32767,0,32767,0,
                              31356,9511,32137,6392,32609,3211,32767,0,32609,-3212,32137,-6393,31356,-9512,
                              27244,18204,30272,12539,32137,6392,32767,0,32137,-6393,30272,-12540,27244,-18205,
                              20787,25329,27244,18204,31356,9511,32767,0,31356,-9512,27244,-18205,20787,-25330
                             };
55

56
int16_t cfo_pucch_ep[24*6] = {24278,-22005,29621,-14010,32412,-4808,32412,4807,29621,14009,24278,22004,
57 58 59 60 61 62 63
                              28897,-15447,31356,-9512,32609,-3212,32609,3211,31356,9511,28897,15446,
                              31785,-7962,32412,-4808,32727,-1608,32727,1607,32412,4807,31785,7961,
                              32767,0,32767,0,32767,0,32767,0,32767,0,32767,0,
                              31785,7961,32412,4807,32727,1607,32727,-1608,32412,-4808,31785,-7962,
                              28897,15446,31356,9511,32609,3211,32609,-3212,31356,-9512,28897,-15447,
                              24278,22004,29621,14009,32412,4807,32412,-4808,29621,-14010,24278,-22005
                             };
64 65


Raymond Knopp's avatar
Raymond Knopp committed
66 67


68
void dump_uci_stats(FILE *fd,PHY_VARS_eNB *eNB,int frame) {
69

70 71 72 73
  int strpos=0;
  char output[16384];

  for (int i=0;i<NUMBER_OF_SCH_STATS_MAX;i++){
74 75
    if (eNB->uci_stats[i].rnti>0) {
      eNB_UCI_STATS_t *uci_stats = &eNB->uci_stats[i];
Raymond Knopp's avatar
Raymond Knopp committed
76 77 78 79
      strpos+=sprintf(output+strpos,"UCI %d RNTI %x: pucch1_trials %d, pucch1_n0 %d dB, pucch1_thres %d dB, current pucch1_stat_pos %d dB, current pucch1_stat_neg %d dB, positive SR count %d\n",
	i,uci_stats->rnti,uci_stats->pucch1_trials,eNB->measurements.n0_pucch_dB/*max(eNB->measurements.n0_subband_power_tot_dB[0], eNB->measurements.n0_subband_power_tot_dB[eNB->frame_parms.N_RB_UL-1])*/,uci_stats->pucch1_thres,dB_fixed(uci_stats->current_pucch1_stat_pos),dB_fixed(uci_stats->current_pucch1_stat_neg),uci_stats->pucch1_positive_SR);
      strpos+=sprintf(output+strpos,"UCI %d RNTI %x: pucch1_low (%d,%d)dB pucch1_high (%d,%d)dB\n",
	    i,uci_stats->rnti,
80 81 82 83 84
            dB_fixed(uci_stats->pucch1_low_stat[0]),
            dB_fixed(uci_stats->pucch1_low_stat[1]),
            dB_fixed(uci_stats->pucch1_high_stat[0]),
            dB_fixed(uci_stats->pucch1_high_stat[1]));
      
Raymond Knopp's avatar
Raymond Knopp committed
85 86
      strpos+=sprintf(output+strpos,"UCI %d RNTI %x: pucch1a_trials %d, pucch1a_stat (%d,%d), pucch1b_trials %d, pucch1b_stat (%d,%d) pucch1ab_DTX %d\n",
            i,uci_stats->rnti,
87 88 89
            uci_stats->pucch1a_trials,
            uci_stats->current_pucch1a_stat_re,
            uci_stats->current_pucch1a_stat_im,
Raymond Knopp's avatar
Raymond Knopp committed
90 91 92
            uci_stats->pucch1b_trials,
	    uci_stats->current_pucch1b_stat_re,
	    uci_stats->current_pucch1b_stat_im,
93 94
            uci_stats->pucch1ab_DTX);
    }
95 96 97
  }
  if (fd) fprintf(fd,"%s",output);
  else    printf("%s",output);  
98
}
Yoshi's avatar
Yoshi committed
99 100 101
/* PUCCH format3 >> */
/* SubCarrier Demap */
uint16_t pucchfmt3_subCarrierDeMapping( PHY_VARS_eNB *eNB,
102
                                        int16_t SubCarrierDeMapData[4][14][12][2],
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
                                        uint16_t n3_pucch ) {
  LTE_eNB_COMMON *eNB_common_vars  = &eNB->common_vars;
  LTE_DL_FRAME_PARMS  *frame_parms = &eNB->frame_parms;
  int16_t             *rxptr;
  uint8_t             N_UL_symb    = D_NSYM1SLT;                      // only Normal CP format
  uint16_t            m;                                              // Mapping to physical resource blocks(m)
  uint32_t            aa;
  uint16_t            k, l;
  uint32_t            symbol_offset;
  uint16_t            carrier_offset;
  m = n3_pucch / D_NPUCCH_SF5;

  // Do detection
  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    for (l=0; l<D_NSYM1SF; l++) {
      if ((l<N_UL_symb) && ((m&1) == 0))
        carrier_offset = (m*6) + frame_parms->first_carrier_offset;
      else if ((l<N_UL_symb) && ((m&1) == 1))
        carrier_offset = frame_parms->first_carrier_offset + (frame_parms->N_RB_DL - (m>>1) - 1)*12;
      else if ((m&1) == 0)
        carrier_offset = frame_parms->first_carrier_offset + (frame_parms->N_RB_DL - (m>>1) - 1)*12;
      else
        carrier_offset = (((m-1)*6) + frame_parms->first_carrier_offset);

      if (carrier_offset > frame_parms->ofdm_symbol_size)
        carrier_offset -= (frame_parms->ofdm_symbol_size);

      symbol_offset = (unsigned int)frame_parms->ofdm_symbol_size*l;
      rxptr = (int16_t *)&eNB_common_vars->rxdataF[aa][symbol_offset];

      for (k=0; k<12; k++,carrier_offset++) {
        SubCarrierDeMapData[aa][l][k][0] = (int16_t)rxptr[carrier_offset<<1];      // DeMapping Data I
        SubCarrierDeMapData[aa][l][k][1] = (int16_t)rxptr[1+(carrier_offset<<1)];  // DeMapping Date Q

        if (carrier_offset==frame_parms->ofdm_symbol_size)
          carrier_offset = 0;

        /*                #ifdef DEBUG_PUCCH_RX
                        LOG_D(PHY,"[eNB] PUCCH subframe %d (%d,%d,%d,%d) : (%d,%d)\n",subframe,l,k,carrier_offset,m,
                        SubCarrierDeMapData[aa][l][k][0],SubCarrierDeMapData[aa][l][k][1]);
            #endif*/
      }
Yoshi's avatar
Yoshi committed
145
    }
146 147 148
  }

  return 0;
Yoshi's avatar
Yoshi committed
149 150 151
}

/* cyclic shift hopping remove */
152 153
uint16_t pucchfmt3_Baseseq_csh_remove( int16_t SubCarrierDeMapData[4][14][12][2],
                                       int16_t CshData_fmt3[4][14][12][2],
Yoshi's avatar
Yoshi committed
154 155
                                       LTE_DL_FRAME_PARMS *frame_parms,
                                       uint8_t subframe,
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
                                       uint8_t ncs_cell[20][7] ) {
  //int16_t     calctmp_baSeq[2];
  int16_t     calctmp_beta[2];
  int16_t     calctmp_alphak[2];
  int16_t     calctmp_SCDeMapData_alphak[2];
  int32_t     n_cell_cs_div64;
  int32_t     n_cell_cs_modNSC_RB;
  //int32_t     NSlot1subframe  = D_NSLT1SF;
  int32_t     NSym1slot       = D_NSYM1SLT; // Symbol per 1slot
  int32_t     NSym1subframe   = D_NSYM1SF;  // Symbol per 1subframe
  int32_t     aa, symNo, slotNo, sym, k;

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {  // Antenna
    for (symNo=0; symNo<NSym1subframe; symNo++) {   // Symbol
      slotNo = symNo / NSym1slot;
      sym = symNo % NSym1slot;
      n_cell_cs_div64 = (int32_t)(ncs_cell[2*subframe+slotNo][sym]/64.0);
      n_cell_cs_modNSC_RB = ncs_cell[2*subframe+slotNo][sym] % 12;
      // for canceling e^(j*PI|_n_cs^cell(ns,l)/64_|/2).
      calctmp_beta[0] = RotTBL_re[(n_cell_cs_div64)&0x3];
      calctmp_beta[1] = RotTBL_im[(n_cell_cs_div64)&0x3];

      for (k=0; k<12; k++) {  // Sub Carrier
        // for canceling being cyclically shifted"(i+n_cs^cell(ns,l))".
        // e^((j*2PI(n_cs^cell(ns,l) mod N_SC)/N_SC)*k).
        calctmp_alphak[0] = alphaTBL_re[((n_cell_cs_modNSC_RB)*k)%12];
        calctmp_alphak[1] = alphaTBL_im[((n_cell_cs_modNSC_RB)*k)%12];
        // e^(-alphar*k)*r_l,m,n,k
        calctmp_SCDeMapData_alphak[0] = (((int32_t)SubCarrierDeMapData[aa][symNo][k][0] * calctmp_alphak[0] + (int32_t)SubCarrierDeMapData[aa][symNo][k][1] * calctmp_alphak[1])>>15);
        calctmp_SCDeMapData_alphak[1] = (((int32_t)SubCarrierDeMapData[aa][symNo][k][1] * calctmp_alphak[0] - (int32_t)SubCarrierDeMapData[aa][symNo][k][0] * calctmp_alphak[1])>>15);
        // (e^(-alphar*k)*r_l,m,n,k) * e^(-beta)
        CshData_fmt3[aa][symNo][k][0] = (((int32_t)calctmp_SCDeMapData_alphak[0] * calctmp_beta[0] + (int32_t)calctmp_SCDeMapData_alphak[1] * calctmp_beta[1])>>15);
        CshData_fmt3[aa][symNo][k][1] = (((int32_t)calctmp_SCDeMapData_alphak[1] * calctmp_beta[0] - (int32_t)calctmp_SCDeMapData_alphak[0] * calctmp_beta[1])>>15);
      }
Yoshi's avatar
Yoshi committed
190
    }
191 192 193
  }

  return 0;
Yoshi's avatar
Yoshi committed
194 195 196 197 198 199 200 201 202
}

#define MAXROW_TBL_SF5_OS_IDX    (5)    // Orthogonal sequence index
const int16_t TBL_3_SF5_GEN_N_DASH_NS[MAXROW_TBL_SF5_OS_IDX] = {0,3,6,8,10};

#define MAXROW_TBL_SF4_OS_IDX    (4)    // Orthogonal sequence index
const int16_t TBL_3_SF4_GEN_N_DASH_NS[MAXROW_TBL_SF4_OS_IDX] = {0,3,6,9};

/* Channel estimation */
203 204 205
uint16_t pucchfmt3_ChannelEstimation( int16_t SubCarrierDeMapData[4][14][12][2],
                                      double delta_theta[4][12],
                                      int16_t ChestValue[4][2][12][2],
Yoshi's avatar
Yoshi committed
206 207 208 209 210 211
                                      int16_t *Interpw,
                                      uint8_t subframe,
                                      uint8_t shortened_format,
                                      LTE_DL_FRAME_PARMS *frame_parms,
                                      uint16_t n3_pucch,
                                      uint16_t n3_pucch_array[NUMBER_OF_UE_MAX],
212 213
                                      uint8_t ncs_cell[20][7] ) {
  uint32_t        aa, symNo, k, slotNo, sym, i, j;
214
  int16_t         np, np_n, ip_ind=-1;
215 216
  //int16_t         npucch_sf;
  int16_t         calctmp[2];
217 218 219 220 221 222
  int16_t         BsCshData[4][D_NSYM1SF][D_NSC1RB][2];
  //int16_t         delta_theta_calctmp[4][4][D_NSC1RB][2], delta_theta_comp[4][D_NSC1RB][2];
  int16_t         delta_theta_comp[4][D_NSC1RB][2];
  int16_t         CsData_allavg[4][14][2];
  int16_t         CsData_temp[4][D_NSYM1SF][D_NSC1RB][2];
  int32_t         IP_CsData_allsfavg[4][14][4][2];
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
  int32_t         IP_allavg[D_NPUCCH_SF5];
  //int16_t         temp_ch[2];
  int16_t         m[NUMBER_OF_UE_MAX], m_self=0, same_m_number;
  uint16_t        n3_pucch_sameRB[NUMBER_OF_UE_MAX];
  int16_t         n_oc0[NUMBER_OF_UE_MAX];
  int16_t         n_oc1[NUMBER_OF_UE_MAX];
  int16_t         np_n_array[2][NUMBER_OF_UE_MAX]; //Cyclic shift
  uint8_t N_PUCCH_SF0 = 5;
  uint8_t N_PUCCH_SF1 = (shortened_format==0)? 5:4;
  uint32_t u0 = (frame_parms->Nid_cell + frame_parms->pusch_config_common.ul_ReferenceSignalsPUSCH.grouphop[subframe<<1]) % 30;
  uint32_t u1 = (frame_parms->Nid_cell + frame_parms->pusch_config_common.ul_ReferenceSignalsPUSCH.grouphop[1+(subframe<<1)]) % 30;
  uint32_t v0=frame_parms->pusch_config_common.ul_ReferenceSignalsPUSCH.seqhop[subframe<<1];
  uint32_t v1=frame_parms->pusch_config_common.ul_ReferenceSignalsPUSCH.seqhop[1+(subframe<<1)];
  uint32_t u=u0;
  uint32_t v=v0;

  //double d_theta[32]={0.0};
  //int32_t temp_theta[32][2]={0};

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    for (symNo=0; symNo<D_NSYM1SF; symNo++) {
      for(ip_ind=0; ip_ind<D_NPUCCH_SF5-1; ip_ind++) {
        IP_CsData_allsfavg[aa][symNo][ip_ind][0] = 0;
        IP_CsData_allsfavg[aa][symNo][ip_ind][1] = 0;
      }
Yoshi's avatar
Yoshi committed
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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 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
  }

  // compute m[], m_self
  for(i=0; i<NUMBER_OF_UE_MAX; i++) {
    m[i] = n3_pucch_array[i] / N_PUCCH_SF0; // N_PUCCH_SF0 = 5

    if(n3_pucch_array[i] == n3_pucch) {
      m_self = i;
    }
  }

  for(i=0; i<NUMBER_OF_UE_MAX; i++) {
    //printf("n3_pucch_array[%d]=%d, m[%d]=%d \n", i, n3_pucch_array[i], i, m[i]);
  }

  //printf("m_self=%d \n", m_self);

  // compute n3_pucch_sameRB[] // Not 4 not be equally divided
  for(i=0, same_m_number=0; i<NUMBER_OF_UE_MAX; i++) {
    if(m[i] == m[m_self]) {
      n3_pucch_sameRB[same_m_number] = n3_pucch_array[i];
      same_m_number++;
    }
  }

  //printf("same_m_number = %d \n", same_m_number);
  for(i=0; i<same_m_number; i++) {
    //printf("n3_pucch_sameRB[%d]=%d \n", i, n3_pucch_sameRB[i]);
  }

  // compute n_oc1[], n_oc0[]
  for(i=0; i<same_m_number; i++) {
    n_oc0[i] = n3_pucch_sameRB[i] % N_PUCCH_SF1; //N_PUCCH_SF1 = (shortened_format==0)? 5:4;

    if (N_PUCCH_SF1 == 5) {
      n_oc1[i] = (3 * n_oc0[i]) % N_PUCCH_SF1;
    } else {
      n_oc1[i] = n_oc0[i] % N_PUCCH_SF1;
    }
  }

  for(i=0; i<same_m_number; i++) {
    //printf("n_oc0[%d]=%d, n_oc1[%d]=%d \n", i, n_oc0[i], i, n_oc1[i]);
  }

  // np_n_array[][]
  for(i=0; i<same_m_number; i++) {
    if (N_PUCCH_SF1 == 5) {
      np_n_array[0][i] = TBL_3_SF5_GEN_N_DASH_NS[n_oc0[i]]; //slot0
      np_n_array[1][i] = TBL_3_SF5_GEN_N_DASH_NS[n_oc1[i]]; //slot1
    } else {
      np_n_array[0][i] = TBL_3_SF4_GEN_N_DASH_NS[n_oc0[i]];
      np_n_array[1][i] = TBL_3_SF4_GEN_N_DASH_NS[n_oc1[i]];
    }
  }

  for(i=0; i<same_m_number; i++) {
    //printf("np_n_array[0][%d]=%d ,np_n_array[1][%d]=%d \n", i, np_n_array[0][i], i, np_n_array[1][i]);
  }

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    for (symNo=0; symNo<D_NSYM1SF; symNo++) { // #define D_NSYM1SF       2*7
      slotNo = symNo / D_NSYM1SLT;
      sym = symNo % D_NSYM1SLT;

      for (k=0; k<D_NSC1RB; k++) { // #define D_NSC1RB        12
        // remove Base Sequence (c_r^*)*(r_l,m,m,n,k) = BsCshData
        BsCshData[aa][symNo][k][0] = (((int32_t)SubCarrierDeMapData[aa][symNo][k][0] * ul_ref_sigs[u][v][0][k<<1] + (int32_t)SubCarrierDeMapData[aa][symNo][k][1] * ul_ref_sigs[u][v][0][1+(k<<1)])>>15);
        BsCshData[aa][symNo][k][1] = (((int32_t)SubCarrierDeMapData[aa][symNo][k][1] * ul_ref_sigs[u][v][0][k<<1] - (int32_t)SubCarrierDeMapData[aa][symNo][k][0] * ul_ref_sigs[u][v][0][1+(k<<1)])>>15);

        if(shortened_format == 1) {
          if (symNo < D_NSYM1SLT) {
            np = n3_pucch % D_NPUCCH_SF4;   // np = n_oc
            np_n = TBL_3_SF4_GEN_N_DASH_NS[np]; //
          } else {
            np = n3_pucch % D_NPUCCH_SF4;   //
            np_n = TBL_3_SF4_GEN_N_DASH_NS[np]; //
          }

          //npucch_sf = D_NPUCCH_SF4;// = 4
        } else {
          if (symNo < D_NSYM1SLT) {
            np = n3_pucch % D_NPUCCH_SF5;
            np_n = TBL_3_SF5_GEN_N_DASH_NS[np];
          } else {
            np = (3 * n3_pucch) % D_NPUCCH_SF5;
            np_n = TBL_3_SF5_GEN_N_DASH_NS[np];
          }

          //npucch_sf = D_NPUCCH_SF5;// = 5
        }

        // cyclic shift e^(-j * beta_n * k)
        calctmp[0] = alphaTBL_re[(((ncs_cell[2*subframe+slotNo][sym] + np_n)%D_NSC1RB)*k)%12];
        calctmp[1] = alphaTBL_im[(((ncs_cell[2*subframe+slotNo][sym] + np_n)%D_NSC1RB)*k)%12];
        // Channel Estimation 1A, g'(n_cs)_l,m,n
        // CsData_temp = g_l,m,n,k
        // remove cyclic shift BsCshData * e^(-j * beta_n * k)
        CsData_temp[aa][symNo][k][0]=((((int32_t)BsCshData[aa][symNo][k][0] * calctmp[0] + (int32_t)BsCshData[aa][symNo][k][1] * calctmp[1])/ D_NSC1RB)>>15);
        CsData_temp[aa][symNo][k][1]=((((int32_t)BsCshData[aa][symNo][k][1] * calctmp[0] - (int32_t)BsCshData[aa][symNo][k][0] * calctmp[1])/ D_NSC1RB)>>15);
        // Interference power for Channel Estimation 1A, No use Cyclic Shift g'(n_cs)_l,m,n
        // Calculated by the cyclic shift that is not used  S(ncs)_est
        ip_ind = 0;

        for(i=0; i<N_PUCCH_SF1; i++) {
          for(j=0; j<same_m_number; j++) {  //np_n_array Loop
            if(shortened_format == 1) {
              if(symNo < D_NSYM1SLT) { // if SF==1 slot0
                if(TBL_3_SF4_GEN_N_DASH_NS[i] == np_n_array[0][j]) {
                  break;
                }
              } else { // if SF==1 slot1
                if(TBL_3_SF4_GEN_N_DASH_NS[i] == np_n_array[1][j]) {
                  break;
                }
              }
            } else {
              if(symNo < D_NSYM1SLT) { // if SF==0 slot0
                if(TBL_3_SF5_GEN_N_DASH_NS[i] == np_n_array[0][j]) {
                  break;
                }
              } else { // if SF==0 slot1
                if(TBL_3_SF5_GEN_N_DASH_NS[i] == np_n_array[1][j]) {
                  break;
Yoshi's avatar
Yoshi committed
373
                }
374
              }
Yoshi's avatar
Yoshi committed
375
            }
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

            if(j == same_m_number - 1) { //when even once it has not been used
              if(shortened_format == 1) {
                calctmp[0] = alphaTBL_re[(((ncs_cell[2*subframe+slotNo][sym] + TBL_3_SF4_GEN_N_DASH_NS[i])%D_NSC1RB)*k)%12]; //D_NSC1RB =12
                calctmp[1] = alphaTBL_im[(((ncs_cell[2*subframe+slotNo][sym] + TBL_3_SF4_GEN_N_DASH_NS[i])%D_NSC1RB)*k)%12];
              } else {
                calctmp[0] = alphaTBL_re[(((ncs_cell[2*subframe+slotNo][sym] + TBL_3_SF5_GEN_N_DASH_NS[i])%D_NSC1RB)*k)%12];
                calctmp[1] = alphaTBL_im[(((ncs_cell[2*subframe+slotNo][sym] + TBL_3_SF5_GEN_N_DASH_NS[i])%D_NSC1RB)*k)%12];
              }

              // IP_CsData_allsfavg = g'(n_cs)_l,m,n
              IP_CsData_allsfavg[aa][symNo][ip_ind][0] += ((((int32_t)BsCshData[aa][symNo][k][0] * calctmp[0] + (int32_t)BsCshData[aa][symNo][k][1] * calctmp[1]))>>15);
              IP_CsData_allsfavg[aa][symNo][ip_ind][1] += ((((int32_t)BsCshData[aa][symNo][k][1] * calctmp[0] - (int32_t)BsCshData[aa][symNo][k][0] * calctmp[1]))>>15);

              if((symNo == 1 || symNo == 5 || symNo == 8 || symNo == 12)) {
              }

              ip_ind++;
Yoshi's avatar
Yoshi committed
394
            }
395
          }
Yoshi's avatar
Yoshi committed
396
        }
397 398 399 400 401 402
      }

      if(symNo > D_NSYM1SLT-1) {
        u=u1;
        v=v1;
      }
Yoshi's avatar
Yoshi committed
403
    }
404 405 406 407 408 409 410 411 412 413 414
  }

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    for (symNo=0; symNo<D_NSYM1SF; symNo++) {
      CsData_allavg[aa][symNo][0] = 0;
      CsData_allavg[aa][symNo][1] = 0;

      for (k=0; k<D_NSC1RB; k++) {
        CsData_allavg[aa][symNo][0] += (int16_t)((double)CsData_temp[aa][symNo][k][0]);
        CsData_allavg[aa][symNo][1] += (int16_t)((double)CsData_temp[aa][symNo][k][1]);
      }
Yoshi's avatar
Yoshi committed
415
    }
416 417 418 419 420 421 422 423 424 425 426 427
  }

  // Frequency deviation estimation
  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    for (k=0; k<12; k++) {
      delta_theta_comp[aa][k][0] = 0;
      delta_theta_comp[aa][k][1] = 0;
      delta_theta_comp[aa][k][0] += (((int32_t)CsData_temp[aa][1][k][0] * CsData_temp[aa][5][k][0] + (int32_t)((CsData_temp[aa][1][k][1])*CsData_temp[aa][5][k][1]))>>8);
      delta_theta_comp[aa][k][1] += (((int32_t)CsData_temp[aa][1][k][0]*CsData_temp[aa][5][k][1] - (int32_t)((CsData_temp[aa][1][k][1])*CsData_temp[aa][5][k][0]) )>>8);
      delta_theta_comp[aa][k][0] += (((int32_t)CsData_temp[aa][8][k][0] * CsData_temp[aa][12][k][0] + (int32_t)((CsData_temp[aa][8][k][1])*CsData_temp[aa][12][k][1]))>>8);
      delta_theta_comp[aa][k][1] += (((int32_t)CsData_temp[aa][8][k][0]*CsData_temp[aa][12][k][1] - (int32_t)((CsData_temp[aa][8][k][1])*CsData_temp[aa][12][k][0]))>>8);
      delta_theta[aa][k] = atan2((double)delta_theta_comp[aa][k][1], (double)delta_theta_comp[aa][k][0]) / 4.0;
Yoshi's avatar
Yoshi committed
428
    }
429 430 431 432 433 434 435 436 437 438 439 440
  }

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    for (k=0; k<D_NSC1RB; k++) {
      ChestValue[aa][0][k][0] = (int16_t)((CsData_allavg[aa][1][0] + (int16_t)(((double)CsData_allavg[aa][5][0] * cos(delta_theta[aa][k]*4)) + ((double)CsData_allavg[aa][5][1] * sin(
                                             delta_theta[aa][k]*4)))) /(2*D_NSC1RB)) ;
      ChestValue[aa][0][k][1] = (int16_t)((CsData_allavg[aa][1][1] + (int16_t)(((double)CsData_allavg[aa][5][1] * cos(delta_theta[aa][k]*4)) - ((double)CsData_allavg[aa][5][0] * sin(
                                             delta_theta[aa][k]*4)))) /(2*D_NSC1RB)) ;
      ChestValue[aa][1][k][0] = (int16_t)((CsData_allavg[aa][8][0] + (int16_t)(((double)CsData_allavg[aa][12][0] * cos(delta_theta[aa][k]*4)) + ((double)CsData_allavg[aa][12][1] * sin(
                                             delta_theta[aa][k]*4)))) /(2*D_NSC1RB)) ;
      ChestValue[aa][1][k][1] = (int16_t)((CsData_allavg[aa][8][1] + (int16_t)(((double)CsData_allavg[aa][12][1] * cos(delta_theta[aa][k]*4)) - ((double)CsData_allavg[aa][12][0] * sin(
                                             delta_theta[aa][k]*4)))) /(2*D_NSC1RB)) ;
Yoshi's avatar
Yoshi committed
441
    }
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  }

  *Interpw = 0;

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    if(ip_ind == 0) {//ip_ind= The total number of cyclic shift of non-use
      *Interpw = 1;
      break;
    }

    for(i=0; i<ip_ind; i++) {
      IP_allavg[i] = 0;
      IP_allavg[i] += (((int32_t)IP_CsData_allsfavg[aa][1][i][0] * IP_CsData_allsfavg[aa][1][i][0] + (int32_t)IP_CsData_allsfavg[aa][1][i][1]*IP_CsData_allsfavg[aa][1][i][1])>>8);
      IP_allavg[i] += (((int32_t)IP_CsData_allsfavg[aa][5][i][0] * IP_CsData_allsfavg[aa][5][i][0] + (int32_t)IP_CsData_allsfavg[aa][5][i][1]*IP_CsData_allsfavg[aa][5][i][1])>>8);
      IP_allavg[i] += (((int32_t)IP_CsData_allsfavg[aa][8][i][0] * IP_CsData_allsfavg[aa][8][i][0] + (int32_t)IP_CsData_allsfavg[aa][8][i][1]*IP_CsData_allsfavg[aa][8][i][1])>>8);
      IP_allavg[i] += (((int32_t)IP_CsData_allsfavg[aa][12][i][0] * IP_CsData_allsfavg[aa][12][i][0] + (int32_t)IP_CsData_allsfavg[aa][12][i][1]*IP_CsData_allsfavg[aa][12][i][1])>>8);
      *Interpw += IP_allavg[i]/(2*D_NSLT1SF*frame_parms->nb_antennas_rx*ip_ind*12);
Yoshi's avatar
Yoshi committed
459
    }
460 461 462
  }

  return 0;
Yoshi's avatar
Yoshi committed
463 464 465
}

/* Channel Equalization */
466 467 468
uint16_t pucchfmt3_Equalization( int16_t CshData_fmt3[4][14][12][2],
                                 int16_t ChdetAfterValue_fmt3[4][14][12][2],
                                 int16_t ChestValue[4][2][12][2],
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
                                 LTE_DL_FRAME_PARMS *frame_parms) {
  int16_t aa, sltNo, symNo, k;

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    sltNo = 0;

    for (symNo=0; symNo<D_NSYM1SF; symNo++) {
      if(symNo >= D_NSYM1SLT) {
        sltNo = 1;
      }

      for (k=0; k<D_NSC1RB; k++) {
        ChdetAfterValue_fmt3[aa][symNo][k][0] = (((int32_t)CshData_fmt3[aa][symNo][k][0] * ChestValue[aa][sltNo][k][0] + (int32_t)CshData_fmt3[aa][symNo][k][1] * ChestValue[aa][sltNo][k][1])>>8);
        ChdetAfterValue_fmt3[aa][symNo][k][1] = (((int32_t)CshData_fmt3[aa][symNo][k][1] * ChestValue[aa][sltNo][k][0] - (int32_t)CshData_fmt3[aa][symNo][k][0] * ChestValue[aa][sltNo][k][1])>>8);
      }
Yoshi's avatar
Yoshi committed
484
    }
485 486 487
  }

  return 0;
Yoshi's avatar
Yoshi committed
488 489 490
}

/* Frequency deviation remove AFC */
491 492 493
uint16_t pucchfmt3_FrqDevRemove( int16_t ChdetAfterValue_fmt3[4][14][12][2],
                                 double delta_theta[4][12],
                                 int16_t RemoveFrqDev_fmt3[4][2][5][12][2],
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
                                 LTE_DL_FRAME_PARMS *frame_parms ) {
  int16_t aa, sltNo, symNo1slt, k, n;
  double calctmp[2];

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    for(sltNo = 0; sltNo<D_NSLT1SF; sltNo++) {
      n=0;

      for (symNo1slt=0, n=0; symNo1slt<D_NSYM1SLT; symNo1slt++) {
        if(!((symNo1slt==1) || (symNo1slt==5))) {
          for (k=0; k<D_NSC1RB; k++) {
            calctmp[0] = cos(delta_theta[aa][k] * (n-1));
            calctmp[1] = sin(delta_theta[aa][k] * (n-1));
            RemoveFrqDev_fmt3[aa][sltNo][n][k][0] = (int16_t)((double)ChdetAfterValue_fmt3[aa][(sltNo*D_NSYM1SLT)+symNo1slt][k][0] * calctmp[0]
                                                    + (double)ChdetAfterValue_fmt3[aa][(sltNo*D_NSYM1SLT)+symNo1slt][k][1] * calctmp[1]);
            RemoveFrqDev_fmt3[aa][sltNo][n][k][1] = (int16_t)((double)ChdetAfterValue_fmt3[aa][(sltNo*D_NSYM1SLT)+symNo1slt][k][1] * calctmp[0]
                                                    - (double)ChdetAfterValue_fmt3[aa][(sltNo*D_NSYM1SLT)+symNo1slt][k][0] * calctmp[1]);
          }

          n++;
Yoshi's avatar
Yoshi committed
514
        }
515
      }
Yoshi's avatar
Yoshi committed
516
    }
517 518 519
  }

  return 0;
Yoshi's avatar
Yoshi committed
520 521
}

522
//for opt.Lev.2
Yoshi's avatar
Yoshi committed
523 524
#define  MAXROW_TBL_SF5  5
#define  MAXCLM_TBL_SF5  5
525 526 527 528 529 530 531
const int16_t TBL_3_SF5[MAXROW_TBL_SF5][MAXCLM_TBL_SF5][2] = {
  { {32767,0}, {32767,0}, {32767,0}, {32767,0}, {32767,0}},
  { {32767,0}, {10126, 31163}, {-26509, 19260}, {-26509, -19260}, {10126, -31163}},
  { {32767,0}, {-26509, 19260}, {10126, -31163}, {10126, 31163}, {-26509, -19260}},
  { {32767,0}, {-26509, -19260}, {10126, 31163}, {10126, -31163}, {-26509, 19260}},
  { {32767,0}, {10126, -31163}, {-26509, -19260}, {-26509, 19260}, {10126, 31163}}
};
Yoshi's avatar
Yoshi committed
532 533 534

#define  MAXROW_TBL_SF4_fmt3 4
#define  MAXCLM_TBL_SF4      4
535 536 537 538 539 540
const int16_t TBL_3_SF4[MAXROW_TBL_SF4_fmt3][MAXCLM_TBL_SF4][2] = {
  { {32767,0}, {32767,0}, {32767,0}, {32767,0}},
  { {32767,0}, {-32767,0}, {32767,0}, {-32767,0}},
  { {32767,0}, {32767,0}, {-32767,0}, {-32767,0}},
  { {32767,0}, {-32767,0}, {-32767,0}, {32767,0}}
};
Yoshi's avatar
Yoshi committed
541 542

/* orthogonal sequence remove */
543 544
uint16_t pucchfmt3_OrthSeqRemove( int16_t RemoveFrqDev_fmt3[4][2][5][12][2],
                                  int16_t Fmt3xDataRmvOrth[4][2][5][12][2],
Yoshi's avatar
Yoshi committed
545 546
                                  uint8_t shortened_format,
                                  uint16_t n3_pucch,
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
                                  LTE_DL_FRAME_PARMS *frame_parms ) {
  int16_t aa, sltNo, n, k;
  int16_t Npucch_sf;
  int16_t noc;

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
    for (sltNo=0; sltNo<D_NSLT1SF; sltNo++) {
      if(shortened_format == 1) {
        if(sltNo == 0) {
          noc = n3_pucch % D_NPUCCH_SF4;
          Npucch_sf = D_NPUCCH_SF5;
        } else {
          noc = n3_pucch % D_NPUCCH_SF4;
          Npucch_sf = D_NPUCCH_SF4;
        }
      } else {
        if(sltNo == 0) {
          noc = n3_pucch % D_NPUCCH_SF5;
          Npucch_sf = D_NPUCCH_SF5;
        } else {
          noc = (3 * n3_pucch) % D_NPUCCH_SF5;
          Npucch_sf = D_NPUCCH_SF5;
Yoshi's avatar
Yoshi committed
569
        }
570 571 572 573 574 575 576 577 578 579 580 581 582
      }

      for (n=0; n<Npucch_sf; n++) {
        for (k=0; k<D_NSC1RB; k++) {
          if ((sltNo == 1) && (shortened_format == 1)) {
            Fmt3xDataRmvOrth[aa][sltNo][n][k][0] = (((int32_t)RemoveFrqDev_fmt3[aa][sltNo][n][k][0] * TBL_3_SF4[noc][n][0] + (int32_t)RemoveFrqDev_fmt3[aa][sltNo][n][k][1] * TBL_3_SF4[noc][n][1])>>15);
            Fmt3xDataRmvOrth[aa][sltNo][n][k][1] = (((int32_t)RemoveFrqDev_fmt3[aa][sltNo][n][k][1] * TBL_3_SF4[noc][n][0] - (int32_t)RemoveFrqDev_fmt3[aa][sltNo][n][k][0] * TBL_3_SF4[noc][n][1])>>15);
          } else {
            Fmt3xDataRmvOrth[aa][sltNo][n][k][0] = (((int32_t)RemoveFrqDev_fmt3[aa][sltNo][n][k][0] * TBL_3_SF5[noc][n][0] + (int32_t)RemoveFrqDev_fmt3[aa][sltNo][n][k][1] * TBL_3_SF5[noc][n][1])>>15);
            Fmt3xDataRmvOrth[aa][sltNo][n][k][1] = (((int32_t)RemoveFrqDev_fmt3[aa][sltNo][n][k][1] * TBL_3_SF5[noc][n][0] - (int32_t)RemoveFrqDev_fmt3[aa][sltNo][n][k][0] * TBL_3_SF5[noc][n][1])>>15);
          }
        }
      }
Yoshi's avatar
Yoshi committed
583
    }
584 585 586
  }

  return 0;
Yoshi's avatar
Yoshi committed
587 588 589
}

/* averaging antenna */
590
uint16_t pucchfmt3_AvgAnt( int16_t Fmt3xDataRmvOrth[4][2][5][12][2],
Yoshi's avatar
Yoshi committed
591 592
                           int16_t Fmt3xDataAvgAnt[2][5][12][2],
                           uint8_t shortened_format,
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
                           LTE_DL_FRAME_PARMS *frame_parms ) {
  int16_t aa, sltNo, n, k;
  int16_t Npucch_sf;

  for (sltNo=0; sltNo<D_NSLT1SF; sltNo++) {
    if((sltNo == 1) && (shortened_format == 1)) {
      Npucch_sf = D_NPUCCH_SF4;
    } else {
      Npucch_sf = D_NPUCCH_SF5;
    }

    for (n=0; n<Npucch_sf; n++) {
      for (k=0; k<D_NSC1RB; k++) {
        Fmt3xDataAvgAnt[sltNo][n][k][0] = 0;
        Fmt3xDataAvgAnt[sltNo][n][k][1] = 0;

        for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
          Fmt3xDataAvgAnt[sltNo][n][k][0] += Fmt3xDataRmvOrth[aa][sltNo][n][k][0]  / frame_parms->nb_antennas_rx;
          Fmt3xDataAvgAnt[sltNo][n][k][1] += Fmt3xDataRmvOrth[aa][sltNo][n][k][1]  / frame_parms->nb_antennas_rx;
Yoshi's avatar
Yoshi committed
612
        }
613
      }
Yoshi's avatar
Yoshi committed
614
    }
615 616 617
  }

  return 0;
Yoshi's avatar
Yoshi committed
618 619 620 621 622
}

/* averaging symbol */
uint16_t pucchfmt3_AvgSym( int16_t Fmt3xDataAvgAnt[2][5][12][2],
                           int16_t Fmt3xDataAvgSym[2][12][2],
623 624 625 626 627 628 629 630 631
                           uint8_t shortened_format ) {
  int16_t sltNo, n, k;
  int16_t Npucch_sf;

  for (sltNo=0; sltNo<D_NSLT1SF; sltNo++) {
    if((sltNo == 1) && (shortened_format == 1)) {
      Npucch_sf = D_NPUCCH_SF4;
    } else {
      Npucch_sf = D_NPUCCH_SF5;
Yoshi's avatar
Yoshi committed
632
    }
633 634 635 636 637 638 639 640 641 642 643 644 645

    for (k=0; k<D_NSC1RB; k++) {
      Fmt3xDataAvgSym[sltNo][k][0] = 0;
      Fmt3xDataAvgSym[sltNo][k][1] = 0;

      for (n=0; n<Npucch_sf; n++) {
        Fmt3xDataAvgSym[sltNo][k][0] += Fmt3xDataAvgAnt[sltNo][n][k][0] / Npucch_sf;
        Fmt3xDataAvgSym[sltNo][k][1] += Fmt3xDataAvgAnt[sltNo][n][k][1] / Npucch_sf;
      }
    }
  }

  return 0;
Yoshi's avatar
Yoshi committed
646 647 648
}

/* iDFT */
649
void pucchfmt3_IDft2( int16_t *x, int16_t *y ) {
Yoshi's avatar
Yoshi committed
650 651
  int16_t i, k;
  int16_t tmp[2];
652 653
  int16_t calctmp[D_NSC1RB*2]= {0};

Yoshi's avatar
Yoshi committed
654 655 656 657 658 659 660
  for(k=0; k<D_NSC1RB; k++) {
    for (i=0; i<D_NSC1RB; i++) {
      tmp[0] = alphaTBL_re[((i*k)%12)];
      tmp[1] = alphaTBL_im[((i*k)%12)];
      calctmp[2*k] += (((int32_t)x[2*i] * tmp[0] - (int32_t)x[2*i+1] * tmp[1])>>15);
      calctmp[2*k+1] += (((int32_t)x[2*i+1] * tmp[0] + (int32_t)x[2*i] * tmp[1])>>15);
    }
661

Yoshi's avatar
Yoshi committed
662 663 664 665 666 667 668 669 670 671 672
    y[2*k] = (int16_t)( (double) calctmp[2*k] / sqrt(D_NSC1RB));
    y[2*k+1] = (int16_t)((double) calctmp[2*k+1] / sqrt(D_NSC1RB));
  }
}

/* descramble */
uint16_t pucchfmt3_Descramble( int16_t IFFTOutData_Fmt3[2][12][2],
                               int16_t b[48],
                               uint8_t subframe,
                               uint32_t Nid_cell,
                               uint32_t rnti
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
                             ) {
  int16_t m, k, c,i,j;
  uint32_t cinit = 0;
  uint32_t x1;
  uint32_t s,s0,s1;
  cinit = (subframe + 1) * ((2 * Nid_cell + 1)<<16) + rnti;
  s0 = lte_gold_generic(&x1,&cinit,1);
  s1 = lte_gold_generic(&x1,&cinit,0);
  i=0;

  for (m=0; m<D_NSLT1SF; m++) {
    for(k=0; k<D_NSC1RB; k++) {
      s = (i<32)? s0:s1;
      j = (i<32)? i:(i-32);
      c=((s>>j)&1);
      b[i] = (IFFTOutData_Fmt3[m][k][0] * (1 - 2*c));
      i++;
      s = (i<32)? s0:s1;
      j = (i<32)? i:(i-32);
      c=((s>>j)&1);
      b[i] = (IFFTOutData_Fmt3[m][k][1] * (1 - 2*c));
      i++;
Yoshi's avatar
Yoshi committed
695
    }
696 697 698
  }

  return 0;
Yoshi's avatar
Yoshi committed
699 700 701 702 703 704
}

int16_t pucchfmt3_Decode( int16_t b[48],
                          uint8_t subframe,
                          int16_t DTXthreshold,
                          int16_t Interpw,
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
                          uint8_t do_sr) {
  int16_t c, i;
  int32_t Rho_tmp;
  int16_t c_max;
  int32_t Rho_max;
  int16_t bit_pattern;

  /* Is payload 6bit or 7bit? */
  if( do_sr == 1 ) {
    bit_pattern = 128;
  } else {
    bit_pattern = 64;
  }

  c=0;
  Rho_tmp = 0;

  for (i=0; i<48; i++) {
    Rho_tmp += b[i] * (1-2*chcod_tbl[c][i]);
  }

  c_max = c;
  Rho_max = Rho_tmp;

  for(c=1; c<bit_pattern; c++) {
Yoshi's avatar
Yoshi committed
730
    Rho_tmp = 0;
731 732 733

    for (i=0; i<48; i++) {
      Rho_tmp += b[i] * (1-2*chcod_tbl[c][i]);
Yoshi's avatar
Yoshi committed
734
    }
735 736 737 738

    if (Rho_tmp > Rho_max) {
      c_max = c;
      Rho_max = Rho_tmp;
Yoshi's avatar
Yoshi committed
739
    }
740 741 742 743 744 745 746 747 748 749 750 751 752
  }

  if(Interpw<1) {
    Interpw=1;
  }

  if((Rho_max/Interpw) > DTXthreshold) {
    // ***Log
    return c_max;
  } else {
    // ***Log
    return -1;
  }
Yoshi's avatar
Yoshi committed
753 754
}

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

uint32_t calc_pucch_1x_interference(PHY_VARS_eNB *eNB,
		  int     frame,
		  uint8_t subframe,
		  uint8_t shortened_format
)
//-----------------------------------------------------------------------------
{
  LTE_eNB_COMMON *common_vars = &eNB->common_vars;
  LTE_DL_FRAME_PARMS *frame_parms = &eNB->frame_parms;

  uint32_t u,v,n,aa;
  uint32_t z[12*14];
  int16_t *zptr;
  int16_t rxcomp[NB_ANTENNAS_RX][2*12*14];
masayuki.harada's avatar
masayuki.harada committed
770
  uint8_t ns,N_UL_symb,nsymb,n_cs_base;
771 772 773 774
  uint16_t i,j,re_offset;
  uint8_t m,l;
  uint8_t n_cs,alpha_ind;
  int16_t tmp_re,tmp_im,W_re=0,W_im=0;
masayuki.harada's avatar
masayuki.harada committed
775
  int16_t W4_nouse[4]={32767,32767,-32768,-32768};
776 777
  int32_t n0_IQ[2];
  double interference_power;
778 779 780
  int16_t *rxptr;
  uint32_t symbol_offset;

masayuki.harada's avatar
masayuki.harada committed
781 782
  uint32_t u0 = (frame_parms->pucch_config_common.grouphop[subframe<<1]) % 30;
  uint32_t u1 = (frame_parms->pucch_config_common.grouphop[1+(subframe<<1)]) % 30;
783 784 785 786 787 788 789
  uint32_t v0=frame_parms->pusch_config_common.ul_ReferenceSignalsPUSCH.seqhop[subframe<<1];
  uint32_t v1=frame_parms->pusch_config_common.ul_ReferenceSignalsPUSCH.seqhop[1+(subframe<<1)];
  int calc_cnt;

  zptr = (int16_t *)z;
  N_UL_symb = (frame_parms->Ncp==NORMAL) ? 7 : 6;

790
  interference_power=0.0;
791 792
  calc_cnt=0;
  // loop over 2 slots
masayuki.harada's avatar
masayuki.harada committed
793 794
  for (n_cs_base=0; n_cs_base<12; n_cs_base++) {
    zptr = (int16_t *)z;
795 796 797 798
    for (ns=(subframe<<1),u=u0,v=v0; ns<(2+(subframe<<1)); ns++,u=u1,v=v1) {

      //loop over symbols in slot
      for (l=0; l<N_UL_symb; l++) {
masayuki.harada's avatar
masayuki.harada committed
799
        n_cs = eNB->ncs_cell[ns][l]+n_cs_base;
800
        if(((l>1)&&(l<N_UL_symb-2)) || ((ns==(1+(subframe<<1))) && (shortened_format==1)) ){
801
          zptr+=24;
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
          continue;
        }

        if (l<2) {                                         // data
          W_re=W4_nouse[l];
          W_im=0;
        } else if ((l>=N_UL_symb-2)) {                     // data
          W_re=W4_nouse[l-N_UL_symb+4];
          W_im=0;
        }

        alpha_ind=0;
        // compute output sequence

        for (n=0; n<12; n++) {

          // this is r_uv^alpha(n)
          tmp_re = (int16_t)(((int32_t)alpha_re[alpha_ind] * ul_ref_sigs[u][v][0][n<<1] - (int32_t)alpha_im[alpha_ind] * ul_ref_sigs[u][v][0][1+(n<<1)])>>15);
          tmp_im = (int16_t)(((int32_t)alpha_re[alpha_ind] * ul_ref_sigs[u][v][0][1+(n<<1)] + (int32_t)alpha_im[alpha_ind] * ul_ref_sigs[u][v][0][n<<1])>>15);

          // this is S(ns)*w_noc(m)*r_uv^alpha(n)
          zptr[n<<1] = (tmp_re*W_re - tmp_im*W_im)>>15;
          zptr[1+(n<<1)] = -(tmp_re*W_im + tmp_im*W_re)>>15;

          alpha_ind = (alpha_ind + n_cs)%12;
        } // n

        zptr+=24;
      } // l
    } // ns

masayuki.harada's avatar
masayuki.harada committed
833
    m = 1;
834 835 836 837 838 839 840

    nsymb = N_UL_symb<<1;

    zptr = (int16_t*)z;

    // Do detection
    for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
841 842
      n0_IQ[0]=0;
      n0_IQ[1]=0;
843 844
      for (j=0,l=0; l<nsymb; l++) {
        if((((l%N_UL_symb)>1)&&((l%N_UL_symb)<N_UL_symb-2)) || ((nsymb>=N_UL_symb) && (shortened_format==1)) ){
845
          j+=24;
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
          continue;
        }
          if ((l<(nsymb>>1)) && ((m&1) == 0))
            re_offset = (m*6) + frame_parms->first_carrier_offset;
          else if ((l<(nsymb>>1)) && ((m&1) == 1))
            re_offset = frame_parms->first_carrier_offset + (frame_parms->N_RB_DL - (m>>1) - 1)*12;
          else if ((m&1) == 0)
            re_offset = frame_parms->first_carrier_offset + (frame_parms->N_RB_DL - (m>>1) - 1)*12;
          else
            re_offset = ((m-1)*6) + frame_parms->first_carrier_offset;

        if (re_offset > frame_parms->ofdm_symbol_size)
          re_offset -= (frame_parms->ofdm_symbol_size);

        symbol_offset = (unsigned int)frame_parms->ofdm_symbol_size*l;
        rxptr = (int16_t *)&common_vars->rxdataF[aa][symbol_offset];

        for (i=0; i<12; i++,j+=2,re_offset++) {
          if (re_offset==frame_parms->ofdm_symbol_size)
            re_offset = 0;

          rxcomp[aa][j]   = (int16_t)((rxptr[re_offset<<1]*(int32_t)zptr[j])>>15)   - ((rxptr[1+(re_offset<<1)]*(int32_t)zptr[1+j])>>15);
          rxcomp[aa][1+j] = (int16_t)((rxptr[re_offset<<1]*(int32_t)zptr[1+j])>>15) + ((rxptr[1+(re_offset<<1)]*(int32_t)zptr[j])>>15);
869 870
          n0_IQ[0]+=rxcomp[aa][j];
          n0_IQ[1]+=rxcomp[aa][1+j];
871
        } //re
masayuki.harada's avatar
masayuki.harada committed
872
        calc_cnt++;
873
      } // symbol
masayuki.harada's avatar
masayuki.harada committed
874 875
      n0_IQ[0]/=12;
      n0_IQ[1]/=12;
masayuki.harada's avatar
masayuki.harada committed
876
      interference_power+= (double)(n0_IQ[0]*n0_IQ[0]+n0_IQ[1]*n0_IQ[1]);
877 878
    }  // antenna
  }
879
  interference_power /= calc_cnt;
masayuki.harada's avatar
masayuki.harada committed
880
  eNB->measurements.n0_pucch_dB = dB_fixed_x10((int)interference_power)/10;
881
  LOG_D(PHY,"estimate pucch noise %lf %d %d\n",interference_power,calc_cnt,eNB->measurements.n0_pucch_dB);
882 883 884 885
  return 0;
}


Yoshi's avatar
Yoshi committed
886 887
/* PUCCH format3 << */

888
uint32_t rx_pucch(PHY_VARS_eNB *eNB,
889
                  PUCCH_FMT_t fmt,
890
                  uint8_t UCI_id,
891 892 893 894 895 896 897
                  uint16_t n1_pucch,
                  uint16_t n2_pucch,
                  uint8_t shortened_format,
                  uint8_t *payload,
                  int     frame,
                  uint8_t subframe,
                  uint8_t pucch1_thres,
898
                  int br_flag
899
                 )
900
//-----------------------------------------------------------------------------
901
{
902 903 904
  static int first_call = 1;
  LTE_eNB_COMMON *common_vars = &eNB->common_vars;
  LTE_DL_FRAME_PARMS *frame_parms = &eNB->frame_parms;
Raymond Knopp's avatar
Raymond Knopp committed
905
  int8_t sigma2_dB = /*max(eNB->measurements.n0_subband_power_tot_dB[0], eNB->measurements.n0_subband_power_tot_dB[eNB->frame_parms.N_RB_UL-1]); */eNB->measurements.n0_pucch_dB;
906

907 908 909
  uint32_t u,v,n,aa;
  uint32_t z[12*14];
  int16_t *zptr;
910
  int16_t rxcomp[4][2*12*14];
911 912
  uint8_t ns,N_UL_symb,nsymb,n_oc,n_oc0,n_oc1;
  uint8_t c = (frame_parms->Ncp==0) ? 3 : 2;
913
  int16_t nprime,nprime0,nprime1;
914 915 916 917 918 919 920
  uint16_t i,j,re_offset,thres,h,off;
  uint8_t Nprime_div_deltaPUCCH_Shift,Nprime,d;
  uint8_t m,l,refs,phase,re,l2,phase_max=0;
  uint8_t n_cs,S,alpha_ind,rem;
  int16_t tmp_re,tmp_im,W_re=0,W_im=0;
  int16_t *rxptr;
  uint32_t symbol_offset;
921 922 923
  int16_t stat0_ref_re[4],stat0_ref_im[4],stat1_ref_re[4],stat1_ref_im[4],*cfo;
  int16_t chest0_re[4][12],chest0_im[4][12];
  int16_t chest1_re[4][12],chest1_im[4][12];
924
  int32_t chest_mag;
925 926
  int32_t stat0_re[4],stat1_re[4],stat0_im[4],stat1_im[4];
  uint32_t stat0[4],stat1[4],stat_max=0,stat0_max[4],stat1_max[4]; 
927
  uint8_t log2_maxh;
928 929 930
  uint8_t deltaPUCCH_Shift          = frame_parms->pucch_config_common.deltaPUCCH_Shift;
  uint8_t NRB2                      = frame_parms->pucch_config_common.nRB_CQI;
  uint8_t Ncs1_div_deltaPUCCH_Shift = frame_parms->pucch_config_common.nCS_AN;
masayuki.harada's avatar
masayuki.harada committed
931 932 933

  uint32_t u0 = (frame_parms->pucch_config_common.grouphop[subframe<<1]) % 30;
  uint32_t u1 = (frame_parms->pucch_config_common.grouphop[1+(subframe<<1)]) % 30;
934 935
  uint32_t v0=frame_parms->pusch_config_common.ul_ReferenceSignalsPUSCH.seqhop[subframe<<1];
  uint32_t v1=frame_parms->pusch_config_common.ul_ReferenceSignalsPUSCH.seqhop[1+(subframe<<1)];
936
  int chL;
Yoshi's avatar
Yoshi committed
937 938
  /* PUCCH format3 >> */
  uint16_t Ret = 0;
939 940 941 942 943 944 945
  int16_t SubCarrierDeMapData[4][14][12][2];       //[Antenna][Symbol][Subcarrier][Complex]
  int16_t CshData_fmt3[4][14][12][2];              //[Antenna][Symbol][Subcarrier][Complex]
  double delta_theta[4][12];                       //[Antenna][Subcarrier][Complex]
  int16_t ChestValue[4][2][12][2];                 //[Antenna][Slot][Subcarrier][Complex]
  int16_t ChdetAfterValue_fmt3[4][14][12][2];      //[Antenna][Symbol][Subcarrier][Complex]
  int16_t RemoveFrqDev_fmt3[4][2][5][12][2];       //[Antenna][Slot][PUCCH_Symbol][Subcarrier][Complex]
  int16_t Fmt3xDataRmvOrth[4][2][5][12][2];        //[Antenna][Slot][PUCCH_Symbol][Subcarrier][Complex]
Yoshi's avatar
Yoshi committed
946 947 948 949 950 951 952 953 954 955
  int16_t Fmt3xDataAvgAnt[2][5][12][2];                         //[Slot][PUCCH_Symbol][Subcarrier][Complex]
  int16_t Fmt3xDataAvgSym[2][12][2];                            //[Slot][Subcarrier][Complex]
  int16_t IFFTOutData_Fmt3[2][12][2];                           //[Slot][Subcarrier][Complex]
  int16_t b[48];                                                //[bit]
  int16_t payload_entity = -1;
  int16_t Interpw;
  int16_t payload_max;
  // TODO
  // When using PUCCH format3, it must be an argument of rx_pucch function
  uint16_t n3_pucch = 20;
956
  uint16_t n3_pucch_array[NUMBER_OF_UE_MAX]= {1};
Yoshi's avatar
Yoshi committed
957
  n3_pucch_array[0]=n3_pucch;
Yoshi's avatar
Yoshi committed
958
  uint8_t do_sr = 1;
959
  uint16_t crnti=0x1234;
Yoshi's avatar
Yoshi committed
960
  int16_t DTXthreshold = 10;
Yoshi's avatar
Yoshi committed
961 962
  /* PUCCH format3 << */

963
  if (first_call == 1) {
964 965 966 967
    for (i=0; i<10; i++) {
      for (j=0; j<NUMBER_OF_UE_MAX; j++) {
        eNB->pucch1_stats_cnt[j][i]=0;
        eNB->pucch1ab_stats_cnt[j][i]=0;
968 969
        if ( IS_SOFTMODEM_IQPLAYER)
          eNB->pucch1_stats_thres[j][i]=0;
970 971
      }
    }
972

973 974
    first_call=0;
  }
975
  eNB_UCI_STATS_t *uci_stats = NULL;
Yoshi's avatar
Yoshi committed
976
  if(fmt!=pucch_format3) {  /* PUCCH format3 */
977 978 979
 
    eNB_UCI_STATS_t *first_uci_stats=NULL;
    for (int i=0;i<NUMBER_OF_SCH_STATS_MAX;i++) 
Raymond Knopp's avatar
Raymond Knopp committed
980
      if (eNB->uci_stats[i].rnti == eNB->uci_vars[UCI_id].rnti) { 
981 982
        uci_stats = &eNB->uci_stats[i];
        break;
Raymond Knopp's avatar
Raymond Knopp committed
983
      } else if (first_uci_stats == NULL && eNB->uci_stats[i].rnti == 0) first_uci_stats = &eNB->uci_stats[i];
984

985 986 987 988 989 990 991 992
    if (uci_stats == NULL) {
      if (first_uci_stats == NULL) {
        LOG_E(PHY,"first_uci_stats is NULL\n");
        return -1;
      }
      uci_stats=first_uci_stats;
      uci_stats->rnti = eNB->uci_vars[UCI_id].rnti;
    }
993 994 995

    AssertFatal(uci_stats!=NULL,"No stat index found\n");
    uci_stats->frame = frame;
996 997 998 999 1000 1001 1002 1003
    // TODO
    // "SR+ACK/NACK" length is only 7 bits.
    // This restriction will be lifted in the future.
    // "CQI/PMI/RI+ACK/NACK" will be supported in the future.
    if ((deltaPUCCH_Shift==0) || (deltaPUCCH_Shift>3)) {
      LOG_E(PHY,"[eNB] rx_pucch: Illegal deltaPUCCH_shift %d (should be 1,2,3)\n",deltaPUCCH_Shift);
      return(-1);
    }
1004

1005 1006 1007 1008
    if (Ncs1_div_deltaPUCCH_Shift > 7) {
      LOG_E(PHY,"[eNB] rx_pucch: Illegal Ncs1_div_deltaPUCCH_Shift %d (should be 0...7)\n",Ncs1_div_deltaPUCCH_Shift);
      return(-1);
    }
1009

1010 1011 1012 1013
    zptr = (int16_t *)z;
    thres = (c*Ncs1_div_deltaPUCCH_Shift);
    Nprime_div_deltaPUCCH_Shift = (n1_pucch < thres) ? Ncs1_div_deltaPUCCH_Shift : (12/deltaPUCCH_Shift);
    Nprime = Nprime_div_deltaPUCCH_Shift * deltaPUCCH_Shift;
1014
#ifdef DEBUG_PUCCH_RX
1015
    printf("[eNB] PUCCH: cNcs1/deltaPUCCH_Shift %d, Nprime %d, n1_pucch %d\n",thres,Nprime,n1_pucch);
1016
#endif
1017
    N_UL_symb = (frame_parms->Ncp==NORMAL) ? 7 : 6;
1018

1019 1020 1021 1022
    if (n1_pucch < thres)
      nprime0=n1_pucch;
    else
      nprime0 = (n1_pucch - thres)%(12*c/deltaPUCCH_Shift);
1023

1024 1025 1026 1027 1028 1029 1030
    if (n1_pucch >= thres)
      nprime1= ((c*(nprime0+1))%((12*c/deltaPUCCH_Shift)+1))-1;
    else {
      d = (frame_parms->Ncp==0) ? 2 : 0;
      h= (nprime0+d)%(c*Nprime_div_deltaPUCCH_Shift);
      nprime1 = (h/c) + (h%c)*Nprime_div_deltaPUCCH_Shift;
    }
1031 1032

#ifdef DEBUG_PUCCH_RX
1033
    printf("PUCCH: nprime0 %d nprime1 %d\n",nprime0,nprime1);
1034
#endif
1035
    n_oc0 = nprime0/Nprime_div_deltaPUCCH_Shift;
1036

1037 1038
    if (frame_parms->Ncp==1)
      n_oc0<<=1;
1039

1040
    n_oc1 = nprime1/Nprime_div_deltaPUCCH_Shift;
1041

1042 1043
    if (frame_parms->Ncp==1)  // extended CP
      n_oc1<<=1;
1044 1045

#ifdef DEBUG_PUCCH_RX
1046
    printf("[eNB] PUCCH: noc0 %d noc11 %d\n",n_oc0,n_oc1);
1047
#endif
1048 1049
    nprime=nprime0;
    n_oc  =n_oc0;
1050

1051 1052 1053 1054 1055 1056
    // loop over 2 slots
    for (ns=(subframe<<1),u=u0,v=v0; ns<(2+(subframe<<1)); ns++,u=u1,v=v1) {
      if ((nprime&1) == 0)
        S=0;  // 1
      else
        S=1;  // j
1057

1058 1059 1060 1061 1062 1063
      /*
      if (fmt==pucch_format1)
        LOG_I(PHY,"[eNB] subframe %d => PUCCH1: u%d %d, v%d %d : ", subframe,ns&1,u,ns&1,v);
      else
        LOG_I(PHY,"[eNB] subframe %d => PUCCH1a/b: u%d %d, v%d %d : ", subframe,ns&1,u,ns&1,v);
      */
1064

1065 1066 1067 1068
      //loop over symbols in slot
      for (l=0; l<N_UL_symb; l++) {
        // Compute n_cs (36.211 p. 18)
        n_cs = eNB->ncs_cell[ns][l];
1069

1070 1071 1072 1073
        if (frame_parms->Ncp==0) { // normal CP
          n_cs = ((uint16_t)n_cs + (nprime*deltaPUCCH_Shift + (n_oc%deltaPUCCH_Shift))%Nprime)%12;
        } else {
          n_cs = ((uint16_t)n_cs + (nprime*deltaPUCCH_Shift + (n_oc>>1))%Nprime)%12;
1074
        }
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

        refs=0;

        // Comput W_noc(m) (36.211 p. 19)
        if ((ns==(1+(subframe<<1))) && (shortened_format==1)) {  // second slot and shortened format
          if (l<2) {                                         // data
            W_re=W3_re[n_oc][l];
            W_im=W3_im[n_oc][l];
          } else if ((l<N_UL_symb-2)&&(frame_parms->Ncp==0)) { // reference and normal CP
            W_re=W3_re[n_oc][l-2];
            W_im=W3_im[n_oc][l-2];
            refs=1;
          } else if ((l<N_UL_symb-2)&&(frame_parms->Ncp==1)) { // reference and extended CP
            W_re=W4[n_oc][l-2];
            W_im=0;
            refs=1;
          } else if ((l>=N_UL_symb-2)) {                      // data
            W_re=W3_re[n_oc][l-N_UL_symb+4];
            W_im=W3_im[n_oc][l-N_UL_symb+4];
          }
        } else {
          if (l<2) {                                         // data
            W_re=W4[n_oc][l];
            W_im=0;
          } else if ((l<N_UL_symb-2)&&(frame_parms->Ncp==NORMAL)) { // reference and normal CP
            W_re=W3_re[n_oc][l-2];
            W_im=W3_im[n_oc][l-2];
            refs=1;
          } else if ((l<N_UL_symb-2)&&(frame_parms->Ncp==EXTENDED)) { // reference and extended CP
            W_re=W4[n_oc][l-2];
            W_im=0;
            refs=1;
          } else if ((l>=N_UL_symb-2)) {                     // data
            W_re=W4[n_oc][l-N_UL_symb+4];
            W_im=0;
          }
1111
        }
1112

1113 1114 1115 1116 1117 1118
        // multiply W by S(ns) (36.211 p.17). only for data, reference symbols do not have this factor
        if ((S==1)&&(refs==0)) {
          tmp_re = W_re;
          W_re = -W_im;
          W_im = tmp_re;
        }
1119 1120

#ifdef DEBUG_PUCCH_RX
1121
        printf("[eNB] PUCCH: ncs[%d][%d]=%d, W_re %d, W_im %d, S %d, refs %d\n",ns,l,n_cs,W_re,W_im,S,refs);
1122
#endif
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
        alpha_ind=0;
        // compute output sequence

        for (n=0; n<12; n++) {
          // this is r_uv^alpha(n)
          tmp_re = (int16_t)(((int32_t)alpha_re[alpha_ind] * ul_ref_sigs[u][v][0][n<<1] - (int32_t)alpha_im[alpha_ind] * ul_ref_sigs[u][v][0][1+(n<<1)])>>15);
          tmp_im = (int16_t)(((int32_t)alpha_re[alpha_ind] * ul_ref_sigs[u][v][0][1+(n<<1)] + (int32_t)alpha_im[alpha_ind] * ul_ref_sigs[u][v][0][n<<1])>>15);
          // this is S(ns)*w_noc(m)*r_uv^alpha(n)
          zptr[n<<1] = (tmp_re*W_re - tmp_im*W_im)>>15;
          zptr[1+(n<<1)] = -(tmp_re*W_im + tmp_im*W_re)>>15;
1133
#ifdef DEBUG_PUCCH_RX
1134 1135
          printf("[eNB] PUCCH subframe %d z(%d,%d) => %d,%d, alpha(%d) => %d,%d\n",subframe,l,n,zptr[n<<1],zptr[(n<<1)+1],
                 alpha_ind,alpha_re[alpha_ind],alpha_im[alpha_ind]);
1136
#endif
1137 1138
          alpha_ind = (alpha_ind + n_cs)%12;
        } // n
1139

1140 1141
        zptr+=24;
      } // l
1142

1143 1144 1145
      nprime=nprime1;
      n_oc  =n_oc1;
    } // ns
1146

1147 1148
    rem = ((((deltaPUCCH_Shift*Ncs1_div_deltaPUCCH_Shift)>>3)&7)>0) ? 1 : 0;
    m = (n1_pucch < thres) ? NRB2 : (((n1_pucch-thres)/(12*c/deltaPUCCH_Shift))+NRB2+((deltaPUCCH_Shift*Ncs1_div_deltaPUCCH_Shift)>>3)+rem);
1149
#ifdef DEBUG_PUCCH_RX
1150
    printf("[eNB] PUCCH: m %d, thres %d, NRB2 %d\n",m,thres,NRB2);
1151
#endif
1152 1153
    nsymb = N_UL_symb<<1;
    zptr = (int16_t *)z;
1154

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
    // Do detection
    for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
      //for (j=0,l=0;l<(nsymb-1);l++) {
      for (j=0,l=0; l<nsymb; l++) {
        if (br_flag > 0 ) {
          if ((m&1) == 0)
            re_offset = (m*6) + frame_parms->first_carrier_offset;
          else
            re_offset = frame_parms->first_carrier_offset + (frame_parms->N_RB_DL - (m>>1) - 1)*12;
        } else {
          if ((l<(nsymb>>1)) && ((m&1) == 0))
            re_offset = (m*6) + frame_parms->first_carrier_offset;
          else if ((l<(nsymb>>1)) && ((m&1) == 1))
            re_offset = frame_parms->first_carrier_offset + (frame_parms->N_RB_DL - (m>>1) - 1)*12;
          else if ((m&1) == 0)
            re_offset = frame_parms->first_carrier_offset + (frame_parms->N_RB_DL - (m>>1) - 1)*12;
          else
            re_offset = ((m-1)*6) + frame_parms->first_carrier_offset;
        }
1174

1175 1176
        if (re_offset > frame_parms->ofdm_symbol_size)
          re_offset -= (frame_parms->ofdm_symbol_size);
1177

1178 1179
        symbol_offset = (unsigned int)frame_parms->ofdm_symbol_size*l;
        rxptr = (int16_t *)&common_vars->rxdataF[aa][symbol_offset];
1180

1181 1182 1183
        for (i=0; i<12; i++,j+=2,re_offset++) {
          if (re_offset==frame_parms->ofdm_symbol_size)
            re_offset = 0;
Cedric Roux's avatar
Cedric Roux committed
1184

1185 1186
          rxcomp[aa][j]   = (int16_t)((rxptr[re_offset<<1]*(int32_t)zptr[j])>>15)   - ((rxptr[1+(re_offset<<1)]*(int32_t)zptr[1+j])>>15);
          rxcomp[aa][1+j] = (int16_t)((rxptr[re_offset<<1]*(int32_t)zptr[1+j])>>15) + ((rxptr[1+(re_offset<<1)]*(int32_t)zptr[j])>>15);
1187
#ifdef DEBUG_PUCCH_RX
1188 1189 1190 1191
          printf("[eNB] PUCCH subframe %d (%d,%d,%d,%d,%d) => (%d,%d) x (%d,%d) : (%d,%d)\n",subframe,l,i,re_offset,m,j,
                 rxptr[re_offset<<1],rxptr[1+(re_offset<<1)],
                 zptr[j],zptr[1+j],
                 rxcomp[aa][j],rxcomp[aa][1+j]);
1192
#endif
1193 1194 1195
        } //re
      } // symbol
    }  // antenna
1196

1197 1198
    // PUCCH Format 1
    // Do cfo correction and MRC across symbols
1199

1200
    if (fmt == pucch_format1) {
1201
      uci_stats->pucch1_trials++;
1202
#ifdef DEBUG_PUCCH_RX
1203
      printf("Doing PUCCH detection for format 1\n");
1204
#endif
1205 1206 1207
      stat_max = 0;

      for (phase=0; phase<7; phase++) {
1208
        int stat=0;
1209
        for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
1210
	  stat0[aa]=0;stat1[aa]=0;
1211
          for (re=0; re<12; re++) {
1212 1213 1214 1215
	    stat0_re[aa]=0;
	    stat0_im[aa]=0;
	    stat1_re[aa]=0;
	    stat1_im[aa]=0;
1216 1217 1218 1219
            off=re<<1;
            cfo =  (frame_parms->Ncp==0) ? &cfo_pucch_np[14*phase] : &cfo_pucch_ep[12*phase];

            for (l=0; l<(nsymb>>1); l++) {
1220 1221
              stat0_re[aa] += (((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15))/nsymb;
              stat0_im[aa] += (((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15))/nsymb;
1222
              off+=2;
1223
#ifdef DEBUG_PUCCH_RX
1224 1225 1226
              printf("[eNB] PUCCH subframe %d (%d,%d,%d) => (%d,%d) x (%d,%d) : (%d,%d) , stat %d\n",subframe,phase,l,re,
                     rxcomp[aa][off],rxcomp[aa][1+off],
                     cfo[l<<1],cfo[1+(l<<1)],
1227
                     stat0_re[aa],stat0_im[aa],stat);
1228
#endif
1229
            }
1230

1231
            for (l2=0,l=(nsymb>>1); l < nsymb; l++,l2++) {
1232 1233
              stat1_re[aa] += (((rxcomp[aa][off]*(int32_t)cfo[l2<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l2<<1)])>>15))/nsymb;
              stat1_im[aa] += (((rxcomp[aa][off]*(int32_t)cfo[1+(l2<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l2<<1)])>>15))/nsymb;
1234
              off+=2;
1235
#ifdef DEBUG_PUCCH_RX
1236 1237 1238
              printf("[eNB] PUCCH subframe %d (%d,%d,%d) => (%d,%d) x (%d,%d) : (%d,%d), stat %d\n",subframe,phase,l2,re,
                     rxcomp[aa][off],rxcomp[aa][1+off],
                     cfo[l2<<1],cfo[1+(l2<<1)],
1239
                     stat1_re[aa],stat1_im[aa],stat);
1240
#endif
1241
            }
1242 1243 1244
           
            stat0[aa] += ((stat0_re[aa]*stat0_re[aa]) + (stat0_im[aa]*stat0_im[aa]));
	    stat1[aa] += ((stat1_re[aa]*stat1_re[aa]) + (stat1_im[aa]*stat1_im[aa]));
1245
          } //re
1246
          stat+=(stat0[aa]+stat1[aa]);
1247 1248 1249 1250
        } // aa
        if (stat>stat_max) {
          stat_max = stat;
          phase_max = phase;
1251 1252 1253 1254
          for (aa=0;aa<frame_parms->nb_antennas_rx;aa++) {
             stat0_max[aa] = stat0[aa];
             stat1_max[aa] = stat1[aa];
          }
1255 1256
        }
      } //phase
1257

1258
      stat_max /= 12;
1259
#ifdef DEBUG_PUCCH_RX
1260
      printf("[eNB] PUCCH: stat %d, stat_max %d, phase_max %d\n", stat,stat_max,phase_max);
1261
#endif
1262
#ifdef DEBUG_PUCCH_RX
1263
      LOG_I(PHY,"[eNB] PUCCH fmt1:  stat_max : %d, sigma2_dB %d (%d, %d), phase_max : %d\n",dB_fixed(stat_max),sigma2_dB,eNB->measurements.n0_subband_power_tot_dBm[6],pucch1_thres,phase_max);
1264
#endif
1265 1266 1267 1268 1269
      eNB->pucch1_stats[UCI_id][(subframe<<10)+eNB->pucch1_stats_cnt[UCI_id][subframe]] = stat_max;
      eNB->pucch1_stats_thres[UCI_id][(subframe<<10)+eNB->pucch1_stats_cnt[UCI_id][subframe]] = sigma2_dB+pucch1_thres;
      eNB->pucch1_stats_cnt[UCI_id][subframe] = (eNB->pucch1_stats_cnt[UCI_id][subframe]+1)&1023;
      uci_stats->pucch1_thres = sigma2_dB+pucch1_thres;
      T(T_ENB_PHY_PUCCH_1_ENERGY, T_INT(eNB->Mod_id), T_INT(eNB->uci_vars[UCI_id].rnti), T_INT(frame), T_INT(subframe),
1270 1271
        T_INT(stat_max), T_INT(sigma2_dB+pucch1_thres));

1272
      
1273 1274 1275 1276 1277 1278
      /*
      if (eNB->pucch1_stats_cnt[UE_id][subframe] == 0) {
        LOG_M("pucch_debug.m","pucch_energy",
         &eNB->pucch1_stats[UE_id][(subframe<<10)],
         1024,1,2);
        AssertFatal(0,"Exiting for PUCCH 1 debug\n");
1279

1280 1281
      }
      */
1282

1283 1284 1285
      // This is a moving average of the PUCCH1 statistics conditioned on being above or below the threshold
      if (sigma2_dB<(dB_fixed(stat_max)-pucch1_thres))  {
        *payload = 1;
Raymond Knopp's avatar
Raymond Knopp committed
1286
        uci_stats->current_pucch1_stat_pos = stat_max;
1287 1288 1289 1290 1291
        for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++) {
          uci_stats->pucch1_low_stat[aa]=stat0_max[aa];
          uci_stats->pucch1_high_stat[aa]=stat1_max[aa];
          uci_stats->pucch1_positive_SR++;
        }
1292
      } else {
Raymond Knopp's avatar
Raymond Knopp committed
1293
        uci_stats->current_pucch1_stat_neg = stat_max;
1294 1295
        *payload = 0;
      }
1296

1297
      if (UCI_id==0) {
1298 1299 1300 1301 1302
        VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_SR_ENERGY,dB_fixed(stat_max));
        VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE0_SR_THRES,sigma2_dB+pucch1_thres);
      }
    } else if ((fmt == pucch_format1a)||(fmt == pucch_format1b)) {
      stat_max = 0;
1303
#ifdef DEBUG_PUCCH_RX
1304
      LOG_D(PHY,"Doing PUCCH detection for format 1a/1b\n");
1305
#endif
1306
      int stat_re=0,stat_im=0;
1307
      for (phase=0; phase<7; phase++) {
1308
        int stat=0;
1309
        for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
1310 1311
          stat0[aa]=0;
          stat1[aa]=0;
1312 1313 1314 1315
          for (re=0; re<12; re++) {
            // compute received energy first slot, seperately for data and reference
            // by coherent combining across symbols but not resource elements
            // Note: assumption is that channel is stationary over symbols in slot after CFO
1316 1317 1318 1319
            stat0_re[aa]=0;
            stat0_im[aa]=0;
            stat0_ref_re[aa]=0;
            stat0_ref_im[aa]=0;
1320 1321 1322 1323 1324
            off=re<<1;
            cfo =  (frame_parms->Ncp==0) ? &cfo_pucch_np[14*phase] : &cfo_pucch_ep[12*phase];

            for (l=0; l<(nsymb>>1); l++) {
              if ((l<2)||(l>(nsymb>>1) - 3)) {  //data symbols
1325 1326
                stat0_re[aa] += ((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15);
                stat0_im[aa] += ((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15);
1327
              } else { //reference symbols
1328 1329
                stat0_ref_re[aa] += ((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15);
                stat0_ref_im[aa] += ((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15);
1330 1331 1332 1333 1334 1335 1336 1337 1338
              }

              off+=2;
#ifdef DEBUG_PUCCH_RX
              printf("[eNB] PUCCH subframe %d (%d,%d) => (%d,%d) x (%d,%d) : (%d,%d)\n",subframe,l,re,
                     rxcomp[aa][off],rxcomp[aa][1+off],
                     cfo[l<<1],cfo[1+(l<<1)],
                     stat_re,stat_im);
#endif
1339 1340
            }

1341
            // this is total energy received, summed over data and reference
1342 1343
            stat0[aa] += ((((stat0_re[aa]*stat0_re[aa])) + ((stat0_im[aa]*stat0_im[aa])) +
                      ((stat0_ref_re[aa]*stat0_ref_re[aa])) + ((stat0_ref_im[aa]*stat0_ref_im[aa])))/nsymb);
1344
            // now second slot
1345 1346 1347 1348
            stat1_re[aa]=0;
            stat1_im[aa]=0;
            stat1_ref_re[aa]=0;
            stat1_ref_im[aa]=0;
1349 1350 1351

            for (l2=0,l=(nsymb>>1); l< nsymb; l++,l2++) {
              if ((l2<2) || ((l2>(nsymb>>1) - 3)) ) {  // data symbols
1352 1353
                stat1_re[aa] += ((rxcomp[aa][off]*(int32_t)cfo[l2<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l2<<1)])>>15);
                stat1_im[aa] += ((rxcomp[aa][off]*(int32_t)cfo[1+(l2<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l2<<1)])>>15);
1354
              } else { //reference_symbols
1355 1356
                stat1_ref_re[aa] += ((rxcomp[aa][off]*(int32_t)cfo[l2<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l2<<1)])>>15);
                stat1_ref_im[aa] += ((rxcomp[aa][off]*(int32_t)cfo[1+(l2<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l2<<1)])>>15);
1357 1358 1359
              }

              off+=2;
1360
#ifdef DEBUG_PUCCH_RX
1361 1362 1363 1364
              printf("[eNB] PUCCH subframe %d (%d,%d) => (%d,%d) x (%d,%d) : (%d,%d)\n",subframe,l2,re,
                     rxcomp[aa][off],rxcomp[aa][1+off],
                     cfo[l2<<1],cfo[1+(l2<<1)],
                     stat_re,stat_im);
1365 1366 1367
#endif
            }

1368
#ifdef DEBUG_PUCCH_RX
1369
            printf("aa%d re %d : phase %d : stat %d\n",aa,re,phase,stat);
1370
#endif
1371 1372
            stat1[aa] += ((((stat1_re[aa]*stat1_re[aa])) + ((stat1_im[aa]*stat1_im[aa])) +
                      ((stat1_ref_re[aa]*stat1_ref_re[aa])) + ((stat1_ref_im[aa]*stat1_ref_im[aa])))/nsymb);
1373
          } //re
1374
          stat+=(stat0[aa]+stat1[aa]);
1375
        } // aa
1376

1377
#ifdef DEBUG_PUCCH_RX
1378
        LOG_D(PHY,"Format 1A: phase %d : stat %d\n",phase,stat);
1379
#endif
1380
        
1381 1382 1383 1384 1385
        if (stat>stat_max) {
          stat_max = stat;
          phase_max = phase;
        }
      } //phase
1386

1387
      stat_max/=(12);  //normalize to energy per symbol and RE
1388
#ifdef DEBUG_PUCCH_RX
1389
      LOG_I(PHY,"[eNB] PUCCH fmt1a/b:  stat_max : %d (%d : sigma2 %d), phase_max : %d\n",stat_max,dB_fixed(stat_max),sigma2_dB,phase_max);
1390
#endif
1391
      // Do detection now
1392

1393 1394

      // It looks like the pucch1_thres value is a bit messy when RF is replayed.
1395 1396
      // For instance i assume to skip pucch1_thres from the test below.
      // Not 100% sure
1397
        
1398
      if (sigma2_dB<(dB_fixed(stat_max) - (IS_SOFTMODEM_IQPLAYER?0:pucch1_thres)) ) {//
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
        chL = (nsymb>>1)-4;
        chest_mag=0;
        cfo =  (frame_parms->Ncp==0) ? &cfo_pucch_np[14*phase_max] : &cfo_pucch_ep[12*phase_max];

        for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
          for (re=0; re<12; re++) {
            // channel estimate for first slot
            chest0_re[aa][re]=0;
            chest0_im[aa][re]=0;

            for (l=2; l<(nsymb>>1)-2; l++) {
              off=(re<<1) + (24*l);
              chest0_re[aa][re] += (((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15))/chL;
              chest0_im[aa][re] += (((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15))/chL;
            }
1414

1415 1416 1417
            // channel estimate for second slot
            chest1_re[aa][re]=0;
            chest1_im[aa][re]=0;
1418

1419 1420 1421 1422 1423 1424 1425 1426
            for (l=2; l<(nsymb>>1)-2; l++) {
              off=(re<<1) + (24*l) + (nsymb>>1)*24;
              chest1_re[aa][re] += (((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15))/chL;
              chest1_im[aa][re] += (((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15))/chL;
            }

            chest_mag = max(chest_mag,(chest0_re[aa][re]*chest0_re[aa][re]) + (chest0_im[aa][re]*chest0_im[aa][re]));
            chest_mag = max(chest_mag,(chest1_re[aa][re]*chest1_re[aa][re]) + (chest1_im[aa][re]*chest1_im[aa][re]));
1427
          }
1428
        }
1429

1430
        log2_maxh = log2_approx(chest_mag)/2;
1431
#ifdef DEBUG_PUCCH_RX
1432
        printf("PUCCH 1A: log2_maxh %d\n",log2_maxh);
1433 1434
#endif

1435 1436
        // now do channel matched filter
        for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
1437 1438 1439 1440 1441
          stat0_re[aa]=0;
          stat0_im[aa]=0;
          stat1_re[aa]=0;
          stat1_im[aa]=0;

1442
          for (re=0; re<12; re++) {
1443
#ifdef DEBUG_PUCCH_RX
1444 1445
            printf("[eNB] PUCCH subframe %d chest0[%d][%d] => (%d,%d)\n",subframe,aa,re,
                   chest0_re[aa][re],chest0_im[aa][re]);
1446
#endif
1447 1448 1449 1450 1451 1452

            // first slot, left of RS
            for (l=0; l<2; l++) {
              off=(re<<1) + (24*l);
              tmp_re = ((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15);
              tmp_im = ((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15);
1453 1454
              stat0_re[aa] += (((tmp_re*chest0_re[aa][re])>>log2_maxh) + ((tmp_im*chest0_im[aa][re])>>log2_maxh));
              stat0_im[aa] += (((tmp_re*chest0_im[aa][re])>>log2_maxh) - ((tmp_im*chest0_re[aa][re])>>log2_maxh));
1455
              off+=2;
1456
#ifdef DEBUG_PUCCH_RX
1457 1458 1459 1460 1461
              printf("[eNB] PUCCH subframe %d (%d,%d) => (%d,%d) x (%d,%d) : (%d,%d) => (%d,%d)\n",subframe,l,re,
                     rxcomp[aa][off],rxcomp[aa][1+off],
                     cfo[l<<1],cfo[1+(l<<1)],
                     tmp_re,tmp_im,
                     stat_re,stat_im);
1462
#endif
1463 1464 1465 1466 1467 1468 1469
            }

            // first slot, right of RS
            for (l=(nsymb>>1)-2; l<(nsymb>>1); l++) {
              off=(re<<1) + (24*l);
              tmp_re = ((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15);
              tmp_im = ((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15);
1470 1471
              stat0_re[aa] += (((tmp_re*chest0_re[aa][re])>>log2_maxh) + ((tmp_im*chest0_im[aa][re])>>log2_maxh));
              stat0_im[aa] += (((tmp_re*chest0_im[aa][re])>>log2_maxh) - ((tmp_im*chest0_re[aa][re])>>log2_maxh));
1472
              off+=2;
1473
#ifdef DEBUG_PUCCH_RX
1474 1475 1476 1477 1478
              printf("[eNB] PUCCH subframe %d (%d,%d) => (%d,%d) x (%d,%d) : (%d,%d) => (%d,%d)\n",subframe,l,re,
                     rxcomp[aa][off],rxcomp[aa][1+off],
                     cfo[l<<1],cfo[1+(l<<1)],
                     tmp_re,tmp_im,
                     stat_re,stat_im);
1479
#endif
1480
            }
1481

1482
#ifdef DEBUG_PUCCH_RX
1483 1484
            printf("[eNB] PUCCH subframe %d chest1[%d][%d] => (%d,%d)\n",subframe,aa,re,
                   chest0_re[aa][re],chest0_im[aa][re]);
1485
#endif
1486 1487 1488 1489 1490 1491

            // second slot, left of RS
            for (l=0; l<2; l++) {
              off=(re<<1) + (24*l) + (nsymb>>1)*24;
              tmp_re = ((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15);
              tmp_im = ((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15);
1492 1493
              stat1_re[aa] += (((tmp_re*chest1_re[aa][re])>>log2_maxh) + ((tmp_im*chest1_im[aa][re])>>log2_maxh));
              stat1_im[aa] += (((tmp_re*chest1_im[aa][re])>>log2_maxh) - ((tmp_im*chest1_re[aa][re])>>log2_maxh));
1494
              off+=2;
1495
#ifdef DEBUG_PUCCH_RX
1496 1497 1498 1499 1500
              printf("[PHY][eNB] PUCCH subframe %d (%d,%d) => (%d,%d) x (%d,%d) : (%d,%d) => (%d,%d)\n",subframe,l,re,
                     rxcomp[aa][off],rxcomp[aa][1+off],
                     cfo[l<<1],cfo[1+(l<<1)],
                     tmp_re,tmp_im,
                     stat_re,stat_im);
1501
#endif
1502
            }
1503

1504 1505 1506 1507 1508
            // second slot, right of RS
            for (l=(nsymb>>1)-2; l<(nsymb>>1)-1; l++) {
              off=(re<<1) + (24*l) + (nsymb>>1)*24;
              tmp_re = ((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15);
              tmp_im = ((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15);
1509 1510
              stat1_re[aa] += (((tmp_re*chest1_re[aa][re])>>log2_maxh) + ((tmp_im*chest1_im[aa][re])>>log2_maxh));
              stat1_im[aa] += (((tmp_re*chest1_im[aa][re])>>log2_maxh) - ((tmp_im*chest1_re[aa][re])>>log2_maxh));
1511
              off+=2;
1512
#ifdef DEBUG_PUCCH_RX
1513 1514 1515 1516 1517
              printf("[PHY][eNB] PUCCH subframe %d (%d,%d) => (%d,%d) x (%d,%d) : (%d,%d) => (%d,%d)\n",subframe,l,re,
                     rxcomp[aa][off],rxcomp[aa][1+off],
                     cfo[l<<1],cfo[1+(l<<1)],
                     tmp_re,tmp_im,
                     stat_re,stat_im);
1518
#endif
1519
            }
1520

1521
#ifdef DEBUG_PUCCH_RX
1522
            printf("aa%d re %d : stat %d,%d\n",aa,re,stat_re,stat_im);
1523
#endif
1524
          } //re
1525 1526
          stat_re+=stat0_re[aa]+stat1_re[aa];
          stat_im+=stat0_im[aa]+stat1_im[aa];
1527 1528 1529
        } // aa

        LOG_D(PHY,"PUCCH 1a/b: subframe %d : stat %d,%d (pos %d)\n",subframe,stat_re,stat_im,
1530
              (subframe<<10) + (eNB->pucch1ab_stats_cnt[UCI_id][subframe]));
1531
        LOG_D(PHY,"In pucch.c PUCCH 1a/b: ACK subframe %d : sigma2_dB %d, stat_max %d, pucch1_thres %d\n",subframe,sigma2_dB,dB_fixed(stat_max),pucch1_thres);
1532 1533 1534
        eNB->pucch1ab_stats[UCI_id][(subframe<<11) + 2*(eNB->pucch1ab_stats_cnt[UCI_id][subframe])] = (stat_re);
        eNB->pucch1ab_stats[UCI_id][(subframe<<11) + 1+2*(eNB->pucch1ab_stats_cnt[UCI_id][subframe])] = (stat_im);
        eNB->pucch1ab_stats_cnt[UCI_id][subframe] = (eNB->pucch1ab_stats_cnt[UCI_id][subframe]+1)&1023;
1535
        /* frame not available here - set to -1 for the moment */
1536
        T(T_ENB_PHY_PUCCH_1AB_IQ, T_INT(eNB->Mod_id), T_INT(eNB->uci_vars[UCI_id].rnti), T_INT(-1), T_INT(subframe), T_INT(stat_re), T_INT(stat_im));
1537 1538
        *payload = (stat_re<0) ? 1 : 2; // 1 == ACK, 2 == NAK

1539 1540
        if (fmt==pucch_format1b) {
          uci_stats->pucch1b_trials++;
1541
          *(1+payload) = (stat_im<0) ? 1 : 2;
1542 1543 1544 1545 1546 1547 1548 1549 1550
          uci_stats->current_pucch1b_stat_re = stat_re;
          uci_stats->current_pucch1b_stat_im = stat_im;
        }
        else {
          uci_stats->pucch1a_trials++;
          uci_stats->current_pucch1a_stat_re = stat_re;
          uci_stats->current_pucch1a_stat_im = stat_im;
        }

1551 1552 1553
      } else { // insufficient energy on PUCCH so NAK
        LOG_D(PHY,"In pucch.c PUCCH 1a/b: NAK subframe %d : sigma2_dB %d, stat_max %d, pucch1_thres %d\n",subframe,sigma2_dB,dB_fixed(stat_max),pucch1_thres);
        *payload = 4;  // DTX
1554 1555 1556
        ((int16_t *)&eNB->pucch1ab_stats[UCI_id][(subframe<<10) + (eNB->pucch1ab_stats_cnt[UCI_id][subframe])])[0] = (int16_t)(stat_re);
        ((int16_t *)&eNB->pucch1ab_stats[UCI_id][(subframe<<10) + (eNB->pucch1ab_stats_cnt[UCI_id][subframe])])[1] = (int16_t)(stat_im);
        eNB->pucch1ab_stats_cnt[UCI_id][subframe] = (eNB->pucch1ab_stats_cnt[UCI_id][subframe]+1)&1023;
1557

1558 1559
        if (fmt==pucch_format1b)
          *(1+payload) = 4;
1560
        uci_stats->pucch1ab_DTX++;
1561 1562 1563
      }
    } else {
      LOG_E(PHY,"[eNB] PUCCH fmt2/2a/2b not supported\n");
1564
    }
1565 1566

    /* PUCCH format3 >> */
Yoshi's avatar
Yoshi committed
1567 1568 1569
  } else {
    /* SubCarrier Demap */
    Ret = pucchfmt3_subCarrierDeMapping( eNB, SubCarrierDeMapData, n3_pucch );
1570

Yoshi's avatar
Yoshi committed
1571
    if(Ret != 0) {
1572 1573
      //***log pucchfmt3_subCarrierDeMapping Error!
      return(-1);
Yoshi's avatar
Yoshi committed
1574
    }
1575

Yoshi's avatar
Yoshi committed
1576 1577
    /* cyclic shift hopping remove */
    Ret = pucchfmt3_Baseseq_csh_remove( SubCarrierDeMapData, CshData_fmt3, frame_parms, subframe, eNB->ncs_cell );
1578

Yoshi's avatar
Yoshi committed
1579
    if(Ret != 0) {
1580 1581
      //***log pucchfmt3_Baseseq_csh_remove Error!
      return(-1);
Yoshi's avatar
Yoshi committed
1582
    }
1583

Yoshi's avatar
Yoshi committed
1584 1585
    /* Channel Estimation */
    Ret = pucchfmt3_ChannelEstimation( SubCarrierDeMapData, delta_theta, ChestValue, &Interpw, subframe, shortened_format, frame_parms, n3_pucch, n3_pucch_array, eNB->ncs_cell );
1586

Yoshi's avatar
Yoshi committed
1587
    if(Ret != 0) {
1588 1589
      //***log pucchfmt3_ChannelEstimation Error!
      return(-1);
Yoshi's avatar
Yoshi committed
1590
    }
1591

Yoshi's avatar
Yoshi committed
1592 1593
    /* Channel Equalization */
    Ret = pucchfmt3_Equalization( CshData_fmt3, ChdetAfterValue_fmt3, ChestValue, frame_parms );
1594

Yoshi's avatar
Yoshi committed
1595
    if(Ret != 0) {
1596 1597
      //***log pucchfmt3_Equalization Error!
      return(-1);
Yoshi's avatar
Yoshi committed
1598
    }
1599

Yoshi's avatar
Yoshi committed
1600 1601
    /* Frequency deviation remove AFC */
    Ret = pucchfmt3_FrqDevRemove( ChdetAfterValue_fmt3, delta_theta, RemoveFrqDev_fmt3, frame_parms );
1602

Yoshi's avatar
Yoshi committed
1603
    if(Ret != 0) {
1604 1605
      //***log pucchfmt3_FrqDevRemove Error!
      return(-1);
Yoshi's avatar
Yoshi committed
1606
    }
1607

Yoshi's avatar
Yoshi committed
1608 1609
    /* orthogonal sequence remove */
    Ret = pucchfmt3_OrthSeqRemove( RemoveFrqDev_fmt3, Fmt3xDataRmvOrth, shortened_format, n3_pucch, frame_parms );
1610

Yoshi's avatar
Yoshi committed
1611
    if(Ret != 0) {
1612 1613
      //***log pucchfmt3_OrthSeqRemove Error!
      return(-1);
Yoshi's avatar
Yoshi committed
1614
    }
1615

Yoshi's avatar
Yoshi committed
1616 1617 1618 1619 1620
    /* averaging antenna */
    pucchfmt3_AvgAnt( Fmt3xDataRmvOrth, Fmt3xDataAvgAnt, shortened_format, frame_parms );
    /* averaging symbol */
    pucchfmt3_AvgSym( Fmt3xDataAvgAnt, Fmt3xDataAvgSym, shortened_format );
    /* IDFT */
1621 1622
    pucchfmt3_IDft2( (int16_t *)Fmt3xDataAvgSym[0], (int16_t *)IFFTOutData_Fmt3[0] );
    pucchfmt3_IDft2( (int16_t *)Fmt3xDataAvgSym[1], (int16_t *)IFFTOutData_Fmt3[1] );
Yoshi's avatar
Yoshi committed
1623
    /* descramble */
1624
    pucchfmt3_Descramble(IFFTOutData_Fmt3, b, subframe, frame_parms->Nid_cell, crnti);
Yoshi's avatar
Yoshi committed
1625 1626 1627

    /* Is payload 6bit or 7bit? */
    if( do_sr == 1 ) {
1628
      payload_max = 7;
Yoshi's avatar
Yoshi committed
1629
    } else {
1630
      payload_max = 6;
Yoshi's avatar
Yoshi committed
1631
    }
1632

Yoshi's avatar
Yoshi committed
1633 1634
    /* decode */
    payload_entity = pucchfmt3_Decode( b, subframe, DTXthreshold, Interpw, do_sr );
1635

Yoshi's avatar
Yoshi committed
1636
    if (payload_entity == -1) {
1637 1638
      //***log pucchfmt3_Decode Error!
      return(-1);
Yoshi's avatar
Yoshi committed
1639
    }
1640

Yoshi's avatar
Yoshi committed
1641 1642 1643 1644
    for(i=0; i<payload_max; i++) {
      *(payload+i) = (uint8_t)((payload_entity>>i) & 0x01);
    }
  }
1645

1646
  /* PUCCH format3 << */
1647
  return((int32_t)stat_max);
1648
}