blob: bd3d3e4b45c2ae6b55d6c9314a340885dda5caf8 [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 {
piotr5f1e1d32014-02-05 18:10:05 +0100127 //set_frequency(d_freq_offset); //if fcch search is successful set frequency offset
128 COUT("Freq offset " << d_freq_offset);
piotr437f5462014-02-04 17:57:25 +0100129 //produced_out = 0;
130 d_state = next_fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100131 }
132 else
133 {
piotr437f5462014-02-04 17:57:25 +0100134 //produced_out = 0;
135 d_state = first_fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100136 }
137 break;
piotr437f5462014-02-04 17:57:25 +0100138
piotrd0bf1492014-02-05 17:27:32 +0100139 case next_fcch_search: //this state is used because it takes some time (a bunch of buffered samples)
140 {
141 COUT("NEXT FCCH search");
142 float prev_freq_offset = d_freq_offset; //before previous set_frequqency cause change
143 if (find_fcch_burst(input, ninput_items[0]))
144 {
145 if (abs(prev_freq_offset - d_freq_offset) > FCCH_MAX_FREQ_OFFSET)
146 {
piotr5f1e1d32014-02-05 18:10:05 +0100147 //set_frequency(d_freq_offset); //call set_frequncy only frequency offset change is greater than some value
148 COUT("Freq offset " << d_freq_offset);
piotr437f5462014-02-04 17:57:25 +0100149 }
piotrd0bf1492014-02-05 17:27:32 +0100150 //produced_out = 0;
151 d_state = sch_search;
152 }
153 else
154 {
155 //produced_out = 0;
156 d_state = next_fcch_search;
157 }
158 break;
159 }
piotr437f5462014-02-04 17:57:25 +0100160
161
piotrd0bf1492014-02-05 17:27:32 +0100162 case sch_search:
163 {
164 DCOUT("SCH search") ;
165 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
166 int t1, t2, t3;
167 int burst_start = 0;
168 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100169
piotrd0bf1492014-02-05 17:27:32 +0100170 if (reach_sch_burst(ninput_items[0])) //wait for a SCH burst
171 {
172 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it
173 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection
174 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst
175 {
piotr437f5462014-02-04 17:57:25 +0100176 COUT("sch burst_start: " << burst_start);
177 COUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3);
178 d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value
179
180 //configure the receiver - tell him where to find which burst type
181 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51); //in the timeslot nr.0 bursts changes according to t3 counter
182 configure_receiver();//TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready
183 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst); //tell where to find fcch bursts
184 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst); //sch bursts
185 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
186 d_burst_nr++;
187
188 consume_each(burst_start + BURST_SIZE * d_OSR); //consume samples up to next guard period
189 d_state = synchronized;
piotrd0bf1492014-02-05 17:27:32 +0100190 }
191 else
192 {
piotr437f5462014-02-04 17:57:25 +0100193 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 +0100194 }
piotrd0bf1492014-02-05 17:27:32 +0100195 }
196 else
197 {
198 d_state = sch_search;
199 }
200 break;
201 }
202 //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
203 case synchronized:
204 {
205 DCOUT("Synchronized") ;
206 vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
207 int burst_start;
208 int offset = 0;
209 int to_consume = 0;
210 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100211
piotrd0bf1492014-02-05 17:27:32 +0100212 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 +0100213
piotrd0bf1492014-02-05 17:27:32 +0100214 switch (b_type)
215 {
216 case fcch_burst: //if it's FCCH burst
217 {
218 const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
219 const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
220 double freq_offset = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it
piotr437f5462014-02-04 17:57:25 +0100221
piotrd0bf1492014-02-05 17:27:32 +0100222 d_freq_offset_vals.push_front(freq_offset);
223 process_normal_burst(d_burst_nr, fc_fb);
224 if (d_freq_offset_vals.size() >= 10)
225 {
226 double sum = std::accumulate(d_freq_offset_vals.begin(), d_freq_offset_vals.end(), 0);
227 double mean_offset = sum / d_freq_offset_vals.size(); //compute mean
228 d_freq_offset_vals.clear();
229 if (abs(mean_offset) > FCCH_MAX_FREQ_OFFSET)
230 {
231 d_freq_offset -= mean_offset; //and adjust frequency if it have changed beyond
piotr5f1e1d32014-02-05 18:10:05 +0100232 //set_frequency(d_freq_offset); //some limit
233 COUT("mean_offset: " << mean_offset);
234 COUT("Adjusting frequency, new frequency offset: " << d_freq_offset << "\n");
piotrd0bf1492014-02-05 17:27:32 +0100235 }
236 }
237 }
238 break;
239 case sch_burst: //if it's SCH burst
240 {
241 int t1, t2, t3, d_ncc, d_bcc;
242 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response
243 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits
244 process_normal_burst(d_burst_nr, output_binary);
245 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data
246 {
247 // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct
248 d_failed_sch = 0;
249 DCOUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3);
250 offset = burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period
251 DCOUT(offset);
252 to_consume += offset; //adjust with offset number of samples to be consumed
253 }
254 else
255 {
256 d_failed_sch++;
257 if (d_failed_sch >= MAX_SCH_ERRORS)
258 {
259 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 +0100260 d_freq_offset_vals.clear();
piotrd0bf1492014-02-05 17:27:32 +0100261 d_freq_offset=0;
262 set_frequency(0);
263 DCOUT("many sch decoding errors");
piotr437f5462014-02-04 17:57:25 +0100264 }
piotr437f5462014-02-04 17:57:25 +0100265 }
piotrd0bf1492014-02-05 17:27:32 +0100266 }
267 break;
piotr437f5462014-02-04 17:57:25 +0100268
piotrd0bf1492014-02-05 17:27:32 +0100269 case normal_burst: //if it's normal burst
270 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
271 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits
272 process_normal_burst(d_burst_nr, output_binary); //TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready
273 break;
piotr437f5462014-02-04 17:57:25 +0100274
piotrd0bf1492014-02-05 17:27:32 +0100275 case dummy_or_normal:
276 {
277 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], TS_DUMMY);
278 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
piotr437f5462014-02-04 17:57:25 +0100279
piotrd0bf1492014-02-05 17:27:32 +0100280 std::vector<unsigned char> v(20);
281 std::vector<unsigned char>::iterator it;
282 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());
283 int different_bits = (it - v.begin());
284
285 if (different_bits > 2)
286 {
287 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], d_bcc);
288 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
289 //if (!output_binary[0] && !output_binary[1] && !output_binary[2]) {
290 // COUT("Normal burst");
291 process_normal_burst(d_burst_nr, output_binary); //TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready
292 //}
293 }
294 else
295 {
296 process_normal_burst(d_burst_nr, dummy_burst);
297 }
298 }
299 case rach_burst:
300 //implementation of this channel isn't possible in current gsm_receiver
301 //it would take some realtime processing, counter of samples from USRP to
302 //stay synchronized with this device and possibility to switch frequency from uplink
303 //to C0 (where sch is) back and forth
304
305 break;
306 case dummy: //if it's dummy
307 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], TS_DUMMY); //read dummy
308 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); // but as far as I know it's pointless
309 break;
310 case empty: //if it's empty burst
311 break; //do nothing
312 }
313
314 d_burst_nr++; //go to next burst
315
316 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); //consume samples of the burst up to next guard period
317 //and add offset which is introduced by
318 //0.25 fractional part of a guard period
319 //burst_number computes this offset
320 //but choice of this class to do this was random
321 consume_each(to_consume);
322 }
323 break;
piotr437f5462014-02-04 17:57:25 +0100324 }
325
piotrd0bf1492014-02-05 17:27:32 +0100326 return produced_out;
327}
piotr437f5462014-02-04 17:57:25 +0100328
piotrd0bf1492014-02-05 17:27:32 +0100329
330bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems)
331{
332 circular_buffer_float phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR); //circular buffer used to scan throug signal to find
333 //best match for FCCH burst
334 float phase_diff = 0;
335 gr_complex conjprod;
336 int start_pos = -1;
337 int hit_count = 0;
338 int miss_count = 0;
339 float min_phase_diff;
340 float max_phase_diff;
341 double best_sum = 0;
342 float lowest_max_min_diff = 99999;
343
344 int to_consume = 0;
345 int sample_number = 0;
346 bool end = false;
347 bool result = false;
348 circular_buffer_float::iterator buffer_iter;
349
350 /**@name Possible states of FCCH search algorithm*/
351 //@{
352 enum states
piotr437f5462014-02-04 17:57:25 +0100353 {
piotr437f5462014-02-04 17:57:25 +0100354 init, ///< initialize variables
355 search, ///< search for positive samples
356 found_something, ///< search for FCCH and the best position of it
357 fcch_found, ///< when FCCH was found
358 search_fail ///< when there is no FCCH in the input vector
piotrd0bf1492014-02-05 17:27:32 +0100359 } fcch_search_state;
360 //@}
piotr437f5462014-02-04 17:57:25 +0100361
piotrd0bf1492014-02-05 17:27:32 +0100362 fcch_search_state = init;
piotr437f5462014-02-04 17:57:25 +0100363
piotrd0bf1492014-02-05 17:27:32 +0100364 while (!end)
365 {
366 switch (fcch_search_state)
367 {
piotr437f5462014-02-04 17:57:25 +0100368
piotrd0bf1492014-02-05 17:27:32 +0100369 case init: //initialize variables
piotr437f5462014-02-04 17:57:25 +0100370 hit_count = 0;
371 miss_count = 0;
372 start_pos = -1;
373 lowest_max_min_diff = 99999;
374 phase_diff_buffer.clear();
375 fcch_search_state = search;
376
377 break;
378
piotrd0bf1492014-02-05 17:27:32 +0100379 case search: // search for positive samples
piotr437f5462014-02-04 17:57:25 +0100380 sample_number++;
381
piotrd0bf1492014-02-05 17:27:32 +0100382 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because
383 {
384 //there's too few samples left to look into,
385 to_consume = sample_number; //don't do anything with those samples which are left
386 //and consume only those which were checked
387 fcch_search_state = search_fail;
388 }
389 else
390 {
391 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
piotr437f5462014-02-04 17:57:25 +0100392
piotrd0bf1492014-02-05 17:27:32 +0100393 if (phase_diff > 0) //if a positive phase difference was found
394 {
395 to_consume = sample_number;
396 fcch_search_state = found_something; //switch to state in which searches for FCCH
397 }
398 else
399 {
400 fcch_search_state = search;
401 }
piotr437f5462014-02-04 17:57:25 +0100402 }
403
404 break;
405
piotrd0bf1492014-02-05 17:27:32 +0100406 case found_something: // search for FCCH and the best position of it
407 {
408 if (phase_diff > 0)
409 {
piotr437f5462014-02-04 17:57:25 +0100410 hit_count++; //positive phase differencies increases hits_count
piotrd0bf1492014-02-05 17:27:32 +0100411 }
412 else
413 {
piotr437f5462014-02-04 17:57:25 +0100414 miss_count++; //negative increases miss_count
piotrd0bf1492014-02-05 17:27:32 +0100415 }
piotr437f5462014-02-04 17:57:25 +0100416
piotrd0bf1492014-02-05 17:27:32 +0100417 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
418 {
piotr437f5462014-02-04 17:57:25 +0100419 //if miss_count exceeds limit before hit_count
420 fcch_search_state = init; //go to init
421 continue;
piotrd0bf1492014-02-05 17:27:32 +0100422 }
423 else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
424 {
piotr437f5462014-02-04 17:57:25 +0100425 //if hit_count and miss_count exceeds limit then FCCH was found
426 fcch_search_state = fcch_found;
427 continue;
piotrd0bf1492014-02-05 17:27:32 +0100428 }
429 else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR))
430 {
piotr437f5462014-02-04 17:57:25 +0100431 //find difference between minimal and maximal element in the buffer
432 //for FCCH this value should be low
433 //this part is searching for a region where this value is lowest
434 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
435 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
436
piotrd0bf1492014-02-05 17:27:32 +0100437 if (lowest_max_min_diff > max_phase_diff - min_phase_diff)
438 {
439 lowest_max_min_diff = max_phase_diff - min_phase_diff;
440 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
441 best_sum = 0;
piotr437f5462014-02-04 17:57:25 +0100442
piotrd0bf1492014-02-05 17:27:32 +0100443 for (buffer_iter = phase_diff_buffer.begin();
444 buffer_iter != (phase_diff_buffer.end());
445 buffer_iter++)
446 {
447 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
448 }
piotr437f5462014-02-04 17:57:25 +0100449 }
piotrd0bf1492014-02-05 17:27:32 +0100450 }
piotr437f5462014-02-04 17:57:25 +0100451
piotrd0bf1492014-02-05 17:27:32 +0100452 sample_number++;
piotr437f5462014-02-04 17:57:25 +0100453
piotrd0bf1492014-02-05 17:27:32 +0100454 if (sample_number >= nitems) //if there's no single sample left to check
455 {
piotr437f5462014-02-04 17:57:25 +0100456 fcch_search_state = search_fail;//FCCH search failed
457 continue;
piotr437f5462014-02-04 17:57:25 +0100458 }
piotrd0bf1492014-02-05 17:27:32 +0100459
460 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
461 phase_diff_buffer.push_back(phase_diff);
462 fcch_search_state = found_something;
463 }
464 break;
465
466 case fcch_found:
467 {
468 DCOUT("fcch found on position: " << d_counter + start_pos);
469 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
470
471 d_fcch_start_pos = d_counter + start_pos;
472
473 //compute frequency offset
474 double phase_offset = best_sum / FCCH_HITS_NEEDED;
475 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
476 d_freq_offset -= freq_offset;
477 DCOUT("freq_offset: " << d_freq_offset);
478
479 end = true;
480 result = true;
piotr437f5462014-02-04 17:57:25 +0100481 break;
piotrd0bf1492014-02-05 17:27:32 +0100482 }
piotr437f5462014-02-04 17:57:25 +0100483
piotrd0bf1492014-02-05 17:27:32 +0100484 case search_fail:
piotr437f5462014-02-04 17:57:25 +0100485 end = true;
486 result = false;
487 break;
488 }
piotr437f5462014-02-04 17:57:25 +0100489 }
490
piotrd0bf1492014-02-05 17:27:32 +0100491 d_counter += to_consume;
492 consume_each(to_consume);
piotr437f5462014-02-04 17:57:25 +0100493
piotrd0bf1492014-02-05 17:27:32 +0100494 return result;
495}
496
497
498double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
499{
500 double phase_sum = 0;
501 unsigned ii;
502
503 for (ii = first_sample; ii < last_sample; ii++)
piotr437f5462014-02-04 17:57:25 +0100504 {
piotr437f5462014-02-04 17:57:25 +0100505 double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
506 phase_sum += phase_diff;
piotr437f5462014-02-04 17:57:25 +0100507 }
508
piotrd0bf1492014-02-05 17:27:32 +0100509 double phase_offset = phase_sum / (last_sample - first_sample);
510 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
511 return freq_offset;
512}
piotr437f5462014-02-04 17:57:25 +0100513
piotrd0bf1492014-02-05 17:27:32 +0100514void receiver_impl::set_frequency(double freq_offset)
515{
516 d_tuner->calleval(freq_offset);
517}
piotr437f5462014-02-04 17:57:25 +0100518
piotrd0bf1492014-02-05 17:27:32 +0100519inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
520{
521 gr_complex conjprod = val1 * conj(val2);
522 return fast_atan2f(imag(conjprod), real(conjprod));
523}
piotr437f5462014-02-04 17:57:25 +0100524
piotrd0bf1492014-02-05 17:27:32 +0100525bool receiver_impl::reach_sch_burst(const int nitems)
526{
527 //it just consumes samples to get near to a SCH burst
528 int to_consume = 0;
529 bool result = false;
530 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
531
532 //consume samples until d_counter will be equal to sample_nr_near_sch_start
533 if (d_counter < sample_nr_near_sch_start)
534 {
535 if (d_counter + nitems >= sample_nr_near_sch_start)
536 {
537 to_consume = sample_nr_near_sch_start - d_counter;
538 }
539 else
540 {
541 to_consume = nitems;
piotr437f5462014-02-04 17:57:25 +0100542 }
543 result = false;
piotrd0bf1492014-02-05 17:27:32 +0100544 }
545 else
546 {
piotr437f5462014-02-04 17:57:25 +0100547 to_consume = 0;
548 result = true;
piotr437f5462014-02-04 17:57:25 +0100549 }
550
piotrd0bf1492014-02-05 17:27:32 +0100551 d_counter += to_consume;
552 consume_each(to_consume);
553 return result;
554}
555
556int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
557{
558 vector_complex correlation_buffer;
559 vector_float power_buffer;
560 vector_float window_energy_buffer;
561
562 int strongest_window_nr;
563 int burst_start = 0;
564 int chan_imp_resp_center = 0;
565 float max_correlation = 0;
566 float energy = 0;
567
568 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++)
piotr437f5462014-02-04 17:57:25 +0100569 {
piotr437f5462014-02-04 17:57:25 +0100570 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
571 correlation_buffer.push_back(correlation);
572 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100573 }
piotr437f5462014-02-04 17:57:25 +0100574
piotrd0bf1492014-02-05 17:27:32 +0100575 //compute window energies
576 vector_float::iterator iter = power_buffer.begin();
577 bool loop_end = false;
578 while (iter != power_buffer.end())
579 {
piotr437f5462014-02-04 17:57:25 +0100580 vector_float::iterator iter_ii = iter;
581 energy = 0;
582
piotrd0bf1492014-02-05 17:27:32 +0100583 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++)
584 {
585 if (iter_ii == power_buffer.end())
586 {
587 loop_end = true;
588 break;
589 }
590 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100591 }
piotrd0bf1492014-02-05 17:27:32 +0100592 if (loop_end)
593 {
594 break;
piotr437f5462014-02-04 17:57:25 +0100595 }
596 iter++;
597 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100598 }
piotr437f5462014-02-04 17:57:25 +0100599
piotrd0bf1492014-02-05 17:27:32 +0100600 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100601 // d_channel_imp_resp.clear();
602
piotrd0bf1492014-02-05 17:27:32 +0100603 max_correlation = 0;
604 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++)
605 {
piotr437f5462014-02-04 17:57:25 +0100606 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100607 if (abs(correlation) > max_correlation)
608 {
609 chan_imp_resp_center = ii;
610 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100611 }
piotrd0bf1492014-02-05 17:27:32 +0100612 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100613 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100614 }
615
piotrd0bf1492014-02-05 17:27:32 +0100616 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
617 return burst_start;
618}
piotr437f5462014-02-04 17:57:25 +0100619
620
piotrd0bf1492014-02-05 17:27:32 +0100621
622void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
623{
624 float output[BURST_SIZE];
625 gr_complex rhh_temp[CHAN_IMP_RESP_LENGTH*d_OSR];
626 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
627 gr_complex filtered_burst[BURST_SIZE];
628 int start_state = 3;
629 unsigned int stop_states[2] = {4, 12};
630
631 autocorrelation(chan_imp_resp, rhh_temp, d_chan_imp_length*d_OSR);
632 for (int ii = 0; ii < (d_chan_imp_length); ii++)
piotr437f5462014-02-04 17:57:25 +0100633 {
piotr437f5462014-02-04 17:57:25 +0100634 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
piotr437f5462014-02-04 17:57:25 +0100635 }
636
piotrd0bf1492014-02-05 17:27:32 +0100637 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
638
639 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
640
641 for (int i = 0; i < BURST_SIZE ; i++)
piotr437f5462014-02-04 17:57:25 +0100642 {
piotrd0bf1492014-02-05 17:27:32 +0100643 output_binary[i] = (output[i] > 0);
644 }
645}
piotr437f5462014-02-04 17:57:25 +0100646
piotrd0bf1492014-02-05 17:27:32 +0100647//TODO consider placing this funtion in a separate class for signal processing
648void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
649{
650 gr_complex j = gr_complex(0.0, 1.0);
piotr437f5462014-02-04 17:57:25 +0100651
piotrd0bf1492014-02-05 17:27:32 +0100652 int current_symbol;
653 int encoded_symbol;
654 int previous_symbol = 2 * input[0] - 1;
655 gmsk_output[0] = start_point;
656
657 for (int i = 1; i < nitems; i++)
658 {
piotr437f5462014-02-04 17:57:25 +0100659 //change bits representation to NRZ
660 current_symbol = 2 * input[i] - 1;
661 //differentially encode
662 encoded_symbol = current_symbol * previous_symbol;
663 //and do gmsk mapping
664 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
665 previous_symbol = current_symbol;
piotr437f5462014-02-04 17:57:25 +0100666 }
piotrd0bf1492014-02-05 17:27:32 +0100667}
piotr437f5462014-02-04 17:57:25 +0100668
piotrd0bf1492014-02-05 17:27:32 +0100669//TODO consider use of some generalized function for correlation and placing it in a separate class for signal processing
670gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
671{
672 gr_complex result(0.0, 0.0);
673 int sample_number = 0;
674
675 for (int ii = 0; ii < length; ii++)
piotr437f5462014-02-04 17:57:25 +0100676 {
piotr437f5462014-02-04 17:57:25 +0100677 sample_number = (ii * d_OSR) ;
678 result += sequence[ii] * conj(input[sample_number]);
piotr437f5462014-02-04 17:57:25 +0100679 }
680
piotrd0bf1492014-02-05 17:27:32 +0100681 result = result / gr_complex(length, 0);
682 return result;
683}
684
685//computes autocorrelation for positive arguments
686//TODO consider placing this funtion in a separate class for signal processing
687inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
688{
689 int i, k;
690 for (k = nitems - 1; k >= 0; k--)
piotr437f5462014-02-04 17:57:25 +0100691 {
piotr437f5462014-02-04 17:57:25 +0100692 out[k] = gr_complex(0, 0);
piotrd0bf1492014-02-05 17:27:32 +0100693 for (i = k; i < nitems; i++)
694 {
695 out[k] += input[i] * conj(input[i-k]);
piotr437f5462014-02-04 17:57:25 +0100696 }
piotr437f5462014-02-04 17:57:25 +0100697 }
piotrd0bf1492014-02-05 17:27:32 +0100698}
piotr437f5462014-02-04 17:57:25 +0100699
piotrd0bf1492014-02-05 17:27:32 +0100700//TODO consider use of some generalized function for filtering and placing it in a separate class for signal processing
701inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
702{
703 int ii = 0, n, a;
704
705 for (n = 0; n < nitems; n++)
piotr437f5462014-02-04 17:57:25 +0100706 {
piotr437f5462014-02-04 17:57:25 +0100707 a = n * d_OSR;
708 output[n] = 0;
709 ii = 0;
710
piotrd0bf1492014-02-05 17:27:32 +0100711 while (ii < filter_length)
712 {
713 if ((a + ii) >= nitems*d_OSR)
714 break;
715 output[n] += input[a+ii] * filter[ii];
716 ii++;
piotr437f5462014-02-04 17:57:25 +0100717 }
piotr437f5462014-02-04 17:57:25 +0100718 }
piotrd0bf1492014-02-05 17:27:32 +0100719}
piotr437f5462014-02-04 17:57:25 +0100720
piotrd0bf1492014-02-05 17:27:32 +0100721//TODO: get_norm_chan_imp_resp is similar to get_sch_chan_imp_resp - consider joining this two functions
722//TODO: this is place where most errors are introduced and can be corrected by improvements to this fuction
723//especially computations of strongest_window_nr
724int receiver_impl::get_norm_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp, int bcc)
725{
726 vector_complex correlation_buffer;
727 vector_float power_buffer;
728 vector_float window_energy_buffer;
piotr437f5462014-02-04 17:57:25 +0100729
piotrd0bf1492014-02-05 17:27:32 +0100730 int strongest_window_nr;
731 int burst_start = 0;
732 int chan_imp_resp_center = 0;
733 float max_correlation = 0;
734 float energy = 0;
piotr437f5462014-02-04 17:57:25 +0100735
piotrd0bf1492014-02-05 17:27:32 +0100736 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
737 int search_start_pos = search_center + 1;
piotr437f5462014-02-04 17:57:25 +0100738 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
piotrd0bf1492014-02-05 17:27:32 +0100739 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 2 * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100740
piotrd0bf1492014-02-05 17:27:32 +0100741 for (int ii = search_start_pos; ii < search_stop_pos; ii++)
742 {
piotr437f5462014-02-04 17:57:25 +0100743 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
744
745 correlation_buffer.push_back(correlation);
746 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100747 }
piotr437f5462014-02-04 17:57:25 +0100748
piotrd0bf1492014-02-05 17:27:32 +0100749 //compute window energies
750 vector_float::iterator iter = power_buffer.begin();
751 bool loop_end = false;
752 while (iter != power_buffer.end())
753 {
piotr437f5462014-02-04 17:57:25 +0100754 vector_float::iterator iter_ii = iter;
755 energy = 0;
756
piotrd0bf1492014-02-05 17:27:32 +0100757 for (int ii = 0; ii < (d_chan_imp_length - 2)*d_OSR; ii++, iter_ii++)
758 {
759 // for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++, iter_ii++) {
760 if (iter_ii == power_buffer.end())
761 {
762 loop_end = true;
763 break;
764 }
765 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100766 }
piotrd0bf1492014-02-05 17:27:32 +0100767 if (loop_end)
768 {
769 break;
piotr437f5462014-02-04 17:57:25 +0100770 }
771 iter++;
772
773 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100774 }
775 //!why doesn't this work
776 int strongest_window_nr_new = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
777 strongest_window_nr = 3; //! so I have to override it here
piotr437f5462014-02-04 17:57:25 +0100778
piotrd0bf1492014-02-05 17:27:32 +0100779 max_correlation = 0;
780 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++)
781 {
piotr437f5462014-02-04 17:57:25 +0100782 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100783 if (abs(correlation) > max_correlation)
784 {
785 chan_imp_resp_center = ii;
786 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100787 }
piotrd0bf1492014-02-05 17:27:32 +0100788 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100789 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100790 }
piotrd0bf1492014-02-05 17:27:32 +0100791 // We want to use the first sample of the impulseresponse, and the
792 // corresponding samples of the received signal.
793 // the variable sync_w should contain the beginning of the used part of
794 // training sequence, which is 3+57+1+6=67 bits into the burst. That is
795 // we have that sync_t16 equals first sample in bit number 67.
796
797 burst_start = search_start_pos + chan_imp_resp_center + strongest_window_nr - TRAIN_POS * d_OSR;
798
799 // GMSK modulator introduces ISI - each bit is expanded for 3*Tb
800 // and it's maximum value is in the last bit period, so burst starts
801 // 2*Tb earlier
802 burst_start -= 2 * d_OSR;
803 burst_start += 2;
804 //COUT("Poczatek ###############################");
805 //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";
806 //COUT("burst_start_new: " << (search_start_pos + strongest_window_nr_new - TRAIN_POS * d_OSR));
807 burst_start=(search_start_pos + strongest_window_nr_new - TRAIN_POS * d_OSR);
808 return burst_start;
809}
piotr437f5462014-02-04 17:57:25 +0100810
811
piotrd0bf1492014-02-05 17:27:32 +0100812void receiver_impl::process_normal_burst(burst_counter burst_nr, const unsigned char * burst_binary)
813{
814 int ii;
815 //std::cout << "fn:" <<burst_nr.get_frame_nr() << " ts" << burst_nr.get_timeslot_nr() << " ";
816 for(ii=0; ii<148; ii++)
piotr437f5462014-02-04 17:57:25 +0100817 {
piotr5f1e1d32014-02-05 18:10:05 +0100818 std::cout << std::setprecision(1) << static_cast<int>(burst_binary[ii]) << "";
piotr437f5462014-02-04 17:57:25 +0100819 }
piotrd0bf1492014-02-05 17:27:32 +0100820 std::cout << std::endl;
821}
822//TODO: this shouldn't be here also - the same reason
823void receiver_impl::configure_receiver()
824{
825 d_channel_conf.set_multiframe_type(TSC0, multiframe_51);
826 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +0100827
piotrd0bf1492014-02-05 17:27:32 +0100828 d_channel_conf.set_burst_types(TSC0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
829 d_channel_conf.set_burst_types(TSC0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
piotr437f5462014-02-04 17:57:25 +0100830
831 // d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_26);
832 // d_channel_conf.set_burst_types(TIMESLOT1, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
833 // d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_26);
834 // d_channel_conf.set_burst_types(TIMESLOT2, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
835 // d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_26);
836 // d_channel_conf.set_burst_types(TIMESLOT3, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
837 // d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_26);
838 // d_channel_conf.set_burst_types(TIMESLOT4, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
839 // d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_26);
840 // d_channel_conf.set_burst_types(TIMESLOT5, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
841 // d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_26);
842 // d_channel_conf.set_burst_types(TIMESLOT6, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal);
843 // d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_26);
844 // 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 +0100845 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
846 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
847 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
848 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
849 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
850 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
851 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
852 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
853 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
854 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
855 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
856 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
857 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
858 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
859
860}
piotr437f5462014-02-04 17:57:25 +0100861
862
piotrd0bf1492014-02-05 17:27:32 +0100863} /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +0100864} /* namespace gr */
865