lte_adjust_sync.c 7.02 KB
Newer Older
ghaddab's avatar
ghaddab committed
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
17
18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19
20
21
22
23
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
ghaddab's avatar
ghaddab committed
27
28

 *******************************************************************************/
29
30
31
32
#include "PHY/types.h"
#include "PHY/defs.h"
#include "PHY/extern.h"

33
#define DEBUG_PHY
34
35
36
37
38
39

// 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,
40
                      PHY_VARS_UE *ue,
41
42
43
                      unsigned char eNB_id,
                      unsigned char clear,
                      short coef)
44
45
46
{

  static int max_pos_fil = 0;
47
  int temp = 0, i, aa, max_val = 0, max_pos = 0;
48
49
50
51
52
53
  int diff;
  short Re,Im,ncoef;

  ncoef = 32767 - coef;

#ifdef DEBUG_PHY
54
  LOG_D(PHY,"frame %d, slot %d: rx_offset (before) = %d\n",ue->frame_rx,ue->slot_rx,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.dl_ch_estimates_time[eNB_id][aa])[(i<<2)];
      Im = ((int16_t*)ue->common_vars.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

81
  diff = max_pos_fil - frame_parms->nb_prefix_samples/8;
82
83

  if ( diff > SYNCH_HYST )
84
    ue->rx_offset++;
85
  else if (diff < -SYNCH_HYST)
86
    ue->rx_offset--;
87

88
89
  if ( ue->rx_offset < 0 )
    ue->rx_offset += FRAME_LENGTH_COMPLEX_SAMPLES;
90

91
92
  if ( ue->rx_offset >= FRAME_LENGTH_COMPLEX_SAMPLES )
    ue->rx_offset -= FRAME_LENGTH_COMPLEX_SAMPLES;
93

94

95
96

#ifdef DEBUG_PHY
97
  LOG_D(PHY,"frame %d: rx_offset (after) = %d : max_pos = %d,max_pos_fil = %d (peak %d)\n",
98
        ue->frame_rx,ue->rx_offset,max_pos,max_pos_fil,temp);
99
100
101
102
103
104
105
#endif //DEBUG_PHY


}


int lte_est_timing_advance(LTE_DL_FRAME_PARMS *frame_parms,
106
                           LTE_eNB_SRS *lte_eNB_srs,
107
108
109
110
                           unsigned int  *eNB_id,
                           unsigned char clear,
                           unsigned char number_of_cards,
                           short coef)
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

{

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

  ncoef = 32768 - coef;

126
  for (ind=0; ind<number_of_cards; ind++) {
127
128
129
130
131

    if (ind==0)
      max_val=0;


132
    for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
133
      // do ifft of channel estimate
134
135
      switch(frame_parms->N_RB_DL) {
      case 6:
136
137
	dft128((int16_t*) &lte_eNB_srs->srs_ch_estimates[ind][aa][0],
	       (int16_t*) lte_eNB_srs->srs_ch_estimates_time[ind][aa],
138
139
140
	       1);
	break;
      case 25:
141
142
	dft512((int16_t*) &lte_eNB_srs->srs_ch_estimates[ind][aa][0],
	       (int16_t*) lte_eNB_srs->srs_ch_estimates_time[ind][aa],
143
144
145
	       1);
	break;
      case 50:
146
147
	dft1024((int16_t*) &lte_eNB_srs->srs_ch_estimates[ind][aa][0],
		(int16_t*) lte_eNB_srs->srs_ch_estimates_time[ind][aa],
148
149
150
		1);
	break;
      case 100:
151
152
	dft2048((int16_t*) &lte_eNB_srs->srs_ch_estimates[ind][aa][0],
	       (int16_t*) lte_eNB_srs->srs_ch_estimates_time[ind][aa],
153
154
155
	       1);
	break;
      }
156
157
158
159
#ifdef USER_MODE
#ifdef DEBUG_PHY
      sprintf(fname,"srs_ch_estimates_time_%d%d.m",ind,aa);
      sprintf(vname,"srs_time_%d%d",ind,aa);
160
      write_output(fname,vname,lte_eNB_srs->srs_ch_estimates_time[ind][aa],frame_parms->ofdm_symbol_size*2,2,1);
161
162
163
164
165
166
167
168
#endif
#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;
169
170

      for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
171
172
        Re = ((int16_t*)lte_eNB_srs->srs_ch_estimates_time[ind][aa])[(i<<1)];
        Im = ((int16_t*)lte_eNB_srs->srs_ch_estimates_time[ind][aa])[1+(i<<1)];
173
        temp += (Re*Re/2) + (Im*Im/2);
174
      }
175

176
      if (temp > max_val) {
177
178
179
        max_pos = i;
        max_val = temp;
        *eNB_id = ind;
180
181
182
183
184
185
186
187
188
      }
    }
  }

  // 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;
189

190
191
192
#ifdef DEBUG_PHY
  //LOG_D(PHY,"frame %d: max_pos = %d, max_pos_fil = %d\n",mac_xface->frame,max_pos,max_pos_fil2);
#endif //DEBUG_PHY
193

194
195
196
197
  return(max_pos_fil2);
}


198
int lte_est_timing_advance_pusch(PHY_VARS_eNB* eNB,uint8_t UE_id)
199
200
201
202
203
204
205
{
  static int first_run=1;
  static int max_pos_fil2=0;
  int temp, i, aa, max_pos=0, max_val=0;
  short Re,Im,coef=24576;
  short ncoef = 32768 - coef;

206
207
  LTE_DL_FRAME_PARMS *frame_parms = &eNB->frame_parms;
  LTE_eNB_PUSCH *eNB_pusch_vars = eNB->pusch_vars[UE_id];
gauthier's avatar
gauthier committed
208
  int32_t **ul_ch_estimates_time=  eNB_pusch_vars->drs_ch_estimates_time[0];
209
  uint8_t cyclic_shift = 0;
knopp's avatar
   
knopp committed
210
  int sync_pos = (frame_parms->ofdm_symbol_size-cyclic_shift*frame_parms->ofdm_symbol_size/12)%(frame_parms->ofdm_symbol_size);
211
212
213


  for (i = 0; i < frame_parms->ofdm_symbol_size; i++) {
214
215
216
    temp = 0;

    for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
217
218
      Re = ((int16_t*)ul_ch_estimates_time[aa])[(i<<1)];
      Im = ((int16_t*)ul_ch_estimates_time[aa])[1+(i<<1)];
219
220
221
222
223
224
225
      temp += (Re*Re/2) + (Im*Im/2);
    }

    if (temp > max_val) {
      max_pos = i;
      max_val = temp;
    }
226
227
  }

228
229
  if (max_pos>frame_parms->ofdm_symbol_size/2)
    max_pos = max_pos-frame_parms->ofdm_symbol_size;
230
231

  // filter position to reduce jitter
232
  if (first_run == 1) {
233
234
    first_run=0;
    max_pos_fil2 = max_pos;
235
  } else
236
    max_pos_fil2 = ((max_pos_fil2 * coef) + (max_pos * ncoef)) >> 15;
237

238
#ifdef DEBUG_PHY
239
  LOG_D(PHY,"frame %d: max_pos = %d, max_pos_fil = %d, sync_pos=%d\n",eNB->proc.frame_rx,max_pos,max_pos_fil2,sync_pos);
240
241
242
243
#endif //DEBUG_PHY

  return(max_pos_fil2-sync_pos);
}