blob: f55d613cd3acdd37a42fa6de3ad4fb519a5baebc [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;
Piotr Krysikd61f85b2016-08-29 07:38:25 +020085
86 unsigned int alignment = volk_get_alignment();
87 d_freq_estim_vector = (lv_32fc_t*)volk_malloc(sizeof(lv_32fc_t)*160, alignment);
88 d_freq_estim_result = (lv_32fc_t*)volk_malloc(sizeof(lv_32fc_t)*1, alignment);
piotr4089c1a2014-08-06 14:10:56 +020089 //don't send samples to the receiver until there are at least samples for one
piotr7f3f3662014-07-08 16:47:53 +020090 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 +010091 gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0));
92 for (i = 0; i < TRAIN_SEQ_NUM; i++)
piotr437f5462014-02-04 17:57:25 +010093 {
piotrf502e0f2014-04-24 10:28:29 +020094 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 +020095 //if first bit of the seqeunce ==1 first symbol ==-1
piotr437f5462014-02-04 17:57:25 +010096 gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint);
piotr437f5462014-02-04 17:57:25 +010097 }
ptrkrysike518bbf2014-11-06 14:50:59 +010098 message_port_register_out(pmt::mp("C0"));
99 message_port_register_out(pmt::mp("CX"));
piotr4089c1a2014-08-06 14:10:56 +0200100 message_port_register_out(pmt::mp("measurements"));
piotr903b1d62014-04-17 11:33:27 +0200101 configure_receiver(); //configure the receiver - tell it where to find which burst type
piotrd0bf1492014-02-05 17:27:32 +0100102}
piotr437f5462014-02-04 17:57:25 +0100103
piotrd0bf1492014-02-05 17:27:32 +0100104/*
105 * Our virtual destructor.
106 */
107receiver_impl::~receiver_impl()
108{
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200109 volk_free(d_freq_estim_vector);
110 volk_free(d_freq_estim_result);
piotrd0bf1492014-02-05 17:27:32 +0100111}
112
piotrd0bf1492014-02-05 17:27:32 +0100113int
piotrc7c249a2014-05-02 17:24:08 +0200114receiver_impl::work(int noutput_items,
115 gr_vector_const_void_star &input_items,
116 gr_vector_void_star &output_items)
piotrd0bf1492014-02-05 17:27:32 +0100117{
ptrkrysik58213792014-10-30 09:05:15 +0100118// std::vector<const gr_complex *> iii = (std::vector<const gr_complex *>) input_items; // jak zrobić to rzutowanie poprawnie
119 gr_complex * input = (gr_complex *) input_items[0];
piotr4089c1a2014-08-06 14:10:56 +0200120 std::vector<tag_t> freq_offset_tags;
121 uint64_t start = nitems_read(0);
122 uint64_t stop = start + noutput_items;
piotr7c82b172014-02-08 14:15:27 +0100123
ptrkrysik32c21162015-04-04 14:01:52 +0200124 float current_time = static_cast<float>(start)/(GSM_SYMBOL_RATE*d_OSR);
125 if((current_time - d_last_time) > 0.1)
126 {
127 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("current_time"),pmt::from_double(current_time));
128 message_port_pub(pmt::mp("measurements"), msg);
129 d_last_time = current_time;
130 }
131
piotr4089c1a2014-08-06 14:10:56 +0200132 pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset");
133 get_tags_in_range(freq_offset_tags, 0, start, stop, key);
134 bool freq_offset_tag_in_fcch = false;
piotr4089c1a2014-08-06 14:10:56 +0200135
136 if(!freq_offset_tags.empty()){
137 tag_t freq_offset_tag = freq_offset_tags[0];
Piotr Krysik43af70d2016-07-20 21:37:24 +0200138 uint64_t tag_offset = freq_offset_tag.offset - start;
piotr4089c1a2014-08-06 14:10:56 +0200139
140 burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr);
141 if(d_state == synchronized && b_type == fcch_burst){
142 uint64_t last_sample_nr = ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1;
143 if(tag_offset < last_sample_nr){
piotr4089c1a2014-08-06 14:10:56 +0200144 freq_offset_tag_in_fcch = true;
145 }
piotr4089c1a2014-08-06 14:10:56 +0200146 }
Piotr Krysik43af70d2016-07-20 21:37:24 +0200147 d_freq_offset_setting = pmt::to_double(freq_offset_tag.value);
piotr4089c1a2014-08-06 14:10:56 +0200148 }
149
piotrd0bf1492014-02-05 17:27:32 +0100150 switch (d_state)
piotr437f5462014-02-04 17:57:25 +0100151 {
piotrd0bf1492014-02-05 17:27:32 +0100152 //bootstrapping
ptrkrysik58213792014-10-30 09:05:15 +0100153 case fcch_search:
piotrd0bf1492014-02-05 17:27:32 +0100154 {
piotr4089c1a2014-08-06 14:10:56 +0200155 double freq_offset_tmp;
156 if (find_fcch_burst(input, noutput_items,freq_offset_tmp))
piotrd0bf1492014-02-05 17:27:32 +0100157 {
piotrd6d66872014-08-06 15:20:33 +0200158 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 +0200159 message_port_pub(pmt::mp("measurements"), msg);
160
piotrd0bf1492014-02-05 17:27:32 +0100161 d_state = sch_search;
162 }
163 else
164 {
piotrd6d66872014-08-06 15:20:33 +0200165 d_state = fcch_search;
piotrd0bf1492014-02-05 17:27:32 +0100166 }
167 break;
168 }
piotr437f5462014-02-04 17:57:25 +0100169
piotrd0bf1492014-02-05 17:27:32 +0100170 case sch_search:
171 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100172 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100173 int t1, t2, t3;
174 int burst_start = 0;
175 unsigned char output_binary[BURST_SIZE];
piotr437f5462014-02-04 17:57:25 +0100176
piotrc7c249a2014-05-02 17:24:08 +0200177 if (reach_sch_burst(noutput_items)) //wait for a SCH burst
piotrd0bf1492014-02-05 17:27:32 +0100178 {
179 burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it
180 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection
181 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst
182 {
piotr437f5462014-02-04 17:57:25 +0100183 d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value
piotr437f5462014-02-04 17:57:25 +0100184 d_burst_nr++;
185
piotr7f3f3662014-07-08 16:47:53 +0200186 consume_each(burst_start + BURST_SIZE * d_OSR + 4*d_OSR); //consume samples up to next guard period
piotr437f5462014-02-04 17:57:25 +0100187 d_state = synchronized;
piotrd0bf1492014-02-05 17:27:32 +0100188 }
189 else
190 {
piotrd6d66872014-08-06 15:20:33 +0200191 d_state = fcch_search; //if there is error in the sch burst go back to fcch search phase
piotr437f5462014-02-04 17:57:25 +0100192 }
piotrd0bf1492014-02-05 17:27:32 +0100193 }
194 else
195 {
196 d_state = sch_search;
197 }
198 break;
199 }
200 //in this state receiver is synchronized and it processes bursts according to burst type for given burst number
201 case synchronized:
202 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100203 std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100204 int offset = 0;
205 int to_consume = 0;
206 unsigned char output_binary[BURST_SIZE];
ptrkrysik58213792014-10-30 09:05:15 +0100207 burst_type b_type;
ptrkrysik380dea82015-08-06 10:11:58 +0200208 unsigned int inputs_to_process=d_cell_allocation.size();
piotr6d152d92014-02-21 00:02:44 +0100209
ptrkrysik380dea82015-08-06 10:11:58 +0200210 if(d_process_uplink)
211 {
212 inputs_to_process = 2*inputs_to_process;
213 }
214
215 for(int input_nr=0; input_nr<inputs_to_process; input_nr++)
piotrd0bf1492014-02-05 17:27:32 +0100216 {
ptrkrysik58213792014-10-30 09:05:15 +0100217 double signal_pwr = 0;
218 input = (gr_complex *)input_items[input_nr];
piotr4089c1a2014-08-06 14:10:56 +0200219
ptrkrysik58213792014-10-30 09:05:15 +0100220 for(int ii=GUARD_PERIOD;ii<TS_BITS;ii++)
piotrd0bf1492014-02-05 17:27:32 +0100221 {
ptrkrysik58213792014-10-30 09:05:15 +0100222 signal_pwr += abs(input[ii])*abs(input[ii]);
piotrd0bf1492014-02-05 17:27:32 +0100223 }
ptrkrysik58213792014-10-30 09:05:15 +0100224 signal_pwr = signal_pwr/(TS_BITS);
225 d_signal_dbm = round(10*log10(signal_pwr/50));
226 if(input_nr==0){
227 d_c0_signal_dbm = d_signal_dbm;
228 }
229
230 if(input_nr==0) //for c0 channel burst type is controlled by channel configuration
piotrd0bf1492014-02-05 17:27:32 +0100231 {
ptrkrysik58213792014-10-30 09:05:15 +0100232 b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number
233 }
234 else
235 {
236 b_type = normal_or_noise; //for the rest it can be only normal burst or noise (at least at this moment of development)
237 }
238
239 switch (b_type)
240 {
241 case fcch_burst: //if it's FCCH burst
242 {
Piotr Krysik43af70d2016-07-20 21:37:24 +0200243 if(freq_offset_tag_in_fcch==false)
244 {
245 const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1;
246 const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR;
247 double freq_offset_tmp = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it
Piotr Krysik43af70d2016-07-20 21:37:24 +0200248 send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100249
Piotr Krysik43af70d2016-07-20 21:37:24 +0200250 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("synchronized"));
251 message_port_pub(pmt::mp("measurements"), msg);
252 }
ptrkrysik58213792014-10-30 09:05:15 +0100253 break;
254 }
255 case sch_burst: //if it's SCH burst
256 {
257 int t1, t2, t3, d_ncc, d_bcc;
258 d_c0_burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response
259
260 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100261 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_SCH, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100262 if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data
piotrd0bf1492014-02-05 17:27:32 +0100263 {
ptrkrysik58213792014-10-30 09:05:15 +0100264 // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct
265 d_failed_sch = 0;
266 offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period
267 to_consume += offset; //adjust with offset number of samples to be consumed
piotr437f5462014-02-04 17:57:25 +0100268 }
ptrkrysik58213792014-10-30 09:05:15 +0100269 else
270 {
271 d_failed_sch++;
272 if (d_failed_sch >= MAX_SCH_ERRORS)
273 {
274 d_state = fcch_search;
275 pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(0.0),pmt::mp("sync_loss"));
276 message_port_pub(pmt::mp("measurements"), msg);
ptrkrysikd57745d2014-12-02 19:05:36 +0100277 //DCOUT("Re-Synchronization!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
ptrkrysik58213792014-10-30 09:05:15 +0100278 }
279 }
280 break;
piotr437f5462014-02-04 17:57:25 +0100281 }
ptrkrysik58213792014-10-30 09:05:15 +0100282 case normal_burst:
283 {
284 float normal_corr_max; //if it's normal burst
285 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
286 detect_burst(input, &channel_imp_resp[0], d_c0_burst_start, output_binary); //MLSE detection of bits
ptrkrysik617ba032014-11-21 10:11:05 +0100287 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100288 break;
289 }
290 case dummy_or_normal:
291 {
292 unsigned int normal_burst_start, dummy_burst_start;
293 float dummy_corr_max, normal_corr_max;
piotr437f5462014-02-04 17:57:25 +0100294
ptrkrysik58213792014-10-30 09:05:15 +0100295 dummy_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY);
296 normal_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc);
297
298 if (normal_corr_max > dummy_corr_max)
299 {
300 d_c0_burst_start = normal_burst_start;
301 detect_burst(input, &channel_imp_resp[0], normal_burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100302 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100303 }
304 else
305 {
306 d_c0_burst_start = dummy_burst_start;
ptrkrysik617ba032014-11-21 10:11:05 +0100307 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100308 }
309 break;
piotrd0bf1492014-02-05 17:27:32 +0100310 }
ptrkrysik58213792014-10-30 09:05:15 +0100311 case rach_burst:
312 break;
313 case dummy:
ptrkrysik617ba032014-11-21 10:11:05 +0100314 send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr);
ptrkrysik58213792014-10-30 09:05:15 +0100315 break;
316 case normal_or_noise:
317 {
318 unsigned int burst_start;
319 float normal_corr_max_tmp;
320 float normal_corr_max=-1e6;
321 int max_tn;
322 std::vector<gr_complex> v(input, input + noutput_items);
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100323 //if(d_signal_dbm>=d_c0_signal_dbm-13)
ptrkrysik58213792014-10-30 09:05:15 +0100324 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100325 if(d_tseq_nums.size()==0) //there is no information about training sequence
326 { //however the receiver can detect it
327 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, 0);
328 float ts_max=normal_corr_max; //with use of a very simple algorithm based on finding
329 int ts_max_num=0; //maximum correlation
330 for(int ss=1; ss<=7; ss++)
331 {
332 get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, ss);
333 if(ts_max<normal_corr_max)
334 {
335 ts_max = normal_corr_max;
336 ts_max_num = ss;
337 }
338 }
339 d_tseq_nums.push_back(ts_max_num);
ptrkrysik58213792014-10-30 09:05:15 +0100340 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100341 int tseq_num;
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100342 if(input_nr<=d_tseq_nums.size())
343 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100344 tseq_num = d_tseq_nums[input_nr-1];
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100345 } else
346 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100347 tseq_num = d_tseq_nums.back();
348 }
349 burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, tseq_num);
350// if(abs(d_c0_burst_start-burst_start)<=2){ //unused check/filter based on timing
Piotr Krysikf0ec6592016-03-11 09:05:46 +0100351 // if((normal_corr_max/sqrt(signal_pwr))>=0.9)
352 {
ptrkrysike518bbf2014-11-06 14:50:59 +0100353 detect_burst(input, &channel_imp_resp[0], burst_start, output_binary);
ptrkrysik617ba032014-11-21 10:11:05 +0100354 send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr);
ptrkrysike518bbf2014-11-06 14:50:59 +0100355 }
ptrkrysik58213792014-10-30 09:05:15 +0100356 }
357 break;
358 }
359 case empty: //if it's empty burst
360 break; //do nothing
361 }
362
ptrkrysik91c28352015-06-07 18:36:15 +0200363 if(input_nr==input_items.size()-1)
ptrkrysik58213792014-10-30 09:05:15 +0100364 {
365 d_burst_nr++; //go to next burst
366 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 +0100367 consume_each(to_consume);
368 }
369 //and add offset which is introduced by
370 //0.25 fractional part of a guard period
371 }
piotrd0bf1492014-02-05 17:27:32 +0100372 }
373 break;
piotr437f5462014-02-04 17:57:25 +0100374 }
piotr6d152d92014-02-21 00:02:44 +0100375 return 0;
piotrd0bf1492014-02-05 17:27:32 +0100376}
piotr437f5462014-02-04 17:57:25 +0100377
piotr4089c1a2014-08-06 14:10:56 +0200378bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset)
piotrd0bf1492014-02-05 17:27:32 +0100379{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100380 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 +0100381 //best match for FCCH burst
382 float phase_diff = 0;
383 gr_complex conjprod;
384 int start_pos = -1;
385 int hit_count = 0;
386 int miss_count = 0;
387 float min_phase_diff;
388 float max_phase_diff;
389 double best_sum = 0;
390 float lowest_max_min_diff = 99999;
391
392 int to_consume = 0;
393 int sample_number = 0;
394 bool end = false;
395 bool result = false;
ptrkrysikef5e2db2015-01-03 12:10:14 +0100396 boost::circular_buffer<float>::iterator buffer_iter;
piotr6d152d92014-02-21 00:02:44 +0100397
piotrd0bf1492014-02-05 17:27:32 +0100398 /**@name Possible states of FCCH search algorithm*/
399 //@{
400 enum states
piotr437f5462014-02-04 17:57:25 +0100401 {
piotr437f5462014-02-04 17:57:25 +0100402 init, ///< initialize variables
403 search, ///< search for positive samples
404 found_something, ///< search for FCCH and the best position of it
405 fcch_found, ///< when FCCH was found
406 search_fail ///< when there is no FCCH in the input vector
piotrd0bf1492014-02-05 17:27:32 +0100407 } fcch_search_state;
408 //@}
piotr437f5462014-02-04 17:57:25 +0100409
piotrd0bf1492014-02-05 17:27:32 +0100410 fcch_search_state = init;
piotr437f5462014-02-04 17:57:25 +0100411
piotrd0bf1492014-02-05 17:27:32 +0100412 while (!end)
413 {
414 switch (fcch_search_state)
415 {
piotr437f5462014-02-04 17:57:25 +0100416
piotrd0bf1492014-02-05 17:27:32 +0100417 case init: //initialize variables
piotr437f5462014-02-04 17:57:25 +0100418 hit_count = 0;
419 miss_count = 0;
420 start_pos = -1;
421 lowest_max_min_diff = 99999;
422 phase_diff_buffer.clear();
423 fcch_search_state = search;
424
425 break;
426
piotr7c82b172014-02-08 14:15:27 +0100427 case search: // search for positive samples
piotr437f5462014-02-04 17:57:25 +0100428 sample_number++;
429
piotrd0bf1492014-02-05 17:27:32 +0100430 if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because
431 {
piotr7c82b172014-02-08 14:15:27 +0100432 //there's too few samples left to look into,
piotrd0bf1492014-02-05 17:27:32 +0100433 to_consume = sample_number; //don't do anything with those samples which are left
piotr7c82b172014-02-08 14:15:27 +0100434 //and consume only those which were checked
piotrd0bf1492014-02-05 17:27:32 +0100435 fcch_search_state = search_fail;
436 }
437 else
438 {
439 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
piotr437f5462014-02-04 17:57:25 +0100440
piotrd0bf1492014-02-05 17:27:32 +0100441 if (phase_diff > 0) //if a positive phase difference was found
442 {
443 to_consume = sample_number;
444 fcch_search_state = found_something; //switch to state in which searches for FCCH
445 }
446 else
447 {
448 fcch_search_state = search;
449 }
piotr437f5462014-02-04 17:57:25 +0100450 }
451
452 break;
453
piotrd0bf1492014-02-05 17:27:32 +0100454 case found_something: // search for FCCH and the best position of it
455 {
456 if (phase_diff > 0)
457 {
piotr437f5462014-02-04 17:57:25 +0100458 hit_count++; //positive phase differencies increases hits_count
piotrd0bf1492014-02-05 17:27:32 +0100459 }
460 else
461 {
piotr437f5462014-02-04 17:57:25 +0100462 miss_count++; //negative increases miss_count
piotrd0bf1492014-02-05 17:27:32 +0100463 }
piotr437f5462014-02-04 17:57:25 +0100464
piotrd0bf1492014-02-05 17:27:32 +0100465 if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR))
466 {
piotr437f5462014-02-04 17:57:25 +0100467 //if miss_count exceeds limit before hit_count
468 fcch_search_state = init; //go to init
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)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR))
472 {
piotr437f5462014-02-04 17:57:25 +0100473 //if hit_count and miss_count exceeds limit then FCCH was found
474 fcch_search_state = fcch_found;
475 continue;
piotrd0bf1492014-02-05 17:27:32 +0100476 }
477 else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR))
478 {
piotr437f5462014-02-04 17:57:25 +0100479 //find difference between minimal and maximal element in the buffer
480 //for FCCH this value should be low
481 //this part is searching for a region where this value is lowest
482 min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
483 max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end()));
484
piotrd0bf1492014-02-05 17:27:32 +0100485 if (lowest_max_min_diff > max_phase_diff - min_phase_diff)
486 {
487 lowest_max_min_diff = max_phase_diff - min_phase_diff;
488 start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos
489 best_sum = 0;
piotr437f5462014-02-04 17:57:25 +0100490
piotrd0bf1492014-02-05 17:27:32 +0100491 for (buffer_iter = phase_diff_buffer.begin();
492 buffer_iter != (phase_diff_buffer.end());
493 buffer_iter++)
494 {
495 best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum
496 }
piotr437f5462014-02-04 17:57:25 +0100497 }
piotrd0bf1492014-02-05 17:27:32 +0100498 }
piotr437f5462014-02-04 17:57:25 +0100499
piotrd0bf1492014-02-05 17:27:32 +0100500 sample_number++;
piotr437f5462014-02-04 17:57:25 +0100501
piotrd0bf1492014-02-05 17:27:32 +0100502 if (sample_number >= nitems) //if there's no single sample left to check
503 {
piotr437f5462014-02-04 17:57:25 +0100504 fcch_search_state = search_fail;//FCCH search failed
505 continue;
piotr437f5462014-02-04 17:57:25 +0100506 }
piotrd0bf1492014-02-05 17:27:32 +0100507
508 phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]);
509 phase_diff_buffer.push_back(phase_diff);
510 fcch_search_state = found_something;
511 }
512 break;
513
514 case fcch_found:
515 {
piotrd0bf1492014-02-05 17:27:32 +0100516 to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst
517
518 d_fcch_start_pos = d_counter + start_pos;
519
520 //compute frequency offset
521 double phase_offset = best_sum / FCCH_HITS_NEEDED;
piotr4089c1a2014-08-06 14:10:56 +0200522 double freq_offset = phase_offset * 1625000.0/6 / (2 * M_PI); //1625000.0/6 - GMSK symbol rate in GSM
523 computed_freq_offset = freq_offset;
piotrd0bf1492014-02-05 17:27:32 +0100524
525 end = true;
526 result = true;
piotr437f5462014-02-04 17:57:25 +0100527 break;
piotrd0bf1492014-02-05 17:27:32 +0100528 }
piotr437f5462014-02-04 17:57:25 +0100529
piotrd0bf1492014-02-05 17:27:32 +0100530 case search_fail:
piotr437f5462014-02-04 17:57:25 +0100531 end = true;
532 result = false;
533 break;
534 }
piotr437f5462014-02-04 17:57:25 +0100535 }
536
piotrd0bf1492014-02-05 17:27:32 +0100537 d_counter += to_consume;
538 consume_each(to_consume);
piotr437f5462014-02-04 17:57:25 +0100539
piotrd0bf1492014-02-05 17:27:32 +0100540 return result;
541}
542
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200543double receiver_impl::estim_freq_norm(const gr_complex * input, unsigned first_sample, unsigned last_sample) //another frequency estimator
piotrd0bf1492014-02-05 17:27:32 +0100544{
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200545
piotrd0bf1492014-02-05 17:27:32 +0100546 unsigned ii;
547
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200548 gr_complex sum = 0;
549
550 for (ii = first_sample; ii < last_sample-d_OSR; ii=ii+d_OSR)
piotr437f5462014-02-04 17:57:25 +0100551 {
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200552 sum += input[ii+d_OSR] * conj(input[ii]);
piotr437f5462014-02-04 17:57:25 +0100553 }
554
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200555 return fast_atan2f(imag(sum), real(sum))/(2*M_PI);
556}
557
558double receiver_impl::estim_freq_norm2(const gr_complex * input, unsigned first_sample, unsigned last_sample) //another frequency estimator - faster one
559{
560
561 unsigned ii;
562
563 int N = (last_sample-first_sample)/d_OSR;
564
565 for (unsigned ii = 0; ii < N; ii++)
566 {
567 d_freq_estim_vector[ii] = input[first_sample+ii*d_OSR];
568 }
569
570 volk_32fc_x2_conjugate_dot_prod_32fc(d_freq_estim_result, d_freq_estim_vector+1, d_freq_estim_vector, N-1);
571
572 return fast_atan2f(imag(d_freq_estim_result[0]), real(d_freq_estim_result[0]))/(2*M_PI);
573}
574
575
576double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
577{
578 float freq_norm = estim_freq_norm2(input, first_sample, last_sample);
579
580// using namespace std;
581// clock_t begin = clock();
582//
583// for(int ii=0;ii<500;ii++){
584// float dupa = estim_freq_norm2(input, first_sample, last_sample);
585// }
586// clock_t end = clock();
587// double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
588// std::cout << "elapsed_secs " << elapsed_secs << std::endl;
589
590// begin = clock();
591//
592// for(int ii=0;ii<500;ii++){
593// float dupa = estim_freq_norm(input, first_sample, last_sample);
594// }
595// end = clock();
596// elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
597// std::cout << "elapsed_secs_old " << elapsed_secs << std::endl;
598
599 float freq_offset = (freq_norm - 0.25) * 1625000.0/6.0;
600
piotrd0bf1492014-02-05 17:27:32 +0100601 return freq_offset;
602}
piotr437f5462014-02-04 17:57:25 +0100603
Piotr Krysikd61f85b2016-08-29 07:38:25 +0200604//double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample)
605//{
606// double phase_sum = 0;
607// unsigned ii;
608
609// for (ii = first_sample; ii < last_sample; ii++)
610// {
611// double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR;
612// phase_sum += phase_diff;
613// }
614
615// double phase_offset = phase_sum / (last_sample - first_sample);
616// double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI);
617// return freq_offset;
618//}
619
piotrd0bf1492014-02-05 17:27:32 +0100620inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2)
621{
622 gr_complex conjprod = val1 * conj(val2);
623 return fast_atan2f(imag(conjprod), real(conjprod));
624}
piotr437f5462014-02-04 17:57:25 +0100625
piotrd0bf1492014-02-05 17:27:32 +0100626bool receiver_impl::reach_sch_burst(const int nitems)
627{
628 //it just consumes samples to get near to a SCH burst
629 int to_consume = 0;
630 bool result = false;
631 unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR;
632
633 //consume samples until d_counter will be equal to sample_nr_near_sch_start
634 if (d_counter < sample_nr_near_sch_start)
635 {
636 if (d_counter + nitems >= sample_nr_near_sch_start)
637 {
638 to_consume = sample_nr_near_sch_start - d_counter;
639 }
640 else
641 {
642 to_consume = nitems;
piotr437f5462014-02-04 17:57:25 +0100643 }
644 result = false;
piotrd0bf1492014-02-05 17:27:32 +0100645 }
646 else
647 {
piotr437f5462014-02-04 17:57:25 +0100648 to_consume = 0;
649 result = true;
piotr437f5462014-02-04 17:57:25 +0100650 }
651
piotrd0bf1492014-02-05 17:27:32 +0100652 d_counter += to_consume;
653 consume_each(to_consume);
654 return result;
655}
656
657int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp)
658{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100659 std::vector<gr_complex> correlation_buffer;
660 std::vector<float> power_buffer;
661 std::vector<float> window_energy_buffer;
piotrd0bf1492014-02-05 17:27:32 +0100662
663 int strongest_window_nr;
664 int burst_start = 0;
665 int chan_imp_resp_center = 0;
666 float max_correlation = 0;
667 float energy = 0;
668
669 for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++)
piotr437f5462014-02-04 17:57:25 +0100670 {
piotr437f5462014-02-04 17:57:25 +0100671 gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]);
672 correlation_buffer.push_back(correlation);
673 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100674 }
piotrd0bf1492014-02-05 17:27:32 +0100675 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100676 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100677 bool loop_end = false;
678 while (iter != power_buffer.end())
679 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100680 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100681 energy = 0;
682
piotrd0bf1492014-02-05 17:27:32 +0100683 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++)
684 {
685 if (iter_ii == power_buffer.end())
686 {
687 loop_end = true;
688 break;
689 }
690 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100691 }
piotrd0bf1492014-02-05 17:27:32 +0100692 if (loop_end)
693 {
694 break;
piotr437f5462014-02-04 17:57:25 +0100695 }
696 iter++;
697 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100698 }
piotr437f5462014-02-04 17:57:25 +0100699
piotrd0bf1492014-02-05 17:27:32 +0100700 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin();
piotr437f5462014-02-04 17:57:25 +0100701 // d_channel_imp_resp.clear();
702
piotrd0bf1492014-02-05 17:27:32 +0100703 max_correlation = 0;
704 for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++)
705 {
piotr437f5462014-02-04 17:57:25 +0100706 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100707 if (abs(correlation) > max_correlation)
708 {
709 chan_imp_resp_center = ii;
710 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100711 }
piotrd0bf1492014-02-05 17:27:32 +0100712 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100713 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100714 }
715
piotrd0bf1492014-02-05 17:27:32 +0100716 burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR;
717 return burst_start;
718}
piotr437f5462014-02-04 17:57:25 +0100719
720
piotrd0bf1492014-02-05 17:27:32 +0100721void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary)
722{
723 float output[BURST_SIZE];
David Holmf2497bd2014-12-01 21:22:37 +0100724 std::vector<gr_complex> rhh_temp(CHAN_IMP_RESP_LENGTH*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100725 gr_complex rhh[CHAN_IMP_RESP_LENGTH];
726 gr_complex filtered_burst[BURST_SIZE];
727 int start_state = 3;
728 unsigned int stop_states[2] = {4, 12};
729
David Holmf2497bd2014-12-01 21:22:37 +0100730 autocorrelation(chan_imp_resp, &rhh_temp[0], d_chan_imp_length*d_OSR);
piotrd0bf1492014-02-05 17:27:32 +0100731 for (int ii = 0; ii < (d_chan_imp_length); ii++)
piotr437f5462014-02-04 17:57:25 +0100732 {
piotr437f5462014-02-04 17:57:25 +0100733 rhh[ii] = conj(rhh_temp[ii*d_OSR]);
piotr437f5462014-02-04 17:57:25 +0100734 }
735
piotrd0bf1492014-02-05 17:27:32 +0100736 mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst);
737
738 viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output);
739
740 for (int i = 0; i < BURST_SIZE ; i++)
piotr437f5462014-02-04 17:57:25 +0100741 {
piotrd0bf1492014-02-05 17:27:32 +0100742 output_binary[i] = (output[i] > 0);
743 }
744}
piotr437f5462014-02-04 17:57:25 +0100745
piotrd0bf1492014-02-05 17:27:32 +0100746void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point)
747{
748 gr_complex j = gr_complex(0.0, 1.0);
piotr437f5462014-02-04 17:57:25 +0100749
piotrd0bf1492014-02-05 17:27:32 +0100750 int current_symbol;
751 int encoded_symbol;
752 int previous_symbol = 2 * input[0] - 1;
753 gmsk_output[0] = start_point;
754
755 for (int i = 1; i < nitems; i++)
756 {
piotr437f5462014-02-04 17:57:25 +0100757 //change bits representation to NRZ
758 current_symbol = 2 * input[i] - 1;
759 //differentially encode
760 encoded_symbol = current_symbol * previous_symbol;
761 //and do gmsk mapping
762 gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1];
763 previous_symbol = current_symbol;
piotr437f5462014-02-04 17:57:25 +0100764 }
piotrd0bf1492014-02-05 17:27:32 +0100765}
piotr437f5462014-02-04 17:57:25 +0100766
piotrd0bf1492014-02-05 17:27:32 +0100767gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input)
768{
769 gr_complex result(0.0, 0.0);
770 int sample_number = 0;
771
772 for (int ii = 0; ii < length; ii++)
piotr437f5462014-02-04 17:57:25 +0100773 {
piotr437f5462014-02-04 17:57:25 +0100774 sample_number = (ii * d_OSR) ;
775 result += sequence[ii] * conj(input[sample_number]);
piotr437f5462014-02-04 17:57:25 +0100776 }
777
piotrd0bf1492014-02-05 17:27:32 +0100778 result = result / gr_complex(length, 0);
779 return result;
780}
781
782//computes autocorrelation for positive arguments
piotrd0bf1492014-02-05 17:27:32 +0100783inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems)
784{
785 int i, k;
786 for (k = nitems - 1; k >= 0; k--)
piotr437f5462014-02-04 17:57:25 +0100787 {
piotr437f5462014-02-04 17:57:25 +0100788 out[k] = gr_complex(0, 0);
piotrd0bf1492014-02-05 17:27:32 +0100789 for (i = k; i < nitems; i++)
790 {
791 out[k] += input[i] * conj(input[i-k]);
piotr437f5462014-02-04 17:57:25 +0100792 }
piotr437f5462014-02-04 17:57:25 +0100793 }
piotrd0bf1492014-02-05 17:27:32 +0100794}
piotr437f5462014-02-04 17:57:25 +0100795
piotrd0bf1492014-02-05 17:27:32 +0100796inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output)
797{
798 int ii = 0, n, a;
799
800 for (n = 0; n < nitems; n++)
piotr437f5462014-02-04 17:57:25 +0100801 {
piotr437f5462014-02-04 17:57:25 +0100802 a = n * d_OSR;
803 output[n] = 0;
804 ii = 0;
805
piotrd0bf1492014-02-05 17:27:32 +0100806 while (ii < filter_length)
807 {
piotrda8a0662014-04-24 10:29:38 +0200808 if ((a + ii) >= nitems*d_OSR){
piotrd0bf1492014-02-05 17:27:32 +0100809 break;
piotrda8a0662014-04-24 10:29:38 +0200810 }
piotrd0bf1492014-02-05 17:27:32 +0100811 output[n] += input[a+ii] * filter[ii];
812 ii++;
piotr437f5462014-02-04 17:57:25 +0100813 }
piotr437f5462014-02-04 17:57:25 +0100814 }
piotrd0bf1492014-02-05 17:27:32 +0100815}
piotr437f5462014-02-04 17:57:25 +0100816
piotrd0bf1492014-02-05 17:27:32 +0100817//especially computations of strongest_window_nr
piotr7e3b0db2014-02-05 22:44:30 +0100818int 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 +0100819{
ptrkrysikef5e2db2015-01-03 12:10:14 +0100820 std::vector<gr_complex> correlation_buffer;
821 std::vector<float> power_buffer;
822 std::vector<float> window_energy_buffer;
piotr437f5462014-02-04 17:57:25 +0100823
piotrd0bf1492014-02-05 17:27:32 +0100824 int strongest_window_nr;
825 int burst_start = 0;
826 int chan_imp_resp_center = 0;
827 float max_correlation = 0;
828 float energy = 0;
piotr5c820252014-04-17 09:43:02 +0200829
piotrd0bf1492014-02-05 17:27:32 +0100830 int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR);
piotr7c82b172014-02-08 14:15:27 +0100831 int search_start_pos = search_center + 1 - 5*d_OSR;
piotr437f5462014-02-04 17:57:25 +0100832 // int search_start_pos = search_center - d_chan_imp_length * d_OSR;
piotr5c820252014-04-17 09:43:02 +0200833 int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 5 * d_OSR;
piotr437f5462014-02-04 17:57:25 +0100834
ptrkrysik58213792014-10-30 09:05:15 +0100835 for(int ii = search_start_pos; ii < search_stop_pos; ii++)
piotrd0bf1492014-02-05 17:27:32 +0100836 {
piotr437f5462014-02-04 17:57:25 +0100837 gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]);
piotr437f5462014-02-04 17:57:25 +0100838 correlation_buffer.push_back(correlation);
839 power_buffer.push_back(std::pow(abs(correlation), 2));
piotrd0bf1492014-02-05 17:27:32 +0100840 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100841// plot(power_buffer);
piotrd0bf1492014-02-05 17:27:32 +0100842 //compute window energies
ptrkrysikef5e2db2015-01-03 12:10:14 +0100843 std::vector<float>::iterator iter = power_buffer.begin();
piotrd0bf1492014-02-05 17:27:32 +0100844 bool loop_end = false;
845 while (iter != power_buffer.end())
846 {
ptrkrysikef5e2db2015-01-03 12:10:14 +0100847 std::vector<float>::iterator iter_ii = iter;
piotr437f5462014-02-04 17:57:25 +0100848 energy = 0;
849
Piotr Krysik97d4f8a2016-02-11 08:40:44 +0100850 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++, iter_ii++)
piotrd0bf1492014-02-05 17:27:32 +0100851 {
piotrd0bf1492014-02-05 17:27:32 +0100852 if (iter_ii == power_buffer.end())
853 {
854 loop_end = true;
855 break;
856 }
857 energy += (*iter_ii);
piotr437f5462014-02-04 17:57:25 +0100858 }
piotrd0bf1492014-02-05 17:27:32 +0100859 if (loop_end)
860 {
861 break;
piotr437f5462014-02-04 17:57:25 +0100862 }
863 iter++;
864
865 window_energy_buffer.push_back(energy);
piotrd0bf1492014-02-05 17:27:32 +0100866 }
piotr437f5462014-02-04 17:57:25 +0100867
piotr5c820252014-04-17 09:43:02 +0200868 strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()-((d_chan_imp_length)*d_OSR)) - window_energy_buffer.begin();
869 //strongest_window_nr = strongest_window_nr-d_OSR;
870 if(strongest_window_nr<0){
871 strongest_window_nr = 0;
872 }
piotr6d152d92014-02-21 00:02:44 +0100873
piotrd0bf1492014-02-05 17:27:32 +0100874 max_correlation = 0;
875 for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++)
876 {
piotr437f5462014-02-04 17:57:25 +0100877 gr_complex correlation = correlation_buffer[strongest_window_nr + ii];
piotrd0bf1492014-02-05 17:27:32 +0100878 if (abs(correlation) > max_correlation)
879 {
880 chan_imp_resp_center = ii;
881 max_correlation = abs(correlation);
piotr437f5462014-02-04 17:57:25 +0100882 }
piotrd0bf1492014-02-05 17:27:32 +0100883 // d_channel_imp_resp.push_back(correlation);
piotr437f5462014-02-04 17:57:25 +0100884 chan_imp_resp[ii] = correlation;
piotr437f5462014-02-04 17:57:25 +0100885 }
ptrkrysike518bbf2014-11-06 14:50:59 +0100886
piotr7e3b0db2014-02-05 22:44:30 +0100887 *corr_max = max_correlation;
piotrd0bf1492014-02-05 17:27:32 +0100888
ptrkrysik58213792014-10-30 09:05:15 +0100889 //DCOUT("strongest_window_nr_new: " << strongest_window_nr);
piotrc7c249a2014-05-02 17:24:08 +0200890 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 +0100891
ptrkrysik58213792014-10-30 09:05:15 +0100892 //DCOUT("burst_start: " << burst_start);
piotrd0bf1492014-02-05 17:27:32 +0100893 return burst_start;
894}
piotr437f5462014-02-04 17:57:25 +0100895
896
ptrkrysik617ba032014-11-21 10:11:05 +0100897void 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 +0100898{
piotr6d152d92014-02-21 00:02:44 +0100899 boost::scoped_ptr<gsmtap_hdr> tap_header(new gsmtap_hdr());
ptrkrysik617ba032014-11-21 10:11:05 +0100900
piotr6d152d92014-02-21 00:02:44 +0100901 tap_header->version = GSMTAP_VERSION;
ptrkrysik7f61c642014-10-30 08:57:27 +0100902 tap_header->hdr_len = sizeof(gsmtap_hdr)/4;
piotr6d152d92014-02-21 00:02:44 +0100903 tap_header->type = GSMTAP_TYPE_UM_BURST;
ptrkrysik617ba032014-11-21 10:11:05 +0100904 tap_header->sub_type = burst_type;
ptrkrysik380dea82015-08-06 10:11:58 +0200905 bool uplink_burst = (input_nr >= d_cell_allocation.size());
906 if(!uplink_burst) // downlink burst
907 {
908 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr());
909 tap_header->frame_number = htobe32(d_burst_nr.get_frame_nr());
910 tap_header->arfcn = htobe16(d_cell_allocation[input_nr]) ;
911 }
912 else //uplink burst
913 {
914 tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.subtract_timeslots(3).get_timeslot_nr());
915 tap_header->frame_number = htobe32(d_burst_nr.subtract_timeslots(3).get_frame_nr());
916 input_nr = input_nr - d_cell_allocation.size();
917 tap_header->arfcn = htobe16(d_cell_allocation[input_nr] | 0x4000);
918 }
piotr6d152d92014-02-21 00:02:44 +0100919 tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm);
ptrkrysik6f6d46d2014-11-12 22:50:18 +0100920 tap_header->snr_db = 0;
ptrkrysik617ba032014-11-21 10:11:05 +0100921
922 int8_t header_plus_burst[sizeof(gsmtap_hdr)+BURST_SIZE];
923 memcpy(header_plus_burst, tap_header.get(), sizeof(gsmtap_hdr));
924 memcpy(header_plus_burst+sizeof(gsmtap_hdr), burst_binary, BURST_SIZE);
925
926 pmt::pmt_t blob_header_plus_burst = pmt::make_blob(header_plus_burst,sizeof(gsmtap_hdr)+BURST_SIZE);
927 pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob_header_plus_burst);
piotrf2b6a1b2014-08-04 11:28:59 +0200928
ptrkrysike518bbf2014-11-06 14:50:59 +0100929 if(input_nr==0){
930 message_port_pub(pmt::mp("C0"), msg);
931 } else {
932 message_port_pub(pmt::mp("CX"), msg);
933 }
piotrd0bf1492014-02-05 17:27:32 +0100934}
piotr6d152d92014-02-21 00:02:44 +0100935
piotrd0bf1492014-02-05 17:27:32 +0100936void receiver_impl::configure_receiver()
937{
piotrce92f982014-04-17 23:37:18 +0200938 d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51);
piotrd0bf1492014-02-05 17:27:32 +0100939 d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotr437f5462014-02-04 17:57:25 +0100940
piotrce92f982014-04-17 23:37:18 +0200941 d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal);
942 d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst);
943 d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst);
piotr437f5462014-02-04 17:57:25 +0100944
piotrd0bf1492014-02-05 17:27:32 +0100945 d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51);
946 d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
947 d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51);
948 d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
949 d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51);
950 d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
951 d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51);
952 d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
953 d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51);
954 d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
955 d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51);
956 d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
957 d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51);
958 d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal);
piotrd0bf1492014-02-05 17:27:32 +0100959}
piotr437f5462014-02-04 17:57:25 +0100960
ptrkrysik7a7b9b02014-11-19 11:27:34 +0100961void receiver_impl::set_cell_allocation(const std::vector<int> &cell_allocation)
piotrf2b6a1b2014-08-04 11:28:59 +0200962{
ptrkrysike518bbf2014-11-06 14:50:59 +0100963 d_cell_allocation = cell_allocation;
964}
965
966void receiver_impl::set_tseq_nums(const std::vector<int> & tseq_nums)
967{
968 d_tseq_nums = tseq_nums;
piotrf2b6a1b2014-08-04 11:28:59 +0200969}
970
971void receiver_impl::reset()
972{
piotrd6d66872014-08-06 15:20:33 +0200973 d_state = fcch_search;
piotrf2b6a1b2014-08-04 11:28:59 +0200974}
piotr437f5462014-02-04 17:57:25 +0100975
piotrd0bf1492014-02-05 17:27:32 +0100976} /* namespace gsm */
piotr437f5462014-02-04 17:57:25 +0100977} /* namespace gr */
978