pucch.c 60 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 33 34
#include "PHY/defs_eNB.h"
#include "PHY/phy_extern.h" 
#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

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


Yoshi's avatar
Yoshi committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/* PUCCH format3 >> */
/* SubCarrier Demap */
uint16_t pucchfmt3_subCarrierDeMapping( PHY_VARS_eNB *eNB,
                                        int16_t SubCarrierDeMapData[NB_ANTENNAS_RX][14][12][2],
                                        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;
105
            rxptr = (int16_t *)&eNB_common_vars->rxdataF[aa][symbol_offset];
Yoshi's avatar
Yoshi committed
106 107 108 109 110 111 112 113
            
            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;
                
114
		/*                #ifdef DEBUG_PUCCH_RX
Yoshi's avatar
Yoshi committed
115 116
                    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]);
117
		    #endif*/
Yoshi's avatar
Yoshi committed
118 119 120 121 122 123 124 125 126 127 128 129 130
            }
        }
    }
    return 0;
}

/* cyclic shift hopping remove */
uint16_t pucchfmt3_Baseseq_csh_remove( int16_t SubCarrierDeMapData[NB_ANTENNAS_RX][14][12][2],
                                       int16_t CshData_fmt3[NB_ANTENNAS_RX][14][12][2],
                                       LTE_DL_FRAME_PARMS *frame_parms,
                                       uint8_t subframe,
                                       uint8_t ncs_cell[20][7] )
{
131
    //int16_t     calctmp_baSeq[2];
Yoshi's avatar
Yoshi committed
132 133 134 135 136 137
    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;
    
138
    //int32_t     NSlot1subframe  = D_NSLT1SF;
Yoshi's avatar
Yoshi committed
139 140
    int32_t     NSym1slot       = D_NSYM1SLT; // Symbol per 1slot
    int32_t     NSym1subframe   = D_NSYM1SF;  // Symbol per 1subframe
141
    int32_t     aa, symNo, slotNo, sym, k;
Yoshi's avatar
Yoshi committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 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 190 191 192 193
    
    
    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);
            }
        }
    }
    return 0;
}

#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 */
uint16_t pucchfmt3_ChannelEstimation( int16_t SubCarrierDeMapData[NB_ANTENNAS_RX][14][12][2],
                                      double delta_theta[NB_ANTENNAS_RX][12],
                                      int16_t ChestValue[NB_ANTENNAS_RX][2][12][2],
                                      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],
                                      uint8_t ncs_cell[20][7] )
{
194 195 196 197
    uint32_t        aa, symNo, k, slotNo, sym, i, j;
    int16_t         np, np_n, ip_ind;
    //int16_t         npucch_sf;
    int16_t         calctmp[2];
Yoshi's avatar
Yoshi committed
198
    int16_t         BsCshData[NB_ANTENNAS_RX][D_NSYM1SF][D_NSC1RB][2];
199 200
    //int16_t         delta_theta_calctmp[NB_ANTENNAS_RX][4][D_NSC1RB][2], delta_theta_comp[NB_ANTENNAS_RX][D_NSC1RB][2];
    int16_t         delta_theta_comp[NB_ANTENNAS_RX][D_NSC1RB][2];
Yoshi's avatar
Yoshi committed
201 202 203 204
    int16_t         CsData_allavg[NB_ANTENNAS_RX][14][2];
    int16_t         CsData_temp[NB_ANTENNAS_RX][D_NSYM1SF][D_NSC1RB][2];
    int32_t         IP_CsData_allsfavg[NB_ANTENNAS_RX][14][4][2];
    int32_t         IP_allavg[D_NPUCCH_SF5];
205
    //int16_t         temp_ch[2];
206
	int16_t         m[NUMBER_OF_UE_MAX], m_self=0, same_m_number;
Yoshi's avatar
Yoshi committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	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;
    
222 223
    //double d_theta[32]={0.0};
    //int32_t temp_theta[32][2]={0};
Yoshi's avatar
Yoshi committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    
    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;
            }
        }
    }
	
	// 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]; //
                    }
306
                    //npucch_sf = D_NPUCCH_SF4;// = 4
Yoshi's avatar
Yoshi committed
307 308 309 310 311 312 313 314
                } 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];
                    }
315
                    //npucch_sf = D_NPUCCH_SF5;// = 5
Yoshi's avatar
Yoshi committed
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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
                }
                // 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;
								}
            				}
						}
						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++;
		        		}
					}
	        	}
            }
            if(symNo > D_NSYM1SLT-1) {
                u=u1;
                v=v1;
            }
        }
    }
    
    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]);
            }
        }
    }
    
    // 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;
        }
    }
    
    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)) ;
        }
    }
    
    *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);
        }
    }
    return 0;
}

/* Channel Equalization */
uint16_t pucchfmt3_Equalization( int16_t CshData_fmt3[NB_ANTENNAS_RX][14][12][2],
                                 int16_t ChdetAfterValue_fmt3[NB_ANTENNAS_RX][14][12][2],
                                 int16_t ChestValue[NB_ANTENNAS_RX][2][12][2],
                                 LTE_DL_FRAME_PARMS *frame_parms)
{
439
    int16_t aa, sltNo, symNo, k;
Yoshi's avatar
Yoshi committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    
    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);
            }
        }
    }
    return 0;
}

/* Frequency deviation remove AFC */
uint16_t pucchfmt3_FrqDevRemove( int16_t ChdetAfterValue_fmt3[NB_ANTENNAS_RX][14][12][2],
                             double delta_theta[NB_ANTENNAS_RX][12],
                             int16_t RemoveFrqDev_fmt3[NB_ANTENNAS_RX][2][5][12][2],
                             LTE_DL_FRAME_PARMS *frame_parms )
{
462
    int16_t aa, sltNo, symNo1slt, k, n;
Yoshi's avatar
Yoshi committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    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++;
                }
            }
        }
    }
    return 0;
}

//for opt.Lev.2 
#define  MAXROW_TBL_SF5  5
#define  MAXCLM_TBL_SF5  5
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}}};

#define  MAXROW_TBL_SF4_fmt3 4
#define  MAXCLM_TBL_SF4      4
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}}};

/* orthogonal sequence remove */
uint16_t pucchfmt3_OrthSeqRemove( int16_t RemoveFrqDev_fmt3[NB_ANTENNAS_RX][2][5][12][2],
                                  int16_t Fmt3xDataRmvOrth[NB_ANTENNAS_RX][2][5][12][2],
                                  uint8_t shortened_format,
                                  uint16_t n3_pucch,
                                  LTE_DL_FRAME_PARMS *frame_parms )
{
513
    int16_t aa, sltNo, n, k;
Yoshi's avatar
Yoshi committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    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;
                }
            }
            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);
                    }
                }
            }
        }
    }
    return 0;
}

/* averaging antenna */
uint16_t pucchfmt3_AvgAnt( int16_t Fmt3xDataRmvOrth[NB_ANTENNAS_RX][2][5][12][2],
                           int16_t Fmt3xDataAvgAnt[2][5][12][2],
                           uint8_t shortened_format,
                           LTE_DL_FRAME_PARMS *frame_parms )
{
558
    int16_t aa, sltNo, n, k;
Yoshi's avatar
Yoshi committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    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;
                }
            }
        }
    }
    return 0;
}

/* averaging symbol */
uint16_t pucchfmt3_AvgSym( int16_t Fmt3xDataAvgAnt[2][5][12][2],
                           int16_t Fmt3xDataAvgSym[2][12][2],
                           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;
        }
        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;
}

/* iDFT */
void pucchfmt3_IDft2( int16_t *x, int16_t *y )
{
  int16_t i, k;
  int16_t tmp[2];
  int16_t calctmp[D_NSC1RB*2]={0};
  
  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);
    }
    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
                              )
{
    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++;
        }
    }
    return 0;
}

int16_t pucchfmt3_Decode( int16_t b[48],
                          uint8_t subframe,
                          int16_t DTXthreshold,
                          int16_t Interpw,
                          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++) {
        Rho_tmp = 0;
        for (i=0;i<48;i++) {
            Rho_tmp += b[i] * (1-2*chcod_tbl[c][i]);
        }
        if (Rho_tmp > Rho_max) {
            c_max = c;
            Rho_max = Rho_tmp;
        }
    }
698
    if(Interpw<1){
Yoshi's avatar
Yoshi committed
699 700 701 702 703 704 705 706 707 708 709 710 711
      Interpw=1;
    }
    if((Rho_max/Interpw) > DTXthreshold) {
        // ***Log
        return c_max;
    } else {
        // ***Log
        return -1;
    }
}

/* PUCCH format3 << */

712
uint32_t rx_pucch(PHY_VARS_eNB *eNB,
713 714 715 716 717 718
		  PUCCH_FMT_t fmt,
		  uint8_t UE_id,
		  uint16_t n1_pucch,
		  uint16_t n2_pucch,
		  uint8_t shortened_format,
		  uint8_t *payload,
719
		  int     frame,
720
		  uint8_t subframe,
sharma's avatar
sharma committed
721
		  uint8_t pucch1_thres
nepes's avatar
nepes committed
722
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0)) 
sharma's avatar
sharma committed
723 724
		  ,uint8_t br_flag
#endif
725 726
)
//-----------------------------------------------------------------------------
727
{
728 729 730 731
  static int first_call = 1;
  LTE_eNB_COMMON *common_vars = &eNB->common_vars;
  LTE_DL_FRAME_PARMS *frame_parms = &eNB->frame_parms;
  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]);
732

733 734 735 736 737 738 739 740 741 742 743 744 745 746
  uint32_t u,v,n,aa;
  uint32_t z[12*14];
  int16_t *zptr;
  int16_t rxcomp[NB_ANTENNAS_RX][2*12*14];
  uint8_t ns,N_UL_symb,nsymb,n_oc,n_oc0,n_oc1;
  uint8_t c = (frame_parms->Ncp==0) ? 3 : 2;
  uint16_t nprime,nprime0,nprime1;
  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;
747 748 749 750
  int16_t stat_ref_re,stat_ref_im,*cfo;
  int16_t chest0_re[NB_ANTENNAS_RX][12],chest0_im[NB_ANTENNAS_RX][12];
  int16_t chest1_re[NB_ANTENNAS_RX][12],chest1_im[NB_ANTENNAS_RX][12];
  int32_t chest_mag;
751
  int32_t stat_re=0,stat_im=0;
752
  uint32_t stat,stat_max=0;
753
  uint8_t log2_maxh;
754 755 756 757 758 759 760 761 762

  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;

  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)];
763
  int chL;
764

Yoshi's avatar
Yoshi committed
765 766 767 768
  /* PUCCH format3 >> */
  uint16_t Ret = 0;
  int16_t SubCarrierDeMapData[NB_ANTENNAS_RX][14][12][2];       //[Antenna][Symbol][Subcarrier][Complex]
  int16_t CshData_fmt3[NB_ANTENNAS_RX][14][12][2];              //[Antenna][Symbol][Subcarrier][Complex]
769
  double delta_theta[NB_ANTENNAS_RX][12];                       //[Antenna][Subcarrier][Complex]
Yoshi's avatar
Yoshi committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
  int16_t ChestValue[NB_ANTENNAS_RX][2][12][2];                 //[Antenna][Slot][Subcarrier][Complex]
  int16_t ChdetAfterValue_fmt3[NB_ANTENNAS_RX][14][12][2];      //[Antenna][Symbol][Subcarrier][Complex]
  int16_t RemoveFrqDev_fmt3[NB_ANTENNAS_RX][2][5][12][2];       //[Antenna][Slot][PUCCH_Symbol][Subcarrier][Complex]
  int16_t Fmt3xDataRmvOrth[NB_ANTENNAS_RX][2][5][12][2];        //[Antenna][Slot][PUCCH_Symbol][Subcarrier][Complex]
  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;
Yoshi's avatar
Yoshi committed
785 786
  uint16_t n3_pucch_array[NUMBER_OF_UE_MAX]={1};
  n3_pucch_array[0]=n3_pucch;
Yoshi's avatar
Yoshi committed
787
  uint8_t do_sr = 1;
788
  uint16_t crnti=0x1234;
Yoshi's avatar
Yoshi committed
789
  int16_t DTXthreshold = 10;
Yoshi's avatar
Yoshi committed
790 791
  /* PUCCH format3 << */

792 793 794
  if (first_call == 1) {
    for (i=0;i<10;i++) {
      for (j=0;j<NUMBER_OF_UE_MAX;j++) {
795 796
	eNB->pucch1_stats_cnt[j][i]=0;
	eNB->pucch1ab_stats_cnt[j][i]=0;
bruno mongazon's avatar
bruno mongazon committed
797
#if defined(USRP_REC_PLAY) // not 100% sure
798
	eNB->pucch1_stats_thres[j][i]=0;
bruno mongazon's avatar
bruno mongazon committed
799
#endif	
800 801 802 803
      }
    }
    first_call=0;
  }
804

805

Yoshi's avatar
Yoshi committed
806
  if(fmt!=pucch_format3) {  /* PUCCH format3 */
Yoshi's avatar
Yoshi committed
807 808
  
  // TODO
Yoshi's avatar
Yoshi committed
809 810
  // "SR+ACK/NACK" length is only 7 bits.
  // This restriction will be lifted in the future.
Yoshi's avatar
Yoshi committed
811
  // "CQI/PMI/RI+ACK/NACK" will be supported in the future.
Yoshi's avatar
Yoshi committed
812

813 814 815 816 817 818 819 820 821 822
  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);
  }

  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);
  }

823
  zptr = (int16_t *)z;
824 825 826 827 828
  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;

#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
829
  printf("[eNB] PUCCH: cNcs1/deltaPUCCH_Shift %d, Nprime %d, n1_pucch %d\n",thres,Nprime,n1_pucch);
830 831
#endif

832
  N_UL_symb = (frame_parms->Ncp==NORMAL) ? 7 : 6;
833 834 835 836

  if (n1_pucch < thres)
    nprime0=n1_pucch;
  else
837 838
    nprime0 = (n1_pucch - thres)%(12*c/deltaPUCCH_Shift);

839 840 841 842 843
  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);
844
    nprime1 = (h/c) + (h%c)*Nprime_div_deltaPUCCH_Shift;
845 846 847
  }

#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
848
  printf("PUCCH: nprime0 %d nprime1 %d\n",nprime0,nprime1);
849 850 851
#endif

  n_oc0 = nprime0/Nprime_div_deltaPUCCH_Shift;
852

853 854 855 856
  if (frame_parms->Ncp==1)
    n_oc0<<=1;

  n_oc1 = nprime1/Nprime_div_deltaPUCCH_Shift;
857

858 859 860 861
  if (frame_parms->Ncp==1)  // extended CP
    n_oc1<<=1;

#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
862
  printf("[eNB] PUCCH: noc0 %d noc11 %d\n",n_oc0,n_oc1);
863 864 865 866 867 868
#endif

  nprime=nprime0;
  n_oc  =n_oc0;

  // loop over 2 slots
869
  for (ns=(subframe<<1),u=u0,v=v0; ns<(2+(subframe<<1)); ns++,u=u1,v=v1) {
870 871 872 873 874

    if ((nprime&1) == 0)
      S=0;  // 1
    else
      S=1;  // j
875 876 877 878 879 880
    /*
    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);
    */
881 882

    //loop over symbols in slot
883
    for (l=0; l<N_UL_symb; l++) {
884
      // Compute n_cs (36.211 p. 18)
885
      n_cs = eNB->ncs_cell[ns][l];
886

887
      if (frame_parms->Ncp==0) { // normal CP
888 889 890
        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;
891 892 893
      }


894

895
      refs=0;
896

897 898 899
      // Comput W_noc(m) (36.211 p. 19)
      if ((ns==(1+(subframe<<1))) && (shortened_format==1)) {  // second slot and shortened format

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
        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;
919
        } else if ((l<N_UL_symb-2)&&(frame_parms->Ncp==NORMAL)) { // reference and normal CP
920 921 922
          W_re=W3_re[n_oc][l-2];
          W_im=W3_im[n_oc][l-2];
          refs=1;
923
        } else if ((l<N_UL_symb-2)&&(frame_parms->Ncp==EXTENDED)) { // reference and extended CP
924 925 926 927 928 929 930
          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;
        }
931 932
      }

933
      // multiply W by S(ns) (36.211 p.17). only for data, reference symbols do not have this factor
934
      if ((S==1)&&(refs==0)) {
935 936 937
        tmp_re = W_re;
        W_re = -W_im;
        W_im = tmp_re;
938 939 940
      }

#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
941
      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);
942 943
#endif
      alpha_ind=0;
944
      // compute output sequence
945

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

948 949 950
        // 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);
951

952 953 954
        // 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;
955 956

#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
957
        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],
958
              alpha_ind,alpha_re[alpha_ind],alpha_im[alpha_ind]);
959
#endif
960

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

964 965 966
      zptr+=24;
    } // l

967 968
    nprime=nprime1;
    n_oc  =n_oc1;
969 970 971 972 973 974 975
  } // ns

  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);

#ifdef DEBUG_PUCCH_RX
976
  printf("[eNB] PUCCH: m %d, thres %d, NRB2 %d\n",m,thres,NRB2);
977 978 979
#endif
  nsymb = N_UL_symb<<1;

980
  zptr = (int16_t*)z;
981 982

  // Do detection
983 984
  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {

985
    //for (j=0,l=0;l<(nsymb-1);l++) {
986
    for (j=0,l=0; l<nsymb; l++) {
nepes's avatar
nepes committed
987
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
sharma's avatar
sharma committed
988 989 990 991 992 993
      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;
      }
994
      else
sharma's avatar
sharma committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005
#endif
      {
        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;
      }
1006

1007
      if (re_offset > frame_parms->ofdm_symbol_size)
1008 1009
        re_offset -= (frame_parms->ofdm_symbol_size);

1010
      symbol_offset = (unsigned int)frame_parms->ofdm_symbol_size*l;
1011
      rxptr = (int16_t *)&common_vars->rxdataF[aa][symbol_offset];
1012 1013 1014 1015 1016

      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
1017 1018 1019
        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);

1020
#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1021
        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,
1022
              rxptr[re_offset<<1],rxptr[1+(re_offset<<1)],
1023 1024
              zptr[j],zptr[1+j],
              rxcomp[aa][j],rxcomp[aa][1+j]);
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
#endif
      } //re
    } // symbol
  }  // antenna


  // PUCCH Format 1
  // Do cfo correction and MRC across symbols

  if (fmt == pucch_format1) {
#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1036
    printf("Doing PUCCH detection for format 1\n");
1037 1038 1039
#endif

    stat_max = 0;
1040 1041 1042


    for (phase=0; phase<7; phase++) {
1043
      stat=0;
1044 1045 1046 1047 1048 1049 1050 1051 1052

      for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
        for (re=0; re<12; re++) {
          stat_re=0;
          stat_im=0;
          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++) {
1053 1054
            stat_re += (((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15))/nsymb;
            stat_im += (((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15))/nsymb;
1055
            off+=2;
1056 1057

		    
1058
#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1059
            printf("[eNB] PUCCH subframe %d (%d,%d,%d) => (%d,%d) x (%d,%d) : (%d,%d) , stat %d\n",subframe,phase,l,re,
1060 1061
                  rxcomp[aa][off],rxcomp[aa][1+off],
                  cfo[l<<1],cfo[1+(l<<1)],
1062
                  stat_re,stat_im,stat);
1063 1064 1065
#endif
          }

Cedric Roux's avatar
Cedric Roux committed
1066
          for (l2=0,l=(nsymb>>1); l < nsymb; l++,l2++) {
1067 1068
            stat_re += (((rxcomp[aa][off]*(int32_t)cfo[l2<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l2<<1)])>>15))/nsymb;
            stat_im += (((rxcomp[aa][off]*(int32_t)cfo[1+(l2<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l2<<1)])>>15))/nsymb;
1069
            off+=2;
1070

1071
#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1072
            printf("[eNB] PUCCH subframe %d (%d,%d,%d) => (%d,%d) x (%d,%d) : (%d,%d), stat %d\n",subframe,phase,l2,re,
1073 1074
                  rxcomp[aa][off],rxcomp[aa][1+off],
                  cfo[l2<<1],cfo[1+(l2<<1)],
1075
                  stat_re,stat_im,stat);
1076 1077
#endif

1078

1079
          }
1080 1081
	  stat += ((stat_re*stat_re) + (stat_im*stat_im));

1082
       } //re
1083 1084
      } // aa

1085
 
1086
      if (stat>stat_max) {
1087 1088
        stat_max = stat;
        phase_max = phase;
1089
      }
1090

1091 1092
    } //phase

Cedric Roux's avatar
Cedric Roux committed
1093
//    stat_max *= nsymb;  // normalize to energy per symbol
1094
//    stat_max /= (frame_parms->N_RB_UL*12); //
Cedric Roux's avatar
Cedric Roux committed
1095
    stat_max /= 12;
1096
    
1097
#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1098
    printf("[eNB] PUCCH: stat %d, stat_max %d, phase_max %d\n", stat,stat_max,phase_max);
1099
#endif
1100 1101

#ifdef DEBUG_PUCCH_RX
sharma's avatar
sharma committed
1102
    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);
1103
#endif
1104

1105
    
1106 1107 1108
    eNB->pucch1_stats[UE_id][(subframe<<10)+eNB->pucch1_stats_cnt[UE_id][subframe]] = stat_max;
    eNB->pucch1_stats_thres[UE_id][(subframe<<10)+eNB->pucch1_stats_cnt[UE_id][subframe]] = sigma2_dB+pucch1_thres;
    eNB->pucch1_stats_cnt[UE_id][subframe] = (eNB->pucch1_stats_cnt[UE_id][subframe]+1)&1023;
1109

Cedric Roux's avatar
Cedric Roux committed
1110
    T(T_ENB_PHY_PUCCH_1_ENERGY, T_INT(eNB->Mod_id), T_INT(eNB->uci_vars[UE_id].rnti), T_INT(frame), T_INT(subframe),
1111 1112
      T_INT(stat_max), T_INT(sigma2_dB+pucch1_thres));

1113
    /*
1114
    if (eNB->pucch1_stats_cnt[UE_id][subframe] == 0) {
bruno mongazon's avatar
bruno mongazon committed
1115
      LOG_M("pucch_debug.m","pucch_energy",
1116
		   &eNB->pucch1_stats[UE_id][(subframe<<10)],
1117 1118 1119 1120 1121 1122
		   1024,1,2);
      AssertFatal(0,"Exiting for PUCCH 1 debug\n");

    }
    */

1123 1124
    // 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))  {
1125
      *payload = 1;
1126 1127
    }
    else {
1128
      *payload = 0;
1129
    }
1130 1131 1132 1133
    if (UE_id==0) {
      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);
    }
1134
  } else if ((fmt == pucch_format1a)||(fmt == pucch_format1b)) {
1135 1136
    stat_max = 0;
#ifdef DEBUG_PUCCH_RX
1137
    LOG_D(PHY,"Doing PUCCH detection for format 1a/1b\n");
1138
#endif
1139

Cedric Roux's avatar
Cedric Roux committed
1140
    for (phase=0; phase<7; phase++) {
1141
      stat=0;
1142 1143 1144

      for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
        for (re=0; re<12; re++) {
1145 1146 1147 1148

	  // 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
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
          stat_re=0;
          stat_im=0;
          stat_ref_re=0;
          stat_ref_im=0;
          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
              stat_re += ((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15);
              stat_im += ((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15);
            } else { //reference symbols
              stat_ref_re += ((rxcomp[aa][off]*(int32_t)cfo[l<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l<<1)])>>15);
              stat_ref_im += ((rxcomp[aa][off]*(int32_t)cfo[1+(l<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l<<1)])>>15);
            }

            off+=2;
1167
#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1168
            printf("[eNB] PUCCH subframe %d (%d,%d) => (%d,%d) x (%d,%d) : (%d,%d)\n",subframe,l,re,
1169 1170 1171 1172 1173
                  rxcomp[aa][off],rxcomp[aa][1+off],
                  cfo[l<<1],cfo[1+(l<<1)],
                  stat_re,stat_im);
#endif
          }
1174 1175 1176
	  // this is total energy received, summed over data and reference
	  stat += ((((stat_re*stat_re)) + ((stat_im*stat_im)) +
		    ((stat_ref_re*stat_ref_re)) + ((stat_ref_im*stat_ref_im)))/nsymb);
1177

1178 1179 1180 1181 1182
	  // now second slot
	  stat_re=0;
	  stat_im=0;
          stat_ref_re=0;
          stat_ref_im=0;
1183

Cedric Roux's avatar
Cedric Roux committed
1184
          for (l2=0,l=(nsymb>>1); l< nsymb; l++,l2++) {
1185 1186 1187 1188
            if ((l2<2) || ((l2>(nsymb>>1) - 3)) ) {  // data symbols
              stat_re += ((rxcomp[aa][off]*(int32_t)cfo[l2<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l2<<1)])>>15);
              stat_im += ((rxcomp[aa][off]*(int32_t)cfo[1+(l2<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l2<<1)])>>15);
            } else { //reference_symbols
1189 1190
              stat_ref_re += ((rxcomp[aa][off]*(int32_t)cfo[l2<<1])>>15)     - ((rxcomp[aa][1+off]*(int32_t)cfo[1+(l2<<1)])>>15);
              stat_ref_im += ((rxcomp[aa][off]*(int32_t)cfo[1+(l2<<1)])>>15) + ((rxcomp[aa][1+off]*(int32_t)cfo[(l2<<1)])>>15);
1191 1192 1193
            }

            off+=2;
1194
#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1195
            printf("[eNB] PUCCH subframe %d (%d,%d) => (%d,%d) x (%d,%d) : (%d,%d)\n",subframe,l2,re,
1196 1197 1198 1199 1200 1201 1202
                  rxcomp[aa][off],rxcomp[aa][1+off],
                  cfo[l2<<1],cfo[1+(l2<<1)],
                  stat_re,stat_im);
#endif

          }

1203
#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1204
          printf("aa%d re %d : phase %d : stat %d\n",aa,re,phase,stat);
1205
#endif
1206 1207 1208 1209 1210

	  stat += ((((stat_re*stat_re)) + ((stat_im*stat_im)) +
		    ((stat_ref_re*stat_ref_re)) + ((stat_ref_im*stat_ref_im)))/nsymb);


1211
        } //re
1212
      } // aa
1213 1214

#ifdef DEBUG_PUCCH_RX
1215
      LOG_D(PHY,"Format 1A: phase %d : stat %d\n",phase,stat);
1216
#endif
1217
      if (stat>stat_max) {
1218 1219
        stat_max = stat;
        phase_max = phase;
1220 1221
      }
    } //phase
1222

1223
    stat_max/=(12);  //normalize to energy per symbol and RE
nepes's avatar
nepes committed
1224
#ifdef DEBUG_PUCCH_RX
nepes's avatar
nepes committed
1225
    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);
nepes's avatar
nepes committed
1226
#endif
1227 1228 1229

    stat_re=0;
    stat_im=0;
1230

1231
    // Do detection now
1232 1233
#if defined(USRP_REC_PLAY)
    // It looks like the value is a bit messy when RF is replayed.
1234
    // For instance i assume to skip pucch1_thres from the test below.
bruno mongazon's avatar
bruno mongazon committed
1235
    // Not 100% sure
1236
    if (sigma2_dB<(dB_fixed(stat_max)))  {//
1237
#else
1238
    if (sigma2_dB<(dB_fixed(stat_max)-pucch1_thres))  {//
1239
#endif
1240
      chL = (nsymb>>1)-4;
1241 1242
      chest_mag=0;
      cfo =  (frame_parms->Ncp==0) ? &cfo_pucch_np[14*phase_max] : &cfo_pucch_ep[12*phase_max];
1243

1244
      for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
1245
	// channel estimates first
1246 1247 1248
        for (re=0; re<12; re++) {

          // channel estimate for first slot
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
          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;
	  }	    
	  

          // channel estimate for second slot
	  chest1_re[aa][re]=0;
	  chest1_im[aa][re]=0;
1261
          for (l=2; l<(nsymb>>1)-2; l++) {
1262 1263 1264
            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;
1265
          }
1266 1267
	  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]));
1268

1269 1270 1271
	}
      }
      log2_maxh = log2_approx(chest_mag)/2;
1272
#ifdef DEBUG_PUCCH_RX
1273
      printf("PUCCH 1A: log2_maxh %d\n",log2_maxh);
1274
#endif
1275 1276 1277
	// now do channel matched filter
      for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
	for (re=0; re<12; re++) {
1278

1279 1280 1281 1282 1283
#ifdef DEBUG_PUCCH_RX
          printf("[eNB] PUCCH subframe %d chest0[%d][%d] => (%d,%d)\n",subframe,aa,re,
                chest0_re[aa][re],chest0_im[aa][re]);
#endif
	  // first slot, left of RS    
1284 1285 1286 1287
          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);
1288 1289
            stat_re += (((tmp_re*chest0_re[aa][re])>>log2_maxh) + ((tmp_im*chest0_im[aa][re])>>log2_maxh));
            stat_im += (((tmp_re*chest0_im[aa][re])>>log2_maxh) - ((tmp_im*chest0_re[aa][re])>>log2_maxh));
1290
            off+=2;
1291
#ifdef DEBUG_PUCCH_RX
1292 1293 1294 1295 1296
            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);
1297
#endif
1298 1299
	  }
	  // first slot, right of RS
1300 1301 1302 1303
          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);
1304 1305
            stat_re += (((tmp_re*chest0_re[aa][re])>>log2_maxh) + ((tmp_im*chest0_im[aa][re])>>log2_maxh));
            stat_im += (((tmp_re*chest0_im[aa][re])>>log2_maxh) - ((tmp_im*chest0_re[aa][re])>>log2_maxh));
1306
            off+=2;
1307
#ifdef DEBUG_PUCCH_RX
1308 1309 1310 1311 1312
            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);
1313
#endif
1314 1315
	  }
	  
sharma's avatar
sharma committed
1316
	  
1317

1318
#ifdef DEBUG_PUCCH_RX
1319 1320
          printf("[eNB] PUCCH subframe %d chest1[%d][%d] => (%d,%d)\n",subframe,aa,re,
                chest0_re[aa][re],chest0_im[aa][re]);
1321
#endif
1322
	  // second slot, left of RS    	  
1323
          for (l=0; l<2; l++) {
1324 1325 1326 1327 1328
	    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);
	    stat_re += (((tmp_re*chest1_re[aa][re])>>log2_maxh) + ((tmp_im*chest1_im[aa][re])>>log2_maxh));
            stat_im += (((tmp_re*chest1_im[aa][re])>>log2_maxh) - ((tmp_im*chest1_re[aa][re])>>log2_maxh));
1329
            off+=2;
1330
#ifdef DEBUG_PUCCH_RX
1331 1332 1333 1334 1335
            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);
1336 1337 1338
#endif
          }

1339
	  // second slot, right of RS    	  
1340 1341 1342 1343
          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);
1344 1345
            stat_re += (((tmp_re*chest1_re[aa][re])>>log2_maxh) + ((tmp_im*chest1_im[aa][re])>>log2_maxh));
            stat_im += (((tmp_re*chest1_im[aa][re])>>log2_maxh) - ((tmp_im*chest1_re[aa][re])>>log2_maxh));
1346
            off+=2;
1347
#ifdef DEBUG_PUCCH_RX
1348 1349 1350 1351 1352
            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);
1353 1354 1355
#endif
          }

1356
#ifdef DEBUG_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1357
          printf("aa%d re %d : stat %d,%d\n",aa,re,stat_re,stat_im);
1358
#endif
1359 1360

        } //re
1361
      } // aa
1362

1363

Cedric Roux's avatar
fix  
Cedric Roux committed
1364
      LOG_D(PHY,"PUCCH 1a/b: subframe %d : stat %d,%d (pos %d)\n",subframe,stat_re,stat_im,
1365
	    (subframe<<10) + (eNB->pucch1ab_stats_cnt[UE_id][subframe]));
nepes's avatar
nepes committed
1366
      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);
1367 1368 1369 1370
      
      eNB->pucch1ab_stats[UE_id][(subframe<<11) + 2*(eNB->pucch1ab_stats_cnt[UE_id][subframe])] = (stat_re);
      eNB->pucch1ab_stats[UE_id][(subframe<<11) + 1+2*(eNB->pucch1ab_stats_cnt[UE_id][subframe])] = (stat_im);
      eNB->pucch1ab_stats_cnt[UE_id][subframe] = (eNB->pucch1ab_stats_cnt[UE_id][subframe]+1)&1023;
1371

1372
      /* frame not available here - set to -1 for the moment */
Cedric Roux's avatar
Cedric Roux committed
1373
      T(T_ENB_PHY_PUCCH_1AB_IQ, T_INT(eNB->Mod_id), T_INT(eNB->uci_vars[UE_id].rnti), T_INT(-1), T_INT(subframe), T_INT(stat_re), T_INT(stat_im));
1374

Raymond Knopp's avatar
Raymond Knopp committed
1375
      *payload = (stat_re<0) ? 1 : 2; // 1 == ACK, 2 == NAK
1376

masayuki.harada's avatar
masayuki.harada committed
1377 1378 1379 1380 1381 1382
      if (fmt==pucch_format1b){
        *payload = (stat_im > stat_re) ? 1 : 2;
        *(1+payload) = (stat_im > (-1.0*stat_re)) ? 2 : 1;
        LOG_D(PHY,"PUCCH 1b ACK/NAK subframe %d : stat %d, stat %d %d, payload %d %d\n",
          subframe,dB_fixed(stat_max),stat_re,stat_im,*payload,*(1+payload));
      }
1383
    } else { // insufficient energy on PUCCH so NAK
1384
#if defined(USRP_REC_PLAY)
Cedric Roux's avatar
Cedric Roux committed
1385
      LOG_D(PHY,"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);
1386
#else
nepes's avatar
nepes committed
1387
      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);
1388 1389
#endif

1390
      *payload = 4;  // DTX
1391 1392 1393
      ((int16_t*)&eNB->pucch1ab_stats[UE_id][(subframe<<10) + (eNB->pucch1ab_stats_cnt[UE_id][subframe])])[0] = (int16_t)(stat_re);
      ((int16_t*)&eNB->pucch1ab_stats[UE_id][(subframe<<10) + (eNB->pucch1ab_stats_cnt[UE_id][subframe])])[1] = (int16_t)(stat_im);
      eNB->pucch1ab_stats_cnt[UE_id][subframe] = (eNB->pucch1ab_stats_cnt[UE_id][subframe]+1)&1023;
1394 1395

      if (fmt==pucch_format1b)
Cedric Roux's avatar
Cedric Roux committed
1396
        *(1+payload) = 4;
1397
    }
1398
  } else {
1399 1400
    LOG_E(PHY,"[eNB] PUCCH fmt2/2a/2b not supported\n");
  }
Yoshi's avatar
Yoshi committed
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
  
  /* PUCCH format3 >> */
  } else {
    /* SubCarrier Demap */
    Ret = pucchfmt3_subCarrierDeMapping( eNB, SubCarrierDeMapData, n3_pucch );
    if(Ret != 0) {
        //***log pucchfmt3_subCarrierDeMapping Error!
        return(-1);
    }
    
    /* cyclic shift hopping remove */
    Ret = pucchfmt3_Baseseq_csh_remove( SubCarrierDeMapData, CshData_fmt3, frame_parms, subframe, eNB->ncs_cell );
    if(Ret != 0) {
        //***log pucchfmt3_Baseseq_csh_remove Error!
        return(-1);
    }
    
    /* Channel Estimation */
    Ret = pucchfmt3_ChannelEstimation( SubCarrierDeMapData, delta_theta, ChestValue, &Interpw, subframe, shortened_format, frame_parms, n3_pucch, n3_pucch_array, eNB->ncs_cell );
    if(Ret != 0) {
        //***log pucchfmt3_ChannelEstimation Error!
        return(-1);
    }
    
    /* Channel Equalization */
    Ret = pucchfmt3_Equalization( CshData_fmt3, ChdetAfterValue_fmt3, ChestValue, frame_parms );
    if(Ret != 0) {
        //***log pucchfmt3_Equalization Error!
        return(-1);
    }
    
    /* Frequency deviation remove AFC */
    Ret = pucchfmt3_FrqDevRemove( ChdetAfterValue_fmt3, delta_theta, RemoveFrqDev_fmt3, frame_parms );
    if(Ret != 0) {
        //***log pucchfmt3_FrqDevRemove Error!
        return(-1);
    }
    
    /* orthogonal sequence remove */
    Ret = pucchfmt3_OrthSeqRemove( RemoveFrqDev_fmt3, Fmt3xDataRmvOrth, shortened_format, n3_pucch, frame_parms );
    if(Ret != 0) {
        //***log pucchfmt3_OrthSeqRemove Error!
        return(-1);
    }
    
    /* averaging antenna */
    pucchfmt3_AvgAnt( Fmt3xDataRmvOrth, Fmt3xDataAvgAnt, shortened_format, frame_parms );
    
    /* averaging symbol */
    pucchfmt3_AvgSym( Fmt3xDataAvgAnt, Fmt3xDataAvgSym, shortened_format );
    
    /* IDFT */
    pucchfmt3_IDft2( (int16_t*)Fmt3xDataAvgSym[0], (int16_t*)IFFTOutData_Fmt3[0] );
    pucchfmt3_IDft2( (int16_t*)Fmt3xDataAvgSym[1], (int16_t*)IFFTOutData_Fmt3[1] );
    
    /* descramble */
1457
    pucchfmt3_Descramble(IFFTOutData_Fmt3, b, subframe, frame_parms->Nid_cell, crnti);
Yoshi's avatar
Yoshi committed
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

    /* Is payload 6bit or 7bit? */
    if( do_sr == 1 ) {
        payload_max = 7;
    } else {
        payload_max = 6;
    }
    
    /* decode */
    payload_entity = pucchfmt3_Decode( b, subframe, DTXthreshold, Interpw, do_sr );
    if (payload_entity == -1) {
        //***log pucchfmt3_Decode Error!
        return(-1);
    }
    
    for(i=0; i<payload_max; i++) {
      *(payload+i) = (uint8_t)((payload_entity>>i) & 0x01);
    }
  }
  /* PUCCH format3 << */
1478

1479
  return((int32_t)stat_max);
1480 1481

}