blob: 186e43708f0a31c93df1d9c09292b475d20795cf [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
ptrkrysik380dea82015-08-06 10:11:58 +020055receiver::make(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums, bool process_uplink)
piotrd0bf1492014-02-05 17:27:32 +010056{
57 return gnuradio::get_initial_sptr
ptrkrysik380dea82015-08-06 10:11:58 +020058 (new receiver_impl(osr, cell_allocation, tseq_nums, process_uplink));
piotrd0bf1492014-02-05 17:27:32 +010059}
60
61/*
62 * The private constructor
63 */
ptrkrysik380dea82015-08-06 10:11:58 +020064receiver_impl::receiver_impl(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums, bool process_uplink)
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),
ptrkrysik380dea82015-08-06 10:11:58 +020069 d_process_uplink(process_uplink),
piotrd0bf1492014-02-05 17:27:32 +010070 d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
piotrd0bf1492014-02-05 17:27:32 +010071 d_counter(0),
72 d_fcch_start_pos(0),
piotr4089c1a2014-08-06 14:10:56 +020073 d_freq_offset_setting(0),
piotrd6d66872014-08-06 15:20:33 +020074 d_state(fcch_search),
piotrd0bf1492014-02-05 17:27:32 +010075 d_burst_nr(osr),
piotr6d152d92014-02-21 00:02:44 +010076 d_failed_sch(0),
ptrkrysike518bbf2014-11-06 14:50:59 +010077 d_signal_dbm(-120),
78 d_tseq_nums(tseq_nums),
ptrkrysik32c21162015-04-04 14:01:52 +020079 d_cell_allocation(cell_allocation),
80 d_last_time(0.0)
piotrd0bf1492014-02-05 17:27:32 +010081{
82 int i;
piotr4089c1a2014-08-06 14:10:56 +020083 //don't send samples to the receiver until there are at least samples for one
piotr7f3f3662014-07-08 16:47:53 +020084 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 +010085 gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0));
86 for (i = 0; i < TRAIN_SEQ_NUM; i++)
piotr437f5462014-02-04 17:57:25 +010087 {
piotrf502e0f2014-04-24 10:28:29 +020088 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 +020089 //if first bit of the seqeunce ==1 first symbol ==-1
piotr437f5462014-02-04 17:57:25 +010090 gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint);
piotr437f5462014-02-04 17:57:25 +010091 }
ptrkrysik380dea82015-08-06 10:11:58 +020092
ptrkrysike518bbf2014-11-06 14:50:59 +010093 message_port_register_out(pmt::mp("C0"));
94 message_port_register_out(pmt::mp("CX"));
piotr4089c1a2014-08-06 14:10:56 +020095 message_port_register_out(pmt::mp("measurements"));
piotr903b1d62014-04-17 11:33:27 +020096 configure_receiver(); //configure the receiver - tell it where to find which burst type
piotrd0bf1492014-02-05 17:27:32 +010097}
piotr437f5462014-02-04 17:57:25 +010098
piotrd0bf1492014-02-05 17:27:32 +010099/*
100 * Our virtual destructor.
101 */
102receiver_impl::~receiver_impl()
103{
104}
105
piotrd0bf1492014-02-05 17:27:32 +0100106int
piotrc7c249a2014-05-02 17:24:08 +0200107receiver_impl::work(int noutput_items,
108 gr_vector_const_void_star &input_items,
109 gr_vector_void_star &output_items)
piotrd0bf1492014-02-05 17:27:32 +0100110{
ptrkrysik58213792014-10-30 09:05:15 +0100111// std::vector<const gr_complex *> iii = (std::vector<const gr_complex *>) input_items; // jak zrobić to rzutowanie poprawnie
112 gr_complex * input = (gr_complex *) input_items[0];
piotr4089c1a2014-08-06 14:10:56 +0200113 std::vector<tag_t> freq_offset_tags;
114 uint64_t start = nitems_read(0);
115 uint64_t stop = start + noutput_items;
piotr7c82b172014-02-08 14:15:27 +0100116
ptrkrysik32c21162015-04-04 14:01:52 +0200117 float current_time = static_cast<float>(start)/(GSM_SYMBOL_RATE*d_OSR);
118 if((current_time - d_last_time) > 0.1)
119 {
120 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("current_time"),pmt::from_double(current_time));
121 message_port_pub(pmt::mp("measurements"), msg);
122 d_last_time = current_time;
123 }
124
piotr4089c1a2014-08-06 14:10:56 +0200125 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
126 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
127 bool freq_offset_tag_in_fcch = false;
128 uint64_t tag_offset=-1; //-1 - just some clearly invalid value
129
130 if(!freq_offset_tags.empty()){
131 tag_t freq_offset_tag = freq_offset_tags[0];
132 tag_offset = freq_offset_tag.offset - start;
133
134 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
135 if(d_state == synchronized && b_type == fcch_burst){
136 uint64_t last_sample_nr = ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1;
137 if(tag_offset < last_sample_nr){
piotr4089c1a2014-08-06 14:10:56 +0200138 freq_offset_tag_in_fcch = true;
139 }
140 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
141 } else {
142 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
143 }
144 }
145
piotrd0bf1492014-02-05 17:27:32 +0100146 switch (d_state)
piotr437f5462014-02-04 17:57:25 +0100147 {
piotrd0bf1492014-02-05 17:27:32 +0100148 //bootstrapping
ptrkrysik58213792014-10-30 09:05:15 +0100149 case fcch_search:
piotrd0bf1492014-02-05 17:27:32 +0100150 {
piotr4089c1a2014-08-06 14:10:56 +0200151 double freq_offset_tmp;
152 if (find_fcch_burst(input, noutput_items,freq_offset_tmp))
piotrd0bf1492014-02-05 17:27:32 +0100153 {
piotrd6d66872014-08-06 15:20:33 +0200154 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 +0200155 message_port_pub(pmt::mp("measurements"), msg);
156
piotrd0bf1492014-02-05 17:27:32 +0100157 d_state = sch_search;
158 }
159 else
160 {
piotrd6d66872014-08-06 15:20:33 +0200161 d_state = fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100162 }
163 break;
164 }
piotr437f5462014-02-04 17:57:25 +0100165
piotrd0bf1492014-02-05 17:27:32 +0100166 case sch_search:
167 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100168 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100169 int t1, t2, t3;
170 int burst_start = 0;
171 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100172
piotrc7c249a2014-05-02 17:24:08 +0200173 if (reach_sch_burst(noutput_items)) //wait for a SCH burst
piotrd0bf1492014-02-05 17:27:32 +0100174 {
175 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it
176 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection
177 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst
178 {
piotr437f5462014-02-04 17:57:25 +0100179 d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value
piotr437f5462014-02-04 17:57:25 +0100180 d_burst_nr++;
181
piotr7f3f3662014-07-08 16:47:53 +0200182 consume_each(burst_start + BURST_SIZE * d_OSR + 4*d_OSR); //consume samples up to next guard period
piotr437f5462014-02-04 17:57:25 +0100183 d_state = synchronized;
piotrd0bf1492014-02-05 17:27:32 +0100184 }
185 else
186 {
piotrd6d66872014-08-06 15:20:33 +0200187 d_state = fcch_search; //if there is error in the sch burst go back to fcch search phase
piotr437f5462014-02-04 17:57:25 +0100188 }
piotrd0bf1492014-02-05 17:27:32 +0100189 }
190 else
191 {
192 d_state = sch_search;
193 }
194 break;
195 }
196 //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
197 case synchronized:
198 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100199 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100200 int offset = 0;
201 int to_consume = 0;
202 unsigned char output_binary[BURST_SIZE];
ptrkrysik58213792014-10-30 09:05:15 +0100203 burst_type b_type;
ptrkrysik380dea82015-08-06 10:11:58 +0200204 unsigned int inputs_to_process=d_cell_allocation.size();
piotr6d152d92014-02-21 00:02:44 +0100205
ptrkrysik380dea82015-08-06 10:11:58 +0200206 if(d_process_uplink)
207 {
208 inputs_to_process = 2*inputs_to_process;
209 }
210
211 for(int input_nr=0; input_nr<inputs_to_process; input_nr++)
piotrd0bf1492014-02-05 17:27:32 +0100212 {
ptrkrysik58213792014-10-30 09:05:15 +0100213 double signal_pwr = 0;
214 input = (gr_complex *)input_items[input_nr];
piotr4089c1a2014-08-06 14:10:56 +0200215
ptrkrysik58213792014-10-30 09:05:15 +0100216 for(int ii=GUARD_PERIOD;ii<TS_BITS;ii++)
piotrd0bf1492014-02-05 17:27:32 +0100217 {
ptrkrysik58213792014-10-30 09:05:15 +0100218 signal_pwr += abs(input[ii])*abs(input[ii]);
piotrd0bf1492014-02-05 17:27:32 +0100219 }
ptrkrysik58213792014-10-30 09:05:15 +0100220 signal_pwr = signal_pwr/(TS_BITS);
221 d_signal_dbm = round(10*log10(signal_pwr/50));
222 if(input_nr==0){
223 d_c0_signal_dbm = d_signal_dbm;
224 }
225
226 if(input_nr==0) //for c0 channel burst type is controlled by channel configuration
piotrd0bf1492014-02-05 17:27:32 +0100227 {
ptrkrysik58213792014-10-30 09:05:15 +0100228 b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number
229 }
230 else
231 {
232 b_type = normal_or_noise; //for the rest it can be only normal burst or noise (at least at this moment of development)
233 }
234
235 switch (b_type)
236 {
237 case fcch_burst: //if it's FCCH burst
238 {
239 const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
240 const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
241 double freq_offset_tmp = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it
242
ptrkrysik617ba032014-11-21 10:11:05 +0100243 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100244
245 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("synchronized"));
246 message_port_pub(pmt::mp("measurements"), msg);
247 break;
248 }
249 case sch_burst: //if it's SCH burst
250 {
251 int t1, t2, t3, d_ncc, d_bcc;
252 d_c0_burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response
253
254 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100255 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_SCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100256 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data
piotrd0bf1492014-02-05 17:27:32 +0100257 {
ptrkrysik58213792014-10-30 09:05:15 +0100258 // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct
259 d_failed_sch = 0;
260 offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period
261 to_consume += offset; //adjust with offset number of samples to be consumed
piotr437f5462014-02-04 17:57:25 +0100262 }
ptrkrysik58213792014-10-30 09:05:15 +0100263 else
264 {
265 d_failed_sch++;
266 if (d_failed_sch >= MAX_SCH_ERRORS)
267 {
268 d_state = fcch_search;
269 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(0.0),pmt::mp("sync_loss"));
270 message_port_pub(pmt::mp("measurements"), msg);
ptrkrysikd57745d2014-12-02 19:05:36 +0100271 //DCOUT("Re-Synchronization!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
ptrkrysik58213792014-10-30 09:05:15 +0100272 }
273 }
274 break;
piotr437f5462014-02-04 17:57:25 +0100275 }
ptrkrysik58213792014-10-30 09:05:15 +0100276 case normal_burst:
277 {
278 float normal_corr_max; //if it's normal burst
279 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
280 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100281 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100282 break;
283 }
284 case dummy_or_normal:
285 {
286 unsigned int normal_burst_start, dummy_burst_start;
287 float dummy_corr_max, normal_corr_max;
piotr437f5462014-02-04 17:57:25 +0100288
ptrkrysik58213792014-10-30 09:05:15 +0100289 dummy_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY);
290 normal_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc);
291
292 if (normal_corr_max > dummy_corr_max)
293 {
294 d_c0_burst_start = normal_burst_start;
295 detect_burst(input, &channel_imp_resp[0], normal_burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100296 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100297 }
298 else
299 {
300 d_c0_burst_start = dummy_burst_start;
ptrkrysik617ba032014-11-21 10:11:05 +0100301 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100302 }
303 break;
piotrd0bf1492014-02-05 17:27:32 +0100304 }
ptrkrysik58213792014-10-30 09:05:15 +0100305 case rach_burst:
306 break;
307 case dummy:
ptrkrysik617ba032014-11-21 10:11:05 +0100308 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100309 break;
310 case normal_or_noise:
311 {
312 unsigned int burst_start;
313 float normal_corr_max_tmp;
314 float normal_corr_max=-1e6;
315 int max_tn;
316 std::vector<gr_complex> v(input, input + noutput_items);
317 if(d_signal_dbm>=d_c0_signal_dbm-13)
318 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100319 if(d_tseq_nums.size()==0) //there is no information about training sequence
320 { //however the receiver can detect it
321 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, 0);
322 float ts_max=normal_corr_max; //with use of a very simple algorithm based on finding
323 int ts_max_num=0; //maximum correlation
324 for(int ss=1; ss<=7; ss++)
325 {
326 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, ss);
327 if(ts_max<normal_corr_max)
328 {
329 ts_max = normal_corr_max;
330 ts_max_num = ss;
331 }
332 }
333 d_tseq_nums.push_back(ts_max_num);
ptrkrysik58213792014-10-30 09:05:15 +0100334 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100335 int tseq_num;
336 if(input_nr<=d_tseq_nums.size()){
337 tseq_num = d_tseq_nums[input_nr-1];
338 } else {
339 tseq_num = d_tseq_nums.back();
340 }
341 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, tseq_num);
342// if(abs(d_c0_burst_start-burst_start)<=2){ //unused check/filter based on timing
ptrkrysik380dea82015-08-06 10:11:58 +0200343 if((normal_corr_max/sqrt(signal_pwr))>=0.8){
ptrkrysike518bbf2014-11-06 14:50:59 +0100344 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100345 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysike518bbf2014-11-06 14:50:59 +0100346 }
ptrkrysik58213792014-10-30 09:05:15 +0100347 }
348 break;
349 }
350 case empty: //if it's empty burst
351 break; //do nothing
352 }
353
ptrkrysik91c28352015-06-07 18:36:15 +0200354 if(input_nr==input_items.size()-1)
ptrkrysik58213792014-10-30 09:05:15 +0100355 {
356 d_burst_nr++; //go to next burst
357 to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); //consume samples of the burst up to next guard period
ptrkrysik58213792014-10-30 09:05:15 +0100358 consume_each(to_consume);
359 }
360 //and add offset which is introduced by
361 //0.25 fractional part of a guard period
362 }
piotrd0bf1492014-02-05 17:27:32 +0100363 }
364 break;
piotr437f5462014-02-04 17:57:25 +0100365 }
piotr6d152d92014-02-21 00:02:44 +0100366 return 0;
piotrd0bf1492014-02-05 17:27:32 +0100367}
piotr437f5462014-02-04 17:57:25 +0100368
piotr4089c1a2014-08-06 14:10:56 +0200369bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset)
piotrd0bf1492014-02-05 17:27:32 +0100370{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100371 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 +0100372 //best match for FCCH burst
373 float phase_diff = 0;
374 gr_complex conjprod;
375 int start_pos = -1;
376 int hit_count = 0;
377 int miss_count = 0;
378 float min_phase_diff;
379 float max_phase_diff;
380 double best_sum = 0;
381 float lowest_max_min_diff = 99999;
382
383 int to_consume = 0;
384 int sample_number = 0;
385 bool end = false;
386 bool result = false;
ptrkrysikef5e2db2015-01-03 12:10:14 +0100387 boost::circular_buffer<float>::iterator buffer_iter;
piotr6d152d92014-02-21 00:02:44 +0100388
piotrd0bf1492014-02-05 17:27:32 +0100389 /**@name Possible states of FCCH search algorithm*/
390 //@{
391 enum states
piotr437f5462014-02-04 17:57:25 +0100392 {
piotr437f5462014-02-04 17:57:25 +0100393 init, ///< initialize variables
394 search, ///< search for positive samples
395 found_something, ///< search for FCCH and the best position of it
396 fcch_found, ///< when FCCH was found
397 search_fail ///< when there is no FCCH in the input vector
piotrd0bf1492014-02-05 17:27:32 +0100398 } fcch_search_state;
399 //@}
piotr437f5462014-02-04 17:57:25 +0100400
piotrd0bf1492014-02-05 17:27:32 +0100401 fcch_search_state = init;
piotr437f5462014-02-04 17:57:25 +0100402
piotrd0bf1492014-02-05 17:27:32 +0100403 while (!end)
404 {
405 switch (fcch_search_state)
406 {
piotr437f5462014-02-04 17:57:25 +0100407
piotrd0bf1492014-02-05 17:27:32 +0100408 case init: //initialize variables
piotr437f5462014-02-04 17:57:25 +0100409 hit_count = 0;
410 miss_count = 0;
411 start_pos = -1;
412 lowest_max_min_diff = 99999;
413 phase_diff_buffer.clear();
414 fcch_search_state = search;
415
416 break;
417
piotr7c82b172014-02-08 14:15:27 +0100418 case search: // search for positive samples
piotr437f5462014-02-04 17:57:25 +0100419 sample_number++;
420
piotrd0bf1492014-02-05 17:27:32 +0100421 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because
422 {
piotr7c82b172014-02-08 14:15:27 +0100423 //there's too few samples left to look into,
piotrd0bf1492014-02-05 17:27:32 +0100424 to_consume = sample_number; //don't do anything with those samples which are left
piotr7c82b172014-02-08 14:15:27 +0100425 //and consume only those which were checked
piotrd0bf1492014-02-05 17:27:32 +0100426 fcch_search_state = search_fail;
427 }
428 else
429 {
430 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
piotr437f5462014-02-04 17:57:25 +0100431
piotrd0bf1492014-02-05 17:27:32 +0100432 if (phase_diff > 0) //if a positive phase difference was found
433 {
434 to_consume = sample_number;
435 fcch_search_state = found_something; //switch to state in which searches for FCCH
436 }
437 else
438 {
439 fcch_search_state = search;
440 }
piotr437f5462014-02-04 17:57:25 +0100441 }
442
443 break;
444
piotrd0bf1492014-02-05 17:27:32 +0100445 case found_something: // search for FCCH and the best position of it
446 {
447 if (phase_diff > 0)
448 {
piotr437f5462014-02-04 17:57:25 +0100449 hit_count++; //positive phase differencies increases hits_count
piotrd0bf1492014-02-05 17:27:32 +0100450 }
451 else
452 {
piotr437f5462014-02-04 17:57:25 +0100453 miss_count++; //negative increases miss_count
piotrd0bf1492014-02-05 17:27:32 +0100454 }
piotr437f5462014-02-04 17:57:25 +0100455
piotrd0bf1492014-02-05 17:27:32 +0100456 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
457 {
piotr437f5462014-02-04 17:57:25 +0100458 //if miss_count exceeds limit before hit_count
459 fcch_search_state = init; //go to init
460 continue;
piotrd0bf1492014-02-05 17:27:32 +0100461 }
462 else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
463 {
piotr437f5462014-02-04 17:57:25 +0100464 //if hit_count and miss_count exceeds limit then FCCH was found
465 fcch_search_state = fcch_found;
466 continue;
piotrd0bf1492014-02-05 17:27:32 +0100467 }
468 else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR))
469 {
piotr437f5462014-02-04 17:57:25 +0100470 //find difference between minimal and maximal element in the buffer
471 //for FCCH this value should be low
472 //this part is searching for a region where this value is lowest
473 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
474 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
475
piotrd0bf1492014-02-05 17:27:32 +0100476 if (lowest_max_min_diff > max_phase_diff - min_phase_diff)
477 {
478 lowest_max_min_diff = max_phase_diff - min_phase_diff;
479 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
480 best_sum = 0;
piotr437f5462014-02-04 17:57:25 +0100481
piotrd0bf1492014-02-05 17:27:32 +0100482 for (buffer_iter = phase_diff_buffer.begin();
483 buffer_iter != (phase_diff_buffer.end());
484 buffer_iter++)
485 {
486 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
487 }
piotr437f5462014-02-04 17:57:25 +0100488 }
piotrd0bf1492014-02-05 17:27:32 +0100489 }
piotr437f5462014-02-04 17:57:25 +0100490
piotrd0bf1492014-02-05 17:27:32 +0100491 sample_number++;
piotr437f5462014-02-04 17:57:25 +0100492
piotrd0bf1492014-02-05 17:27:32 +0100493 if (sample_number >= nitems) //if there's no single sample left to check
494 {
piotr437f5462014-02-04 17:57:25 +0100495 fcch_search_state = search_fail;//FCCH search failed
496 continue;
piotr437f5462014-02-04 17:57:25 +0100497 }
piotrd0bf1492014-02-05 17:27:32 +0100498
499 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
500 phase_diff_buffer.push_back(phase_diff);
501 fcch_search_state = found_something;
502 }
503 break;
504
505 case fcch_found:
506 {
piotrd0bf1492014-02-05 17:27:32 +0100507 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
508
509 d_fcch_start_pos = d_counter + start_pos;
510
511 //compute frequency offset
512 double phase_offset = best_sum / FCCH_HITS_NEEDED;
piotr4089c1a2014-08-06 14:10:56 +0200513 double freq_offset = phase_offset * 1625000.0/6 / (2 * M_PI); //1625000.0/6 - GMSK symbol rate in GSM
514 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100515
516 end = true;
517 result = true;
piotr437f5462014-02-04 17:57:25 +0100518 break;
piotrd0bf1492014-02-05 17:27:32 +0100519 }
piotr437f5462014-02-04 17:57:25 +0100520
piotrd0bf1492014-02-05 17:27:32 +0100521 case search_fail:
piotr437f5462014-02-04 17:57:25 +0100522 end = true;
523 result = false;
524 break;
525 }
piotr437f5462014-02-04 17:57:25 +0100526 }
527
piotrd0bf1492014-02-05 17:27:32 +0100528 d_counter += to_consume;
529 consume_each(to_consume);
piotr437f5462014-02-04 17:57:25 +0100530
piotrd0bf1492014-02-05 17:27:32 +0100531 return result;
532}
533
piotrd0bf1492014-02-05 17:27:32 +0100534double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
535{
536 double phase_sum = 0;
537 unsigned ii;
538
539 for (ii = first_sample; ii < last_sample; ii++)
piotr437f5462014-02-04 17:57:25 +0100540 {
piotr437f5462014-02-04 17:57:25 +0100541 double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
542 phase_sum += phase_diff;
piotr437f5462014-02-04 17:57:25 +0100543 }
544
piotrd0bf1492014-02-05 17:27:32 +0100545 double phase_offset = phase_sum / (last_sample - first_sample);
546 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
547 return freq_offset;
548}
piotr437f5462014-02-04 17:57:25 +0100549
piotrd0bf1492014-02-05 17:27:32 +0100550inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
551{
552 gr_complex conjprod = val1 * conj(val2);
553 return fast_atan2f(imag(conjprod), real(conjprod));
554}
piotr437f5462014-02-04 17:57:25 +0100555
piotrd0bf1492014-02-05 17:27:32 +0100556bool receiver_impl::reach_sch_burst(const int nitems)
557{
558 //it just consumes samples to get near to a SCH burst
559 int to_consume = 0;
560 bool result = false;
561 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
562
563 //consume samples until d_counter will be equal to sample_nr_near_sch_start
564 if (d_counter < sample_nr_near_sch_start)
565 {
566 if (d_counter + nitems >= sample_nr_near_sch_start)
567 {
568 to_consume = sample_nr_near_sch_start - d_counter;
569 }
570 else
571 {
572 to_consume = nitems;
piotr437f5462014-02-04 17:57:25 +0100573 }
574 result = false;
piotrd0bf1492014-02-05 17:27:32 +0100575 }
576 else
577 {
piotr437f5462014-02-04 17:57:25 +0100578 to_consume = 0;
579 result = true;
piotr437f5462014-02-04 17:57:25 +0100580 }
581
piotrd0bf1492014-02-05 17:27:32 +0100582 d_counter += to_consume;
583 consume_each(to_consume);
584 return result;
585}
586
587int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
588{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100589 std::vector<gr_complex> correlation_buffer;
590 std::vector<float> power_buffer;
591 std::vector<float> window_energy_buffer;
piotrd0bf1492014-02-05 17:27:32 +0100592
593 int strongest_window_nr;
594 int burst_start = 0;
595 int chan_imp_resp_center = 0;
596 float max_correlation = 0;
597 float energy = 0;
598
599 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++)
piotr437f5462014-02-04 17:57:25 +0100600 {
piotr437f5462014-02-04 17:57:25 +0100601 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
602 correlation_buffer.push_back(correlation);
603 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100604 }
piotrd0bf1492014-02-05 17:27:32 +0100605 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100606 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100607 bool loop_end = false;
608 while (iter != power_buffer.end())
609 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100610 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100611 energy = 0;
612
piotrd0bf1492014-02-05 17:27:32 +0100613 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++)
614 {
615 if (iter_ii == power_buffer.end())
616 {
617 loop_end = true;
618 break;
619 }
620 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100621 }
piotrd0bf1492014-02-05 17:27:32 +0100622 if (loop_end)
623 {
624 break;
piotr437f5462014-02-04 17:57:25 +0100625 }
626 iter++;
627 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100628 }
piotr437f5462014-02-04 17:57:25 +0100629
piotrd0bf1492014-02-05 17:27:32 +0100630 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100631 // d_channel_imp_resp.clear();
632
piotrd0bf1492014-02-05 17:27:32 +0100633 max_correlation = 0;
634 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++)
635 {
piotr437f5462014-02-04 17:57:25 +0100636 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100637 if (abs(correlation) > max_correlation)
638 {
639 chan_imp_resp_center = ii;
640 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100641 }
piotrd0bf1492014-02-05 17:27:32 +0100642 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100643 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100644 }
645
piotrd0bf1492014-02-05 17:27:32 +0100646 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
647 return burst_start;
648}
piotr437f5462014-02-04 17:57:25 +0100649
650
piotrd0bf1492014-02-05 17:27:32 +0100651void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
652{
653 float output[BURST_SIZE];
David Holmf2497bd2014-12-01 21:22:37 +0100654 std::vector<gr_complex> rhh_temp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100655 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
656 gr_complex filtered_burst[BURST_SIZE];
657 int start_state = 3;
658 unsigned int stop_states[2] = {4, 12};
659
David Holmf2497bd2014-12-01 21:22:37 +0100660 autocorrelation(chan_imp_resp, &rhh_temp[0], d_chan_imp_length*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100661 for (int ii = 0; ii < (d_chan_imp_length); ii++)
piotr437f5462014-02-04 17:57:25 +0100662 {
piotr437f5462014-02-04 17:57:25 +0100663 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
piotr437f5462014-02-04 17:57:25 +0100664 }
665
piotrd0bf1492014-02-05 17:27:32 +0100666 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
667
668 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
669
670 for (int i = 0; i < BURST_SIZE ; i++)
piotr437f5462014-02-04 17:57:25 +0100671 {
piotrd0bf1492014-02-05 17:27:32 +0100672 output_binary[i] = (output[i] > 0);
673 }
674}
piotr437f5462014-02-04 17:57:25 +0100675
piotrd0bf1492014-02-05 17:27:32 +0100676void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
677{
678 gr_complex j = gr_complex(0.0, 1.0);
piotr437f5462014-02-04 17:57:25 +0100679
piotrd0bf1492014-02-05 17:27:32 +0100680 int current_symbol;
681 int encoded_symbol;
682 int previous_symbol = 2 * input[0] - 1;
683 gmsk_output[0] = start_point;
684
685 for (int i = 1; i < nitems; i++)
686 {
piotr437f5462014-02-04 17:57:25 +0100687 //change bits representation to NRZ
688 current_symbol = 2 * input[i] - 1;
689 //differentially encode
690 encoded_symbol = current_symbol * previous_symbol;
691 //and do gmsk mapping
692 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
693 previous_symbol = current_symbol;
piotr437f5462014-02-04 17:57:25 +0100694 }
piotrd0bf1492014-02-05 17:27:32 +0100695}
piotr437f5462014-02-04 17:57:25 +0100696
piotrd0bf1492014-02-05 17:27:32 +0100697gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
698{
699 gr_complex result(0.0, 0.0);
700 int sample_number = 0;
701
702 for (int ii = 0; ii < length; ii++)
piotr437f5462014-02-04 17:57:25 +0100703 {
piotr437f5462014-02-04 17:57:25 +0100704 sample_number = (ii * d_OSR) ;
705 result += sequence[ii] * conj(input[sample_number]);
piotr437f5462014-02-04 17:57:25 +0100706 }
707
piotrd0bf1492014-02-05 17:27:32 +0100708 result = result / gr_complex(length, 0);
709 return result;
710}
711
712//computes autocorrelation for positive arguments
piotrd0bf1492014-02-05 17:27:32 +0100713inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
714{
715 int i, k;
716 for (k = nitems - 1; k >= 0; k--)
piotr437f5462014-02-04 17:57:25 +0100717 {
piotr437f5462014-02-04 17:57:25 +0100718 out[k] = gr_complex(0, 0);
piotrd0bf1492014-02-05 17:27:32 +0100719 for (i = k; i < nitems; i++)
720 {
721 out[k] += input[i] * conj(input[i-k]);
piotr437f5462014-02-04 17:57:25 +0100722 }
piotr437f5462014-02-04 17:57:25 +0100723 }
piotrd0bf1492014-02-05 17:27:32 +0100724}
piotr437f5462014-02-04 17:57:25 +0100725
piotrd0bf1492014-02-05 17:27:32 +0100726inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
727{
728 int ii = 0, n, a;
729
730 for (n = 0; n < nitems; n++)
piotr437f5462014-02-04 17:57:25 +0100731 {
piotr437f5462014-02-04 17:57:25 +0100732 a = n * d_OSR;
733 output[n] = 0;
734 ii = 0;
735
piotrd0bf1492014-02-05 17:27:32 +0100736 while (ii < filter_length)
737 {
piotrda8a0662014-04-24 10:29:38 +0200738 if ((a + ii) >= nitems*d_OSR){
piotrd0bf1492014-02-05 17:27:32 +0100739 break;
piotrda8a0662014-04-24 10:29:38 +0200740 }
piotrd0bf1492014-02-05 17:27:32 +0100741 output[n] += input[a+ii] * filter[ii];
742 ii++;
piotr437f5462014-02-04 17:57:25 +0100743 }
piotr437f5462014-02-04 17:57:25 +0100744 }
piotrd0bf1492014-02-05 17:27:32 +0100745}
piotr437f5462014-02-04 17:57:25 +0100746
piotrd0bf1492014-02-05 17:27:32 +0100747//especially computations of strongest_window_nr
piotr7e3b0db2014-02-05 22:44:30 +0100748int 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 +0100749{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100750 std::vector<gr_complex> correlation_buffer;
751 std::vector<float> power_buffer;
752 std::vector<float> window_energy_buffer;
piotr437f5462014-02-04 17:57:25 +0100753
piotrd0bf1492014-02-05 17:27:32 +0100754 int strongest_window_nr;
755 int burst_start = 0;
756 int chan_imp_resp_center = 0;
757 float max_correlation = 0;
758 float energy = 0;
piotr5c820252014-04-17 09:43:02 +0200759
piotrd0bf1492014-02-05 17:27:32 +0100760 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
piotr7c82b172014-02-08 14:15:27 +0100761 int search_start_pos = search_center + 1 - 5*d_OSR;
piotr437f5462014-02-04 17:57:25 +0100762 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
piotr5c820252014-04-17 09:43:02 +0200763 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 5 * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100764
ptrkrysik58213792014-10-30 09:05:15 +0100765 for(int ii = search_start_pos; ii < search_stop_pos; ii++)
piotrd0bf1492014-02-05 17:27:32 +0100766 {
piotr437f5462014-02-04 17:57:25 +0100767 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100768 correlation_buffer.push_back(correlation);
769 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100770 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100771// plot(power_buffer);
piotrd0bf1492014-02-05 17:27:32 +0100772 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100773 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100774 bool loop_end = false;
775 while (iter != power_buffer.end())
776 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100777 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100778 energy = 0;
779
piotrd0bf1492014-02-05 17:27:32 +0100780 for (int ii = 0; ii < (d_chan_imp_length - 2)*d_OSR; ii++, iter_ii++)
781 {
piotrd0bf1492014-02-05 17:27:32 +0100782 if (iter_ii == power_buffer.end())
783 {
784 loop_end = true;
785 break;
786 }
787 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100788 }
piotrd0bf1492014-02-05 17:27:32 +0100789 if (loop_end)
790 {
791 break;
piotr437f5462014-02-04 17:57:25 +0100792 }
793 iter++;
794
795 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100796 }
piotr437f5462014-02-04 17:57:25 +0100797
piotr5c820252014-04-17 09:43:02 +0200798 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()-((d_chan_imp_length)*d_OSR)) - window_energy_buffer.begin();
799 //strongest_window_nr = strongest_window_nr-d_OSR;
800 if(strongest_window_nr<0){
801 strongest_window_nr = 0;
802 }
piotr6d152d92014-02-21 00:02:44 +0100803
piotrd0bf1492014-02-05 17:27:32 +0100804 max_correlation = 0;
805 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++)
806 {
piotr437f5462014-02-04 17:57:25 +0100807 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100808 if (abs(correlation) > max_correlation)
809 {
810 chan_imp_resp_center = ii;
811 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100812 }
piotrd0bf1492014-02-05 17:27:32 +0100813 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100814 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100815 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100816
piotr7e3b0db2014-02-05 22:44:30 +0100817 *corr_max = max_correlation;
piotrd0bf1492014-02-05 17:27:32 +0100818
ptrkrysik58213792014-10-30 09:05:15 +0100819 //DCOUT("strongest_window_nr_new: " << strongest_window_nr);
piotrc7c249a2014-05-02 17:24:08 +0200820 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 +0100821
ptrkrysik58213792014-10-30 09:05:15 +0100822 //DCOUT("burst_start: " << burst_start);
piotrd0bf1492014-02-05 17:27:32 +0100823 return burst_start;
824}
piotr437f5462014-02-04 17:57:25 +0100825
826
ptrkrysik617ba032014-11-21 10:11:05 +0100827void 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 +0100828{
piotr6d152d92014-02-21 00:02:44 +0100829 boost::scoped_ptr<gsmtap_hdr> tap_header(new gsmtap_hdr());
ptrkrysik617ba032014-11-21 10:11:05 +0100830
piotr6d152d92014-02-21 00:02:44 +0100831 tap_header->version = GSMTAP_VERSION;
ptrkrysik7f61c642014-10-30 08:57:27 +0100832 tap_header->hdr_len = sizeof(gsmtap_hdr)/4;
piotr6d152d92014-02-21 00:02:44 +0100833 tap_header->type = GSMTAP_TYPE_UM_BURST;
ptrkrysik617ba032014-11-21 10:11:05 +0100834 tap_header->sub_type = burst_type;
ptrkrysik380dea82015-08-06 10:11:58 +0200835 bool uplink_burst = (input_nr >= d_cell_allocation.size());
836 if(!uplink_burst) // downlink burst
837 {
838 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
839 tap_header->frame_number = htobe32(d_burst_nr.get_frame_nr());
840 tap_header->arfcn = htobe16(d_cell_allocation[input_nr]) ;
841 }
842 else //uplink burst
843 {
844 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.subtract_timeslots(3).get_timeslot_nr());
845 tap_header->frame_number = htobe32(d_burst_nr.subtract_timeslots(3).get_frame_nr());
846 input_nr = input_nr - d_cell_allocation.size();
847 tap_header->arfcn = htobe16(d_cell_allocation[input_nr] | 0x4000);
848 }
piotr6d152d92014-02-21 00:02:44 +0100849 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100850 tap_header->snr_db = 0;
ptrkrysik617ba032014-11-21 10:11:05 +0100851
852 int8_t header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE];
853 memcpy(header_plus_burst, tap_header.get(), sizeof(gsmtap_hdr));
854 memcpy(header_plus_burst+sizeof(gsmtap_hdr), burst_binary, BURST_SIZE);
855
856 pmt::pmt_t blob_header_plus_burst = pmt::make_blob(header_plus_burst,sizeof(gsmtap_hdr)+BURST_SIZE);
857 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob_header_plus_burst);
piotrf2b6a1b2014-08-04 11:28:59 +0200858
ptrkrysike518bbf2014-11-06 14:50:59 +0100859 if(input_nr==0){
860 message_port_pub(pmt::mp("C0"), msg);
861 } else {
862 message_port_pub(pmt::mp("CX"), msg);
863 }
piotrd0bf1492014-02-05 17:27:32 +0100864}
piotr6d152d92014-02-21 00:02:44 +0100865
piotrd0bf1492014-02-05 17:27:32 +0100866void receiver_impl::configure_receiver()
867{
piotrce92f982014-04-17 23:37:18 +0200868 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
piotrd0bf1492014-02-05 17:27:32 +0100869 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +0100870
piotrce92f982014-04-17 23:37:18 +0200871 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
872 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
873 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +0100874
piotrd0bf1492014-02-05 17:27:32 +0100875 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
876 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
877 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
878 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
879 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
880 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
881 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
882 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
883 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
884 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
885 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
886 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
887 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
888 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrd0bf1492014-02-05 17:27:32 +0100889}
piotr437f5462014-02-04 17:57:25 +0100890
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100891void receiver_impl::set_cell_allocation(const std::vector<int> &cell_allocation)
piotrf2b6a1b2014-08-04 11:28:59 +0200892{
ptrkrysike518bbf2014-11-06 14:50:59 +0100893 d_cell_allocation = cell_allocation;
894}
895
896void receiver_impl::set_tseq_nums(const std::vector<int> & tseq_nums)
897{
898 d_tseq_nums = tseq_nums;
piotrf2b6a1b2014-08-04 11:28:59 +0200899}
900
901void receiver_impl::reset()
902{
piotrd6d66872014-08-06 15:20:33 +0200903 d_state = fcch_search;
piotrf2b6a1b2014-08-04 11:28:59 +0200904}
piotr437f5462014-02-04 17:57:25 +0100905
piotrd0bf1492014-02-05 17:27:32 +0100906} /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +0100907} /* namespace gr */
908