blob: bd2b306a94cd9f57379702d3e2e6c68f5b2a6b3d [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),
ptrkrysik32c21162015-04-04 14:01:52 +020078 d_cell_allocation(cell_allocation),
79 d_last_time(0.0)
piotrd0bf1492014-02-05 17:27:32 +010080{
81 int i;
piotr4089c1a2014-08-06 14:10:56 +020082 //don't send samples to the receiver until there are at least samples for one
piotr7f3f3662014-07-08 16:47:53 +020083 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 +010084 gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0));
85 for (i = 0; i < TRAIN_SEQ_NUM; i++)
piotr437f5462014-02-04 17:57:25 +010086 {
piotrf502e0f2014-04-24 10:28:29 +020087 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 +020088 //if first bit of the seqeunce ==1 first symbol ==-1
piotr437f5462014-02-04 17:57:25 +010089 gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint);
piotr437f5462014-02-04 17:57:25 +010090 }
ptrkrysike518bbf2014-11-06 14:50:59 +010091 message_port_register_out(pmt::mp("C0"));
92 message_port_register_out(pmt::mp("CX"));
piotr4089c1a2014-08-06 14:10:56 +020093 message_port_register_out(pmt::mp("measurements"));
piotr903b1d62014-04-17 11:33:27 +020094 configure_receiver(); //configure the receiver - tell it where to find which burst type
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
piotrd0bf1492014-02-05 17:27:32 +0100104int
piotrc7c249a2014-05-02 17:24:08 +0200105receiver_impl::work(int noutput_items,
106 gr_vector_const_void_star &input_items,
107 gr_vector_void_star &output_items)
piotrd0bf1492014-02-05 17:27:32 +0100108{
ptrkrysik58213792014-10-30 09:05:15 +0100109// std::vector<const gr_complex *> iii = (std::vector<const gr_complex *>) input_items; // jak zrobić to rzutowanie poprawnie
110 gr_complex * input = (gr_complex *) input_items[0];
piotr4089c1a2014-08-06 14:10:56 +0200111 std::vector<tag_t> freq_offset_tags;
112 uint64_t start = nitems_read(0);
113 uint64_t stop = start + noutput_items;
piotr7c82b172014-02-08 14:15:27 +0100114
ptrkrysik32c21162015-04-04 14:01:52 +0200115 float current_time = static_cast<float>(start)/(GSM_SYMBOL_RATE*d_OSR);
116 if((current_time - d_last_time) > 0.1)
117 {
118 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("current_time"),pmt::from_double(current_time));
119 message_port_pub(pmt::mp("measurements"), msg);
120 d_last_time = current_time;
121 }
122
piotr4089c1a2014-08-06 14:10:56 +0200123 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
124 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
125 bool freq_offset_tag_in_fcch = false;
126 uint64_t tag_offset=-1; //-1 - just some clearly invalid value
127
128 if(!freq_offset_tags.empty()){
129 tag_t freq_offset_tag = freq_offset_tags[0];
130 tag_offset = freq_offset_tag.offset - start;
131
132 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
133 if(d_state == synchronized && b_type == fcch_burst){
134 uint64_t last_sample_nr = ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1;
135 if(tag_offset < last_sample_nr){
piotr4089c1a2014-08-06 14:10:56 +0200136 freq_offset_tag_in_fcch = true;
137 }
138 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
139 } else {
140 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
141 }
142 }
143
piotrd0bf1492014-02-05 17:27:32 +0100144 switch (d_state)
piotr437f5462014-02-04 17:57:25 +0100145 {
piotrd0bf1492014-02-05 17:27:32 +0100146 //bootstrapping
ptrkrysik58213792014-10-30 09:05:15 +0100147 case fcch_search:
piotrd0bf1492014-02-05 17:27:32 +0100148 {
piotr4089c1a2014-08-06 14:10:56 +0200149 double freq_offset_tmp;
150 if (find_fcch_burst(input, noutput_items,freq_offset_tmp))
piotrd0bf1492014-02-05 17:27:32 +0100151 {
piotrd6d66872014-08-06 15:20:33 +0200152 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 +0200153 message_port_pub(pmt::mp("measurements"), msg);
154
piotrd0bf1492014-02-05 17:27:32 +0100155 d_state = sch_search;
156 }
157 else
158 {
piotrd6d66872014-08-06 15:20:33 +0200159 d_state = fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100160 }
161 break;
162 }
piotr437f5462014-02-04 17:57:25 +0100163
piotrd0bf1492014-02-05 17:27:32 +0100164 case sch_search:
165 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100166 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100167 int t1, t2, t3;
168 int burst_start = 0;
169 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100170
piotrc7c249a2014-05-02 17:24:08 +0200171 if (reach_sch_burst(noutput_items)) //wait for a SCH burst
piotrd0bf1492014-02-05 17:27:32 +0100172 {
173 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it
174 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100175 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst
piotrd0bf1492014-02-05 17:27:32 +0100176 {
piotr437f5462014-02-04 17:57:25 +0100177 d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value
piotr437f5462014-02-04 17:57:25 +0100178 d_burst_nr++;
179
piotr7f3f3662014-07-08 16:47:53 +0200180 consume_each(burst_start + BURST_SIZE * d_OSR + 4*d_OSR); //consume samples up to next guard period
piotr437f5462014-02-04 17:57:25 +0100181 d_state = synchronized;
piotrd0bf1492014-02-05 17:27:32 +0100182 }
183 else
184 {
piotrd6d66872014-08-06 15:20:33 +0200185 d_state = fcch_search; //if there is error in the sch burst go back to fcch search phase
piotr437f5462014-02-04 17:57:25 +0100186 }
piotrd0bf1492014-02-05 17:27:32 +0100187 }
188 else
189 {
190 d_state = sch_search;
191 }
192 break;
193 }
194 //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
195 case synchronized:
196 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100197 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100198 int offset = 0;
199 int to_consume = 0;
200 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100201
ptrkrysik58213792014-10-30 09:05:15 +0100202 burst_type b_type;
piotr6d152d92014-02-21 00:02:44 +0100203
ptrkrysike518bbf2014-11-06 14:50:59 +0100204 for(int input_nr=0; input_nr<d_cell_allocation.size(); input_nr++)
piotrd0bf1492014-02-05 17:27:32 +0100205 {
ptrkrysik58213792014-10-30 09:05:15 +0100206 double signal_pwr = 0;
207 input = (gr_complex *)input_items[input_nr];
piotr4089c1a2014-08-06 14:10:56 +0200208
ptrkrysik58213792014-10-30 09:05:15 +0100209 for(int ii=GUARD_PERIOD;ii<TS_BITS;ii++)
piotrd0bf1492014-02-05 17:27:32 +0100210 {
ptrkrysik58213792014-10-30 09:05:15 +0100211 signal_pwr += abs(input[ii])*abs(input[ii]);
piotrd0bf1492014-02-05 17:27:32 +0100212 }
ptrkrysik58213792014-10-30 09:05:15 +0100213 signal_pwr = signal_pwr/(TS_BITS);
214 d_signal_dbm = round(10*log10(signal_pwr/50));
215 if(input_nr==0){
216 d_c0_signal_dbm = d_signal_dbm;
217 }
218
219 if(input_nr==0) //for c0 channel burst type is controlled by channel configuration
piotrd0bf1492014-02-05 17:27:32 +0100220 {
ptrkrysik58213792014-10-30 09:05:15 +0100221 b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number
222 }
223 else
224 {
225 b_type = normal_or_noise; //for the rest it can be only normal burst or noise (at least at this moment of development)
226 }
227
228 switch (b_type)
229 {
230 case fcch_burst: //if it's FCCH burst
231 {
232 const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
233 const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
234 double freq_offset_tmp = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it
235
ptrkrysik617ba032014-11-21 10:11:05 +0100236 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100237
238 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("synchronized"));
239 message_port_pub(pmt::mp("measurements"), msg);
240 break;
241 }
242 case sch_burst: //if it's SCH burst
243 {
244 int t1, t2, t3, d_ncc, d_bcc;
245 d_c0_burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response
246
247 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100248 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_SCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100249 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data
piotrd0bf1492014-02-05 17:27:32 +0100250 {
ptrkrysik58213792014-10-30 09:05:15 +0100251 // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct
252 d_failed_sch = 0;
253 offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period
254 to_consume += offset; //adjust with offset number of samples to be consumed
piotr437f5462014-02-04 17:57:25 +0100255 }
ptrkrysik58213792014-10-30 09:05:15 +0100256 else
257 {
258 d_failed_sch++;
259 if (d_failed_sch >= MAX_SCH_ERRORS)
260 {
261 d_state = fcch_search;
262 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(0.0),pmt::mp("sync_loss"));
263 message_port_pub(pmt::mp("measurements"), msg);
ptrkrysikd57745d2014-12-02 19:05:36 +0100264 //DCOUT("Re-Synchronization!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
ptrkrysik58213792014-10-30 09:05:15 +0100265 }
266 }
267 break;
piotr437f5462014-02-04 17:57:25 +0100268 }
ptrkrysik58213792014-10-30 09:05:15 +0100269 case normal_burst:
270 {
271 float normal_corr_max; //if it's normal burst
272 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
273 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100274 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100275 break;
276 }
277 case dummy_or_normal:
278 {
279 unsigned int normal_burst_start, dummy_burst_start;
280 float dummy_corr_max, normal_corr_max;
piotr437f5462014-02-04 17:57:25 +0100281
ptrkrysik58213792014-10-30 09:05:15 +0100282 dummy_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY);
283 normal_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc);
284
285 if (normal_corr_max > dummy_corr_max)
286 {
287 d_c0_burst_start = normal_burst_start;
288 detect_burst(input, &channel_imp_resp[0], normal_burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100289 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100290 }
291 else
292 {
293 d_c0_burst_start = dummy_burst_start;
ptrkrysik617ba032014-11-21 10:11:05 +0100294 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100295 }
296 break;
piotrd0bf1492014-02-05 17:27:32 +0100297 }
ptrkrysik58213792014-10-30 09:05:15 +0100298 case rach_burst:
299 break;
300 case dummy:
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 break;
303 case normal_or_noise:
304 {
305 unsigned int burst_start;
306 float normal_corr_max_tmp;
307 float normal_corr_max=-1e6;
308 int max_tn;
309 std::vector<gr_complex> v(input, input + noutput_items);
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100310 //if(d_signal_dbm>=d_c0_signal_dbm-13)
ptrkrysik58213792014-10-30 09:05:15 +0100311 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100312 if(d_tseq_nums.size()==0) //there is no information about training sequence
313 { //however the receiver can detect it
314 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, 0);
315 float ts_max=normal_corr_max; //with use of a very simple algorithm based on finding
316 int ts_max_num=0; //maximum correlation
317 for(int ss=1; ss<=7; ss++)
318 {
319 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, ss);
320 if(ts_max<normal_corr_max)
321 {
322 ts_max = normal_corr_max;
323 ts_max_num = ss;
324 }
325 }
326 d_tseq_nums.push_back(ts_max_num);
ptrkrysik58213792014-10-30 09:05:15 +0100327 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100328 int tseq_num;
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100329 if(input_nr<=d_tseq_nums.size())
330 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100331 tseq_num = d_tseq_nums[input_nr-1];
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100332 } else
333 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100334 tseq_num = d_tseq_nums.back();
335 }
336 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, tseq_num);
337// if(abs(d_c0_burst_start-burst_start)<=2){ //unused check/filter based on timing
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100338 // if((normal_corr_max/sqrt(signal_pwr))>=0.9)
339 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100340 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100341 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysike518bbf2014-11-06 14:50:59 +0100342 }
ptrkrysik58213792014-10-30 09:05:15 +0100343 }
344 break;
345 }
346 case empty: //if it's empty burst
347 break; //do nothing
348 }
349
ptrkrysik91c28352015-06-07 18:36:15 +0200350 if(input_nr==input_items.size()-1)
ptrkrysik58213792014-10-30 09:05:15 +0100351 {
352 d_burst_nr++; //go to next burst
353 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 +0100354 consume_each(to_consume);
355 }
356 //and add offset which is introduced by
357 //0.25 fractional part of a guard period
358 }
piotrd0bf1492014-02-05 17:27:32 +0100359 }
360 break;
piotr437f5462014-02-04 17:57:25 +0100361 }
piotr6d152d92014-02-21 00:02:44 +0100362 return 0;
piotrd0bf1492014-02-05 17:27:32 +0100363}
piotr437f5462014-02-04 17:57:25 +0100364
piotr4089c1a2014-08-06 14:10:56 +0200365bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset)
piotrd0bf1492014-02-05 17:27:32 +0100366{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100367 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 +0100368 //best match for FCCH burst
369 float phase_diff = 0;
370 gr_complex conjprod;
371 int start_pos = -1;
372 int hit_count = 0;
373 int miss_count = 0;
374 float min_phase_diff;
375 float max_phase_diff;
376 double best_sum = 0;
377 float lowest_max_min_diff = 99999;
378
379 int to_consume = 0;
380 int sample_number = 0;
381 bool end = false;
382 bool result = false;
ptrkrysikef5e2db2015-01-03 12:10:14 +0100383 boost::circular_buffer<float>::iterator buffer_iter;
piotr6d152d92014-02-21 00:02:44 +0100384
piotrd0bf1492014-02-05 17:27:32 +0100385 /**@name Possible states of FCCH search algorithm*/
386 //@{
387 enum states
piotr437f5462014-02-04 17:57:25 +0100388 {
piotr437f5462014-02-04 17:57:25 +0100389 init, ///< initialize variables
390 search, ///< search for positive samples
391 found_something, ///< search for FCCH and the best position of it
392 fcch_found, ///< when FCCH was found
393 search_fail ///< when there is no FCCH in the input vector
piotrd0bf1492014-02-05 17:27:32 +0100394 } fcch_search_state;
395 //@}
piotr437f5462014-02-04 17:57:25 +0100396
piotrd0bf1492014-02-05 17:27:32 +0100397 fcch_search_state = init;
piotr437f5462014-02-04 17:57:25 +0100398
piotrd0bf1492014-02-05 17:27:32 +0100399 while (!end)
400 {
401 switch (fcch_search_state)
402 {
piotr437f5462014-02-04 17:57:25 +0100403
piotrd0bf1492014-02-05 17:27:32 +0100404 case init: //initialize variables
piotr437f5462014-02-04 17:57:25 +0100405 hit_count = 0;
406 miss_count = 0;
407 start_pos = -1;
408 lowest_max_min_diff = 99999;
409 phase_diff_buffer.clear();
410 fcch_search_state = search;
411
412 break;
413
piotr7c82b172014-02-08 14:15:27 +0100414 case search: // search for positive samples
piotr437f5462014-02-04 17:57:25 +0100415 sample_number++;
416
piotrd0bf1492014-02-05 17:27:32 +0100417 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because
418 {
piotr7c82b172014-02-08 14:15:27 +0100419 //there's too few samples left to look into,
piotrd0bf1492014-02-05 17:27:32 +0100420 to_consume = sample_number; //don't do anything with those samples which are left
piotr7c82b172014-02-08 14:15:27 +0100421 //and consume only those which were checked
piotrd0bf1492014-02-05 17:27:32 +0100422 fcch_search_state = search_fail;
423 }
424 else
425 {
426 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
piotr437f5462014-02-04 17:57:25 +0100427
piotrd0bf1492014-02-05 17:27:32 +0100428 if (phase_diff > 0) //if a positive phase difference was found
429 {
430 to_consume = sample_number;
431 fcch_search_state = found_something; //switch to state in which searches for FCCH
432 }
433 else
434 {
435 fcch_search_state = search;
436 }
piotr437f5462014-02-04 17:57:25 +0100437 }
438
439 break;
440
piotrd0bf1492014-02-05 17:27:32 +0100441 case found_something: // search for FCCH and the best position of it
442 {
443 if (phase_diff > 0)
444 {
piotr437f5462014-02-04 17:57:25 +0100445 hit_count++; //positive phase differencies increases hits_count
piotrd0bf1492014-02-05 17:27:32 +0100446 }
447 else
448 {
piotr437f5462014-02-04 17:57:25 +0100449 miss_count++; //negative increases miss_count
piotrd0bf1492014-02-05 17:27:32 +0100450 }
piotr437f5462014-02-04 17:57:25 +0100451
piotrd0bf1492014-02-05 17:27:32 +0100452 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
453 {
piotr437f5462014-02-04 17:57:25 +0100454 //if miss_count exceeds limit before hit_count
455 fcch_search_state = init; //go to init
456 continue;
piotrd0bf1492014-02-05 17:27:32 +0100457 }
458 else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
459 {
piotr437f5462014-02-04 17:57:25 +0100460 //if hit_count and miss_count exceeds limit then FCCH was found
461 fcch_search_state = fcch_found;
462 continue;
piotrd0bf1492014-02-05 17:27:32 +0100463 }
464 else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR))
465 {
piotr437f5462014-02-04 17:57:25 +0100466 //find difference between minimal and maximal element in the buffer
467 //for FCCH this value should be low
468 //this part is searching for a region where this value is lowest
469 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
470 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
471
piotrd0bf1492014-02-05 17:27:32 +0100472 if (lowest_max_min_diff > max_phase_diff - min_phase_diff)
473 {
474 lowest_max_min_diff = max_phase_diff - min_phase_diff;
475 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
476 best_sum = 0;
piotr437f5462014-02-04 17:57:25 +0100477
piotrd0bf1492014-02-05 17:27:32 +0100478 for (buffer_iter = phase_diff_buffer.begin();
479 buffer_iter != (phase_diff_buffer.end());
480 buffer_iter++)
481 {
482 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
483 }
piotr437f5462014-02-04 17:57:25 +0100484 }
piotrd0bf1492014-02-05 17:27:32 +0100485 }
piotr437f5462014-02-04 17:57:25 +0100486
piotrd0bf1492014-02-05 17:27:32 +0100487 sample_number++;
piotr437f5462014-02-04 17:57:25 +0100488
piotrd0bf1492014-02-05 17:27:32 +0100489 if (sample_number >= nitems) //if there's no single sample left to check
490 {
piotr437f5462014-02-04 17:57:25 +0100491 fcch_search_state = search_fail;//FCCH search failed
492 continue;
piotr437f5462014-02-04 17:57:25 +0100493 }
piotrd0bf1492014-02-05 17:27:32 +0100494
495 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
496 phase_diff_buffer.push_back(phase_diff);
497 fcch_search_state = found_something;
498 }
499 break;
500
501 case fcch_found:
502 {
piotrd0bf1492014-02-05 17:27:32 +0100503 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
504
505 d_fcch_start_pos = d_counter + start_pos;
506
507 //compute frequency offset
508 double phase_offset = best_sum / FCCH_HITS_NEEDED;
piotr4089c1a2014-08-06 14:10:56 +0200509 double freq_offset = phase_offset * 1625000.0/6 / (2 * M_PI); //1625000.0/6 - GMSK symbol rate in GSM
510 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100511
512 end = true;
513 result = true;
piotr437f5462014-02-04 17:57:25 +0100514 break;
piotrd0bf1492014-02-05 17:27:32 +0100515 }
piotr437f5462014-02-04 17:57:25 +0100516
piotrd0bf1492014-02-05 17:27:32 +0100517 case search_fail:
piotr437f5462014-02-04 17:57:25 +0100518 end = true;
519 result = false;
520 break;
521 }
piotr437f5462014-02-04 17:57:25 +0100522 }
523
piotrd0bf1492014-02-05 17:27:32 +0100524 d_counter += to_consume;
525 consume_each(to_consume);
piotr437f5462014-02-04 17:57:25 +0100526
piotrd0bf1492014-02-05 17:27:32 +0100527 return result;
528}
529
piotrd0bf1492014-02-05 17:27:32 +0100530double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
531{
532 double phase_sum = 0;
533 unsigned ii;
534
535 for (ii = first_sample; ii < last_sample; ii++)
piotr437f5462014-02-04 17:57:25 +0100536 {
piotr437f5462014-02-04 17:57:25 +0100537 double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
538 phase_sum += phase_diff;
piotr437f5462014-02-04 17:57:25 +0100539 }
540
piotrd0bf1492014-02-05 17:27:32 +0100541 double phase_offset = phase_sum / (last_sample - first_sample);
542 double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
543 return freq_offset;
544}
piotr437f5462014-02-04 17:57:25 +0100545
piotrd0bf1492014-02-05 17:27:32 +0100546inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
547{
548 gr_complex conjprod = val1 * conj(val2);
549 return fast_atan2f(imag(conjprod), real(conjprod));
550}
piotr437f5462014-02-04 17:57:25 +0100551
piotrd0bf1492014-02-05 17:27:32 +0100552bool receiver_impl::reach_sch_burst(const int nitems)
553{
554 //it just consumes samples to get near to a SCH burst
555 int to_consume = 0;
556 bool result = false;
557 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
558
559 //consume samples until d_counter will be equal to sample_nr_near_sch_start
560 if (d_counter < sample_nr_near_sch_start)
561 {
562 if (d_counter + nitems >= sample_nr_near_sch_start)
563 {
564 to_consume = sample_nr_near_sch_start - d_counter;
565 }
566 else
567 {
568 to_consume = nitems;
piotr437f5462014-02-04 17:57:25 +0100569 }
570 result = false;
piotrd0bf1492014-02-05 17:27:32 +0100571 }
572 else
573 {
piotr437f5462014-02-04 17:57:25 +0100574 to_consume = 0;
575 result = true;
piotr437f5462014-02-04 17:57:25 +0100576 }
577
piotrd0bf1492014-02-05 17:27:32 +0100578 d_counter += to_consume;
579 consume_each(to_consume);
580 return result;
581}
582
583int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
584{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100585 std::vector<gr_complex> correlation_buffer;
586 std::vector<float> power_buffer;
587 std::vector<float> window_energy_buffer;
piotrd0bf1492014-02-05 17:27:32 +0100588
589 int strongest_window_nr;
590 int burst_start = 0;
591 int chan_imp_resp_center = 0;
592 float max_correlation = 0;
593 float energy = 0;
594
595 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++)
piotr437f5462014-02-04 17:57:25 +0100596 {
piotr437f5462014-02-04 17:57:25 +0100597 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
598 correlation_buffer.push_back(correlation);
599 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100600 }
piotrd0bf1492014-02-05 17:27:32 +0100601 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100602 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100603 bool loop_end = false;
604 while (iter != power_buffer.end())
605 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100606 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100607 energy = 0;
608
piotrd0bf1492014-02-05 17:27:32 +0100609 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++)
610 {
611 if (iter_ii == power_buffer.end())
612 {
613 loop_end = true;
614 break;
615 }
616 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100617 }
piotrd0bf1492014-02-05 17:27:32 +0100618 if (loop_end)
619 {
620 break;
piotr437f5462014-02-04 17:57:25 +0100621 }
622 iter++;
623 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100624 }
piotr437f5462014-02-04 17:57:25 +0100625
piotrd0bf1492014-02-05 17:27:32 +0100626 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100627 // d_channel_imp_resp.clear();
628
piotrd0bf1492014-02-05 17:27:32 +0100629 max_correlation = 0;
630 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++)
631 {
piotr437f5462014-02-04 17:57:25 +0100632 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100633 if (abs(correlation) > max_correlation)
634 {
635 chan_imp_resp_center = ii;
636 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100637 }
piotrd0bf1492014-02-05 17:27:32 +0100638 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100639 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100640 }
641
piotrd0bf1492014-02-05 17:27:32 +0100642 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
643 return burst_start;
644}
piotr437f5462014-02-04 17:57:25 +0100645
646
piotrd0bf1492014-02-05 17:27:32 +0100647void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
648{
649 float output[BURST_SIZE];
David Holmf2497bd2014-12-01 21:22:37 +0100650 std::vector<gr_complex> rhh_temp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100651 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
652 gr_complex filtered_burst[BURST_SIZE];
653 int start_state = 3;
654 unsigned int stop_states[2] = {4, 12};
655
David Holmf2497bd2014-12-01 21:22:37 +0100656 autocorrelation(chan_imp_resp, &rhh_temp[0], d_chan_imp_length*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100657 for (int ii = 0; ii < (d_chan_imp_length); ii++)
piotr437f5462014-02-04 17:57:25 +0100658 {
piotr437f5462014-02-04 17:57:25 +0100659 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
piotr437f5462014-02-04 17:57:25 +0100660 }
661
piotrd0bf1492014-02-05 17:27:32 +0100662 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
663
664 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
665
666 for (int i = 0; i < BURST_SIZE ; i++)
piotr437f5462014-02-04 17:57:25 +0100667 {
piotrd0bf1492014-02-05 17:27:32 +0100668 output_binary[i] = (output[i] > 0);
669 }
670}
piotr437f5462014-02-04 17:57:25 +0100671
piotrd0bf1492014-02-05 17:27:32 +0100672void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
673{
674 gr_complex j = gr_complex(0.0, 1.0);
piotr437f5462014-02-04 17:57:25 +0100675
piotrd0bf1492014-02-05 17:27:32 +0100676 int current_symbol;
677 int encoded_symbol;
678 int previous_symbol = 2 * input[0] - 1;
679 gmsk_output[0] = start_point;
680
681 for (int i = 1; i < nitems; i++)
682 {
piotr437f5462014-02-04 17:57:25 +0100683 //change bits representation to NRZ
684 current_symbol = 2 * input[i] - 1;
685 //differentially encode
686 encoded_symbol = current_symbol * previous_symbol;
687 //and do gmsk mapping
688 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
689 previous_symbol = current_symbol;
piotr437f5462014-02-04 17:57:25 +0100690 }
piotrd0bf1492014-02-05 17:27:32 +0100691}
piotr437f5462014-02-04 17:57:25 +0100692
piotrd0bf1492014-02-05 17:27:32 +0100693gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
694{
695 gr_complex result(0.0, 0.0);
696 int sample_number = 0;
697
698 for (int ii = 0; ii < length; ii++)
piotr437f5462014-02-04 17:57:25 +0100699 {
piotr437f5462014-02-04 17:57:25 +0100700 sample_number = (ii * d_OSR) ;
701 result += sequence[ii] * conj(input[sample_number]);
piotr437f5462014-02-04 17:57:25 +0100702 }
703
piotrd0bf1492014-02-05 17:27:32 +0100704 result = result / gr_complex(length, 0);
705 return result;
706}
707
708//computes autocorrelation for positive arguments
piotrd0bf1492014-02-05 17:27:32 +0100709inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
710{
711 int i, k;
712 for (k = nitems - 1; k >= 0; k--)
piotr437f5462014-02-04 17:57:25 +0100713 {
piotr437f5462014-02-04 17:57:25 +0100714 out[k] = gr_complex(0, 0);
piotrd0bf1492014-02-05 17:27:32 +0100715 for (i = k; i < nitems; i++)
716 {
717 out[k] += input[i] * conj(input[i-k]);
piotr437f5462014-02-04 17:57:25 +0100718 }
piotr437f5462014-02-04 17:57:25 +0100719 }
piotrd0bf1492014-02-05 17:27:32 +0100720}
piotr437f5462014-02-04 17:57:25 +0100721
piotrd0bf1492014-02-05 17:27:32 +0100722inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
723{
724 int ii = 0, n, a;
725
726 for (n = 0; n < nitems; n++)
piotr437f5462014-02-04 17:57:25 +0100727 {
piotr437f5462014-02-04 17:57:25 +0100728 a = n * d_OSR;
729 output[n] = 0;
730 ii = 0;
731
piotrd0bf1492014-02-05 17:27:32 +0100732 while (ii < filter_length)
733 {
piotrda8a0662014-04-24 10:29:38 +0200734 if ((a + ii) >= nitems*d_OSR){
piotrd0bf1492014-02-05 17:27:32 +0100735 break;
piotrda8a0662014-04-24 10:29:38 +0200736 }
piotrd0bf1492014-02-05 17:27:32 +0100737 output[n] += input[a+ii] * filter[ii];
738 ii++;
piotr437f5462014-02-04 17:57:25 +0100739 }
piotr437f5462014-02-04 17:57:25 +0100740 }
piotrd0bf1492014-02-05 17:27:32 +0100741}
piotr437f5462014-02-04 17:57:25 +0100742
piotrd0bf1492014-02-05 17:27:32 +0100743//especially computations of strongest_window_nr
piotr7e3b0db2014-02-05 22:44:30 +0100744int 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 +0100745{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100746 std::vector<gr_complex> correlation_buffer;
747 std::vector<float> power_buffer;
748 std::vector<float> window_energy_buffer;
piotr437f5462014-02-04 17:57:25 +0100749
piotrd0bf1492014-02-05 17:27:32 +0100750 int strongest_window_nr;
751 int burst_start = 0;
752 int chan_imp_resp_center = 0;
753 float max_correlation = 0;
754 float energy = 0;
piotr5c820252014-04-17 09:43:02 +0200755
piotrd0bf1492014-02-05 17:27:32 +0100756 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
piotr7c82b172014-02-08 14:15:27 +0100757 int search_start_pos = search_center + 1 - 5*d_OSR;
piotr437f5462014-02-04 17:57:25 +0100758 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
piotr5c820252014-04-17 09:43:02 +0200759 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 5 * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100760
ptrkrysik58213792014-10-30 09:05:15 +0100761 for(int ii = search_start_pos; ii < search_stop_pos; ii++)
piotrd0bf1492014-02-05 17:27:32 +0100762 {
piotr437f5462014-02-04 17:57:25 +0100763 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100764 correlation_buffer.push_back(correlation);
765 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100766 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100767// plot(power_buffer);
piotrd0bf1492014-02-05 17:27:32 +0100768 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100769 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100770 bool loop_end = false;
771 while (iter != power_buffer.end())
772 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100773 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100774 energy = 0;
775
Piotr Krysik97d4f8a2016-02-11 08:40:44 +0100776 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++, iter_ii++)
piotrd0bf1492014-02-05 17:27:32 +0100777 {
piotrd0bf1492014-02-05 17:27:32 +0100778 if (iter_ii == power_buffer.end())
779 {
780 loop_end = true;
781 break;
782 }
783 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100784 }
piotrd0bf1492014-02-05 17:27:32 +0100785 if (loop_end)
786 {
787 break;
piotr437f5462014-02-04 17:57:25 +0100788 }
789 iter++;
790
791 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100792 }
piotr437f5462014-02-04 17:57:25 +0100793
piotr5c820252014-04-17 09:43:02 +0200794 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()-((d_chan_imp_length)*d_OSR)) - window_energy_buffer.begin();
795 //strongest_window_nr = strongest_window_nr-d_OSR;
796 if(strongest_window_nr<0){
797 strongest_window_nr = 0;
798 }
piotr6d152d92014-02-21 00:02:44 +0100799
piotrd0bf1492014-02-05 17:27:32 +0100800 max_correlation = 0;
801 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++)
802 {
piotr437f5462014-02-04 17:57:25 +0100803 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100804 if (abs(correlation) > max_correlation)
805 {
806 chan_imp_resp_center = ii;
807 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100808 }
piotrd0bf1492014-02-05 17:27:32 +0100809 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100810 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100811 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100812
piotr7e3b0db2014-02-05 22:44:30 +0100813 *corr_max = max_correlation;
piotrd0bf1492014-02-05 17:27:32 +0100814
ptrkrysik58213792014-10-30 09:05:15 +0100815 //DCOUT("strongest_window_nr_new: " << strongest_window_nr);
piotrc7c249a2014-05-02 17:24:08 +0200816 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 +0100817
ptrkrysik58213792014-10-30 09:05:15 +0100818 //DCOUT("burst_start: " << burst_start);
piotrd0bf1492014-02-05 17:27:32 +0100819 return burst_start;
820}
piotr437f5462014-02-04 17:57:25 +0100821
822
ptrkrysik617ba032014-11-21 10:11:05 +0100823void 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 +0100824{
piotr6d152d92014-02-21 00:02:44 +0100825 boost::scoped_ptr<gsmtap_hdr> tap_header(new gsmtap_hdr());
ptrkrysik617ba032014-11-21 10:11:05 +0100826
piotr6d152d92014-02-21 00:02:44 +0100827 tap_header->version = GSMTAP_VERSION;
ptrkrysik7f61c642014-10-30 08:57:27 +0100828 tap_header->hdr_len = sizeof(gsmtap_hdr)/4;
piotr6d152d92014-02-21 00:02:44 +0100829 tap_header->type = GSMTAP_TYPE_UM_BURST;
830 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100831 tap_header->frame_number = htobe32(d_burst_nr.get_frame_nr());
ptrkrysik617ba032014-11-21 10:11:05 +0100832 tap_header->sub_type = burst_type;
ptrkrysik617ba032014-11-21 10:11:05 +0100833 tap_header->arfcn = htobe16(d_cell_allocation[input_nr]) ;
piotr6d152d92014-02-21 00:02:44 +0100834 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100835 tap_header->snr_db = 0;
ptrkrysik617ba032014-11-21 10:11:05 +0100836
837 int8_t header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE];
838 memcpy(header_plus_burst, tap_header.get(), sizeof(gsmtap_hdr));
839 memcpy(header_plus_burst+sizeof(gsmtap_hdr), burst_binary, BURST_SIZE);
840
841 pmt::pmt_t blob_header_plus_burst = pmt::make_blob(header_plus_burst,sizeof(gsmtap_hdr)+BURST_SIZE);
842 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob_header_plus_burst);
piotrf2b6a1b2014-08-04 11:28:59 +0200843
ptrkrysike518bbf2014-11-06 14:50:59 +0100844 if(input_nr==0){
845 message_port_pub(pmt::mp("C0"), msg);
846 } else {
847 message_port_pub(pmt::mp("CX"), msg);
848 }
piotrd0bf1492014-02-05 17:27:32 +0100849}
piotr6d152d92014-02-21 00:02:44 +0100850
piotrd0bf1492014-02-05 17:27:32 +0100851void receiver_impl::configure_receiver()
852{
piotrce92f982014-04-17 23:37:18 +0200853 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
piotrd0bf1492014-02-05 17:27:32 +0100854 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +0100855
piotrce92f982014-04-17 23:37:18 +0200856 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
857 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
858 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +0100859
piotrd0bf1492014-02-05 17:27:32 +0100860 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
861 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
862 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
863 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
864 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
865 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
866 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
867 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
868 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
869 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
870 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
871 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
872 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
873 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrd0bf1492014-02-05 17:27:32 +0100874}
piotr437f5462014-02-04 17:57:25 +0100875
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100876void receiver_impl::set_cell_allocation(const std::vector<int> &cell_allocation)
piotrf2b6a1b2014-08-04 11:28:59 +0200877{
ptrkrysike518bbf2014-11-06 14:50:59 +0100878 d_cell_allocation = cell_allocation;
879}
880
881void receiver_impl::set_tseq_nums(const std::vector<int> & tseq_nums)
882{
883 d_tseq_nums = tseq_nums;
piotrf2b6a1b2014-08-04 11:28:59 +0200884}
885
886void receiver_impl::reset()
887{
piotrd6d66872014-08-06 15:20:33 +0200888 d_state = fcch_search;
piotrf2b6a1b2014-08-04 11:28:59 +0200889}
piotr437f5462014-02-04 17:57:25 +0100890
piotrd0bf1492014-02-05 17:27:32 +0100891} /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +0100892} /* namespace gr */
893