nr_dl_channel_estimation.c 67.4 KB
Newer Older
Hongzhi Wang's avatar
Hongzhi Wang committed
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.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

#include <string.h>
Hongzhi Wang's avatar
Hongzhi Wang committed
23
#include "SCHED_NR_UE/defs.h"
24
#include "nr_estimation.h"
Raymond Knopp's avatar
Raymond Knopp committed
25
#include "PHY/NR_REFSIG/refsig_defs_ue.h"
26
#include "PHY/NR_REFSIG/nr_refsig.h"
27
#include "PHY/NR_REFSIG/dmrs_nr.h"
28
#include "PHY/NR_REFSIG/ptrs_nr.h"
29
#include "PHY/NR_REFSIG/nr_mod_table.h"
30
#include "PHY/NR_TRANSPORT/nr_sch_dmrs.h"
31
#include "PHY/NR_TRANSPORT/nr_transport_proto.h"
32
#include "common/utils/nr/nr_common.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
33
#include "filt16a_32.h"
34
#include "T.h"
35
#include <openair1/PHY/TOOLS/phy_scope_interface.h>
francescomani's avatar
francescomani committed
36 37
#include "nfapi/open-nFAPI/nfapi/public_inc/nfapi_nr_interface.h"

38
extern openair0_config_t openair0_cfg[];
39

40 41 42 43
// #define DEBUG_PDSCH
// #define DEBUG_PDCCH
// #define DEBUG_PBCH(a...) printf(a)
#define DEBUG_PBCH(a...)
44 45
//#define DEBUG_PRS_CHEST   // To enable PRS Matlab dumps
//#define DEBUG_PRS_PRINTS  // To enable PRS channel estimation debug logs
46 47 48 49 50

#define CH_INTERP 0
#define NO_INTERP 1

/* Generic function to find the peak of channel estimation buffer */
51
void peak_estimator(int32_t *buffer, int32_t buf_len, int32_t *peak_idx, int32_t *peak_val, int32_t mean_val)
52 53 54 55 56 57 58 59 60 61 62
{
  int32_t max_val = 0, max_idx = 0, abs_val = 0;
  for(int k = 0; k < buf_len; k++)
  {
    abs_val = squaredMod(((c16_t*)buffer)[k]);
    if(abs_val > max_val)
    {
      max_val = abs_val;
      max_idx = k;
    }
  }
Sagar Parsawar's avatar
Sagar Parsawar committed
63

64
  // Check for detection threshold
Sagar Parsawar's avatar
Sagar Parsawar committed
65 66
  LOG_D(PHY, "PRS ToA estimator: max_val %d, mean_val %d, max_idx %d\n", max_val, mean_val, max_idx);
  if ((mean_val != 0) && (max_val / mean_val > 10)) {
67 68
    *peak_val = max_val;
    *peak_idx = max_idx;
Sagar Parsawar's avatar
Sagar Parsawar committed
69
  } else {
70 71 72
    *peak_val = 0;
    *peak_idx = 0;
  }
73
}
74

75 76
int nr_prs_channel_estimation(uint8_t gNB_id,
                              uint8_t rsc_id,
77
                              uint8_t rep_num,
78
                              PHY_VARS_NR_UE *ue,
79
                              const UE_nr_rxtx_proc_t *proc,
80
                              NR_DL_FRAME_PARMS *frame_params,
81
                              c16_t rxdataF[][ue->frame_parms.samples_per_slot_wCP])
82
{
83
  uint8_t rxAnt = 0, idx = 0;
84
  prs_config_t *prs_cfg  = &ue->prs_vars[gNB_id]->prs_resource[rsc_id].prs_cfg;
85
  prs_meas_t **prs_meas  = ue->prs_vars[gNB_id]->prs_resource[rsc_id].prs_meas;
86 87 88
  c16_t ch_tmp_buf[ ue->frame_parms.ofdm_symbol_size] __attribute__((aligned(32)));
  int32_t chF_interpol[frame_params->nb_antennas_rx][NR_PRS_IDFT_OVERSAMP_FACTOR*ue->frame_parms.ofdm_symbol_size] __attribute__((aligned(32)));
  int32_t chT_interpol[frame_params->nb_antennas_rx][NR_PRS_IDFT_OVERSAMP_FACTOR*ue->frame_parms.ofdm_symbol_size] __attribute__((aligned(32)));
laurent's avatar
laurent committed
89 90 91 92
  memset(ch_tmp_buf,0,sizeof(ch_tmp_buf));
  memset(chF_interpol,0,sizeof(chF_interpol));
  memset(chT_interpol,0,sizeof(chF_interpol));
  
93
  int slot_prs           = (proc->nr_slot_rx - rep_num*prs_cfg->PRSResourceTimeGap + frame_params->slots_per_frame)%frame_params->slots_per_frame;
94
  uint32_t **nr_gold_prs = ue->nr_gold_prs[gNB_id][rsc_id][slot_prs];
laurent's avatar
laurent committed
95 96 97

  int16_t *rxF, *pil, mod_prs[NR_MAX_PRS_LENGTH << 1];
  const int16_t *fl, *fm, *fmm, *fml, *fmr, *fr;
98
  int16_t ch[2] = {0}, noiseFig[2] = {0};
99
  int16_t k_prime = 0, k = 0;
100
  int32_t ch_pwr = 0, snr = 0,  rsrp = 0, mean_val = 0, prs_toa = 0;
101
  double ch_pwr_dbm = 0.0f;
102
#ifdef DEBUG_PRS_CHEST
103
  char filename[64] = {0}, varname[64] = {0};
104
#endif
laurent's avatar
laurent committed
105
  int16_t *ch_tmp     = (int16_t *)ch_tmp_buf; 
106
  int16_t scale_factor = (1.0f/(float)(prs_cfg->NumPRSSymbols))*(1<<15);
107
  int16_t num_pilots   = (12/prs_cfg->CombSize)*prs_cfg->NumRB;
Sagar Parsawar's avatar
Sagar Parsawar committed
108 109 110
  int16_t first_half = frame_params->ofdm_symbol_size - frame_params->first_carrier_offset;
  int16_t second_half = (prs_cfg->NumRB * 12) - first_half;
  int16_t start_offset = NR_PRS_IDFT_OVERSAMP_FACTOR * frame_params->ofdm_symbol_size - first_half;
111
  LOG_D(PHY, "start_offset %d, first_half %d, second_half %d\n", start_offset, first_half, second_half);
112

113
  int16_t k_prime_table[K_PRIME_TABLE_ROW_SIZE][K_PRIME_TABLE_COL_SIZE] = PRS_K_PRIME_TABLE;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  for(int l = prs_cfg->SymbolStart; l < prs_cfg->SymbolStart+prs_cfg->NumPRSSymbols; l++)
  {
    int symInd = l-prs_cfg->SymbolStart;
    if (prs_cfg->CombSize == 2) {
      k_prime = k_prime_table[0][symInd];
    }
    else if (prs_cfg->CombSize == 4){
      k_prime = k_prime_table[1][symInd];
    }
    else if (prs_cfg->CombSize == 6){
      k_prime = k_prime_table[2][symInd];
    }
    else if (prs_cfg->CombSize == 12){
      k_prime = k_prime_table[3][symInd];
    }
   
130
#ifdef DEBUG_PRS_PRINTS 
131
    printf("[gNB %d][rsc %d] PRS config l %d k_prime %d:\nprs_cfg->SymbolStart %d\nprs_cfg->NumPRSSymbols %d\nprs_cfg->NumRB %d\nprs_cfg->CombSize %d\n", gNB_id, rsc_id, l, k_prime, prs_cfg->SymbolStart, prs_cfg->NumPRSSymbols, prs_cfg->NumRB, prs_cfg->CombSize);
132
#endif
133
    // Pilots generation and modulation
134 135

    AssertFatal(num_pilots > 0, "num_pilots needs to be gt 0 or mod_prs[0] UB");
136
    for (int m = 0; m < num_pilots; m++) 
137 138 139 140 141
    {
      idx = (((nr_gold_prs[l][(m<<1)>>5])>>((m<<1)&0x1f))&3);
      mod_prs[m<<1]     = nr_qpsk_mod_table[idx<<1];
      mod_prs[(m<<1)+1] = nr_qpsk_mod_table[(idx<<1) + 1];
    } 
142
     
143
    for (rxAnt=0; rxAnt < frame_params->nb_antennas_rx; rxAnt++)
144
    {
145
      // reset variables
Sagar Parsawar's avatar
Sagar Parsawar committed
146 147 148
      snr = 0;
      rsrp = 0;

149
      // calculate RE offset
Sagar Parsawar's avatar
Sagar Parsawar committed
150
      k = (prs_cfg->REOffset + k_prime) % prs_cfg->CombSize + prs_cfg->RBOffset * 12 + frame_params->first_carrier_offset;
151

152 153 154
      // Channel estimation and interpolation
      pil       = (int16_t *)&mod_prs[0];
      rxF       = (int16_t *)&rxdataF[rxAnt][l*frame_params->ofdm_symbol_size + k];
155
      
156
      if(prs_cfg->CombSize == 2)
157
      {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        // Choose the interpolation filters
        switch (k_prime) {
          case 0:
            fl  = filt8_l0;
            fml = filt8_m0;
            fmm = filt8_mm0;
            fmr = filt8_mr0;
            fm  = filt8_m0;
            fr  = filt8_r0;
            break;

          case 1:
            fl  = filt8_l1;
            fmm = filt8_mm1;
            fml = filt8_ml1;
            fmr = fmm;
            fm  = filt8_m1;
            fr  = filt8_r1;
            break;

          default:
179
            LOG_I(PHY, "%s: ERROR!! Invalid k_prime=%d for PRS comb_size %d, symbol %d\n",__FUNCTION__, k_prime, prs_cfg->CombSize, l);
180 181 182 183 184
            return(-1);
            break;
        }

        //Start pilot
185 186
        ch[0] = (int16_t)(((int32_t)rxF[0]*pil[0] + (int32_t)rxF[1]*pil[1])>>15);
        ch[1] = (int16_t)(((int32_t)rxF[1]*pil[0] - (int32_t)rxF[0]*pil[1])>>15);
187 188
        multadd_real_vector_complex_scalar(fl,
		    	       ch,
189
		    	       ch_tmp,
190 191
		    	       8);

Sagar Parsawar's avatar
Sagar Parsawar committed
192 193
        // SNR & RSRP estimation
        rsrp += squaredMod(*((c16_t *)rxF));
194 195
        noiseFig[0] = rxF[0] - (int16_t)(((int32_t)ch[0]*pil[0] - (int32_t)ch[1]*pil[1])>>15);
        noiseFig[1] = rxF[1] - (int16_t)(((int32_t)ch[1]*pil[0] + (int32_t)ch[0]*pil[1])>>15);
laurent's avatar
laurent committed
196
        snr += 10*log10(squaredMod(*(c16_t*)rxF) - squaredMod(*(c16_t*)noiseFig)) - 10*log10(squaredMod(*(c16_t*)noiseFig));
197 198 199
#ifdef DEBUG_PRS_PRINTS
        printf("[Rx %d] pilot %3d, SNR %+2d dB: rxF - > (%+3d, %+3d) addr %p  ch -> (%+3d, %+3d), pil -> (%+d, %+d) \n", rxAnt, 0, snr, rxF[0],rxF[1],&rxF[0],ch[0],ch[1],pil[0],pil[1]);
#endif
200 201 202 203
        pil +=2;
        k   = (k+prs_cfg->CombSize) % frame_params->ofdm_symbol_size;
        rxF = (int16_t *)&rxdataF[rxAnt][l*frame_params->ofdm_symbol_size + k];
        
204
        //Middle pilots
205
        for(int pIdx = 1; pIdx < num_pilots-1; pIdx+=2)
206 207 208 209 210 211 212
        {
          ch[0] = (int16_t)(((int32_t)rxF[0]*pil[0] + (int32_t)rxF[1]*pil[1])>>15);
          ch[1] = (int16_t)(((int32_t)rxF[1]*pil[0] - (int32_t)rxF[0]*pil[1])>>15);
          if(pIdx == 1) // 2nd pilot
          {
            multadd_real_vector_complex_scalar(fml,
		        	       ch,
213
		        	       ch_tmp,
214 215 216 217 218 219
		        	       8);
          }
          else
          {
            multadd_real_vector_complex_scalar(fm,
		        	       ch,
220
		        	       ch_tmp,
221 222
		        	       8);
          }
Sagar Parsawar's avatar
Sagar Parsawar committed
223 224 225

          // SNR & RSRP estimation
          rsrp += squaredMod(*((c16_t *)rxF));
226 227
          noiseFig[0] = rxF[0] - (int16_t)(((int32_t)ch[0]*pil[0] - (int32_t)ch[1]*pil[1])>>15);
          noiseFig[1] = rxF[1] - (int16_t)(((int32_t)ch[1]*pil[0] + (int32_t)ch[0]*pil[1])>>15);
laurent's avatar
laurent committed
228
          snr += 10*log10(squaredMod(*(c16_t*)rxF) - squaredMod(*(c16_t*)noiseFig)) - 10*log10(squaredMod(*(c16_t*)noiseFig));
229 230 231
#ifdef DEBUG_PRS_PRINTS
          printf("[Rx %d] pilot %3d, SNR %+2d dB: rxF - > (%+3d, %+3d) addr %p  ch -> (%+3d, %+3d), pil -> (%+d, %+d) \n", rxAnt, pIdx, snr/(pIdx+1), rxF[0],rxF[1],&rxF[0],ch[0],ch[1],pil[0],pil[1]);
#endif
232 233 234 235 236 237
          pil +=2;
          k   = (k+prs_cfg->CombSize) % frame_params->ofdm_symbol_size;
          rxF = (int16_t *)&rxdataF[rxAnt][l*frame_params->ofdm_symbol_size + k];

          ch[0] = (int16_t)(((int32_t)rxF[0]*pil[0] + (int32_t)rxF[1]*pil[1])>>15);
          ch[1] = (int16_t)(((int32_t)rxF[1]*pil[0] - (int32_t)rxF[0]*pil[1])>>15);
238
          if(pIdx == (num_pilots-3)) // 2nd last pilot
239 240 241
          {
            multadd_real_vector_complex_scalar(fmr,
		        	       ch,
242
		        	       ch_tmp,
243 244 245 246 247 248
		        	       8);
          }
          else
          {
            multadd_real_vector_complex_scalar(fmm,
		        	       ch,
249
		        	       ch_tmp,
250 251
		        	       8);
          }
Sagar Parsawar's avatar
Sagar Parsawar committed
252 253 254

          // SNR & RSRP estimation
          rsrp += squaredMod(*((c16_t *)rxF));
255 256
          noiseFig[0] = rxF[0] - (int16_t)(((int32_t)ch[0]*pil[0] - (int32_t)ch[1]*pil[1])>>15);
          noiseFig[1] = rxF[1] - (int16_t)(((int32_t)ch[1]*pil[0] + (int32_t)ch[0]*pil[1])>>15);
laurent's avatar
laurent committed
257
          snr += 10*log10(squaredMod(*((c16_t*)rxF)) - squaredMod(*((c16_t*)noiseFig))) - 10*log10(squaredMod(*((c16_t*)noiseFig)));
258 259 260
#ifdef DEBUG_PRS_PRINTS
          printf("[Rx %d] pilot %3d, SNR %+2d dB: rxF - > (%+3d, %+3d) addr %p  ch -> (%+3d, %+3d), pil -> (%+d, %+d) \n", rxAnt, pIdx+1, snr/(pIdx+2), rxF[0],rxF[1],&rxF[0],ch[0],ch[1],pil[0],pil[1]);
#endif
261 262 263
          pil +=2;
          k   = (k+prs_cfg->CombSize) % frame_params->ofdm_symbol_size;
          rxF = (int16_t *)&rxdataF[rxAnt][l*frame_params->ofdm_symbol_size + k];
264
          ch_tmp +=8;
265
        }
266

267 268 269 270 271
        //End pilot
        ch[0] = (int16_t)(((int32_t)rxF[0]*pil[0] + (int32_t)rxF[1]*pil[1])>>15);
        ch[1] = (int16_t)(((int32_t)rxF[1]*pil[0] - (int32_t)rxF[0]*pil[1])>>15);
        multadd_real_vector_complex_scalar(fr,
	      	       ch,
272
	      	       ch_tmp,
273
	      	       8);
274

Sagar Parsawar's avatar
Sagar Parsawar committed
275 276 277 278 279
        // SNR & RSRP estimation
        rsrp += squaredMod(*((c16_t *)rxF));
        noiseFig[0] = rxF[0] - (int16_t)(((int32_t)ch[0] * pil[0] - (int32_t)ch[1] * pil[1]) >> 15);
        noiseFig[1] = rxF[1] - (int16_t)(((int32_t)ch[1] * pil[0] + (int32_t)ch[0] * pil[1]) >> 15);
        snr += 10 * log10(squaredMod(*((c16_t *)rxF)) - squaredMod(*((c16_t *)noiseFig))) - 10 * log10(squaredMod(*((c16_t *)noiseFig)));
280
#ifdef DEBUG_PRS_PRINTS
Sagar Parsawar's avatar
Sagar Parsawar committed
281 282 283 284 285 286 287 288 289 290 291
        printf("[Rx %d] pilot %3d, SNR %+2d dB: rxF - > (%+3d, %+3d) addr %p  ch -> (%+3d, %+3d), pil -> (%+d, %+d) \n",
               rxAnt,
               num_pilots - 1,
               snr / num_pilots,
               rxF[0],
               rxF[1],
               &rxF[0],
               ch[0],
               ch[1],
               pil[0],
               pil[1]);
292
#endif
293 294 295 296 297 298
      }
      else if(prs_cfg->CombSize == 4)
      {
        // Choose the interpolation filters
        switch (k_prime) {
          case 0:
299
            fl = (short int *)filt16a_l0;
300 301
            fml = filt16a_mm0;
            fmm = filt16a_mm0;
302 303 304
            fmr = (short int *)filt16a_m0;
            fm = (short int *)filt16a_m0;
            fr = (short int *)filt16a_r0;
305 306 307
            break;

          case 1:
308
            fl = (short int *)filt16a_l1;
309 310 311
            fml = filt16a_ml1;
            fmm = filt16a_mm1;
            fmr = filt16a_mr1;
312 313
            fm = (short int *)filt16a_m1;
            fr = (short int *)filt16a_r1;
314 315 316
            break;

          case 2:
317
            fl = (short int *)filt16a_l2;
318 319 320
            fml = filt16a_ml2;
            fmm = filt16a_mm2;
            fmr = filt16a_mr2;
321 322
            fm = (short int *)filt16a_m2;
            fr = (short int *)filt16a_r2;
323 324 325
            break;

          case 3:
326
            fl = (short int *)filt16a_l3;
327 328 329
            fml = filt16a_ml3;
            fmm = filt16a_mm3;
            fmr = filt16a_mm3;
330 331
            fm = (short int *)filt16a_m3;
            fr = (short int *)filt16a_r3;
332 333 334
            break;

          default:
335
            LOG_I(PHY, "%s: ERROR!! Invalid k_prime=%d for PRS comb_size %d, symbol %d\n",__FUNCTION__, k_prime, prs_cfg->CombSize, l);
336 337 338
            return(-1);
            break;
        }
339

340 341 342 343 344
        //Start pilot
        ch[0] = (int16_t)(((int32_t)rxF[0]*pil[0] + (int32_t)rxF[1]*pil[1])>>15);
        ch[1] = (int16_t)(((int32_t)rxF[1]*pil[0] - (int32_t)rxF[0]*pil[1])>>15);
        multadd_real_vector_complex_scalar(fl,
	      	       ch,
345
	      	       ch_tmp,
346
	      	       16);
Sagar Parsawar's avatar
Sagar Parsawar committed
347 348 349

        // SNR & RSRP estimation
        rsrp += squaredMod(*((c16_t *)rxF));
350 351
        noiseFig[0] = rxF[0] - (int16_t)(((int32_t)ch[0]*pil[0] - (int32_t)ch[1]*pil[1])>>15);
        noiseFig[1] = rxF[1] - (int16_t)(((int32_t)ch[1]*pil[0] + (int32_t)ch[0]*pil[1])>>15);
laurent's avatar
laurent committed
352
        snr += 10*log10(squaredMod(*((c16_t*)rxF)) - squaredMod(*((c16_t*)noiseFig))) - 10*log10(squaredMod(*((c16_t*)noiseFig)));
353 354 355
#ifdef DEBUG_PRS_PRINTS
        printf("[Rx %d] pilot %3d, SNR %+2d dB: rxF - > (%+3d, %+3d) addr %p  ch -> (%+3d, %+3d), pil -> (%+d, %+d) \n", rxAnt, 0, snr, rxF[0],rxF[1],&rxF[0],ch[0],ch[1],pil[0],pil[1]);
#endif
356 357 358
        pil +=2;
        k   = (k+prs_cfg->CombSize) % frame_params->ofdm_symbol_size;
        rxF = (int16_t *)&rxdataF[rxAnt][l*frame_params->ofdm_symbol_size + k];
359 360 361

        ch[0] = (int16_t)(((int32_t)rxF[0]*pil[0] + (int32_t)rxF[1]*pil[1])>>15);
        ch[1] = (int16_t)(((int32_t)rxF[1]*pil[0] - (int32_t)rxF[0]*pil[1])>>15);
362 363
        multadd_real_vector_complex_scalar(fml,
	      	       ch,
364
	      	       ch_tmp,
365 366
	      	       16);

Sagar Parsawar's avatar
Sagar Parsawar committed
367 368
        // SNR & RSRP estimation
        rsrp += squaredMod(*((c16_t *)rxF));
369 370
        noiseFig[0] = rxF[0] - (int16_t)(((int32_t)ch[0]*pil[0] - (int32_t)ch[1]*pil[1])>>15);
        noiseFig[1] = rxF[1] - (int16_t)(((int32_t)ch[1]*pil[0] + (int32_t)ch[0]*pil[1])>>15);
laurent's avatar
laurent committed
371
        snr += 10*log10(squaredMod(*((c16_t*)rxF)) - squaredMod(*((c16_t*)noiseFig))) - 10*log10(squaredMod(*((c16_t*)noiseFig)));
372 373 374
#ifdef DEBUG_PRS_PRINTS
        printf("[Rx %d] pilot %3d, SNR %+2d dB: rxF - > (%+3d, %+3d) addr %p  ch -> (%+3d, %+3d), pil -> (%+d, %+d) \n", rxAnt, 1, snr/2, rxF[0],rxF[1],&rxF[0],ch[0],ch[1],pil[0],pil[1]);
#endif
375 376 377
        pil +=2;
        k   = (k+prs_cfg->CombSize) % frame_params->ofdm_symbol_size;
        rxF = (int16_t *)&rxdataF[rxAnt][l*frame_params->ofdm_symbol_size + k];
378
        ch_tmp +=8;
379 380

        //Middle pilots
381
        for(int pIdx = 2; pIdx < num_pilots-2; pIdx++)
382 383 384 385 386
        {
          ch[0] = (int16_t)(((int32_t)rxF[0]*pil[0] + (int32_t)rxF[1]*pil[1])>>15);
          ch[1] = (int16_t)(((int32_t)rxF[1]*pil[0] - (int32_t)rxF[0]*pil[1])>>15);
          multadd_real_vector_complex_scalar(fmm,
	      	         ch,
387
	      	         ch_tmp,
388
	      	         16);
Sagar Parsawar's avatar
Sagar Parsawar committed
389 390 391

          // SNR & RSRP estimation
          rsrp += squaredMod(*((c16_t *)rxF));
392 393
          noiseFig[0] = rxF[0] - (int16_t)(((int32_t)ch[0]*pil[0] - (int32_t)ch[1]*pil[1])>>15);
          noiseFig[1] = rxF[1] - (int16_t)(((int32_t)ch[1]*pil[0] + (int32_t)ch[0]*pil[1])>>15);
laurent's avatar
laurent committed
394
          snr += 10*log10(squaredMod(*((c16_t*)rxF)) - squaredMod(*((c16_t*)noiseFig))) - 10*log10(squaredMod(*((c16_t*)noiseFig)));
395 396 397
#ifdef DEBUG_PRS_PRINTS
          printf("[Rx %d] pilot %3d, SNR %+2d dB: rxF - > (%+3d, %+3d) addr %p  ch -> (%+3d, %+3d), pil -> (%+d, %+d) \n", rxAnt, pIdx, snr/(pIdx+1), rxF[0],rxF[1],&rxF[0],ch[0],ch[1],pil[0],pil[1]);
#endif
398 399 400
          pil +=2;
          k   = (k+prs_cfg->CombSize) % frame_params->ofdm_symbol_size;
          rxF = (int16_t *)&rxdataF[rxAnt][l*frame_params->ofdm_symbol_size + k];
401
          ch_tmp +=8;
402 403 404 405 406 407 408
        }

        //End pilot
        ch[0] = (int16_t)(((int32_t)rxF[0]*pil[0] + (int32_t)rxF[1]*pil[1])>>15);
        ch[1] = (int16_t)(((int32_t)rxF[1]*pil[0] - (int32_t)rxF[0]*pil[1])>>15);
        multadd_real_vector_complex_scalar(fmr,
	      	       ch,
409
	      	       ch_tmp,
410
	      	       16);
Sagar Parsawar's avatar
Sagar Parsawar committed
411 412 413

        // SNR & RSRP estimation
        rsrp += squaredMod(*((c16_t *)rxF));
414 415
        noiseFig[0] = rxF[0] - (int16_t)(((int32_t)ch[0]*pil[0] - (int32_t)ch[1]*pil[1])>>15);
        noiseFig[1] = rxF[1] - (int16_t)(((int32_t)ch[1]*pil[0] + (int32_t)ch[0]*pil[1])>>15);
laurent's avatar
laurent committed
416
        snr += 10*log10(squaredMod(*((c16_t*)rxF)) - squaredMod(*((c16_t*)noiseFig))) - 10*log10(squaredMod(*((c16_t*)noiseFig)));
417 418 419
#ifdef DEBUG_PRS_PRINTS
        printf("[Rx %d] pilot %3d, SNR %+2d dB: rxF - > (%+3d, %+3d) addr %p  ch -> (%+3d, %+3d), pil -> (%+d, %+d) \n", rxAnt, num_pilots-2, snr/(num_pilots-1), rxF[0],rxF[1],&rxF[0],ch[0],ch[1],pil[0],pil[1]);
#endif
420 421 422
        pil +=2;
        k   = (k+prs_cfg->CombSize) % frame_params->ofdm_symbol_size;
        rxF = (int16_t *)&rxdataF[rxAnt][l*frame_params->ofdm_symbol_size + k];
423

424 425 426 427
        ch[0] = (int16_t)(((int32_t)rxF[0]*pil[0] + (int32_t)rxF[1]*pil[1])>>15);
        ch[1] = (int16_t)(((int32_t)rxF[1]*pil[0] - (int32_t)rxF[0]*pil[1])>>15);
        multadd_real_vector_complex_scalar(fr,
	      	       ch,
428
	      	       ch_tmp,
429
	      	       16);
Sagar Parsawar's avatar
Sagar Parsawar committed
430 431 432

        // SNR & RSRP estimation
        rsrp += squaredMod(*((c16_t *)rxF));
433 434
        noiseFig[0] = rxF[0] - (int16_t)(((int32_t)ch[0]*pil[0] - (int32_t)ch[1]*pil[1])>>15);
        noiseFig[1] = rxF[1] - (int16_t)(((int32_t)ch[1]*pil[0] + (int32_t)ch[0]*pil[1])>>15);
laurent's avatar
laurent committed
435
        snr += 10*log10(squaredMod(*((c16_t*)rxF)) - squaredMod(*((c16_t*)noiseFig))) - 10*log10(squaredMod(*((c16_t*)noiseFig)));
436 437 438
#ifdef DEBUG_PRS_PRINTS
        printf("[Rx %d] pilot %3d, SNR %+2d dB: rxF - > (%+3d, %+3d) addr %p  ch -> (%+3d, %+3d), pil -> (%+d, +%d) \n", rxAnt, num_pilots-1, snr/num_pilots, rxF[0],rxF[1],&rxF[0],ch[0],ch[1],pil[0],pil[1]);
#endif
439 440 441
      }
      else
      {
442
        AssertFatal((prs_cfg->CombSize == 2)||(prs_cfg->CombSize == 4), "[%s] DL PRS CombSize other than 2 and 4 are NOT supported currently. Exiting!!!", __FUNCTION__);
443
      }
Sagar Parsawar's avatar
Sagar Parsawar committed
444

445
      // average out the SNR and RSRP computed
Sagar Parsawar's avatar
Sagar Parsawar committed
446 447
      prs_meas[rxAnt]->snr = snr / (float)num_pilots;
      prs_meas[rxAnt]->rsrp = rsrp / (float)num_pilots;
448

449
      //reset channel pointer
laurent's avatar
laurent committed
450
      ch_tmp = (int16_t*)ch_tmp_buf;
451 452 453 454 455 456 457 458 459 460 461 462
    } // for rxAnt
  } //for l
  
  for (rxAnt=0; rxAnt < frame_params->nb_antennas_rx; rxAnt++)
  {
    // scale by averaging factor 1/NumPrsSymbols
    multadd_complex_vector_real_scalar(ch_tmp,
                                       scale_factor,
                                       ch_tmp,
                                       1,
                                       frame_params->ofdm_symbol_size);

463
#ifdef DEBUG_PRS_PRINTS
464 465 466 467 468 469 470 471 472 473
    for (int rb = 0; rb < prs_cfg->NumRB; rb++)
    {
      printf("================================================================\n");
      printf("\t\t\t[gNB %d][Rx %d][RB %d]\n", gNB_id, rxAnt, rb);
      printf("================================================================\n");
      idx = (12*rb)<<1;
      printf("%4d %4d  %4d %4d  %4d %4d  %4d %4d  %4d %4d  %4d %4d\n", ch_tmp[idx], ch_tmp[idx+1], ch_tmp[idx+2], ch_tmp[idx+3], ch_tmp[idx+4], ch_tmp[idx+5], ch_tmp[idx+6], ch_tmp[idx+7], ch_tmp[idx+8], ch_tmp[idx+9], ch_tmp[idx+10], ch_tmp[idx+11]);
      printf("%4d %4d  %4d %4d  %4d %4d  %4d %4d  %4d %4d  %4d %4d\n", ch_tmp[idx+12], ch_tmp[idx+13], ch_tmp[idx+14], ch_tmp[idx+15], ch_tmp[idx+16], ch_tmp[idx+17], ch_tmp[idx+18], ch_tmp[idx+19], ch_tmp[idx+20], ch_tmp[idx+21], ch_tmp[idx+22], ch_tmp[idx+23]);
      printf("\n");
    }
474
#endif
475

476 477
    // Place PRS channel estimates in FFT shifted format
    if(first_half > 0)
Sagar Parsawar's avatar
Sagar Parsawar committed
478
      memcpy((int16_t *)&chF_interpol[rxAnt][start_offset], &ch_tmp[0], first_half * sizeof(int32_t));
479
    if(second_half > 0)
Sagar Parsawar's avatar
Sagar Parsawar committed
480
      memcpy((int16_t *)&chF_interpol[rxAnt][0], &ch_tmp[first_half << 1], second_half * sizeof(int32_t));
481

482 483
    // Time domain IMPULSE response
    idft_size_idx_t idftsizeidx;
484
    switch (NR_PRS_IDFT_OVERSAMP_FACTOR*frame_params->ofdm_symbol_size) {
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
    case 128:
      idftsizeidx = IDFT_128;
      break;

    case 256:
      idftsizeidx = IDFT_256;
      break;

    case 512:
      idftsizeidx = IDFT_512;
      break;

    case 768:
      idftsizeidx = IDFT_768;
      break;

    case 1024:
      idftsizeidx = IDFT_1024;
      break;

    case 1536:
      idftsizeidx = IDFT_1536;
      break;

    case 2048:
      idftsizeidx = IDFT_2048;
      break;

    case 3072:
      idftsizeidx = IDFT_3072;
      break;

    case 4096:
      idftsizeidx = IDFT_4096;
      break;
520 521 522 523 524
    
    case 6144:
      idftsizeidx = IDFT_6144;
      break;
 
525
    // 16x IDFT oversampling
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
    case 8192:
      idftsizeidx = IDFT_8192;
      break;

    case 12288:
      idftsizeidx = IDFT_12288;
      break;

    case 16384:
      idftsizeidx = IDFT_16384;
      break;

    case 24576:
      idftsizeidx = IDFT_24576;
      break;

    case 32768:
      idftsizeidx = IDFT_32768;
      break;

    case 49152:
      idftsizeidx = IDFT_49152;
      break;

    case 65536:
      idftsizeidx = IDFT_65536;
      break;

554 555 556 557
    default:
      LOG_I(PHY, "%s: unsupported ofdm symbol size \n", __FUNCTION__);
      assert(0);
    }
558

559
    idft(idftsizeidx,
560 561
         (int16_t *)&chF_interpol[rxAnt][0],
         (int16_t *)&chT_interpol[rxAnt][0],1);
562 563

    // peak estimator
Sagar Parsawar's avatar
Sagar Parsawar committed
564 565
    mean_val = squaredMod(((c16_t *)ch_tmp)[(prs_cfg->NumRB * 12) >> 1]);
    peak_estimator(&chT_interpol[rxAnt][0], NR_PRS_IDFT_OVERSAMP_FACTOR * frame_params->ofdm_symbol_size, &prs_toa, &ch_pwr, mean_val);
566 567

    // adjusting the rx_gains for channel peak power
Sagar Parsawar's avatar
Sagar Parsawar committed
568 569 570 571 572
    ch_pwr_dbm = 10 * log10(ch_pwr) + 30 - 10 * log10(pow(2, 30)) - ((int)openair0_cfg[0].rx_gain[0] - (int)openair0_cfg[0].rx_gain_offset[0]) - dB_fixed(frame_params->ofdm_symbol_size);

    prs_meas[rxAnt]->rsrp_dBm =
        10 * log10(prs_meas[rxAnt]->rsrp) + 30 - 10 * log10(pow(2, 30)) - ((int)openair0_cfg[0].rx_gain[0] - (int)openair0_cfg[0].rx_gain_offset[0]) - dB_fixed(ue->frame_parms.ofdm_symbol_size);

573
    //prs measurements
574 575 576 577
    prs_meas[rxAnt]->gNB_id     = gNB_id;
    prs_meas[rxAnt]->sfn        = proc->frame_rx;
    prs_meas[rxAnt]->slot       = proc->nr_slot_rx;
    prs_meas[rxAnt]->rxAnt_idx  = rxAnt;
578
    prs_meas[rxAnt]->dl_aoa     = rsc_id;
Sagar Parsawar's avatar
Sagar Parsawar committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    prs_meas[rxAnt]->dl_toa = prs_toa / (float)NR_PRS_IDFT_OVERSAMP_FACTOR;
    if ((frame_params->ofdm_symbol_size - prs_meas[rxAnt]->dl_toa) < frame_params->ofdm_symbol_size / 2)
      prs_meas[rxAnt]->dl_toa -= (frame_params->ofdm_symbol_size);
    LOG_I(PHY,
          "[gNB %d][rsc %d][Rx %d][sfn %d][slot %d] DL PRS ToA ==> %.1f / %d samples, peak channel power %.1f dBm, SNR %+.1f dB, rsrp %+.1f dBm\n",
          gNB_id,
          rsc_id,
          rxAnt,
          proc->frame_rx,
          proc->nr_slot_rx,
          prs_meas[rxAnt]->dl_toa,
          frame_params->ofdm_symbol_size,
          ch_pwr_dbm,
          prs_meas[rxAnt]->snr,
          prs_meas[rxAnt]->rsrp_dBm);
594

595
#ifdef DEBUG_PRS_CHEST
596
    sprintf(filename, "%s%i%s", "PRSpilot_", rxAnt, ".m");
597
    LOG_M(filename, "prs_loc", &mod_prs[0], num_pilots,1,1);
598 599
    sprintf(filename, "%s%i%s", "rxSigF_", rxAnt, ".m");
    sprintf(varname, "%s%i", "rxF_", rxAnt);
600
    LOG_M(filename, varname, &rxdataF[rxAnt][0], prs_cfg->NumPRSSymbols*frame_params->ofdm_symbol_size,1,1);
601 602
    sprintf(filename, "%s%i%s", "prsChestF_", rxAnt, ".m");
    sprintf(varname, "%s%i", "prsChF_", rxAnt);
603
    LOG_M(filename, varname, &chF_interpol[rxAnt][start_offset], frame_params->ofdm_symbol_size,1,1);
604 605
    sprintf(filename, "%s%i%s", "prsChestT_", rxAnt, ".m");
    sprintf(varname, "%s%i", "prsChT_", rxAnt);
606
    LOG_M(filename, varname, &chT_interpol[rxAnt][start_offset], frame_params->ofdm_symbol_size,1,1);
607
#endif
608

609
    // T tracer dump
610 611 612 613
    T(T_UE_PHY_INPUT_SIGNAL, T_INT(gNB_id),
      T_INT(proc->frame_rx), T_INT(proc->nr_slot_rx),
      T_INT(rxAnt), T_BUFFER(&rxdataF[rxAnt][0], frame_params->samples_per_slot_wCP*sizeof(int32_t)));

Sagar Parsawar's avatar
Sagar Parsawar committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
    T(T_UE_PHY_DL_CHANNEL_ESTIMATE_FREQ,
      T_INT(gNB_id),
      T_INT(rsc_id),
      T_INT(proc->frame_rx),
      T_INT(proc->nr_slot_rx),
      T_INT(rxAnt),
      T_BUFFER(&chF_interpol[rxAnt][0], NR_PRS_IDFT_OVERSAMP_FACTOR * frame_params->ofdm_symbol_size * sizeof(int32_t)));

    T(T_UE_PHY_DL_CHANNEL_ESTIMATE,
      T_INT(gNB_id),
      T_INT(rsc_id),
      T_INT(proc->frame_rx),
      T_INT(proc->nr_slot_rx),
      T_INT(rxAnt),
      T_BUFFER(&chT_interpol[rxAnt][0], NR_PRS_IDFT_OVERSAMP_FACTOR * frame_params->ofdm_symbol_size * sizeof(int32_t)));
629
  }
630

631 632
  return(0);
}
Raymond Knopp's avatar
Raymond Knopp committed
633

634
int nr_pbch_dmrs_correlation(PHY_VARS_NR_UE *ue,
635
                             const UE_nr_rxtx_proc_t *proc,
yilmazt's avatar
yilmazt committed
636 637
                             unsigned char symbol,
                             int dmrss,
638
                             NR_UE_SSB *current_ssb,
639
                             c16_t rxdataF[][ue->frame_parms.samples_per_slot_wCP])
640
{
641
  c16_t pilot[200] __attribute__((aligned(16)));
642 643
  unsigned int pilot_cnt;
  int symbol_offset;
644

645 646 647 648 649 650
  uint8_t ssb_index=current_ssb->i_ssb;
  uint8_t n_hf=current_ssb->n_hf;

  unsigned int  ssb_offset = ue->frame_parms.first_carrier_offset + ue->frame_parms.ssb_start_subcarrier;
  if (ssb_offset>= ue->frame_parms.ofdm_symbol_size) ssb_offset-=ue->frame_parms.ofdm_symbol_size;

651 652 653
  AssertFatal(dmrss >= 0 && dmrss < 3,
	      "symbol %d is illegal for PBCH DM-RS \n",
	      dmrss);
654 655 656

  symbol_offset = ue->frame_parms.ofdm_symbol_size*symbol;

657
  unsigned int k = ue->frame_parms.Nid_cell % 4;
658

659 660 661 662 663 664
  DEBUG_PBCH("PBCH DMRS Correlation : gNB_id %d , OFDM size %d, Ncp=%d, k=%d symbol %d\n",
             proc->gNB_id,
             ue->frame_parms.ofdm_symbol_size,
             ue->frame_parms.Ncp,
             k,
             symbol);
665 666

  // generate pilot
667
  // Note: pilot returned by the following function is already the complex conjugate of the transmitted DMRS
668
  nr_pbch_dmrs_rx(dmrss, ue->nr_gold_pbch[n_hf][ssb_index], pilot);
669

670
  for (int aarx=0; aarx<ue->frame_parms.nb_antennas_rx; aarx++) {
671

672
    int re_offset = ssb_offset;
673 674
    c16_t *pil = pilot;
    c16_t *rxF = &rxdataF[aarx][symbol_offset + k];
675

676 677
    DEBUG_PBCH("pbch ch est pilot RB_DL %d\n", ue->frame_parms.N_RB_DL);
    DEBUG_PBCH("k %d, first_carrier %d\n", k, ue->frame_parms.first_carrier_offset);
678 679 680
    //if ((ue->frame_parms.N_RB_DL&1)==0) {

    // Treat first 2 pilots specially (left edge)
681
    current_ssb->c = c32x16maddShift(*pil, rxF[re_offset], current_ssb->c, 15);
682

683 684
    DEBUG_PBCH("ch 0 %d\n", pil->r * rxF[re_offset].r - pil->i * rxF[re_offset].i);
    DEBUG_PBCH("pilot 0 : rxF - > (%d,%d)  pil -> (%d,%d) \n", rxF[re_offset].r, rxF[re_offset].i, pil->r, pil->i);
685

686
    pil++;
687
    re_offset = (re_offset+4) % ue->frame_parms.ofdm_symbol_size;
688
    current_ssb->c = c32x16maddShift(*pil, rxF[re_offset], current_ssb->c, 15);
689

690
    DEBUG_PBCH("pilot 1 : rxF - > (%d,%d)  pil -> (%d,%d) \n", rxF[re_offset].r, rxF[re_offset].i, pil->r, pil->i);
691

692
    pil++;
693
    re_offset = (re_offset+4) % ue->frame_parms.ofdm_symbol_size;
694
    current_ssb->c = c32x16maddShift(*pil, rxF[re_offset], current_ssb->c, 15);
695

696
    DEBUG_PBCH("pilot 2 : rxF - > (%d,%d), pil -> (%d,%d) \n", rxF[re_offset].r, rxF[re_offset].i, pil->r, pil->i);
697

698 699
    pil++;
    re_offset = (re_offset + 4) % ue->frame_parms.ofdm_symbol_size;
700

701
    for (pilot_cnt = 3; pilot_cnt < (3 * 20); pilot_cnt += 3) {
702 703 704
      // in 2nd symbol, skip middle  REs (48 with DMRS,  144 for SSS, and another 48 with DMRS) 
      if (dmrss == 1 && pilot_cnt == 12) {
	pilot_cnt=48;
705
  re_offset = (re_offset + 144) % ue->frame_parms.ofdm_symbol_size;
706
      }
707
      current_ssb->c = c32x16maddShift(*pil, rxF[re_offset], current_ssb->c, 15);
708

709
      DEBUG_PBCH("pilot %u : rxF= (%d,%d) pil= (%d,%d) \n", pilot_cnt, rxF[re_offset].r, rxF[re_offset].i, pil->r, pil->i);
710

711
      pil++;
712
      re_offset = (re_offset+4) % ue->frame_parms.ofdm_symbol_size;
713
      current_ssb->c = c32x16maddShift(*pil, rxF[re_offset], current_ssb->c, 15);
714

715 716
      DEBUG_PBCH("pilot %u : rxF= (%d,%d) pil= (%d,%d) \n", pilot_cnt + 1, rxF[re_offset].r, rxF[re_offset].i, pil->r, pil->i);
      pil++;
717
      re_offset = (re_offset+4) % ue->frame_parms.ofdm_symbol_size;
718
      current_ssb->c = c32x16maddShift(*pil, rxF[re_offset], current_ssb->c, 15);
719

720 721 722
      DEBUG_PBCH("pilot %u : rxF= (%d,%d)  pil= (%d,%d) \n", pilot_cnt + 2, rxF[re_offset].r, rxF[re_offset].i, pil->r, pil->i);
      pil++;
      re_offset = (re_offset + 4) % ue->frame_parms.ofdm_symbol_size;
723 724 725 726 727
    }
  }
  return(0);
}

Hongzhi Wang's avatar
Hongzhi Wang committed
728
int nr_pbch_channel_estimation(PHY_VARS_NR_UE *ue,
729
                               int estimateSz,
730 731
                               struct complex16 dl_ch_estimates[][estimateSz],
                               struct complex16 dl_ch_estimates_time[][ue->frame_parms.ofdm_symbol_size],
732
                               const UE_nr_rxtx_proc_t *proc,
yilmazt's avatar
yilmazt committed
733 734 735
                               unsigned char symbol,
                               int dmrss,
                               uint8_t ssb_index,
736
                               uint8_t n_hf,
737
                               c16_t rxdataF[][ue->frame_parms.samples_per_slot_wCP])
Hongzhi Wang's avatar
Hongzhi Wang committed
738
{
739
  int Ns = proc->nr_slot_rx;
740
  c16_t pilot[200] __attribute__((aligned(16)));
laurent's avatar
laurent committed
741
  //int slot_pbch;
Hongzhi Wang's avatar
Hongzhi Wang committed
742

743
  const int nushift = ue->frame_parms.Nid_cell % 4;
Raymond Knopp's avatar
Raymond Knopp committed
744 745 746
  unsigned int  ssb_offset = ue->frame_parms.first_carrier_offset + ue->frame_parms.ssb_start_subcarrier;
  if (ssb_offset>= ue->frame_parms.ofdm_symbol_size) ssb_offset-=ue->frame_parms.ofdm_symbol_size;

747
  const int ch_offset = ue->frame_parms.ofdm_symbol_size * symbol;
Hongzhi Wang's avatar
Hongzhi Wang committed
748

749 750 751
  AssertFatal(dmrss >= 0 && dmrss < 3,
	      "symbol %d is illegal for PBCH DM-RS \n",
	      dmrss);
Raymond Knopp's avatar
Raymond Knopp committed
752

753
  const int symbol_offset = ue->frame_parms.ofdm_symbol_size * symbol;
Hongzhi Wang's avatar
Hongzhi Wang committed
754

755 756 757 758 759 760 761 762 763 764
  const int k = nushift;
  const c16_t *fl, *fm, *fr;
  DEBUG_PBCH("PBCH Channel Estimation : gNB_id %d ch_offset %d, OFDM size %d, Ncp=%d, Ns=%d, k=%d symbol %d\n",
             proc->gNB_id,
             ch_offset,
             ue->frame_parms.ofdm_symbol_size,
             ue->frame_parms.Ncp,
             Ns,
             k,
             symbol);
Hongzhi Wang's avatar
Hongzhi Wang committed
765 766 767

  switch (k) {
  case 0:
Raymond Knopp's avatar
Raymond Knopp committed
768 769 770 771
    fl = filt16a_l0;
    fm = filt16a_m0;
    fr = filt16a_r0;
    break;
Hongzhi Wang's avatar
Hongzhi Wang committed
772 773

  case 1:
Raymond Knopp's avatar
Raymond Knopp committed
774 775 776 777
    fl = filt16a_l1;
    fm = filt16a_m1;
    fr = filt16a_r1;
    break;
Hongzhi Wang's avatar
Hongzhi Wang committed
778 779

  case 2:
Raymond Knopp's avatar
Raymond Knopp committed
780 781 782 783
    fl = filt16a_l2;
    fm = filt16a_m2;
    fr = filt16a_r2;
    break;
Hongzhi Wang's avatar
Hongzhi Wang committed
784 785

  case 3:
Raymond Knopp's avatar
Raymond Knopp committed
786 787 788 789
    fl = filt16a_l3;
    fm = filt16a_m3;
    fr = filt16a_r3;
    break;
Hongzhi Wang's avatar
Hongzhi Wang committed
790 791 792 793 794 795 796

  default:
    msg("pbch_channel_estimation: k=%d -> ERROR\n",k);
    return(-1);
    break;
  }

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
  idft_size_idx_t idftsizeidx;
  
  switch (ue->frame_parms.ofdm_symbol_size) {
  case 128:
    idftsizeidx = IDFT_128;
    break;
    
  case 256:
    idftsizeidx = IDFT_256;
    break;
    
  case 512:
    idftsizeidx = IDFT_512;
    break;
    
812 813 814 815
  case 768:
    idftsizeidx = IDFT_768;
    break;

816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
  case 1024:
    idftsizeidx = IDFT_1024;
    break;
    
  case 1536:
    idftsizeidx = IDFT_1536;
    break;
    
  case 2048:
    idftsizeidx = IDFT_2048;
    break;
    
  case 3072:
    idftsizeidx = IDFT_3072;
    break;
    
  case 4096:
    idftsizeidx = IDFT_4096;
    break;
835 836 837 838 839
  
  case 6144:
    idftsizeidx = IDFT_6144;
    break;
 
840 841 842 843 844
  default:
    printf("unsupported ofdm symbol size \n");
    assert(0);
  }
  
Hongzhi Wang's avatar
Hongzhi Wang committed
845
  // generate pilot
846
  // Note: pilot returned by the following function is already the complex conjugate of the transmitted DMRS
847
  nr_pbch_dmrs_rx(dmrss, ue->nr_gold_pbch[n_hf][ssb_index], pilot);
Hongzhi Wang's avatar
Hongzhi Wang committed
848

849
  for (int aarx=0; aarx<ue->frame_parms.nb_antennas_rx; aarx++) {
Hongzhi Wang's avatar
Hongzhi Wang committed
850

851
    int re_offset = ssb_offset;
852 853 854
    c16_t *pil = pilot;
    c16_t *rxF = &rxdataF[aarx][symbol_offset + k];
    c16_t *dl_ch = &dl_ch_estimates[aarx][ch_offset];
Francesco Mani's avatar
Francesco Mani committed
855

856
    memset(dl_ch, 0, sizeof(c16_t) * ue->frame_parms.ofdm_symbol_size);
857

858 859
    DEBUG_PBCH("pbch ch est pilot RB_DL %d\n", ue->frame_parms.N_RB_DL);
    DEBUG_PBCH("k %d, first_carrier %d\n", k, ue->frame_parms.first_carrier_offset);
Hongzhi Wang's avatar
Hongzhi Wang committed
860

Raymond Knopp's avatar
Raymond Knopp committed
861
    // Treat first 2 pilots specially (left edge)
862 863 864 865 866 867
    c16_t ch;
    ch = c16mulShift(*pil, rxF[re_offset], 15);
    DEBUG_PBCH("pilot 0: rxF= (%d,%d), ch= (%d,%d), pil=(%d,%d)\n", rxF[re_offset].r, rxF[re_offset].i, ch.r, ch.i, pil->r, pil->i);
    multaddRealVectorComplexScalar(fl, ch, dl_ch, 16);
    pil++;
    re_offset = (re_offset + 4) % ue->frame_parms.ofdm_symbol_size;
Francesco Mani's avatar
Francesco Mani committed
868

869
    ch = c16mulShift(*pil, rxF[re_offset], 15);
Raymond Knopp's avatar
Raymond Knopp committed
870

871 872 873 874
    DEBUG_PBCH("pilot 1: rxF= (%d,%d), ch= (%d,%d), pil=(%d,%d)\n", rxF[re_offset].r, rxF[re_offset].i, ch.r, ch.i, pil->r, pil->i);
    multaddRealVectorComplexScalar(fm, ch, dl_ch, 16);
    pil++;
    re_offset = (re_offset + 4) % ue->frame_parms.ofdm_symbol_size;
875

876
    ch = c16mulShift(*pil, rxF[re_offset], 15);
Raymond Knopp's avatar
Raymond Knopp committed
877

878 879 880 881
    DEBUG_PBCH("pilot 2: rxF= (%d,%d), ch= (%d,%d), pil=(%d,%d)\n", rxF[re_offset].r, rxF[re_offset].i, ch.r, ch.i, pil->r, pil->i);
    multaddRealVectorComplexScalar(fr, ch, dl_ch, 16);
    pil++;
    re_offset = (re_offset + 4) % ue->frame_parms.ofdm_symbol_size;
882
    dl_ch += 24;
Raymond Knopp's avatar
Raymond Knopp committed
883

884
    for (int pilot_cnt = 3; pilot_cnt < (3 * 20); pilot_cnt += 3) {
Raymond Knopp's avatar
Raymond Knopp committed
885 886 887 888 889
      //	if (pilot_cnt == 30)
      //	  rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k)];

      // in 2nd symbol, skip middle  REs (48 with DMRS,  144 for SSS, and another 48 with DMRS) 
      if (dmrss == 1 && pilot_cnt == 12) {
890
        pilot_cnt=48;
891
        re_offset = (re_offset + 144) % ue->frame_parms.ofdm_symbol_size;
892
        dl_ch += 288;
Raymond Knopp's avatar
Raymond Knopp committed
893
      }
894 895 896 897 898 899 900 901 902 903
      ch = c16mulShift(*pil, rxF[re_offset], 15);
      DEBUG_PBCH("pilot %u: rxF= (%d,%d), ch= (%d,%d), pil=(%d,%d)\n",
                 pilot_cnt,
                 rxF[re_offset].r,
                 rxF[re_offset].i,
                 ch.r,
                 ch.i,
                 pil->r,
                 pil->i);
      multaddRealVectorComplexScalar(fl, ch, dl_ch, 16);
Francesco Mani's avatar
Francesco Mani committed
904

905
      pil++;
906
      re_offset = (re_offset+4) % ue->frame_parms.ofdm_symbol_size;
907 908 909 910 911 912 913 914 915 916 917
      ch = c16mulShift(*pil, rxF[re_offset], 15);
      DEBUG_PBCH("pilot %u: rxF= (%d,%d), ch= (%d,%d), pil=(%d,%d)\n",
                 pilot_cnt + 1,
                 rxF[re_offset].r,
                 rxF[re_offset].i,
                 ch.r,
                 ch.i,
                 pil->r,
                 pil->i);
      multaddRealVectorComplexScalar(fm, ch, dl_ch, 16);
      pil++;
918
      re_offset = (re_offset+4) % ue->frame_parms.ofdm_symbol_size;
919 920 921 922 923 924 925 926 927 928 929 930 931
      ch = c16mulShift(*pil, rxF[re_offset], 15);
      DEBUG_PBCH("pilot %u: rxF= (%d,%d), ch= (%d,%d), pil=(%d,%d)\n",
                 pilot_cnt + 2,
                 rxF[re_offset].r,
                 rxF[re_offset].i,
                 ch.r,
                 ch.i,
                 pil->r,
                 pil->i);
      multaddRealVectorComplexScalar(fr, ch, dl_ch, 16);
      pil++;
      re_offset = (re_offset + 4) % ue->frame_parms.ofdm_symbol_size;
      dl_ch += 12;
Raymond Knopp's avatar
Raymond Knopp committed
932
    }
Hongzhi Wang's avatar
Hongzhi Wang committed
933

934
    if( dmrss == 2) // update time statistics for last PBCH symbol
Hongzhi Wang's avatar
Hongzhi Wang committed
935
    {
936
      // do ifft of channel estimate
937 938 939 940 941
      LOG_D(PHY,"Channel Impulse Computation Slot %d Symbol %d ch_offset %d\n", Ns, symbol, ch_offset);
      idft(idftsizeidx,
	   (int16_t*) &dl_ch_estimates[aarx][ch_offset],
	   (int16_t*) dl_ch_estimates_time[aarx],
	   1);
Hongzhi Wang's avatar
Hongzhi Wang committed
942
    }
Hongzhi Wang's avatar
Hongzhi Wang committed
943
  }
944

945
  if (dmrss == 2)
946 947 948 949 950 951 952
    UEscopeCopy(ue,
                pbchDlChEstimateTime,
                (void *)dl_ch_estimates_time,
                sizeof(c16_t),
                ue->frame_parms.nb_antennas_rx,
                ue->frame_parms.ofdm_symbol_size,
                0);
953

Hongzhi Wang's avatar
Hongzhi Wang committed
954 955 956
  return(0);
}

957
void nr_pdcch_channel_estimation(PHY_VARS_NR_UE *ue,
958
                                 const UE_nr_rxtx_proc_t *proc,
959 960 961 962 963
                                 unsigned char symbol,
                                 fapi_nr_coreset_t *coreset,
                                 uint16_t first_carrier_offset,
                                 uint16_t BWPStart,
                                 int32_t pdcch_est_size,
964
                                 c16_t pdcch_dl_ch_estimates[][pdcch_est_size],
965
                                 c16_t rxdataF[][ue->frame_parms.samples_per_slot_wCP])
966
{
967

968 969
  int Ns = proc->nr_slot_rx;
  int gNB_id = proc->gNB_id;
Hongzhi Wang's avatar
Hongzhi Wang committed
970
  unsigned char aarx;
971 972
  unsigned short k;
  unsigned int pilot_cnt;
973
  int16_t ch[2],*pil,*rxF,*dl_ch;
974 975
  int ch_offset,symbol_offset;

976
  ch_offset     = ue->frame_parms.ofdm_symbol_size*symbol;
977 978 979

  symbol_offset = ue->frame_parms.ofdm_symbol_size*symbol;

980 981 982 983 984 985
  int nb_rb_coreset=0;
  int coreset_start_rb=0;
  get_coreset_rballoc(coreset->frequency_domain_resource,&nb_rb_coreset,&coreset_start_rb);
  if(nb_rb_coreset==0) return;

#ifdef DEBUG_PDCCH
francescomani's avatar
francescomani committed
986
  printf("pdcch_channel_estimation: first_carrier_offset %d, BWPStart %d, coreset_start_rb %d, coreset_nb_rb %d\n",
987 988 989 990
         first_carrier_offset, BWPStart, coreset_start_rb, nb_rb_coreset);
#endif

  unsigned short coreset_start_subcarrier = first_carrier_offset+(BWPStart + coreset_start_rb)*12;
991

hongzhi wang's avatar
hongzhi wang committed
992
#ifdef DEBUG_PDCCH
993 994
  printf("PDCCH Channel Estimation : gNB_id %d ch_offset %d, OFDM size %d, Ncp=%d, Ns=%d, symbol %d\n",
         gNB_id,ch_offset,ue->frame_parms.ofdm_symbol_size,ue->frame_parms.Ncp,Ns,symbol);
995 996
#endif

997 998 999 1000 1001
#if CH_INTERP
  int16_t *fl = filt16a_l1;
  int16_t *fm = filt16a_m1;
  int16_t *fr = filt16a_r1;
#endif
1002

1003
  unsigned short scrambling_id = coreset->pdcch_dmrs_scrambling_id;
1004
  // checking if re-initialization of scrambling IDs is needed (should be done here but scrambling ID for PDCCH is not taken from RRC)
1005 1006 1007 1008
  if (scrambling_id != ue->scramblingID_pdcch){
    ue->scramblingID_pdcch = scrambling_id;
    nr_gold_pdcch(ue,ue->scramblingID_pdcch);
  }
1009

1010 1011 1012 1013 1014
  int dmrs_ref = 0;
  if (coreset->CoreSetType == NFAPI_NR_CSET_CONFIG_PDCCH_CONFIG)
    dmrs_ref = BWPStart;
  // generate pilot
  int pilot[(nb_rb_coreset + dmrs_ref) * 3] __attribute__((aligned(16)));
1015
  // Note: pilot returned by the following function is already the complex conjugate of the transmitted DMRS
1016
  nr_pdcch_dmrs_rx(ue, Ns, ue->nr_gold_pdcch[gNB_id][Ns][symbol], (c16_t *)pilot, 2000, (nb_rb_coreset + dmrs_ref));
1017 1018 1019

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

sfn's avatar
sfn committed
1020
    k = coreset_start_subcarrier;
1021
    pil   = (int16_t *)&pilot[dmrs_ref*3];
1022
    rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+1)];
1023
    dl_ch = (int16_t *)&pdcch_dl_ch_estimates[aarx][ch_offset];
1024

1025
    memset(dl_ch,0,4*(ue->frame_parms.ofdm_symbol_size));
1026

hongzhi wang's avatar
hongzhi wang committed
1027
#ifdef DEBUG_PDCCH
1028
    printf("pdcch ch est pilot addr %p RB_DL %d\n",&pilot[dmrs_ref*3], ue->frame_parms.N_RB_DL);
1029 1030 1031 1032
    printf("k %d, first_carrier %d\n",k,ue->frame_parms.first_carrier_offset);
    printf("rxF addr %p\n", rxF);

    printf("dl_ch addr %p\n",dl_ch);
1033
#endif
1034
  #if CH_INTERP
1035
    //    if ((ue->frame_parms.N_RB_DL&1)==0) {
1036 1037 1038
    // Treat first 2 pilots specially (left edge)
    ch[0] = (int16_t)(((int32_t)pil[0]*rxF[0] - (int32_t)pil[1]*rxF[1])>>15);
    ch[1] = (int16_t)(((int32_t)pil[0]*rxF[1] + (int32_t)pil[1]*rxF[0])>>15);
hongzhi wang's avatar
hongzhi wang committed
1039
#ifdef DEBUG_PDCCH
1040 1041
    printf("ch 0 %d\n",((int32_t)pil[0]*rxF[0] - (int32_t)pil[1]*rxF[1]));
    printf("pilot 0 : rxF - > (%d,%d) addr %p  ch -> (%d,%d), pil -> (%d,%d) \n",rxF[0],rxF[1],&rxF[0],ch[0],ch[1],pil[0],pil[1]);
1042
#endif
1043 1044 1045 1046
    multadd_real_vector_complex_scalar(fl,
				       ch,
				       dl_ch,
				       16);
1047 1048
    pil += 2;
    rxF += 8;
1049 1050 1051 1052 1053 1054
    k   += 4;

    if (k >= ue->frame_parms.ofdm_symbol_size) {
      k  -= ue->frame_parms.ofdm_symbol_size;
      rxF = (int16_t *)&rxdataF[aarx][(symbol_offset+k+1)];
    }
1055

1056 1057
    ch[0] = (int16_t)(((int32_t)pil[0]*rxF[0] - (int32_t)pil[1]*rxF[1])>>15);
    ch[1] = (int16_t)(((int32_t)pil[0]*rxF[1] + (int32_t)pil[1]*rxF[0])>>15);
hongzhi wang's avatar
hongzhi wang committed
1058
#ifdef DEBUG_PDCCH
1059
    printf("pilot 1 : rxF - > (%d,%d) ch -> (%d,%d), pil -> (%d,%d) \n",rxF[0],rxF[1],ch[0],ch[1],pil[0],pil[1]);
1060
#endif
1061 1062 1063 1064
    multadd_real_vector_complex_scalar(fm,
				       ch,
				       dl_ch,
				       16);
1065 1066
    pil += 2;
    rxF += 8;
1067 1068 1069 1070 1071 1072
    k   += 4;

    if (k >= ue->frame_parms.ofdm_symbol_size) {
      k  -= ue->frame_parms.ofdm_symbol_size;
      rxF = (int16_t *)&rxdataF[aarx][(symbol_offset+k+1)];
    }
1073

1074 1075
    ch[0] = (int16_t)(((int32_t)pil[0]*rxF[0] - (int32_t)pil[1]*rxF[1])>>15);
    ch[1] = (int16_t)(((int32_t)pil[0]*rxF[1] + (int32_t)pil[1]*rxF[0])>>15);
1076

hongzhi wang's avatar
hongzhi wang committed
1077
#ifdef DEBUG_PDCCH
1078
    printf("pilot 2 : rxF - > (%d,%d) ch -> (%d,%d), pil -> (%d,%d) \n",rxF[0],rxF[1],ch[0],ch[1],pil[0],pil[1]);
1079 1080
#endif

1081 1082 1083 1084
    multadd_real_vector_complex_scalar(fr,
				       ch,
				       dl_ch,
				       16);
1085
#ifdef DEBUG_PDCCH
1086 1087
    for (int m =0; m<12; m++)
      printf("data :  dl_ch -> (%d,%d)\n",dl_ch[0+2*m],dl_ch[1+2*m]);
1088 1089 1090
#endif
    dl_ch += 24;

1091 1092
    pil += 2;
    rxF += 8;
1093
    k   += 4;
1094

1095
    for (pilot_cnt=3; pilot_cnt<(3*nb_rb_coreset); pilot_cnt += 3) {
1096

1097 1098 1099 1100
      if (k >= ue->frame_parms.ofdm_symbol_size) {
        k  -= ue->frame_parms.ofdm_symbol_size;
        rxF = (int16_t *)&rxdataF[aarx][(symbol_offset+k+1)];
      }
1101

1102 1103
      ch[0] = (int16_t)(((int32_t)pil[0]*rxF[0] - (int32_t)pil[1]*rxF[1])>>15);
      ch[1] = (int16_t)(((int32_t)pil[0]*rxF[1] + (int32_t)pil[1]*rxF[0])>>15);
hongzhi wang's avatar
hongzhi wang committed
1104
#ifdef DEBUG_PDCCH
1105
      printf("pilot %u : rxF - > (%d,%d) ch -> (%d,%d), pil -> (%d,%d) \n",pilot_cnt,rxF[0],rxF[1],ch[0],ch[1],pil[0],pil[1]);
1106
#endif
1107 1108 1109 1110
      multadd_real_vector_complex_scalar(fl,
					 ch,
					 dl_ch,
					 16);
1111

1112 1113
      //for (int i= 0; i<8; i++)
      //            printf("pilot_cnt %d dl_ch %d %d\n", pilot_cnt, dl_ch+i, *(dl_ch+i));
1114

1115 1116
      pil += 2;
      rxF += 8;
1117 1118 1119 1120 1121 1122
      k   += 4;

      if (k >= ue->frame_parms.ofdm_symbol_size) {
        k  -= ue->frame_parms.ofdm_symbol_size;
        rxF = (int16_t *)&rxdataF[aarx][(symbol_offset+k+1)];
      }
1123

1124 1125
      ch[0] = (int16_t)(((int32_t)pil[0]*rxF[0] - (int32_t)pil[1]*rxF[1])>>15);
      ch[1] = (int16_t)(((int32_t)pil[0]*rxF[1] + (int32_t)pil[1]*rxF[0])>>15);
hongzhi wang's avatar
hongzhi wang committed
1126
#ifdef DEBUG_PDCCH
1127
      printf("pilot %u : rxF - > (%d,%d) ch -> (%d,%d), pil -> (%d,%d) \n",pilot_cnt+1,rxF[0],rxF[1],ch[0],ch[1],pil[0],pil[1]);
Hongzhi Wang's avatar
Hongzhi Wang committed
1128
#endif
1129 1130 1131 1132
      multadd_real_vector_complex_scalar(fm,
					 ch,
					 dl_ch,
					 16);
1133 1134
      pil += 2;
      rxF += 8;
1135 1136 1137 1138 1139 1140
      k   += 4;

      if (k >= ue->frame_parms.ofdm_symbol_size) {
        k  -= ue->frame_parms.ofdm_symbol_size;
        rxF = (int16_t *)&rxdataF[aarx][(symbol_offset+k+1)];
      }
Hongzhi Wang's avatar
Hongzhi Wang committed
1141

1142 1143
      ch[0] = (int16_t)(((int32_t)pil[0]*rxF[0] - (int32_t)pil[1]*rxF[1])>>15);
      ch[1] = (int16_t)(((int32_t)pil[0]*rxF[1] + (int32_t)pil[1]*rxF[0])>>15);
Hongzhi Wang's avatar
Hongzhi Wang committed
1144

hongzhi wang's avatar
hongzhi wang committed
1145
#ifdef DEBUG_PDCCH
1146
      printf("pilot %u : rxF - > (%d,%d) ch -> (%d,%d), pil -> (%d,%d) \n",pilot_cnt+2,rxF[0],rxF[1],ch[0],ch[1],pil[0],pil[1]);
hongzhi wang's avatar
hongzhi wang committed
1147
#endif
Hongzhi Wang's avatar
Hongzhi Wang committed
1148

1149 1150 1151 1152
      multadd_real_vector_complex_scalar(fr,
					 ch,
					 dl_ch,
					 16);
1153 1154 1155 1156
#ifdef DEBUG_PDCCH
    for (int m =0; m<12; m++)
      printf("data :  dl_ch -> (%d,%d)\n",dl_ch[0+2*m],dl_ch[1+2*m]);
#endif
1157
      dl_ch += 24;
Hongzhi Wang's avatar
Hongzhi Wang committed
1158

1159 1160 1161
      pil += 2;
      rxF += 8;
      k   += 4;
1162
    }
1163 1164 1165 1166 1167 1168 1169 1170 1171
  #else //ELSE CH_INTERP
    int ch_sum[2] = {0, 0};

    for (pilot_cnt = 0; pilot_cnt < 3*nb_rb_coreset; pilot_cnt++) {
      if (k >= ue->frame_parms.ofdm_symbol_size) {
        k  -= ue->frame_parms.ofdm_symbol_size;
        rxF = (int16_t *)&rxdataF[aarx][(symbol_offset+k+1)];
      }
#ifdef DEBUG_PDCCH
1172
      printf("pilot[%u] = (%d, %d)\trxF[%d] = (%d, %d)\n", pilot_cnt, pil[0], pil[1], k+1, rxF[0], rxF[1]);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
#endif
      ch_sum[0] += (int16_t)(((int32_t)pil[0]*rxF[0] - (int32_t)pil[1]*rxF[1])>>15);
      ch_sum[1] += (int16_t)(((int32_t)pil[0]*rxF[1] + (int32_t)pil[1]*rxF[0])>>15);
      pil += 2;
      rxF += 8;
      k   += 4;

      if (pilot_cnt % 3 == 2) {
        ch[0] = ch_sum[0] / 3;
        ch[1] = ch_sum[1] / 3;
        multadd_real_vector_complex_scalar(filt16a_1, ch, dl_ch, 16);
1184 1185 1186 1187
#ifdef DEBUG_PDCCH
        for (int m =0; m<12; m++)
          printf("data :  dl_ch -> (%d,%d)\n",dl_ch[0+2*m],dl_ch[1+2*m]);
#endif
1188 1189 1190 1191 1192 1193
        dl_ch += 24;
        ch_sum[0] = 0;
        ch_sum[1] = 0;
      }
    }
  #endif //END CH_INTERP
Hongzhi Wang's avatar
Hongzhi Wang committed
1194 1195


1196
    //}
Hongzhi Wang's avatar
Hongzhi Wang committed
1197 1198 1199 1200

  }
}

1201 1202 1203 1204 1205 1206
void NFAPI_NR_DMRS_TYPE1_linear_interp(NR_DL_FRAME_PARMS *frame_parms,
                                       c16_t *rxF,
                                       c16_t *pil,
                                       c16_t *dl_ch,
                                       unsigned short bwp_start_subcarrier,
                                       unsigned short nb_rb_pdsch,
1207 1208
                                       int8_t delta,
                                       delay_t *delay)
1209
{
1210
  c16_t *dl_ch0 = dl_ch;
1211 1212
  int re_offset = bwp_start_subcarrier % frame_parms->ofdm_symbol_size;

1213 1214
  c16_t dl_ls_est[frame_parms->ofdm_symbol_size] __attribute__((aligned(32)));
  memset(dl_ls_est, 0, sizeof(dl_ls_est));
1215 1216 1217

  for (int pilot_cnt = 0; pilot_cnt < 6 * nb_rb_pdsch; pilot_cnt++) {
    if (pilot_cnt % 2 == 0) {
1218
      c16_t ch = c16mulShift(*pil, rxF[re_offset], 15);
1219 1220 1221 1222 1223 1224
      pil++;
      re_offset = (re_offset + 2) % frame_parms->ofdm_symbol_size;
      ch = c16maddShift(*pil, rxF[re_offset], ch, 15);
      ch = c16Shift(ch, 1);
      pil++;
      re_offset = (re_offset + 2) % frame_parms->ofdm_symbol_size;
1225 1226 1227
      for (int k = pilot_cnt << 1; k < (pilot_cnt << 1) + 4; k++) {
        dl_ls_est[k] = ch;
      }
1228 1229 1230
    }

#ifdef DEBUG_PDSCH
1231 1232 1233 1234 1235 1236 1237 1238
    printf("pilot %3d: pil -> (%6d,%6d), rxF -> (%4d,%4d), ch -> (%4d,%4d) \n",
           pilot_cnt,
           pil->r,
           pil->i,
           rxF[re_offset].r,
           rxF[re_offset].i,
           dl_ls_est[pilot_cnt << 1].r,
           dl_ls_est[pilot_cnt << 1].i);
1239
#endif
1240 1241
  }

1242 1243 1244
  c16_t ch_estimates_time[frame_parms->ofdm_symbol_size] __attribute__((aligned(32)));
  memset(ch_estimates_time, 0, sizeof(ch_estimates_time));
  nr_est_delay(frame_parms->ofdm_symbol_size, dl_ls_est, ch_estimates_time, delay);
1245 1246
  int delay_idx = get_delay_idx(delay->est_delay, MAX_DELAY_COMP);
  c16_t *dl_delay_table = frame_parms->delay_table[delay_idx];
1247

1248
  for (int pilot_cnt = 0; pilot_cnt < 6 * nb_rb_pdsch; pilot_cnt++) {
1249 1250
    int k = pilot_cnt << 1;
    c16_t ch = c16mulShift(dl_ls_est[k], dl_delay_table[k], 8);
1251
    if (pilot_cnt == 0) { // Treat first pilot
1252
      c16multaddVectRealComplex(filt16_ul_p0, &ch, dl_ch, 16);
1253
    } else if (pilot_cnt == 1 || pilot_cnt == 2) {
1254
      c16multaddVectRealComplex(filt16_ul_p1p2, &ch, dl_ch, 16);
1255
    } else if (pilot_cnt == 6 * nb_rb_pdsch - 1) { // Treat last pilot
1256
      c16multaddVectRealComplex(filt16_ul_last, &ch, dl_ch, 16);
1257
    } else { // Treat middle pilots
1258
      c16multaddVectRealComplex(filt16_ul_middle, &ch, dl_ch, 16);
1259
      if (pilot_cnt % 2 == 0) {
1260
        dl_ch += 4;
1261 1262 1263
      }
    }
  }
1264 1265 1266 1267 1268 1269 1270 1271

  // Revert delay
  dl_ch = dl_ch0;
  int inv_delay_idx = get_delay_idx(-delay->est_delay, MAX_DELAY_COMP);
  c16_t *dl_inv_delay_table = frame_parms->delay_table[inv_delay_idx];
  for (int k = 0; k < 12 * nb_rb_pdsch; k++) {
    dl_ch[k] = c16mulShift(dl_ch[k], dl_inv_delay_table[k], 8);
  }
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 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
}

void NFAPI_NR_DMRS_TYPE1_average_prb(NR_DL_FRAME_PARMS *frame_parms,
                                     c16_t *rxF,
                                     c16_t *pil,
                                     c16_t *dl_ch,
                                     unsigned short bwp_start_subcarrier,
                                     unsigned short nb_rb_pdsch)
{
  int re_offset = bwp_start_subcarrier % frame_parms->ofdm_symbol_size;
  c16_t ch = {0};
  int P_average = 6;

  c32_t ch32 = {0};
  for (int p_av = 0; p_av < P_average; p_av++) {
    ch32 = c32x16maddShift(*pil, rxF[re_offset], ch32, 15);
    pil++;
    re_offset = (re_offset + 2) % frame_parms->ofdm_symbol_size;
  }
  ch = c16x32div(ch32, P_average);

#if NO_INTERP
  for (int i = 0; i < 2 * P_average; i++) {
    dl_ch[i] = ch;
  }
  dl_ch += 2 * P_average;
#else
  c16multaddVectRealComplex(filt8_avlip0, &ch, dl_ch, 8);
  dl_ch += 16;
  c16multaddVectRealComplex(filt8_avlip1, &ch, dl_ch, 8);
  dl_ch += 16;
  c16multaddVectRealComplex(filt8_avlip2, &ch, dl_ch, 8);
  dl_ch -= 24;
#endif

  for (int pilot_cnt = P_average; pilot_cnt < 6 * (nb_rb_pdsch - 1); pilot_cnt += P_average) {
    ch32.r = 0;
    ch32.i = 0;
    for (int p_av = 0; p_av < P_average; p_av++) {
      ch32 = c32x16maddShift(*pil, rxF[re_offset], ch32, 15);
      pil++;
      re_offset = (re_offset + 2) % frame_parms->ofdm_symbol_size;
    }
    ch = c16x32div(ch32, P_average);

#if NO_INTERP
    for (int i = 0; i < 2 * P_average; i++) {
      dl_ch[i] = ch;
    }
    dl_ch += 2 * P_average;
#else
    dl_ch[3].r += (ch.r * 1365) >> 15; // 1/12*16384
    dl_ch[3].i += (ch.i * 1365) >> 15; // 1/12*16384
    dl_ch += 4;
    c16multaddVectRealComplex(filt8_avlip3, &ch, dl_ch, 8);
    dl_ch += 8;
    c16multaddVectRealComplex(filt8_avlip4, &ch, dl_ch, 8);
    dl_ch += 8;
    c16multaddVectRealComplex(filt8_avlip5, &ch, dl_ch, 8);
    dl_ch -= 8;
#endif
  }

  ch32.r = 0;
  ch32.i = 0;
  for (int p_av = 0; p_av < P_average; p_av++) {
    ch32 = c32x16maddShift(*pil, rxF[re_offset], ch32, 15);
    pil++;
    re_offset = (re_offset + 2) % frame_parms->ofdm_symbol_size;
  }
  ch = c16x32div(ch32, P_average);

#if NO_INTERP
  for (int i = 0; i < 2 * P_average; i++) {
    dl_ch[i] = ch;
  }
  dl_ch += 2 * P_average;
#else
  dl_ch[3].r += (ch.r * 1365) >> 15; // 1/12*16384
  dl_ch[3].i += (ch.i * 1365) >> 15; // 1/12*16384
  dl_ch += 4;
  c16multaddVectRealComplex(filt8_avlip3, &ch, dl_ch, 8);
  dl_ch += 8;
  c16multaddVectRealComplex(filt8_avlip6, &ch, dl_ch, 8);
#endif
}

void NFAPI_NR_DMRS_TYPE2_linear_interp(NR_DL_FRAME_PARMS *frame_parms,
                                       c16_t *rxF,
                                       c16_t *pil,
                                       c16_t *dl_ch,
                                       unsigned short bwp_start_subcarrier,
                                       unsigned short nb_rb_pdsch,
                                       int8_t delta,
1366 1367
                                       unsigned short p,
                                       delay_t *delay)
1368 1369 1370
{
  int re_offset = bwp_start_subcarrier % frame_parms->ofdm_symbol_size;
  c16_t *dl_ch0 = dl_ch;
1371 1372 1373

  c16_t dl_ls_est[frame_parms->ofdm_symbol_size] __attribute__((aligned(32)));
  memset(dl_ls_est, 0, sizeof(dl_ls_est));
1374 1375

  for (int pilot_cnt = 0; pilot_cnt < 4 * nb_rb_pdsch; pilot_cnt += 2) {
1376
    c16_t ch_l = c16mulShift(*pil, rxF[re_offset], 15);
1377
#ifdef DEBUG_PDSCH
1378
    printf("pilot %3d: pil -> (%6d,%6d), rxF -> (%4d,%4d), ch -> (%4d,%4d) \n", pilot_cnt, pil->r, pil->i, rxF[re_offset].r, rxF[re_offset].i, ch_l.r, ch_l.i);
1379 1380 1381
#endif
    pil++;
    re_offset = (re_offset + 1) % frame_parms->ofdm_symbol_size;
1382
    c16_t ch_r = c16mulShift(*pil, rxF[re_offset], 15);
1383
#ifdef DEBUG_PDSCH
1384
    printf("pilot %3d: pil -> (%6d,%6d), rxF -> (%4d,%4d), ch -> (%4d,%4d) \n", pilot_cnt, pil->r, pil->i, rxF[re_offset].r, rxF[re_offset].i, ch_r.r, ch_r.i);
1385
#endif
1386 1387 1388
    c16_t ch = c16addShift(ch_l, ch_r, 1);
    for (int k = 3 * pilot_cnt; k < (3 * pilot_cnt) + 6; k++) {
      dl_ls_est[k] = ch;
1389 1390 1391 1392 1393
    }
    pil++;
    re_offset = (re_offset + 5) % frame_parms->ofdm_symbol_size;
  }

1394 1395 1396
  c16_t ch_estimates_time[frame_parms->ofdm_symbol_size] __attribute__((aligned(32)));
  memset(ch_estimates_time, 0, sizeof(ch_estimates_time));
  nr_est_delay(frame_parms->ofdm_symbol_size, dl_ls_est, ch_estimates_time, delay);
1397 1398 1399 1400
  int delay_idx = get_delay_idx(delay->est_delay, MAX_DELAY_COMP);
  c16_t *dl_delay_table = frame_parms->delay_table[delay_idx];

  for (int pilot_cnt = 0; pilot_cnt < 6 * nb_rb_pdsch; pilot_cnt++) {
1401 1402
    int k = (pilot_cnt / 3) * 6;
    c16_t ch = c16mulShift(dl_ls_est[k], dl_delay_table[k], 8);
1403
    if (pilot_cnt == 0) { // Treat first pilot
1404 1405 1406
      c16multaddVectRealComplex(filt16_ul_p0, &ch, dl_ch, 16);
    } else if (pilot_cnt == 1 || pilot_cnt == 2) {
      c16multaddVectRealComplex(filt16_ul_p1p2, &ch, dl_ch, 16);
1407
    } else if (pilot_cnt == 6 * nb_rb_pdsch - 1) { // Treat last pilot
1408
      c16multaddVectRealComplex(filt16_ul_last, &ch, dl_ch, 16);
1409
    } else { // Treat middle pilots
1410
      c16multaddVectRealComplex(filt16_ul_middle, &ch, dl_ch, 16);
1411 1412 1413 1414 1415 1416
      if (pilot_cnt % 2 == 0) {
        dl_ch += 4;
      }
    }
  }

1417 1418 1419 1420 1421 1422
  // Revert delay
  dl_ch = dl_ch0;
  int inv_delay_idx = get_delay_idx(-delay->est_delay, MAX_DELAY_COMP);
  c16_t *dl_inv_delay_table = frame_parms->delay_table[inv_delay_idx];
  for (int k = 0; k < 12 * nb_rb_pdsch; k++) {
    dl_ch[k] = c16mulShift(dl_ch[k], dl_inv_delay_table[k], 8);
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
  }
}

void NFAPI_NR_DMRS_TYPE2_average_prb(NR_DL_FRAME_PARMS *frame_parms,
                                     c16_t *rxF,
                                     c16_t *pil,
                                     c16_t *dl_ch,
                                     unsigned short bwp_start_subcarrier,
                                     unsigned short nb_rb_pdsch)
{
  int re_offset = bwp_start_subcarrier % frame_parms->ofdm_symbol_size;
  c16_t ch = {0};
  int P_average = 4;

  c32_t ch32 = {0};
  for (int p_av = 0; p_av < P_average; p_av++) {
    ch32 = c32x16maddShift(*pil, rxF[re_offset], ch32, 15);
    pil++;
    re_offset = (re_offset + 1) % frame_parms->ofdm_symbol_size;
  }
  ch = c16x32div(ch32, P_average);

#if NO_INTERP
  for (int i = 0; i < 3 * P_average; i++) {
    dl_ch[i] = ch;
  }
  dl_ch += 3 * P_average;
#else
  c16multaddVectRealComplex(filt8_avlip0, &ch, dl_ch, 8);
  dl_ch += 8;
  c16multaddVectRealComplex(filt8_avlip1, &ch, dl_ch, 8);
  dl_ch += 8;
  c16multaddVectRealComplex(filt8_avlip2, &ch, dl_ch, 8);
  dl_ch -= 12;
#endif

  for (int pilot_cnt = P_average; pilot_cnt < 4 * (nb_rb_pdsch - 1); pilot_cnt += P_average) {
    ch32.r = 0;
    ch32.i = 0;
    for (int p_av = 0; p_av < P_average; p_av++) {
      ch32 = c32x16maddShift(*pil, rxF[re_offset], ch32, 15);
      pil++;
      re_offset = (re_offset + 5) % frame_parms->ofdm_symbol_size;
    }
    ch = c16x32div(ch32, P_average);

#if NO_INTERP
    for (int i = 0; i < 3 * P_average; i++) {
      dl_ch[i] = ch;
    }
    dl_ch += 3 * P_average;
#else
    dl_ch[3].r += (ch.r * 1365) >> 15; // 1/12*16384
    dl_ch[3].i += (ch.i * 1365) >> 15; // 1/12*16384
    dl_ch += 4;
    c16multaddVectRealComplex(filt8_avlip3, &ch, dl_ch, 8);
    dl_ch += 8;
    c16multaddVectRealComplex(filt8_avlip4, &ch, dl_ch, 8);
    dl_ch += 8;
    c16multaddVectRealComplex(filt8_avlip5, &ch, dl_ch, 8);
    dl_ch -= 8;
#endif
  }

  ch32.r = 0;
  ch32.i = 0;
  for (int p_av = 0; p_av < P_average; p_av++) {
    ch32 = c32x16maddShift(*pil, rxF[re_offset], ch32, 15);
    pil++;
    re_offset = (re_offset + 5) % frame_parms->ofdm_symbol_size;
  }
  ch = c16x32div(ch32, P_average);

#if NO_INTERP
  for (int i = 0; i < 3 * P_average; i++) {
    dl_ch[i] = ch;
  }
  dl_ch += 3 * P_average;
#else
  dl_ch[3].r += (ch.r * 1365) >> 15; // 1/12*16384
  dl_ch[3].i += (ch.i * 1365) >> 15; // 1/12*16384
  dl_ch += 4;
  c16multaddVectRealComplex(filt8_avlip3, &ch, dl_ch, 8);
  dl_ch += 8;
  c16multaddVectRealComplex(filt8_avlip6, &ch, dl_ch, 8);
#endif
}
1510
int nr_pdsch_channel_estimation(PHY_VARS_NR_UE *ue,
1511
                                const UE_nr_rxtx_proc_t *proc,
yilmazt's avatar
yilmazt committed
1512 1513
                                unsigned short p,
                                unsigned char symbol,
1514 1515
                                unsigned char nscid,
                                unsigned short scrambling_id,
1516
                                unsigned short BWPStart,
1517
                                uint8_t config_type,
1518
                                uint16_t rb_offset,
yilmazt's avatar
yilmazt committed
1519
                                unsigned short bwp_start_subcarrier,
1520 1521 1522
                                unsigned short nb_rb_pdsch,
                                uint32_t pdsch_est_size,
                                int32_t dl_ch_estimates[][pdsch_est_size],
1523 1524
                                int rxdataFsize,
                                c16_t rxdataF[][rxdataFsize])
1525
{
1526 1527
  int gNB_id = proc->gNB_id;
  int Ns = proc->nr_slot_rx;
1528 1529
  const int ch_offset = ue->frame_parms.ofdm_symbol_size * symbol;
  const int symbol_offset = ue->frame_parms.ofdm_symbol_size * symbol;
1530

1531
#ifdef DEBUG_PDSCH
1532
  printf("PDSCH Channel Estimation : gNB_id %d ch_offset %d, symbol_offset %d OFDM size %d, Ncp=%d, Ns=%d, bwp_start_subcarrier=%d symbol %d\n",
1533 1534 1535 1536 1537 1538
         gNB_id,
         ch_offset,
         symbol_offset,
         ue->frame_parms.ofdm_symbol_size,
         ue->frame_parms.Ncp,
         Ns,
1539
         bwp_start_subcarrier,
1540
         symbol);
1541 1542
#endif

1543 1544
  // generate pilot for gNB port number 1000+p
  int8_t delta = get_delta(p, config_type);
1545

1546
  // checking if re-initialization of scrambling IDs is needed
1547
  if (scrambling_id != ue->scramblingID_dlsch[nscid]) {
1548 1549 1550
    ue->scramblingID_dlsch[nscid] = scrambling_id;
    nr_gold_pdsch(ue, nscid, scrambling_id);
  }
1551

1552
  c16_t pilot[3280] __attribute__((aligned(16)));
1553
  // Note: pilot returned by the following function is already the complex conjugate of the transmitted DMRS
1554
  nr_pdsch_dmrs_rx(ue, Ns, ue->nr_gold_pdsch[gNB_id][Ns][symbol][nscid], pilot, 1000 + p, 0, nb_rb_pdsch + rb_offset, config_type);
1555

1556 1557 1558 1559
  uint8_t nushift = 0;
  if (config_type == NFAPI_NR_DMRS_TYPE1) {
    nushift = (p >> 1) & 1;
  } else { // NFAPI_NR_DMRS_TYPE2
1560
    nushift = delta;
1561
  }
1562

1563 1564
  delay_t delay = {0};

1565
  for (int aarx = 0; aarx < ue->frame_parms.nb_antennas_rx; aarx++) {
1566

hongzhi wang's avatar
hongzhi wang committed
1567
#ifdef DEBUG_PDSCH
1568 1569 1570
    printf("\n============================================\n");
    printf("==== Tx port %i, Rx antenna %i, Symbol %i ====\n", p, aarx, symbol);
    printf("============================================\n");
1571 1572
#endif

1573
    c16_t *rxF = &rxdataF[aarx][symbol_offset + nushift];
1574 1575
    c16_t *dl_ch = (c16_t *)&dl_ch_estimates[p * ue->frame_parms.nb_antennas_rx + aarx][ch_offset];
    memset(dl_ch, 0, sizeof(*dl_ch) * ue->frame_parms.ofdm_symbol_size);
1576

1577
    if (config_type == NFAPI_NR_DMRS_TYPE1 && ue->chest_freq == 0) {
1578 1579 1580 1581 1582 1583
      NFAPI_NR_DMRS_TYPE1_linear_interp(&ue->frame_parms,
                                        rxF,
                                        &pilot[6 * rb_offset],
                                        dl_ch,
                                        bwp_start_subcarrier,
                                        nb_rb_pdsch,
1584 1585
                                        delta,
                                        &delay);
1586

1587
    } else if (config_type == NFAPI_NR_DMRS_TYPE2 && ue->chest_freq == 0) {
1588 1589 1590 1591 1592 1593 1594
      NFAPI_NR_DMRS_TYPE2_linear_interp(&ue->frame_parms,
                                        rxF,
                                        &pilot[4 * rb_offset],
                                        dl_ch,
                                        bwp_start_subcarrier,
                                        nb_rb_pdsch,
                                        delta,
1595 1596
                                        p,
                                        &delay);
1597

1598
    } else if (config_type == NFAPI_NR_DMRS_TYPE1) {
1599 1600 1601 1602 1603 1604
      NFAPI_NR_DMRS_TYPE1_average_prb(&ue->frame_parms,
                                      rxF,
                                      &pilot[6 * rb_offset],
                                      dl_ch,
                                      bwp_start_subcarrier,
                                      nb_rb_pdsch);
1605

1606
    } else {
1607 1608 1609 1610 1611 1612
      NFAPI_NR_DMRS_TYPE2_average_prb(&ue->frame_parms,
                                      rxF,
                                      &pilot[4 * rb_offset],
                                      dl_ch,
                                      bwp_start_subcarrier,
                                      nb_rb_pdsch);
1613
    }
1614

1615
#ifdef DEBUG_PDSCH
1616 1617 1618 1619
    dl_ch = (c16_t *)&dl_ch_estimates[p * ue->frame_parms.nb_antennas_rx + aarx][ch_offset];
    for (uint16_t idxP = 0; idxP < ceil((float)nb_rb_pdsch * 12 / 8); idxP++) {
      for (uint8_t idxI = 0; idxI < 8; idxI++) {
        printf("%4d\t%4d\t", dl_ch[idxP * 8 + idxI].r, dl_ch[idxP * 8 + idxI].i);
1620
      }
1621
      printf("%2d\n", idxP);
1622
    }
1623
#endif
1624
  }
1625
  return 0;
1626 1627
}

1628 1629 1630 1631
/*******************************************************************
 *
 * NAME :         nr_pdsch_ptrs_processing
 *
1632
 * PARAMETERS :   PHY_VARS_NR_UE    : ue data structure
1633 1634 1635 1636
 *                c16_t             : ptrs_phase_per_slot array
 *                int32_t           : ptrs_re_per_slot array
 *                uint32_t          : rx_size,
 *                int32_t           : rxdataF_comp, array
1637 1638 1639
 *                NR_DL_FRAME_PARMS : frame_parms pointer
 *                NR_DL_UE_HARQ_t   : dlsch0_harq pointer
 *                NR_DL_UE_HARQ_t   : dlsch1_harq pointer
1640
 *                uint8_t           : gNB_id,
1641
 *                uint8_t           : nr_slot_rx,
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
 *                unsigned char     : symbol,
 *                uint32_t          : nb_re_pdsch,
 *                uint16_t          : rnti
 *                RX_type_t         : rx_type
 * RETURN : Nothing
 *
 * DESCRIPTION :
 *  If ptrs is enabled process the symbol accordingly
 *  1) Estimate common phase error per PTRS symbol
 *  2) Interpolate PTRS estimated value in TD after all PTRS symbols
 *  3) Compensate signal with PTRS estimation for slot
 *********************************************************************/
void nr_pdsch_ptrs_processing(PHY_VARS_NR_UE *ue,
1655
                              int nbRx,
1656 1657
                              c16_t ptrs_phase_per_slot[][14],
                              int32_t ptrs_re_per_slot[][14],
1658 1659
                              uint32_t rx_size_symbol,
                              int32_t rxdataF_comp[][nbRx][rx_size_symbol * NR_SYMBOLS_PER_SLOT],
1660 1661 1662
                              NR_DL_FRAME_PARMS *frame_parms,
                              NR_DL_UE_HARQ_t *dlsch0_harq,
                              NR_DL_UE_HARQ_t *dlsch1_harq,
1663
                              uint8_t gNB_id,
1664 1665 1666 1667
                              uint8_t nr_slot_rx,
                              unsigned char symbol,
                              uint32_t nb_re_pdsch,
                              uint16_t rnti,
1668
                              NR_UE_DLSCH_t dlsch[2])
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
{
  //#define DEBUG_DL_PTRS 1
  int32_t *ptrs_re_symbol = NULL;
  int8_t   ret = 0;
  /* harq specific variables */
  uint8_t  symbInSlot       = 0;
  uint16_t *startSymbIndex  = NULL;
  uint16_t *nbSymb          = NULL;
  uint8_t  *L_ptrs          = NULL;
  uint8_t  *K_ptrs          = NULL;
  uint16_t *dmrsSymbPos     = NULL;
  uint16_t *ptrsSymbPos     = NULL;
  uint8_t  *ptrsSymbIdx     = NULL;
  uint8_t  *ptrsReOffset    = NULL;
  uint16_t *nb_rb           = NULL;
1684
  int nscid = 0;
1685

1686
  if(dlsch0_harq->status == ACTIVE) {
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
    symbInSlot      = dlsch[0].dlsch_config.start_symbol + dlsch[0].dlsch_config.number_symbols;
    startSymbIndex  = &dlsch[0].dlsch_config.start_symbol;
    nbSymb          = &dlsch[0].dlsch_config.number_symbols;
    L_ptrs          = &dlsch[0].dlsch_config.PTRSTimeDensity;
    K_ptrs          = &dlsch[0].dlsch_config.PTRSFreqDensity;
    dmrsSymbPos     = &dlsch[0].dlsch_config.dlDmrsSymbPos;
    ptrsReOffset    = &dlsch[0].dlsch_config.PTRSReOffset;
    nb_rb           = &dlsch[0].dlsch_config.number_rbs;
    ptrsSymbPos     = &dlsch[0].ptrs_symbols;
    ptrsSymbIdx     = &dlsch[0].ptrs_symbol_index;
1697
    nscid = dlsch[0].dlsch_config.nscid;
1698
  }
1699
  if(dlsch1_harq) {
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
    symbInSlot      = dlsch[1].dlsch_config.start_symbol + dlsch[1].dlsch_config.number_symbols;
    startSymbIndex  = &dlsch[1].dlsch_config.start_symbol;
    nbSymb          = &dlsch[1].dlsch_config.number_symbols;
    L_ptrs          = &dlsch[1].dlsch_config.PTRSTimeDensity;
    K_ptrs          = &dlsch[1].dlsch_config.PTRSFreqDensity;
    dmrsSymbPos     = &dlsch[1].dlsch_config.dlDmrsSymbPos;
    ptrsReOffset    = &dlsch[1].dlsch_config.PTRSReOffset;
    nb_rb           = &dlsch[1].dlsch_config.number_rbs;
    ptrsSymbPos     = &dlsch[1].ptrs_symbols;
    ptrsSymbIdx     = &dlsch[1].ptrs_symbol_index;
1710
    nscid = dlsch[1].dlsch_config.nscid;
1711 1712
  }
  /* loop over antennas */
1713
  for (int aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
1714 1715
    c16_t *phase_per_symbol = (c16_t*)ptrs_phase_per_slot[aarx];
    ptrs_re_symbol = (int32_t*)ptrs_re_per_slot[aarx];
1716
    ptrs_re_symbol[symbol] = 0;
1717
    phase_per_symbol[symbol].i = 0; // Imag
1718
    /* set DMRS estimates to 0 angle with magnitude 1 */
1719
    if(is_dmrs_symbol(symbol,*dmrsSymbPos)) {
1720
      /* set DMRS real estimation to 32767 */
1721
      phase_per_symbol[symbol].r=INT16_MAX; // 32767
1722
#ifdef DEBUG_DL_PTRS
1723
      printf("[PHY][PTRS]: DMRS Symbol %d -> %4d + j*%4d\n", symbol, phase_per_symbol[symbol].r,phase_per_symbol[symbol].i);
1724 1725
#endif
    }
1726
    else { // real ptrs value is set to 0
1727
      phase_per_symbol[symbol].r = 0; // Real
1728 1729
    }

1730 1731
    if(dlsch0_harq->status == ACTIVE) {
      if(symbol == *startSymbIndex) {
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
        *ptrsSymbPos = 0;
        set_ptrs_symb_idx(ptrsSymbPos,
                          *nbSymb,
                          *startSymbIndex,
                          1<< *L_ptrs,
                          *dmrsSymbPos);
      }
      /* if not PTRS symbol set current ptrs symbol index to zero*/
      *ptrsSymbIdx = 0;
      /* Check if current symbol contains PTRS */
1742
      if(is_ptrs_symbol(symbol, *ptrsSymbPos)) {
1743 1744 1745 1746
        *ptrsSymbIdx = symbol;
        /*------------------------------------------------------------------------------------------------------- */
        /* 1) Estimate common phase error per PTRS symbol                                                                */
        /*------------------------------------------------------------------------------------------------------- */
francescomani's avatar
francescomani committed
1747 1748 1749
        nr_ptrs_cpe_estimation(*K_ptrs,
                               *ptrsReOffset,
                               *nb_rb,
1750
                               rnti,
1751
                               nr_slot_rx,
1752 1753 1754
                               symbol,
                               frame_parms->ofdm_symbol_size,
                               (int16_t *)(rxdataF_comp[0][aarx] + symbol * nb_re_pdsch),
1755
                               ue->nr_gold_pdsch[gNB_id][nr_slot_rx][symbol][nscid],
1756
                               (int16_t*)&phase_per_symbol[symbol],
1757 1758 1759 1760 1761
                               &ptrs_re_symbol[symbol]);
      }
    }// HARQ 0

    /* For last OFDM symbol at each antenna perform interpolation and compensation for the slot*/
1762
    if(symbol == (symbInSlot -1)) {
1763 1764 1765 1766
      /*------------------------------------------------------------------------------------------------------- */
      /* 2) Interpolate PTRS estimated value in TD */
      /*------------------------------------------------------------------------------------------------------- */
      /* If L-PTRS is > 0 then we need interpolation */
1767
      if(*L_ptrs > 0) {
1768
        ret = nr_ptrs_process_slot(*dmrsSymbPos, *ptrsSymbPos, (int16_t*)phase_per_symbol, *startSymbIndex, *nbSymb);
1769
        if(ret != 0) {
1770 1771 1772 1773
          LOG_W(PHY,"[PTRS] Compensation is skipped due to error in PTRS slot processing !!\n");
        }
      }
#ifdef DEBUG_DL_PTRS
1774
      LOG_M("ptrsEst.m","est",ptrs_phase_per_slot[aarx],frame_parms->symbols_per_slot,1,1 );
1775
      LOG_M("rxdataF_bf_ptrs_comp.m", "bf_ptrs_cmp", rxdataF_comp[0][aarx] + (*startSymbIndex) * NR_NB_SC_PER_RB * (*nb_rb), (*nb_rb) * NR_NB_SC_PER_RB * (*nbSymb), 1, 1);
1776 1777 1778 1779
#endif
      /*------------------------------------------------------------------------------------------------------- */
      /* 3) Compensated DMRS based estimated signal with PTRS estimation                                        */
      /*--------------------------------------------------------------------------------------------------------*/
1780
      for(uint8_t i = *startSymbIndex; i< symbInSlot ;i++) {
1781
        /* DMRS Symbol has 0 phase so no need to rotate the respective symbol */
1782
        /* Skip rotation if the slot processing is wrong */
1783
        if((!is_dmrs_symbol(i,*dmrsSymbPos)) && (ret == 0)) {
1784
#ifdef DEBUG_DL_PTRS
1785
          printf("[PHY][DL][PTRS]: Rotate Symbol %2d with  %d + j* %d\n", i, phase_per_symbol[i].r,phase_per_symbol[i].i);
1786
#endif
1787
          rotate_cpx_vector((c16_t *)&rxdataF_comp[0][aarx][(i * (*nb_rb) * NR_NB_SC_PER_RB)],
1788
                            &phase_per_symbol[i],
1789 1790 1791
                            (c16_t *)&rxdataF_comp[0][aarx][(i * (*nb_rb) * NR_NB_SC_PER_RB)],
                            ((*nb_rb) * NR_NB_SC_PER_RB),
                            15);
1792 1793 1794 1795
        }// if not DMRS Symbol
      }// symbol loop
    }// last symbol check
  }//Antenna loop
1796
}//main function