lte_adjust_sync.c 8.21 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
Cedric Roux's avatar
Cedric Roux committed
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
21
 * 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
 */

22
23
24
25
#include "PHY/types.h"
#include "PHY/defs.h"
#include "PHY/extern.h"

26
27
#include "UTIL/LOG/vcd_signal_dumper.h"

28
#define DEBUG_PHY
29
30
31
32
33
34

// Adjust location synchronization point to account for drift
// The adjustment is performed once per frame based on the
// last channel estimate of the receiver

void lte_adjust_synch(LTE_DL_FRAME_PARMS *frame_parms,
35
                      PHY_VARS_UE *ue,
36
                      unsigned char eNB_id,
37
					  uint8_t subframe,
38
39
                      unsigned char clear,
                      short coef)
40
41
42
{

  static int max_pos_fil = 0;
43
44
  static int count_max_pos_ok = 0;
  static int first_time = 1;
45
  int temp = 0, i, aa, max_val = 0, max_pos = 0;
46
47
48
  int diff;
  short Re,Im,ncoef;

49
50
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_ADJUST_SYNCH, VCD_FUNCTION_IN);

51
52
53
  ncoef = 32767 - coef;

#ifdef DEBUG_PHY
54
  LOG_D(PHY,"AbsSubframe %d.%d: rx_offset (before) = %d\n",ue->proc.proc_rxtx[0].frame_rx%1024,subframe,ue->rx_offset);
55
56
57
58
59
60
#endif //DEBUG_PHY


  // we only use channel estimates from tx antenna 0 here
  for (i = 0; i < frame_parms->nb_prefix_samples; i++) {
    temp = 0;
61
62

    for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
63
64
      Re = ((int16_t*)ue->common_vars.common_vars_rx_data_per_thread[ue->current_thread_id[subframe]].dl_ch_estimates_time[eNB_id][aa])[(i<<2)];
      Im = ((int16_t*)ue->common_vars.common_vars_rx_data_per_thread[ue->current_thread_id[subframe]].dl_ch_estimates_time[eNB_id][aa])[1+(i<<2)];
65
66
      temp += (Re*Re/2) + (Im*Im/2);
    }
67

68
69
70
71
72
73
74
75
76
77
78
79
    if (temp > max_val) {
      max_pos = i;
      max_val = temp;
    }
  }

  // filter position to reduce jitter
  if (clear == 1)
    max_pos_fil = max_pos;
  else
    max_pos_fil = ((max_pos_fil * coef) + (max_pos * ncoef)) >> 15;

80
  // do not filter to have proactive timing adjustment
81
  max_pos_fil = max_pos;
82

83
84
85
86
  if(subframe == 6)
  {
      diff = max_pos_fil - (frame_parms->nb_prefix_samples>>3);

Cedric Roux's avatar
Cedric Roux committed
87
88
89
90
91
#if BASIC_SIMULATOR
      /* a hack without which the UE does not connect (to be fixed somehow) */
      diff = 0;
#endif

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
      if ( abs(diff) < SYNCH_HYST )
          ue->rx_offset = 0;
      else
          ue->rx_offset = diff;

      if(abs(diff)<5)
          count_max_pos_ok ++;
      else
          count_max_pos_ok = 0;

      if(count_max_pos_ok > 10 && first_time == 1)
      {
          first_time = 0;
          ue->time_sync_cell = 1;
          if (ue->mac_enabled==1) {
              LOG_I(PHY,"[UE%d] Sending synch status to higher layers\n",ue->Mod_id);
              //mac_resynch();
109
              dl_phy_sync_success(ue->Mod_id,ue->proc.proc_rxtx[0].frame_rx,0,1);//ue->common_vars.eNb_id);
110
111
112
113
114
115
              ue->UE_mode[0] = PRACH;
          }
          else {
              ue->UE_mode[0] = PUSCH;
          }
      }
116

117
118
      if ( ue->rx_offset < 0 )
          ue->rx_offset += FRAME_LENGTH_COMPLEX_SAMPLES;
119

120
121
      if ( ue->rx_offset >= FRAME_LENGTH_COMPLEX_SAMPLES )
          ue->rx_offset -= FRAME_LENGTH_COMPLEX_SAMPLES;
122

123
124


125
      #ifdef DEBUG_PHY
126
127
      LOG_D(PHY,"AbsSubframe %d.%d: ThreadId %d diff =%i rx_offset (final) = %i : clear %d,max_pos = %d,max_pos_fil = %d (peak %d) max_val %d target_pos %d \n",
              ue->proc.proc_rxtx[ue->current_thread_id[subframe]].frame_rx,
128
              subframe,
129
              ue->current_thread_id[subframe],
130
131
132
133
134
135
136
137
              diff,
              ue->rx_offset,
              clear,
              max_pos,
              max_pos_fil,
              temp,max_val,
              (frame_parms->nb_prefix_samples>>3));
      #endif //DEBUG_PHY
138

139
140
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_UE_ADJUST_SYNCH, VCD_FUNCTION_OUT);
  }
141
142
143
144
}


int lte_est_timing_advance(LTE_DL_FRAME_PARMS *frame_parms,
145
                           LTE_eNB_SRS *lte_eNB_srs,
146
147
148
149
                           unsigned int  *eNB_id,
                           unsigned char clear,
                           unsigned char number_of_cards,
                           short coef)
150
151
152
153
154
155
156
157
158
159
160
161
162

{

  static int max_pos_fil2 = 0;
  int temp, i, aa, max_pos = 0,ind;
  int max_val=0;
  short Re,Im,ncoef;
#ifdef DEBUG_PHY
  char fname[100],vname[100];
#endif

  ncoef = 32768 - coef;

163
  for (ind=0; ind<number_of_cards; ind++) {
164
165
166
167
168

    if (ind==0)
      max_val=0;


169
    for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
170
      // do ifft of channel estimate
171
172
      switch(frame_parms->N_RB_DL) {
      case 6:
173
174
	dft128((int16_t*) &lte_eNB_srs->srs_ch_estimates[aa][0],
	       (int16_t*) lte_eNB_srs->srs_ch_estimates_time[aa],
175
176
177
	       1);
	break;
      case 25:
178
179
	dft512((int16_t*) &lte_eNB_srs->srs_ch_estimates[aa][0],
	       (int16_t*) lte_eNB_srs->srs_ch_estimates_time[aa],
180
181
182
	       1);
	break;
      case 50:
183
184
	dft1024((int16_t*) &lte_eNB_srs->srs_ch_estimates[aa][0],
		(int16_t*) lte_eNB_srs->srs_ch_estimates_time[aa],
185
186
187
		1);
	break;
      case 100:
188
189
	dft2048((int16_t*) &lte_eNB_srs->srs_ch_estimates[aa][0],
	       (int16_t*) lte_eNB_srs->srs_ch_estimates_time[aa],
190
191
192
	       1);
	break;
      }
193
194
195
#ifdef DEBUG_PHY
      sprintf(fname,"srs_ch_estimates_time_%d%d.m",ind,aa);
      sprintf(vname,"srs_time_%d%d",ind,aa);
196
      write_output(fname,vname,lte_eNB_srs->srs_ch_estimates_time[aa],frame_parms->ofdm_symbol_size*2,2,1);
197
198
199
200
201
202
203
#endif
    }

    // we only use channel estimates from tx antenna 0 here
    // remember we fixed the SRS to use only every second subcarriers
    for (i = 0; i < frame_parms->ofdm_symbol_size/2; i++) {
      temp = 0;
204
205

      for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
206
207
        Re = ((int16_t*)lte_eNB_srs->srs_ch_estimates_time[aa])[(i<<1)];
        Im = ((int16_t*)lte_eNB_srs->srs_ch_estimates_time[aa])[1+(i<<1)];
208
        temp += (Re*Re/2) + (Im*Im/2);
209
      }
210

211
      if (temp > max_val) {
212
213
214
        max_pos = i;
        max_val = temp;
        *eNB_id = ind;
215
216
217
218
219
220
221
222
223
      }
    }
  }

  // filter position to reduce jitter
  if (clear == 1)
    max_pos_fil2 = max_pos;
  else
    max_pos_fil2 = ((max_pos_fil2 * coef) + (max_pos * ncoef)) >> 15;
224

225
226
227
228
  return(max_pos_fil2);
}


229
int lte_est_timing_advance_pusch(PHY_VARS_eNB* eNB,uint8_t UE_id)
230
231
{
  int temp, i, aa, max_pos=0, max_val=0;
Cedric Roux's avatar
Cedric Roux committed
232
  short Re,Im;
233

234
235
  LTE_DL_FRAME_PARMS *frame_parms = &eNB->frame_parms;
  LTE_eNB_PUSCH *eNB_pusch_vars = eNB->pusch_vars[UE_id];
236
  int32_t **ul_ch_estimates_time=  eNB_pusch_vars->drs_ch_estimates_time;
237
  uint8_t cyclic_shift = 0;
knopp's avatar
   
knopp committed
238
  int sync_pos = (frame_parms->ofdm_symbol_size-cyclic_shift*frame_parms->ofdm_symbol_size/12)%(frame_parms->ofdm_symbol_size);
239

240
241
242
  AssertFatal(frame_parms->ofdm_symbol_size > 127,"frame_parms->ofdm_symbol_size %d<128\n",frame_parms->ofdm_symbol_size);
  AssertFatal(frame_parms->nb_antennas_rx >0 && frame_parms->nb_antennas_rx<3,"frame_parms->nb_antennas_rx %d not in [0,1]\n",
	      frame_parms->nb_antennas_rx);
243
  for (i = 0; i < frame_parms->ofdm_symbol_size; i++) {
244
245
246
    temp = 0;

    for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
247
248
      Re = ((int16_t*)ul_ch_estimates_time[aa])[(i<<1)];
      Im = ((int16_t*)ul_ch_estimates_time[aa])[1+(i<<1)];
249
250
251
252
253
254
255
      temp += (Re*Re/2) + (Im*Im/2);
    }

    if (temp > max_val) {
      max_pos = i;
      max_val = temp;
    }
256
257
  }

258
259
  if (max_pos>frame_parms->ofdm_symbol_size/2)
    max_pos = max_pos-frame_parms->ofdm_symbol_size;
260

261
  //#ifdef DEBUG_PHY
Cedric Roux's avatar
Cedric Roux committed
262
  LOG_D(PHY,"frame %d: max_pos = %d, sync_pos=%d\n",eNB->proc.frame_rx,max_pos,sync_pos);
263
  //#endif //DEBUG_PHY
264

Cedric Roux's avatar
Cedric Roux committed
265
  return max_pos - sync_pos;
266
}