blob: 689079b8d5d9c61652ba501a0dd7fdf12d955dab [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001/* -*- c++ -*- */
2/*
3 * Copyright 2014 <+YOU OR YOUR COMPANY+>.
4 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
8 * any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <gnuradio/io_signature.h>
26#include "receiver_impl.h"
27
28#include <gnuradio/io_signature.h>
29#include <gnuradio/math.h>
30#include <math.h>
31#include <boost/circular_buffer.hpp>
32#include <algorithm>
33#include <numeric>
34#include <viterbi_detector.h>
35#include <string.h>
36#include <sch.h>
37#include <iostream>
38#include <iomanip>
39
40#include <assert.h>
41
42#define SYNC_SEARCH_RANGE 30
43
44namespace gr {
45 namespace gsm {
46
47 typedef std::list<float> list_float;
48 typedef std::vector<float> vector_float;
49
50 typedef boost::circular_buffer<float> circular_buffer_float;
51
52 receiver::sptr
53 receiver::make(feval_dd * tuner, int osr)
54 {
55 return gnuradio::get_initial_sptr
56 (new receiver_impl(tuner, osr));
57 }
58
59 /*
60 * The private constructor
61 */
62 receiver_impl::receiver_impl(feval_dd * tuner, int osr)
63 : gr::block("receiver",
64 gr::io_signature::make(1, 1, sizeof(gr_complex)),
65 gr::io_signature::make(0, 1, 142 * sizeof(float))),
66 d_OSR(osr),
67 d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
68 d_tuner(tuner),
69 d_counter(0),
70 d_fcch_start_pos(0),
71 d_freq_offset(0),
72 d_state(first_fcch_search),
73 d_burst_nr(osr),
74 d_failed_sch(0)
75 {
76 int i;
77 gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0));
78 for (i = 0; i < TRAIN_SEQ_NUM; i++) {
79 gr_complex startpoint;
80 if (i == 6 || i == 7) { //this is nasty hack
81 startpoint = gr_complex(-1.0, 0.0); //if I don't change it here all bits of normal bursts for BTSes with bcc=6 will have reversed values
82 } else {
83 startpoint = gr_complex(1.0, 0.0); //I've checked this hack for bcc==0,1,2,3,4,6
84 } //I don't know what about bcc==5 and 7 yet
85 //TODO:find source of this situation - this is purely mathematical problem I guess
86
87 gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint);
88 }
89 }
90
91 /*
92 * Our virtual destructor.
93 */
94 receiver_impl::~receiver_impl()
95 {
96 }
97
98 void receiver_impl::forecast(int noutput_items, gr_vector_int &ninput_items_required)
99 {
100 ninput_items_required[0] = noutput_items * floor((TS_BITS + 2 * GUARD_PERIOD) * d_OSR);
101 }
102
103
104 int
105 receiver_impl::general_work(int noutput_items,
106 gr_vector_int &ninput_items,
107 gr_vector_const_void_star &input_items,
108 gr_vector_void_star &output_items)
109 {
110 const gr_complex *input = (const gr_complex *) input_items[0];
111 //float *out = (float *) output_items[0];
112 int produced_out = 0; //how many output elements were produced - this isn't used yet
113 //probably the gsm receiver will be changed into sink so this variable won't be necessary
114 switch (d_state) {
115 //bootstrapping
116 case first_fcch_search:
117 if (find_fcch_burst(input, ninput_items[0])) { //find frequency correction burst in the input buffer
118 set_frequency(d_freq_offset); //if fcch search is successful set frequency offset
119 //produced_out = 0;
120 d_state = next_fcch_search;
121 } else {
122 //produced_out = 0;
123 d_state = first_fcch_search;
124 }
125 break;
126
127 case next_fcch_search: { //this state is used because it takes some time (a bunch of buffered samples)
128 COUT("fcch");
129 float prev_freq_offset = d_freq_offset; //before previous set_frequqency cause change
130 if (find_fcch_burst(input, ninput_items[0])) {
131 if (abs(prev_freq_offset - d_freq_offset) > FCCH_MAX_FREQ_OFFSET) {
132 set_frequency(d_freq_offset); //call set_frequncy only frequency offset change is greater than some value
133 }
134 //produced_out = 0;
135 d_state = sch_search;
136 } else {
137 //produced_out = 0;
138 d_state = next_fcch_search;
139 }
140 break;
141 }
142
143
144 case sch_search: {
145 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
146 int t1, t2, t3;
147 int burst_start = 0;
148 unsigned char output_binary[BURST_SIZE];
149
150 if (reach_sch_burst(ninput_items[0])) { //wait for a SCH burst
151 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it
152 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection
153 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) { //decode SCH burst
154 COUT("sch burst_start: " << burst_start);
155 COUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3);
156 d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value
157
158 //configure the receiver - tell him where to find which burst type
159 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51); //in the timeslot nr.0 bursts changes according to t3 counter
160 configure_receiver();//TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready
161 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst); //tell where to find fcch bursts
162 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst); //sch bursts
163 d_channel_conf.set_burst_types(TIMESLOT0, BCCH_FRAMES, sizeof(BCCH_FRAMES) / sizeof(unsigned), normal_burst);//!and maybe normal bursts of the BCCH logical channel
164 d_burst_nr++;
165
166 consume_each(burst_start + BURST_SIZE * d_OSR); //consume samples up to next guard period
167 d_state = synchronized;
168 } else {
169 d_state = next_fcch_search; //if there is error in the sch burst go back to fcch search phase
170 }
171 } else {
172 d_state = sch_search;
173 }
174 break;
175 }
176 //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
177 case synchronized: {
178 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
179 int burst_start;
180 int offset = 0;
181 int to_consume = 0;
182 unsigned char output_binary[BURST_SIZE];
183
184 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number
185
186 switch (b_type) {
187 case fcch_burst: { //if it's FCCH burst
188 const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
189 const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
190 double freq_offset = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it
191
192 d_freq_offset_vals.push_front(freq_offset);
193 //process_normal_burst(d_burst_nr, fc_fb);
194 if (d_freq_offset_vals.size() >= 10) {
195 double sum = std::accumulate(d_freq_offset_vals.begin(), d_freq_offset_vals.end(), 0);
196 double mean_offset = sum / d_freq_offset_vals.size(); //compute mean
197 d_freq_offset_vals.clear();
198 if (abs(mean_offset) > FCCH_MAX_FREQ_OFFSET) {
199 d_freq_offset -= mean_offset; //and adjust frequency if it have changed beyond
200 set_frequency(d_freq_offset); //some limit
201 DCOUT("mean_offset: " << mean_offset);
202 DCOUT("Adjusting frequency, new frequency offset: " << d_freq_offset << "\n");
203 }
204 }
205 }
206 break;
207 case sch_burst: { //if it's SCH burst
208 int t1, t2, t3, d_ncc, d_bcc;
209 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response
210 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits
211 //process_normal_burst(d_burst_nr, output_binary);
212 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) { //and decode SCH data
213 // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct
214 d_failed_sch = 0;
215 DCOUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3);
216 offset = burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period
217 DCOUT(offset);
218 to_consume += offset; //adjust with offset number of samples to be consumed
219 } else {
220 d_failed_sch++;
221 if (d_failed_sch >= MAX_SCH_ERRORS) {
222 // d_state = next_fcch_search; //TODO: this isn't good, the receiver is going wild when it goes back to next_fcch_search from here
223 // d_freq_offset_vals.clear();
224 DCOUT("many sch decoding errors");
225 }
226 }
227 }
228 break;
229
230 case normal_burst: //if it's normal burst
231 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], d_bcc); //get channel impulse response for given training sequence number - d_bcc
232 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits
233 process_normal_burst(d_burst_nr, output_binary); //TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready
234 break;
235
236 case dummy_or_normal: {
237 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], TS_DUMMY);
238 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
239
240 std::vector<unsigned char> v(20);
241 std::vector<unsigned char>::iterator it;
242 it = std::set_difference(output_binary + TRAIN_POS, output_binary + TRAIN_POS + 16, &train_seq[TS_DUMMY][5], &train_seq[TS_DUMMY][21], v.begin());
243 int different_bits = (it - v.begin());
244
245 if (different_bits > 2) {
246 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], d_bcc);
247 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
248 //if (!output_binary[0] && !output_binary[1] && !output_binary[2]) {
249 COUT("Normal burst");
250 process_normal_burst(d_burst_nr, output_binary); //TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready
251 //}
252 } else {
253 //process_normal_burst(d_burst_nr, dummy_burst);
254 }
255 }
256 case rach_burst:
257 //implementation of this channel isn't possible in current gsm_receiver
258 //it would take some realtime processing, counter of samples from USRP to
259 //stay synchronized with this device and possibility to switch frequency from uplink
260 //to C0 (where sch is) back and forth
261
262 break;
263 case dummy: //if it's dummy
264 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], TS_DUMMY); //read dummy
265 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); // but as far as I know it's pointless
266 break;
267 case empty: //if it's empty burst
268 break; //do nothing
269 }
270
271 d_burst_nr++; //go to next burst
272
273 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); //consume samples of the burst up to next guard period
274 //and add offset which is introduced by
275 //0.25 fractional part of a guard period
276 //burst_number computes this offset
277 //but choice of this class to do this was random
278 consume_each(to_consume);
279 }
280 break;
281 }
282
283 return produced_out;
284 }
285
286
287 bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems)
288 {
289 circular_buffer_float phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR); //circular buffer used to scan throug signal to find
290 //best match for FCCH burst
291 float phase_diff = 0;
292 gr_complex conjprod;
293 int start_pos = -1;
294 int hit_count = 0;
295 int miss_count = 0;
296 float min_phase_diff;
297 float max_phase_diff;
298 double best_sum = 0;
299 float lowest_max_min_diff = 99999;
300
301 int to_consume = 0;
302 int sample_number = 0;
303 bool end = false;
304 bool result = false;
305 circular_buffer_float::iterator buffer_iter;
306
307 /**@name Possible states of FCCH search algorithm*/
308 //@{
309 enum states {
310 init, ///< initialize variables
311 search, ///< search for positive samples
312 found_something, ///< search for FCCH and the best position of it
313 fcch_found, ///< when FCCH was found
314 search_fail ///< when there is no FCCH in the input vector
315 } fcch_search_state;
316 //@}
317
318 fcch_search_state = init;
319
320 while (!end) {
321 switch (fcch_search_state) {
322
323 case init: //initialize variables
324 hit_count = 0;
325 miss_count = 0;
326 start_pos = -1;
327 lowest_max_min_diff = 99999;
328 phase_diff_buffer.clear();
329 fcch_search_state = search;
330
331 break;
332
333 case search: // search for positive samples
334 sample_number++;
335
336 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) { //if it isn't possible to find FCCH because
337 //there's too few samples left to look into,
338 to_consume = sample_number; //don't do anything with those samples which are left
339 //and consume only those which were checked
340 fcch_search_state = search_fail;
341 } else {
342 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
343
344 if (phase_diff > 0) { //if a positive phase difference was found
345 to_consume = sample_number;
346 fcch_search_state = found_something; //switch to state in which searches for FCCH
347 } else {
348 fcch_search_state = search;
349 }
350 }
351
352 break;
353
354 case found_something: {// search for FCCH and the best position of it
355 if (phase_diff > 0) {
356 hit_count++; //positive phase differencies increases hits_count
357 } else {
358 miss_count++; //negative increases miss_count
359 }
360
361 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR)) {
362 //if miss_count exceeds limit before hit_count
363 fcch_search_state = init; //go to init
364 continue;
365 } else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR)) {
366 //if hit_count and miss_count exceeds limit then FCCH was found
367 fcch_search_state = fcch_found;
368 continue;
369 } else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) {
370 //find difference between minimal and maximal element in the buffer
371 //for FCCH this value should be low
372 //this part is searching for a region where this value is lowest
373 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
374 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
375
376 if (lowest_max_min_diff > max_phase_diff - min_phase_diff) {
377 lowest_max_min_diff = max_phase_diff - min_phase_diff;
378 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
379 best_sum = 0;
380
381 for (buffer_iter = phase_diff_buffer.begin();
382 buffer_iter != (phase_diff_buffer.end());
383 buffer_iter++) {
384 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
385 }
386 }
387 }
388
389 sample_number++;
390
391 if (sample_number >= nitems) { //if there's no single sample left to check
392 fcch_search_state = search_fail;//FCCH search failed
393 continue;
394 }
395
396 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
397 phase_diff_buffer.push_back(phase_diff);
398 fcch_search_state = found_something;
399 }
400 break;
401
402 case fcch_found: {
403 DCOUT("fcch found on position: " << d_counter + start_pos);
404 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
405
406 d_fcch_start_pos = d_counter + start_pos;
407
408 //compute frequency offset
409 double phase_offset = best_sum / FCCH_HITS_NEEDED;
410 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
411 d_freq_offset -= freq_offset;
412 DCOUT("freq_offset: " << d_freq_offset);
413
414 end = true;
415 result = true;
416 break;
417 }
418
419 case search_fail:
420 end = true;
421 result = false;
422 break;
423 }
424 }
425
426 d_counter += to_consume;
427 consume_each(to_consume);
428
429 return result;
430 }
431
432
433 double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
434 {
435 double phase_sum = 0;
436 unsigned ii;
437
438 for (ii = first_sample; ii < last_sample; ii++) {
439 double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
440 phase_sum += phase_diff;
441 }
442
443 double phase_offset = phase_sum / (last_sample - first_sample);
444 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
445 return freq_offset;
446 }
447
448 void receiver_impl::set_frequency(double freq_offset)
449 {
450 d_tuner->calleval(freq_offset);
451 }
452
453 inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
454 {
455 gr_complex conjprod = val1 * conj(val2);
456 return fast_atan2f(imag(conjprod), real(conjprod));
457 }
458
459 bool receiver_impl::reach_sch_burst(const int nitems)
460 {
461 //it just consumes samples to get near to a SCH burst
462 int to_consume = 0;
463 bool result = false;
464 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
465
466 //consume samples until d_counter will be equal to sample_nr_near_sch_start
467 if (d_counter < sample_nr_near_sch_start) {
468 if (d_counter + nitems >= sample_nr_near_sch_start) {
469 to_consume = sample_nr_near_sch_start - d_counter;
470 } else {
471 to_consume = nitems;
472 }
473 result = false;
474 } else {
475 to_consume = 0;
476 result = true;
477 }
478
479 d_counter += to_consume;
480 consume_each(to_consume);
481 return result;
482 }
483
484 int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
485 {
486 vector_complex correlation_buffer;
487 vector_float power_buffer;
488 vector_float window_energy_buffer;
489
490 int strongest_window_nr;
491 int burst_start = 0;
492 int chan_imp_resp_center = 0;
493 float max_correlation = 0;
494 float energy = 0;
495
496 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++) {
497 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
498 correlation_buffer.push_back(correlation);
499 power_buffer.push_back(std::pow(abs(correlation), 2));
500 }
501
502 //compute window energies
503 vector_float::iterator iter = power_buffer.begin();
504 bool loop_end = false;
505 while (iter != power_buffer.end()) {
506 vector_float::iterator iter_ii = iter;
507 energy = 0;
508
509 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++) {
510 if (iter_ii == power_buffer.end()) {
511 loop_end = true;
512 break;
513 }
514 energy += (*iter_ii);
515 }
516 if (loop_end) {
517 break;
518 }
519 iter++;
520 window_energy_buffer.push_back(energy);
521 }
522
523 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
524 // d_channel_imp_resp.clear();
525
526 max_correlation = 0;
527 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++) {
528 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
529 if (abs(correlation) > max_correlation) {
530 chan_imp_resp_center = ii;
531 max_correlation = abs(correlation);
532 }
533 // d_channel_imp_resp.push_back(correlation);
534 chan_imp_resp[ii] = correlation;
535 }
536
537 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
538 return burst_start;
539 }
540
541
542
543 void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
544 {
545 float output[BURST_SIZE];
546 gr_complex rhh_temp[CHAN_IMP_RESP_LENGTH*d_OSR];
547 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
548 gr_complex filtered_burst[BURST_SIZE];
549 int start_state = 3;
550 unsigned int stop_states[2] = {4, 12};
551
552 autocorrelation(chan_imp_resp, rhh_temp, d_chan_imp_length*d_OSR);
553 for (int ii = 0; ii < (d_chan_imp_length); ii++) {
554 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
555 }
556
557 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
558
559 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
560
561 for (int i = 0; i < BURST_SIZE ; i++) {
562 output_binary[i] = (output[i] > 0);
563 }
564 }
565
566 //TODO consider placing this funtion in a separate class for signal processing
567 void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
568 {
569 gr_complex j = gr_complex(0.0, 1.0);
570
571 int current_symbol;
572 int encoded_symbol;
573 int previous_symbol = 2 * input[0] - 1;
574 gmsk_output[0] = start_point;
575
576 for (int i = 1; i < nitems; i++) {
577 //change bits representation to NRZ
578 current_symbol = 2 * input[i] - 1;
579 //differentially encode
580 encoded_symbol = current_symbol * previous_symbol;
581 //and do gmsk mapping
582 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
583 previous_symbol = current_symbol;
584 }
585 }
586
587 //TODO consider use of some generalized function for correlation and placing it in a separate class for signal processing
588 gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
589 {
590 gr_complex result(0.0, 0.0);
591 int sample_number = 0;
592
593 for (int ii = 0; ii < length; ii++) {
594 sample_number = (ii * d_OSR) ;
595 result += sequence[ii] * conj(input[sample_number]);
596 }
597
598 result = result / gr_complex(length, 0);
599 return result;
600 }
601
602 //computes autocorrelation for positive arguments
603 //TODO consider placing this funtion in a separate class for signal processing
604 inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
605 {
606 int i, k;
607 for (k = nitems - 1; k >= 0; k--) {
608 out[k] = gr_complex(0, 0);
609 for (i = k; i < nitems; i++) {
610 out[k] += input[i] * conj(input[i-k]);
611 }
612 }
613 }
614
615 //TODO consider use of some generalized function for filtering and placing it in a separate class for signal processing
616 inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
617 {
618 int ii = 0, n, a;
619
620 for (n = 0; n < nitems; n++) {
621 a = n * d_OSR;
622 output[n] = 0;
623 ii = 0;
624
625 while (ii < filter_length) {
626 if ((a + ii) >= nitems*d_OSR)
627 break;
628 output[n] += input[a+ii] * filter[ii];
629 ii++;
630 }
631 }
632 }
633
634 //TODO: get_norm_chan_imp_resp is similar to get_sch_chan_imp_resp - consider joining this two functions
635 //TODO: this is place where most errors are introduced and can be corrected by improvements to this fuction
636 //especially computations of strongest_window_nr
637 int receiver_impl::get_norm_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp, int bcc)
638 {
639 vector_complex correlation_buffer;
640 vector_float power_buffer;
641 vector_float window_energy_buffer;
642
643 int strongest_window_nr;
644 int burst_start = 0;
645 int chan_imp_resp_center = 0;
646 float max_correlation = 0;
647 float energy = 0;
648
649 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
650 int search_start_pos = search_center + 1;
651 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
652 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 2 * d_OSR;
653
654 for (int ii = search_start_pos; ii < search_stop_pos; ii++) {
655 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
656
657 correlation_buffer.push_back(correlation);
658 power_buffer.push_back(std::pow(abs(correlation), 2));
659 }
660
661 //compute window energies
662 vector_float::iterator iter = power_buffer.begin();
663 bool loop_end = false;
664 while (iter != power_buffer.end()) {
665 vector_float::iterator iter_ii = iter;
666 energy = 0;
667
668 for (int ii = 0; ii < (d_chan_imp_length - 2)*d_OSR; ii++, iter_ii++) {
669 // for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++, iter_ii++) {
670 if (iter_ii == power_buffer.end()) {
671 loop_end = true;
672 break;
673 }
674 energy += (*iter_ii);
675 }
676 if (loop_end) {
677 break;
678 }
679 iter++;
680
681 window_energy_buffer.push_back(energy);
682 }
683 //!why doesn't this work
684 int strongest_window_nr_new = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
685 strongest_window_nr = 3; //! so I have to override it here
686
687 max_correlation = 0;
688 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++) {
689 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
690 if (abs(correlation) > max_correlation) {
691 chan_imp_resp_center = ii;
692 max_correlation = abs(correlation);
693 }
694 // d_channel_imp_resp.push_back(correlation);
695 chan_imp_resp[ii] = correlation;
696 }
697 // We want to use the first sample of the impulseresponse, and the
698 // corresponding samples of the received signal.
699 // the variable sync_w should contain the beginning of the used part of
700 // training sequence, which is 3+57+1+6=67 bits into the burst. That is
701 // we have that sync_t16 equals first sample in bit number 67.
702
703 burst_start = search_start_pos + chan_imp_resp_center + strongest_window_nr - TRAIN_POS * d_OSR;
704
705 // GMSK modulator introduces ISI - each bit is expanded for 3*Tb
706 // and it's maximum value is in the last bit period, so burst starts
707 // 2*Tb earlier
708 burst_start -= 2 * d_OSR;
709 burst_start += 2;
710 COUT("Poczatek ###############################");
711 std::cout << " burst_start: " << burst_start << " center: " << ((float)(search_start_pos + strongest_window_nr + chan_imp_resp_center)) / d_OSR << " stronegest window nr: " << strongest_window_nr << "\n";
712 COUT("burst_start_new: " << (search_start_pos + strongest_window_nr_new - TRAIN_POS * d_OSR));
713 burst_start=(search_start_pos + strongest_window_nr_new - TRAIN_POS * d_OSR)
714 return burst_start;
715 }
716
717
718 void receiver_impl::process_normal_burst(burst_counter burst_nr, const unsigned char * burst_binary)
719 {
720 int ii;
721 //std::cout << "fn:" <<burst_nr.get_frame_nr() << " ts" << burst_nr.get_timeslot_nr() << " ";
722 for(ii=0;ii<148;ii++){
723 std::cout << std::setprecision(1) << static_cast<int>(burst_binary[ii]);
724 }
725 std::cout << std::endl;
726 }
727 //TODO: this shouldn't be here also - the same reason
728 void receiver_impl::configure_receiver()
729 {
730 d_channel_conf.set_multiframe_type(TSC0, multiframe_51);
731 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
732
733 d_channel_conf.set_burst_types(TSC0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
734 d_channel_conf.set_burst_types(TSC0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
735
736 // d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_26);
737 // d_channel_conf.set_burst_types(TIMESLOT1, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
738 // d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_26);
739 // d_channel_conf.set_burst_types(TIMESLOT2, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
740 // d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_26);
741 // d_channel_conf.set_burst_types(TIMESLOT3, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
742 // d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_26);
743 // d_channel_conf.set_burst_types(TIMESLOT4, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
744 // d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_26);
745 // d_channel_conf.set_burst_types(TIMESLOT5, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
746 // d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_26);
747 // d_channel_conf.set_burst_types(TIMESLOT6, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
748 // d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_26);
749 // d_channel_conf.set_burst_types(TIMESLOT7, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
750 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
751 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
752 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
753 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
754 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
755 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
756 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
757 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
758 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
759 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
760 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
761 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
762 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
763 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
764
765 }
766
767
768 } /* namespace gsm */
769} /* namespace gr */
770