blob: f43f6e0eb1e1148a61a6810f97c76706ee164768 [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>
Piotr Krysikd61f85b2016-08-29 07:38:25 +020029#include <volk/volk.h>
piotr437f5462014-02-04 17:57:25 +010030#include <math.h>
31#include <boost/circular_buffer.hpp>
32#include <algorithm>
33#include <numeric>
David Holmf2497bd2014-12-01 21:22:37 +010034#include <vector>
piotr437f5462014-02-04 17:57:25 +010035#include <viterbi_detector.h>
36#include <string.h>
piotr437f5462014-02-04 17:57:25 +010037#include <iostream>
Piotr Krysikd61f85b2016-08-29 07:38:25 +020038#include <time.h> //!!!
Piotr Krysik0a932e62016-08-29 07:37:30 +020039//#include <iomanip>
piotr6d152d92014-02-21 00:02:44 +010040#include <boost/scoped_ptr.hpp>
ptrkrysik3be74a72014-12-13 10:11:00 +010041
42#include <sch.h>
43#include "receiver_impl.h"
44#include <grgsm/endian.h>
ptrkrysik58213792014-10-30 09:05:15 +010045
ptrkrysikd85d4602014-11-13 10:11:53 +010046//files included for debuging
47//#include "plotting/plotting.hpp"
48//#include <pthread.h>
piotr437f5462014-02-04 17:57:25 +010049
50#define SYNC_SEARCH_RANGE 30
51
piotrd0bf1492014-02-05 17:27:32 +010052namespace gr
53{
54namespace gsm
55{
piotrd0bf1492014-02-05 17:27:32 +010056receiver::sptr
ptrkrysik380dea82015-08-06 10:11:58 +020057receiver::make(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums, bool process_uplink)
piotrd0bf1492014-02-05 17:27:32 +010058{
59 return gnuradio::get_initial_sptr
ptrkrysik380dea82015-08-06 10:11:58 +020060 (new receiver_impl(osr, cell_allocation, tseq_nums, process_uplink));
piotrd0bf1492014-02-05 17:27:32 +010061}
62
63/*
64 * The private constructor
65 */
ptrkrysik380dea82015-08-06 10:11:58 +020066receiver_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 +020067 : gr::sync_block("receiver",
ptrkrysik58213792014-10-30 09:05:15 +010068 gr::io_signature::make(1, -1, sizeof(gr_complex)),
piotr7c82b172014-02-08 14:15:27 +010069 gr::io_signature::make(0, 0, 0)),
piotrd0bf1492014-02-05 17:27:32 +010070 d_OSR(osr),
ptrkrysik380dea82015-08-06 10:11:58 +020071 d_process_uplink(process_uplink),
piotrd0bf1492014-02-05 17:27:32 +010072 d_chan_imp_length(CHAN_IMP_RESP_LENGTH),
piotrd0bf1492014-02-05 17:27:32 +010073 d_counter(0),
74 d_fcch_start_pos(0),
piotr4089c1a2014-08-06 14:10:56 +020075 d_freq_offset_setting(0),
piotrd6d66872014-08-06 15:20:33 +020076 d_state(fcch_search),
piotrd0bf1492014-02-05 17:27:32 +010077 d_burst_nr(osr),
piotr6d152d92014-02-21 00:02:44 +010078 d_failed_sch(0),
ptrkrysike518bbf2014-11-06 14:50:59 +010079 d_signal_dbm(-120),
80 d_tseq_nums(tseq_nums),
ptrkrysik32c21162015-04-04 14:01:52 +020081 d_cell_allocation(cell_allocation),
82 d_last_time(0.0)
piotrd0bf1492014-02-05 17:27:32 +010083{
84 int i;
piotr4089c1a2014-08-06 14:10:56 +020085 //don't send samples to the receiver until there are at least samples for one
piotr7f3f3662014-07-08 16:47:53 +020086 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 +010087 gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0));
88 for (i = 0; i < TRAIN_SEQ_NUM; i++)
piotr437f5462014-02-04 17:57:25 +010089 {
piotrf502e0f2014-04-24 10:28:29 +020090 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 +020091 //if first bit of the seqeunce ==1 first symbol ==-1
piotr437f5462014-02-04 17:57:25 +010092 gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint);
piotr437f5462014-02-04 17:57:25 +010093 }
ptrkrysike518bbf2014-11-06 14:50:59 +010094 message_port_register_out(pmt::mp("C0"));
95 message_port_register_out(pmt::mp("CX"));
piotr4089c1a2014-08-06 14:10:56 +020096 message_port_register_out(pmt::mp("measurements"));
piotr903b1d62014-04-17 11:33:27 +020097 configure_receiver(); //configure the receiver - tell it where to find which burst type
piotrd0bf1492014-02-05 17:27:32 +010098}
piotr437f5462014-02-04 17:57:25 +010099
piotrd0bf1492014-02-05 17:27:32 +0100100/*
101 * Our virtual destructor.
102 */
103receiver_impl::~receiver_impl()
104{
105}
106
piotrd0bf1492014-02-05 17:27:32 +0100107int
piotrc7c249a2014-05-02 17:24:08 +0200108receiver_impl::work(int noutput_items,
109 gr_vector_const_void_star &input_items,
110 gr_vector_void_star &output_items)
piotrd0bf1492014-02-05 17:27:32 +0100111{
ptrkrysik58213792014-10-30 09:05:15 +0100112// std::vector<const gr_complex *> iii = (std::vector<const gr_complex *>) input_items; // jak zrobić to rzutowanie poprawnie
113 gr_complex * input = (gr_complex *) input_items[0];
piotr4089c1a2014-08-06 14:10:56 +0200114 std::vector<tag_t> freq_offset_tags;
115 uint64_t start = nitems_read(0);
116 uint64_t stop = start + noutput_items;
piotr7c82b172014-02-08 14:15:27 +0100117
ptrkrysik32c21162015-04-04 14:01:52 +0200118 float current_time = static_cast<float>(start)/(GSM_SYMBOL_RATE*d_OSR);
119 if((current_time - d_last_time) > 0.1)
120 {
121 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("current_time"),pmt::from_double(current_time));
122 message_port_pub(pmt::mp("measurements"), msg);
123 d_last_time = current_time;
124 }
125
piotr4089c1a2014-08-06 14:10:56 +0200126 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
127 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
128 bool freq_offset_tag_in_fcch = false;
piotr4089c1a2014-08-06 14:10:56 +0200129
130 if(!freq_offset_tags.empty()){
131 tag_t freq_offset_tag = freq_offset_tags[0];
Piotr Krysik43af70d2016-07-20 21:37:24 +0200132 uint64_t tag_offset = freq_offset_tag.offset - start;
piotr4089c1a2014-08-06 14:10:56 +0200133
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 }
piotr4089c1a2014-08-06 14:10:56 +0200140 }
Piotr Krysik43af70d2016-07-20 21:37:24 +0200141 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
piotr4089c1a2014-08-06 14:10:56 +0200142 }
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
175 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst
176 {
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];
ptrkrysik58213792014-10-30 09:05:15 +0100201 burst_type b_type;
ptrkrysik380dea82015-08-06 10:11:58 +0200202 unsigned int inputs_to_process=d_cell_allocation.size();
piotr6d152d92014-02-21 00:02:44 +0100203
ptrkrysik380dea82015-08-06 10:11:58 +0200204 if(d_process_uplink)
205 {
206 inputs_to_process = 2*inputs_to_process;
207 }
208
209 for(int input_nr=0; input_nr<inputs_to_process; input_nr++)
piotrd0bf1492014-02-05 17:27:32 +0100210 {
ptrkrysik58213792014-10-30 09:05:15 +0100211 double signal_pwr = 0;
212 input = (gr_complex *)input_items[input_nr];
piotr4089c1a2014-08-06 14:10:56 +0200213
ptrkrysik58213792014-10-30 09:05:15 +0100214 for(int ii=GUARD_PERIOD;ii<TS_BITS;ii++)
piotrd0bf1492014-02-05 17:27:32 +0100215 {
ptrkrysik58213792014-10-30 09:05:15 +0100216 signal_pwr += abs(input[ii])*abs(input[ii]);
piotrd0bf1492014-02-05 17:27:32 +0100217 }
ptrkrysik58213792014-10-30 09:05:15 +0100218 signal_pwr = signal_pwr/(TS_BITS);
219 d_signal_dbm = round(10*log10(signal_pwr/50));
220 if(input_nr==0){
221 d_c0_signal_dbm = d_signal_dbm;
222 }
223
224 if(input_nr==0) //for c0 channel burst type is controlled by channel configuration
piotrd0bf1492014-02-05 17:27:32 +0100225 {
ptrkrysik58213792014-10-30 09:05:15 +0100226 b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number
227 }
228 else
229 {
230 b_type = normal_or_noise; //for the rest it can be only normal burst or noise (at least at this moment of development)
231 }
232
233 switch (b_type)
234 {
235 case fcch_burst: //if it's FCCH burst
236 {
Piotr Krysik43af70d2016-07-20 21:37:24 +0200237 if(freq_offset_tag_in_fcch==false)
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
Piotr Krysik43af70d2016-07-20 21:37:24 +0200242 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100243
Piotr Krysik43af70d2016-07-20 21:37:24 +0200244 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("synchronized"));
245 message_port_pub(pmt::mp("measurements"), msg);
246 }
ptrkrysik58213792014-10-30 09:05:15 +0100247 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);
Piotr Krysik9bc0fc02017-01-18 21:53:17 +0100291
ptrkrysik58213792014-10-30 09:05:15 +0100292 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);
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100317 //if(d_signal_dbm>=d_c0_signal_dbm-13)
ptrkrysik58213792014-10-30 09:05:15 +0100318 {
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;
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100336 if(input_nr<=d_tseq_nums.size())
337 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100338 tseq_num = d_tseq_nums[input_nr-1];
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100339 } else
340 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100341 tseq_num = d_tseq_nums.back();
342 }
343 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, tseq_num);
344// if(abs(d_c0_burst_start-burst_start)<=2){ //unused check/filter based on timing
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100345 // if((normal_corr_max/sqrt(signal_pwr))>=0.9)
346 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100347 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100348 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysike518bbf2014-11-06 14:50:59 +0100349 }
ptrkrysik58213792014-10-30 09:05:15 +0100350 }
351 break;
352 }
353 case empty: //if it's empty burst
354 break; //do nothing
355 }
356
ptrkrysik91c28352015-06-07 18:36:15 +0200357 if(input_nr==input_items.size()-1)
ptrkrysik58213792014-10-30 09:05:15 +0100358 {
359 d_burst_nr++; //go to next burst
360 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 +0100361 consume_each(to_consume);
362 }
363 //and add offset which is introduced by
364 //0.25 fractional part of a guard period
365 }
piotrd0bf1492014-02-05 17:27:32 +0100366 }
367 break;
piotr437f5462014-02-04 17:57:25 +0100368 }
piotr6d152d92014-02-21 00:02:44 +0100369 return 0;
piotrd0bf1492014-02-05 17:27:32 +0100370}
piotr437f5462014-02-04 17:57:25 +0100371
piotr4089c1a2014-08-06 14:10:56 +0200372bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset)
piotrd0bf1492014-02-05 17:27:32 +0100373{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100374 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 +0100375 //best match for FCCH burst
376 float phase_diff = 0;
377 gr_complex conjprod;
378 int start_pos = -1;
379 int hit_count = 0;
380 int miss_count = 0;
381 float min_phase_diff;
382 float max_phase_diff;
383 double best_sum = 0;
384 float lowest_max_min_diff = 99999;
385
386 int to_consume = 0;
387 int sample_number = 0;
388 bool end = false;
389 bool result = false;
ptrkrysikef5e2db2015-01-03 12:10:14 +0100390 boost::circular_buffer<float>::iterator buffer_iter;
piotr6d152d92014-02-21 00:02:44 +0100391
piotrd0bf1492014-02-05 17:27:32 +0100392 /**@name Possible states of FCCH search algorithm*/
393 //@{
394 enum states
piotr437f5462014-02-04 17:57:25 +0100395 {
piotr437f5462014-02-04 17:57:25 +0100396 init, ///< initialize variables
397 search, ///< search for positive samples
398 found_something, ///< search for FCCH and the best position of it
399 fcch_found, ///< when FCCH was found
400 search_fail ///< when there is no FCCH in the input vector
piotrd0bf1492014-02-05 17:27:32 +0100401 } fcch_search_state;
402 //@}
piotr437f5462014-02-04 17:57:25 +0100403
piotrd0bf1492014-02-05 17:27:32 +0100404 fcch_search_state = init;
piotr437f5462014-02-04 17:57:25 +0100405
piotrd0bf1492014-02-05 17:27:32 +0100406 while (!end)
407 {
408 switch (fcch_search_state)
409 {
piotr437f5462014-02-04 17:57:25 +0100410
piotrd0bf1492014-02-05 17:27:32 +0100411 case init: //initialize variables
piotr437f5462014-02-04 17:57:25 +0100412 hit_count = 0;
413 miss_count = 0;
414 start_pos = -1;
415 lowest_max_min_diff = 99999;
416 phase_diff_buffer.clear();
417 fcch_search_state = search;
418
419 break;
420
piotr7c82b172014-02-08 14:15:27 +0100421 case search: // search for positive samples
piotr437f5462014-02-04 17:57:25 +0100422 sample_number++;
423
piotrd0bf1492014-02-05 17:27:32 +0100424 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because
425 {
piotr7c82b172014-02-08 14:15:27 +0100426 //there's too few samples left to look into,
piotrd0bf1492014-02-05 17:27:32 +0100427 to_consume = sample_number; //don't do anything with those samples which are left
piotr7c82b172014-02-08 14:15:27 +0100428 //and consume only those which were checked
piotrd0bf1492014-02-05 17:27:32 +0100429 fcch_search_state = search_fail;
430 }
431 else
432 {
433 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
piotr437f5462014-02-04 17:57:25 +0100434
piotrd0bf1492014-02-05 17:27:32 +0100435 if (phase_diff > 0) //if a positive phase difference was found
436 {
437 to_consume = sample_number;
438 fcch_search_state = found_something; //switch to state in which searches for FCCH
439 }
440 else
441 {
442 fcch_search_state = search;
443 }
piotr437f5462014-02-04 17:57:25 +0100444 }
445
446 break;
447
piotrd0bf1492014-02-05 17:27:32 +0100448 case found_something: // search for FCCH and the best position of it
449 {
450 if (phase_diff > 0)
451 {
piotr437f5462014-02-04 17:57:25 +0100452 hit_count++; //positive phase differencies increases hits_count
piotrd0bf1492014-02-05 17:27:32 +0100453 }
454 else
455 {
piotr437f5462014-02-04 17:57:25 +0100456 miss_count++; //negative increases miss_count
piotrd0bf1492014-02-05 17:27:32 +0100457 }
piotr437f5462014-02-04 17:57:25 +0100458
piotrd0bf1492014-02-05 17:27:32 +0100459 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
460 {
piotr437f5462014-02-04 17:57:25 +0100461 //if miss_count exceeds limit before hit_count
462 fcch_search_state = init; //go to init
463 continue;
piotrd0bf1492014-02-05 17:27:32 +0100464 }
465 else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
466 {
piotr437f5462014-02-04 17:57:25 +0100467 //if hit_count and miss_count exceeds limit then FCCH was found
468 fcch_search_state = fcch_found;
469 continue;
piotrd0bf1492014-02-05 17:27:32 +0100470 }
471 else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR))
472 {
piotr437f5462014-02-04 17:57:25 +0100473 //find difference between minimal and maximal element in the buffer
474 //for FCCH this value should be low
475 //this part is searching for a region where this value is lowest
476 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
477 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
478
piotrd0bf1492014-02-05 17:27:32 +0100479 if (lowest_max_min_diff > max_phase_diff - min_phase_diff)
480 {
481 lowest_max_min_diff = max_phase_diff - min_phase_diff;
482 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
483 best_sum = 0;
piotr437f5462014-02-04 17:57:25 +0100484
piotrd0bf1492014-02-05 17:27:32 +0100485 for (buffer_iter = phase_diff_buffer.begin();
486 buffer_iter != (phase_diff_buffer.end());
487 buffer_iter++)
488 {
489 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
490 }
piotr437f5462014-02-04 17:57:25 +0100491 }
piotrd0bf1492014-02-05 17:27:32 +0100492 }
piotr437f5462014-02-04 17:57:25 +0100493
piotrd0bf1492014-02-05 17:27:32 +0100494 sample_number++;
piotr437f5462014-02-04 17:57:25 +0100495
piotrd0bf1492014-02-05 17:27:32 +0100496 if (sample_number >= nitems) //if there's no single sample left to check
497 {
piotr437f5462014-02-04 17:57:25 +0100498 fcch_search_state = search_fail;//FCCH search failed
499 continue;
piotr437f5462014-02-04 17:57:25 +0100500 }
piotrd0bf1492014-02-05 17:27:32 +0100501
502 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
503 phase_diff_buffer.push_back(phase_diff);
504 fcch_search_state = found_something;
505 }
506 break;
507
508 case fcch_found:
509 {
piotrd0bf1492014-02-05 17:27:32 +0100510 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
511
512 d_fcch_start_pos = d_counter + start_pos;
513
514 //compute frequency offset
515 double phase_offset = best_sum / FCCH_HITS_NEEDED;
piotr4089c1a2014-08-06 14:10:56 +0200516 double freq_offset = phase_offset * 1625000.0/6 / (2 * M_PI); //1625000.0/6 - GMSK symbol rate in GSM
517 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100518
519 end = true;
520 result = true;
piotr437f5462014-02-04 17:57:25 +0100521 break;
piotrd0bf1492014-02-05 17:27:32 +0100522 }
piotr437f5462014-02-04 17:57:25 +0100523
piotrd0bf1492014-02-05 17:27:32 +0100524 case search_fail:
piotr437f5462014-02-04 17:57:25 +0100525 end = true;
526 result = false;
527 break;
528 }
piotr437f5462014-02-04 17:57:25 +0100529 }
530
piotrd0bf1492014-02-05 17:27:32 +0100531 d_counter += to_consume;
532 consume_each(to_consume);
piotr437f5462014-02-04 17:57:25 +0100533
piotrd0bf1492014-02-05 17:27:32 +0100534 return result;
535}
536
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200537double receiver_impl::estim_freq_norm(const gr_complex * input, unsigned first_sample, unsigned last_sample) //another frequency estimator
piotrd0bf1492014-02-05 17:27:32 +0100538{
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200539
piotrd0bf1492014-02-05 17:27:32 +0100540 unsigned ii;
541
Piotr Krysik9bc0fc02017-01-18 21:53:17 +0100542 double sum = 0;
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200543
Piotr Krysik9bc0fc02017-01-18 21:53:17 +0100544 for (ii = first_sample; ii < last_sample-1; ii++)
piotr437f5462014-02-04 17:57:25 +0100545 {
Piotr Krysik9bc0fc02017-01-18 21:53:17 +0100546 sum += fast_atan2f(input[ii+1] * conj(input[ii]));
piotr437f5462014-02-04 17:57:25 +0100547 }
Piotr Krysik9bc0fc02017-01-18 21:53:17 +0100548 int N = last_sample-1-first_sample;
549 return sum/(2*M_PI)/N * d_OSR;
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200550}
551
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200552double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
553{
Piotr Krysik9bc0fc02017-01-18 21:53:17 +0100554 float freq_norm = estim_freq_norm(input, first_sample, last_sample);
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200555 float freq_offset = (freq_norm - 0.25) * 1625000.0/6.0;
piotrd0bf1492014-02-05 17:27:32 +0100556 return freq_offset;
557}
piotr437f5462014-02-04 17:57:25 +0100558
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200559//double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
560//{
561// double phase_sum = 0;
562// unsigned ii;
563
564// for (ii = first_sample; ii < last_sample; ii++)
565// {
566// double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
567// phase_sum += phase_diff;
568// }
569
570// double phase_offset = phase_sum / (last_sample - first_sample);
571// double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
572// return freq_offset;
573//}
574
piotrd0bf1492014-02-05 17:27:32 +0100575inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
576{
577 gr_complex conjprod = val1 * conj(val2);
578 return fast_atan2f(imag(conjprod), real(conjprod));
579}
piotr437f5462014-02-04 17:57:25 +0100580
piotrd0bf1492014-02-05 17:27:32 +0100581bool receiver_impl::reach_sch_burst(const int nitems)
582{
583 //it just consumes samples to get near to a SCH burst
584 int to_consume = 0;
585 bool result = false;
586 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
587
588 //consume samples until d_counter will be equal to sample_nr_near_sch_start
589 if (d_counter < sample_nr_near_sch_start)
590 {
591 if (d_counter + nitems >= sample_nr_near_sch_start)
592 {
593 to_consume = sample_nr_near_sch_start - d_counter;
594 }
595 else
596 {
597 to_consume = nitems;
piotr437f5462014-02-04 17:57:25 +0100598 }
599 result = false;
piotrd0bf1492014-02-05 17:27:32 +0100600 }
601 else
602 {
piotr437f5462014-02-04 17:57:25 +0100603 to_consume = 0;
604 result = true;
piotr437f5462014-02-04 17:57:25 +0100605 }
606
piotrd0bf1492014-02-05 17:27:32 +0100607 d_counter += to_consume;
608 consume_each(to_consume);
609 return result;
610}
611
612int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
613{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100614 std::vector<gr_complex> correlation_buffer;
615 std::vector<float> power_buffer;
616 std::vector<float> window_energy_buffer;
piotrd0bf1492014-02-05 17:27:32 +0100617
618 int strongest_window_nr;
619 int burst_start = 0;
620 int chan_imp_resp_center = 0;
621 float max_correlation = 0;
622 float energy = 0;
623
624 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++)
piotr437f5462014-02-04 17:57:25 +0100625 {
piotr437f5462014-02-04 17:57:25 +0100626 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
627 correlation_buffer.push_back(correlation);
628 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100629 }
piotrd0bf1492014-02-05 17:27:32 +0100630 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100631 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100632 bool loop_end = false;
633 while (iter != power_buffer.end())
634 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100635 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100636 energy = 0;
637
piotrd0bf1492014-02-05 17:27:32 +0100638 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++)
639 {
640 if (iter_ii == power_buffer.end())
641 {
642 loop_end = true;
643 break;
644 }
645 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100646 }
piotrd0bf1492014-02-05 17:27:32 +0100647 if (loop_end)
648 {
649 break;
piotr437f5462014-02-04 17:57:25 +0100650 }
651 iter++;
652 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100653 }
piotr437f5462014-02-04 17:57:25 +0100654
piotrd0bf1492014-02-05 17:27:32 +0100655 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100656 // d_channel_imp_resp.clear();
657
piotrd0bf1492014-02-05 17:27:32 +0100658 max_correlation = 0;
659 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++)
660 {
piotr437f5462014-02-04 17:57:25 +0100661 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100662 if (abs(correlation) > max_correlation)
663 {
664 chan_imp_resp_center = ii;
665 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100666 }
piotrd0bf1492014-02-05 17:27:32 +0100667 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100668 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100669 }
670
piotrd0bf1492014-02-05 17:27:32 +0100671 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
672 return burst_start;
673}
piotr437f5462014-02-04 17:57:25 +0100674
675
piotrd0bf1492014-02-05 17:27:32 +0100676void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
677{
678 float output[BURST_SIZE];
David Holmf2497bd2014-12-01 21:22:37 +0100679 std::vector<gr_complex> rhh_temp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100680 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
681 gr_complex filtered_burst[BURST_SIZE];
682 int start_state = 3;
683 unsigned int stop_states[2] = {4, 12};
684
David Holmf2497bd2014-12-01 21:22:37 +0100685 autocorrelation(chan_imp_resp, &rhh_temp[0], d_chan_imp_length*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100686 for (int ii = 0; ii < (d_chan_imp_length); ii++)
piotr437f5462014-02-04 17:57:25 +0100687 {
piotr437f5462014-02-04 17:57:25 +0100688 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
piotr437f5462014-02-04 17:57:25 +0100689 }
690
piotrd0bf1492014-02-05 17:27:32 +0100691 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
692
693 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
694
695 for (int i = 0; i < BURST_SIZE ; i++)
piotr437f5462014-02-04 17:57:25 +0100696 {
piotrd0bf1492014-02-05 17:27:32 +0100697 output_binary[i] = (output[i] > 0);
698 }
699}
piotr437f5462014-02-04 17:57:25 +0100700
piotrd0bf1492014-02-05 17:27:32 +0100701void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
702{
703 gr_complex j = gr_complex(0.0, 1.0);
piotr437f5462014-02-04 17:57:25 +0100704
piotrd0bf1492014-02-05 17:27:32 +0100705 int current_symbol;
706 int encoded_symbol;
707 int previous_symbol = 2 * input[0] - 1;
708 gmsk_output[0] = start_point;
709
710 for (int i = 1; i < nitems; i++)
711 {
piotr437f5462014-02-04 17:57:25 +0100712 //change bits representation to NRZ
713 current_symbol = 2 * input[i] - 1;
714 //differentially encode
715 encoded_symbol = current_symbol * previous_symbol;
716 //and do gmsk mapping
717 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
718 previous_symbol = current_symbol;
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 +0100722gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
723{
724 gr_complex result(0.0, 0.0);
725 int sample_number = 0;
726
727 for (int ii = 0; ii < length; ii++)
piotr437f5462014-02-04 17:57:25 +0100728 {
piotr437f5462014-02-04 17:57:25 +0100729 sample_number = (ii * d_OSR) ;
730 result += sequence[ii] * conj(input[sample_number]);
piotr437f5462014-02-04 17:57:25 +0100731 }
732
piotrd0bf1492014-02-05 17:27:32 +0100733 result = result / gr_complex(length, 0);
734 return result;
735}
736
737//computes autocorrelation for positive arguments
piotrd0bf1492014-02-05 17:27:32 +0100738inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
739{
740 int i, k;
741 for (k = nitems - 1; k >= 0; k--)
piotr437f5462014-02-04 17:57:25 +0100742 {
piotr437f5462014-02-04 17:57:25 +0100743 out[k] = gr_complex(0, 0);
piotrd0bf1492014-02-05 17:27:32 +0100744 for (i = k; i < nitems; i++)
745 {
746 out[k] += input[i] * conj(input[i-k]);
piotr437f5462014-02-04 17:57:25 +0100747 }
piotr437f5462014-02-04 17:57:25 +0100748 }
piotrd0bf1492014-02-05 17:27:32 +0100749}
piotr437f5462014-02-04 17:57:25 +0100750
piotrd0bf1492014-02-05 17:27:32 +0100751inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
752{
753 int ii = 0, n, a;
754
755 for (n = 0; n < nitems; n++)
piotr437f5462014-02-04 17:57:25 +0100756 {
piotr437f5462014-02-04 17:57:25 +0100757 a = n * d_OSR;
758 output[n] = 0;
759 ii = 0;
760
piotrd0bf1492014-02-05 17:27:32 +0100761 while (ii < filter_length)
762 {
piotrda8a0662014-04-24 10:29:38 +0200763 if ((a + ii) >= nitems*d_OSR){
piotrd0bf1492014-02-05 17:27:32 +0100764 break;
piotrda8a0662014-04-24 10:29:38 +0200765 }
piotrd0bf1492014-02-05 17:27:32 +0100766 output[n] += input[a+ii] * filter[ii];
767 ii++;
piotr437f5462014-02-04 17:57:25 +0100768 }
piotr437f5462014-02-04 17:57:25 +0100769 }
piotrd0bf1492014-02-05 17:27:32 +0100770}
piotr437f5462014-02-04 17:57:25 +0100771
piotrd0bf1492014-02-05 17:27:32 +0100772//especially computations of strongest_window_nr
piotr7e3b0db2014-02-05 22:44:30 +0100773int 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 +0100774{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100775 std::vector<gr_complex> correlation_buffer;
776 std::vector<float> power_buffer;
777 std::vector<float> window_energy_buffer;
piotr437f5462014-02-04 17:57:25 +0100778
piotrd0bf1492014-02-05 17:27:32 +0100779 int strongest_window_nr;
780 int burst_start = 0;
781 int chan_imp_resp_center = 0;
782 float max_correlation = 0;
783 float energy = 0;
piotr5c820252014-04-17 09:43:02 +0200784
piotrd0bf1492014-02-05 17:27:32 +0100785 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
piotr7c82b172014-02-08 14:15:27 +0100786 int search_start_pos = search_center + 1 - 5*d_OSR;
piotr437f5462014-02-04 17:57:25 +0100787 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
piotr5c820252014-04-17 09:43:02 +0200788 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 5 * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100789
ptrkrysik58213792014-10-30 09:05:15 +0100790 for(int ii = search_start_pos; ii < search_stop_pos; ii++)
piotrd0bf1492014-02-05 17:27:32 +0100791 {
piotr437f5462014-02-04 17:57:25 +0100792 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100793 correlation_buffer.push_back(correlation);
794 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100795 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100796// plot(power_buffer);
piotrd0bf1492014-02-05 17:27:32 +0100797 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100798 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100799 bool loop_end = false;
800 while (iter != power_buffer.end())
801 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100802 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100803 energy = 0;
804
Piotr Krysik97d4f8a2016-02-11 08:40:44 +0100805 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++, iter_ii++)
piotrd0bf1492014-02-05 17:27:32 +0100806 {
piotrd0bf1492014-02-05 17:27:32 +0100807 if (iter_ii == power_buffer.end())
808 {
809 loop_end = true;
810 break;
811 }
812 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100813 }
piotrd0bf1492014-02-05 17:27:32 +0100814 if (loop_end)
815 {
816 break;
piotr437f5462014-02-04 17:57:25 +0100817 }
818 iter++;
819
820 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100821 }
piotr437f5462014-02-04 17:57:25 +0100822
piotr5c820252014-04-17 09:43:02 +0200823 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()-((d_chan_imp_length)*d_OSR)) - window_energy_buffer.begin();
824 //strongest_window_nr = strongest_window_nr-d_OSR;
825 if(strongest_window_nr<0){
826 strongest_window_nr = 0;
827 }
piotr6d152d92014-02-21 00:02:44 +0100828
piotrd0bf1492014-02-05 17:27:32 +0100829 max_correlation = 0;
830 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++)
831 {
piotr437f5462014-02-04 17:57:25 +0100832 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100833 if (abs(correlation) > max_correlation)
834 {
835 chan_imp_resp_center = ii;
836 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100837 }
piotrd0bf1492014-02-05 17:27:32 +0100838 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100839 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100840 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100841
piotr7e3b0db2014-02-05 22:44:30 +0100842 *corr_max = max_correlation;
piotrd0bf1492014-02-05 17:27:32 +0100843
ptrkrysik58213792014-10-30 09:05:15 +0100844 //DCOUT("strongest_window_nr_new: " << strongest_window_nr);
piotrc7c249a2014-05-02 17:24:08 +0200845 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 +0100846
ptrkrysik58213792014-10-30 09:05:15 +0100847 //DCOUT("burst_start: " << burst_start);
piotrd0bf1492014-02-05 17:27:32 +0100848 return burst_start;
849}
piotr437f5462014-02-04 17:57:25 +0100850
851
ptrkrysik617ba032014-11-21 10:11:05 +0100852void 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 +0100853{
piotr6d152d92014-02-21 00:02:44 +0100854 boost::scoped_ptr<gsmtap_hdr> tap_header(new gsmtap_hdr());
ptrkrysik617ba032014-11-21 10:11:05 +0100855
piotr6d152d92014-02-21 00:02:44 +0100856 tap_header->version = GSMTAP_VERSION;
ptrkrysik7f61c642014-10-30 08:57:27 +0100857 tap_header->hdr_len = sizeof(gsmtap_hdr)/4;
piotr6d152d92014-02-21 00:02:44 +0100858 tap_header->type = GSMTAP_TYPE_UM_BURST;
ptrkrysik617ba032014-11-21 10:11:05 +0100859 tap_header->sub_type = burst_type;
ptrkrysik380dea82015-08-06 10:11:58 +0200860 bool uplink_burst = (input_nr >= d_cell_allocation.size());
861 if(!uplink_burst) // downlink burst
862 {
863 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
864 tap_header->frame_number = htobe32(d_burst_nr.get_frame_nr());
865 tap_header->arfcn = htobe16(d_cell_allocation[input_nr]) ;
866 }
867 else //uplink burst
868 {
869 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.subtract_timeslots(3).get_timeslot_nr());
870 tap_header->frame_number = htobe32(d_burst_nr.subtract_timeslots(3).get_frame_nr());
871 input_nr = input_nr - d_cell_allocation.size();
872 tap_header->arfcn = htobe16(d_cell_allocation[input_nr] | 0x4000);
873 }
piotr6d152d92014-02-21 00:02:44 +0100874 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100875 tap_header->snr_db = 0;
ptrkrysik617ba032014-11-21 10:11:05 +0100876
877 int8_t header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE];
878 memcpy(header_plus_burst, tap_header.get(), sizeof(gsmtap_hdr));
879 memcpy(header_plus_burst+sizeof(gsmtap_hdr), burst_binary, BURST_SIZE);
880
881 pmt::pmt_t blob_header_plus_burst = pmt::make_blob(header_plus_burst,sizeof(gsmtap_hdr)+BURST_SIZE);
882 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob_header_plus_burst);
piotrf2b6a1b2014-08-04 11:28:59 +0200883
ptrkrysike518bbf2014-11-06 14:50:59 +0100884 if(input_nr==0){
885 message_port_pub(pmt::mp("C0"), msg);
886 } else {
887 message_port_pub(pmt::mp("CX"), msg);
888 }
piotrd0bf1492014-02-05 17:27:32 +0100889}
piotr6d152d92014-02-21 00:02:44 +0100890
piotrd0bf1492014-02-05 17:27:32 +0100891void receiver_impl::configure_receiver()
892{
piotrce92f982014-04-17 23:37:18 +0200893 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
piotrd0bf1492014-02-05 17:27:32 +0100894 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +0100895
piotrce92f982014-04-17 23:37:18 +0200896 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
897 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
898 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +0100899
piotrd0bf1492014-02-05 17:27:32 +0100900 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
901 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
902 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
903 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
904 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
905 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
906 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
907 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
908 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
909 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
910 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
911 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
912 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
913 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrd0bf1492014-02-05 17:27:32 +0100914}
piotr437f5462014-02-04 17:57:25 +0100915
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100916void receiver_impl::set_cell_allocation(const std::vector<int> &cell_allocation)
piotrf2b6a1b2014-08-04 11:28:59 +0200917{
ptrkrysike518bbf2014-11-06 14:50:59 +0100918 d_cell_allocation = cell_allocation;
919}
920
921void receiver_impl::set_tseq_nums(const std::vector<int> & tseq_nums)
922{
923 d_tseq_nums = tseq_nums;
piotrf2b6a1b2014-08-04 11:28:59 +0200924}
925
926void receiver_impl::reset()
927{
piotrd6d66872014-08-06 15:20:33 +0200928 d_state = fcch_search;
piotrf2b6a1b2014-08-04 11:28:59 +0200929}
piotr437f5462014-02-04 17:57:25 +0100930
piotrd0bf1492014-02-05 17:27:32 +0100931} /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +0100932} /* namespace gr */
933