lte_dl_bf_channel_estimation.c 30.9 KB
Newer Older
Cedric Roux's avatar
Cedric Roux committed
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
Cedric Roux's avatar
Cedric Roux committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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
 */
21 22

#include <string.h>
23
#include "PHY/defs_UE.h"
24
#include "filt16_32.h"
25
//#define DEBUG_BF_CH
26 27 28 29 30 31 32 33 34

int lte_dl_bf_channel_estimation(PHY_VARS_UE *phy_vars_ue,
                                 uint8_t eNB_id,
                                 uint8_t eNB_offset,
                                 unsigned char Ns,
                                 unsigned char p,
                                 unsigned char symbol)
{
  
35 36 37 38
  unsigned short rb,nb_rb=0;
  unsigned char aarx,l,lprime,nsymb,skip_half=0,sss_symb,pss_symb=0,rb_alloc_ind,harq_pid,uespec_pilots=0;
  int beamforming_mode, ch_offset;
  uint8_t subframe;
Xiwen JIANG's avatar
Xiwen JIANG committed
39
  int8_t uespec_nushift, uespec_poffset=0, pil_offset=0;
40
  uint8_t pilot0,pilot1,pilot2,pilot3;
41

42 43
  short ch[2], *pil, *rxF, *dl_bf_ch, *dl_bf_ch_prev;
  short *fl, *fm, *fr, *fl_dc, *fm_dc, *fr_dc, *f1, *f2l, *f2r;
44

45
  unsigned int *rballoc; 
46 47 48
  int **rxdataF;
  int32_t **dl_bf_ch_estimates;
  int uespec_pilot[300];
49

50
  LTE_DL_FRAME_PARMS *frame_parms = &phy_vars_ue->frame_parms;
51
  LTE_UE_DLSCH_t **dlsch_ue       = phy_vars_ue->dlsch[phy_vars_ue->current_thread_id[Ns>>1]][eNB_id];
52
  LTE_DL_UE_HARQ_t *dlsch0_harq; 
53

Bilel's avatar
Bilel committed
54
  harq_pid    = dlsch_ue[0]->current_harq_pid;
55 56
  dlsch0_harq = dlsch_ue[0]->harq_processes[harq_pid];

57 58 59 60 61
  if (((frame_parms->Ncp == NORMAL) && (symbol>=7)) ||
      ((frame_parms->Ncp == EXTENDED) && (symbol>=6)))
    rballoc = dlsch0_harq->rb_alloc_odd;
  else
    rballoc = dlsch0_harq->rb_alloc_even;
62

63
  rxdataF = phy_vars_ue->common_vars.common_vars_rx_data_per_thread[phy_vars_ue->current_thread_id[Ns>>1]].rxdataF;
64

65
  dl_bf_ch_estimates = phy_vars_ue->pdsch_vars[phy_vars_ue->current_thread_id[Ns>>1]][eNB_id]->dl_bf_ch_estimates;
66 67 68 69
  beamforming_mode   = phy_vars_ue->transmission_mode[eNB_id]>6 ? phy_vars_ue->transmission_mode[eNB_id] : 0;

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

73
  
74 75 76
  uespec_nushift = frame_parms->Nid_cell%3;
  subframe = Ns>>1;
 
77

78 79 80
    //generate ue specific pilots
    lprime = symbol/3-1;
    lte_dl_ue_spec_rx(phy_vars_ue,uespec_pilot,Ns,5,lprime,0,dlsch0_harq->nb_rb);
81
    //write_output("uespec_pilot_rx.m","uespec_pilot",uespec_pilot,300,1,1);
82 83 84 85 86 87 88 89

    if (frame_parms->Ncp==0){
      if (symbol==3 || symbol==6 || symbol==9 || symbol==12)
        uespec_pilots = 1;
    } else{
      if (symbol==4 || symbol==7 || symbol==10)
        uespec_pilots = 1;
    }
90
   
91 92 93
    if ((frame_parms->Ncp==0 && (symbol==6 ||symbol ==12)) || (frame_parms->Ncp==1 && symbol==7))
      uespec_poffset = 2;

94
    if (phy_vars_ue->frame_parms.Ncp == 0) { // normal prefix
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
      pilot0 = 3;
      pilot1 = 6;
      pilot2 = 9;
      pilot3 = 12;
    } else { // extended prefix
      pilot0 = 4;
      pilot1 = 7;
      pilot2 = 10;
    }

    //define the filter
    pil_offset = (uespec_nushift+uespec_poffset)%3;
    // printf("symbol=%d,pil_offset=%d\n",symbol,pil_offset);
    switch (pil_offset) {
    case 0:
      fl = filt16_l0;
      fm = filt16_m0;
      fr = filt16_r0;
      fl_dc = filt16_l0;
      fm_dc = filt16_m0;
      fr_dc = filt16_r0;
      f1 = filt16_1;
      f2l = filt16_2l0;
      f2r = filt16_2r0;
      break;

    case 1:
      fl = filt16_l1;
      fm = filt16_m1;
      fr = filt16_r1;
      fl_dc = filt16_l1;
      fm_dc = filt16_m1;
      fr_dc = filt16_r1;
      f1 = filt16_1;
      f2l = filt16_2l1;
      f2r = filt16_2r1;
      break;
132

133 134 135 136 137 138 139 140 141 142 143
    case 2:
      fl = filt16_l2;
      fm = filt16_m2;
      fr = filt16_r2;
      fl_dc = filt16_l2;
      fm_dc = filt16_m2;
      fr_dc = filt16_r2;
      f1 = filt16_1;
      f2l = filt16_2l0;
      f2r = filt16_2r0;
      break;
144

145 146 147 148 149 150 151 152 153 154 155
    case 3:
      fl = filt16_l3;
      fm = filt16_m3;
      fr = filt16_r3;
      fl_dc = filt16_l3;
      fm_dc = filt16_m3;
      fr_dc = filt16_r3;
      f1 = filt16_1;
      f2l = filt16_2l1;
      f2r = filt16_2r1;
      break;
156
    }
Xiwen JIANG's avatar
Xiwen JIANG committed
157 158
 // beamforming mode extension
 /* }
159 160 161
  else if (beamforming_mode==0)
    msg("lte_dl_bf_channel_estimation:No beamforming is performed.\n");
  else
Xiwen JIANG's avatar
Xiwen JIANG committed
162
    msg("lte_dl_bf_channel_estimation:Beamforming mode not supported yet.\n");*/
163
  
164

165 166 167 168 169 170
  l=symbol;
  nsymb = (frame_parms->Ncp==NORMAL) ? 14:12;

  if (frame_parms->frame_type == TDD) {  //TDD
    sss_symb = nsymb-1;
    pss_symb = 2;
171
  } else {
172 173
    sss_symb = (nsymb>>1)-2;
    pss_symb = (nsymb>>1)-1;
174
  }
175 176 177 178 179


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

    rxF  = (short *)&rxdataF[aarx][pil_offset + frame_parms->first_carrier_offset + symbol*frame_parms->ofdm_symbol_size];
Xiwen JIANG's avatar
Xiwen JIANG committed
180
    pil  = (short *)uespec_pilot;
181 182 183 184 185
    dl_bf_ch = (short *)&dl_bf_ch_estimates[aarx][ch_offset];

    memset(dl_bf_ch,0,4*(frame_parms->ofdm_symbol_size));
    //memset(dl_bf_ch,0,2*(frame_parms->ofdm_symbol_size));

Xiwen JIANG's avatar
Xiwen JIANG committed
186 187 188
    if (phy_vars_ue->high_speed_flag==0) {
    // multiply previous channel estimate by ch_est_alpha
      if (frame_parms->Ncp==0){
189 190 191
        multadd_complex_vector_real_scalar(dl_bf_ch-(frame_parms->ofdm_symbol_size<<1),
                                           phy_vars_ue->ch_est_alpha,dl_bf_ch-(frame_parms->ofdm_symbol_size<<1),
                                           1,frame_parms->ofdm_symbol_size);
Xiwen JIANG's avatar
Xiwen JIANG committed
192
      } else {
193
        LOG_E(PHY,"lte_dl_bf_channel_estimation: beamforming channel estimation not supported for TM7 Extended CP.\n"); // phy_vars_ue->ch_est_beta should be defined equaling 1/3
Xiwen JIANG's avatar
Xiwen JIANG committed
194 195
      }
    }
196 197
    //estimation and interpolation

198
    if ((frame_parms->N_RB_DL&1) == 0) { // even number of RBs
199 200 201
      for (rb=0; rb<frame_parms->N_RB_DL; rb++) {

        if (rb < 32)
202
          rb_alloc_ind = (rballoc[0]>>rb) & 1;
203
        else if (rb < 64)
204
          rb_alloc_ind = (rballoc[1]>>(rb-32)) & 1;
205
        else if (rb < 96)
206
          rb_alloc_ind = (rballoc[2]>>(rb-64)) & 1;
207
        else if (rb < 100)
208
          rb_alloc_ind = (rballoc[3]>>(rb-96)) & 1;
209 210 211 212 213
        else
          rb_alloc_ind = 0;

        // For second half of RBs skip DC carrier
        if (rb==(frame_parms->N_RB_DL>>1)) {
Xiwen JIANG's avatar
Xiwen JIANG committed
214
          rxF       = (short *)&rxdataF[aarx][(1 + (symbol*(frame_parms->ofdm_symbol_size)))];
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        }

        if (rb_alloc_ind==1) {
          if (uespec_pilots==1) {
            if (beamforming_mode==7) {
              if (frame_parms->Ncp==0) {

                ch[0] = (short)(((int)pil[0]*rxF[0] - (int)pil[1]*rxF[1])>>15);
                ch[1] = (short)(((int)pil[0]*rxF[1] + (int)pil[1]*rxF[0])>>15);
                multadd_real_vector_complex_scalar(fl,ch,dl_bf_ch,16);
                
                ch[0] = (short)(((int)pil[0]*rxF[8] - (int)pil[1]*rxF[9])>>15);
                ch[1] = (short)(((int)pil[0]*rxF[9] + (int)pil[1]*rxF[8])>>15);
                multadd_real_vector_complex_scalar(fm,ch,dl_bf_ch,16);

                ch[0] = (short)(((int)pil[0]*rxF[16] - (int)pil[1]*rxF[17])>>15);
                ch[1] = (short)(((int)pil[0]*rxF[17] + (int)pil[1]*rxF[16])>>15);
                multadd_real_vector_complex_scalar(fr,ch,dl_bf_ch,16);
233
              } else {
234
                LOG_E(PHY,"lte_dl_bf_channel_estimation(lte_dl_bf_channel_estimation.c):TM7 beamgforming channel estimation not supported for extented CP\n");
235
                exit(-1);
236 237
              }
            } else {
238
              LOG_E(PHY,"lte_dl_bf_channel_estimation(lte_dl_bf_channel_estimation.c): transmission mode not supported.\n");
239 240 241 242 243 244 245 246
            }
          }
          nb_rb++;
        }

        rxF+=24;
        dl_bf_ch+=24;
      }
247
    } else {  // Odd number of RBs
248 249 250 251
      for (rb=0; rb<frame_parms->N_RB_DL>>1; rb++) {
        skip_half=0;

        if (rb < 32)
252
          rb_alloc_ind = (rballoc[0]>>rb) & 1;
253
        else if (rb < 64)
254
          rb_alloc_ind = (rballoc[1]>>(rb-32)) & 1;
255
        else if (rb < 96)
256
          rb_alloc_ind = (rballoc[2]>>(rb-64)) & 1;
257
        else if (rb < 100)
258
          rb_alloc_ind = (rballoc[3]>>(rb-96)) & 1;
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        else
          rb_alloc_ind = 0;

        // PBCH
        if ((subframe==0) && (rb>((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l>=(nsymb>>1)) && (l<((nsymb>>1) + 4))) {
          rb_alloc_ind = 0;
        }

        //PBCH subframe 0, symbols nsymb>>1 ... nsymb>>1 + 3
        if ((subframe==0) && (rb==((frame_parms->N_RB_DL>>1)-3)) && (l>=(nsymb>>1)) && (l<((nsymb>>1) + 4)))
          skip_half=1;
        else if ((subframe==0) && (rb==((frame_parms->N_RB_DL>>1)+3)) && (l>=(nsymb>>1)) && (l<((nsymb>>1) + 4)))
          skip_half=2;

        //SSS
        if (((subframe==0)||(subframe==5)) &&
            (rb>((frame_parms->N_RB_DL>>1)-3)) &&
            (rb<((frame_parms->N_RB_DL>>1)+3)) &&
            (l==sss_symb) ) {
          rb_alloc_ind = 0;
        }

        //SSS
        if (((subframe==0)||(subframe==5)) &&
            (rb==((frame_parms->N_RB_DL>>1)-3)) &&
            (l==sss_symb))
          skip_half=1;
        else if (((subframe==0)||(subframe==5)) &&
                 (rb==((frame_parms->N_RB_DL>>1)+3)) &&
                 (l==sss_symb))
          skip_half=2;

        //PSS in subframe 0/5 if FDD
        if (frame_parms->frame_type == FDD) {  //FDD
          if (((subframe==0)||(subframe==5)) && (rb>((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb) ) {
            rb_alloc_ind = 0;
          }

          if (((subframe==0)||(subframe==5)) && (rb==((frame_parms->N_RB_DL>>1)-3)) && (l==pss_symb))
            skip_half=1;
          else if (((subframe==0)||(subframe==5)) && (rb==((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb))
            skip_half=2;
        }

303
        if ((frame_parms->frame_type == TDD) && ((subframe==1)||(subframe==6))) { //TDD Subframe 1 and 6
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
          if ((rb>((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb) ) {
            rb_alloc_ind = 0;
          }

          if ((rb==((frame_parms->N_RB_DL>>1)-3)) && (l==pss_symb))
            skip_half=1;
          else if ((rb==((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb))
            skip_half=2;
        }

        //printf("symbol=%d,pil_offset=%d\ni,rb_alloc_ind=%d,uespec_pilots=%d,beamforming_mode=%d,Ncp=%d,skip_half=%d\n",symbol,pil_offset,rb_alloc_ind,uespec_pilots,beamforming_mode,frame_parms->Ncp,skip_half);
        if (rb_alloc_ind==1) {
          if (uespec_pilots==1) {
            if (beamforming_mode==7) {
              if (frame_parms->Ncp==0) {
                if (skip_half==1) {
                  if (pil_offset<2) {

                    ch[0] = (short)(((int)pil[0]*rxF[0] - (int)pil[1]*rxF[1])>>15);
                    ch[1] = (short)(((int)pil[0]*rxF[1] + (int)pil[1]*rxF[0])>>15);
                    multadd_real_vector_complex_scalar(f2l,ch,dl_bf_ch,16); 
                    pil+=2;
                    
                    ch[0] = (short)(((int)pil[0]*rxF[8] - (int)pil[1]*rxF[9])>>15);
                    ch[1] = (short)(((int)pil[0]*rxF[9] + (int)pil[1]*rxF[8])>>15);
                    multadd_real_vector_complex_scalar(f2r,ch,dl_bf_ch,16); 
                    pil+=2;

                  } else {

                    ch[0] = (short)(((int)pil[0]*rxF[0] - (int)pil[1]*rxF[1])>>15);
                    ch[1] = (short)(((int)pil[0]*rxF[1] + (int)pil[1]*rxF[0])>>15);
                    multadd_real_vector_complex_scalar(f1,ch,dl_bf_ch,16);
                    pil+=2;
                  }
                } else if (skip_half==2) {
                  if (pil_offset<2) {

                    ch[0] = (short)(((int)pil[0]*rxF[16] - (int)pil[1]*rxF[17])>>15);
                    ch[1] = (short)(((int)pil[0]*rxF[17] + (int)pil[1]*rxF[16])>>15);
                    multadd_real_vector_complex_scalar(f1,ch,dl_bf_ch,16); 
                    pil+=2;
                    
                  } else {

                    ch[0] = (short)(((int)pil[0]*rxF[8] - (int)pil[1]*rxF[9])>>15);
                    ch[1] = (short)(((int)pil[0]*rxF[9] + (int)pil[1]*rxF[8])>>15);
                    multadd_real_vector_complex_scalar(f2l,ch,dl_bf_ch,16);
                    pil+=2;

                    ch[0] = (short)(((int)pil[0]*rxF[16] - (int)pil[1]*rxF[17])>>15);
                    ch[1] = (short)(((int)pil[0]*rxF[17] + (int)pil[1]*rxF[16])>>15);
                    multadd_real_vector_complex_scalar(f2r,ch,dl_bf_ch,16); 
                    pil+=2;

                  }
                } else {

                  ch[0] = (short)(((int)pil[0]*rxF[0] - (int)pil[1]*rxF[1])>>15);
                  ch[1] = (short)(((int)pil[0]*rxF[1] + (int)pil[1]*rxF[0])>>15);
                  multadd_real_vector_complex_scalar(fl,ch,dl_bf_ch,16);
365 366 367
#ifdef DEBUG_BF_CH
                  printf("symbol=%d,rxF[0]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[0],rxF[1],pil[0],pil[1],ch[0],ch[1]);
#endif
368 369 370 371 372
                  pil+=2;
                  
                  ch[0] = (short)(((int)pil[0]*rxF[8] - (int)pil[1]*rxF[9])>>15);
                  ch[1] = (short)(((int)pil[0]*rxF[9] + (int)pil[1]*rxF[8])>>15);
                  multadd_real_vector_complex_scalar(fm,ch,dl_bf_ch,16);
373 374 375
#ifdef DEBUG_BF_CH
                  printf("symbol=%d,rxF[4]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[8],rxF[9],pil[0],pil[1],ch[0],ch[1]);
#endif
376 377 378 379 380
                  pil+=2;

                  ch[0] = (short)(((int)pil[0]*rxF[16] - (int)pil[1]*rxF[17])>>15);
                  ch[1] = (short)(((int)pil[0]*rxF[17] + (int)pil[1]*rxF[16])>>15);
                  multadd_real_vector_complex_scalar(fr,ch,dl_bf_ch,16);
381 382 383
#ifdef DEBUG_BF_CH
                  printf("symbol=%d,rxF[8]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[16],rxF[17],pil[0],pil[1],ch[0],ch[1]);
#endif
384 385 386 387
                  pil+=2;

               }  
             } else {
388 389
		LOG_E(PHY,"lte_dl_bf_channel_estimation(lte_dl_bf_channel_estimation.c):TM7 beamgforming channel estimation not supported for extented CP\n");
		exit(-1);
390 391 392
             }
          
           } else {
393
	      LOG_E(PHY,"lte_dl_bf_channel_estimation(lte_dl_bf_channel_estimation.c):transmission mode not supported.\n");
394 395 396 397 398 399 400 401 402 403 404
           }
         }       
         nb_rb++;
       }

        rxF+=24;
        dl_bf_ch+=24;
      } // first half loop

      // Do middle RB (around DC) 
      if (rb < 32)
405
        rb_alloc_ind = (rballoc[0]>>rb) & 1;
406
      else if (rb < 64)
407
        rb_alloc_ind = (rballoc[1]>>(rb-32)) & 1;
408
      else if (rb < 96)
409
        rb_alloc_ind = (rballoc[2]>>(rb-64)) & 1;
410
      else if (rb < 100)
411
        rb_alloc_ind = (rballoc[3]>>(rb-96)) & 1;
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
      else
        rb_alloc_ind = 0;

      // PBCH
      if ((subframe==0) && (rb>=((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l>=(nsymb>>1)) && (l<((nsymb>>1) + 4))) {
        rb_alloc_ind = 0;
      }

      //SSS
      if (((subframe==0)||(subframe==5)) && (rb>=((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l==sss_symb) ) {
        rb_alloc_ind = 0;
      }

      if (frame_parms->frame_type == FDD) {
       //PSS
        if (((subframe==0)||(subframe==5)) && (rb>=((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb) ) {
          rb_alloc_ind = 0;
        }
      }

432
      if ((frame_parms->frame_type == TDD) && ((subframe==1)||(subframe==6))) {
433 434 435 436 437 438 439 440 441 442 443 444
        //PSS
        if ((rb>((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb) ) {
          rb_alloc_ind = 0;
        }
      }

      //printf("DC rb %d (%p)\n",rb,rxF);
      if (rb_alloc_ind==1) {
        if (pil_offset<2) {
          ch[0] = (short)(((int)pil[0]*rxF[0] - (int)pil[1]*rxF[1])>>15);
          ch[1] = (short)(((int)pil[0]*rxF[1] + (int)pil[1]*rxF[0])>>15);
          multadd_real_vector_complex_scalar(fl_dc,ch,dl_bf_ch,16);
445 446 447
#ifdef DEBUG_BF_CH
          //printf("symbol=%d,rxF[0]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[0],rxF[1],pil[0],pil[1],ch[0],ch[1]);
#endif
448 449 450 451 452
          pil+=2;;
                  
          ch[0] = (short)(((int)pil[0]*rxF[8] - (int)pil[1]*rxF[9])>>15);
          ch[1] = (short)(((int)pil[0]*rxF[9] + (int)pil[1]*rxF[8])>>15);
          multadd_real_vector_complex_scalar(fm_dc,ch,dl_bf_ch,16);
453 454 455
#ifdef DEBUG_BF_CH
          //printf("symbol=%d,rxF[4]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[8],rxF[9],pil[0],pil[1],ch[0],ch[1]);
#endif
456 457 458 459 460 461 462
          pil+=2;;

          rxF   = (short *)&rxdataF[aarx][symbol*(frame_parms->ofdm_symbol_size)];

          ch[0] = (short)(((int)pil[0]*rxF[6] - (int)pil[1]*rxF[7])>>15);
          ch[1] = (short)(((int)pil[0]*rxF[7] + (int)pil[1]*rxF[6])>>15);
          multadd_real_vector_complex_scalar(fr_dc,ch,dl_bf_ch,16);
463 464 465
#ifdef DEBUG_BF_CH
          //printf("symbol=%d,rxF[3]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[6],rxF[7],pil[0],pil[1],ch[0],ch[1]);
#endif
466 467 468 469 470
          pil+=2;;
        } else {
          ch[0] = (short)(((int)pil[0]*rxF[0] - (int)pil[1]*rxF[1])>>15);
          ch[1] = (short)(((int)pil[0]*rxF[1] + (int)pil[1]*rxF[0])>>15);
          multadd_real_vector_complex_scalar(fl_dc,ch,dl_bf_ch,16);
471 472 473
#ifdef DEBUG_BF_CH
          //printf("symbol=%d,rxF[0]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[0],rxF[1],pil[0],pil[1],ch[0],ch[1]);
#endif
474 475 476 477 478 479 480
          pil+=2;;
                  
          rxF   = (short *)&rxdataF[aarx][symbol*(frame_parms->ofdm_symbol_size)];

          ch[0] = (short)(((int)pil[0]*rxF[2] - (int)pil[1]*rxF[3])>>15);
          ch[1] = (short)(((int)pil[0]*rxF[3] + (int)pil[1]*rxF[2])>>15);
          multadd_real_vector_complex_scalar(fm_dc,ch,dl_bf_ch,16);
481 482 483
#ifdef DEBUG_BF_CH
          //printf("symbol=%d,rxF[1]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[2],rxF[3],pil[0],pil[1],ch[0],ch[1]);
#endif
484 485 486 487 488
          pil+=2;;

          ch[0] = (short)(((int)pil[0]*rxF[10] - (int)pil[1]*rxF[11])>>15);
          ch[1] = (short)(((int)pil[0]*rxF[11] + (int)pil[1]*rxF[10])>>15);
          multadd_real_vector_complex_scalar(fr_dc,ch,dl_bf_ch,16);
489 490 491
#ifdef DEBUG_BF_CH
          //printf("symbol=%d,rxF[5]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[10],rxF[11],pil[0],pil[1],ch[0],ch[1]);
#endif
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
          pil+=2;;
        }
      } // rballoc==1
      else {
        rxF       = (short *)&rxdataF[aarx][pil_offset+((symbol*(frame_parms->ofdm_symbol_size)))];
      }

      rxF+=14+2*pil_offset;
      dl_bf_ch+=24;
      rb++;

      for (; rb<frame_parms->N_RB_DL; rb++) {
        skip_half=0;

        if (rb < 32)
507
          rb_alloc_ind = (rballoc[0]>>rb) & 1;
508
        else if (rb < 64)
509
          rb_alloc_ind = (rballoc[1]>>(rb-32)) & 1;
510
        else if (rb < 96)
511
          rb_alloc_ind = (rballoc[2]>>(rb-64)) & 1;
512
        else if (rb < 100)
513
          rb_alloc_ind = (rballoc[3]>>(rb-96)) & 1;
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
        else
          rb_alloc_ind = 0;

        // PBCH
        if ((subframe==0) && (rb>((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l>=nsymb>>1) && (l<((nsymb>>1) + 4))) {
          rb_alloc_ind = 0;
        }

        //PBCH subframe 0, symbols nsymb>>1 ... nsymb>>1 + 3
        if ((subframe==0) && (rb==((frame_parms->N_RB_DL>>1)-3)) && (l>=(nsymb>>1)) && (l<((nsymb>>1) + 4)))
          skip_half=1;
        else if ((subframe==0) && (rb==((frame_parms->N_RB_DL>>1)+3)) && (l>=(nsymb>>1)) && (l<((nsymb>>1) + 4)))
          skip_half=2;

        //SSS
        if (((subframe==0)||(subframe==5)) && (rb>((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l==sss_symb) ) {
          rb_alloc_ind = 0;
        }

        //SSS
        if (((subframe==0)||(subframe==5)) && (rb==((frame_parms->N_RB_DL>>1)-3)) && (l==sss_symb))
          skip_half=1;
        else if (((subframe==0)||(subframe==5)) && (rb==((frame_parms->N_RB_DL>>1)+3)) && (l==sss_symb))
          skip_half=2;

        if (frame_parms->frame_type == FDD) {
          //PSS
          if (((subframe==0)||(subframe==5)) && (rb>((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb) ) {
            rb_alloc_ind = 0;
          }

          //PSS
          if (((subframe==0)||(subframe==5)) && (rb==((frame_parms->N_RB_DL>>1)-3)) && (l==pss_symb))
            skip_half=1;
          else if (((subframe==0)||(subframe==5)) && (rb==((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb))
            skip_half=2;
        }

552
        if ((frame_parms->frame_type == TDD) && ((subframe==1)||(subframe==6))) { //TDD Subframe 1 and 6
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
          if ((rb>((frame_parms->N_RB_DL>>1)-3)) && (rb<((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb) ) {
            rb_alloc_ind = 0;
          }

          if ((rb==((frame_parms->N_RB_DL>>1)-3)) && (l==pss_symb))
            skip_half=1;
          else if ((rb==((frame_parms->N_RB_DL>>1)+3)) && (l==pss_symb))
            skip_half=2;
        }

        if (rb_alloc_ind==1) {
          if (uespec_pilots==1) {
            if (beamforming_mode==7) {
              if (frame_parms->Ncp==0) {
                if (skip_half==1) {
                  if (pil_offset<2) {

                    ch[0] = (short)(((int)pil[0]*rxF[0] - (int)pil[1]*rxF[1])>>15);
                    ch[1] = (short)(((int)pil[0]*rxF[1] + (int)pil[1]*rxF[0])>>15);
                    multadd_real_vector_complex_scalar(f2l,ch,dl_bf_ch,16); 
                    pil+=2;
                    
                    ch[0] = (short)(((int)pil[0]*rxF[8] - (int)pil[1]*rxF[9])>>15);
                    ch[1] = (short)(((int)pil[0]*rxF[9] + (int)pil[1]*rxF[8])>>15);
                    multadd_real_vector_complex_scalar(f2r,ch,dl_bf_ch,16); 
                    pil+=2;
          

                  } else {

                    ch[0] = (short)(((int)pil[0]*rxF[0] - (int)pil[1]*rxF[1])>>15);
                    ch[1] = (short)(((int)pil[0]*rxF[1] + (int)pil[1]*rxF[0])>>15);
                    multadd_real_vector_complex_scalar(f1,ch,dl_bf_ch,16);
                    pil+=2;
                  }
                } else if (skip_half==2) {
                   if (pil_offset<2) {

                     ch[0] = (short)(((int)pil[0]*rxF[16] - (int)pil[1]*rxF[17])>>15);
                     ch[1] = (short)(((int)pil[0]*rxF[17] + (int)pil[1]*rxF[16])>>15);
                     multadd_real_vector_complex_scalar(f1,ch,dl_bf_ch,16); 
                     pil+=2;
                     
                   } else {

                     ch[0] = (short)(((int)pil[0]*rxF[8] - (int)pil[1]*rxF[9])>>15);
                     ch[1] = (short)(((int)pil[0]*rxF[9] + (int)pil[1]*rxF[8])>>15);
                     multadd_real_vector_complex_scalar(f2l,ch,dl_bf_ch,16);
                     pil+=2;

                     ch[0] = (short)(((int)pil[0]*rxF[16] - (int)pil[1]*rxF[17])>>15);
                     ch[1] = (short)(((int)pil[0]*rxF[17] + (int)pil[1]*rxF[16])>>15);
                     multadd_real_vector_complex_scalar(f2r,ch,dl_bf_ch,16); 
                     pil+=2;

                   }
                } else {

                  ch[0] = (short)(((int)pil[0]*rxF[0] - (int)pil[1]*rxF[1])>>15);
                  ch[1] = (short)(((int)pil[0]*rxF[1] + (int)pil[1]*rxF[0])>>15);
                  multadd_real_vector_complex_scalar(fl,ch,dl_bf_ch,16);
614 615 616
#ifdef DEBUG_BF_CH
                  printf("symbol=%d,rxF[0]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[0],rxF[1],pil[0],pil[1],ch[0],ch[1]);
#endif
617 618 619 620 621
                  pil+=2;
                  
                  ch[0] = (short)(((int)pil[0]*rxF[8] - (int)pil[1]*rxF[9])>>15);
                  ch[1] = (short)(((int)pil[0]*rxF[9] + (int)pil[1]*rxF[8])>>15);
                  multadd_real_vector_complex_scalar(fm,ch,dl_bf_ch,16);
622 623 624
#ifdef DEBUG_BF_CH
                  printf("symbol=%d,rxF[4]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[8],rxF[9],pil[0],pil[1],ch[0],ch[1]);
#endif
625 626 627 628 629
                  pil+=2;

                  ch[0] = (short)(((int)pil[0]*rxF[16] - (int)pil[1]*rxF[17])>>15);
                  ch[1] = (short)(((int)pil[0]*rxF[17] + (int)pil[1]*rxF[16])>>15);
                  multadd_real_vector_complex_scalar(fr,ch,dl_bf_ch,16);
630 631 632
#ifdef DEBUG_BF_CH
                  printf("symbol=%d,rxF[8]=(%d,%d),pil=(%d,%d),ch=(%d,%d)\n",symbol,rxF[16],rxF[17],pil[0],pil[1],ch[0],ch[1]);
#endif
633 634 635 636
                  pil+=2;

                }
              } else {
637
                LOG_E(PHY,"lte_dl_bf_channel_estimation(lte_dl_bf_channel_estimation.c):TM7 beamgforming channel estimation not supported for extented CP\n");
638 639 640 641
                exit(-1);
              }
            
            } else {
642
              LOG_E(PHY,"lte_dl_bf_channel_estimation(lte_dl_bf_channel_estimation.c):transmission mode not supported.\n");
643 644 645 646 647 648 649 650 651 652 653 654 655 656
            }
          }
          nb_rb++;
        }

        rxF+=24;
        dl_bf_ch+=24;
      } // second half of RBs
    } // odd number of RBs  

    // Temporal Interpolation
    if (phy_vars_ue->perfect_ce == 0) {

      dl_bf_ch = (short *)&dl_bf_ch_estimates[aarx][ch_offset];
657 658 659
#ifdef DEBUG_BF_CH
      printf("[dlsch_bf_ch_est.c]:symbol %d, dl_bf_ch (%d,%d)\n",symbol,dl_bf_ch[0],dl_bf_ch[1]);
#endif
660 661 662 663 664

      if (phy_vars_ue->high_speed_flag == 0) {
        multadd_complex_vector_real_scalar(dl_bf_ch,
                                           32767-phy_vars_ue->ch_est_alpha,
                                           dl_bf_ch-(frame_parms->ofdm_symbol_size<<1),0,frame_parms->ofdm_symbol_size);
665
        //printf("dlsch_bf_ch_est.c:symbol %d,dl_bf_ch (%d,%d)\n",symbol,*(dl_bf_ch-512*2),*(dl_bf_ch-512*2+1));
666 667 668 669 670 671 672
      } else { // high_speed_flag == 1
        if (beamforming_mode==7) {
          if (frame_parms->Ncp==0) {
            if (symbol == pilot0) {
              //      printf("Interpolating %d->0\n",4-phy_vars_ue->lte_frame_parms.Ncp);
              //      dl_bf_ch_prev = (short *)&dl_bf_ch_estimates[aarx][(4-phy_vars_ue->lte_frame_parms.Ncp)*(frame_parms->ofdm_symbol_size)];
              dl_bf_ch_prev = (short *)&dl_bf_ch_estimates[aarx][pilot3*(frame_parms->ofdm_symbol_size)];
673 674 675
#ifdef DEBUG_BF_CH
              printf("[dlsch_bf_ch_est.c] symbol=%d, dl_bf_ch_prev=(%d,%d), dl_bf_ch=(%d,%d)\n", symbol, dl_bf_ch_prev[0], dl_bf_ch_prev[1], dl_bf_ch[0], dl_bf_ch[1]);
#endif
676 677 678 679 680 681 682 683 684 685 686 687 688 689
              // pilot spacing 5 symbols (1/5,2/5,3/5,4/5 combination)
              multadd_complex_vector_real_scalar(dl_bf_ch_prev,26214,dl_bf_ch_prev+(2*(frame_parms->ofdm_symbol_size)),1,frame_parms->ofdm_symbol_size);
              multadd_complex_vector_real_scalar(dl_bf_ch,6554,dl_bf_ch_prev+(2*(frame_parms->ofdm_symbol_size)),0,frame_parms->ofdm_symbol_size);

              multadd_complex_vector_real_scalar(dl_bf_ch_prev,19661,dl_bf_ch-(3*2*(frame_parms->ofdm_symbol_size)),1,frame_parms->ofdm_symbol_size);
              multadd_complex_vector_real_scalar(dl_bf_ch,13107,dl_bf_ch-(3*2*(frame_parms->ofdm_symbol_size)),0,frame_parms->ofdm_symbol_size);

              multadd_complex_vector_real_scalar(dl_bf_ch_prev,13107,dl_bf_ch-(2*((frame_parms->ofdm_symbol_size)<<1)),1,frame_parms->ofdm_symbol_size);
              multadd_complex_vector_real_scalar(dl_bf_ch,19661,dl_bf_ch-(2*((frame_parms->ofdm_symbol_size)<<1)),0,frame_parms->ofdm_symbol_size);

              multadd_complex_vector_real_scalar(dl_bf_ch_prev,6554,dl_bf_ch-(2*(frame_parms->ofdm_symbol_size)),1,frame_parms->ofdm_symbol_size);
              multadd_complex_vector_real_scalar(dl_bf_ch,26214,dl_bf_ch-(2*(frame_parms->ofdm_symbol_size)),0,frame_parms->ofdm_symbol_size);
            } else if (symbol == pilot1) {
              dl_bf_ch_prev = (short *)&dl_bf_ch_estimates[aarx][pilot0*(frame_parms->ofdm_symbol_size)];
690 691 692
#ifdef DEBUG_BF_CH
              printf("[dlsch_bf_ch_est.c] symbol=%d, dl_bf_ch_prev=(%d,%d), dl_bf_ch=(%d,%d)\n", symbol, dl_bf_ch_prev[0], dl_bf_ch_prev[1], dl_bf_ch[0], dl_bf_ch[1]);
#endif
693 694 695 696 697 698 699 700 701 702

              // pilot spacing 3 symbols (1/3,2/3 combination)
              multadd_complex_vector_real_scalar(dl_bf_ch_prev,21845,dl_bf_ch_prev+(2*(frame_parms->ofdm_symbol_size)),1,frame_parms->ofdm_symbol_size);
              multadd_complex_vector_real_scalar(dl_bf_ch,10923,dl_bf_ch_prev+(2*(frame_parms->ofdm_symbol_size)),0,frame_parms->ofdm_symbol_size);

              multadd_complex_vector_real_scalar(dl_bf_ch_prev,10923,dl_bf_ch_prev+(2*((frame_parms->ofdm_symbol_size)<<1)),1,frame_parms->ofdm_symbol_size);
              multadd_complex_vector_real_scalar(dl_bf_ch,21845,dl_bf_ch_prev+(2*((frame_parms->ofdm_symbol_size)<<1)),0,frame_parms->ofdm_symbol_size);

            } else if (symbol == pilot2) {
              dl_bf_ch_prev = (short *)&dl_bf_ch_estimates[aarx][pilot1*(frame_parms->ofdm_symbol_size)];
703 704 705
#ifdef DEBUG_BF_CH
              printf("[dlsch_bf_ch_est.c] symbol=%d, dl_bf_ch_prev=(%d,%d), dl_bf_ch=(%d,%d)\n", symbol, dl_bf_ch_prev[0], dl_bf_ch_prev[1], dl_bf_ch[0], dl_bf_ch[1]);
#endif
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

              multadd_complex_vector_real_scalar(dl_bf_ch_prev,21845,dl_bf_ch_prev+(2*(frame_parms->ofdm_symbol_size)),1,frame_parms->ofdm_symbol_size);
              multadd_complex_vector_real_scalar(dl_bf_ch,10923,dl_bf_ch_prev+(2*(frame_parms->ofdm_symbol_size)),0,frame_parms->ofdm_symbol_size);

              multadd_complex_vector_real_scalar(dl_bf_ch_prev,10923,dl_bf_ch_prev+(2*((frame_parms->ofdm_symbol_size)<<1)),1,frame_parms->ofdm_symbol_size);
              multadd_complex_vector_real_scalar(dl_bf_ch,21845,dl_bf_ch_prev+(2*((frame_parms->ofdm_symbol_size)<<1)),0,frame_parms->ofdm_symbol_size);
            } else { // symbol == pilot3
            //      printf("Interpolating 0->%d\n",4-phy_vars_ue->lte_frame_parms.Ncp);
            dl_bf_ch_prev = (short *)&dl_bf_ch_estimates[aarx][pilot2*(frame_parms->ofdm_symbol_size)];

            // pilot spacing 3 symbols (1/3,2/3 combination)
            multadd_complex_vector_real_scalar(dl_bf_ch_prev,21845,dl_bf_ch_prev+(2*(frame_parms->ofdm_symbol_size)),1,frame_parms->ofdm_symbol_size);
            multadd_complex_vector_real_scalar(dl_bf_ch,10923,dl_bf_ch_prev+(2*(frame_parms->ofdm_symbol_size)),0,frame_parms->ofdm_symbol_size);

            multadd_complex_vector_real_scalar(dl_bf_ch_prev,10923,dl_bf_ch_prev+(2*((frame_parms->ofdm_symbol_size)<<1)),1,frame_parms->ofdm_symbol_size);
            multadd_complex_vector_real_scalar(dl_bf_ch,21845,dl_bf_ch_prev+(2*((frame_parms->ofdm_symbol_size)<<1)),0,frame_parms->ofdm_symbol_size);
            }

          } else {
725
            LOG_E(PHY,"lte_dl_bf_channel_estimation:temporal interpolation not supported for TM7 extented CP.\n");
726 727
          }
        } else {
728
          LOG_E(PHY,"lte_dl_bf_channel_estimation:temporal interpolation not supported for this beamforming mode.\n");
729 730 731 732
        } 
      }
    }
  } //aarx
733 734 735 736
 
#ifdef DEBUG_BF_CH  
    printf("[dlsch_bf_ch_est.c]: dl_bf_estimates[0][600] %d, %d \n",*(short *)&dl_bf_ch_estimates[0][600],*(short*)&phy_vars_ue->lte_ue_pdsch_vars[eNB_id]->dl_bf_ch_estimates[0][600]);
#endif
737 738 739 740

  return(0);

}