blob: 2c2a0c3f5c0f31352d82392fae023c69bbaa85e6 [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001/* -*- c++ -*- */
piotrd0bf1492014-02-05 17:27:32 +01002/*
piotr437f5462014-02-04 17:57:25 +01003 * Copyright 2014 <+YOU OR YOUR COMPANY+>.
piotrd0bf1492014-02-05 17:27:32 +01004 *
piotr437f5462014-02-04 17:57:25 +01005 * 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.
piotrd0bf1492014-02-05 17:27:32 +01009 *
piotr437f5462014-02-04 17:57:25 +010010 * 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.
piotrd0bf1492014-02-05 17:27:32 +010014 *
piotr437f5462014-02-04 17:57:25 +010015 * 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
piotrd0bf1492014-02-05 17:27:32 +010044namespace gr
45{
46namespace gsm
47{
piotr437f5462014-02-04 17:57:25 +010048
piotrd0bf1492014-02-05 17:27:32 +010049typedef std::list<float> list_float;
50typedef std::vector<float> vector_float;
piotr437f5462014-02-04 17:57:25 +010051
piotrd0bf1492014-02-05 17:27:32 +010052typedef boost::circular_buffer<float> circular_buffer_float;
piotr437f5462014-02-04 17:57:25 +010053
piotrd0bf1492014-02-05 17:27:32 +010054receiver::sptr
55receiver::make(feval_dd * tuner, int osr)
56{
57 return gnuradio::get_initial_sptr
58 (new receiver_impl(tuner, osr));
59}
60
61/*
62 * The private constructor
63 */
64receiver_impl::receiver_impl(feval_dd * tuner, int osr)
65 : gr::block("receiver",
66 gr::io_signature::make(1, 1, sizeof(gr_complex)),
67 gr::io_signature::make(0, 1, 142 * sizeof(float))),
68 d_OSR(osr),
69 d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
70 d_tuner(tuner),
71 d_counter(0),
72 d_fcch_start_pos(0),
73 d_freq_offset(0),
74 d_state(first_fcch_search),
75 d_burst_nr(osr),
76 d_failed_sch(0)
77{
78 int i;
79 gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0));
80 for (i = 0; i < TRAIN_SEQ_NUM; i++)
piotr437f5462014-02-04 17:57:25 +010081 {
piotr437f5462014-02-04 17:57:25 +010082 gr_complex startpoint;
piotrd0bf1492014-02-05 17:27:32 +010083 if (i == 6 || i == 7) //this is nasty hack
84 {
85 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
86 }
87 else
88 {
89 startpoint = gr_complex(1.0, 0.0); //I've checked this hack for bcc==0,1,2,3,4,6
piotr437f5462014-02-04 17:57:25 +010090 } //I don't know what about bcc==5 and 7 yet
91 //TODO:find source of this situation - this is purely mathematical problem I guess
92
93 gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint);
piotr437f5462014-02-04 17:57:25 +010094 }
piotrd0bf1492014-02-05 17:27:32 +010095}
piotr437f5462014-02-04 17:57:25 +010096
piotrd0bf1492014-02-05 17:27:32 +010097/*
98 * Our virtual destructor.
99 */
100receiver_impl::~receiver_impl()
101{
102}
103
104void receiver_impl::forecast(int noutput_items, gr_vector_int &ninput_items_required)
105{
106 ninput_items_required[0] = noutput_items * floor((TS_BITS + 2 * GUARD_PERIOD) * d_OSR);
107}
108
109
110int
111receiver_impl::general_work(int noutput_items,
112 gr_vector_int &ninput_items,
113 gr_vector_const_void_star &input_items,
114 gr_vector_void_star &output_items)
115{
116 const gr_complex *input = (const gr_complex *) input_items[0];
117 //float *out = (float *) output_items[0];
118 int produced_out = 0; //how many output elements were produced - this isn't used yet
119 //probably the gsm receiver will be changed into sink so this variable won't be necessary
120 switch (d_state)
piotr437f5462014-02-04 17:57:25 +0100121 {
piotrd0bf1492014-02-05 17:27:32 +0100122 //bootstrapping
123 case first_fcch_search:
124 COUT("FCCH search");
125 if (find_fcch_burst(input, ninput_items[0])) //find frequency correction burst in the input buffer
126 {
piotr437f5462014-02-04 17:57:25 +0100127 set_frequency(d_freq_offset); //if fcch search is successful set frequency offset
128 //produced_out = 0;
129 d_state = next_fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100130 }
131 else
132 {
piotr437f5462014-02-04 17:57:25 +0100133 //produced_out = 0;
134 d_state = first_fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100135 }
136 break;
piotr437f5462014-02-04 17:57:25 +0100137
piotrd0bf1492014-02-05 17:27:32 +0100138 case next_fcch_search: //this state is used because it takes some time (a bunch of buffered samples)
139 {
140 COUT("NEXT FCCH search");
141 float prev_freq_offset = d_freq_offset; //before previous set_frequqency cause change
142 if (find_fcch_burst(input, ninput_items[0]))
143 {
144 if (abs(prev_freq_offset - d_freq_offset) > FCCH_MAX_FREQ_OFFSET)
145 {
piotr437f5462014-02-04 17:57:25 +0100146 set_frequency(d_freq_offset); //call set_frequncy only frequency offset change is greater than some value
piotr437f5462014-02-04 17:57:25 +0100147 }
piotrd0bf1492014-02-05 17:27:32 +0100148 //produced_out = 0;
149 d_state = sch_search;
150 }
151 else
152 {
153 //produced_out = 0;
154 d_state = next_fcch_search;
155 }
156 break;
157 }
piotr437f5462014-02-04 17:57:25 +0100158
159
piotrd0bf1492014-02-05 17:27:32 +0100160 case sch_search:
161 {
162 DCOUT("SCH search") ;
163 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
164 int t1, t2, t3;
165 int burst_start = 0;
166 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100167
piotrd0bf1492014-02-05 17:27:32 +0100168 if (reach_sch_burst(ninput_items[0])) //wait for a SCH burst
169 {
170 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it
171 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection
172 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst
173 {
piotr437f5462014-02-04 17:57:25 +0100174 COUT("sch burst_start: " << burst_start);
175 COUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3);
176 d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value
177
178 //configure the receiver - tell him where to find which burst type
179 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51); //in the timeslot nr.0 bursts changes according to t3 counter
180 configure_receiver();//TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready
181 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst); //tell where to find fcch bursts
182 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst); //sch bursts
183 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
184 d_burst_nr++;
185
186 consume_each(burst_start + BURST_SIZE * d_OSR); //consume samples up to next guard period
187 d_state = synchronized;
piotrd0bf1492014-02-05 17:27:32 +0100188 }
189 else
190 {
piotr437f5462014-02-04 17:57:25 +0100191 d_state = next_fcch_search; //if there is error in the sch burst go back to fcch search phase
piotr437f5462014-02-04 17:57:25 +0100192 }
piotrd0bf1492014-02-05 17:27:32 +0100193 }
194 else
195 {
196 d_state = sch_search;
197 }
198 break;
199 }
200 //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
201 case synchronized:
202 {
203 DCOUT("Synchronized") ;
204 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
205 int burst_start;
206 int offset = 0;
207 int to_consume = 0;
208 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100209
piotrd0bf1492014-02-05 17:27:32 +0100210 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number
piotr437f5462014-02-04 17:57:25 +0100211
piotrd0bf1492014-02-05 17:27:32 +0100212 switch (b_type)
213 {
214 case fcch_burst: //if it's FCCH burst
215 {
216 const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
217 const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
218 double freq_offset = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it
piotr437f5462014-02-04 17:57:25 +0100219
piotrd0bf1492014-02-05 17:27:32 +0100220 d_freq_offset_vals.push_front(freq_offset);
221 process_normal_burst(d_burst_nr, fc_fb);
222 if (d_freq_offset_vals.size() >= 10)
223 {
224 double sum = std::accumulate(d_freq_offset_vals.begin(), d_freq_offset_vals.end(), 0);
225 double mean_offset = sum / d_freq_offset_vals.size(); //compute mean
226 d_freq_offset_vals.clear();
227 if (abs(mean_offset) > FCCH_MAX_FREQ_OFFSET)
228 {
229 d_freq_offset -= mean_offset; //and adjust frequency if it have changed beyond
230 set_frequency(d_freq_offset); //some limit
231 DCOUT("mean_offset: " << mean_offset);
232 DCOUT("Adjusting frequency, new frequency offset: " << d_freq_offset << "\n");
233 }
234 }
235 }
236 break;
237 case sch_burst: //if it's SCH burst
238 {
239 int t1, t2, t3, d_ncc, d_bcc;
240 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response
241 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits
242 process_normal_burst(d_burst_nr, output_binary);
243 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data
244 {
245 // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct
246 d_failed_sch = 0;
247 DCOUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3);
248 offset = burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period
249 DCOUT(offset);
250 to_consume += offset; //adjust with offset number of samples to be consumed
251 }
252 else
253 {
254 d_failed_sch++;
255 if (d_failed_sch >= MAX_SCH_ERRORS)
256 {
257 d_state = first_fcch_search; //TODO: this isn't good, the receiver is going wild when it goes back to next_fcch_search from here
piotr437f5462014-02-04 17:57:25 +0100258 d_freq_offset_vals.clear();
piotrd0bf1492014-02-05 17:27:32 +0100259 d_freq_offset=0;
260 set_frequency(0);
261 DCOUT("many sch decoding errors");
piotr437f5462014-02-04 17:57:25 +0100262 }
piotr437f5462014-02-04 17:57:25 +0100263 }
piotrd0bf1492014-02-05 17:27:32 +0100264 }
265 break;
piotr437f5462014-02-04 17:57:25 +0100266
piotrd0bf1492014-02-05 17:27:32 +0100267 case normal_burst: //if it's normal burst
268 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
269 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits
270 process_normal_burst(d_burst_nr, output_binary); //TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready
271 break;
piotr437f5462014-02-04 17:57:25 +0100272
piotrd0bf1492014-02-05 17:27:32 +0100273 case dummy_or_normal:
274 {
275 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], TS_DUMMY);
276 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
piotr437f5462014-02-04 17:57:25 +0100277
piotrd0bf1492014-02-05 17:27:32 +0100278 std::vector<unsigned char> v(20);
279 std::vector<unsigned char>::iterator it;
280 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());
281 int different_bits = (it - v.begin());
282
283 if (different_bits > 2)
284 {
285 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], d_bcc);
286 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
287 //if (!output_binary[0] && !output_binary[1] && !output_binary[2]) {
288 // COUT("Normal burst");
289 process_normal_burst(d_burst_nr, output_binary); //TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready
290 //}
291 }
292 else
293 {
294 process_normal_burst(d_burst_nr, dummy_burst);
295 }
296 }
297 case rach_burst:
298 //implementation of this channel isn't possible in current gsm_receiver
299 //it would take some realtime processing, counter of samples from USRP to
300 //stay synchronized with this device and possibility to switch frequency from uplink
301 //to C0 (where sch is) back and forth
302
303 break;
304 case dummy: //if it's dummy
305 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], TS_DUMMY); //read dummy
306 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); // but as far as I know it's pointless
307 break;
308 case empty: //if it's empty burst
309 break; //do nothing
310 }
311
312 d_burst_nr++; //go to next burst
313
314 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); //consume samples of the burst up to next guard period
315 //and add offset which is introduced by
316 //0.25 fractional part of a guard period
317 //burst_number computes this offset
318 //but choice of this class to do this was random
319 consume_each(to_consume);
320 }
321 break;
piotr437f5462014-02-04 17:57:25 +0100322 }
323
piotrd0bf1492014-02-05 17:27:32 +0100324 return produced_out;
325}
piotr437f5462014-02-04 17:57:25 +0100326
piotrd0bf1492014-02-05 17:27:32 +0100327
328bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems)
329{
330 circular_buffer_float phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR); //circular buffer used to scan throug signal to find
331 //best match for FCCH burst
332 float phase_diff = 0;
333 gr_complex conjprod;
334 int start_pos = -1;
335 int hit_count = 0;
336 int miss_count = 0;
337 float min_phase_diff;
338 float max_phase_diff;
339 double best_sum = 0;
340 float lowest_max_min_diff = 99999;
341
342 int to_consume = 0;
343 int sample_number = 0;
344 bool end = false;
345 bool result = false;
346 circular_buffer_float::iterator buffer_iter;
347
348 /**@name Possible states of FCCH search algorithm*/
349 //@{
350 enum states
piotr437f5462014-02-04 17:57:25 +0100351 {
piotr437f5462014-02-04 17:57:25 +0100352 init, ///< initialize variables
353 search, ///< search for positive samples
354 found_something, ///< search for FCCH and the best position of it
355 fcch_found, ///< when FCCH was found
356 search_fail ///< when there is no FCCH in the input vector
piotrd0bf1492014-02-05 17:27:32 +0100357 } fcch_search_state;
358 //@}
piotr437f5462014-02-04 17:57:25 +0100359
piotrd0bf1492014-02-05 17:27:32 +0100360 fcch_search_state = init;
piotr437f5462014-02-04 17:57:25 +0100361
piotrd0bf1492014-02-05 17:27:32 +0100362 while (!end)
363 {
364 switch (fcch_search_state)
365 {
piotr437f5462014-02-04 17:57:25 +0100366
piotrd0bf1492014-02-05 17:27:32 +0100367 case init: //initialize variables
piotr437f5462014-02-04 17:57:25 +0100368 hit_count = 0;
369 miss_count = 0;
370 start_pos = -1;
371 lowest_max_min_diff = 99999;
372 phase_diff_buffer.clear();
373 fcch_search_state = search;
374
375 break;
376
piotrd0bf1492014-02-05 17:27:32 +0100377 case search: // search for positive samples
piotr437f5462014-02-04 17:57:25 +0100378 sample_number++;
379
piotrd0bf1492014-02-05 17:27:32 +0100380 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because
381 {
382 //there's too few samples left to look into,
383 to_consume = sample_number; //don't do anything with those samples which are left
384 //and consume only those which were checked
385 fcch_search_state = search_fail;
386 }
387 else
388 {
389 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
piotr437f5462014-02-04 17:57:25 +0100390
piotrd0bf1492014-02-05 17:27:32 +0100391 if (phase_diff > 0) //if a positive phase difference was found
392 {
393 to_consume = sample_number;
394 fcch_search_state = found_something; //switch to state in which searches for FCCH
395 }
396 else
397 {
398 fcch_search_state = search;
399 }
piotr437f5462014-02-04 17:57:25 +0100400 }
401
402 break;
403
piotrd0bf1492014-02-05 17:27:32 +0100404 case found_something: // search for FCCH and the best position of it
405 {
406 if (phase_diff > 0)
407 {
piotr437f5462014-02-04 17:57:25 +0100408 hit_count++; //positive phase differencies increases hits_count
piotrd0bf1492014-02-05 17:27:32 +0100409 }
410 else
411 {
piotr437f5462014-02-04 17:57:25 +0100412 miss_count++; //negative increases miss_count
piotrd0bf1492014-02-05 17:27:32 +0100413 }
piotr437f5462014-02-04 17:57:25 +0100414
piotrd0bf1492014-02-05 17:27:32 +0100415 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
416 {
piotr437f5462014-02-04 17:57:25 +0100417 //if miss_count exceeds limit before hit_count
418 fcch_search_state = init; //go to init
419 continue;
piotrd0bf1492014-02-05 17:27:32 +0100420 }
421 else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
422 {
piotr437f5462014-02-04 17:57:25 +0100423 //if hit_count and miss_count exceeds limit then FCCH was found
424 fcch_search_state = fcch_found;
425 continue;
piotrd0bf1492014-02-05 17:27:32 +0100426 }
427 else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR))
428 {
piotr437f5462014-02-04 17:57:25 +0100429 //find difference between minimal and maximal element in the buffer
430 //for FCCH this value should be low
431 //this part is searching for a region where this value is lowest
432 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
433 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
434
piotrd0bf1492014-02-05 17:27:32 +0100435 if (lowest_max_min_diff > max_phase_diff - min_phase_diff)
436 {
437 lowest_max_min_diff = max_phase_diff - min_phase_diff;
438 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
439 best_sum = 0;
piotr437f5462014-02-04 17:57:25 +0100440
piotrd0bf1492014-02-05 17:27:32 +0100441 for (buffer_iter = phase_diff_buffer.begin();
442 buffer_iter != (phase_diff_buffer.end());
443 buffer_iter++)
444 {
445 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
446 }
piotr437f5462014-02-04 17:57:25 +0100447 }
piotrd0bf1492014-02-05 17:27:32 +0100448 }
piotr437f5462014-02-04 17:57:25 +0100449
piotrd0bf1492014-02-05 17:27:32 +0100450 sample_number++;
piotr437f5462014-02-04 17:57:25 +0100451
piotrd0bf1492014-02-05 17:27:32 +0100452 if (sample_number >= nitems) //if there's no single sample left to check
453 {
piotr437f5462014-02-04 17:57:25 +0100454 fcch_search_state = search_fail;//FCCH search failed
455 continue;
piotr437f5462014-02-04 17:57:25 +0100456 }
piotrd0bf1492014-02-05 17:27:32 +0100457
458 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
459 phase_diff_buffer.push_back(phase_diff);
460 fcch_search_state = found_something;
461 }
462 break;
463
464 case fcch_found:
465 {
466 DCOUT("fcch found on position: " << d_counter + start_pos);
467 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
468
469 d_fcch_start_pos = d_counter + start_pos;
470
471 //compute frequency offset
472 double phase_offset = best_sum / FCCH_HITS_NEEDED;
473 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
474 d_freq_offset -= freq_offset;
475 DCOUT("freq_offset: " << d_freq_offset);
476
477 end = true;
478 result = true;
piotr437f5462014-02-04 17:57:25 +0100479 break;
piotrd0bf1492014-02-05 17:27:32 +0100480 }
piotr437f5462014-02-04 17:57:25 +0100481
piotrd0bf1492014-02-05 17:27:32 +0100482 case search_fail:
piotr437f5462014-02-04 17:57:25 +0100483 end = true;
484 result = false;
485 break;
486 }
piotr437f5462014-02-04 17:57:25 +0100487 }
488
piotrd0bf1492014-02-05 17:27:32 +0100489 d_counter += to_consume;
490 consume_each(to_consume);
piotr437f5462014-02-04 17:57:25 +0100491
piotrd0bf1492014-02-05 17:27:32 +0100492 return result;
493}
494
495
496double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
497{
498 double phase_sum = 0;
499 unsigned ii;
500
501 for (ii = first_sample; ii < last_sample; ii++)
piotr437f5462014-02-04 17:57:25 +0100502 {
piotr437f5462014-02-04 17:57:25 +0100503 double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
504 phase_sum += phase_diff;
piotr437f5462014-02-04 17:57:25 +0100505 }
506
piotrd0bf1492014-02-05 17:27:32 +0100507 double phase_offset = phase_sum / (last_sample - first_sample);
508 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
509 return freq_offset;
510}
piotr437f5462014-02-04 17:57:25 +0100511
piotrd0bf1492014-02-05 17:27:32 +0100512void receiver_impl::set_frequency(double freq_offset)
513{
514 d_tuner->calleval(freq_offset);
515}
piotr437f5462014-02-04 17:57:25 +0100516
piotrd0bf1492014-02-05 17:27:32 +0100517inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
518{
519 gr_complex conjprod = val1 * conj(val2);
520 return fast_atan2f(imag(conjprod), real(conjprod));
521}
piotr437f5462014-02-04 17:57:25 +0100522
piotrd0bf1492014-02-05 17:27:32 +0100523bool receiver_impl::reach_sch_burst(const int nitems)
524{
525 //it just consumes samples to get near to a SCH burst
526 int to_consume = 0;
527 bool result = false;
528 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
529
530 //consume samples until d_counter will be equal to sample_nr_near_sch_start
531 if (d_counter < sample_nr_near_sch_start)
532 {
533 if (d_counter + nitems >= sample_nr_near_sch_start)
534 {
535 to_consume = sample_nr_near_sch_start - d_counter;
536 }
537 else
538 {
539 to_consume = nitems;
piotr437f5462014-02-04 17:57:25 +0100540 }
541 result = false;
piotrd0bf1492014-02-05 17:27:32 +0100542 }
543 else
544 {
piotr437f5462014-02-04 17:57:25 +0100545 to_consume = 0;
546 result = true;
piotr437f5462014-02-04 17:57:25 +0100547 }
548
piotrd0bf1492014-02-05 17:27:32 +0100549 d_counter += to_consume;
550 consume_each(to_consume);
551 return result;
552}
553
554int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
555{
556 vector_complex correlation_buffer;
557 vector_float power_buffer;
558 vector_float window_energy_buffer;
559
560 int strongest_window_nr;
561 int burst_start = 0;
562 int chan_imp_resp_center = 0;
563 float max_correlation = 0;
564 float energy = 0;
565
566 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++)
piotr437f5462014-02-04 17:57:25 +0100567 {
piotr437f5462014-02-04 17:57:25 +0100568 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
569 correlation_buffer.push_back(correlation);
570 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100571 }
piotr437f5462014-02-04 17:57:25 +0100572
piotrd0bf1492014-02-05 17:27:32 +0100573 //compute window energies
574 vector_float::iterator iter = power_buffer.begin();
575 bool loop_end = false;
576 while (iter != power_buffer.end())
577 {
piotr437f5462014-02-04 17:57:25 +0100578 vector_float::iterator iter_ii = iter;
579 energy = 0;
580
piotrd0bf1492014-02-05 17:27:32 +0100581 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++)
582 {
583 if (iter_ii == power_buffer.end())
584 {
585 loop_end = true;
586 break;
587 }
588 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100589 }
piotrd0bf1492014-02-05 17:27:32 +0100590 if (loop_end)
591 {
592 break;
piotr437f5462014-02-04 17:57:25 +0100593 }
594 iter++;
595 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100596 }
piotr437f5462014-02-04 17:57:25 +0100597
piotrd0bf1492014-02-05 17:27:32 +0100598 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100599 // d_channel_imp_resp.clear();
600
piotrd0bf1492014-02-05 17:27:32 +0100601 max_correlation = 0;
602 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++)
603 {
piotr437f5462014-02-04 17:57:25 +0100604 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100605 if (abs(correlation) > max_correlation)
606 {
607 chan_imp_resp_center = ii;
608 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100609 }
piotrd0bf1492014-02-05 17:27:32 +0100610 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100611 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100612 }
613
piotrd0bf1492014-02-05 17:27:32 +0100614 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
615 return burst_start;
616}
piotr437f5462014-02-04 17:57:25 +0100617
618
piotrd0bf1492014-02-05 17:27:32 +0100619
620void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
621{
622 float output[BURST_SIZE];
623 gr_complex rhh_temp[CHAN_IMP_RESP_LENGTH*d_OSR];
624 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
625 gr_complex filtered_burst[BURST_SIZE];
626 int start_state = 3;
627 unsigned int stop_states[2] = {4, 12};
628
629 autocorrelation(chan_imp_resp, rhh_temp, d_chan_imp_length*d_OSR);
630 for (int ii = 0; ii < (d_chan_imp_length); ii++)
piotr437f5462014-02-04 17:57:25 +0100631 {
piotr437f5462014-02-04 17:57:25 +0100632 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
piotr437f5462014-02-04 17:57:25 +0100633 }
634
piotrd0bf1492014-02-05 17:27:32 +0100635 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
636
637 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
638
639 for (int i = 0; i < BURST_SIZE ; i++)
piotr437f5462014-02-04 17:57:25 +0100640 {
piotrd0bf1492014-02-05 17:27:32 +0100641 output_binary[i] = (output[i] > 0);
642 }
643}
piotr437f5462014-02-04 17:57:25 +0100644
piotrd0bf1492014-02-05 17:27:32 +0100645//TODO consider placing this funtion in a separate class for signal processing
646void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
647{
648 gr_complex j = gr_complex(0.0, 1.0);
piotr437f5462014-02-04 17:57:25 +0100649
piotrd0bf1492014-02-05 17:27:32 +0100650 int current_symbol;
651 int encoded_symbol;
652 int previous_symbol = 2 * input[0] - 1;
653 gmsk_output[0] = start_point;
654
655 for (int i = 1; i < nitems; i++)
656 {
piotr437f5462014-02-04 17:57:25 +0100657 //change bits representation to NRZ
658 current_symbol = 2 * input[i] - 1;
659 //differentially encode
660 encoded_symbol = current_symbol * previous_symbol;
661 //and do gmsk mapping
662 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
663 previous_symbol = current_symbol;
piotr437f5462014-02-04 17:57:25 +0100664 }
piotrd0bf1492014-02-05 17:27:32 +0100665}
piotr437f5462014-02-04 17:57:25 +0100666
piotrd0bf1492014-02-05 17:27:32 +0100667//TODO consider use of some generalized function for correlation and placing it in a separate class for signal processing
668gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
669{
670 gr_complex result(0.0, 0.0);
671 int sample_number = 0;
672
673 for (int ii = 0; ii < length; ii++)
piotr437f5462014-02-04 17:57:25 +0100674 {
piotr437f5462014-02-04 17:57:25 +0100675 sample_number = (ii * d_OSR) ;
676 result += sequence[ii] * conj(input[sample_number]);
piotr437f5462014-02-04 17:57:25 +0100677 }
678
piotrd0bf1492014-02-05 17:27:32 +0100679 result = result / gr_complex(length, 0);
680 return result;
681}
682
683//computes autocorrelation for positive arguments
684//TODO consider placing this funtion in a separate class for signal processing
685inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
686{
687 int i, k;
688 for (k = nitems - 1; k >= 0; k--)
piotr437f5462014-02-04 17:57:25 +0100689 {
piotr437f5462014-02-04 17:57:25 +0100690 out[k] = gr_complex(0, 0);
piotrd0bf1492014-02-05 17:27:32 +0100691 for (i = k; i < nitems; i++)
692 {
693 out[k] += input[i] * conj(input[i-k]);
piotr437f5462014-02-04 17:57:25 +0100694 }
piotr437f5462014-02-04 17:57:25 +0100695 }
piotrd0bf1492014-02-05 17:27:32 +0100696}
piotr437f5462014-02-04 17:57:25 +0100697
piotrd0bf1492014-02-05 17:27:32 +0100698//TODO consider use of some generalized function for filtering and placing it in a separate class for signal processing
699inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
700{
701 int ii = 0, n, a;
702
703 for (n = 0; n < nitems; n++)
piotr437f5462014-02-04 17:57:25 +0100704 {
piotr437f5462014-02-04 17:57:25 +0100705 a = n * d_OSR;
706 output[n] = 0;
707 ii = 0;
708
piotrd0bf1492014-02-05 17:27:32 +0100709 while (ii < filter_length)
710 {
711 if ((a + ii) >= nitems*d_OSR)
712 break;
713 output[n] += input[a+ii] * filter[ii];
714 ii++;
piotr437f5462014-02-04 17:57:25 +0100715 }
piotr437f5462014-02-04 17:57:25 +0100716 }
piotrd0bf1492014-02-05 17:27:32 +0100717}
piotr437f5462014-02-04 17:57:25 +0100718
piotrd0bf1492014-02-05 17:27:32 +0100719//TODO: get_norm_chan_imp_resp is similar to get_sch_chan_imp_resp - consider joining this two functions
720//TODO: this is place where most errors are introduced and can be corrected by improvements to this fuction
721//especially computations of strongest_window_nr
722int receiver_impl::get_norm_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp, int bcc)
723{
724 vector_complex correlation_buffer;
725 vector_float power_buffer;
726 vector_float window_energy_buffer;
piotr437f5462014-02-04 17:57:25 +0100727
piotrd0bf1492014-02-05 17:27:32 +0100728 int strongest_window_nr;
729 int burst_start = 0;
730 int chan_imp_resp_center = 0;
731 float max_correlation = 0;
732 float energy = 0;
piotr437f5462014-02-04 17:57:25 +0100733
piotrd0bf1492014-02-05 17:27:32 +0100734 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
735 int search_start_pos = search_center + 1;
piotr437f5462014-02-04 17:57:25 +0100736 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
piotrd0bf1492014-02-05 17:27:32 +0100737 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 2 * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100738
piotrd0bf1492014-02-05 17:27:32 +0100739 for (int ii = search_start_pos; ii < search_stop_pos; ii++)
740 {
piotr437f5462014-02-04 17:57:25 +0100741 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
742
743 correlation_buffer.push_back(correlation);
744 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100745 }
piotr437f5462014-02-04 17:57:25 +0100746
piotrd0bf1492014-02-05 17:27:32 +0100747 //compute window energies
748 vector_float::iterator iter = power_buffer.begin();
749 bool loop_end = false;
750 while (iter != power_buffer.end())
751 {
piotr437f5462014-02-04 17:57:25 +0100752 vector_float::iterator iter_ii = iter;
753 energy = 0;
754
piotrd0bf1492014-02-05 17:27:32 +0100755 for (int ii = 0; ii < (d_chan_imp_length - 2)*d_OSR; ii++, iter_ii++)
756 {
757 // for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++, iter_ii++) {
758 if (iter_ii == power_buffer.end())
759 {
760 loop_end = true;
761 break;
762 }
763 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100764 }
piotrd0bf1492014-02-05 17:27:32 +0100765 if (loop_end)
766 {
767 break;
piotr437f5462014-02-04 17:57:25 +0100768 }
769 iter++;
770
771 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100772 }
773 //!why doesn't this work
774 int strongest_window_nr_new = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
775 strongest_window_nr = 3; //! so I have to override it here
piotr437f5462014-02-04 17:57:25 +0100776
piotrd0bf1492014-02-05 17:27:32 +0100777 max_correlation = 0;
778 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++)
779 {
piotr437f5462014-02-04 17:57:25 +0100780 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100781 if (abs(correlation) > max_correlation)
782 {
783 chan_imp_resp_center = ii;
784 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100785 }
piotrd0bf1492014-02-05 17:27:32 +0100786 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100787 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100788 }
piotrd0bf1492014-02-05 17:27:32 +0100789 // We want to use the first sample of the impulseresponse, and the
790 // corresponding samples of the received signal.
791 // the variable sync_w should contain the beginning of the used part of
792 // training sequence, which is 3+57+1+6=67 bits into the burst. That is
793 // we have that sync_t16 equals first sample in bit number 67.
794
795 burst_start = search_start_pos + chan_imp_resp_center + strongest_window_nr - TRAIN_POS * d_OSR;
796
797 // GMSK modulator introduces ISI - each bit is expanded for 3*Tb
798 // and it's maximum value is in the last bit period, so burst starts
799 // 2*Tb earlier
800 burst_start -= 2 * d_OSR;
801 burst_start += 2;
802 //COUT("Poczatek ###############################");
803 //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";
804 //COUT("burst_start_new: " << (search_start_pos + strongest_window_nr_new - TRAIN_POS * d_OSR));
805 burst_start=(search_start_pos + strongest_window_nr_new - TRAIN_POS * d_OSR);
806 return burst_start;
807}
piotr437f5462014-02-04 17:57:25 +0100808
809
piotrd0bf1492014-02-05 17:27:32 +0100810void receiver_impl::process_normal_burst(burst_counter burst_nr, const unsigned char * burst_binary)
811{
812 int ii;
813 //std::cout << "fn:" <<burst_nr.get_frame_nr() << " ts" << burst_nr.get_timeslot_nr() << " ";
814 for(ii=0; ii<148; ii++)
piotr437f5462014-02-04 17:57:25 +0100815 {
piotrd0bf1492014-02-05 17:27:32 +0100816 std::cout << std::setprecision(1) << static_cast<int>(burst_binary[ii]) << " ";
piotr437f5462014-02-04 17:57:25 +0100817 }
piotrd0bf1492014-02-05 17:27:32 +0100818 std::cout << std::endl;
819}
820//TODO: this shouldn't be here also - the same reason
821void receiver_impl::configure_receiver()
822{
823 d_channel_conf.set_multiframe_type(TSC0, multiframe_51);
824 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +0100825
piotrd0bf1492014-02-05 17:27:32 +0100826 d_channel_conf.set_burst_types(TSC0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
827 d_channel_conf.set_burst_types(TSC0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
piotr437f5462014-02-04 17:57:25 +0100828
829 // d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_26);
830 // d_channel_conf.set_burst_types(TIMESLOT1, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
831 // d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_26);
832 // d_channel_conf.set_burst_types(TIMESLOT2, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
833 // d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_26);
834 // d_channel_conf.set_burst_types(TIMESLOT3, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
835 // d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_26);
836 // d_channel_conf.set_burst_types(TIMESLOT4, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
837 // d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_26);
838 // d_channel_conf.set_burst_types(TIMESLOT5, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
839 // d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_26);
840 // d_channel_conf.set_burst_types(TIMESLOT6, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
841 // d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_26);
842 // d_channel_conf.set_burst_types(TIMESLOT7, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
piotrd0bf1492014-02-05 17:27:32 +0100843 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
844 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
845 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
846 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
847 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
848 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
849 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
850 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
851 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
852 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
853 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
854 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
855 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
856 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
857
858}
piotr437f5462014-02-04 17:57:25 +0100859
860
piotrd0bf1492014-02-05 17:27:32 +0100861} /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +0100862} /* namespace gr */
863