nr_dl_channel_estimation.c 21.8 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
/*
 * 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
 */

knopp's avatar
knopp committed
22

Hongzhi Wang's avatar
Hongzhi Wang committed
23
#include <string.h>
knopp's avatar
knopp committed
24

Hongzhi Wang's avatar
Hongzhi Wang committed
25 26 27
//#include "defs.h"
//#include "SCHED/defs.h"
#include "PHY/defs_nr_UE.h"
knopp's avatar
knopp committed
28
#include "PHY/NR_REFSIG/refsig_defs_ue.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
29 30
#include "filt16a_32.h"
#include "T.h"
hongzhi wang's avatar
hongzhi wang committed
31
//#define DEBUG_CH
Hongzhi Wang's avatar
Hongzhi Wang committed
32

knopp's avatar
knopp committed
33

Hongzhi Wang's avatar
Hongzhi Wang committed
34
int nr_pbch_channel_estimation(PHY_VARS_NR_UE *ue,
knopp's avatar
knopp committed
35 36 37 38 39
			       uint8_t eNB_id,
			       uint8_t eNB_offset,
			       unsigned char Ns,
			       unsigned char l,
			       unsigned char symbol)
Hongzhi Wang's avatar
Hongzhi Wang committed
40
{
knopp's avatar
knopp committed
41
  int pilot[200] __attribute__((aligned(16)));
hongzhi wang's avatar
hongzhi wang committed
42
  unsigned char aarx;
Hongzhi Wang's avatar
Hongzhi Wang committed
43
  unsigned short k;
hongzhi wang's avatar
hongzhi wang committed
44
  unsigned int pilot_cnt;
hongzhi wang's avatar
hongzhi wang committed
45
  int16_t ch[2],*pil,*rxF,*dl_ch,*fl,*fm,*fr;
Hongzhi Wang's avatar
Hongzhi Wang committed
46
  int ch_offset,symbol_offset;
knopp's avatar
knopp committed
47
  int dmrss;
Hongzhi Wang's avatar
Hongzhi Wang committed
48

hongzhi wang's avatar
hongzhi wang committed
49
  //uint16_t Nid_cell = (eNB_offset == 0) ? ue->frame_parms.Nid_cell : ue->measurements.adj_cell_id[eNB_offset-1];
Hongzhi Wang's avatar
Hongzhi Wang committed
50

knopp's avatar
knopp committed
51
  uint8_t nushift,ssb_index=0, n_hf=0;
hongzhi wang's avatar
hongzhi wang committed
52
  int **dl_ch_estimates  =ue->common_vars.common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].dl_ch_estimates[eNB_offset];
Hongzhi Wang's avatar
Hongzhi Wang committed
53 54
  int **rxdataF=ue->common_vars.common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].rxdataF;

hongzhi wang's avatar
hongzhi wang committed
55
  nushift =  ue->frame_parms.Nid_cell%4;
hongzhi wang's avatar
hongzhi wang committed
56
  ue->frame_parms.nushift = nushift;
knopp's avatar
knopp committed
57 58 59 60 61 62 63

  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;

  if (ue->is_synchronized ==0 ) dmrss = symbol-1;
  else dmrss = symbol-5;

Hongzhi Wang's avatar
Hongzhi Wang committed
64 65 66 67 68
  if (ue->high_speed_flag == 0) // use second channel estimate position for temporary storage
    ch_offset     = ue->frame_parms.ofdm_symbol_size ;
  else
    ch_offset     = ue->frame_parms.ofdm_symbol_size*symbol;

knopp's avatar
knopp committed
69
  AssertFatal((symbol > 0 && symbol < 4 && ue->is_synchronized == 0) || 
70
	      (symbol > 4 && symbol < 8 && ue->is_synchronized == 1),
knopp's avatar
knopp committed
71 72 73
	      "symbol %d is illegal for PBCH DM-RS (is_synchronized %d)\n",
	      symbol,ue->is_synchronized);

Hongzhi Wang's avatar
Hongzhi Wang committed
74 75
  symbol_offset = ue->frame_parms.ofdm_symbol_size*symbol;

knopp's avatar
knopp committed
76

Hongzhi Wang's avatar
Hongzhi Wang committed
77 78 79
  k = nushift;

#ifdef DEBUG_CH
knopp's avatar
knopp committed
80
  printf("PBCH Channel Estimation : ThreadId %d, eNB_offset %d ch_offset %d, OFDM size %d, Ncp=%d, l=%d, Ns=%d, k=%d symbol %d\n",ue->current_thread_id[Ns>>1], eNB_offset,ch_offset,ue->frame_parms.ofdm_symbol_size,
hongzhi wang's avatar
hongzhi wang committed
81
         ue->frame_parms.Ncp,l,Ns,k, symbol);
Hongzhi Wang's avatar
Hongzhi Wang committed
82 83 84 85
#endif

  switch (k) {
  case 0:
knopp's avatar
knopp committed
86 87 88 89
    fl = filt16a_l0;
    fm = filt16a_m0;
    fr = filt16a_r0;
    break;
Hongzhi Wang's avatar
Hongzhi Wang committed
90 91

  case 1:
knopp's avatar
knopp committed
92 93 94 95
    fl = filt16a_l1;
    fm = filt16a_m1;
    fr = filt16a_r1;
    break;
Hongzhi Wang's avatar
Hongzhi Wang committed
96 97

  case 2:
knopp's avatar
knopp committed
98 99 100 101
    fl = filt16a_l2;
    fm = filt16a_m2;
    fr = filt16a_r2;
    break;
Hongzhi Wang's avatar
Hongzhi Wang committed
102 103

  case 3:
knopp's avatar
knopp committed
104 105 106 107
    fl = filt16a_l3;
    fm = filt16a_m3;
    fr = filt16a_r3;
    break;
Hongzhi Wang's avatar
Hongzhi Wang committed
108 109 110 111 112 113 114 115

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

  // generate pilot
knopp's avatar
knopp committed
116
  nr_pbch_dmrs_rx(dmrss,ue->nr_gold_pbch[n_hf][ssb_index], &pilot[0]);
Hongzhi Wang's avatar
Hongzhi Wang committed
117

knopp's avatar
knopp committed
118
  int re_offset = ssb_offset;
Hongzhi Wang's avatar
Hongzhi Wang committed
119 120
  for (aarx=0; aarx<ue->frame_parms.nb_antennas_rx; aarx++) {

knopp's avatar
knopp committed
121 122 123 124
    pil   = (int16_t *)&pilot[0];
    rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+re_offset)];
    dl_ch = (int16_t *)&dl_ch_estimates[aarx][ch_offset];
 
Hongzhi Wang's avatar
Hongzhi Wang committed
125 126 127 128 129
    memset(dl_ch,0,4*(ue->frame_parms.ofdm_symbol_size));
    if (ue->high_speed_flag==0) // multiply previous channel estimate by ch_est_alpha
      multadd_complex_vector_real_scalar(dl_ch-(ue->frame_parms.ofdm_symbol_size<<1),
                                         ue->ch_est_alpha,dl_ch-(ue->frame_parms.ofdm_symbol_size<<1),
                                         1,ue->frame_parms.ofdm_symbol_size);
130
#ifdef DEBUG_CH
knopp's avatar
knopp committed
131
    printf("pbch ch est pilot addr %p RB_DL %d\n",&pilot[0], ue->frame_parms.N_RB_DL);
Hongzhi Wang's avatar
Hongzhi Wang committed
132
    printf("k %d, first_carrier %d\n",k,ue->frame_parms.first_carrier_offset);
hongzhi wang's avatar
hongzhi wang committed
133 134
    printf("rxF addr %p\n", rxF);
    printf("dl_ch addr %p\n",dl_ch);
135
#endif
hongzhi wang's avatar
hongzhi wang committed
136
    //if ((ue->frame_parms.N_RB_DL&1)==0) {
Hongzhi Wang's avatar
Hongzhi Wang committed
137

knopp's avatar
knopp committed
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    // 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);
#ifdef DEBUG_CH
    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]);
#endif
    multadd_real_vector_complex_scalar(fl,
				       ch,
				       dl_ch,
				       16);
    pil+=2;
    re_offset = (re_offset+4)&(ue->frame_parms.ofdm_symbol_size-1);
    rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+re_offset)];

    //for (int i= 0; i<8; i++)
    //printf("dl_ch addr %p %d\n", dl_ch+i, *(dl_ch+i));

    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);
#ifdef DEBUG_CH
    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]);
#endif
    multadd_real_vector_complex_scalar(fm,
				       ch,
				       dl_ch,
				       16);
    pil+=2;
    re_offset = (re_offset+4)&(ue->frame_parms.ofdm_symbol_size-1);
    rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+re_offset)];

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

#ifdef DEBUG_CH
    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]);
#endif

    multadd_real_vector_complex_scalar(fr,
				       ch,
				       dl_ch,
				       16);
    pil+=2;
    re_offset = (re_offset+4)&(ue->frame_parms.ofdm_symbol_size-1);
    rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+re_offset)];
    dl_ch+=24;

    for (pilot_cnt=3; pilot_cnt<(3*20); pilot_cnt+=3) {

      //	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) {
	pilot_cnt=48;
	re_offset = (re_offset+144)&(ue->frame_parms.ofdm_symbol_size-1);
	rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+re_offset)];
	dl_ch += 288;
      }
Hongzhi Wang's avatar
Hongzhi Wang committed
197 198
      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);
199
#ifdef DEBUG_CH
knopp's avatar
knopp committed
200
      printf("pilot %d : rxF - > (%d,%d) ch -> (%d,%d), pil -> (%d,%d) \n",pilot_cnt,rxF[0],rxF[1],ch[0],ch[1],pil[0],pil[1]);
201
#endif
Hongzhi Wang's avatar
Hongzhi Wang committed
202
      multadd_real_vector_complex_scalar(fl,
knopp's avatar
knopp committed
203 204 205 206
					 ch,
					 dl_ch,
					 16);

hongzhi wang's avatar
hongzhi wang committed
207
      //for (int i= 0; i<8; i++)
knopp's avatar
knopp committed
208
      //            printf("pilot_cnt %d dl_ch %d %d\n", pilot_cnt, dl_ch+i, *(dl_ch+i));
Hongzhi Wang's avatar
Hongzhi Wang committed
209

knopp's avatar
knopp committed
210 211 212 213 214
      pil+=2;
      re_offset = (re_offset+4)&(ue->frame_parms.ofdm_symbol_size-1);
      rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+re_offset)];
        
  
Hongzhi Wang's avatar
Hongzhi Wang committed
215 216 217
      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);
#ifdef DEBUG_CH
knopp's avatar
knopp committed
218
      printf("pilot %d : 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
219 220
#endif
      multadd_real_vector_complex_scalar(fm,
knopp's avatar
knopp committed
221 222 223
					 ch,
					 dl_ch,
					 16);
Hongzhi Wang's avatar
Hongzhi Wang committed
224
      pil+=2;
knopp's avatar
knopp committed
225 226 227
      re_offset = (re_offset+4)&(ue->frame_parms.ofdm_symbol_size-1);
      rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+re_offset)];
        
Hongzhi Wang's avatar
Hongzhi Wang committed
228 229 230 231

      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
232
#ifdef DEBUG_CH
knopp's avatar
knopp committed
233
      printf("pilot %d : 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
234
#endif
Hongzhi Wang's avatar
Hongzhi Wang committed
235 236

      multadd_real_vector_complex_scalar(fr,
knopp's avatar
knopp committed
237 238 239
					 ch,
					 dl_ch,
					 16);
Hongzhi Wang's avatar
Hongzhi Wang committed
240
      pil+=2;
knopp's avatar
knopp committed
241 242
      re_offset = (re_offset+4)&(ue->frame_parms.ofdm_symbol_size-1);
      rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+re_offset)];
hongzhi wang's avatar
hongzhi wang committed
243
      dl_ch+=24;
Hongzhi Wang's avatar
Hongzhi Wang committed
244

knopp's avatar
knopp committed
245
    }
Hongzhi Wang's avatar
Hongzhi Wang committed
246 247


hongzhi wang's avatar
hongzhi wang committed
248
    //}
Hongzhi Wang's avatar
Hongzhi Wang committed
249 250 251 252 253 254

  }

  return(0);
}

255
int nr_pdcch_channel_estimation(PHY_VARS_NR_UE *ue,
knopp's avatar
knopp committed
256 257 258 259 260 261 262
				uint8_t eNB_id,
				uint8_t eNB_offset,
				unsigned char Ns,
				unsigned char l,
				unsigned char symbol,
				unsigned short coreset_start_subcarrier,
				unsigned short nb_rb_coreset)
263
{
knopp's avatar
knopp committed
264
  int pilot[200] __attribute__((aligned(16)));
265 266 267 268 269 270 271 272 273
  unsigned char aarx;
  unsigned short k;
  unsigned int pilot_cnt;
  int16_t ch[2],*pil,*rxF,*dl_ch,*fl,*fm,*fr;
  int ch_offset,symbol_offset;

  //uint16_t Nid_cell = (eNB_offset == 0) ? ue->frame_parms.Nid_cell : ue->measurements.adj_cell_id[eNB_offset-1];

  uint8_t nushift;
274 275
  int **dl_ch_estimates  =ue->common_vars.common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].dl_ch_estimates[eNB_offset];
  int **rxdataF=ue->common_vars.common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].rxdataF;
276 277 278 279 280 281 282 283 284 285 286

  nushift = 1;
  ue->frame_parms.nushift = nushift;

  if (ue->high_speed_flag == 0) // use second channel estimate position for temporary storage
    ch_offset     = ue->frame_parms.ofdm_symbol_size ;
  else
    ch_offset     = ue->frame_parms.ofdm_symbol_size*symbol;

  symbol_offset = ue->frame_parms.ofdm_symbol_size*symbol;

287
  k = coreset_start_subcarrier;
288 289

#ifdef DEBUG_CH
290
  printf("PDCCH Channel Estimation : ThreadId %d, eNB_offset %d ch_offset %d, OFDM size %d, Ncp=%d, l=%d, Ns=%d, k=%d symbol %d\n",ue->current_thread_id[Ns>>1], eNB_offset,ch_offset,ue->frame_parms.ofdm_symbol_size,
291 292 293 294 295 296 297 298
         ue->frame_parms.Ncp,l,Ns,k, symbol);
#endif

  fl = filt16a_l1;
  fm = filt16a_m1;
  fr = filt16a_r1;

  // generate pilot
knopp's avatar
knopp committed
299
  nr_pdcch_dmrs_rx(ue,eNB_offset,Ns,ue->nr_gold_pdcch[eNB_offset][Ns][symbol], &pilot[0],2000,nb_rb_coreset);
300 301 302

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

knopp's avatar
knopp committed
303
    pil   = (int16_t *)&pilot[0];
304
    rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+nushift)];
knopp's avatar
knopp committed
305
    dl_ch = (int16_t *)&dl_ch_estimates[aarx][ch_offset];
306 307 308 309 310 311

    memset(dl_ch,0,4*(ue->frame_parms.ofdm_symbol_size));
    if (ue->high_speed_flag==0) // multiply previous channel estimate by ch_est_alpha
      multadd_complex_vector_real_scalar(dl_ch-(ue->frame_parms.ofdm_symbol_size<<1),
                                         ue->ch_est_alpha,dl_ch-(ue->frame_parms.ofdm_symbol_size<<1),
                                         1,ue->frame_parms.ofdm_symbol_size);
knopp's avatar
knopp committed
312 313
    //#ifdef DEBUG_CH
    printf("pdcch ch est pilot addr %p RB_DL %d\n",&pilot[0], ue->frame_parms.N_RB_DL);
314 315 316
    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);
knopp's avatar
knopp committed
317
    //#endif
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    if ((ue->frame_parms.N_RB_DL&1)==0) {

      // 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);
#ifdef DEBUG_CH
      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]);
#endif
      multadd_real_vector_complex_scalar(fl,
                                         ch,
                                         dl_ch,
                                         16);
      pil+=2;
      rxF+=8;
      //for (int i= 0; i<8; i++)
      //printf("dl_ch addr %p %d\n", dl_ch+i, *(dl_ch+i));

      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);
#ifdef DEBUG_CH
      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]);
#endif
      multadd_real_vector_complex_scalar(fm,
                                         ch,
                                         dl_ch,
                                         16);
      pil+=2;
      rxF+=8;

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

#ifdef DEBUG_CH
      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]);
#endif

      multadd_real_vector_complex_scalar(fr,
                                         ch,
                                         dl_ch,
                                         16);
hongzhi wang's avatar
hongzhi wang committed
359 360 361
                                         
#ifdef DEBUG_CH       
      for (int m =0; m<12; m++)
knopp's avatar
knopp committed
362
	printf("data :  dl_ch -> (%d,%d)\n",dl_ch[0+2*m],dl_ch[1+2*m]);
hongzhi wang's avatar
hongzhi wang committed
363
#endif      
364 365 366
      pil+=2;
      rxF+=8;
      dl_ch+=24;
367
      k+=12;
hongzhi wang's avatar
hongzhi wang committed
368 369
      
      
370

371
      for (pilot_cnt=3; pilot_cnt<(3*nb_rb_coreset); pilot_cnt+=3) {
372

373
        if (k >= ue->frame_parms.ofdm_symbol_size){
knopp's avatar
knopp committed
374 375
	  k-=ue->frame_parms.ofdm_symbol_size;
	  rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+nushift)];}
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

        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);
#ifdef DEBUG_CH
	printf("pilot %d : rxF - > (%d,%d) ch -> (%d,%d), pil -> (%d,%d) \n",pilot_cnt,rxF[0],rxF[1],ch[0],ch[1],pil[0],pil[1]);
#endif
        multadd_real_vector_complex_scalar(fl,
                                           ch,
                                           dl_ch,
                                           16);

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

        pil+=2;
        rxF+=8;

Hongzhi Wang's avatar
Hongzhi Wang committed
393 394 395
        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);
#ifdef DEBUG_CH
hongzhi wang's avatar
hongzhi wang committed
396
	printf("pilot %d : 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
397 398 399 400 401 402 403 404 405 406 407
#endif
        multadd_real_vector_complex_scalar(fm,
                                           ch,
                                           dl_ch,
                                           16);
        pil+=2;
        rxF+=8;

        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
408 409 410
#ifdef DEBUG_CH
	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]);
#endif
Hongzhi Wang's avatar
Hongzhi Wang committed
411 412 413 414 415 416 417

        multadd_real_vector_complex_scalar(fr,
                                           ch,
                                           dl_ch,
                                           16);
        pil+=2;
        rxF+=8;
hongzhi wang's avatar
hongzhi wang committed
418
        dl_ch+=24;
419
        k+=12;
Hongzhi Wang's avatar
Hongzhi Wang committed
420 421 422 423 424 425 426 427 428 429 430

      }


    }

  }

  return(0);
}

431
int nr_pdsch_channel_estimation(PHY_VARS_NR_UE *ue,
knopp's avatar
knopp committed
432 433 434 435 436 437 438
				uint8_t eNB_id,
				uint8_t eNB_offset,
				unsigned char Ns,
				unsigned char l,
				unsigned char symbol,
				unsigned short bwp_start_subcarrier,
				unsigned short nb_rb_pdsch)
439
{
knopp's avatar
knopp committed
440
  int pilot[200] __attribute__((aligned(16)));
441 442 443 444 445 446
  unsigned char aarx;
  unsigned short k;
  unsigned int pilot_cnt;
  int16_t ch[2],*pil,*rxF,*dl_ch,*fl,*fm,*fr,*fml,*fmr;
  int ch_offset,symbol_offset;

knopp's avatar
knopp committed
447

448 449
  //uint16_t Nid_cell = (eNB_offset == 0) ? ue->frame_parms.Nid_cell : ue->measurements.adj_cell_id[eNB_offset-1];

knopp's avatar
knopp committed
450
  uint8_t nushift=0;
451 452 453 454 455 456 457 458 459 460 461 462 463 464
  int **dl_ch_estimates  =ue->common_vars.common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].dl_ch_estimates[eNB_offset];
  int **rxdataF=ue->common_vars.common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].rxdataF;


  if (ue->high_speed_flag == 0) // use second channel estimate position for temporary storage
    ch_offset     = ue->frame_parms.ofdm_symbol_size ;
  else
    ch_offset     = ue->frame_parms.ofdm_symbol_size*symbol;

  symbol_offset = ue->frame_parms.ofdm_symbol_size*symbol;

  k = bwp_start_subcarrier;

#ifdef DEBUG_CH
knopp's avatar
knopp committed
465
  printf("PBCH Channel Estimation : ThreadId %d, eNB_offset %d ch_offset %d, OFDM size %d, Ncp=%d, l=%d, Ns=%d, k=%d symbol %d\n",ue->current_thread_id[Ns>>1], eNB_offset,ch_offset,ue->frame_parms.ofdm_symbol_size,
466 467 468 469
         ue->frame_parms.Ncp,l,Ns,k, symbol);
#endif

  switch (nushift) {
knopp's avatar
knopp committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
  case 0:
    fl = filt8_l0;
    fm = filt8_m0;
    fr = filt8_r0;
    fml = filt8_m0;
    fmr = filt8_mr0;
    break;

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

  default:
    msg("pdsch_channel_estimation: nushift=%d -> ERROR\n",nushift);
    return(-1);
    break;
  }
491 492 493


  // generate pilot
knopp's avatar
knopp committed
494
  nr_pdsch_dmrs_rx(ue,eNB_offset,Ns,ue->nr_gold_pdsch[eNB_offset][Ns][symbol], &pilot[0],1000,1,nb_rb_pdsch);
495 496 497

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

knopp's avatar
knopp committed
498
    pil   = (int16_t *)&pilot[0];
499
    rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+nushift)];
knopp's avatar
knopp committed
500
    dl_ch = (int16_t *)&dl_ch_estimates[aarx][ch_offset];
501 502 503 504 505 506 507

    memset(dl_ch,0,4*(ue->frame_parms.ofdm_symbol_size));
    if (ue->high_speed_flag==0) // multiply previous channel estimate by ch_est_alpha
      multadd_complex_vector_real_scalar(dl_ch-(ue->frame_parms.ofdm_symbol_size<<1),
                                         ue->ch_est_alpha,dl_ch-(ue->frame_parms.ofdm_symbol_size<<1),
                                         1,ue->frame_parms.ofdm_symbol_size);
#ifdef DEBUG_CH
knopp's avatar
knopp committed
508
    printf("ch est pilot addr %p RB_DL %d\n",&pilot[0], ue->frame_parms.N_RB_DL);
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
    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);
#endif
    if ((ue->frame_parms.N_RB_DL&1)==0) {

      // 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);
#ifdef DEBUG_CH
      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]);
#endif
      multadd_real_vector_complex_scalar(fl,
                                         ch,
                                         dl_ch,
                                         8);
      pil+=2;
      rxF+=4;
      //for (int i= 0; i<8; i++)
      //printf("dl_ch addr %p %d\n", dl_ch+i, *(dl_ch+i));

      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);
#ifdef DEBUG_CH
      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]);
#endif
      multadd_real_vector_complex_scalar(fml,
                                         ch,
                                         dl_ch,
                                         8);
      pil+=2;
      rxF+=4;
      dl_ch+=4;
      k+=4;

      for (pilot_cnt=2; pilot_cnt<(6*(nb_rb_pdsch-1)+4); pilot_cnt+=2) {
    	if ((pilot_cnt%6)==0)
knopp's avatar
knopp committed
547
	  dl_ch+=4;
548 549

        if (k >= ue->frame_parms.ofdm_symbol_size){
knopp's avatar
knopp committed
550 551
	  k-=ue->frame_parms.ofdm_symbol_size;
	  rxF   = (int16_t *)&rxdataF[aarx][(symbol_offset+k+nushift)];}
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 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

        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);
#ifdef DEBUG_CH
	printf("pilot %d : rxF - > (%d,%d) ch -> (%d,%d), pil -> (%d,%d) \n",pilot_cnt,rxF[0],rxF[1],ch[0],ch[1],pil[0],pil[1]);
#endif
        multadd_real_vector_complex_scalar(fm,
                                           ch,
                                           dl_ch,
                                           8);

        pil+=2;
        rxF+=4;
        dl_ch+=4;

        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);
#ifdef DEBUG_CH
	printf("pilot %d : 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]);
#endif
        multadd_real_vector_complex_scalar(fm,
                                           ch,
                                           dl_ch,
                                           8);
        pil+=2;
        rxF+=4;
        dl_ch+=4;
        k+=4;

      }

      // Treat first 2 pilots specially (right 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);
#ifdef DEBUG_CH
      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]);
#endif
      multadd_real_vector_complex_scalar(fmr,
                                         ch,
                                         dl_ch,
                                         8);
      pil+=2;
      rxF+=4;
      //for (int i= 0; i<8; i++)
      //printf("dl_ch addr %p %d\n", dl_ch+i, *(dl_ch+i));

      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);
#ifdef DEBUG_CH
      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]);
#endif
      multadd_real_vector_complex_scalar(fr,
                                         ch,
                                         dl_ch,
                                         8);
      pil+=2;
      rxF+=4;
      dl_ch+=4;
      k+=4;

    }

  }

  return(0);
}