summaryrefslogtreecommitdiff
path: root/src/lib/gsm_receiver_cf.cc
blob: 5453253064ce5c581809eddf80d7e33aa52c4c91 (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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
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
552
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
/* -*- 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 <gr_io_signature.h>
#include <gr_math.h>
#include <math.h>
#include <Assert.h>
#include <list>
#include <boost/circular_buffer.hpp>
#include <algorithm>
#include <gsm_receiver_cf.h>
#include <viterbi_detector.h>
#include <sch.h>

#define FCCH_BUFFER_SIZE (FCCH_HITS_NEEDED)
#define SYNC_SEARCH_RANGE 40

//TODO !! - move this methods to some else place

// - move it to some else place !!
typedef std::list<float> list_float;
typedef std::vector<float> vector_float;

typedef boost::circular_buffer<float> circular_buffer_float;

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_chan_imp_length(CHAN_IMP_RESP_LENGTH),
    d_tuner(tuner),
    d_counter(0),
    d_fcch_start_pos(0),
    d_freq_offset(0),
    d_state(first_fcch_search)
//    d_fcch_count(0), //!!
//    d_x_temp(0),//!!
//    d_x2_temp(0)//!!
{
  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 + 2 * SAFETY_MARGIN) * d_OSR;
}

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;
  float prev_freq_offset;

  switch (d_state) {
      //bootstrapping
    case first_fcch_search:
      if (find_fcch_burst(in, ninput_items[0])) {
        set_frequency(d_freq_offset);
        produced_out = 0;
        d_state = next_fcch_search;
      } else {
        produced_out = 0;
        d_state = first_fcch_search;
      }
      break;

    case next_fcch_search:
      prev_freq_offset = d_freq_offset;
      if (find_fcch_burst(in, ninput_items[0])) {
        if (abs(d_freq_offset) > 100) {
          set_frequency(d_freq_offset);
        }
        produced_out = 0;
        d_state = sch_search;
      } else {
        produced_out = 0;
        d_state = next_fcch_search;
      }
      break;

    case sch_search:
      if (find_sch_burst(in, ninput_items[0], out)) {
        d_channel_conf.set_multiframe_type(0, multiframe_51);
        d_channel_conf.set_burst_types(0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
        d_channel_conf.set_burst_types(0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
        d_burst_nr++;
        d_state = synchronized;
      } else {
        d_state = sch_search;
      }
      break;

      //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
    case synchronized: {
        gr_complex chan_imp_resp[100];//!!
        burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
        int burst_start;
        int offset = 0;
        int to_consume = 0;
        unsigned char output_binary[BURST_SIZE];

        switch (b_type) {
          case fcch_burst: {
              int ii;
              int first_sample = (GUARD_BITS+TAIL_BITS)*d_OSR;
              int last_sample = first_sample+USEFUL_BITS*d_OSR;
              double phase_sum = 0;
              for(ii = first_sample; ii < last_sample; ii++){
                phase_sum += compute_phase_diff(in[ii], in[ii-1]) - (M_PI / 2) / d_OSR;
              }
              std::cout << "freq_offset: " << d_freq_offset << "\n";

              double freq_offset = compute_freq_offset(phase_sum, USEFUL_BITS-TAIL_BITS);
              d_freq_offset -= freq_offset;
              set_frequency(d_freq_offset);
              std::cout << "freq_offset: " << d_freq_offset << "\n";
            }
            break;
          case sch_burst: {
              int t1, t2, t3, d_ncc, d_bcc;
              burst_start = get_sch_chan_imp_resp(in, chan_imp_resp);
              detect_burst(in, chan_imp_resp, burst_start, output_binary);
              if(decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0){
//                d_burst_nr.set(t1, t2, t3, 0);
                printf("bcc: %d, ncc: %d, t1: %d, t2: %d, t3: %d\n", d_bcc, d_ncc, t1, t2, t3);
                offset =  burst_start - GUARD_BITS * d_OSR;
                to_consume += offset;
              }
              std::cout << offset << std::endl;
            }
            break;
          case normal_burst:

            break;

          case rach_burst:
            break;
          case dummy:
            break;
          case empty:
            break;
        }

        d_burst_nr++;
        to_consume += floor(TS_BITS * d_OSR);
        consume_each(to_consume);
      }
      break;
  }

  return produced_out;
}

bool gsm_receiver_cf::find_fcch_burst(const gr_complex *in, const int nitems)
{
  circular_buffer_float phase_diff_buffer(FCCH_BUFFER_SIZE * d_OSR);

  float phase_diff = 0;
  gr_complex conjprod;
  int hit_count = 0;
  int miss_count = 0;
  int start_pos = -1;
  float min_phase_diff;
  float max_phase_diff;
  double best_sum;
  float lowest_max_min_diff = 99999;

  int to_consume = 0;
  int sample_number = 0;
  bool end = false;
  bool result = false;
  double freq_offset;
  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;
        phase_diff_buffer.clear();
        fcch_search_state = search;

        break;

      case search:
        sample_number++;

        if (sample_number > nitems - FCCH_BUFFER_SIZE * d_OSR) {
          to_consume = sample_number;
          fcch_search_state = search_fail;
        } else {
          phase_diff = compute_phase_diff(in[sample_number], in[sample_number-1]);

          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;
          continue;
        } else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * 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(phase_diff_buffer.begin(), phase_diff_buffer.end()));
          max_phase_diff = *(max_element(phase_diff_buffer.begin(), 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;
            best_sum = 0;

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

        sample_number++;

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

        phase_diff = compute_phase_diff(in[sample_number], in[sample_number-1]);
        phase_diff_buffer.push_back(phase_diff);
        fcch_search_state = found_something;

        break;

      case fcch_found:
        DCOUT("fcch found on position: " << d_counter + start_pos);
        to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1;

        d_fcch_start_pos = d_counter + start_pos;
        freq_offset = compute_freq_offset(best_sum, FCCH_HITS_NEEDED);
        d_freq_offset -= freq_offset;

        end = true;
        result = true;
        break;

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

  d_counter += to_consume;
  consume_each(to_consume);

  return result;
}

double gsm_receiver_cf::compute_freq_offset(double best_sum, unsigned denominator)
{
  float phase_offset, freq_offset;

  phase_offset = best_sum / denominator;
  freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);

//  d_fcch_count++;
//   d_x_temp += freq_offset;
//   d_x2_temp += freq_offset * freq_offset;
//   d_mean = d_x_temp / d_fcch_count;

  DCOUT("freq_offset: " << freq_offset );//" best_sum: " << best_sum
//   DCOUT("wariance: " << 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);
}

inline float gsm_receiver_cf::compute_phase_diff(gr_complex val1, gr_complex val2)
{
  gr_complex conjprod = val1 * conj(val2);
  return gr_fast_atan2f(imag(conjprod), real(conjprod));
}

bool gsm_receiver_cf::find_sch_burst(const gr_complex *in, const int nitems , float *out)
{
  int to_consume = 0;
  bool end = false;
  bool result = false;
  int sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
  gr_complex chan_imp_resp[CHAN_IMP_RESP_LENGTH*d_OSR];
//   vector_complex correlation_buffer;
//   vector_float power_buffer;
//   vector_float window_energy_buffer;

  enum states {
    start, reach_sch, find_sch_start, detect_and_decode_sch, search_not_finished, sch_found
  } sch_search_state;

  sch_search_state = start;
  int t1, t2, t3;
  int burst_start = 0;
  unsigned char output_binary[BURST_SIZE];

  while (!end) {
    switch (sch_search_state) {

      case start:
        if (d_counter < sample_nr_near_sch_start) {
          sch_search_state = reach_sch;
        } else {
          sch_search_state = find_sch_start;
        }
        break;

      case reach_sch:
        if (d_counter + nitems >= sample_nr_near_sch_start) {
          to_consume = sample_nr_near_sch_start - d_counter;
        } else {
          to_consume = nitems;
        }
        sch_search_state = search_not_finished;
        break;

      case find_sch_start:
        burst_start = get_sch_chan_imp_resp(in, chan_imp_resp);
        sch_search_state = detect_and_decode_sch;
        break;
      case detect_and_decode_sch:
        detect_burst(in, chan_imp_resp, burst_start, output_binary);
        decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc);
        d_burst_nr.set(t1, t2, t3, 0);

        printf("bcc: %d, ncc: %d, t1: %d, t2: %d, t3: %d\n", d_bcc, d_ncc, t1, t2, t3);
        to_consume += burst_start + BURST_SIZE * d_OSR;
        sch_search_state = sch_found;
        break;

      case search_not_finished:
        result = false;
        end = true;
        break;

      case sch_found:
        result = true;
        end = true;
        break;
    }
  }

  d_counter += to_consume;
  consume_each(to_consume);
  return result;
}

int gsm_receiver_cf::get_sch_chan_imp_resp(const gr_complex *in, gr_complex * chan_imp_resp)
{
  vector_complex correlation_buffer;
  vector_float power_buffer;
  vector_float window_energy_buffer;

  int strongest_window_nr;
  int burst_start = 0;
  int chan_imp_resp_center;
  float max_correlation = 0;
  float energy = 0;

  for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE)*d_OSR; ii++) {
    gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], &in[ii], N_SYNC_BITS - 10);
    correlation_buffer.push_back(correlation);
    power_buffer.push_back(pow(abs(correlation), 2));
  }

  //compute window energies
  vector_float::iterator iter = power_buffer.begin();
  bool loop_end = false;
  while (iter != power_buffer.end()) {
    vector_float::iterator iter_ii = iter;
    energy = 0;

    for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++, iter_ii++) {
      if (iter_ii == power_buffer.end()) {
        loop_end = true;
        break;
      }
      energy += (*iter_ii);
    }
    if (loop_end) {
      break;
    }
    iter++;
    window_energy_buffer.push_back(energy);
  }

  strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
  d_channel_imp_resp.clear();

  max_correlation = 0;
  for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++) {
    gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
    if (abs(correlation) > max_correlation) {
      chan_imp_resp_center = ii;
      max_correlation = abs(correlation);
    }
    d_channel_imp_resp.push_back(correlation);
    chan_imp_resp[ii] = correlation;
  }

  burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
  return burst_start;
}

void gsm_receiver_cf::detect_burst(const gr_complex * in, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
{
  float output[BURST_SIZE];
  gr_complex rhh_temp[CHAN_IMP_RESP_LENGTH*d_OSR];
  gr_complex rhh[CHAN_IMP_RESP_LENGTH];
  gr_complex filtered_burst[BURST_SIZE];
  int start_state = 3;
  unsigned int stop_states[2] = {4, 12};

  autocorrelation(chan_imp_resp, rhh_temp, d_chan_imp_length*d_OSR);
  for (int ii = 0; ii < (d_chan_imp_length); ii++) {
    rhh[ii] = conj(rhh_temp[ii*d_OSR]);
  }

  mafi(&in[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);

  viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);

  for (int i = 0; i < BURST_SIZE ; i++) {
    output_binary[i] = (output[i] > 0);
  }
}

//TODO consider placing this funtion in a separate class for signal processing
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;
  }
}

//TODO consider use of some generalized function for correlation and placing it in a separate class  for signal processing
gr_complex gsm_receiver_cf::correlate_sequence(const gr_complex * sequence, const gr_complex * input_signal, int length)
{
  gr_complex result(0.0, 0.0);
  int sample_number = 0;

  for (int ii = 0; ii < length; ii++) {
    sample_number = (ii * d_OSR) ;
    result += sequence[ii] * conj(input_signal[sample_number]);
  }

  result = result / gr_complex(length, 0);
  return result;
}

// gr_complex gsm_receiver_cf::compute_energy(const gr_complex * input_signal, int length)
// {
//   float result = 0;
//   int sample_number = 0;
//
//   for (int ii = 0; ii < length; ii++) {
//     result += input_signal[(ii * d_OSR)];
//   }
//
//   return result;
// }

//computes autocorrelation for positive values
//TODO consider placing this funtion in a separate class for signal processing
inline void gsm_receiver_cf::autocorrelation(const gr_complex * input, gr_complex * out, int length)
{
  int i, k;
  for (k = length - 1; k >= 0; k--) {
    out[k] = gr_complex(0, 0);
    for (i = k; i < length; i++) {
      out[k] += input[i] * conj(input[i-k]);
    }
  }
}

//TODO consider use of some generalized function for filtering and placing it in a separate class  for signal processing
//funkcja matched filter
inline void gsm_receiver_cf::mafi(const gr_complex * input, int input_length, gr_complex * filter, int filter_length, gr_complex * output)
{
  int ii = 0, n, a;

  for (n = 0; n < input_length; n++) {
    a = n * d_OSR;
    output[n] = 0;
    ii = 0;

    while (ii < filter_length) {
      if ((a + ii) >= input_length*d_OSR)
        break;
      output[n] += input[a+ii] * filter[ii]; //!!conj
      ii++;
    }
    output[n] = output[n] * gr_complex(0, -1);//!!this shouldn't be here
  }
}
personal git repositories of Harald Welte. Your mileage may vary