summaryrefslogtreecommitdiff
path: root/src/lib/gsm_receiver_cf.cc
blob: 3b0e426d387bf954cf64633d5f957dc921629148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
132
133
134
135
136
137
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
303
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* -*- c++ -*- */
/*
 * Copyright 2004 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio 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, or (at your option)
 * any later version.
 *
 * GNU Radio 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
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gsm_receiver_cf.h>
#include <gr_io_signature.h>
#include <gr_math.h>
#include <math.h>
#include <Assert.h>
#include <algorithm>

#define SAFETY_MARGIN 50
#define BUFFER_SIZE (FCCH_HITS_NEEDED)

gsm_receiver_cf_sptr
gsm_make_receiver_cf(gr_feval_dd *tuner, int osr)
{
  return gsm_receiver_cf_sptr(new gsm_receiver_cf(tuner, osr));
}

static const int MIN_IN = 1; // mininum number of input streams
static const int MAX_IN = 1; // maximum number of input streams
static const int MIN_OUT = 0; // minimum number of output streams
static const int MAX_OUT = 1; // maximum number of output streams

/*
 * The private constructor
 */
gsm_receiver_cf::gsm_receiver_cf(gr_feval_dd *tuner, int osr)
    : gr_block("gsm_receiver",
               gr_make_io_signature(MIN_IN, MAX_IN, sizeof(gr_complex)),
               gr_make_io_signature(MIN_OUT, MAX_OUT, 142 * sizeof(float))),
    d_OSR(osr),
    d_tuner(tuner),
    d_prev_freq_offset(0),
    d_phase_diff_buffer(BUFFER_SIZE * osr),
    d_counter(0),
    d_x_temp(0),
    d_x2_temp(0),
    d_fcch_count(0),
    d_fcch_start_pos(0),
    d_state(fcch_search)
{
  gmsk_mapper(SYNC_BITS, d_sch_training_seq, N_SYNC_BITS);
}

/*
 * Virtual destructor.
 */
gsm_receiver_cf::~gsm_receiver_cf()
{
}

void gsm_receiver_cf::forecast(int noutput_items, gr_vector_int &ninput_items_required)
{
  ninput_items_required[0] = noutput_items * TS_BITS; //TODO include oversampling ratio here
}

int
gsm_receiver_cf::general_work(int noutput_items,
                              gr_vector_int &ninput_items,
                              gr_vector_const_void_star &input_items,
                              gr_vector_void_star &output_items)
{
  const gr_complex *in = (const gr_complex *) input_items[0];
  float *out = (float *) output_items[0];
  int produced_out = 0;

  switch (d_state) {

    case fcch_search:

      if (find_fcch_burst(in, ninput_items[0])) {
        produced_out = 1;
        d_state = sch_search;
      } else {
        produced_out = 0;
        d_state = fcch_search;
      }

      break;

    case sch_search:

      if (find_sch_burst(in, ninput_items[0], out)) {

        d_state = sch_search;
      } else {
        for (int i = 0; i < ninput_items[0] - N_SYNC_BITS; i++) {
          gr_complex result;
          d_counter++;
          int ninput = N_SYNC_BITS - 10;
          const gr_complex * input_signal = in + i;
          gr_complex * sequence = &d_sch_training_seq[5];
          result =  correlation(&d_sch_training_seq[5], in + i, N_SYNC_BITS-10);
           if (abs(result) > 60000) {
             std::cout << "Max val: " << abs(result) << " position: " << d_counter - d_fcch_start_pos << std::endl;
// 
//             for (int j = 1; j <= 54; j++) {
//               std::cout << sequence[j-1] << " * " << conj(input_signal[(j * d_OSR)]) << std::endl;
//             }
// 
//             gr_complex result2(0, 0);
// 
//             for (int j = 1; j <= 54; j++) {
//               result2 +=  sequence[j-1] *  conj(input_signal[(j * d_OSR)]);
//             }
// 
//             std::cout << "Spr: " << abs(result2) << std::endl;
// 
//             std::cout << std::endl;
           }


//          std::cout << "(" << real(in[i]) << "," << imag(in[i]) << ")\n";
//          std::cout << "(" << real(result) << "," << imag(result) << ")\n";
        }

        consume_each(ninput_items[0]);

        d_state = sch_search;
      }

      break;
  }

//  for (int i = 0; i < TS_BITS; i++) {
//    out[i] = d_phase_diff_buffer[i+start_pos-USEFUL_BITS];
//  }

  return produced_out;
}

bool gsm_receiver_cf::find_fcch_burst(const gr_complex *in, const int nitems)
{
  float phase_diff = 0;
  gr_complex conjprod;
  int hit_count = 0;
  int miss_count = 0;
  int start_pos;
  float min_phase_diff, max_phase_diff, lowest_max_min_diff;

  int to_consume = 0;
  int sample_number = 0;
  bool end = false;
  bool result = false;

  circular_buffer_float::iterator buffer_iter;

  enum states {
    init, search, found_something, fcch_found, search_fail
  } fcch_search_state;

  fcch_search_state = init;

  while (!end) {
    switch (fcch_search_state) {

      case init:
        hit_count = 0;
        miss_count = 0;
        start_pos = -1;
        lowest_max_min_diff = 99999;
        d_phase_diff_buffer.clear();
        fcch_search_state = search;

        break;

      case search:
        sample_number++;

        if (sample_number > nitems - BUFFER_SIZE * d_OSR) {
          to_consume = sample_number;
          fcch_search_state = search_fail;
        } else {

          conjprod = in[sample_number] * conj(in[sample_number-1]);
          phase_diff = gr_fast_atan2f(imag(conjprod), real(conjprod));

          if (phase_diff > 0) {
            to_consume = sample_number;
            fcch_search_state = found_something;
          } else {
            fcch_search_state = search;
          }
        }

        break;

      case found_something:

        if (phase_diff > 0) {
          hit_count++;
        } else {
          miss_count++;
        }

        if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR)) {
          fcch_search_state = init;
          //  DCOUT("hit_count: " << hit_count << " miss_count: " << miss_count << " d_counter: " << d_counter);
          continue;
        } else if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) {
          fcch_search_state = fcch_found;
          continue;
        } else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) {
          //find difference between minimal and maximal element in the buffer
          //for FCCH this value should be low
          //this part is searching for a region where this value is lowest
          min_phase_diff = *(min_element(d_phase_diff_buffer.begin(), d_phase_diff_buffer.end()));
          max_phase_diff = *(max_element(d_phase_diff_buffer.begin(), d_phase_diff_buffer.end()));

          if (lowest_max_min_diff > max_phase_diff - min_phase_diff) {
            lowest_max_min_diff = max_phase_diff - min_phase_diff;
            start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR;
            d_best_sum = 0;

            for (buffer_iter = (d_phase_diff_buffer.begin());
                 buffer_iter != (d_phase_diff_buffer.end());
                 buffer_iter++) {
              d_best_sum += *buffer_iter - (M_PI / 2) / d_OSR;
            }
          }
        }

        sample_number++;

        if (sample_number >= nitems) {
          fcch_search_state = search_fail;
          continue;
        }

        conjprod = in[sample_number] * conj(in[sample_number-1]);

        phase_diff = gr_fast_atan2f(imag(conjprod), real(conjprod));
        d_phase_diff_buffer.push_back(phase_diff);

        fcch_search_state = found_something;

        break;

      case fcch_found:
        DCOUT("znalezione fcch na pozycji" << d_counter + start_pos);
        to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1;
        /*  mean = d_best_sum / FCCH_HITS_NEEDED;
        phase_offset = mean - (M_PI / 2);
        freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);*/
        d_fcch_start_pos = d_counter + start_pos;
        compute_freq_offset();
        end = true;
        result = true;
        break;

      case search_fail:
        end = true;
        result = false;
        break;
    }
  }

  //  DCOUT("hit_count: " << hit_count << " miss_count: " << miss_count << " d_counter: " << d_counter);

  d_counter += to_consume;

  consume_each(to_consume);

  return result;
}


double gsm_receiver_cf::compute_freq_offset()
{
  float mean, phase_offset, freq_offset;

  DCOUT(" d_phase_diff_buffer.size(): " <<  d_phase_diff_buffer.size());

  //mean = d_best_sum / FCCH_HITS_NEEDED;
  phase_offset = d_best_sum / FCCH_HITS_NEEDED;
  //phase_offset = mean - ;
  freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
  DCOUT("freq_offset: " << freq_offset << " d_best_sum: " << d_best_sum);
  d_fcch_count++;
  d_x_temp += freq_offset;
  d_x2_temp += freq_offset * freq_offset;
  d_mean = d_x_temp / d_fcch_count;

  set_frequency(freq_offset);
  d_prev_freq_offset -= freq_offset;

  DCOUT("wariancja: " << sqrt((d_x2_temp / d_fcch_count - d_mean * d_mean)) << " fcch_count:" << d_fcch_count << " d_mean: " << d_mean);

  return freq_offset;
}

void gsm_receiver_cf::set_frequency(double freq_offset)
{
  d_tuner->calleval(freq_offset);
}

bool gsm_receiver_cf::find_sch_burst(const gr_complex *in, const int nitems , float *out)
{
  int sample_number = 0;
  int to_consume = 0;
  bool end = false;
  bool result = false;
  int sch_start = d_fcch_start_pos + FRAME_BITS * d_OSR;

  enum states {
    init, search, sch_found, search_fail
  } sch_search_state;

  while (!end) {
    switch (sch_search_state) {

      case init:
        sch_search_state = search_fail;
        break;

      case search:

        if ((sch_start >= d_counter) && (sch_start <= d_counter + nitems)) {
          DCOUT("sch_start-d_counter: " << sch_start - d_counter);
          /*        for (int i = 0; i < nitems - N_SYNC_BITS; i++) {
                    gr_complex result =  correlation(&d_sch_training_seq[5], in + i, N_SYNC_BITS-10);
                    //std::cout << "(" << real(in[i]) << "," << imag(in[i]) << ")\n";
                    std::cout << "(" << real(result) << "," << imag(result) << ")\n";
                  }
                  std::cout << std::endl;
          */
        }

        to_consume = nitems - N_SYNC_BITS;

        sch_search_state = search_fail;
        break;

      case sch_found:
        end = true;
        break;

      case search_fail:
        end = true;
        break;
    }
  }

  d_counter += to_consume;

//  consume_each(to_consume);

  return result;
}

void gsm_receiver_cf::gmsk_mapper(const int * input, gr_complex * output, int ninput)
{
  gr_complex j = gr_complex(0.0, 1.0);

  int current_symbol;
  int encoded_symbol;
  int previous_symbol = 2 * input[0] - 1;
  output[0] = gr_complex(1.0, 0.0);

  for (int i = 1; i < ninput; i++) {
    //change bits representation to NRZ
    current_symbol = 2 * input[i] - 1;
    //differentially encode
    encoded_symbol = current_symbol * previous_symbol;
    //and do gmsk mapping
    output[i] = j * gr_complex(encoded_symbol, 0.0) * output[i-1];
    previous_symbol = current_symbol;
  }
}

gr_complex gsm_receiver_cf::correlation(const gr_complex * sequence, const gr_complex * input_signal, int ninput)
{
  gr_complex result(0.0, 0.0);

  for (int ii = 1; (ii * d_OSR) <= ninput; ii++) {
    result += sequence[ii-1] * conj(input_signal[(ii * d_OSR)]);
  }

  return result;
}
personal git repositories of Harald Welte. Your mileage may vary