blob: d089c93f37c9315da66cc2be760f3b708248bd5c [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001/* -*- c++ -*- */
piotrd0bf1492014-02-05 17:27:32 +01002/*
ptrkrysik529895b2014-12-02 18:07:38 +01003 * @file
4 * @author Piotr Krysik <ptrkrysik@gmail.com>
5 * @section LICENSE
piotrd0bf1492014-02-05 17:27:32 +01006 *
ptrkrysik529895b2014-12-02 18:07:38 +01007 * Gr-gsm is free software; you can redistribute it and/or modify
piotr437f5462014-02-04 17:57:25 +01008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
piotrd0bf1492014-02-05 17:27:32 +010011 *
ptrkrysik529895b2014-12-02 18:07:38 +010012 * Gr-gsm is distributed in the hope that it will be useful,
piotr437f5462014-02-04 17:57:25 +010013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
piotrd0bf1492014-02-05 17:27:32 +010016 *
piotr437f5462014-02-04 17:57:25 +010017 * You should have received a copy of the GNU General Public License
ptrkrysik529895b2014-12-02 18:07:38 +010018 * along with gr-gsm; see the file COPYING. If not, write to
piotr437f5462014-02-04 17:57:25 +010019 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <gnuradio/io_signature.h>
piotr437f5462014-02-04 17:57:25 +010028#include <gnuradio/math.h>
29#include <math.h>
30#include <boost/circular_buffer.hpp>
31#include <algorithm>
32#include <numeric>
David Holmf2497bd2014-12-01 21:22:37 +010033#include <vector>
piotr437f5462014-02-04 17:57:25 +010034#include <viterbi_detector.h>
35#include <string.h>
piotr437f5462014-02-04 17:57:25 +010036#include <iostream>
37#include <iomanip>
piotr6d152d92014-02-21 00:02:44 +010038#include <boost/scoped_ptr.hpp>
ptrkrysik3be74a72014-12-13 10:11:00 +010039
40#include <sch.h>
41#include "receiver_impl.h"
42#include <grgsm/endian.h>
ptrkrysik58213792014-10-30 09:05:15 +010043
ptrkrysikd85d4602014-11-13 10:11:53 +010044//files included for debuging
45//#include "plotting/plotting.hpp"
46//#include <pthread.h>
piotr437f5462014-02-04 17:57:25 +010047
48#define SYNC_SEARCH_RANGE 30
49
piotrd0bf1492014-02-05 17:27:32 +010050namespace gr
51{
52namespace gsm
53{
piotrd0bf1492014-02-05 17:27:32 +010054receiver::sptr
ptrkrysik7a7b9b02014-11-19 11:27:34 +010055receiver::make(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums)
piotrd0bf1492014-02-05 17:27:32 +010056{
57 return gnuradio::get_initial_sptr
ptrkrysike518bbf2014-11-06 14:50:59 +010058 (new receiver_impl(osr, cell_allocation, tseq_nums));
piotrd0bf1492014-02-05 17:27:32 +010059}
60
61/*
62 * The private constructor
63 */
ptrkrysik7a7b9b02014-11-19 11:27:34 +010064receiver_impl::receiver_impl(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums)
piotrc7c249a2014-05-02 17:24:08 +020065 : gr::sync_block("receiver",
ptrkrysik58213792014-10-30 09:05:15 +010066 gr::io_signature::make(1, -1, sizeof(gr_complex)),
piotr7c82b172014-02-08 14:15:27 +010067 gr::io_signature::make(0, 0, 0)),
piotrd0bf1492014-02-05 17:27:32 +010068 d_OSR(osr),
69 d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
piotrd0bf1492014-02-05 17:27:32 +010070 d_counter(0),
71 d_fcch_start_pos(0),
piotr4089c1a2014-08-06 14:10:56 +020072 d_freq_offset_setting(0),
piotrd6d66872014-08-06 15:20:33 +020073 d_state(fcch_search),
piotrd0bf1492014-02-05 17:27:32 +010074 d_burst_nr(osr),
piotr6d152d92014-02-21 00:02:44 +010075 d_failed_sch(0),
ptrkrysike518bbf2014-11-06 14:50:59 +010076 d_signal_dbm(-120),
77 d_tseq_nums(tseq_nums),
78 d_cell_allocation(cell_allocation)
piotrd0bf1492014-02-05 17:27:32 +010079{
80 int i;
piotr4089c1a2014-08-06 14:10:56 +020081 //don't send samples to the receiver until there are at least samples for one
piotr7f3f3662014-07-08 16:47:53 +020082 set_output_multiple(floor((TS_BITS + 2 * GUARD_PERIOD) * d_OSR)); // burst and two gurad periods (one gurard period is an arbitrary overlap)
piotrd0bf1492014-02-05 17:27:32 +010083 gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0));
84 for (i = 0; i < TRAIN_SEQ_NUM; i++)
piotr437f5462014-02-04 17:57:25 +010085 {
piotrf502e0f2014-04-24 10:28:29 +020086 gr_complex startpoint = (train_seq[i][0]==0) ? gr_complex(1.0, 0.0) : gr_complex(-1.0, 0.0); //if first bit of the seqeunce ==0 first symbol ==1
piotr7f3f3662014-07-08 16:47:53 +020087 //if first bit of the seqeunce ==1 first symbol ==-1
piotr437f5462014-02-04 17:57:25 +010088 gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint);
piotr437f5462014-02-04 17:57:25 +010089 }
ptrkrysike518bbf2014-11-06 14:50:59 +010090 message_port_register_out(pmt::mp("C0"));
91 message_port_register_out(pmt::mp("CX"));
piotr4089c1a2014-08-06 14:10:56 +020092 message_port_register_out(pmt::mp("measurements"));
piotr903b1d62014-04-17 11:33:27 +020093 configure_receiver(); //configure the receiver - tell it where to find which burst type
piotrd0bf1492014-02-05 17:27:32 +010094}
piotr437f5462014-02-04 17:57:25 +010095
piotrd0bf1492014-02-05 17:27:32 +010096/*
97 * Our virtual destructor.
98 */
99receiver_impl::~receiver_impl()
100{
101}
102
piotrd0bf1492014-02-05 17:27:32 +0100103int
piotrc7c249a2014-05-02 17:24:08 +0200104receiver_impl::work(int noutput_items,
105 gr_vector_const_void_star &input_items,
106 gr_vector_void_star &output_items)
piotrd0bf1492014-02-05 17:27:32 +0100107{
ptrkrysik58213792014-10-30 09:05:15 +0100108// std::vector<const gr_complex *> iii = (std::vector<const gr_complex *>) input_items; // jak zrobić to rzutowanie poprawnie
109 gr_complex * input = (gr_complex *) input_items[0];
piotr4089c1a2014-08-06 14:10:56 +0200110 std::vector<tag_t> freq_offset_tags;
111 uint64_t start = nitems_read(0);
112 uint64_t stop = start + noutput_items;
piotr7c82b172014-02-08 14:15:27 +0100113
piotr4089c1a2014-08-06 14:10:56 +0200114 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
115 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
116 bool freq_offset_tag_in_fcch = false;
117 uint64_t tag_offset=-1; //-1 - just some clearly invalid value
118
119 if(!freq_offset_tags.empty()){
120 tag_t freq_offset_tag = freq_offset_tags[0];
121 tag_offset = freq_offset_tag.offset - start;
122
123 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
124 if(d_state == synchronized && b_type == fcch_burst){
125 uint64_t last_sample_nr = ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1;
126 if(tag_offset < last_sample_nr){
piotr4089c1a2014-08-06 14:10:56 +0200127 freq_offset_tag_in_fcch = true;
128 }
129 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
130 } else {
131 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
132 }
133 }
134
piotrd0bf1492014-02-05 17:27:32 +0100135 switch (d_state)
piotr437f5462014-02-04 17:57:25 +0100136 {
piotrd0bf1492014-02-05 17:27:32 +0100137 //bootstrapping
ptrkrysik58213792014-10-30 09:05:15 +0100138 case fcch_search:
piotrd0bf1492014-02-05 17:27:32 +0100139 {
piotr4089c1a2014-08-06 14:10:56 +0200140 double freq_offset_tmp;
141 if (find_fcch_burst(input, noutput_items,freq_offset_tmp))
piotrd0bf1492014-02-05 17:27:32 +0100142 {
piotrd6d66872014-08-06 15:20:33 +0200143 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("fcch_search"));
piotr4089c1a2014-08-06 14:10:56 +0200144 message_port_pub(pmt::mp("measurements"), msg);
145
piotrd0bf1492014-02-05 17:27:32 +0100146 d_state = sch_search;
147 }
148 else
149 {
piotrd6d66872014-08-06 15:20:33 +0200150 d_state = fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100151 }
152 break;
153 }
piotr437f5462014-02-04 17:57:25 +0100154
piotrd0bf1492014-02-05 17:27:32 +0100155 case sch_search:
156 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100157 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100158 int t1, t2, t3;
159 int burst_start = 0;
160 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100161
piotrc7c249a2014-05-02 17:24:08 +0200162 if (reach_sch_burst(noutput_items)) //wait for a SCH burst
piotrd0bf1492014-02-05 17:27:32 +0100163 {
164 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it
165 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection
166 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst
167 {
piotr437f5462014-02-04 17:57:25 +0100168 d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value
piotr437f5462014-02-04 17:57:25 +0100169 d_burst_nr++;
170
piotr7f3f3662014-07-08 16:47:53 +0200171 consume_each(burst_start + BURST_SIZE * d_OSR + 4*d_OSR); //consume samples up to next guard period
piotr437f5462014-02-04 17:57:25 +0100172 d_state = synchronized;
piotrd0bf1492014-02-05 17:27:32 +0100173 }
174 else
175 {
piotrd6d66872014-08-06 15:20:33 +0200176 d_state = fcch_search; //if there is error in the sch burst go back to fcch search phase
piotr437f5462014-02-04 17:57:25 +0100177 }
piotrd0bf1492014-02-05 17:27:32 +0100178 }
179 else
180 {
181 d_state = sch_search;
182 }
183 break;
184 }
185 //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
186 case synchronized:
187 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100188 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100189 int offset = 0;
190 int to_consume = 0;
191 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100192
ptrkrysik58213792014-10-30 09:05:15 +0100193 burst_type b_type;
piotr6d152d92014-02-21 00:02:44 +0100194
ptrkrysike518bbf2014-11-06 14:50:59 +0100195 for(int input_nr=0; input_nr<d_cell_allocation.size(); input_nr++)
piotrd0bf1492014-02-05 17:27:32 +0100196 {
ptrkrysik58213792014-10-30 09:05:15 +0100197 double signal_pwr = 0;
198 input = (gr_complex *)input_items[input_nr];
piotr4089c1a2014-08-06 14:10:56 +0200199
ptrkrysik58213792014-10-30 09:05:15 +0100200 for(int ii=GUARD_PERIOD;ii<TS_BITS;ii++)
piotrd0bf1492014-02-05 17:27:32 +0100201 {
ptrkrysik58213792014-10-30 09:05:15 +0100202 signal_pwr += abs(input[ii])*abs(input[ii]);
piotrd0bf1492014-02-05 17:27:32 +0100203 }
ptrkrysik58213792014-10-30 09:05:15 +0100204 signal_pwr = signal_pwr/(TS_BITS);
205 d_signal_dbm = round(10*log10(signal_pwr/50));
206 if(input_nr==0){
207 d_c0_signal_dbm = d_signal_dbm;
208 }
209
210 if(input_nr==0) //for c0 channel burst type is controlled by channel configuration
piotrd0bf1492014-02-05 17:27:32 +0100211 {
ptrkrysik58213792014-10-30 09:05:15 +0100212 b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number
213 }
214 else
215 {
216 b_type = normal_or_noise; //for the rest it can be only normal burst or noise (at least at this moment of development)
217 }
218
219 switch (b_type)
220 {
221 case fcch_burst: //if it's FCCH burst
222 {
223 const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
224 const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
225 double freq_offset_tmp = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it
226
ptrkrysik617ba032014-11-21 10:11:05 +0100227 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100228
229 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("synchronized"));
230 message_port_pub(pmt::mp("measurements"), msg);
231 break;
232 }
233 case sch_burst: //if it's SCH burst
234 {
235 int t1, t2, t3, d_ncc, d_bcc;
236 d_c0_burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response
237
238 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100239 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_SCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100240 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data
piotrd0bf1492014-02-05 17:27:32 +0100241 {
ptrkrysik58213792014-10-30 09:05:15 +0100242 // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct
243 d_failed_sch = 0;
244 offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period
245 to_consume += offset; //adjust with offset number of samples to be consumed
piotr437f5462014-02-04 17:57:25 +0100246 }
ptrkrysik58213792014-10-30 09:05:15 +0100247 else
248 {
249 d_failed_sch++;
250 if (d_failed_sch >= MAX_SCH_ERRORS)
251 {
252 d_state = fcch_search;
253 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(0.0),pmt::mp("sync_loss"));
254 message_port_pub(pmt::mp("measurements"), msg);
ptrkrysikd57745d2014-12-02 19:05:36 +0100255 //DCOUT("Re-Synchronization!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
ptrkrysik58213792014-10-30 09:05:15 +0100256 }
257 }
258 break;
piotr437f5462014-02-04 17:57:25 +0100259 }
ptrkrysik58213792014-10-30 09:05:15 +0100260 case normal_burst:
261 {
262 float normal_corr_max; //if it's normal burst
263 d_c0_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc); //get channel impulse response for given training sequence number - d_bcc
264 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100265 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100266 break;
267 }
268 case dummy_or_normal:
269 {
270 unsigned int normal_burst_start, dummy_burst_start;
271 float dummy_corr_max, normal_corr_max;
piotr437f5462014-02-04 17:57:25 +0100272
ptrkrysik58213792014-10-30 09:05:15 +0100273 dummy_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY);
274 normal_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc);
275
276 if (normal_corr_max > dummy_corr_max)
277 {
278 d_c0_burst_start = normal_burst_start;
279 detect_burst(input, &channel_imp_resp[0], normal_burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100280 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100281 }
282 else
283 {
284 d_c0_burst_start = dummy_burst_start;
ptrkrysik617ba032014-11-21 10:11:05 +0100285 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100286 }
287 break;
piotrd0bf1492014-02-05 17:27:32 +0100288 }
ptrkrysik58213792014-10-30 09:05:15 +0100289 case rach_burst:
290 break;
291 case dummy:
ptrkrysik617ba032014-11-21 10:11:05 +0100292 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100293 break;
294 case normal_or_noise:
295 {
296 unsigned int burst_start;
297 float normal_corr_max_tmp;
298 float normal_corr_max=-1e6;
299 int max_tn;
300 std::vector<gr_complex> v(input, input + noutput_items);
301 if(d_signal_dbm>=d_c0_signal_dbm-13)
302 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100303 if(d_tseq_nums.size()==0) //there is no information about training sequence
304 { //however the receiver can detect it
305 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, 0);
306 float ts_max=normal_corr_max; //with use of a very simple algorithm based on finding
307 int ts_max_num=0; //maximum correlation
308 for(int ss=1; ss<=7; ss++)
309 {
310 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, ss);
311 if(ts_max<normal_corr_max)
312 {
313 ts_max = normal_corr_max;
314 ts_max_num = ss;
315 }
316 }
317 d_tseq_nums.push_back(ts_max_num);
ptrkrysik58213792014-10-30 09:05:15 +0100318 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100319 int tseq_num;
320 if(input_nr<=d_tseq_nums.size()){
321 tseq_num = d_tseq_nums[input_nr-1];
322 } else {
323 tseq_num = d_tseq_nums.back();
324 }
325 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, tseq_num);
326// if(abs(d_c0_burst_start-burst_start)<=2){ //unused check/filter based on timing
327 if((normal_corr_max/sqrt(signal_pwr))>=0.9){
328 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100329 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysike518bbf2014-11-06 14:50:59 +0100330 }
ptrkrysik58213792014-10-30 09:05:15 +0100331 }
332 break;
333 }
334 case empty: //if it's empty burst
335 break; //do nothing
336 }
337
338 if(input_nr==0)
339 {
340 d_burst_nr++; //go to next burst
341 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); //consume samples of the burst up to next guard period
342 }
343
344 if(input_nr==input_items.size()-1)
345 {
346 consume_each(to_consume);
347 }
348 //and add offset which is introduced by
349 //0.25 fractional part of a guard period
350 }
piotrd0bf1492014-02-05 17:27:32 +0100351 }
352 break;
piotr437f5462014-02-04 17:57:25 +0100353 }
piotr6d152d92014-02-21 00:02:44 +0100354 return 0;
piotrd0bf1492014-02-05 17:27:32 +0100355}
piotr437f5462014-02-04 17:57:25 +0100356
piotr4089c1a2014-08-06 14:10:56 +0200357bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset)
piotrd0bf1492014-02-05 17:27:32 +0100358{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100359 boost::circular_buffer<float> phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR); //circular buffer used to scan throug signal to find
piotrd0bf1492014-02-05 17:27:32 +0100360 //best match for FCCH burst
361 float phase_diff = 0;
362 gr_complex conjprod;
363 int start_pos = -1;
364 int hit_count = 0;
365 int miss_count = 0;
366 float min_phase_diff;
367 float max_phase_diff;
368 double best_sum = 0;
369 float lowest_max_min_diff = 99999;
370
371 int to_consume = 0;
372 int sample_number = 0;
373 bool end = false;
374 bool result = false;
ptrkrysikef5e2db2015-01-03 12:10:14 +0100375 boost::circular_buffer<float>::iterator buffer_iter;
piotr6d152d92014-02-21 00:02:44 +0100376
piotrd0bf1492014-02-05 17:27:32 +0100377 /**@name Possible states of FCCH search algorithm*/
378 //@{
379 enum states
piotr437f5462014-02-04 17:57:25 +0100380 {
piotr437f5462014-02-04 17:57:25 +0100381 init, ///< initialize variables
382 search, ///< search for positive samples
383 found_something, ///< search for FCCH and the best position of it
384 fcch_found, ///< when FCCH was found
385 search_fail ///< when there is no FCCH in the input vector
piotrd0bf1492014-02-05 17:27:32 +0100386 } fcch_search_state;
387 //@}
piotr437f5462014-02-04 17:57:25 +0100388
piotrd0bf1492014-02-05 17:27:32 +0100389 fcch_search_state = init;
piotr437f5462014-02-04 17:57:25 +0100390
piotrd0bf1492014-02-05 17:27:32 +0100391 while (!end)
392 {
393 switch (fcch_search_state)
394 {
piotr437f5462014-02-04 17:57:25 +0100395
piotrd0bf1492014-02-05 17:27:32 +0100396 case init: //initialize variables
piotr437f5462014-02-04 17:57:25 +0100397 hit_count = 0;
398 miss_count = 0;
399 start_pos = -1;
400 lowest_max_min_diff = 99999;
401 phase_diff_buffer.clear();
402 fcch_search_state = search;
403
404 break;
405
piotr7c82b172014-02-08 14:15:27 +0100406 case search: // search for positive samples
piotr437f5462014-02-04 17:57:25 +0100407 sample_number++;
408
piotrd0bf1492014-02-05 17:27:32 +0100409 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because
410 {
piotr7c82b172014-02-08 14:15:27 +0100411 //there's too few samples left to look into,
piotrd0bf1492014-02-05 17:27:32 +0100412 to_consume = sample_number; //don't do anything with those samples which are left
piotr7c82b172014-02-08 14:15:27 +0100413 //and consume only those which were checked
piotrd0bf1492014-02-05 17:27:32 +0100414 fcch_search_state = search_fail;
415 }
416 else
417 {
418 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
piotr437f5462014-02-04 17:57:25 +0100419
piotrd0bf1492014-02-05 17:27:32 +0100420 if (phase_diff > 0) //if a positive phase difference was found
421 {
422 to_consume = sample_number;
423 fcch_search_state = found_something; //switch to state in which searches for FCCH
424 }
425 else
426 {
427 fcch_search_state = search;
428 }
piotr437f5462014-02-04 17:57:25 +0100429 }
430
431 break;
432
piotrd0bf1492014-02-05 17:27:32 +0100433 case found_something: // search for FCCH and the best position of it
434 {
435 if (phase_diff > 0)
436 {
piotr437f5462014-02-04 17:57:25 +0100437 hit_count++; //positive phase differencies increases hits_count
piotrd0bf1492014-02-05 17:27:32 +0100438 }
439 else
440 {
piotr437f5462014-02-04 17:57:25 +0100441 miss_count++; //negative increases miss_count
piotrd0bf1492014-02-05 17:27:32 +0100442 }
piotr437f5462014-02-04 17:57:25 +0100443
piotrd0bf1492014-02-05 17:27:32 +0100444 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
445 {
piotr437f5462014-02-04 17:57:25 +0100446 //if miss_count exceeds limit before hit_count
447 fcch_search_state = init; //go to init
448 continue;
piotrd0bf1492014-02-05 17:27:32 +0100449 }
450 else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
451 {
piotr437f5462014-02-04 17:57:25 +0100452 //if hit_count and miss_count exceeds limit then FCCH was found
453 fcch_search_state = fcch_found;
454 continue;
piotrd0bf1492014-02-05 17:27:32 +0100455 }
456 else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR))
457 {
piotr437f5462014-02-04 17:57:25 +0100458 //find difference between minimal and maximal element in the buffer
459 //for FCCH this value should be low
460 //this part is searching for a region where this value is lowest
461 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
462 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
463
piotrd0bf1492014-02-05 17:27:32 +0100464 if (lowest_max_min_diff > max_phase_diff - min_phase_diff)
465 {
466 lowest_max_min_diff = max_phase_diff - min_phase_diff;
467 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
468 best_sum = 0;
piotr437f5462014-02-04 17:57:25 +0100469
piotrd0bf1492014-02-05 17:27:32 +0100470 for (buffer_iter = phase_diff_buffer.begin();
471 buffer_iter != (phase_diff_buffer.end());
472 buffer_iter++)
473 {
474 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
475 }
piotr437f5462014-02-04 17:57:25 +0100476 }
piotrd0bf1492014-02-05 17:27:32 +0100477 }
piotr437f5462014-02-04 17:57:25 +0100478
piotrd0bf1492014-02-05 17:27:32 +0100479 sample_number++;
piotr437f5462014-02-04 17:57:25 +0100480
piotrd0bf1492014-02-05 17:27:32 +0100481 if (sample_number >= nitems) //if there's no single sample left to check
482 {
piotr437f5462014-02-04 17:57:25 +0100483 fcch_search_state = search_fail;//FCCH search failed
484 continue;
piotr437f5462014-02-04 17:57:25 +0100485 }
piotrd0bf1492014-02-05 17:27:32 +0100486
487 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
488 phase_diff_buffer.push_back(phase_diff);
489 fcch_search_state = found_something;
490 }
491 break;
492
493 case fcch_found:
494 {
piotrd0bf1492014-02-05 17:27:32 +0100495 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
496
497 d_fcch_start_pos = d_counter + start_pos;
498
499 //compute frequency offset
500 double phase_offset = best_sum / FCCH_HITS_NEEDED;
piotr4089c1a2014-08-06 14:10:56 +0200501 double freq_offset = phase_offset * 1625000.0/6 / (2 * M_PI); //1625000.0/6 - GMSK symbol rate in GSM
502 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100503
504 end = true;
505 result = true;
piotr437f5462014-02-04 17:57:25 +0100506 break;
piotrd0bf1492014-02-05 17:27:32 +0100507 }
piotr437f5462014-02-04 17:57:25 +0100508
piotrd0bf1492014-02-05 17:27:32 +0100509 case search_fail:
piotr437f5462014-02-04 17:57:25 +0100510 end = true;
511 result = false;
512 break;
513 }
piotr437f5462014-02-04 17:57:25 +0100514 }
515
piotrd0bf1492014-02-05 17:27:32 +0100516 d_counter += to_consume;
517 consume_each(to_consume);
piotr437f5462014-02-04 17:57:25 +0100518
piotrd0bf1492014-02-05 17:27:32 +0100519 return result;
520}
521
piotrd0bf1492014-02-05 17:27:32 +0100522double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
523{
524 double phase_sum = 0;
525 unsigned ii;
526
527 for (ii = first_sample; ii < last_sample; ii++)
piotr437f5462014-02-04 17:57:25 +0100528 {
piotr437f5462014-02-04 17:57:25 +0100529 double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
530 phase_sum += phase_diff;
piotr437f5462014-02-04 17:57:25 +0100531 }
532
piotrd0bf1492014-02-05 17:27:32 +0100533 double phase_offset = phase_sum / (last_sample - first_sample);
534 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
535 return freq_offset;
536}
piotr437f5462014-02-04 17:57:25 +0100537
piotrd0bf1492014-02-05 17:27:32 +0100538inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
539{
540 gr_complex conjprod = val1 * conj(val2);
541 return fast_atan2f(imag(conjprod), real(conjprod));
542}
piotr437f5462014-02-04 17:57:25 +0100543
piotrd0bf1492014-02-05 17:27:32 +0100544bool receiver_impl::reach_sch_burst(const int nitems)
545{
546 //it just consumes samples to get near to a SCH burst
547 int to_consume = 0;
548 bool result = false;
549 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
550
551 //consume samples until d_counter will be equal to sample_nr_near_sch_start
552 if (d_counter < sample_nr_near_sch_start)
553 {
554 if (d_counter + nitems >= sample_nr_near_sch_start)
555 {
556 to_consume = sample_nr_near_sch_start - d_counter;
557 }
558 else
559 {
560 to_consume = nitems;
piotr437f5462014-02-04 17:57:25 +0100561 }
562 result = false;
piotrd0bf1492014-02-05 17:27:32 +0100563 }
564 else
565 {
piotr437f5462014-02-04 17:57:25 +0100566 to_consume = 0;
567 result = true;
piotr437f5462014-02-04 17:57:25 +0100568 }
569
piotrd0bf1492014-02-05 17:27:32 +0100570 d_counter += to_consume;
571 consume_each(to_consume);
572 return result;
573}
574
575int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
576{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100577 std::vector<gr_complex> correlation_buffer;
578 std::vector<float> power_buffer;
579 std::vector<float> window_energy_buffer;
piotrd0bf1492014-02-05 17:27:32 +0100580
581 int strongest_window_nr;
582 int burst_start = 0;
583 int chan_imp_resp_center = 0;
584 float max_correlation = 0;
585 float energy = 0;
586
587 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++)
piotr437f5462014-02-04 17:57:25 +0100588 {
piotr437f5462014-02-04 17:57:25 +0100589 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
590 correlation_buffer.push_back(correlation);
591 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100592 }
piotrd0bf1492014-02-05 17:27:32 +0100593 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100594 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100595 bool loop_end = false;
596 while (iter != power_buffer.end())
597 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100598 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100599 energy = 0;
600
piotrd0bf1492014-02-05 17:27:32 +0100601 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++)
602 {
603 if (iter_ii == power_buffer.end())
604 {
605 loop_end = true;
606 break;
607 }
608 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100609 }
piotrd0bf1492014-02-05 17:27:32 +0100610 if (loop_end)
611 {
612 break;
piotr437f5462014-02-04 17:57:25 +0100613 }
614 iter++;
615 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100616 }
piotr437f5462014-02-04 17:57:25 +0100617
piotrd0bf1492014-02-05 17:27:32 +0100618 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100619 // d_channel_imp_resp.clear();
620
piotrd0bf1492014-02-05 17:27:32 +0100621 max_correlation = 0;
622 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++)
623 {
piotr437f5462014-02-04 17:57:25 +0100624 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100625 if (abs(correlation) > max_correlation)
626 {
627 chan_imp_resp_center = ii;
628 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100629 }
piotrd0bf1492014-02-05 17:27:32 +0100630 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100631 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100632 }
633
piotrd0bf1492014-02-05 17:27:32 +0100634 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
635 return burst_start;
636}
piotr437f5462014-02-04 17:57:25 +0100637
638
piotrd0bf1492014-02-05 17:27:32 +0100639void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
640{
641 float output[BURST_SIZE];
David Holmf2497bd2014-12-01 21:22:37 +0100642 std::vector<gr_complex> rhh_temp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100643 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
644 gr_complex filtered_burst[BURST_SIZE];
645 int start_state = 3;
646 unsigned int stop_states[2] = {4, 12};
647
David Holmf2497bd2014-12-01 21:22:37 +0100648 autocorrelation(chan_imp_resp, &rhh_temp[0], d_chan_imp_length*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100649 for (int ii = 0; ii < (d_chan_imp_length); ii++)
piotr437f5462014-02-04 17:57:25 +0100650 {
piotr437f5462014-02-04 17:57:25 +0100651 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
piotr437f5462014-02-04 17:57:25 +0100652 }
653
piotrd0bf1492014-02-05 17:27:32 +0100654 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
655
656 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
657
658 for (int i = 0; i < BURST_SIZE ; i++)
piotr437f5462014-02-04 17:57:25 +0100659 {
piotrd0bf1492014-02-05 17:27:32 +0100660 output_binary[i] = (output[i] > 0);
661 }
662}
piotr437f5462014-02-04 17:57:25 +0100663
piotrd0bf1492014-02-05 17:27:32 +0100664void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
665{
666 gr_complex j = gr_complex(0.0, 1.0);
piotr437f5462014-02-04 17:57:25 +0100667
piotrd0bf1492014-02-05 17:27:32 +0100668 int current_symbol;
669 int encoded_symbol;
670 int previous_symbol = 2 * input[0] - 1;
671 gmsk_output[0] = start_point;
672
673 for (int i = 1; i < nitems; i++)
674 {
piotr437f5462014-02-04 17:57:25 +0100675 //change bits representation to NRZ
676 current_symbol = 2 * input[i] - 1;
677 //differentially encode
678 encoded_symbol = current_symbol * previous_symbol;
679 //and do gmsk mapping
680 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
681 previous_symbol = current_symbol;
piotr437f5462014-02-04 17:57:25 +0100682 }
piotrd0bf1492014-02-05 17:27:32 +0100683}
piotr437f5462014-02-04 17:57:25 +0100684
piotrd0bf1492014-02-05 17:27:32 +0100685gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
686{
687 gr_complex result(0.0, 0.0);
688 int sample_number = 0;
689
690 for (int ii = 0; ii < length; ii++)
piotr437f5462014-02-04 17:57:25 +0100691 {
piotr437f5462014-02-04 17:57:25 +0100692 sample_number = (ii * d_OSR) ;
693 result += sequence[ii] * conj(input[sample_number]);
piotr437f5462014-02-04 17:57:25 +0100694 }
695
piotrd0bf1492014-02-05 17:27:32 +0100696 result = result / gr_complex(length, 0);
697 return result;
698}
699
700//computes autocorrelation for positive arguments
piotrd0bf1492014-02-05 17:27:32 +0100701inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
702{
703 int i, k;
704 for (k = nitems - 1; k >= 0; k--)
piotr437f5462014-02-04 17:57:25 +0100705 {
piotr437f5462014-02-04 17:57:25 +0100706 out[k] = gr_complex(0, 0);
piotrd0bf1492014-02-05 17:27:32 +0100707 for (i = k; i < nitems; i++)
708 {
709 out[k] += input[i] * conj(input[i-k]);
piotr437f5462014-02-04 17:57:25 +0100710 }
piotr437f5462014-02-04 17:57:25 +0100711 }
piotrd0bf1492014-02-05 17:27:32 +0100712}
piotr437f5462014-02-04 17:57:25 +0100713
piotrd0bf1492014-02-05 17:27:32 +0100714inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
715{
716 int ii = 0, n, a;
717
718 for (n = 0; n < nitems; n++)
piotr437f5462014-02-04 17:57:25 +0100719 {
piotr437f5462014-02-04 17:57:25 +0100720 a = n * d_OSR;
721 output[n] = 0;
722 ii = 0;
723
piotrd0bf1492014-02-05 17:27:32 +0100724 while (ii < filter_length)
725 {
piotrda8a0662014-04-24 10:29:38 +0200726 if ((a + ii) >= nitems*d_OSR){
piotrd0bf1492014-02-05 17:27:32 +0100727 break;
piotrda8a0662014-04-24 10:29:38 +0200728 }
piotrd0bf1492014-02-05 17:27:32 +0100729 output[n] += input[a+ii] * filter[ii];
730 ii++;
piotr437f5462014-02-04 17:57:25 +0100731 }
piotr437f5462014-02-04 17:57:25 +0100732 }
piotrd0bf1492014-02-05 17:27:32 +0100733}
piotr437f5462014-02-04 17:57:25 +0100734
piotrd0bf1492014-02-05 17:27:32 +0100735//especially computations of strongest_window_nr
piotr7e3b0db2014-02-05 22:44:30 +0100736int receiver_impl::get_norm_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp, float *corr_max, int bcc)
piotrd0bf1492014-02-05 17:27:32 +0100737{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100738 std::vector<gr_complex> correlation_buffer;
739 std::vector<float> power_buffer;
740 std::vector<float> window_energy_buffer;
piotr437f5462014-02-04 17:57:25 +0100741
piotrd0bf1492014-02-05 17:27:32 +0100742 int strongest_window_nr;
743 int burst_start = 0;
744 int chan_imp_resp_center = 0;
745 float max_correlation = 0;
746 float energy = 0;
piotr5c820252014-04-17 09:43:02 +0200747
piotrd0bf1492014-02-05 17:27:32 +0100748 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
piotr7c82b172014-02-08 14:15:27 +0100749 int search_start_pos = search_center + 1 - 5*d_OSR;
piotr437f5462014-02-04 17:57:25 +0100750 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
piotr5c820252014-04-17 09:43:02 +0200751 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 5 * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100752
ptrkrysik58213792014-10-30 09:05:15 +0100753 for(int ii = search_start_pos; ii < search_stop_pos; ii++)
piotrd0bf1492014-02-05 17:27:32 +0100754 {
piotr437f5462014-02-04 17:57:25 +0100755 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100756 correlation_buffer.push_back(correlation);
757 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100758 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100759// plot(power_buffer);
piotrd0bf1492014-02-05 17:27:32 +0100760 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100761 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100762 bool loop_end = false;
763 while (iter != power_buffer.end())
764 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100765 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100766 energy = 0;
767
piotrd0bf1492014-02-05 17:27:32 +0100768 for (int ii = 0; ii < (d_chan_imp_length - 2)*d_OSR; ii++, iter_ii++)
769 {
piotrd0bf1492014-02-05 17:27:32 +0100770 if (iter_ii == power_buffer.end())
771 {
772 loop_end = true;
773 break;
774 }
775 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100776 }
piotrd0bf1492014-02-05 17:27:32 +0100777 if (loop_end)
778 {
779 break;
piotr437f5462014-02-04 17:57:25 +0100780 }
781 iter++;
782
783 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100784 }
piotr437f5462014-02-04 17:57:25 +0100785
piotr5c820252014-04-17 09:43:02 +0200786 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()-((d_chan_imp_length)*d_OSR)) - window_energy_buffer.begin();
787 //strongest_window_nr = strongest_window_nr-d_OSR;
788 if(strongest_window_nr<0){
789 strongest_window_nr = 0;
790 }
piotr6d152d92014-02-21 00:02:44 +0100791
piotrd0bf1492014-02-05 17:27:32 +0100792 max_correlation = 0;
793 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++)
794 {
piotr437f5462014-02-04 17:57:25 +0100795 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100796 if (abs(correlation) > max_correlation)
797 {
798 chan_imp_resp_center = ii;
799 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100800 }
piotrd0bf1492014-02-05 17:27:32 +0100801 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100802 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100803 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100804
piotr7e3b0db2014-02-05 22:44:30 +0100805 *corr_max = max_correlation;
piotrd0bf1492014-02-05 17:27:32 +0100806
ptrkrysik58213792014-10-30 09:05:15 +0100807 //DCOUT("strongest_window_nr_new: " << strongest_window_nr);
piotrc7c249a2014-05-02 17:24:08 +0200808 burst_start = search_start_pos + strongest_window_nr - TRAIN_POS * d_OSR; //compute first sample posiiton which corresponds to the first sample of the impulse response
piotr7c82b172014-02-08 14:15:27 +0100809
ptrkrysik58213792014-10-30 09:05:15 +0100810 //DCOUT("burst_start: " << burst_start);
piotrd0bf1492014-02-05 17:27:32 +0100811 return burst_start;
812}
piotr437f5462014-02-04 17:57:25 +0100813
814
ptrkrysik617ba032014-11-21 10:11:05 +0100815void receiver_impl::send_burst(burst_counter burst_nr, const unsigned char * burst_binary, uint8_t burst_type, unsigned int input_nr)
piotrd0bf1492014-02-05 17:27:32 +0100816{
piotr6d152d92014-02-21 00:02:44 +0100817 boost::scoped_ptr<gsmtap_hdr> tap_header(new gsmtap_hdr());
ptrkrysik617ba032014-11-21 10:11:05 +0100818
piotr6d152d92014-02-21 00:02:44 +0100819 tap_header->version = GSMTAP_VERSION;
ptrkrysik7f61c642014-10-30 08:57:27 +0100820 tap_header->hdr_len = sizeof(gsmtap_hdr)/4;
piotr6d152d92014-02-21 00:02:44 +0100821 tap_header->type = GSMTAP_TYPE_UM_BURST;
822 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100823 tap_header->frame_number = htobe32(d_burst_nr.get_frame_nr());
ptrkrysik617ba032014-11-21 10:11:05 +0100824 tap_header->sub_type = burst_type;
ptrkrysik617ba032014-11-21 10:11:05 +0100825 tap_header->arfcn = htobe16(d_cell_allocation[input_nr]) ;
piotr6d152d92014-02-21 00:02:44 +0100826 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100827 tap_header->snr_db = 0;
ptrkrysik617ba032014-11-21 10:11:05 +0100828
829 int8_t header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE];
830 memcpy(header_plus_burst, tap_header.get(), sizeof(gsmtap_hdr));
831 memcpy(header_plus_burst+sizeof(gsmtap_hdr), burst_binary, BURST_SIZE);
832
833 pmt::pmt_t blob_header_plus_burst = pmt::make_blob(header_plus_burst,sizeof(gsmtap_hdr)+BURST_SIZE);
834 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob_header_plus_burst);
piotrf2b6a1b2014-08-04 11:28:59 +0200835
ptrkrysike518bbf2014-11-06 14:50:59 +0100836 if(input_nr==0){
837 message_port_pub(pmt::mp("C0"), msg);
838 } else {
839 message_port_pub(pmt::mp("CX"), msg);
840 }
piotrd0bf1492014-02-05 17:27:32 +0100841}
piotr6d152d92014-02-21 00:02:44 +0100842
piotrd0bf1492014-02-05 17:27:32 +0100843void receiver_impl::configure_receiver()
844{
piotrce92f982014-04-17 23:37:18 +0200845 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
piotrd0bf1492014-02-05 17:27:32 +0100846 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +0100847
piotrce92f982014-04-17 23:37:18 +0200848 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
849 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
850 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +0100851
piotrd0bf1492014-02-05 17:27:32 +0100852 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
853 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
854 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
855 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
856 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
857 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
858 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
859 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
860 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
861 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
862 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
863 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
864 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
865 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrd0bf1492014-02-05 17:27:32 +0100866}
piotr437f5462014-02-04 17:57:25 +0100867
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100868void receiver_impl::set_cell_allocation(const std::vector<int> &cell_allocation)
piotrf2b6a1b2014-08-04 11:28:59 +0200869{
ptrkrysike518bbf2014-11-06 14:50:59 +0100870 d_cell_allocation = cell_allocation;
871}
872
873void receiver_impl::set_tseq_nums(const std::vector<int> & tseq_nums)
874{
875 d_tseq_nums = tseq_nums;
piotrf2b6a1b2014-08-04 11:28:59 +0200876}
877
878void receiver_impl::reset()
879{
piotrd6d66872014-08-06 15:20:33 +0200880 d_state = fcch_search;
piotrf2b6a1b2014-08-04 11:28:59 +0200881}
piotr437f5462014-02-04 17:57:25 +0100882
piotrd0bf1492014-02-05 17:27:32 +0100883} /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +0100884} /* namespace gr */
885