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

22 23
#include "PHY/defs_UE.h"
#include "modulation_UE.h"
24 25
#include "PHY/LTE_ESTIMATION/lte_estimation.h"

26 27 28 29
//#define DEBUG_FEP

#define SOFFSET 0

30
int slot_fep(PHY_VARS_UE *ue,
31 32 33
             unsigned char l,
             unsigned char Ns,
             int sample_offset,
34 35
             int no_prefix,
	     int reset_freq_est)
36
{
37

38 39
  LTE_DL_FRAME_PARMS *frame_parms = &ue->frame_parms;
  LTE_UE_COMMON *common_vars   = &ue->common_vars;
40
  uint8_t eNB_id = 0;//ue_common_vars->eNb_id;
41 42 43 44 45 46 47 48 49 50
  unsigned char aa;
  unsigned char symbol = l+((7-frame_parms->Ncp)*(Ns&1)); ///symbol within sub-frame
  unsigned int nb_prefix_samples = (no_prefix ? 0 : frame_parms->nb_prefix_samples);
  unsigned int nb_prefix_samples0 = (no_prefix ? 0 : frame_parms->nb_prefix_samples0);
  unsigned int subframe_offset;//,subframe_offset_F;
  unsigned int slot_offset;
  int i;
  unsigned int frame_length_samples = frame_parms->samples_per_tti * 10;
  unsigned int rx_offset;

51 52 53 54 55
  /*LTE_UE_DLSCH_t **dlsch_ue = phy_vars_ue->dlsch_ue[eNB_id];
  unsigned char harq_pid = dlsch_ue[0]->current_harq_pid; 
  LTE_DL_UE_HARQ_t *dlsch0_harq = dlsch_ue[0]->harq_processes[harq_pid];
  int uespec_pilot[9][1200];*/

56
  void (*dft)(int16_t *,int16_t *, int);
57
  int tmp_dft_in[2048] __attribute__ ((aligned (32)));  // This is for misalignment issues for 6 and 15 PRBs
58

59 60
  switch (frame_parms->ofdm_symbol_size) {
  case 128:
61 62
    dft = dft128;
    break;
63

64
  case 256:
65 66
    dft = dft256;
    break;
67

68
  case 512:
69 70
    dft = dft512;
    break;
71

72
  case 1024:
73 74
    dft = dft1024;
    break;
75

76 77 78 79 80
  case 1536:
    dft = dft1536;
    break;

  case 2048:
81 82
    dft = dft2048;
    break;
83

84 85 86 87 88 89 90 91
  default:
    dft = dft512;
    break;
  }

  if (no_prefix) {
    subframe_offset = frame_parms->ofdm_symbol_size * frame_parms->symbols_per_tti * (Ns>>1);
    slot_offset = frame_parms->ofdm_symbol_size * (frame_parms->symbols_per_tti>>1) * (Ns%2);
92
  } else {
93 94 95
    subframe_offset = frame_parms->samples_per_tti * (Ns>>1);
    slot_offset = (frame_parms->samples_per_tti>>1) * (Ns%2);
  }
96

97 98 99 100
  //  subframe_offset_F = frame_parms->ofdm_symbol_size * frame_parms->symbols_per_tti * (Ns>>1);


  if (l<0 || l>=7-frame_parms->Ncp) {
101
    printf("slot_fep: l must be between 0 and %d\n",7-frame_parms->Ncp);
102 103
    return(-1);
  }
104

105
  if (Ns<0 || Ns>=20) {
106
    printf("slot_fep: Ns must be between 0 and 19\n");
107 108 109 110
    return(-1);
  }


111 112

  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
113
    memset(&common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],0,frame_parms->ofdm_symbol_size*sizeof(int));
114

115
    rx_offset = sample_offset + slot_offset + nb_prefix_samples0 + subframe_offset - SOFFSET;
116 117
    // Align with 256 bit
    //    rx_offset = rx_offset&0xfffffff8;
118

119
    if (l==0) {
120

121
      if (rx_offset > (frame_length_samples - frame_parms->ofdm_symbol_size))
122 123
        memcpy((short *)&common_vars->rxdata[aa][frame_length_samples],
               (short *)&common_vars->rxdata[aa][0],
124
               frame_parms->ofdm_symbol_size*sizeof(int));
125

126
      if ((rx_offset&7)!=0) {  // if input to dft is not 256-bit aligned, issue for size 6,15 and 25 PRBs
127
        memcpy((void *)tmp_dft_in,
128
               (void *)&common_vars->rxdata[aa][rx_offset % frame_length_samples],
129 130
               frame_parms->ofdm_symbol_size*sizeof(int));
        dft((int16_t *)tmp_dft_in,
131
            (int16_t *)&common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],1);
132
      } else { // use dft input from RX buffer directly
Gabriel's avatar
Gabriel committed
133 134 135
#if UE_TIMING_TRACE
          start_meas(&ue->rx_dft_stats);
#endif
136

137
        dft((int16_t *)&common_vars->rxdata[aa][(rx_offset) % frame_length_samples],
138
            (int16_t *)&common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],1);
Gabriel's avatar
Gabriel committed
139
#if UE_TIMING_TRACE
140
        stop_meas(&ue->rx_dft_stats);
Gabriel's avatar
Gabriel committed
141
#endif
142

143
      }
144
    } else {
145 146
      rx_offset += (frame_parms->ofdm_symbol_size+nb_prefix_samples)*l;// +
      //                   (frame_parms->ofdm_symbol_size+nb_prefix_samples)*(l-1);
147

148
#ifdef DEBUG_FEP
149
      //  if (ue->frame <100)
150 151
      LOG_I(PHY,"slot_fep: frame %d: slot %d, symbol %d, nb_prefix_samples %d, nb_prefix_samples0 %d, slot_offset %d, subframe_offset %d, sample_offset %d,rx_offset %d, frame_length_samples %d\n", ue->proc.proc_rxtx[(Ns>>1)&1].frame_rx,Ns, symbol,
          nb_prefix_samples,nb_prefix_samples0,slot_offset,subframe_offset,sample_offset,rx_offset,frame_length_samples);
152
#endif
153

154
      if (rx_offset > (frame_length_samples - frame_parms->ofdm_symbol_size))
155 156
        memcpy((void *)&common_vars->rxdata[aa][frame_length_samples],
               (void *)&common_vars->rxdata[aa][0],
157
               frame_parms->ofdm_symbol_size*sizeof(int));
Gabriel's avatar
Gabriel committed
158
#if UE_TIMING_TRACE
159
      start_meas(&ue->rx_dft_stats);
Gabriel's avatar
Gabriel committed
160
#endif
161

162
      if ((rx_offset&7)!=0) {  // if input to dft is not 128-bit aligned, issue for size 6 and 15 PRBs
163
        memcpy((void *)tmp_dft_in,
164
               (void *)&common_vars->rxdata[aa][(rx_offset) % frame_length_samples],
165 166
               frame_parms->ofdm_symbol_size*sizeof(int));
        dft((int16_t *)tmp_dft_in,
167
            (int16_t *)&common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],1);
168
      } else { // use dft input from RX buffer directly
169

170
        dft((int16_t *)&common_vars->rxdata[aa][(rx_offset) % frame_length_samples],
171
            (int16_t *)&common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],1);
172
      }
Gabriel's avatar
Gabriel committed
173
#if UE_TIMING_TRACE
174
      stop_meas(&ue->rx_dft_stats);
Gabriel's avatar
Gabriel committed
175
#endif
176 177


178
    }
179

180 181 182 183
    #ifdef DEBUG_FEP
        //  if (ue->frame <100)
        printf("slot_fep: frame %d: symbol %d rx_offset %d\n", ue->proc.proc_rxtx[(Ns>>1)&1].frame_rx, symbol,rx_offset);
    #endif
184
  }
185

186
  if (ue->perfect_ce == 0) {
187
    if ((l==0) || (l==(4-frame_parms->Ncp))) {
Xiwen JIANG's avatar
Xiwen JIANG committed
188
      for (aa=0; aa<frame_parms->nb_antenna_ports_eNB; aa++) {
189 190

#ifdef DEBUG_FEP
191
        printf("Channel estimation eNB %d, aatx %d, slot %d, symbol %d\n",eNB_id,aa,Ns,l);
192
#endif
Gabriel's avatar
Gabriel committed
193
#if UE_TIMING_TRACE
194
        start_meas(&ue->dlsch_channel_estimation_stats);
Gabriel's avatar
Gabriel committed
195
#endif
196
        lte_dl_channel_estimation(ue,eNB_id,0,
197 198 199 200
                                  Ns,
                                  aa,
                                  l,
                                  symbol);
Gabriel's avatar
Gabriel committed
201
#if UE_TIMING_TRACE
202
        stop_meas(&ue->dlsch_channel_estimation_stats);
Gabriel's avatar
Gabriel committed
203
#endif
204

205 206
        for (i=0; i<ue->measurements.n_adj_cells; i++) {
          lte_dl_channel_estimation(ue,eNB_id,i+1,
207 208 209 210 211
                                    Ns,
                                    aa,
                                    l,
                                    symbol);
        }
212 213 214 215
      }


      // do frequency offset estimation here!
216
      // use channel estimates from current symbol (=ch_t) and last symbol (ch_{t-1})
217
#ifdef DEBUG_FEP
218
      printf("Frequency offset estimation\n");
219 220
#endif

221
      if (l==(4-frame_parms->Ncp)) {
222

Gabriel's avatar
Gabriel committed
223 224 225
#if UE_TIMING_TRACE
          start_meas(&ue->dlsch_freq_offset_estimation_stats);
#endif
226

227
        lte_est_freq_offset(common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns>>1]].dl_ch_estimates[0],
228 229
                            frame_parms,
                            l,
230
                            &common_vars->freq_offset,
231
			    reset_freq_est);
Gabriel's avatar
Gabriel committed
232
#if UE_TIMING_TRACE
233
        stop_meas(&ue->dlsch_freq_offset_estimation_stats);
Gabriel's avatar
Gabriel committed
234
#endif
235 236

      }
237
    }
238

239
  }
240

241
#ifdef DEBUG_FEP
242
  printf("slot_fep: done\n");
243 244 245
#endif
  return(0);
}
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

int front_end_fft(PHY_VARS_UE *ue,
             unsigned char l,
             unsigned char Ns,
             int sample_offset,
             int no_prefix)
{
  LTE_DL_FRAME_PARMS *frame_parms = &ue->frame_parms;
  LTE_UE_COMMON *common_vars   = &ue->common_vars;
  unsigned char aa;
  unsigned char symbol = l+((7-frame_parms->Ncp)*(Ns&1)); ///symbol within sub-frame
  unsigned int nb_prefix_samples = (no_prefix ? 0 : frame_parms->nb_prefix_samples);
  unsigned int nb_prefix_samples0 = (no_prefix ? 0 : frame_parms->nb_prefix_samples0);
  unsigned int subframe_offset;//,subframe_offset_F;
  unsigned int slot_offset;
  unsigned int frame_length_samples = frame_parms->samples_per_tti * 10;
  unsigned int rx_offset;
263
  uint8_t  threadId;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

  /*LTE_UE_DLSCH_t **dlsch_ue = phy_vars_ue->dlsch_ue[eNB_id];
  unsigned char harq_pid = dlsch_ue[0]->current_harq_pid;
  LTE_DL_UE_HARQ_t *dlsch0_harq = dlsch_ue[0]->harq_processes[harq_pid];
  int uespec_pilot[9][1200];*/

  void (*dft)(int16_t *,int16_t *, int);
  int tmp_dft_in[2048] __attribute__ ((aligned (32)));  // This is for misalignment issues for 6 and 15 PRBs

  switch (frame_parms->ofdm_symbol_size) {
  case 128:
    dft = dft128;
    break;

  case 256:
    dft = dft256;
    break;

  case 512:
    dft = dft512;
    break;

  case 1024:
    dft = dft1024;
    break;

  case 1536:
    dft = dft1536;
    break;

  case 2048:
    dft = dft2048;
    break;

  default:
    dft = dft512;
    break;
  }

  if (no_prefix) {
    subframe_offset = frame_parms->ofdm_symbol_size * frame_parms->symbols_per_tti * (Ns>>1);
    slot_offset = frame_parms->ofdm_symbol_size * (frame_parms->symbols_per_tti>>1) * (Ns%2);
  } else {
    subframe_offset = frame_parms->samples_per_tti * (Ns>>1);
    slot_offset = (frame_parms->samples_per_tti>>1) * (Ns%2);
  }

  //  subframe_offset_F = frame_parms->ofdm_symbol_size * frame_parms->symbols_per_tti * (Ns>>1);


  if (l<0 || l>=7-frame_parms->Ncp) {
    printf("slot_fep: l must be between 0 and %d\n",7-frame_parms->Ncp);
    return(-1);
  }

  if (Ns<0 || Ns>=20) {
    printf("slot_fep: Ns must be between 0 and 19\n");
    return(-1);
  }



326
  threadId = ue->current_thread_id[Ns>>1];
327
  for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
328 329
      // change thread index
    memset(&common_vars->common_vars_rx_data_per_thread[threadId].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],0,frame_parms->ofdm_symbol_size*sizeof(int));
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

    rx_offset = sample_offset + slot_offset + nb_prefix_samples0 + subframe_offset - SOFFSET;
    // Align with 256 bit
    //    rx_offset = rx_offset&0xfffffff8;

    if (l==0) {

      if (rx_offset > (frame_length_samples - frame_parms->ofdm_symbol_size))
        memcpy((short *)&common_vars->rxdata[aa][frame_length_samples],
               (short *)&common_vars->rxdata[aa][0],
               frame_parms->ofdm_symbol_size*sizeof(int));

      if ((rx_offset&7)!=0) {  // if input to dft is not 256-bit aligned, issue for size 6,15 and 25 PRBs
        memcpy((void *)tmp_dft_in,
               (void *)&common_vars->rxdata[aa][rx_offset % frame_length_samples],
               frame_parms->ofdm_symbol_size*sizeof(int));
        dft((int16_t *)tmp_dft_in,
347
            (int16_t *)&common_vars->common_vars_rx_data_per_thread[threadId].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],1);
348 349 350 351
      } else { // use dft input from RX buffer directly
        start_meas(&ue->rx_dft_stats);

        dft((int16_t *)&common_vars->rxdata[aa][(rx_offset) % frame_length_samples],
352
            (int16_t *)&common_vars->common_vars_rx_data_per_thread[threadId].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],1);
353 354 355 356 357 358 359 360 361
        stop_meas(&ue->rx_dft_stats);

      }
    } else {
      rx_offset += (frame_parms->ofdm_symbol_size+nb_prefix_samples)*l;// +
      //                   (frame_parms->ofdm_symbol_size+nb_prefix_samples)*(l-1);

#ifdef DEBUG_FEP
      //  if (ue->frame <100)
362 363
      LOG_I(PHY,"slot_fep: frame %d: slot %d, threadId %d, symbol %d, nb_prefix_samples %d, nb_prefix_samples0 %d, slot_offset %d, subframe_offset %d, sample_offset %d,rx_offset %d, frame_length_samples %d\n",
              ue->proc.proc_rxtx[threadId].frame_rx,Ns, threadId,symbol,
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
          nb_prefix_samples,nb_prefix_samples0,slot_offset,subframe_offset,sample_offset,rx_offset,frame_length_samples);
#endif

      if (rx_offset > (frame_length_samples - frame_parms->ofdm_symbol_size))
        memcpy((void *)&common_vars->rxdata[aa][frame_length_samples],
               (void *)&common_vars->rxdata[aa][0],
               frame_parms->ofdm_symbol_size*sizeof(int));

      start_meas(&ue->rx_dft_stats);

      if ((rx_offset&7)!=0) {  // if input to dft is not 128-bit aligned, issue for size 6 and 15 PRBs
        memcpy((void *)tmp_dft_in,
               (void *)&common_vars->rxdata[aa][(rx_offset) % frame_length_samples],
               frame_parms->ofdm_symbol_size*sizeof(int));
        dft((int16_t *)tmp_dft_in,
379
            (int16_t *)&common_vars->common_vars_rx_data_per_thread[threadId].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],1);
380 381 382
      } else { // use dft input from RX buffer directly

        dft((int16_t *)&common_vars->rxdata[aa][(rx_offset) % frame_length_samples],
383
            (int16_t *)&common_vars->common_vars_rx_data_per_thread[threadId].rxdataF[aa][frame_parms->ofdm_symbol_size*symbol],1);
384 385 386 387 388 389 390 391 392
      }

      stop_meas(&ue->rx_dft_stats);


    }

    #ifdef DEBUG_FEP
        //  if (ue->frame <100)
393
        printf("slot_fep: frame %d: symbol %d rx_offset %d\n", ue->proc.proc_rxtx[threadId].frame_rx, symbol,rx_offset);
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    #endif
  }
  return(0);
}

int front_end_chanEst(PHY_VARS_UE *ue,
             unsigned char l,
             unsigned char Ns,
             int reset_freq_est)
{
  LTE_DL_FRAME_PARMS *frame_parms = &ue->frame_parms;
  LTE_UE_COMMON *common_vars   = &ue->common_vars;
  uint8_t eNB_id = 0;//ue_common_vars->eNb_id;
  unsigned char aa;
  unsigned char symbol = l+((7-frame_parms->Ncp)*(Ns&1)); ///symbol within sub-frame
  int i;

  /*LTE_UE_DLSCH_t **dlsch_ue = phy_vars_ue->dlsch_ue[eNB_id];
  unsigned char harq_pid = dlsch_ue[0]->current_harq_pid;
  LTE_DL_UE_HARQ_t *dlsch0_harq = dlsch_ue[0]->harq_processes[harq_pid];
  int uespec_pilot[9][1200];*/

  if (ue->perfect_ce == 0) {
    if ((l==0) || (l==(4-frame_parms->Ncp))) {
      for (aa=0; aa<frame_parms->nb_antenna_ports_eNB; aa++) {

#ifdef DEBUG_FEP
        printf("Channel estimation eNB %d, aatx %d, slot %d, symbol %d\n",eNB_id,aa,Ns,l);
#endif
        start_meas(&ue->dlsch_channel_estimation_stats);
        lte_dl_channel_estimation(ue,eNB_id,0,
                                  Ns,
                                  aa,
                                  l,
                                  symbol);
        stop_meas(&ue->dlsch_channel_estimation_stats);

        for (i=0; i<ue->measurements.n_adj_cells; i++) {
          lte_dl_channel_estimation(ue,eNB_id,i+1,
                                    Ns,
                                    aa,
                                    l,
                                    symbol);
        }
      }


      // do frequency offset estimation here!
      // use channel estimates from current symbol (=ch_t) and last symbol (ch_{t-1})
#ifdef DEBUG_FEP
      printf("Frequency offset estimation\n");
#endif

      if (l==(4-frame_parms->Ncp)) {
        start_meas(&ue->dlsch_freq_offset_estimation_stats);
        lte_est_freq_offset(common_vars->common_vars_rx_data_per_thread[(Ns>>1)&0x1].dl_ch_estimates[0],
                            frame_parms,
                            l,
                            &common_vars->freq_offset,
                reset_freq_est);
        stop_meas(&ue->dlsch_freq_offset_estimation_stats);

      }
    }

  }
  return(0);
}