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

/*! \file PHY/NR_TRANSPORT/pucch_rx.c
Raymond Knopp's avatar
Raymond Knopp committed
23 24 25 26 27 28 29 30 31
 * \brief Top-level routines for decoding the PUCCH physical channel
 * \author A. Mico Pereperez, Padarthi Naga Prasanth, Francesco Mani, Raymond Knopp
 * \date 2020
 * \version 0.2
 * \company Eurecom
 * \email:
 * \note
 * \warning
 */
32 33 34 35 36 37 38 39 40 41
#include<stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include "PHY/impl_defs_nr.h"
#include "PHY/defs_nr_common.h"
42 43
#include "PHY/defs_gNB.h"
#include "PHY/sse_intrin.h"
44
#include "PHY/NR_UE_TRANSPORT/pucch_nr.h"
laurent's avatar
laurent committed
45
#include <openair1/PHY/CODING/nrSmallBlock/nr_small_block_defs.h>
Francesco Mani's avatar
Francesco Mani committed
46
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
47
#include "PHY/NR_REFSIG/nr_refsig.h"
48 49 50
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"

51 52
#include "nfapi/oai_integration/vendor_ext.h"

53 54
#include "T.h"

55
//#define DEBUG_NR_PUCCH_RX 1
56

Francesco Mani's avatar
Francesco Mani committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
NR_gNB_PUCCH_t *new_gNB_pucch(void){
    NR_gNB_PUCCH_t *pucch;
    pucch = (NR_gNB_PUCCH_t *)malloc16(sizeof(NR_gNB_PUCCH_t));
    pucch->active = 0;
    return (pucch);
}

int nr_find_pucch(uint16_t rnti,
                  int frame,
                  int slot,
                  PHY_VARS_gNB *gNB) {

  AssertFatal(gNB!=NULL,"gNB is null\n");
  int index = -1;

72
  for (int i=0; i<NUMBER_OF_NR_PUCCH_MAX; i++) {
Francesco Mani's avatar
Francesco Mani committed
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
    AssertFatal(gNB->pucch[i]!=NULL,"gNB->pucch[%d] is null\n",i);
    if ((gNB->pucch[i]->active >0) &&
        (gNB->pucch[i]->pucch_pdu.rnti==rnti) &&
        (gNB->pucch[i]->frame==frame) &&
        (gNB->pucch[i]->slot==slot)) return(i);
    else if ((gNB->pucch[i]->active == 0) && (index==-1)) index=i;
  }

  if (index==-1)
    LOG_E(MAC,"PUCCH list is full\n");

  return(index);
}

void nr_fill_pucch(PHY_VARS_gNB *gNB,
                   int frame,
                   int slot,
                   nfapi_nr_pucch_pdu_t *pucch_pdu) {

  int id = nr_find_pucch(pucch_pdu->rnti,frame,slot,gNB);
  AssertFatal( (id>=0) && (id<NUMBER_OF_NR_PUCCH_MAX),
              "invalid id found for pucch !!! rnti %04x id %d\n",pucch_pdu->rnti,id);

  NR_gNB_PUCCH_t  *pucch = gNB->pucch[id];
  pucch->frame = frame;
  pucch->slot = slot;
  pucch->active = 1;
  memcpy((void*)&pucch->pucch_pdu, (void*)pucch_pdu, sizeof(nfapi_nr_pucch_pdu_t));
}


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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
int get_pucch0_cs_lut_index(PHY_VARS_gNB *gNB,nfapi_nr_pucch_pdu_t* pucch_pdu) {

  int i=0;

#ifdef DEBUG_NR_PUCCH_RX
  printf("getting index for LUT with %d entries, Nid %d\n",gNB->pucch0_lut.nb_id, pucch_pdu->hopping_id);
#endif

  for (i=0;i<gNB->pucch0_lut.nb_id;i++) {
    if (gNB->pucch0_lut.Nid[i] == pucch_pdu->hopping_id) break;
  }
#ifdef DEBUG_NR_PUCCH_RX
  printf("found index %d\n",i);
#endif
  if (i<gNB->pucch0_lut.nb_id) return(i);

#ifdef DEBUG_NR_PUCCH_RX
  printf("Initializing PUCCH0 LUT index %i with Nid %d\n",i, pucch_pdu->hopping_id);
#endif
  // initialize
  gNB->pucch0_lut.Nid[gNB->pucch0_lut.nb_id]=pucch_pdu->hopping_id;
  for (int slot=0;slot<10<<pucch_pdu->subcarrier_spacing;slot++)
    for (int symbol=0;symbol<14;symbol++)
      gNB->pucch0_lut.lut[gNB->pucch0_lut.nb_id][slot][symbol] = (int)floor(nr_cyclic_shift_hopping(pucch_pdu->hopping_id,0,0,symbol,0,slot)/0.5235987756);
  gNB->pucch0_lut.nb_id++;
  return(gNB->pucch0_lut.nb_id-1);
}


  
int16_t idft12_re[12][12] = {
  {23170,23170,23170,23170,23170,23170,23170,23170,23170,23170,23170,23170},
  {23170,20066,11585,0,-11585,-20066,-23170,-20066,-11585,0,11585,20066},
  {23170,11585,-11585,-23170,-11585,11585,23170,11585,-11585,-23170,-11585,11585},
  {23170,0,-23170,0,23170,0,-23170,0,23170,0,-23170,0},
  {23170,-11585,-11585,23170,-11585,-11585,23170,-11585,-11585,23170,-11585,-11585},
  {23170,-20066,11585,0,-11585,20066,-23170,20066,-11585,0,11585,-20066},
  {23170,-23170,23170,-23170,23170,-23170,23170,-23170,23170,-23170,23170,-23170},
  {23170,-20066,11585,0,-11585,20066,-23170,20066,-11585,0,11585,-20066},
  {23170,-11585,-11585,23170,-11585,-11585,23170,-11585,-11585,23170,-11585,-11585},
  {23170,0,-23170,0,23170,0,-23170,0,23170,0,-23170,0},
  {23170,11585,-11585,-23170,-11585,11585,23170,11585,-11585,-23170,-11585,11585},
  {23170,20066,11585,0,-11585,-20066,-23170,-20066,-11585,0,11585,20066}
};

int16_t idft12_im[12][12] = {
  {0,0,0,0,0,0,0,0,0,0,0,0},
  {0,11585,20066,23170,20066,11585,0,-11585,-20066,-23170,-20066,-11585},
  {0,20066,20066,0,-20066,-20066,0,20066,20066,0,-20066,-20066},
  {0,23170,0,-23170,0,23170,0,-23170,0,23170,0,-23170},
  {0,20066,-20066,0,20066,-20066,0,20066,-20066,0,20066,-20066},
  {0,11585,-20066,23170,-20066,11585,0,-11585,20066,-23170,20066,-11585},
  {0,0,0,0,0,0,0,0,0,0,0,0},
  {0,-11585,20066,-23170,20066,-11585,0,11585,-20066,23170,-20066,11585},
  {0,-20066,20066,0,-20066,20066,0,-20066,20066,0,-20066,20066},
  {0,-23170,0,23170,0,-23170,0,23170,0,-23170,0,23170},
  {0,-20066,-20066,0,20066,20066,0,-20066,-20066,0,20066,20066},
  {0,-11585,-20066,-23170,-20066,-11585,0,11585,20066,23170,20066,11585}
};


void nr_decode_pucch0(PHY_VARS_gNB *gNB,
166
                      int frame,
167
                      int slot,
Francesco Mani's avatar
Francesco Mani committed
168
                      nfapi_nr_uci_pucch_pdu_format_0_1_t* uci_pdu,
169 170
                      nfapi_nr_pucch_pdu_t* pucch_pdu) {

171 172
  int32_t **rxdataF = gNB->common_vars.rxdataF;
  NR_DL_FRAME_PARMS *frame_parms = &gNB->frame_parms;
Eurecom's avatar
Eurecom committed
173
  int soffset=(slot&3)*frame_parms->symbols_per_slot*frame_parms->ofdm_symbol_size;
174
  int nr_sequences;
175
  const uint8_t *mcs;
176 177 178

  pucch_GroupHopping_t pucch_GroupHopping = pucch_pdu->group_hop_flag + (pucch_pdu->sequence_hop_flag<<1);

179 180 181 182
  AssertFatal(pucch_pdu->bit_len_harq > 0 || pucch_pdu->sr_flag > 0,
	      "Either bit_len_harq (%d) or sr_flag (%d) must be > 0\n",
	      pucch_pdu->bit_len_harq,pucch_pdu->sr_flag);

183 184
  NR_gNB_UCI_STATS_t *uci_stats=NULL;
  NR_gNB_UCI_STATS_t *first_uci_stats=NULL;
185 186
  for (int i=0;i<NUMBER_OF_NR_UCI_STATS_MAX;i++)
     if (gNB->uci_stats[i].rnti == pucch_pdu->rnti) {
187 188 189 190 191 192 193 194 195
        uci_stats = &gNB->uci_stats[i];
        break;
     } else if (first_uci_stats == NULL && gNB->uci_stats[i].rnti == 0) first_uci_stats = &gNB->uci_stats[i];

  if (uci_stats == NULL) { uci_stats=first_uci_stats; uci_stats->rnti = pucch_pdu->rnti;}

  AssertFatal(uci_stats!=NULL,"No stat index found\n");
  uci_stats->frame = frame;

196 197 198 199 200
  if(pucch_pdu->bit_len_harq==0){
    mcs=table1_mcs;
    nr_sequences=1;
  }
  else if(pucch_pdu->bit_len_harq==1){
201
    mcs=table1_mcs;
202
    nr_sequences=4>>(1-pucch_pdu->sr_flag);
203
  }
204
  else{
205
    mcs=table2_mcs;
206
    nr_sequences=8>>(1-pucch_pdu->sr_flag);
207
  }
208

209 210 211
  LOG_D(PHY,"pucch0: nr_symbols %d, start_symbol %d, prb_start %d, second_hop_prb %d,  group_hop_flag %d, sequence_hop_flag %d, O_ACK %d, O_SR %d, mcs %d initial_cyclic_shift %d\n",
        pucch_pdu->nr_of_symbols,pucch_pdu->start_symbol_index,pucch_pdu->prb_start,pucch_pdu->second_hop_prb,pucch_pdu->group_hop_flag,pucch_pdu->sequence_hop_flag,pucch_pdu->bit_len_harq,
        pucch_pdu->sr_flag,mcs[0],pucch_pdu->initial_cyclic_shift);
212

213
  int cs_ind = get_pucch0_cs_lut_index(gNB,pucch_pdu);
214 215 216 217 218 219 220 221
  /*
   * Implement TS 38.211 Subclause 6.3.2.3.1 Sequence generation
   *
   */
  /*
   * Defining cyclic shift hopping TS 38.211 Subclause 6.3.2.2.2
   */
  // alpha is cyclic shift
laurent's avatar
laurent committed
222
  //double alpha;
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  // lnormal is the OFDM symbol number in the PUCCH transmission where l=0 corresponds to the first OFDM symbol of the PUCCH transmission
  //uint8_t lnormal;
  // lprime is the index of the OFDM symbol in the slot that corresponds to the first OFDM symbol of the PUCCH transmission in the slot given by [5, TS 38.213]
  //uint8_t lprime;

  /*
   * in TS 38.213 Subclause 9.2.1 it is said that:
   * for PUCCH format 0 or PUCCH format 1, the index of the cyclic shift
   * is indicated by higher layer parameter PUCCH-F0-F1-initial-cyclic-shift
   */

  /*
   * Implementing TS 38.211 Subclause 6.3.2.3.1, the sequence x(n) shall be generated according to:
   * x(l*12+n) = r_u_v_alpha_delta(n)
   */
  // the value of u,v (delta always 0 for PUCCH) has to be calculated according to TS 38.211 Subclause 6.3.2.2.1
239
  uint8_t u[2]={0,0},v[2]={0,0};
240 241

  // x_n contains the sequence r_u_v_alpha_delta(n)
242

243
  int n,i;
244
  int prb_offset[2] = {pucch_pdu->bwp_start+pucch_pdu->prb_start, pucch_pdu->bwp_start+pucch_pdu->prb_start};
245

246
  nr_group_sequence_hopping(pucch_GroupHopping,pucch_pdu->hopping_id,0,slot,&u[0],&v[0]); // calculating u and v value first hop
rmagueta's avatar
rmagueta committed
247
  LOG_D(PHY,"pucch0: u %d, v %d\n",u[0],v[0]);
248 249


250 251
  if (pucch_pdu->freq_hop_flag == 1) {
    nr_group_sequence_hopping(pucch_GroupHopping,pucch_pdu->hopping_id,1,slot,&u[1],&v[1]); // calculating u and v value second hop
rmagueta's avatar
rmagueta committed
252
    LOG_D(PHY,"pucch0 second hop: u %d, v %d\n",u[1],v[1]);
253
    prb_offset[1] = pucch_pdu->bwp_start+pucch_pdu->second_hop_prb;
254
  }
255

Raymond Knopp's avatar
Raymond Knopp committed
256

257
  AssertFatal(pucch_pdu->nr_of_symbols < 3,"nr_of_symbols %d not allowed\n",pucch_pdu->nr_of_symbols);
258
  uint32_t re_offset[2]={0,0};
259
  uint8_t l2;
260

261 262 263 264 265
  const int16_t *x_re[2],*x_im[2];
  x_re[0] = table_5_2_2_2_2_Re[u[0]];
  x_im[0] = table_5_2_2_2_2_Im[u[0]];
  x_re[1] = table_5_2_2_2_2_Re[u[1]];
  x_im[1] = table_5_2_2_2_2_Im[u[1]];
Raymond Knopp's avatar
Raymond Knopp committed
266

Raymond Knopp's avatar
Raymond Knopp committed
267
  int16_t xr[1+frame_parms->nb_antennas_rx][1+pucch_pdu->nr_of_symbols][24]  __attribute__((aligned(32)));
Eurecom's avatar
Eurecom committed
268
  int64_t xrtmag=0,xrtmag_next=0;
269
  uint8_t maxpos=0;
Raymond Knopp's avatar
Raymond Knopp committed
270
  uint8_t index=0;
271
  for (int l=0; l<pucch_pdu->nr_of_symbols; l++) {
Raymond Knopp's avatar
Raymond Knopp committed
272 273 274 275
    for (int aarx=0;aarx<frame_parms->nb_antennas_rx;aarx++) {
      memset((void*)xr[aarx][l],0,24*sizeof(int16_t));
    }
  }
276
  int n2;
277

278
  for (int l=0; l<pucch_pdu->nr_of_symbols; l++) {
Raymond Knopp's avatar
Raymond Knopp committed
279
    l2 = l+pucch_pdu->start_symbol_index;
280
    re_offset[l] = (12*prb_offset[l]) + frame_parms->first_carrier_offset;
281
    if (re_offset[l]>= frame_parms->ofdm_symbol_size)
282
      re_offset[l]-=frame_parms->ofdm_symbol_size;
Raymond Knopp's avatar
Raymond Knopp committed
283
  
284
    AssertFatal(re_offset[l]+12 < frame_parms->ofdm_symbol_size,"pucch straddles DC carrier, handle this!\n");
285 286
    int16_t *r;
    for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++) {
Eurecom's avatar
Eurecom committed
287
      r=(int16_t*)&rxdataF[aa][soffset+(l2*frame_parms->ofdm_symbol_size)+re_offset[l]];
288 289
      n2=0;
      for (n=0;n<12;n++,n2+=2) {
Raymond Knopp's avatar
Raymond Knopp committed
290 291
        xr[aa][l][n2]  +=(int16_t)(((int32_t)x_re[l][n]*r[n2]+(int32_t)x_im[l][n]*r[n2+1])>>15);
        xr[aa][l][n2+1]+=(int16_t)(((int32_t)x_re[l][n]*r[n2+1]-(int32_t)x_im[l][n]*r[n2])>>15);
292
#ifdef DEBUG_NR_PUCCH_RX
293
        printf("x (%d,%d), r%d.%d (%d,%d), xr (%d,%d)\n",
Raymond Knopp's avatar
Raymond Knopp committed
294
	               x_re[l][n],x_im[l][n],l2,re_offset[l],r[n2],r[n2+1],xr[aa][l][n2],xr[aa][l][n2+1]);
295
#endif
296
      }
297
    }
Raymond Knopp's avatar
Raymond Knopp committed
298
  }
rmagueta's avatar
rmagueta committed
299

Raymond Knopp's avatar
Raymond Knopp committed
300 301
  int32_t corr_re[1+frame_parms->nb_antennas_rx][2];
  int32_t corr_im[1+frame_parms->nb_antennas_rx][2];
rmagueta's avatar
rmagueta committed
302
  //int32_t no_corr = 0;
303
  int seq_index;
rmagueta's avatar
rmagueta committed
304 305
  int64_t temp;

306
  for(i=0;i<nr_sequences;i++){
Raymond Knopp's avatar
Raymond Knopp committed
307

308
    for (int l=0;l<pucch_pdu->nr_of_symbols;l++) {
Raymond Knopp's avatar
Raymond Knopp committed
309
      seq_index = (pucch_pdu->initial_cyclic_shift+
310 311
		   mcs[i]+
		   gNB->pucch0_lut.lut[cs_ind][slot][l+pucch_pdu->start_symbol_index])%12;
312 313 314
#ifdef DEBUG_NR_PUCCH_RX
      printf("PUCCH symbol %d seq %d, seq_index %d, mcs %d\n",l,i,seq_index,mcs[i]);
#endif
Raymond Knopp's avatar
Raymond Knopp committed
315 316 317
      for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++) {
        corr_re[aa][l]=0;
        corr_im[aa][l]=0;
318

Raymond Knopp's avatar
Raymond Knopp committed
319 320 321 322 323
        n2=0;
        for (n=0;n<12;n++,n2+=2) {
          corr_re[aa][l]+=(xr[aa][l][n2]*idft12_re[seq_index][n]+xr[aa][l][n2+1]*idft12_im[seq_index][n])>>15;
          corr_im[aa][l]+=(xr[aa][l][n2]*idft12_im[seq_index][n]-xr[aa][l][n2+1]*idft12_re[seq_index][n])>>15;
        }
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
    LOG_D(PHY,"PUCCH IDFT[%d/%d] = (%d,%d)=>%f\n",
          mcs[i],seq_index,corr_re[0][0],corr_im[0][0],
          10*log10((double)corr_re[0][0]*corr_re[0][0] + (double)corr_im[0][0]*corr_im[0][0]));
    if (pucch_pdu->nr_of_symbols==2) 
       LOG_D(PHY,"PUCCH 2nd symbol IDFT[%d/%d] = (%d,%d)=>%f\n",
             mcs[i],seq_index,corr_re[0][1],corr_im[0][1],
             10*log10((double)corr_re[0][1]*corr_re[0][1] + (double)corr_im[0][1]*corr_im[0][1]));
    if (pucch_pdu->freq_hop_flag == 0) {
       if (pucch_pdu->nr_of_symbols==1) {// non-coherent correlation
          temp=0;
          for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++)
             temp+=(int64_t)corr_re[aa][0]*corr_re[aa][0] + (int64_t)corr_im[aa][0]*corr_im[aa][0];
        } else {
          int64_t corr_re2=0;
          int64_t corr_im2=0;
          temp=0;
          for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++) {
             corr_re2 = (int64_t)corr_re[aa][0]+corr_re[aa][1];
             corr_im2 = (int64_t)corr_im[aa][0]+corr_im[aa][1];
             // coherent combining of 2 symbols and then complex modulus for single-frequency case
             temp+=corr_re2*corr_re2 + corr_im2*corr_im2;
          }
        }
    } else if (pucch_pdu->freq_hop_flag == 1) {
350
      // full non-coherent combining of 2 symbols for frequency-hopping case
Raymond Knopp's avatar
Raymond Knopp committed
351 352 353 354
      temp=0;
      for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++)
        temp += (int64_t)corr_re[aa][0]*corr_re[aa][0] + (int64_t)corr_im[aa][0]*corr_im[aa][0] + (int64_t)corr_re[aa][1]*corr_re[aa][1] + (int64_t)corr_im[aa][1]*corr_im[aa][1];
    }
355 356
    else AssertFatal(1==0,"shouldn't happen\n");

357
    if (temp>xrtmag) {
Eurecom's avatar
Eurecom committed
358
      xrtmag_next = xrtmag;
Raymond Knopp's avatar
Raymond Knopp committed
359
      xrtmag=temp;
francescomani's avatar
francescomani committed
360
      LOG_D(PHY,"Sequence %d xrtmag %ld xrtmag_next %ld\n", i, xrtmag, xrtmag_next);
361
      maxpos=i;
Raymond Knopp's avatar
Raymond Knopp committed
362 363 364 365
      uci_stats->current_pucch0_stat0 = 0;
      int64_t temp2=0,temp3=0;;
      for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++) {
        temp2 += ((int64_t)corr_re[aa][0]*corr_re[aa][0] + (int64_t)corr_im[aa][0]*corr_im[aa][0]);
366 367
        if (pucch_pdu->nr_of_symbols==2)
	  temp3 += ((int64_t)corr_re[aa][1]*corr_re[aa][1] + (int64_t)corr_im[aa][1]*corr_im[aa][1]);
Raymond Knopp's avatar
Raymond Knopp committed
368 369
      }
      uci_stats->current_pucch0_stat0= dB_fixed64(temp2);
370 371
      if ( pucch_pdu->nr_of_symbols==2)
	uci_stats->current_pucch0_stat1= dB_fixed64(temp3);
Raymond Knopp's avatar
Raymond Knopp committed
372
    }
francescomani's avatar
francescomani committed
373 374
    else if (temp>xrtmag_next)
      xrtmag_next = temp;
Raymond Knopp's avatar
Raymond Knopp committed
375
  }
376

377 378
  int xrtmag_dBtimes10 = 10*(int)dB_fixed64(xrtmag/(12*pucch_pdu->nr_of_symbols));
  int xrtmag_next_dBtimes10 = 10*(int)dB_fixed64(xrtmag_next/(12*pucch_pdu->nr_of_symbols));
379
#ifdef DEBUG_NR_PUCCH_RX
380
  printf("PUCCH 0 : maxpos %d\n",maxpos);
381
#endif
382 383

  index=maxpos;
384 385
  uci_stats->pucch0_n00 = gNB->measurements.n0_subband_power_tot_dB[prb_offset[0]];
  uci_stats->pucch0_n01 = gNB->measurements.n0_subband_power_tot_dB[prb_offset[1]];
Raymond Knopp's avatar
Raymond Knopp committed
386
  LOG_D(PHY,"n00[%d] = %d, n01[%d] = %d\n",prb_offset[0],uci_stats->pucch0_n00,prb_offset[1],uci_stats->pucch0_n01);
387
  // estimate CQI for MAC (from antenna port 0 only)
388
  int max_n0 = uci_stats->pucch0_n00>uci_stats->pucch0_n01 ? uci_stats->pucch0_n00:uci_stats->pucch0_n01;
Eurecom's avatar
Eurecom committed
389 390
  int SNRtimes10,sigenergy=0;
  for (int aa=0;aa<frame_parms->nb_antennas_rx;aa++)
Eurecom's avatar
Eurecom committed
391 392 393 394
    sigenergy += signal_energy_nodc(&rxdataF[aa][soffset+
                                                 (pucch_pdu->start_symbol_index*frame_parms->ofdm_symbol_size)+
                                                 re_offset[0]],12);
  SNRtimes10 = xrtmag_dBtimes10-(10*max_n0);
395 396 397 398 399
  int cqi;
  if (SNRtimes10 < -640) cqi=0;
  else if (SNRtimes10 >  635) cqi=255;
  else cqi=(640+SNRtimes10)/5;

Eurecom's avatar
Eurecom committed
400
  uci_stats->pucch0_thres = gNB->pucch0_thres; /* + (10*max_n0);*/
401 402
  bool no_conf=false;
  if (nr_sequences>1) {
403
    if (xrtmag_dBtimes10 < (50+xrtmag_next_dBtimes10) || SNRtimes10 < uci_stats->pucch0_thres)
404 405
      no_conf=true;
  }
rmagueta's avatar
rmagueta committed
406
  gNB->bad_pucch += no_conf;
Francesco Mani's avatar
Francesco Mani committed
407
  // first bit of bitmap for sr presence and second bit for acknack presence
408
  uci_pdu->pduBitmap = pucch_pdu->sr_flag | ((pucch_pdu->bit_len_harq>0)<<1);
Francesco Mani's avatar
Francesco Mani committed
409
  uci_pdu->pucch_format = 0; // format 0
410
  uci_pdu->rnti = pucch_pdu->rnti;
411
  uci_pdu->ul_cqi = cqi;
Francesco Mani's avatar
Francesco Mani committed
412
  uci_pdu->timing_advance = 0xffff; // currently not valid
Eurecom's avatar
Eurecom committed
413
  uci_pdu->rssi = 1280 - (10*dB_fixed(32767*32767))-dB_fixed_times10(sigenergy);
414

415 416 417
  if (pucch_pdu->bit_len_harq==0) {
    uci_pdu->harq = NULL;
    uci_pdu->sr = calloc(1,sizeof(*uci_pdu->sr));
418 419
    uci_pdu->sr->sr_confidence_level = no_conf ? 1 : 0;
    uci_stats->pucch0_sr_trials++;
Eurecom's avatar
Eurecom committed
420
    if (xrtmag_dBtimes10>(10*gNB->measurements.n0_power_tot_dB)) {
421
      uci_pdu->sr->sr_indication = 1;
422
      uci_stats->pucch0_positive_SR++;
423 424 425
    } else {
      uci_pdu->sr->sr_indication = 0;
    }
Francesco Mani's avatar
Francesco Mani committed
426
  }
427
  else if (pucch_pdu->bit_len_harq==1) {
Francesco Mani's avatar
Francesco Mani committed
428
    uci_pdu->harq = calloc(1,sizeof(*uci_pdu->harq));
429
    uci_pdu->harq->num_harq = 1;
430
    uci_pdu->harq->harq_confidence_level = no_conf ? 1 : 0;
431
    uci_pdu->harq->harq_list = (nfapi_nr_harq_t*)malloc(1);
432
    uci_pdu->harq->harq_list[0].harq_value = !(index&0x01);
Francesco Mani's avatar
Francesco Mani committed
433
    LOG_D(PHY, "[DLSCH/PDSCH/PUCCH] %d.%d HARQ value %d (0 pass, 1 fail) with confidence level %d (0 is good, 1 is bad) xrt_mag %d xrt_mag_next %d n0 %d (%d,%d) pucch0_thres %d, cqi %d, SNRtimes10 %d, energy %f, sync_pos %d\n",
434
          frame,slot,uci_pdu->harq->harq_list[0].harq_value,uci_pdu->harq->harq_confidence_level,xrtmag_dBtimes10,xrtmag_next_dBtimes10,max_n0,uci_stats->pucch0_n00,uci_stats->pucch0_n01,uci_stats->pucch0_thres,cqi,SNRtimes10,10*log10((double)sigenergy),gNB->ulsch_stats[0].sync_pos);
435 436 437
    if (pucch_pdu->sr_flag == 1) {
      uci_pdu->sr = calloc(1,sizeof(*uci_pdu->sr));
      uci_pdu->sr->sr_indication = (index>1) ? 1 : 0;
438 439
      uci_pdu->sr->sr_confidence_level = no_conf ? 1 : 0;
      uci_stats->pucch0_positive_SR++;
440
    }
441
    uci_stats->pucch01_trials++;
442 443 444 445
  }
  else {
    uci_pdu->harq = calloc(1,sizeof(*uci_pdu->harq));
    uci_pdu->harq->num_harq = 2;
446
    uci_pdu->harq->harq_confidence_level = (no_conf) ? 1 : 0;
447
    uci_pdu->harq->harq_list = (nfapi_nr_harq_t*)malloc(2);
448 449 450
    uci_pdu->harq->harq_list[1].harq_value = !(index&0x01);
    uci_pdu->harq->harq_list[0].harq_value = !((index>>1)&0x01);
    LOG_D(PHY, "[DLSCH/PDSCH/PUCCH] %d.%d HARQ values %d (0 pass, 1 fail) and %d with confidence level %d (0 is good, 1 is bad), xrt_mag %d xrt_mag_next %d n0 %d (%d,%d) pucch0_thres %d, cqi %d, SNRtimes10 %d,sync_pos %d\n",
451
          frame,slot,uci_pdu->harq->harq_list[1].harq_value,uci_pdu->harq->harq_list[0].harq_value,uci_pdu->harq->harq_confidence_level,xrtmag_dBtimes10,xrtmag_next_dBtimes10,max_n0,uci_stats->pucch0_n00,uci_stats->pucch0_n01,uci_stats->pucch0_thres,cqi,SNRtimes10,gNB->ulsch_stats[0].sync_pos);
452 453 454
    if (pucch_pdu->sr_flag == 1) {
      uci_pdu->sr = calloc(1,sizeof(*uci_pdu->sr));
      uci_pdu->sr->sr_indication = (index>3) ? 1 : 0;
455
      uci_pdu->sr->sr_confidence_level = (no_conf) ? 1 : 0;
456
    }
Francesco Mani's avatar
Francesco Mani committed
457
  }
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
}





void nr_decode_pucch1(  int32_t **rxdataF,
		        pucch_GroupHopping_t pucch_GroupHopping,
                        uint32_t n_id,       // hoppingID higher layer parameter  
                        uint64_t *payload,
		       	NR_DL_FRAME_PARMS *frame_parms, 
                        int16_t amp,
                        int nr_tti_tx,
                        uint8_t m0,
                        uint8_t nrofSymbols,
                        uint8_t startingSymbolIndex,
                        uint16_t startingPRB,
                        uint16_t startingPRB_intraSlotHopping,
                        uint8_t timeDomainOCC,
                        uint8_t nr_bit) {
#ifdef DEBUG_NR_PUCCH_RX
479
  printf("\t [nr_generate_pucch1] start function at slot(nr_tti_tx)=%d payload=%lp m0=%d nrofSymbols=%d startingSymbolIndex=%d startingPRB=%d startingPRB_intraSlotHopping=%d timeDomainOCC=%d nr_bit=%d\n",
480 481 482 483 484 485
         nr_tti_tx,payload,m0,nrofSymbols,startingSymbolIndex,startingPRB,startingPRB_intraSlotHopping,timeDomainOCC,nr_bit);
#endif
  /*
   * Implement TS 38.211 Subclause 6.3.2.4.1 Sequence modulation
   *
   */
Eurecom's avatar
Eurecom committed
486
  int soffset = (nr_tti_tx&3)*frame_parms->symbols_per_slot * frame_parms->ofdm_symbol_size;
487 488 489
  // complex-valued symbol d_re, d_im containing complex-valued symbol d(0):
  int16_t d_re=0, d_im=0,d1_re=0,d1_im=0;
#ifdef DEBUG_NR_PUCCH_RX
490
  printf("\t [nr_generate_pucch1] sequence modulation: payload=%lp \tde_re=%d \tde_im=%d\n",payload,d_re,d_im);
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
#endif
  /*
   * Defining cyclic shift hopping TS 38.211 Subclause 6.3.2.2.2
   */
  // alpha is cyclic shift
  double alpha;
  // lnormal is the OFDM symbol number in the PUCCH transmission where l=0 corresponds to the first OFDM symbol of the PUCCH transmission
  //uint8_t lnormal = 0 ;
  // lprime is the index of the OFDM symbol in the slot that corresponds to the first OFDM symbol of the PUCCH transmission in the slot given by [5, TS 38.213]
  uint8_t lprime = startingSymbolIndex;
  // mcs = 0 except for PUCCH format 0
  uint8_t mcs=0;
  // r_u_v_alpha_delta_re and r_u_v_alpha_delta_im tables containing the sequence y(n) for the PUCCH, when they are multiplied by d(0)
  // r_u_v_alpha_delta_dmrs_re and r_u_v_alpha_delta_dmrs_im tables containing the sequence for the DM-RS.
  int16_t r_u_v_alpha_delta_re[12],r_u_v_alpha_delta_im[12],r_u_v_alpha_delta_dmrs_re[12],r_u_v_alpha_delta_dmrs_im[12];
  /*
   * in TS 38.213 Subclause 9.2.1 it is said that:
   * for PUCCH format 0 or PUCCH format 1, the index of the cyclic shift
   * is indicated by higher layer parameter PUCCH-F0-F1-initial-cyclic-shift
   */
  /*
   * the complex-valued symbol d_0 shall be multiplied with a sequence r_u_v_alpha_delta(n): y(n) = d_0 * r_u_v_alpha_delta(n)
   */
  // the value of u,v (delta always 0 for PUCCH) has to be calculated according to TS 38.211 Subclause 6.3.2.2.1
  uint8_t u=0,v=0;//,delta=0;
  // if frequency hopping is disabled, intraSlotFrequencyHopping is not provided
  //              n_hop = 0
  // if frequency hopping is enabled,  intraSlotFrequencyHopping is     provided
  //              n_hop = 0 for first hop
  //              n_hop = 1 for second hop
  uint8_t n_hop = 0;
  // Intra-slot frequency hopping shall be assumed when the higher-layer parameter intraSlotFrequencyHopping is provided,
  // regardless of whether the frequency-hop distance is zero or not,
  // otherwise no intra-slot frequency hopping shall be assumed
  //uint8_t PUCCH_Frequency_Hopping = 0 ; // from higher layers
  uint8_t intraSlotFrequencyHopping = 0;

  if (startingPRB != startingPRB_intraSlotHopping) {
    intraSlotFrequencyHopping=1;
  }

#ifdef DEBUG_NR_PUCCH_RX
  printf("\t [nr_generate_pucch1] intraSlotFrequencyHopping = %d \n",intraSlotFrequencyHopping);
#endif
  /*
   * Implementing TS 38.211 Subclause 6.3.2.4.2 Mapping to physical resources
   */
  //int32_t *txptr;
  uint32_t re_offset=0;
  int i=0;
#define MAX_SIZE_Z 168 // this value has to be calculated from mprime*12*table_6_3_2_4_1_1_N_SF_mprime_PUCCH_1_noHop[pucch_symbol_length]+m*12+n
  int16_t z_re_rx[MAX_SIZE_Z],z_im_rx[MAX_SIZE_Z],z_re_temp,z_im_temp;
  int16_t z_dmrs_re_rx[MAX_SIZE_Z],z_dmrs_im_rx[MAX_SIZE_Z],z_dmrs_re_temp,z_dmrs_im_temp;
  memset(z_re_rx,0,MAX_SIZE_Z*sizeof(int16_t));
  memset(z_im_rx,0,MAX_SIZE_Z*sizeof(int16_t));
  memset(z_dmrs_re_rx,0,MAX_SIZE_Z*sizeof(int16_t));
  memset(z_dmrs_im_rx,0,MAX_SIZE_Z*sizeof(int16_t));
  int l=0;
  for(l=0;l<nrofSymbols;l++){     //extracting data and dmrs from rxdataF
    if ((intraSlotFrequencyHopping == 1) && (l<floor(nrofSymbols/2))) { // intra-slot hopping enabled, we need to calculate new offset PRB
      startingPRB = startingPRB + startingPRB_intraSlotHopping;
    }

    if ((startingPRB <  (frame_parms->N_RB_DL>>1)) && ((frame_parms->N_RB_DL & 1) == 0)) { // if number RBs in bandwidth is even and current PRB is lower band
      re_offset = ((l+startingSymbolIndex)*frame_parms->ofdm_symbol_size) + (12*startingPRB) + frame_parms->first_carrier_offset;
    }

    if ((startingPRB >= (frame_parms->N_RB_DL>>1)) && ((frame_parms->N_RB_DL & 1) == 0)) { // if number RBs in bandwidth is even and current PRB is upper band
      re_offset = ((l+startingSymbolIndex)*frame_parms->ofdm_symbol_size) + (12*(startingPRB-(frame_parms->N_RB_DL>>1)));
    }

    if ((startingPRB <  (frame_parms->N_RB_DL>>1)) && ((frame_parms->N_RB_DL & 1) == 1)) { // if number RBs in bandwidth is odd  and current PRB is lower band
      re_offset = ((l+startingSymbolIndex)*frame_parms->ofdm_symbol_size) + (12*startingPRB) + frame_parms->first_carrier_offset;
    }

    if ((startingPRB >  (frame_parms->N_RB_DL>>1)) && ((frame_parms->N_RB_DL & 1) == 1)) { // if number RBs in bandwidth is odd  and current PRB is upper band
      re_offset = ((l+startingSymbolIndex)*frame_parms->ofdm_symbol_size) + (12*(startingPRB-(frame_parms->N_RB_DL>>1))) + 6;
    }

    if ((startingPRB == (frame_parms->N_RB_DL>>1)) && ((frame_parms->N_RB_DL & 1) == 1)) { // if number RBs in bandwidth is odd  and current PRB contains DC
      re_offset = ((l+startingSymbolIndex)*frame_parms->ofdm_symbol_size) + (12*startingPRB) + frame_parms->first_carrier_offset;
    }

    for (int n=0; n<12; n++) {
      if ((n==6) && (startingPRB == (frame_parms->N_RB_DL>>1)) && ((frame_parms->N_RB_DL & 1) == 1)) {
        // if number RBs in bandwidth is odd  and current PRB contains DC, we need to recalculate the offset when n=6 (for second half PRB)
        re_offset = ((l+startingSymbolIndex)*frame_parms->ofdm_symbol_size);
      }

      if (l%2 == 1) { // mapping PUCCH according to TS38.211 subclause 6.4.1.3.1
Eurecom's avatar
Eurecom committed
581 582
        z_re_rx[i+n] = ((int16_t *)&rxdataF[0][soffset+re_offset])[0];
        z_im_rx[i+n] = ((int16_t *)&rxdataF[0][soffset+re_offset])[1];
583
#ifdef DEBUG_NR_PUCCH_RX
584
        printf("\t [nr_generate_pucch1] mapping PUCCH to RE \t amp=%d \tofdm_symbol_size=%d \tN_RB_DL=%d \tfirst_carrier_offset=%d \tz_pucch[%d]=txptr(%u)=(x_n(l=%d,n=%d)=(%d,%d))\n",
585
               amp,frame_parms->ofdm_symbol_size,frame_parms->N_RB_DL,frame_parms->first_carrier_offset,i+n,re_offset,
Eurecom's avatar
Eurecom committed
586
               l,n,((int16_t *)&rxdataF[0][soffset+re_offset])[0],((int16_t *)&rxdataF[0][soffset+re_offset])[1]);
587 588 589 590
#endif
      }

      if (l%2 == 0) { // mapping DM-RS signal according to TS38.211 subclause 6.4.1.3.1
Eurecom's avatar
Eurecom committed
591 592
        z_dmrs_re_rx[i+n] = ((int16_t *)&rxdataF[0][soffset+re_offset])[0];
        z_dmrs_im_rx[i+n] = ((int16_t *)&rxdataF[0][soffset+re_offset])[1];
Raymond Knopp's avatar
Raymond Knopp committed
593
	//	printf("%d\t%d\t%d\n",l,z_dmrs_re_rx[i+n],z_dmrs_im_rx[i+n]);
594
#ifdef DEBUG_NR_PUCCH_RX
595
        printf("\t [nr_generate_pucch1] mapping DM-RS to RE \t amp=%d \tofdm_symbol_size=%d \tN_RB_DL=%d \tfirst_carrier_offset=%d \tz_dm-rs[%d]=txptr(%u)=(x_n(l=%d,n=%d)=(%d,%d))\n",
596
               amp,frame_parms->ofdm_symbol_size,frame_parms->N_RB_DL,frame_parms->first_carrier_offset,i+n,re_offset,
Eurecom's avatar
Eurecom committed
597
               l,n,((int16_t *)&rxdataF[0][soffset+re_offset])[0],((int16_t *)&rxdataF[0][soffset+re_offset])[1]);
598
#endif
Raymond Knopp's avatar
Raymond Knopp committed
599
	//        printf("l=%d\ti=%d\tre_offset=%d\treceived dmrs re=%d\tim=%d\n",l,i,re_offset,z_dmrs_re_rx[i+n],z_dmrs_im_rx[i+n]);
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
      }

      re_offset++;
    }
    if (l%2 == 1) i+=12;
  }
  int16_t y_n_re[12],y_n_im[12],y1_n_re[12],y1_n_im[12];
  memset(y_n_re,0,12*sizeof(int16_t));
  memset(y_n_im,0,12*sizeof(int16_t));
  memset(y1_n_re,0,12*sizeof(int16_t));
  memset(y1_n_im,0,12*sizeof(int16_t));
  //generating transmitted sequence and dmrs
  for (l=0; l<nrofSymbols; l++) {
#ifdef DEBUG_NR_PUCCH_RX
    printf("\t [nr_generate_pucch1] for symbol l=%d, lprime=%d\n",
           l,lprime);
#endif
    // y_n contains the complex value d multiplied by the sequence r_u_v
Raymond Knopp's avatar
Raymond Knopp committed
618
    if ((intraSlotFrequencyHopping == 1) && (l >= (int)floor(nrofSymbols/2))) n_hop = 1; // n_hop = 1 for second hop
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

#ifdef DEBUG_NR_PUCCH_RX
    printf("\t [nr_generate_pucch1] entering function nr_group_sequence_hopping with n_hop=%d, nr_tti_tx=%d\n",
           n_hop,nr_tti_tx);
#endif
    nr_group_sequence_hopping(pucch_GroupHopping,n_id,n_hop,nr_tti_tx,&u,&v); // calculating u and v value
    alpha = nr_cyclic_shift_hopping(n_id,m0,mcs,l,lprime,nr_tti_tx);
    
    for (int n=0; n<12; n++) {  // generating low papr sequences
      if(l%2==1){ 
        r_u_v_alpha_delta_re[n] = (int16_t)(((((int32_t)(round(32767*cos(alpha*n))) * table_5_2_2_2_2_Re[u][n])>>15)
                                             - (((int32_t)(round(32767*sin(alpha*n))) * table_5_2_2_2_2_Im[u][n])>>15))); // Re part of base sequence shifted by alpha
        r_u_v_alpha_delta_im[n] = (int16_t)(((((int32_t)(round(32767*cos(alpha*n))) * table_5_2_2_2_2_Im[u][n])>>15)
                                             + (((int32_t)(round(32767*sin(alpha*n))) * table_5_2_2_2_2_Re[u][n])>>15))); // Im part of base sequence shifted by alpha
      }
      else{
        r_u_v_alpha_delta_dmrs_re[n] = (int16_t)(((((int32_t)(round(32767*cos(alpha*n))) * table_5_2_2_2_2_Re[u][n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
636
						  - (((int32_t)(round(32767*sin(alpha*n))) * table_5_2_2_2_2_Im[u][n])>>15))); // Re part of DMRS base sequence shifted by alpha
637
        r_u_v_alpha_delta_dmrs_im[n] = (int16_t)(((((int32_t)(round(32767*cos(alpha*n))) * table_5_2_2_2_2_Im[u][n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
638
						  + (((int32_t)(round(32767*sin(alpha*n))) * table_5_2_2_2_2_Re[u][n])>>15))); // Im part of DMRS base sequence shifted by alpha
639 640 641
        r_u_v_alpha_delta_dmrs_re[n] = (int16_t)(((int32_t)(amp*r_u_v_alpha_delta_dmrs_re[n]))>>15);
        r_u_v_alpha_delta_dmrs_im[n] = (int16_t)(((int32_t)(amp*r_u_v_alpha_delta_dmrs_im[n]))>>15);
      }
Raymond Knopp's avatar
Raymond Knopp committed
642
      //      printf("symbol=%d\tr_u_rx_re=%d\tr_u_rx_im=%d\n",l,r_u_v_alpha_delta_dmrs_re[n], r_u_v_alpha_delta_dmrs_im[n]);
643
      // PUCCH sequence = DM-RS sequence multiplied by d(0)
Raymond Knopp's avatar
Raymond Knopp committed
644 645 646 647
      /*      y_n_re[n]               = (int16_t)(((((int32_t)(r_u_v_alpha_delta_re[n])*d_re)>>15)
	      - (((int32_t)(r_u_v_alpha_delta_im[n])*d_im)>>15))); // Re part of y(n)
	      y_n_im[n]               = (int16_t)(((((int32_t)(r_u_v_alpha_delta_re[n])*d_im)>>15)
	      + (((int32_t)(r_u_v_alpha_delta_im[n])*d_re)>>15))); // Im part of y(n) */
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
#ifdef DEBUG_NR_PUCCH_RX
      printf("\t [nr_generate_pucch1] sequence generation \tu=%d \tv=%d \talpha=%lf \tr_u_v_alpha_delta[n=%d]=(%d,%d) \ty_n[n=%d]=(%d,%d)\n",
             u,v,alpha,n,r_u_v_alpha_delta_re[n],r_u_v_alpha_delta_im[n],n,y_n_re[n],y_n_im[n]);
#endif
    }
    /*
     * The block of complex-valued symbols y(n) shall be block-wise spread with the orthogonal sequence wi(m)
     * (defined in table_6_3_2_4_1_2_Wi_Re and table_6_3_2_4_1_2_Wi_Im)
     * z(mprime*12*table_6_3_2_4_1_1_N_SF_mprime_PUCCH_1_noHop[pucch_symbol_length]+m*12+n)=wi(m)*y(n)
     *
     * The block of complex-valued symbols r_u_v_alpha_dmrs_delta(n) for DM-RS shall be block-wise spread with the orthogonal sequence wi(m)
     * (defined in table_6_3_2_4_1_2_Wi_Re and table_6_3_2_4_1_2_Wi_Im)
     * z(mprime*12*table_6_4_1_3_1_1_1_N_SF_mprime_PUCCH_1_noHop[pucch_symbol_length]+m*12+n)=wi(m)*y(n)
     *
     */
    // the orthogonal sequence index for wi(m) defined in TS 38.213 Subclause 9.2.1
    // the index of the orthogonal cover code is from a set determined as described in [4, TS 38.211]
    // and is indicated by higher layer parameter PUCCH-F1-time-domain-OCC
    // In the PUCCH_Config IE, the PUCCH-format1, timeDomainOCC field
    uint8_t w_index = timeDomainOCC;
    // N_SF_mprime_PUCCH_1 contains N_SF_mprime from table 6.3.2.4.1-1   (depending on number of PUCCH symbols nrofSymbols, mprime and intra-slot hopping enabled/disabled)
    uint8_t N_SF_mprime_PUCCH_1;
    // N_SF_mprime_PUCCH_1 contains N_SF_mprime from table 6.4.1.3.1.1-1 (depending on number of PUCCH symbols nrofSymbols, mprime and intra-slot hopping enabled/disabled)
    uint8_t N_SF_mprime_PUCCH_DMRS_1;
    // N_SF_mprime_PUCCH_1 contains N_SF_mprime from table 6.3.2.4.1-1   (depending on number of PUCCH symbols nrofSymbols, mprime=0 and intra-slot hopping enabled/disabled)
    uint8_t N_SF_mprime0_PUCCH_1;
    // N_SF_mprime_PUCCH_1 contains N_SF_mprime from table 6.4.1.3.1.1-1 (depending on number of PUCCH symbols nrofSymbols, mprime=0 and intra-slot hopping enabled/disabled)
    uint8_t N_SF_mprime0_PUCCH_DMRS_1;
    // mprime is 0 if no intra-slot hopping / mprime is {0,1} if intra-slot hopping
    uint8_t mprime = 0;

    if (intraSlotFrequencyHopping == 0) { // intra-slot hopping disabled
#ifdef DEBUG_NR_PUCCH_RX
      printf("\t [nr_generate_pucch1] block-wise spread with the orthogonal sequence wi(m) if intraSlotFrequencyHopping = %d, intra-slot hopping disabled\n",
             intraSlotFrequencyHopping);
#endif
      N_SF_mprime_PUCCH_1       =   table_6_3_2_4_1_1_N_SF_mprime_PUCCH_1_noHop[nrofSymbols-1]; // only if intra-slot hopping not enabled (PUCCH)
      N_SF_mprime_PUCCH_DMRS_1  = table_6_4_1_3_1_1_1_N_SF_mprime_PUCCH_1_noHop[nrofSymbols-1]; // only if intra-slot hopping not enabled (DM-RS)
      N_SF_mprime0_PUCCH_1      =   table_6_3_2_4_1_1_N_SF_mprime_PUCCH_1_noHop[nrofSymbols-1]; // only if intra-slot hopping not enabled mprime = 0 (PUCCH)
      N_SF_mprime0_PUCCH_DMRS_1 = table_6_4_1_3_1_1_1_N_SF_mprime_PUCCH_1_noHop[nrofSymbols-1]; // only if intra-slot hopping not enabled mprime = 0 (DM-RS)
#ifdef DEBUG_NR_PUCCH_RX
      printf("\t [nr_generate_pucch1] w_index = %d, N_SF_mprime_PUCCH_1 = %d, N_SF_mprime_PUCCH_DMRS_1 = %d, N_SF_mprime0_PUCCH_1 = %d, N_SF_mprime0_PUCCH_DMRS_1 = %d\n",
             w_index, N_SF_mprime_PUCCH_1,N_SF_mprime_PUCCH_DMRS_1,N_SF_mprime0_PUCCH_1,N_SF_mprime0_PUCCH_DMRS_1);
#endif
      if(l%2==1){
        for (int m=0; m < N_SF_mprime_PUCCH_1; m++) {
	  if(floor(l/2)*12==(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)){
            for (int n=0; n<12 ; n++) {
              z_re_temp = (int16_t)(((((int32_t)(table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m])*z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
697
				     + (((int32_t)(table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m])*z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15))>>1);
698
              z_im_temp = (int16_t)(((((int32_t)(table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m])*z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
699
				     - (((int32_t)(table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m])*z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15))>>1);
700 701
              z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]=z_re_temp; 
              z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]=z_im_temp; 
Raymond Knopp's avatar
Raymond Knopp committed
702
	      //	      printf("symbol=%d\tz_re_rx=%d\tz_im_rx=%d\t",l,(int)z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],(int)z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]);
703 704 705 706 707
#ifdef DEBUG_NR_PUCCH_RX
              printf("\t [nr_generate_pucch1] block-wise spread with wi(m) (mprime=%d, m=%d, n=%d) z[%d] = ((%d * %d - %d * %d), (%d * %d + %d * %d)) = (%d,%d)\n",
                     mprime, m, n, (mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n,
                     table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m],y_n_re[n],table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m],y_n_im[n],
                     table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m],y_n_im[n],table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m],y_n_re[n],
Raymond Knopp's avatar
Raymond Knopp committed
708
                     z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]);
709 710 711
#endif   
	      // multiplying with conjugate of low papr sequence  
	      z_re_temp = (int16_t)(((((int32_t)(r_u_v_alpha_delta_re[n])*z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
712
				     + (((int32_t)(r_u_v_alpha_delta_im[n])*z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15))>>1); 
713
              z_im_temp = (int16_t)(((((int32_t)(r_u_v_alpha_delta_re[n])*z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
714
				     - (((int32_t)(r_u_v_alpha_delta_im[n])*z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15))>>1);
715 716
              z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n] = z_re_temp;
              z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n] = z_im_temp;
Raymond Knopp's avatar
Raymond Knopp committed
717 718 719 720
	      /*	      if(z_re_temp<0){
			      printf("\nBug detection %d\t%d\t%d\t%d\n",r_u_v_alpha_delta_re[n],z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],(((int32_t)(r_u_v_alpha_delta_re[n])*z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15),(((int32_t)(r_u_v_alpha_delta_im[n])*z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15));
			      }
			      printf("z1_re_rx=%d\tz1_im_rx=%d\n",(int)z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],(int)z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]); */ 
721 722 723 724 725 726 727 728 729 730
	    }
	  }
        }
      }

      else{
        for (int m=0; m < N_SF_mprime_PUCCH_DMRS_1; m++) {
          if(floor(l/2)*12==(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)){
            for (int n=0; n<12 ; n++) {
              z_dmrs_re_temp = (int16_t)(((((int32_t)(table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_DMRS_1][w_index][m])*z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
731
					  + (((int32_t)(table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_DMRS_1][w_index][m])*z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15))>>1);
732
              z_dmrs_im_temp =  (int16_t)(((((int32_t)(table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_DMRS_1][w_index][m])*z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
733
					   - (((int32_t)(table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_DMRS_1][w_index][m])*z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15))>>1);
734 735
              z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n] = z_dmrs_re_temp;
              z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n] = z_dmrs_im_temp;
Raymond Knopp's avatar
Raymond Knopp committed
736
	      //              printf("symbol=%d\tz_dmrs_re_rx=%d\tz_dmrs_im_rx=%d\t",l,(int)z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],(int)z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]);
737 738 739 740 741
#ifdef DEBUG_NR_PUCCH_RX
              printf("\t [nr_generate_pucch1] block-wise spread with wi(m) (mprime=%d, m=%d, n=%d) z[%d] = ((%d * %d - %d * %d), (%d * %d + %d * %d)) = (%d,%d)\n",
                     mprime, m, n, (mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n,
                     table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m],r_u_v_alpha_delta_dmrs_re[n],table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m],r_u_v_alpha_delta_dmrs_im[n],
                     table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m],r_u_v_alpha_delta_dmrs_im[n],table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m],r_u_v_alpha_delta_dmrs_re[n],
Raymond Knopp's avatar
Raymond Knopp committed
742
                     z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]);
743 744 745
#endif
              //finding channel coeffcients by dividing received dmrs with actual dmrs and storing them in z_dmrs_re_rx and z_dmrs_im_rx arrays
              z_dmrs_re_temp = (int16_t)(((((int32_t)(r_u_v_alpha_delta_dmrs_re[n])*z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
746
					  + (((int32_t)(r_u_v_alpha_delta_dmrs_im[n])*z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15))>>1); 
747
              z_dmrs_im_temp = (int16_t)(((((int32_t)(r_u_v_alpha_delta_dmrs_re[n])*z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
748 749 750 751
					  - (((int32_t)(r_u_v_alpha_delta_dmrs_im[n])*z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15))>>1);
	      /*	      if(z_dmrs_re_temp<0){
			      printf("\nBug detection %d\t%d\t%d\t%d\n",r_u_v_alpha_delta_dmrs_re[n],z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],(((int32_t)(r_u_v_alpha_delta_dmrs_re[n])*z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15),(((int32_t)(r_u_v_alpha_delta_dmrs_im[n])*z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15));
			      }*/
752 753
	      z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n] = z_dmrs_re_temp;
	      z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n] = z_dmrs_im_temp; 
Raymond Knopp's avatar
Raymond Knopp committed
754 755 756
	      //	      printf("z1_dmrs_re_rx=%d\tz1_dmrs_im_rx=%d\n",(int)z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],(int)z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]);
	      /* z_dmrs_re_rx[(int)(l/2)*12+n]=z_dmrs_re_rx[(int)(l/2)*12+n]/r_u_v_alpha_delta_dmrs_re[n]; 
		 z_dmrs_im_rx[(int)(l/2)*12+n]=z_dmrs_im_rx[(int)(l/2)*12+n]/r_u_v_alpha_delta_dmrs_im[n]; */
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
	    }
	  }
        }
      }
    }

    if (intraSlotFrequencyHopping == 1) { // intra-slot hopping enabled
#ifdef DEBUG_NR_PUCCH_RX
      printf("\t [nr_generate_pucch1] block-wise spread with the orthogonal sequence wi(m) if intraSlotFrequencyHopping = %d, intra-slot hopping enabled\n",
             intraSlotFrequencyHopping);
#endif
      N_SF_mprime_PUCCH_1       =   table_6_3_2_4_1_1_N_SF_mprime_PUCCH_1_m0Hop[nrofSymbols-1]; // only if intra-slot hopping enabled mprime = 0 (PUCCH)
      N_SF_mprime_PUCCH_DMRS_1  = table_6_4_1_3_1_1_1_N_SF_mprime_PUCCH_1_m0Hop[nrofSymbols-1]; // only if intra-slot hopping enabled mprime = 0 (DM-RS)
      N_SF_mprime0_PUCCH_1      =   table_6_3_2_4_1_1_N_SF_mprime_PUCCH_1_m0Hop[nrofSymbols-1]; // only if intra-slot hopping enabled mprime = 0 (PUCCH)
      N_SF_mprime0_PUCCH_DMRS_1 = table_6_4_1_3_1_1_1_N_SF_mprime_PUCCH_1_m0Hop[nrofSymbols-1]; // only if intra-slot hopping enabled mprime = 0 (DM-RS)
#ifdef DEBUG_NR_PUCCH_RX
      printf("\t [nr_generate_pucch1] w_index = %d, N_SF_mprime_PUCCH_1 = %d, N_SF_mprime_PUCCH_DMRS_1 = %d, N_SF_mprime0_PUCCH_1 = %d, N_SF_mprime0_PUCCH_DMRS_1 = %d\n",
             w_index, N_SF_mprime_PUCCH_1,N_SF_mprime_PUCCH_DMRS_1,N_SF_mprime0_PUCCH_1,N_SF_mprime0_PUCCH_DMRS_1);
#endif

      for (mprime = 0; mprime<2; mprime++) { // mprime can get values {0,1}
	if(l%2==1){
          for (int m=0; m < N_SF_mprime_PUCCH_1; m++) {
            if(floor(l/2)*12==(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)){
              for (int n=0; n<12 ; n++) {
                z_re_temp = (int16_t)(((((int32_t)(table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m])*z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
783
				       + (((int32_t)(table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m])*z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15))>>1);
784
                z_im_temp = (int16_t)(((((int32_t)(table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m])*z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
785
				       - (((int32_t)(table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m])*z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15))>>1);
786 787 788 789 790 791 792
                z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n] = z_re_temp;
                z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n] = z_im_temp;
#ifdef DEBUG_NR_PUCCH_RX
                printf("\t [nr_generate_pucch1] block-wise spread with wi(m) (mprime=%d, m=%d, n=%d) z[%d] = ((%d * %d - %d * %d), (%d * %d + %d * %d)) = (%d,%d)\n",
                       mprime, m, n, (mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n,
                       table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m],y_n_re[n],table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m],y_n_im[n],
                       table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m],y_n_im[n],table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m],y_n_re[n],
Raymond Knopp's avatar
Raymond Knopp committed
793
                       z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]);
794 795
#endif 
                z_re_temp = (int16_t)(((((int32_t)(r_u_v_alpha_delta_re[n])*z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
796
				       + (((int32_t)(r_u_v_alpha_delta_im[n])*z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15))>>1); 
797
                z_im_temp = (int16_t)(((((int32_t)(r_u_v_alpha_delta_re[n])*z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
798
				       - (((int32_t)(r_u_v_alpha_delta_im[n])*z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n])>>15))>>1); 	  
799 800 801 802 803 804 805 806 807 808 809 810
	        z_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n] = z_re_temp; 
                z_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n] = z_im_temp; 
	      }
	    }
	  }
        }

	else{
	  for (int m=0; m < N_SF_mprime_PUCCH_DMRS_1; m++) {
            if(floor(l/2)*12==(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)){
              for (int n=0; n<12 ; n++) {
                z_dmrs_re_temp = (int16_t)(((((int32_t)(table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_DMRS_1][w_index][m])*z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
811
					    + (((int32_t)(table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_DMRS_1][w_index][m])*z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15))>>1);
812
                z_dmrs_im_temp = (int16_t)(((((int32_t)(table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_DMRS_1][w_index][m])*z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
813
					    - (((int32_t)(table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_DMRS_1][w_index][m])*z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15))>>1);
814 815 816 817 818 819 820
                z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n] = z_dmrs_re_temp; 
                z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n] = z_dmrs_im_temp; 
#ifdef DEBUG_NR_PUCCH_RX
                printf("\t [nr_generate_pucch1] block-wise spread with wi(m) (mprime=%d, m=%d, n=%d) z[%d] = ((%d * %d - %d * %d), (%d * %d + %d * %d)) = (%d,%d)\n",
                       mprime, m, n, (mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n,
                       table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m],r_u_v_alpha_delta_dmrs_re[n],table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m],r_u_v_alpha_delta_dmrs_im[n],
                       table_6_3_2_4_1_2_Wi_Re[N_SF_mprime_PUCCH_1][w_index][m],r_u_v_alpha_delta_dmrs_im[n],table_6_3_2_4_1_2_Wi_Im[N_SF_mprime_PUCCH_1][w_index][m],r_u_v_alpha_delta_dmrs_re[n],
Raymond Knopp's avatar
Raymond Knopp committed
821
                       z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n],z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_1)+(m*12)+n]);
822 823 824
#endif
                //finding channel coeffcients by dividing received dmrs with actual dmrs and storing them in z_dmrs_re_rx and z_dmrs_im_rx arrays
                z_dmrs_re_temp = (int16_t)(((((int32_t)(r_u_v_alpha_delta_dmrs_re[n])*z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
825
					    + (((int32_t)(r_u_v_alpha_delta_dmrs_im[n])*z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15))>>1); 
826
                z_dmrs_im_temp = (int16_t)(((((int32_t)(r_u_v_alpha_delta_dmrs_re[n])*z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15)
Raymond Knopp's avatar
Raymond Knopp committed
827
					    - (((int32_t)(r_u_v_alpha_delta_dmrs_im[n])*z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n])>>15))>>1);
828 829 830
	        z_dmrs_re_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n] = z_dmrs_re_temp; 
                z_dmrs_im_rx[(mprime*12*N_SF_mprime0_PUCCH_DMRS_1)+(m*12)+n] = z_dmrs_im_temp; 

Raymond Knopp's avatar
Raymond Knopp committed
831 832
		/* 	z_dmrs_re_rx[(int)(l/2)*12+n]=z_dmrs_re_rx[(int)(l/2)*12+n]/r_u_v_alpha_delta_dmrs_re[n]; 
			z_dmrs_im_rx[(int)(l/2)*12+n]=z_dmrs_im_rx[(int)(l/2)*12+n]/r_u_v_alpha_delta_dmrs_im[n]; */
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
	      }
	    }
	  }
        }

        N_SF_mprime_PUCCH_1       =   table_6_3_2_4_1_1_N_SF_mprime_PUCCH_1_m1Hop[nrofSymbols-1]; // only if intra-slot hopping enabled mprime = 1 (PUCCH)
        N_SF_mprime_PUCCH_DMRS_1  = table_6_4_1_3_1_1_1_N_SF_mprime_PUCCH_1_m1Hop[nrofSymbols-1]; // only if intra-slot hopping enabled mprime = 1 (DM-RS)
      }
    }
  }
  int16_t H_re[12],H_im[12],H1_re[12],H1_im[12];
  memset(H_re,0,12*sizeof(int16_t));
  memset(H_im,0,12*sizeof(int16_t));
  memset(H1_re,0,12*sizeof(int16_t));
  memset(H1_im,0,12*sizeof(int16_t)); 
  //averaging channel coefficients
  for(l=0;l<=ceil(nrofSymbols/2);l++){
    if(intraSlotFrequencyHopping==0){
      for(int n=0;n<12;n++){
        H_re[n]=round(z_dmrs_re_rx[l*12+n]/ceil(nrofSymbols/2))+H_re[n];
        H_im[n]=round(z_dmrs_im_rx[l*12+n]/ceil(nrofSymbols/2))+H_im[n];
      }
    }
    else{
      if(l<round(nrofSymbols/4)){
        for(int n=0;n<12;n++){
          H_re[n]=round(z_dmrs_re_rx[l*12+n]/round(nrofSymbols/4))+H_re[n];
          H_im[n]=round(z_dmrs_im_rx[l*12+n]/round(nrofSymbols/4))+H_im[n];
	}
      }
      else{
        for(int n=0;n<12;n++){
          H1_re[n]=round(z_dmrs_re_rx[l*12+n]/(ceil(nrofSymbols/2)-round(nrofSymbols/4)))+H1_re[n];
          H1_im[n]=round(z_dmrs_im_rx[l*12+n]/(ceil(nrofSymbols/2))-round(nrofSymbols/4))+H1_im[n];
	} 
      }
    }
  }
  //averaging information sequences
  for(l=0;l<floor(nrofSymbols/2);l++){
    if(intraSlotFrequencyHopping==0){
      for(int n=0;n<12;n++){
        y_n_re[n]=round(z_re_rx[l*12+n]/floor(nrofSymbols/2))+y_n_re[n];
        y_n_im[n]=round(z_im_rx[l*12+n]/floor(nrofSymbols/2))+y_n_im[n];
      }
    }
    else{
      if(l<floor(nrofSymbols/4)){
        for(int n=0;n<12;n++){
          y_n_re[n]=round(z_re_rx[l*12+n]/floor(nrofSymbols/4))+y_n_re[n];
          y_n_im[n]=round(z_im_rx[l*12+n]/floor(nrofSymbols/4))+y_n_im[n];
Raymond Knopp's avatar
Raymond Knopp committed
884 885
	}	     
      }
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
      else{
        for(int n=0;n<12;n++){
          y1_n_re[n]=round(z_re_rx[l*12+n]/round(nrofSymbols/4))+y1_n_re[n];
          y1_n_im[n]=round(z_im_rx[l*12+n]/round(nrofSymbols/4))+y1_n_im[n];
        }
      }	
    }
  }
  // mrc combining to obtain z_re and z_im
  if(intraSlotFrequencyHopping==0){
    for(int n=0;n<12;n++){
      d_re = round(((int16_t)(((((int32_t)(H_re[n])*y_n_re[n])>>15) + (((int32_t)(H_im[n])*y_n_im[n])>>15))>>1))/12)+d_re; 
      d_im = round(((int16_t)(((((int32_t)(H_re[n])*y_n_im[n])>>15) - (((int32_t)(H_im[n])*y_n_re[n])>>15))>>1))/12)+d_im; 
    }
  }
  else{
    for(int n=0;n<12;n++){
      d_re = round(((int16_t)(((((int32_t)(H_re[n])*y_n_re[n])>>15) + (((int32_t)(H_im[n])*y_n_im[n])>>15))>>1))/12)+d_re; 
      d_im = round(((int16_t)(((((int32_t)(H_re[n])*y_n_im[n])>>15) - (((int32_t)(H_im[n])*y_n_re[n])>>15))>>1))/12)+d_im;
      d1_re = round(((int16_t)(((((int32_t)(H1_re[n])*y1_n_re[n])>>15) + (((int32_t)(H1_im[n])*y1_n_im[n])>>15))>>1))/12)+d1_re; 
      d1_im = round(((int16_t)(((((int32_t)(H1_re[n])*y1_n_im[n])>>15) - (((int32_t)(H1_im[n])*y1_n_re[n])>>15))>>1))/12)+d1_im; 
    }
    d_re=round(d_re/2);
    d_im=round(d_im/2);
    d1_re=round(d1_re/2);
    d1_im=round(d1_im/2);
    d_re=d_re+d1_re;
    d_im=d_im+d1_im;
  }
  //Decoding QPSK or BPSK symbols to obtain payload bits
  if(nr_bit==1){
Raymond Knopp's avatar
Raymond Knopp committed
917 918 919 920 921 922
    if((d_re+d_im)>0){
      *payload=0;
    }
    else{
      *payload=1;
    } 
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
  }
  else if(nr_bit==2){
    if((d_re>0)&&(d_im>0)){
      *payload=0;
    }
    else if((d_re<0)&&(d_im>0)){
      *payload=1;
    } 
    else if((d_re>0)&&(d_im<0)){
      *payload=2;
    }
    else{
      *payload=3;
    }
  }
938 939
}

940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
__m256i pucch2_3bit[8*2];
__m256i pucch2_4bit[16*2];
__m256i pucch2_5bit[32*2];
__m256i pucch2_6bit[64*2];
__m256i pucch2_7bit[128*2];
__m256i pucch2_8bit[256*2];
__m256i pucch2_9bit[512*2];
__m256i pucch2_10bit[1024*2];
__m256i pucch2_11bit[2048*2];

__m256i *pucch2_lut[9]={pucch2_3bit,
			pucch2_4bit,
			pucch2_5bit,
			pucch2_6bit,
			pucch2_7bit,
			pucch2_8bit,
			pucch2_9bit,
			pucch2_10bit,
			pucch2_11bit};

Raymond Knopp's avatar
Raymond Knopp committed
960
__m64 pucch2_polar_4bit[16];
961
__m128i pucch2_polar_llr_num_lut[256],pucch2_polar_llr_den_lut[256];
962

963 964 965 966 967 968 969 970
void init_pucch2_luts() {

  uint32_t out;
  int8_t bit; 
  
  for (int b=3;b<12;b++) {
    for (uint16_t i=0;i<(1<<b);i++) {
      out=encodeSmallBlock(&i,b);
971
#ifdef DEBUG_NR_PUCCH_RX
972
      if (b==3) printf("in %d, out %x\n",i,out);
973
#endif
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
      __m256i *lut_i=&pucch2_lut[b-3][i<<1];
      __m256i *lut_ip1=&pucch2_lut[b-3][1+(i<<1)];
      bit = (out&0x1) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,0);
      bit = (out&0x2) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,0);
      bit = (out&0x4) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,1);
      bit = (out&0x8) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,1);
      bit = (out&0x10) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,2);
      bit = (out&0x20) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,2);
      bit = (out&0x40) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,3);
      bit = (out&0x80) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,3);
      bit = (out&0x100) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,4);
      bit = (out&0x200) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,4);
      bit = (out&0x400) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,5);
      bit = (out&0x800) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,5);
      bit = (out&0x1000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,6);
      bit = (out&0x2000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,6);
      bit = (out&0x4000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,7);
      bit = (out&0x8000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,7);
      bit = (out&0x10000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,8);
      bit = (out&0x20000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,8);
      bit = (out&0x40000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,9);
      bit = (out&0x80000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,9);
      bit = (out&0x100000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,10);
      bit = (out&0x200000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,10);
      bit = (out&0x400000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,11);
      bit = (out&0x800000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,11);
      bit = (out&0x1000000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,12);
      bit = (out&0x2000000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,12);
      bit = (out&0x4000000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,13);
      bit = (out&0x8000000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,13);
      bit = (out&0x10000000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,14);
      bit = (out&0x20000000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,14);
      bit = (out&0x40000000) > 0 ? -1 : 1;
      *lut_i = _mm256_insert_epi16(*lut_i,bit,15);
      bit = (out&0x80000000) > 0 ? -1 : 1;
      *lut_ip1 = _mm256_insert_epi16(*lut_ip1,bit,15);
    }
  }
Raymond Knopp's avatar
Raymond Knopp committed
1042 1043 1044 1045
  for (uint16_t i=0;i<16;i++) {
    __m64 *lut_i=&pucch2_polar_4bit[i];

    bit = (i&0x1) > 0 ? -1 : 1;
1046
    *lut_i = _mm_insert_pi16(*lut_i,bit,0);
Raymond Knopp's avatar
Raymond Knopp committed
1047
    bit = (i&0x2) > 0 ? -1 : 1;
1048
    *lut_i = _mm_insert_pi16(*lut_i,bit,1);
Raymond Knopp's avatar
Raymond Knopp committed
1049
    bit = (i&0x4) > 0 ? -1 : 1;
1050
    *lut_i = _mm_insert_pi16(*lut_i,bit,2);
Raymond Knopp's avatar
Raymond Knopp committed
1051
    bit = (i&0x8) > 0 ? -1 : 1;
1052 1053
    *lut_i = _mm_insert_pi16(*lut_i,bit,3);
  }
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  for (int i=0;i<256;i++) {
    __m128i *lut_num_i=&pucch2_polar_llr_num_lut[i];
    __m128i *lut_den_i=&pucch2_polar_llr_den_lut[i];
    bit = (i&0x1) > 0 ? 0 : 1;
   *lut_num_i = _mm_insert_epi16(*lut_num_i,bit,0);
   *lut_den_i = _mm_insert_epi16(*lut_den_i,1-bit,0);

    bit = (i&0x10) > 0 ? 0 : 1;
   *lut_num_i = _mm_insert_epi16(*lut_num_i,bit,1);
   *lut_den_i = _mm_insert_epi16(*lut_den_i,1-bit,1);

    bit = (i&0x2) > 0 ? 0 : 1;
   *lut_num_i = _mm_insert_epi16(*lut_num_i,bit,2);
   *lut_den_i = _mm_insert_epi16(*lut_den_i,1-bit,2);

    bit = (i&0x20) > 0 ? 0 : 1;
   *lut_num_i = _mm_insert_epi16(*lut_num_i,bit,3);
   *lut_den_i = _mm_insert_epi16(*lut_den_i,1-bit,3);

    bit = (i&0x4) > 0 ? 0 : 1;
   *lut_num_i = _mm_insert_epi16(*lut_num_i,bit,4);
   *lut_den_i = _mm_insert_epi16(*lut_den_i,1-bit,4);

    bit = (i&0x40) > 0 ? 0 : 1;
   *lut_num_i = _mm_insert_epi16(*lut_num_i,bit,5);
   *lut_den_i = _mm_insert_epi16(*lut_den_i,1-bit,5);

    bit = (i&0x8) > 0 ? 0 : 1;
   *lut_num_i = _mm_insert_epi16(*lut_num_i,bit,6);
   *lut_den_i = _mm_insert_epi16(*lut_den_i,1-bit,6);

    bit = (i&0x80) > 0 ? 0 : 1;
   *lut_num_i = _mm_insert_epi16(*lut_num_i,bit,7);
   *lut_den_i = _mm_insert_epi16(*lut_den_i,1-bit,7);
1088 1089

#ifdef DEBUG_NR_PUCCH_RX
1090 1091 1092 1093 1094 1095 1096 1097 1098
   printf("i %d, lut_num (%d,%d,%d,%d,%d,%d,%d,%d)\n",i,
	  ((int16_t *)lut_num_i)[0],
	  ((int16_t *)lut_num_i)[1],
	  ((int16_t *)lut_num_i)[2],
	  ((int16_t *)lut_num_i)[3],
	  ((int16_t *)lut_num_i)[4],
	  ((int16_t *)lut_num_i)[5],
	  ((int16_t *)lut_num_i)[6],
	  ((int16_t *)lut_num_i)[7]);
1099
#endif
1100
  }
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
}


void nr_decode_pucch2(PHY_VARS_gNB *gNB,
                      int slot,
                      nfapi_nr_uci_pucch_pdu_format_2_3_4_t* uci_pdu,
                      nfapi_nr_pucch_pdu_t* pucch_pdu) {

  int32_t **rxdataF = gNB->common_vars.rxdataF;
  NR_DL_FRAME_PARMS *frame_parms = &gNB->frame_parms;
laurent's avatar
laurent committed
1111
  //pucch_GroupHopping_t pucch_GroupHopping = pucch_pdu->group_hop_flag + (pucch_pdu->sequence_hop_flag<<1);
1112 1113 1114 1115

  AssertFatal(pucch_pdu->nr_of_symbols==1 || pucch_pdu->nr_of_symbols==2,
	      "Illegal number of symbols  for PUCCH 2 %d\n",pucch_pdu->nr_of_symbols);

1116 1117 1118
  AssertFatal((pucch_pdu->prb_start-((pucch_pdu->prb_start>>2)<<2))==0,
              "Current pucch2 receiver implementation requires a PRB offset multiple of 4. The one selected is %d",
              pucch_pdu->prb_start);
1119 1120 1121

  //extract pucch and dmrs first

1122
  int l2=pucch_pdu->start_symbol_index;
Raymond Knopp's avatar
Raymond Knopp committed
1123 1124
  int re_offset[2];
  re_offset[0] = 12*(pucch_pdu->prb_start+pucch_pdu->bwp_start) + frame_parms->first_carrier_offset;
Eurecom's avatar
Eurecom committed
1125
  int soffset=(slot&3)*frame_parms->symbols_per_slot*frame_parms->ofdm_symbol_size; 
Raymond Knopp's avatar
Raymond Knopp committed
1126

1127
  if (re_offset[0]>= frame_parms->ofdm_symbol_size)
Raymond Knopp's avatar
Raymond Knopp committed
1128 1129 1130 1131
    re_offset[0]-=frame_parms->ofdm_symbol_size;
  if (pucch_pdu->freq_hop_flag == 0) re_offset[1] = re_offset[0];
  else {
    re_offset[1] = 12*(pucch_pdu->second_hop_prb+pucch_pdu->bwp_start) + frame_parms->first_carrier_offset;
1132
    if (re_offset[1]>= frame_parms->ofdm_symbol_size)
Raymond Knopp's avatar
Raymond Knopp committed
1133 1134
      re_offset[1]-=frame_parms->ofdm_symbol_size;
  }
1135 1136 1137 1138 1139 1140
  AssertFatal(pucch_pdu->prb_size*pucch_pdu->nr_of_symbols > 1,"number of PRB*SYMB (%d,%d)< 2",
	      pucch_pdu->prb_size,pucch_pdu->nr_of_symbols);

  int Prx = gNB->gNB_config.carrier_config.num_rx_ant.value;
  int Prx2 = (Prx==1)?2:Prx;
  // use 2 for Nb antennas in case of single antenna to allow the following allocations
Raymond Knopp's avatar
Raymond Knopp committed
1141 1142 1143 1144 1145 1146 1147
  int prb_size_ext = pucch_pdu->prb_size+(pucch_pdu->prb_size&1);
  int16_t r_re_ext[Prx2][2][8*prb_size_ext] __attribute__((aligned(32)));
  int16_t r_im_ext[Prx2][2][8*prb_size_ext] __attribute__((aligned(32)));
  int16_t r_re_ext2[Prx2][2][8*prb_size_ext] __attribute__((aligned(32)));
  int16_t r_im_ext2[Prx2][2][8*prb_size_ext] __attribute__((aligned(32)));
  int16_t rd_re_ext[Prx2][2][4*prb_size_ext] __attribute__((aligned(32)));
  int16_t rd_im_ext[Prx2][2][4*prb_size_ext] __attribute__((aligned(32)));
1148
  int16_t *r_re_ext_p,*r_im_ext_p,*rd_re_ext_p,*rd_im_ext_p;
1149

1150 1151
  int nb_re_pucch = 12*pucch_pdu->prb_size;

Raymond Knopp's avatar
Raymond Knopp committed
1152
  int16_t rp[Prx2][2][nb_re_pucch*2],*tmp_rp;
1153 1154
  __m64 dmrs_re,dmrs_im;

1155
  for (int aa=0;aa<Prx;aa++){
1156
    for (int symb=0;symb<pucch_pdu->nr_of_symbols;symb++) {
Raymond Knopp's avatar
Raymond Knopp committed
1157
      tmp_rp = ((int16_t *)&rxdataF[aa][soffset + (l2+symb)*frame_parms->ofdm_symbol_size]);
1158

Raymond Knopp's avatar
Raymond Knopp committed
1159 1160 1161 1162 1163 1164 1165 1166 1167
      if (re_offset[symb] + nb_re_pucch < frame_parms->ofdm_symbol_size) {
        memcpy1((void*)rp[aa][symb],(void*)&tmp_rp[re_offset[symb]*2],nb_re_pucch*sizeof(int32_t));
      }
      else {
        int neg_length = frame_parms->ofdm_symbol_size-re_offset[symb];
        int pos_length = nb_re_pucch-neg_length;
        memcpy1((void*)rp[aa][symb],(void*)&tmp_rp[re_offset[symb]*2],neg_length*sizeof(int32_t));
        memcpy1((void*)&rp[aa][symb][neg_length*2],(void*)tmp_rp,pos_length*sizeof(int32_t));
      }
1168 1169
    }
  }
1170 1171 1172 1173 1174
#ifdef DEBUG_NR_PUCCH_RX
  printf("Decoding pucch2 for %d symbols, %d PRB\n",pucch_pdu->nr_of_symbols,pucch_pdu->prb_size);
#endif

  int nc_group_size=1; // 2 PRB
Raymond Knopp's avatar
Raymond Knopp committed
1175 1176
  int ngroup = prb_size_ext/nc_group_size/2;
  int32_t corr32_re[2][ngroup][Prx2],corr32_im[2][ngroup][Prx2];
1177 1178 1179
  for (int aa=0;aa<Prx;aa++)
    for (int group=0;group<ngroup;group++) {
      corr32_re[0][group][aa]=0; corr32_im[0][group][aa]=0;
Raymond Knopp's avatar
Raymond Knopp committed
1180 1181
      corr32_re[1][group][aa]=0; corr32_im[1][group][aa]=0;
    }
1182

Raymond Knopp's avatar
Raymond Knopp committed
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
  //  AssertFatal((pucch_pdu->prb_size&1) == 0,"prb_size %d is not a multiple of 2\n",pucch_pdu->prb_size);
  if ((pucch_pdu->prb_size&1) > 0) { // if the number of PRBs is odd
    for (int symb=0; symb<pucch_pdu->nr_of_symbols;symb++) {
      for (int aa=0;aa<Prx;aa++) {
        memset(&r_re_ext[aa][symb][8*pucch_pdu->prb_size],0,8*pucch_pdu->prb_size*sizeof(int16_t));
        memset(&r_im_ext[aa][symb][8*pucch_pdu->prb_size],0,8*pucch_pdu->prb_size*sizeof(int16_t));
        memset(&rd_re_ext[aa][symb][4*pucch_pdu->prb_size],0,8*pucch_pdu->prb_size*sizeof(int16_t));
        memset(&rd_im_ext[aa][symb][4*pucch_pdu->prb_size],0,8*pucch_pdu->prb_size*sizeof(int16_t));
      }
    }
  }
  for (int symb=0; symb<pucch_pdu->nr_of_symbols;symb++) {
1195
    // 24 REs contains 48x16-bit, so 6x8x16-bit
Raymond Knopp's avatar
Raymond Knopp committed
1196 1197
    for (int prb=0;prb<pucch_pdu->prb_size;prb+=2) {
      for (int aa=0;aa<Prx;aa++) {
Raymond Knopp's avatar
Raymond Knopp committed
1198 1199 1200 1201
        r_re_ext_p=&r_re_ext[aa][symb][8*prb];
        r_im_ext_p=&r_im_ext[aa][symb][8*prb];
        rd_re_ext_p=&rd_re_ext[aa][symb][4*prb];
        rd_im_ext_p=&rd_im_ext[aa][symb][4*prb];
Raymond Knopp's avatar
Raymond Knopp committed
1202

1203
        for (int idx=0; idx<8; idx++) {
Raymond Knopp's avatar
Raymond Knopp committed
1204 1205 1206 1207 1208 1209
          r_re_ext_p[idx<<1]=rp[aa][symb][prb*24+6*idx];
          r_im_ext_p[idx<<1]=rp[aa][symb][prb*24+1+6*idx];
          rd_re_ext_p[idx]=rp[aa][symb][prb*24+2+6*idx];
          rd_im_ext_p[idx]=rp[aa][symb][prb*24+3+6*idx];
          r_re_ext_p[1+(idx<<1)]=rp[aa][symb][prb*24+4+6*idx];
          r_im_ext_p[1+(idx<<1)]=rp[aa][symb][prb*24+5+6*idx];
1210
        }
1211

1212
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1213 1214
        for (int i=0;i<8;i++) printf("Ant %d PRB %d dmrs[%d] -> (%d,%d)\n",aa,prb+(i>>2),i,rd_re_ext_p[i],rd_im_ext_p[i]);
        for (int i=0;i<16;i++) printf("Ant %d PRB %d data[%d] -> (%d,%d)\n",aa,prb+(i>>3),i,r_re_ext_p[i],r_im_ext_p[i]);
1215
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1216 1217
      } // aa
    } // prb
1218

Raymond Knopp's avatar
Raymond Knopp committed
1219
    // first compute DMRS component
1220

Raymond Knopp's avatar
Raymond Knopp committed
1221
    uint32_t x1, x2, s=0;
Raymond Knopp's avatar
Raymond Knopp committed
1222
    x2 = (((1<<17)*((14*slot) + (pucch_pdu->start_symbol_index+symb) + 1)*((2*pucch_pdu->dmrs_scrambling_id) + 1)) + (2*pucch_pdu->dmrs_scrambling_id))%(1U<<31); // c_init calculation according to TS38.211 subclause
1223
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1224 1225
    printf("slot %d, start_symbol_index %d, symbol %d, dmrs_scrambling_id %d\n",
           slot,pucch_pdu->start_symbol_index,symb,pucch_pdu->dmrs_scrambling_id);
1226
#endif
1227 1228 1229 1230 1231
    int reset = 1;
    for (int i=0; i<=(pucch_pdu->prb_start>>2); i++) {
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
    }
1232

Raymond Knopp's avatar
Raymond Knopp committed
1233 1234 1235 1236 1237
    for (int group=0;group<ngroup;group++) {
      // each group has 8*nc_group_size elements, compute 1 complex correlation with DMRS per group
      // non-coherent combining across groups
      dmrs_re = byte2m64_re[((uint8_t*)&s)[(group&1)<<1]];
      dmrs_im = byte2m64_im[((uint8_t*)&s)[(group&1)<<1]];
1238
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1239
      printf("Group %d: s %x x2 %x ((%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
Raymond Knopp's avatar
Raymond Knopp committed
1240 1241
             group,
             ((uint16_t*)&s)[0],x2,
1242 1243 1244 1245
             ((int16_t*)&dmrs_re)[0],((int16_t*)&dmrs_im)[0],
             ((int16_t*)&dmrs_re)[1],((int16_t*)&dmrs_im)[1],
             ((int16_t*)&dmrs_re)[2],((int16_t*)&dmrs_im)[2],
             ((int16_t*)&dmrs_re)[3],((int16_t*)&dmrs_im)[3]);
1246
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1247
      for (int aa=0;aa<Prx;aa++) {
Raymond Knopp's avatar
Raymond Knopp committed
1248 1249
        rd_re_ext_p=&rd_re_ext[aa][symb][8*group];
        rd_im_ext_p=&rd_im_ext[aa][symb][8*group];
1250

1251
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1252 1253 1254 1255 1256 1257
        printf("Group %d: rd ((%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
               group,
               rd_re_ext_p[0],rd_im_ext_p[0],
               rd_re_ext_p[1],rd_im_ext_p[1],
               rd_re_ext_p[2],rd_im_ext_p[2],
               rd_re_ext_p[3],rd_im_ext_p[3]);
1258
#endif
1259 1260 1261 1262 1263 1264 1265 1266
        corr32_re[symb][group][aa]+=(rd_re_ext_p[0]*((int16_t*)&dmrs_re)[0] + rd_im_ext_p[0]*((int16_t*)&dmrs_im)[0]);
        corr32_im[symb][group][aa]+=(-rd_re_ext_p[0]*((int16_t*)&dmrs_im)[0] + rd_im_ext_p[0]*((int16_t*)&dmrs_re)[0]);
        corr32_re[symb][group][aa]+=(rd_re_ext_p[1]*((int16_t*)&dmrs_re)[1] + rd_im_ext_p[1]*((int16_t*)&dmrs_im)[1]);
        corr32_im[symb][group][aa]+=(-rd_re_ext_p[1]*((int16_t*)&dmrs_im)[1] + rd_im_ext_p[1]*((int16_t*)&dmrs_re)[1]);
        corr32_re[symb][group][aa]+=(rd_re_ext_p[2]*((int16_t*)&dmrs_re)[2] + rd_im_ext_p[2]*((int16_t*)&dmrs_im)[2]);
        corr32_im[symb][group][aa]+=(-rd_re_ext_p[2]*((int16_t*)&dmrs_im)[2] + rd_im_ext_p[2]*((int16_t*)&dmrs_re)[2]);
        corr32_re[symb][group][aa]+=(rd_re_ext_p[3]*((int16_t*)&dmrs_re)[3] + rd_im_ext_p[3]*((int16_t*)&dmrs_im)[3]);
        corr32_im[symb][group][aa]+=(-rd_re_ext_p[3]*((int16_t*)&dmrs_im)[3] + rd_im_ext_p[3]*((int16_t*)&dmrs_re)[3]);
Raymond Knopp's avatar
Raymond Knopp committed
1267 1268 1269
      }
      dmrs_re = byte2m64_re[((uint8_t*)&s)[1+((group&1)<<1)]];
      dmrs_im = byte2m64_im[((uint8_t*)&s)[1+((group&1)<<1)]];
1270
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1271
      printf("Group %d: s %x ((%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
Raymond Knopp's avatar
Raymond Knopp committed
1272 1273
             group,
             ((uint16_t*)&s)[1],
1274 1275 1276
             ((int16_t*)&dmrs_re)[0],((int16_t*)&dmrs_im)[0],
             ((int16_t*)&dmrs_re)[1],((int16_t*)&dmrs_im)[1],
             ((int16_t*)&dmrs_re)[2],((int16_t*)&dmrs_im)[2],
Raymond Knopp's avatar
Raymond Knopp committed
1277
             ((int16_t*)&dmrs_re)[3],((int16_t*)&dmrs_im)[3]);
1278
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1279
      for (int aa=0;aa<Prx;aa++) {
Raymond Knopp's avatar
Raymond Knopp committed
1280 1281
        rd_re_ext_p=&rd_re_ext[aa][symb][8*group];
        rd_im_ext_p=&rd_im_ext[aa][symb][8*group];
1282
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1283 1284 1285 1286 1287 1288
        printf("Group %d: rd ((%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
               group,
               rd_re_ext_p[4],rd_im_ext_p[4],
               rd_re_ext_p[5],rd_im_ext_p[5],
               rd_re_ext_p[6],rd_im_ext_p[6],
               rd_re_ext_p[7],rd_im_ext_p[7]);
1289
#endif
1290 1291 1292 1293 1294 1295 1296 1297
        corr32_re[symb][group][aa]+=(rd_re_ext_p[4]*((int16_t*)&dmrs_re)[0] + rd_im_ext_p[4]*((int16_t*)&dmrs_im)[0]);
        corr32_im[symb][group][aa]+=(-rd_re_ext_p[4]*((int16_t*)&dmrs_im)[0] + rd_im_ext_p[4]*((int16_t*)&dmrs_re)[0]);
        corr32_re[symb][group][aa]+=(rd_re_ext_p[5]*((int16_t*)&dmrs_re)[1] + rd_im_ext_p[5]*((int16_t*)&dmrs_im)[1]);
        corr32_im[symb][group][aa]+=(-rd_re_ext_p[5]*((int16_t*)&dmrs_im)[1] + rd_im_ext_p[5]*((int16_t*)&dmrs_re)[1]);
        corr32_re[symb][group][aa]+=(rd_re_ext_p[6]*((int16_t*)&dmrs_re)[2] + rd_im_ext_p[6]*((int16_t*)&dmrs_im)[2]);
        corr32_im[symb][group][aa]+=(-rd_re_ext_p[6]*((int16_t*)&dmrs_im)[2] + rd_im_ext_p[6]*((int16_t*)&dmrs_re)[2]);
        corr32_re[symb][group][aa]+=(rd_re_ext_p[7]*((int16_t*)&dmrs_re)[3] + rd_im_ext_p[7]*((int16_t*)&dmrs_im)[3]);
        corr32_im[symb][group][aa]+=(-rd_re_ext_p[7]*((int16_t*)&dmrs_im)[3] + rd_im_ext_p[7]*((int16_t*)&dmrs_re)[3]);
Raymond Knopp's avatar
Raymond Knopp committed
1298 1299
        /*	corr32_re[group][aa]>>=5;
                corr32_im[group][aa]>>=5;*/
1300
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1301
        printf("Group %d: corr32 (%d,%d)\n",group,corr32_re[symb][group][aa],corr32_im[symb][group][aa]);
1302
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1303
      } //aa    
1304

Raymond Knopp's avatar
Raymond Knopp committed
1305 1306
      if ((group&1) == 1) s = lte_gold_generic(&x1, &x2, 0);
    } // group
Raymond Knopp's avatar
Raymond Knopp committed
1307
  } // symb
1308 1309 1310 1311 1312 1313 1314 1315

  uint32_t x1, x2, s=0;  
  // unscrambling
  x2 = ((pucch_pdu->rnti)<<15)+pucch_pdu->data_scrambling_id;
  s = lte_gold_generic(&x1, &x2, 1);
#ifdef DEBUG_NR_PUCCH_RX
  printf("x2 %x, s %x\n",x2,s);
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
  for (int symb=0;symb<pucch_pdu->nr_of_symbols;symb++) {
    __m64 c_re0,c_im0,c_re1,c_im1,c_re2,c_im2,c_re3,c_im3;
    int re_off=0;
    for (int prb=0;prb<prb_size_ext;prb+=2,re_off+=16) {
      c_re0 = byte2m64_re[((uint8_t*)&s)[0]];
      c_im0 = byte2m64_im[((uint8_t*)&s)[0]];
      c_re1 = byte2m64_re[((uint8_t*)&s)[1]];
      c_im1 = byte2m64_im[((uint8_t*)&s)[1]];
      c_re2 = byte2m64_re[((uint8_t*)&s)[2]];
      c_im2 = byte2m64_im[((uint8_t*)&s)[2]];
      c_re3 = byte2m64_re[((uint8_t*)&s)[3]];
      c_im3 = byte2m64_im[((uint8_t*)&s)[3]];
1328

Raymond Knopp's avatar
Raymond Knopp committed
1329
      for (int aa=0;aa<Prx;aa++) {
1330
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
        printf("prb %d: rd ((%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
               prb,
               r_re_ext[aa][symb][re_off],r_im_ext[aa][symb][re_off],
               r_re_ext[aa][symb][re_off+1],r_im_ext[aa][symb][re_off+1],
               r_re_ext[aa][symb][re_off+2],r_im_ext[aa][symb][re_off+2],
               r_re_ext[aa][symb][re_off+3],r_im_ext[aa][symb][re_off+3],
               r_re_ext[aa][symb][re_off+4],r_im_ext[aa][symb][re_off+4],
               r_re_ext[aa][symb][re_off+5],r_im_ext[aa][symb][re_off+5],
               r_re_ext[aa][symb][re_off+6],r_im_ext[aa][symb][re_off+6],
               r_re_ext[aa][symb][re_off+7],r_im_ext[aa][symb][re_off+7]);
        printf("prb %d (%x): c ((%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
               prb,s,
               ((int16_t*)&c_re0)[0],((int16_t*)&c_im0)[0],
               ((int16_t*)&c_re0)[1],((int16_t*)&c_im0)[1],
               ((int16_t*)&c_re0)[2],((int16_t*)&c_im0)[2],
               ((int16_t*)&c_re0)[3],((int16_t*)&c_im0)[3],
               ((int16_t*)&c_re1)[0],((int16_t*)&c_im1)[0],
               ((int16_t*)&c_re1)[1],((int16_t*)&c_im1)[1],
               ((int16_t*)&c_re1)[2],((int16_t*)&c_im1)[2],
               ((int16_t*)&c_re1)[3],((int16_t*)&c_im1)[3]
               );
        printf("prb %d: rd ((%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
               prb+1,
               r_re_ext[aa][symb][re_off+8],r_im_ext[aa][symb][re_off+8],
               r_re_ext[aa][symb][re_off+9],r_im_ext[aa][symb][re_off+9],
               r_re_ext[aa][symb][re_off+10],r_im_ext[aa][symb][re_off+10],
               r_re_ext[aa][symb][re_off+11],r_im_ext[aa][symb][re_off+11],
               r_re_ext[aa][symb][re_off+12],r_im_ext[aa][symb][re_off+12],
               r_re_ext[aa][symb][re_off+13],r_im_ext[aa][symb][re_off+13],
               r_re_ext[aa][symb][re_off+14],r_im_ext[aa][symb][re_off+14],
               r_re_ext[aa][symb][re_off+15],r_im_ext[aa][symb][re_off+15]);
        printf("prb %d (%x): c ((%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
               prb+1,s,
               ((int16_t*)&c_re2)[0],((int16_t*)&c_im2)[0],
               ((int16_t*)&c_re2)[1],((int16_t*)&c_im2)[1],
               ((int16_t*)&c_re2)[2],((int16_t*)&c_im2)[2],
               ((int16_t*)&c_re2)[3],((int16_t*)&c_im2)[3],
               ((int16_t*)&c_re3)[0],((int16_t*)&c_im3)[0],
               ((int16_t*)&c_re3)[1],((int16_t*)&c_im3)[1],
               ((int16_t*)&c_re3)[2],((int16_t*)&c_im3)[2],
               ((int16_t*)&c_re3)[3],((int16_t*)&c_im3)[3]
               );
1373 1374
#endif

Raymond Knopp's avatar
Raymond Knopp committed
1375 1376 1377 1378
        ((__m64*)&r_re_ext2[aa][symb][re_off])[0] = _mm_mullo_pi16(((__m64*)&r_re_ext[aa][symb][re_off])[0],c_im0);
        ((__m64*)&r_re_ext[aa][symb][re_off])[0] = _mm_mullo_pi16(((__m64*)&r_re_ext[aa][symb][re_off])[0],c_re0);
        ((__m64*)&r_im_ext2[aa][symb][re_off])[0] = _mm_mullo_pi16(((__m64*)&r_im_ext[aa][symb][re_off])[0],c_re0);
        ((__m64*)&r_im_ext[aa][symb][re_off])[0] = _mm_mullo_pi16(((__m64*)&r_im_ext[aa][symb][re_off])[0],c_im0);
1379

Raymond Knopp's avatar
Raymond Knopp committed
1380 1381 1382 1383
        ((__m64*)&r_re_ext2[aa][symb][re_off])[1] = _mm_mullo_pi16(((__m64*)&r_re_ext[aa][symb][re_off])[1],c_im1);
        ((__m64*)&r_re_ext[aa][symb][re_off])[1] = _mm_mullo_pi16(((__m64*)&r_re_ext[aa][symb][re_off])[1],c_re1);
        ((__m64*)&r_im_ext2[aa][symb][re_off])[1] = _mm_mullo_pi16(((__m64*)&r_im_ext[aa][symb][re_off])[1],c_re1);
        ((__m64*)&r_im_ext[aa][symb][re_off])[1] = _mm_mullo_pi16(((__m64*)&r_im_ext[aa][symb][re_off])[1],c_im1);
1384

Raymond Knopp's avatar
Raymond Knopp committed
1385 1386 1387 1388
        ((__m64*)&r_re_ext2[aa][symb][re_off])[2] = _mm_mullo_pi16(((__m64*)&r_re_ext[aa][symb][re_off])[2],c_im2);
        ((__m64*)&r_re_ext[aa][symb][re_off])[2] = _mm_mullo_pi16(((__m64*)&r_re_ext[aa][symb][re_off])[2],c_re2);
        ((__m64*)&r_im_ext2[aa][symb][re_off])[2] = _mm_mullo_pi16(((__m64*)&r_im_ext[aa][symb][re_off])[2],c_re2);
        ((__m64*)&r_im_ext[aa][symb][re_off])[2] = _mm_mullo_pi16(((__m64*)&r_im_ext[aa][symb][re_off])[2],c_im2);
1389

Raymond Knopp's avatar
Raymond Knopp committed
1390 1391 1392 1393
        ((__m64*)&r_re_ext2[aa][symb][re_off])[3] = _mm_mullo_pi16(((__m64*)&r_re_ext[aa][symb][re_off])[3],c_im3);
        ((__m64*)&r_re_ext[aa][symb][re_off])[3] = _mm_mullo_pi16(((__m64*)&r_re_ext[aa][symb][re_off])[3],c_re3);
        ((__m64*)&r_im_ext2[aa][symb][re_off])[3] = _mm_mullo_pi16(((__m64*)&r_im_ext[aa][symb][re_off])[3],c_re3);
        ((__m64*)&r_im_ext[aa][symb][re_off])[3] = _mm_mullo_pi16(((__m64*)&r_im_ext[aa][symb][re_off])[3],c_im3);
1394 1395

#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
        printf("prb %d: r ((%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
               prb,
               r_re_ext[aa][symb][re_off],r_im_ext[aa][symb][re_off],
               r_re_ext[aa][symb][re_off+1],r_im_ext[aa][symb][re_off+1],
               r_re_ext[aa][symb][re_off+2],r_im_ext[aa][symb][re_off+2],
               r_re_ext[aa][symb][re_off+3],r_im_ext[aa][symb][re_off+3],
               r_re_ext[aa][symb][re_off+4],r_im_ext[aa][symb][re_off+4],
               r_re_ext[aa][symb][re_off+5],r_im_ext[aa][symb][re_off+5],
               r_re_ext[aa][symb][re_off+6],r_im_ext[aa][symb][re_off+6],
               r_re_ext[aa][symb][re_off+7],r_im_ext[aa][symb][re_off+7]);
        printf("prb %d: r ((%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d),(%d,%d))\n",
               prb+1,
               r_re_ext[aa][symb][re_off+8],r_im_ext[aa][symb][re_off+8],
               r_re_ext[aa][symb][re_off+9],r_im_ext[aa][symb][re_off+9],
               r_re_ext[aa][symb][re_off+10],r_im_ext[aa][symb][re_off+10],
               r_re_ext[aa][symb][re_off+11],r_im_ext[aa][symb][re_off+11],
               r_re_ext[aa][symb][re_off+12],r_im_ext[aa][symb][re_off+12],
               r_re_ext[aa][symb][re_off+13],r_im_ext[aa][symb][re_off+13],
               r_re_ext[aa][symb][re_off+14],r_im_ext[aa][symb][re_off+14],
               r_re_ext[aa][symb][re_off+15],r_im_ext[aa][symb][re_off+15]);
1416
#endif      
Raymond Knopp's avatar
Raymond Knopp committed
1417 1418
      }
      s = lte_gold_generic(&x1, &x2, 0);
1419
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1420
      printf("\n");
1421
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1422 1423
    }
  } //symb
1424 1425
  int nb_bit = pucch_pdu->bit_len_harq+pucch_pdu->sr_flag+pucch_pdu->bit_len_csi_part1+pucch_pdu->bit_len_csi_part2;
  AssertFatal(nb_bit > 2  && nb_bit< 65,"illegal length (%d : %d,%d,%d,%d)\n",nb_bit,pucch_pdu->bit_len_harq,pucch_pdu->sr_flag,pucch_pdu->bit_len_csi_part1,pucch_pdu->bit_len_csi_part2);
1426

1427 1428 1429
  uint64_t decodedPayload[2];
  uint8_t corr_dB;
  int decoderState=2;
1430
  if (nb_bit < 12) { // short blocklength case
Raymond Knopp's avatar
Raymond Knopp committed
1431 1432 1433 1434
    __m256i *rp_re[Prx2][2];
    __m256i *rp2_re[Prx2][2];
    __m256i *rp_im[Prx2][2];
    __m256i *rp2_im[Prx2][2];
1435
    for (int aa=0;aa<Prx;aa++) {
Raymond Knopp's avatar
Raymond Knopp committed
1436 1437 1438 1439 1440 1441
      for (int symb=0;symb<pucch_pdu->nr_of_symbols;symb++) {
        rp_re[aa][symb] = (__m256i*)r_re_ext[aa][symb];
        rp_im[aa][symb] = (__m256i*)r_im_ext[aa][symb];
        rp2_re[aa][symb] = (__m256i*)r_re_ext2[aa][symb];
        rp2_im[aa][symb] = (__m256i*)r_im_ext2[aa][symb];
      }
1442
    }
1443
    __m256i prod_re[Prx2],prod_im[Prx2];
Francesco Mani's avatar
Francesco Mani committed
1444
    uint64_t corr=0;
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
    int cw_ML=0;
    
    
    for (int cw=0;cw<1<<nb_bit;cw++) {
#ifdef DEBUG_NR_PUCCH_RX
      printf("cw %d:",cw);
      for (int i=0;i<32;i+=2) {
	printf("%d,%d,",
	       ((int16_t*)&pucch2_lut[nb_bit-3][cw<<1])[i>>1],
	       ((int16_t*)&pucch2_lut[nb_bit-3][cw<<1])[1+(i>>1)]);
      }
      printf("\n");
#endif
Francesco Mani's avatar
Francesco Mani committed
1458
      uint64_t corr_tmp = 0;
1459

Raymond Knopp's avatar
Raymond Knopp committed
1460 1461 1462 1463 1464 1465 1466 1467
      for (int symb=0;symb<pucch_pdu->nr_of_symbols;symb++) {
        for (int group=0;group<ngroup;group++) {
          // do complex correlation
          for (int aa=0;aa<Prx;aa++) {
            prod_re[aa] = /*_mm256_srai_epi16(*/_mm256_adds_epi16(_mm256_mullo_epi16(pucch2_lut[nb_bit-3][cw<<1],rp_re[aa][symb][group]),
                                                                  _mm256_mullo_epi16(pucch2_lut[nb_bit-3][(cw<<1)+1],rp_im[aa][symb][group]))/*,5)*/;
            prod_im[aa] = /*_mm256_srai_epi16(*/_mm256_subs_epi16(_mm256_mullo_epi16(pucch2_lut[nb_bit-3][cw<<1],rp2_im[aa][symb][group]),
                                                                  _mm256_mullo_epi16(pucch2_lut[nb_bit-3][(cw<<1)+1],rp2_re[aa][symb][group]))/*,5)*/;
1468
#ifdef DEBUG_NR_PUCCH_RX
Raymond Knopp's avatar
Raymond Knopp committed
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
            printf("prod_re[%d] => (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)\n",aa,
                   ((int16_t*)&prod_re[aa])[0],((int16_t*)&prod_re[aa])[1],((int16_t*)&prod_re[aa])[2],((int16_t*)&prod_re[aa])[3],
                   ((int16_t*)&prod_re[aa])[4],((int16_t*)&prod_re[aa])[5],((int16_t*)&prod_re[aa])[6],((int16_t*)&prod_re[aa])[7],
                   ((int16_t*)&prod_re[aa])[8],((int16_t*)&prod_re[aa])[9],((int16_t*)&prod_re[aa])[10],((int16_t*)&prod_re[aa])[11],
                   ((int16_t*)&prod_re[aa])[12],((int16_t*)&prod_re[aa])[13],((int16_t*)&prod_re[aa])[14],((int16_t*)&prod_re[aa])[15]);
            printf("prod_im[%d] => (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)\n",aa,
                   ((int16_t*)&prod_im[aa])[0],((int16_t*)&prod_im[aa])[1],((int16_t*)&prod_im[aa])[2],((int16_t*)&prod_im[aa])[3],
                   ((int16_t*)&prod_im[aa])[4],((int16_t*)&prod_im[aa])[5],((int16_t*)&prod_im[aa])[6],((int16_t*)&prod_im[aa])[7],
                   ((int16_t*)&prod_im[aa])[8],((int16_t*)&prod_im[aa])[9],((int16_t*)&prod_im[aa])[10],((int16_t*)&prod_im[aa])[11],
                   ((int16_t*)&prod_im[aa])[12],((int16_t*)&prod_im[aa])[13],((int16_t*)&prod_im[aa])[14],((int16_t*)&prod_im[aa])[15]);
1479

Raymond Knopp's avatar
Raymond Knopp committed
1480
#endif
Raymond Knopp's avatar
Raymond Knopp committed
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
            prod_re[aa] = _mm256_hadds_epi16(prod_re[aa],prod_re[aa]);// 0+1
            prod_im[aa] = _mm256_hadds_epi16(prod_im[aa],prod_im[aa]);
            prod_re[aa] = _mm256_hadds_epi16(prod_re[aa],prod_re[aa]);// 0+1+2+3
            prod_im[aa] = _mm256_hadds_epi16(prod_im[aa],prod_im[aa]);
            prod_re[aa] = _mm256_hadds_epi16(prod_re[aa],prod_re[aa]);// 0+1+2+3+4+5+6+7
            prod_im[aa] = _mm256_hadds_epi16(prod_im[aa],prod_im[aa]);
            prod_re[aa] = _mm256_hadds_epi16(prod_re[aa],prod_re[aa]);// 0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15
            prod_im[aa] = _mm256_hadds_epi16(prod_im[aa],prod_im[aa]);
          }
          int64_t corr_re=0,corr_im=0;
1491 1492


Raymond Knopp's avatar
Raymond Knopp committed
1493 1494 1495 1496 1497 1498 1499 1500
          for (int aa=0;aa<Prx;aa++) {
#ifdef DEBUG_NR_PUCCH_RX
            printf("pucch2 cw %d group %d aa %d: (%d,%d)+(%d,%d) = (%d,%d)\n",cw,group,aa,
              corr32_re[symb][group][aa],corr32_im[symb][group][aa],
              ((int16_t*)(&prod_re[aa]))[0],
              ((int16_t*)(&prod_im[aa]))[0],
              corr32_re[symb][group][aa]+((int16_t*)(&prod_re[aa]))[0],
              corr32_im[symb][group][aa]+((int16_t*)(&prod_im[aa]))[0]);
1501

Raymond Knopp's avatar
Raymond Knopp committed
1502 1503 1504 1505 1506 1507 1508
#endif
            LOG_D(PHY,"pucch2 cw %d group %d aa %d: (%d,%d)+(%d,%d) = (%d,%d)\n",cw,group,aa,
              corr32_re[symb][group][aa],corr32_im[symb][group][aa],
              ((int16_t*)(&prod_re[aa]))[0],
              ((int16_t*)(&prod_im[aa]))[0],
              corr32_re[symb][group][aa]+((int16_t*)(&prod_re[aa]))[0],
              corr32_im[symb][group][aa]+((int16_t*)(&prod_im[aa]))[0]);
1509

Raymond Knopp's avatar
Raymond Knopp committed
1510 1511
            corr_re = ( corr32_re[symb][group][aa]+((int16_t*)(&prod_re[aa]))[0]);
            corr_im = ( corr32_im[symb][group][aa]+((int16_t*)(&prod_im[aa]))[0]);
1512 1513

            corr_tmp += corr_re*corr_re + corr_im*corr_im;
Raymond Knopp's avatar
Raymond Knopp committed
1514 1515
          } // aa loop
        }// group loop
1516
      } // symb loop
1517
      if (corr_tmp > corr) {
Raymond Knopp's avatar
Raymond Knopp committed
1518 1519
         corr = corr_tmp;
         cw_ML=cw;
1520
      }
1521
    } // cw loop
1522
    corr_dB = dB_fixed64((uint64_t)corr);
Raymond Knopp's avatar
Raymond Knopp committed
1523 1524 1525
#ifdef DEBUG_NR_PUCCH_RX
    printf("cw_ML %d, metric %d dB\n",cw_ML,corr_dB);
#endif
Robert Schmidt's avatar
Robert Schmidt committed
1526
    LOG_D(PHY,"cw_ML %d, metric %d dB\n",cw_ML,corr_dB);
1527
    decodedPayload[0]=(uint64_t)cw_ML;
1528 1529
  }
  else { // polar coded case
1530 1531

    t_nrPolar_params *currentPtr = nr_polar_params(2,nb_bit,pucch_pdu->prb_size,1,&gNB->uci_polarParams);
Raymond Knopp's avatar
Raymond Knopp committed
1532 1533 1534 1535 1536
    __m64 *rp_re[Prx2][2];
    __m64 *rp2_re[Prx2][2];
    __m64 *rp_im[Prx2][2];
    __m64 *rp2_im[Prx2][2];
    __m128i llrs[pucch_pdu->prb_size*2*pucch_pdu->nr_of_symbols];
1537

Raymond Knopp's avatar
Raymond Knopp committed
1538
    for (int aa=0;aa<Prx;aa++) {
Raymond Knopp's avatar
Raymond Knopp committed
1539 1540 1541 1542 1543 1544
      for (int symb=0;symb<pucch_pdu->nr_of_symbols;symb++) {
        rp_re[aa][symb] = (__m64*)r_re_ext[aa][symb];
        rp_im[aa][symb] = (__m64*)r_im_ext[aa][symb];
        rp2_re[aa][symb] = (__m64*)r_re_ext2[aa][symb];
        rp2_im[aa][symb] = (__m64*)r_im_ext2[aa][symb];
      }
1545
    }
Raymond Knopp's avatar
Raymond Knopp committed
1546
    __m64 prod_re[Prx2],prod_im[Prx2];
1547 1548

#ifdef DEBUG_NR_PUCCH_RX
1549 1550
    for (int cw=0;cw<16;cw++) {

Raymond Knopp's avatar
Raymond Knopp committed
1551 1552 1553 1554 1555 1556
      printf("cw %d:",cw);
      for (int i=0;i<4;i++) {
	printf("%d,",
	       ((int16_t*)&pucch2_polar_4bit[cw])[i>>1]);
      }
      printf("\n");
1557
    }
1558
#endif
1559
    
Raymond Knopp's avatar
Raymond Knopp committed
1560 1561
    // non-coherent LLR computation on groups of 4 REs (half-PRBs)
    int32_t corr_re,corr_im,corr_tmp;
1562
    __m128i corr16,llr_num,llr_den;
Raphael Defosseux's avatar
Raphael Defosseux committed
1563
    uint64_t corr = 0;
Raymond Knopp's avatar
Raymond Knopp committed
1564 1565 1566 1567 1568
    for (int symb=0;symb<pucch_pdu->nr_of_symbols;symb++) {
      for (int half_prb=0;half_prb<(2*pucch_pdu->prb_size);half_prb++) {
        llr_num=_mm_set1_epi16(0);llr_den=_mm_set1_epi16(0);
        for (int cw=0;cw<256;cw++) {
          corr_tmp=0;
1569
          for (int aa=0;aa<Prx;aa++) {
Raymond Knopp's avatar
Raymond Knopp committed
1570 1571 1572 1573 1574 1575 1576 1577
            prod_re[aa] = _mm_srai_pi16(_mm_adds_pi16(_mm_mullo_pi16(pucch2_polar_4bit[cw&15],rp_re[aa][symb][half_prb]),
                                                      _mm_mullo_pi16(pucch2_polar_4bit[cw>>4],rp_im[aa][symb][half_prb])),5);
            prod_im[aa] = _mm_srai_pi16(_mm_subs_pi16(_mm_mullo_pi16(pucch2_polar_4bit[cw&15],rp2_im[aa][symb][half_prb]),
                                                      _mm_mullo_pi16(pucch2_polar_4bit[cw>>4],rp2_re[aa][symb][half_prb])),5);
            prod_re[aa] = _mm_hadds_pi16(prod_re[aa],prod_re[aa]);// 0+1
            prod_im[aa] = _mm_hadds_pi16(prod_im[aa],prod_im[aa]);
            prod_re[aa] = _mm_hadds_pi16(prod_re[aa],prod_re[aa]);// 0+1+2+3
            prod_im[aa] = _mm_hadds_pi16(prod_im[aa],prod_im[aa]);
1578

Raymond Knopp's avatar
Raymond Knopp committed
1579 1580 1581
            // this is for UL CQI measurement
            if (cw==0) corr += ((int64_t)corr32_re[symb][half_prb>>2][aa]*corr32_re[symb][half_prb>>2][aa])+
                         ((int64_t)corr32_im[symb][half_prb>>2][aa]*corr32_im[symb][half_prb>>2][aa]);
1582 1583


Raymond Knopp's avatar
Raymond Knopp committed
1584 1585 1586
            corr_re = ( corr32_re[symb][half_prb>>2][aa]/(2*nc_group_size*4/2)+((int16_t*)(&prod_re[aa]))[0]);
            corr_im = ( corr32_im[symb][half_prb>>2][aa]/(2*nc_group_size*4/2)+((int16_t*)(&prod_im[aa]))[0]);
            corr_tmp += corr_re*corr_re + corr_im*corr_im;
1587
           /*
Raymond Knopp's avatar
Raymond Knopp committed
1588 1589 1590 1591 1592
              LOG_D(PHY,"pucch2 half_prb %d cw %d (%d,%d) aa %d: (%d,%d,%d,%d,%d,%d,%d,%d)x(%d,%d,%d,%d,%d,%d,%d,%d)  (%d,%d)+(%d,%d) = (%d,%d) => %d\n",
              half_prb,cw,cw&15,cw>>4,aa,
              ((int16_t*)&pucch2_polar_4bit[cw&15])[0],((int16_t*)&pucch2_polar_4bit[cw>>4])[0],
              ((int16_t*)&pucch2_polar_4bit[cw&15])[1],((int16_t*)&pucch2_polar_4bit[cw>>4])[1],
              ((int16_t*)&pucch2_polar_4bit[cw&15])[2],((int16_t*)&pucch2_polar_4bit[cw>>4])[2],
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
                ((int16_t*)&pucch2_polar_4bit[cw&15])[3],((int16_t*)&pucch2_polar_4bit[cw>>4])[3],
                ((int16_t*)&rp_re[aa][half_prb])[0],((int16_t*)&rp_im[aa][half_prb])[0],
                ((int16_t*)&rp_re[aa][half_prb])[1],((int16_t*)&rp_im[aa][half_prb])[1],
                ((int16_t*)&rp_re[aa][half_prb])[2],((int16_t*)&rp_im[aa][half_prb])[2],
                ((int16_t*)&rp_re[aa][half_prb])[3],((int16_t*)&rp_im[aa][half_prb])[3],
                corr32_re[half_prb>>2][aa]/(2*nc_group_size*4/2),corr32_im[half_prb>>2][aa]/(2*nc_group_size*4/2),
                ((int16_t*)(&prod_re[aa]))[0],
                ((int16_t*)(&prod_im[aa]))[0],
                corr_re,
                corr_im,
                corr_tmp);
1604
*/
Raymond Knopp's avatar
Raymond Knopp committed
1605
	}
1606
	corr16 = _mm_set1_epi16((int16_t)(corr_tmp>>8));
1607

1608
	LOG_D(PHY,"half_prb %d cw %d corr16 %d\n",half_prb,cw,corr_tmp>>8);
1609

1610 1611
	llr_num = _mm_max_epi16(_mm_mullo_epi16(corr16,pucch2_polar_llr_num_lut[cw]),llr_num);
	llr_den = _mm_max_epi16(_mm_mullo_epi16(corr16,pucch2_polar_llr_den_lut[cw]),llr_den);
1612

1613 1614 1615 1616 1617 1618 1619 1620 1621
	LOG_D(PHY,"lut_num (%d,%d,%d,%d,%d,%d,%d,%d)\n",
	      ((int16_t*)&pucch2_polar_llr_num_lut[cw])[0],
	      ((int16_t*)&pucch2_polar_llr_num_lut[cw])[1],
	      ((int16_t*)&pucch2_polar_llr_num_lut[cw])[2],
	      ((int16_t*)&pucch2_polar_llr_num_lut[cw])[3],
	      ((int16_t*)&pucch2_polar_llr_num_lut[cw])[4],
	      ((int16_t*)&pucch2_polar_llr_num_lut[cw])[5],
	      ((int16_t*)&pucch2_polar_llr_num_lut[cw])[6],
	      ((int16_t*)&pucch2_polar_llr_num_lut[cw])[7]);
1622

1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
	LOG_D(PHY,"llr_num (%d,%d,%d,%d,%d,%d,%d,%d)\n",
	      ((int16_t*)&llr_num)[0],
	      ((int16_t*)&llr_num)[1],
	      ((int16_t*)&llr_num)[2],
	      ((int16_t*)&llr_num)[3],
	      ((int16_t*)&llr_num)[4],
	      ((int16_t*)&llr_num)[5],
	      ((int16_t*)&llr_num)[6],
	      ((int16_t*)&llr_num)[7]);
	LOG_D(PHY,"llr_den (%d,%d,%d,%d,%d,%d,%d,%d)\n",
	      ((int16_t*)&llr_den)[0],
	      ((int16_t*)&llr_den)[1],
	      ((int16_t*)&llr_den)[2],
	      ((int16_t*)&llr_den)[3],
	      ((int16_t*)&llr_den)[4],
	      ((int16_t*)&llr_den)[5],
	      ((int16_t*)&llr_den)[6],
	      ((int16_t*)&llr_den)[7]);
1641

1642
      }
1643
      // compute llrs
Raymond Knopp's avatar
Raymond Knopp committed
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
        llrs[half_prb + (symb*2*pucch_pdu->prb_size)] = _mm_subs_epi16(llr_num,llr_den);
        LOG_D(PHY,"llrs[%d] : (%d,%d,%d,%d,%d,%d,%d,%d)\n",
              half_prb,
              ((int16_t*)&llrs[half_prb])[0],
              ((int16_t*)&llrs[half_prb])[1],
              ((int16_t*)&llrs[half_prb])[2],
              ((int16_t*)&llrs[half_prb])[3],
              ((int16_t*)&llrs[half_prb])[4],
              ((int16_t*)&llrs[half_prb])[5],
              ((int16_t*)&llrs[half_prb])[6],
              ((int16_t*)&llrs[half_prb])[7]);
      } // half_prb
    } // symb
1657 1658
    // run polar decoder on llrs
    decoderState = polar_decoder_int16((int16_t*)llrs, decodedPayload, 0, currentPtr);
1659
    LOG_D(PHY,"UCI decoderState %d, payload[0] %llu\n",decoderState,(unsigned long long)decodedPayload[0]);
1660 1661 1662
    if (decoderState>0) decoderState=1;
    corr_dB = dB_fixed64(corr);
    LOG_D(PHY,"metric %d dB\n",corr_dB);
1663
  }
1664

Raymond Knopp's avatar
Raymond Knopp committed
1665 1666
    // estimate CQI for MAC (from antenna port 0 only)
  int SNRtimes10 = dB_fixed_times10(signal_energy_nodc(&rxdataF[0][soffset+(l2*frame_parms->ofdm_symbol_size)+re_offset[0]],
1667
                                                       12*pucch_pdu->prb_size)) -
Raymond Knopp's avatar
Raymond Knopp committed
1668
                                                       (10*gNB->measurements.n0_power_tot_dB);
1669
  int cqi,bit_left;
1670 1671 1672 1673
  if (SNRtimes10 < -640) cqi=0;
  else if (SNRtimes10 >  635) cqi=255;
  else cqi=(640+SNRtimes10)/5;

1674
  uci_pdu->harq.harq_bit_len = pucch_pdu->bit_len_harq;
1675 1676 1677 1678
  uci_pdu->pduBitmap=0;
  uci_pdu->rnti=pucch_pdu->rnti;
  uci_pdu->handle=pucch_pdu->handle;
  uci_pdu->pucch_format=0;
1679 1680
  uci_pdu->ul_cqi=cqi;
  uci_pdu->timing_advance=0xffff; // currently not valid
Raymond Knopp's avatar
Raymond Knopp committed
1681
  uci_pdu->rssi=1280 - (10*dB_fixed(32767*32767)-dB_fixed_times10(signal_energy_nodc(&rxdataF[0][soffset+(l2*frame_parms->ofdm_symbol_size)+re_offset[0]],12*pucch_pdu->prb_size)));
1682 1683 1684
  if (pucch_pdu->bit_len_harq>0) {
    int harq_bytes=pucch_pdu->bit_len_harq>>3;
    if ((pucch_pdu->bit_len_harq&7) > 0) harq_bytes++;
1685
    uci_pdu->pduBitmap|=2;
1686
    uci_pdu->harq.harq_payload = (uint8_t*)malloc(harq_bytes);
1687
    uci_pdu->harq.harq_crc = decoderState;
1688 1689 1690 1691 1692
    int i=0;
    for (;i<harq_bytes-1;i++) {
      uci_pdu->harq.harq_payload[i] = decodedPayload[0] & 255;
      decodedPayload[0]>>=8;
    }
Francesco Mani's avatar
Francesco Mani committed
1693
    bit_left = pucch_pdu->bit_len_harq-((harq_bytes-1)<<3);
1694
    uci_pdu->harq.harq_payload[i] = decodedPayload[0] & ((1<<bit_left)-1);
1695
    decodedPayload[0] >>= pucch_pdu->bit_len_harq;
1696 1697 1698
  }
  
  if (pucch_pdu->sr_flag == 1) {
1699
    uci_pdu->pduBitmap|=1;
1700 1701
    uci_pdu->sr.sr_bit_len = 1;
    uci_pdu->sr.sr_payload = malloc(1);
1702
    uci_pdu->sr.sr_payload[0] = decodedPayload[0]&1;
1703
    decodedPayload[0] = decodedPayload[0]>>1;
1704
  }
1705 1706 1707
  // csi
  if (pucch_pdu->bit_len_csi_part1>0) {
    uci_pdu->pduBitmap|=4;
1708
    uci_pdu->csi_part1.csi_part1_bit_len=pucch_pdu->bit_len_csi_part1;
1709 1710 1711
    int csi_part1_bytes=pucch_pdu->bit_len_csi_part1>>3;
    if ((pucch_pdu->bit_len_csi_part1&7) > 0) csi_part1_bytes++;
    uci_pdu->csi_part1.csi_part1_payload = (uint8_t*)malloc(csi_part1_bytes);
1712
    uci_pdu->csi_part1.csi_part1_crc = decoderState;
1713 1714 1715 1716 1717
    int i=0;
    for (;i<csi_part1_bytes-1;i++) {
      uci_pdu->csi_part1.csi_part1_payload[i] = decodedPayload[0] & 255;
      decodedPayload[0]>>=8;
    }
Francesco Mani's avatar
Francesco Mani committed
1718
    bit_left = pucch_pdu->bit_len_csi_part1-((csi_part1_bytes-1)<<3);
1719
    uci_pdu->csi_part1.csi_part1_payload[i] = decodedPayload[0] & ((1<<bit_left)-1);
1720
    decodedPayload[0] >>= pucch_pdu->bit_len_csi_part1;
1721 1722 1723 1724 1725
  }
  
  if (pucch_pdu->bit_len_csi_part2>0) {
    uci_pdu->pduBitmap|=8;
  }
1726
}
1727

rmagueta's avatar
rmagueta committed
1728
void nr_dump_uci_stats(FILE *fd,PHY_VARS_gNB *gNB,int frame) {
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741

   int strpos=0;
   char output[16384];

   for (int i=0;i<NUMBER_OF_NR_UCI_STATS_MAX;i++){
      if (gNB->uci_stats[i].rnti>0) {
         NR_gNB_UCI_STATS_t *uci_stats = &gNB->uci_stats[i];
         if (uci_stats->pucch0_sr_trials > 0)
             strpos+=sprintf(output+strpos,"UCI %d RNTI %x: pucch0_sr_trials %d, pucch0_n00 %d dB, pucch0_n01 %d dB, pucch0_sr_thres %d dB, current pucch1_stat0 %d dB, current pucch1_stat1 %d dB, positive SR count %d\n",
                             i,uci_stats->rnti,uci_stats->pucch0_sr_trials,uci_stats->pucch0_n00,uci_stats->pucch0_n01,uci_stats->pucch0_sr_thres,dB_fixed(uci_stats->current_pucch0_sr_stat0),dB_fixed(uci_stats->current_pucch0_sr_stat1),uci_stats->pucch0_positive_SR);
         if (uci_stats->pucch01_trials > 0)
            strpos+=sprintf(output+strpos,"UCI %d RNTI %x: pucch01_trials %d, pucch0_n00 %d dB, pucch0_n01 %d dB, pucch0_thres %d dB, current pucch0_stat0 %d dB, current pucch1_stat1 %d dB, pucch01_DTX %d\n",
                            i,uci_stats->rnti,uci_stats->pucch01_trials,uci_stats->pucch0_n01,uci_stats->pucch0_n01,uci_stats->pucch0_thres,dB_fixed(uci_stats->current_pucch0_stat0),dB_fixed(uci_stats->current_pucch0_stat1),uci_stats->pucch01_DTX);
1742

1743 1744 1745 1746
         if (uci_stats->pucch02_trials > 0)
             strpos+=sprintf(output+strpos,"UCI %d RNTI %x: pucch01_trials %d, pucch0_n00 %d dB, pucch0_n01 %d dB, pucch0_thres %d dB, current pucch0_stat0 %d dB, current pucch0_stat1 %d dB, pucch01_DTX %d\n",
                             i,uci_stats->rnti,uci_stats->pucch02_trials,uci_stats->pucch0_n00,uci_stats->pucch0_n01,uci_stats->pucch0_thres,dB_fixed(uci_stats->current_pucch0_stat0),dB_fixed(uci_stats->current_pucch0_stat1),uci_stats->pucch02_DTX);

1747
         if (uci_stats->pucch2_trials > 0)
1748 1749 1750 1751 1752 1753 1754
           strpos+=sprintf(output+strpos,"UCI %d RNTI %x: pucch2_trials %d, pucch2_DTX %d\n",
                           i,uci_stats->rnti,
                           uci_stats->pucch2_trials,
                           uci_stats->pucch2_DTX);
       }
    }
    if (fd) fprintf(fd,"%s",output);
1755
    else    printf("%s",output);
1756
}