piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 2 | /* |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 3 | * Copyright 2014 <+YOU OR YOUR COMPANY+>. |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 4 | * |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3, or (at your option) |
| 8 | * any later version. |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 9 | * |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 14 | * |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; see the file COPYING. If not, write to |
| 17 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include "config.h" |
| 23 | #endif |
| 24 | |
| 25 | #include <gnuradio/io_signature.h> |
| 26 | #include "receiver_impl.h" |
| 27 | |
| 28 | #include <gnuradio/io_signature.h> |
| 29 | #include <gnuradio/math.h> |
| 30 | #include <math.h> |
| 31 | #include <boost/circular_buffer.hpp> |
| 32 | #include <algorithm> |
| 33 | #include <numeric> |
| 34 | #include <viterbi_detector.h> |
| 35 | #include <string.h> |
| 36 | #include <sch.h> |
| 37 | #include <iostream> |
| 38 | #include <iomanip> |
| 39 | |
| 40 | #include <assert.h> |
| 41 | |
| 42 | #define SYNC_SEARCH_RANGE 30 |
| 43 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 44 | namespace gr |
| 45 | { |
| 46 | namespace gsm |
| 47 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 48 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 49 | typedef std::list<float> list_float; |
| 50 | typedef std::vector<float> vector_float; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 51 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 52 | typedef boost::circular_buffer<float> circular_buffer_float; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 53 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 54 | receiver::sptr |
| 55 | receiver::make(feval_dd * tuner, int osr) |
| 56 | { |
| 57 | return gnuradio::get_initial_sptr |
| 58 | (new receiver_impl(tuner, osr)); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * The private constructor |
| 63 | */ |
| 64 | receiver_impl::receiver_impl(feval_dd * tuner, int osr) |
| 65 | : gr::block("receiver", |
| 66 | gr::io_signature::make(1, 1, sizeof(gr_complex)), |
| 67 | gr::io_signature::make(0, 1, 142 * sizeof(float))), |
| 68 | d_OSR(osr), |
| 69 | d_chan_imp_length(CHAN_IMP_RESP_LENGTH), |
| 70 | d_tuner(tuner), |
| 71 | d_counter(0), |
| 72 | d_fcch_start_pos(0), |
| 73 | d_freq_offset(0), |
| 74 | d_state(first_fcch_search), |
| 75 | d_burst_nr(osr), |
| 76 | d_failed_sch(0) |
| 77 | { |
| 78 | int i; |
| 79 | gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0)); |
| 80 | for (i = 0; i < TRAIN_SEQ_NUM; i++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 81 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 82 | gr_complex startpoint; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 83 | if (i == 6 || i == 7) //this is nasty hack |
| 84 | { |
| 85 | startpoint = gr_complex(-1.0, 0.0); //if I don't change it here all bits of normal bursts for BTSes with bcc=6 will have reversed values |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | startpoint = gr_complex(1.0, 0.0); //I've checked this hack for bcc==0,1,2,3,4,6 |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 90 | } //I don't know what about bcc==5 and 7 yet |
| 91 | //TODO:find source of this situation - this is purely mathematical problem I guess |
| 92 | |
| 93 | gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 94 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 95 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 96 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 97 | /* |
| 98 | * Our virtual destructor. |
| 99 | */ |
| 100 | receiver_impl::~receiver_impl() |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | void receiver_impl::forecast(int noutput_items, gr_vector_int &ninput_items_required) |
| 105 | { |
| 106 | ninput_items_required[0] = noutput_items * floor((TS_BITS + 2 * GUARD_PERIOD) * d_OSR); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | int |
| 111 | receiver_impl::general_work(int noutput_items, |
| 112 | gr_vector_int &ninput_items, |
| 113 | gr_vector_const_void_star &input_items, |
| 114 | gr_vector_void_star &output_items) |
| 115 | { |
| 116 | const gr_complex *input = (const gr_complex *) input_items[0]; |
| 117 | //float *out = (float *) output_items[0]; |
| 118 | int produced_out = 0; //how many output elements were produced - this isn't used yet |
| 119 | //probably the gsm receiver will be changed into sink so this variable won't be necessary |
| 120 | switch (d_state) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 121 | { |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 122 | //bootstrapping |
| 123 | case first_fcch_search: |
| 124 | COUT("FCCH search"); |
| 125 | if (find_fcch_burst(input, ninput_items[0])) //find frequency correction burst in the input buffer |
| 126 | { |
piotr | 5f1e1d3 | 2014-02-05 18:10:05 +0100 | [diff] [blame^] | 127 | //set_frequency(d_freq_offset); //if fcch search is successful set frequency offset |
| 128 | COUT("Freq offset " << d_freq_offset); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 129 | //produced_out = 0; |
| 130 | d_state = next_fcch_search; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 131 | } |
| 132 | else |
| 133 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 134 | //produced_out = 0; |
| 135 | d_state = first_fcch_search; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 136 | } |
| 137 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 138 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 139 | case next_fcch_search: //this state is used because it takes some time (a bunch of buffered samples) |
| 140 | { |
| 141 | COUT("NEXT FCCH search"); |
| 142 | float prev_freq_offset = d_freq_offset; //before previous set_frequqency cause change |
| 143 | if (find_fcch_burst(input, ninput_items[0])) |
| 144 | { |
| 145 | if (abs(prev_freq_offset - d_freq_offset) > FCCH_MAX_FREQ_OFFSET) |
| 146 | { |
piotr | 5f1e1d3 | 2014-02-05 18:10:05 +0100 | [diff] [blame^] | 147 | //set_frequency(d_freq_offset); //call set_frequncy only frequency offset change is greater than some value |
| 148 | COUT("Freq offset " << d_freq_offset); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 149 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 150 | //produced_out = 0; |
| 151 | d_state = sch_search; |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | //produced_out = 0; |
| 156 | d_state = next_fcch_search; |
| 157 | } |
| 158 | break; |
| 159 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 160 | |
| 161 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 162 | case sch_search: |
| 163 | { |
| 164 | DCOUT("SCH search") ; |
| 165 | vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR); |
| 166 | int t1, t2, t3; |
| 167 | int burst_start = 0; |
| 168 | unsigned char output_binary[BURST_SIZE]; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 169 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 170 | if (reach_sch_burst(ninput_items[0])) //wait for a SCH burst |
| 171 | { |
| 172 | burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it |
| 173 | detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection |
| 174 | if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst |
| 175 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 176 | COUT("sch burst_start: " << burst_start); |
| 177 | COUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3); |
| 178 | d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value |
| 179 | |
| 180 | //configure the receiver - tell him where to find which burst type |
| 181 | d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51); //in the timeslot nr.0 bursts changes according to t3 counter |
| 182 | configure_receiver();//TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready |
| 183 | d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst); //tell where to find fcch bursts |
| 184 | d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst); //sch bursts |
| 185 | d_channel_conf.set_burst_types(TIMESLOT0, BCCH_FRAMES, sizeof(BCCH_FRAMES) / sizeof(unsigned), normal_burst);//!and maybe normal bursts of the BCCH logical channel |
| 186 | d_burst_nr++; |
| 187 | |
| 188 | consume_each(burst_start + BURST_SIZE * d_OSR); //consume samples up to next guard period |
| 189 | d_state = synchronized; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 190 | } |
| 191 | else |
| 192 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 193 | d_state = next_fcch_search; //if there is error in the sch burst go back to fcch search phase |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 194 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 195 | } |
| 196 | else |
| 197 | { |
| 198 | d_state = sch_search; |
| 199 | } |
| 200 | break; |
| 201 | } |
| 202 | //in this state receiver is synchronized and it processes bursts according to burst type for given burst number |
| 203 | case synchronized: |
| 204 | { |
| 205 | DCOUT("Synchronized") ; |
| 206 | vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR); |
| 207 | int burst_start; |
| 208 | int offset = 0; |
| 209 | int to_consume = 0; |
| 210 | unsigned char output_binary[BURST_SIZE]; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 211 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 212 | burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 213 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 214 | switch (b_type) |
| 215 | { |
| 216 | case fcch_burst: //if it's FCCH burst |
| 217 | { |
| 218 | const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1; |
| 219 | const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR; |
| 220 | double freq_offset = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 221 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 222 | d_freq_offset_vals.push_front(freq_offset); |
| 223 | process_normal_burst(d_burst_nr, fc_fb); |
| 224 | if (d_freq_offset_vals.size() >= 10) |
| 225 | { |
| 226 | double sum = std::accumulate(d_freq_offset_vals.begin(), d_freq_offset_vals.end(), 0); |
| 227 | double mean_offset = sum / d_freq_offset_vals.size(); //compute mean |
| 228 | d_freq_offset_vals.clear(); |
| 229 | if (abs(mean_offset) > FCCH_MAX_FREQ_OFFSET) |
| 230 | { |
| 231 | d_freq_offset -= mean_offset; //and adjust frequency if it have changed beyond |
piotr | 5f1e1d3 | 2014-02-05 18:10:05 +0100 | [diff] [blame^] | 232 | //set_frequency(d_freq_offset); //some limit |
| 233 | COUT("mean_offset: " << mean_offset); |
| 234 | COUT("Adjusting frequency, new frequency offset: " << d_freq_offset << "\n"); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |
| 238 | break; |
| 239 | case sch_burst: //if it's SCH burst |
| 240 | { |
| 241 | int t1, t2, t3, d_ncc, d_bcc; |
| 242 | burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response |
| 243 | detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits |
| 244 | process_normal_burst(d_burst_nr, output_binary); |
| 245 | if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data |
| 246 | { |
| 247 | // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct |
| 248 | d_failed_sch = 0; |
| 249 | DCOUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3); |
| 250 | offset = burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period |
| 251 | DCOUT(offset); |
| 252 | to_consume += offset; //adjust with offset number of samples to be consumed |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | d_failed_sch++; |
| 257 | if (d_failed_sch >= MAX_SCH_ERRORS) |
| 258 | { |
| 259 | d_state = first_fcch_search; //TODO: this isn't good, the receiver is going wild when it goes back to next_fcch_search from here |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 260 | d_freq_offset_vals.clear(); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 261 | d_freq_offset=0; |
| 262 | set_frequency(0); |
| 263 | DCOUT("many sch decoding errors"); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 264 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 265 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 266 | } |
| 267 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 268 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 269 | case normal_burst: //if it's normal burst |
| 270 | burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], d_bcc); //get channel impulse response for given training sequence number - d_bcc |
| 271 | detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits |
| 272 | process_normal_burst(d_burst_nr, output_binary); //TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready |
| 273 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 274 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 275 | case dummy_or_normal: |
| 276 | { |
| 277 | burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], TS_DUMMY); |
| 278 | detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 279 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 280 | std::vector<unsigned char> v(20); |
| 281 | std::vector<unsigned char>::iterator it; |
| 282 | it = std::set_difference(output_binary + TRAIN_POS, output_binary + TRAIN_POS + 16, &train_seq[TS_DUMMY][5], &train_seq[TS_DUMMY][21], v.begin()); |
| 283 | int different_bits = (it - v.begin()); |
| 284 | |
| 285 | if (different_bits > 2) |
| 286 | { |
| 287 | burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], d_bcc); |
| 288 | detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); |
| 289 | //if (!output_binary[0] && !output_binary[1] && !output_binary[2]) { |
| 290 | // COUT("Normal burst"); |
| 291 | process_normal_burst(d_burst_nr, output_binary); //TODO: this shouldn't be here - remove it when gsm receiver's interface will be ready |
| 292 | //} |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | process_normal_burst(d_burst_nr, dummy_burst); |
| 297 | } |
| 298 | } |
| 299 | case rach_burst: |
| 300 | //implementation of this channel isn't possible in current gsm_receiver |
| 301 | //it would take some realtime processing, counter of samples from USRP to |
| 302 | //stay synchronized with this device and possibility to switch frequency from uplink |
| 303 | //to C0 (where sch is) back and forth |
| 304 | |
| 305 | break; |
| 306 | case dummy: //if it's dummy |
| 307 | burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], TS_DUMMY); //read dummy |
| 308 | detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); // but as far as I know it's pointless |
| 309 | break; |
| 310 | case empty: //if it's empty burst |
| 311 | break; //do nothing |
| 312 | } |
| 313 | |
| 314 | d_burst_nr++; //go to next burst |
| 315 | |
| 316 | to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); //consume samples of the burst up to next guard period |
| 317 | //and add offset which is introduced by |
| 318 | //0.25 fractional part of a guard period |
| 319 | //burst_number computes this offset |
| 320 | //but choice of this class to do this was random |
| 321 | consume_each(to_consume); |
| 322 | } |
| 323 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 324 | } |
| 325 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 326 | return produced_out; |
| 327 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 328 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 329 | |
| 330 | bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems) |
| 331 | { |
| 332 | circular_buffer_float phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR); //circular buffer used to scan throug signal to find |
| 333 | //best match for FCCH burst |
| 334 | float phase_diff = 0; |
| 335 | gr_complex conjprod; |
| 336 | int start_pos = -1; |
| 337 | int hit_count = 0; |
| 338 | int miss_count = 0; |
| 339 | float min_phase_diff; |
| 340 | float max_phase_diff; |
| 341 | double best_sum = 0; |
| 342 | float lowest_max_min_diff = 99999; |
| 343 | |
| 344 | int to_consume = 0; |
| 345 | int sample_number = 0; |
| 346 | bool end = false; |
| 347 | bool result = false; |
| 348 | circular_buffer_float::iterator buffer_iter; |
| 349 | |
| 350 | /**@name Possible states of FCCH search algorithm*/ |
| 351 | //@{ |
| 352 | enum states |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 353 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 354 | init, ///< initialize variables |
| 355 | search, ///< search for positive samples |
| 356 | found_something, ///< search for FCCH and the best position of it |
| 357 | fcch_found, ///< when FCCH was found |
| 358 | search_fail ///< when there is no FCCH in the input vector |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 359 | } fcch_search_state; |
| 360 | //@} |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 361 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 362 | fcch_search_state = init; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 363 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 364 | while (!end) |
| 365 | { |
| 366 | switch (fcch_search_state) |
| 367 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 368 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 369 | case init: //initialize variables |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 370 | hit_count = 0; |
| 371 | miss_count = 0; |
| 372 | start_pos = -1; |
| 373 | lowest_max_min_diff = 99999; |
| 374 | phase_diff_buffer.clear(); |
| 375 | fcch_search_state = search; |
| 376 | |
| 377 | break; |
| 378 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 379 | case search: // search for positive samples |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 380 | sample_number++; |
| 381 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 382 | if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because |
| 383 | { |
| 384 | //there's too few samples left to look into, |
| 385 | to_consume = sample_number; //don't do anything with those samples which are left |
| 386 | //and consume only those which were checked |
| 387 | fcch_search_state = search_fail; |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 392 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 393 | if (phase_diff > 0) //if a positive phase difference was found |
| 394 | { |
| 395 | to_consume = sample_number; |
| 396 | fcch_search_state = found_something; //switch to state in which searches for FCCH |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | fcch_search_state = search; |
| 401 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | break; |
| 405 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 406 | case found_something: // search for FCCH and the best position of it |
| 407 | { |
| 408 | if (phase_diff > 0) |
| 409 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 410 | hit_count++; //positive phase differencies increases hits_count |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 411 | } |
| 412 | else |
| 413 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 414 | miss_count++; //negative increases miss_count |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 415 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 416 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 417 | if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR)) |
| 418 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 419 | //if miss_count exceeds limit before hit_count |
| 420 | fcch_search_state = init; //go to init |
| 421 | continue; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 422 | } |
| 423 | else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR)) |
| 424 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 425 | //if hit_count and miss_count exceeds limit then FCCH was found |
| 426 | fcch_search_state = fcch_found; |
| 427 | continue; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 428 | } |
| 429 | else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) |
| 430 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 431 | //find difference between minimal and maximal element in the buffer |
| 432 | //for FCCH this value should be low |
| 433 | //this part is searching for a region where this value is lowest |
| 434 | min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end())); |
| 435 | max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end())); |
| 436 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 437 | if (lowest_max_min_diff > max_phase_diff - min_phase_diff) |
| 438 | { |
| 439 | lowest_max_min_diff = max_phase_diff - min_phase_diff; |
| 440 | start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos |
| 441 | best_sum = 0; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 442 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 443 | for (buffer_iter = phase_diff_buffer.begin(); |
| 444 | buffer_iter != (phase_diff_buffer.end()); |
| 445 | buffer_iter++) |
| 446 | { |
| 447 | best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum |
| 448 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 449 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 450 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 451 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 452 | sample_number++; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 453 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 454 | if (sample_number >= nitems) //if there's no single sample left to check |
| 455 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 456 | fcch_search_state = search_fail;//FCCH search failed |
| 457 | continue; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 458 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 459 | |
| 460 | phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]); |
| 461 | phase_diff_buffer.push_back(phase_diff); |
| 462 | fcch_search_state = found_something; |
| 463 | } |
| 464 | break; |
| 465 | |
| 466 | case fcch_found: |
| 467 | { |
| 468 | DCOUT("fcch found on position: " << d_counter + start_pos); |
| 469 | to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst |
| 470 | |
| 471 | d_fcch_start_pos = d_counter + start_pos; |
| 472 | |
| 473 | //compute frequency offset |
| 474 | double phase_offset = best_sum / FCCH_HITS_NEEDED; |
| 475 | double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI); |
| 476 | d_freq_offset -= freq_offset; |
| 477 | DCOUT("freq_offset: " << d_freq_offset); |
| 478 | |
| 479 | end = true; |
| 480 | result = true; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 481 | break; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 482 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 483 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 484 | case search_fail: |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 485 | end = true; |
| 486 | result = false; |
| 487 | break; |
| 488 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 489 | } |
| 490 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 491 | d_counter += to_consume; |
| 492 | consume_each(to_consume); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 493 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 494 | return result; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample) |
| 499 | { |
| 500 | double phase_sum = 0; |
| 501 | unsigned ii; |
| 502 | |
| 503 | for (ii = first_sample; ii < last_sample; ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 504 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 505 | double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR; |
| 506 | phase_sum += phase_diff; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 507 | } |
| 508 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 509 | double phase_offset = phase_sum / (last_sample - first_sample); |
| 510 | double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI); |
| 511 | return freq_offset; |
| 512 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 513 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 514 | void receiver_impl::set_frequency(double freq_offset) |
| 515 | { |
| 516 | d_tuner->calleval(freq_offset); |
| 517 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 518 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 519 | inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2) |
| 520 | { |
| 521 | gr_complex conjprod = val1 * conj(val2); |
| 522 | return fast_atan2f(imag(conjprod), real(conjprod)); |
| 523 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 524 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 525 | bool receiver_impl::reach_sch_burst(const int nitems) |
| 526 | { |
| 527 | //it just consumes samples to get near to a SCH burst |
| 528 | int to_consume = 0; |
| 529 | bool result = false; |
| 530 | unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR; |
| 531 | |
| 532 | //consume samples until d_counter will be equal to sample_nr_near_sch_start |
| 533 | if (d_counter < sample_nr_near_sch_start) |
| 534 | { |
| 535 | if (d_counter + nitems >= sample_nr_near_sch_start) |
| 536 | { |
| 537 | to_consume = sample_nr_near_sch_start - d_counter; |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | to_consume = nitems; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 542 | } |
| 543 | result = false; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 544 | } |
| 545 | else |
| 546 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 547 | to_consume = 0; |
| 548 | result = true; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 549 | } |
| 550 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 551 | d_counter += to_consume; |
| 552 | consume_each(to_consume); |
| 553 | return result; |
| 554 | } |
| 555 | |
| 556 | int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp) |
| 557 | { |
| 558 | vector_complex correlation_buffer; |
| 559 | vector_float power_buffer; |
| 560 | vector_float window_energy_buffer; |
| 561 | |
| 562 | int strongest_window_nr; |
| 563 | int burst_start = 0; |
| 564 | int chan_imp_resp_center = 0; |
| 565 | float max_correlation = 0; |
| 566 | float energy = 0; |
| 567 | |
| 568 | for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 569 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 570 | gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]); |
| 571 | correlation_buffer.push_back(correlation); |
| 572 | power_buffer.push_back(std::pow(abs(correlation), 2)); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 573 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 574 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 575 | //compute window energies |
| 576 | vector_float::iterator iter = power_buffer.begin(); |
| 577 | bool loop_end = false; |
| 578 | while (iter != power_buffer.end()) |
| 579 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 580 | vector_float::iterator iter_ii = iter; |
| 581 | energy = 0; |
| 582 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 583 | for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++) |
| 584 | { |
| 585 | if (iter_ii == power_buffer.end()) |
| 586 | { |
| 587 | loop_end = true; |
| 588 | break; |
| 589 | } |
| 590 | energy += (*iter_ii); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 591 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 592 | if (loop_end) |
| 593 | { |
| 594 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 595 | } |
| 596 | iter++; |
| 597 | window_energy_buffer.push_back(energy); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 598 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 599 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 600 | strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin(); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 601 | // d_channel_imp_resp.clear(); |
| 602 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 603 | max_correlation = 0; |
| 604 | for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++) |
| 605 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 606 | gr_complex correlation = correlation_buffer[strongest_window_nr + ii]; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 607 | if (abs(correlation) > max_correlation) |
| 608 | { |
| 609 | chan_imp_resp_center = ii; |
| 610 | max_correlation = abs(correlation); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 611 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 612 | // d_channel_imp_resp.push_back(correlation); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 613 | chan_imp_resp[ii] = correlation; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 614 | } |
| 615 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 616 | burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR; |
| 617 | return burst_start; |
| 618 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 619 | |
| 620 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 621 | |
| 622 | void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary) |
| 623 | { |
| 624 | float output[BURST_SIZE]; |
| 625 | gr_complex rhh_temp[CHAN_IMP_RESP_LENGTH*d_OSR]; |
| 626 | gr_complex rhh[CHAN_IMP_RESP_LENGTH]; |
| 627 | gr_complex filtered_burst[BURST_SIZE]; |
| 628 | int start_state = 3; |
| 629 | unsigned int stop_states[2] = {4, 12}; |
| 630 | |
| 631 | autocorrelation(chan_imp_resp, rhh_temp, d_chan_imp_length*d_OSR); |
| 632 | for (int ii = 0; ii < (d_chan_imp_length); ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 633 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 634 | rhh[ii] = conj(rhh_temp[ii*d_OSR]); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 635 | } |
| 636 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 637 | mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst); |
| 638 | |
| 639 | viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output); |
| 640 | |
| 641 | for (int i = 0; i < BURST_SIZE ; i++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 642 | { |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 643 | output_binary[i] = (output[i] > 0); |
| 644 | } |
| 645 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 646 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 647 | //TODO consider placing this funtion in a separate class for signal processing |
| 648 | void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point) |
| 649 | { |
| 650 | gr_complex j = gr_complex(0.0, 1.0); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 651 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 652 | int current_symbol; |
| 653 | int encoded_symbol; |
| 654 | int previous_symbol = 2 * input[0] - 1; |
| 655 | gmsk_output[0] = start_point; |
| 656 | |
| 657 | for (int i = 1; i < nitems; i++) |
| 658 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 659 | //change bits representation to NRZ |
| 660 | current_symbol = 2 * input[i] - 1; |
| 661 | //differentially encode |
| 662 | encoded_symbol = current_symbol * previous_symbol; |
| 663 | //and do gmsk mapping |
| 664 | gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1]; |
| 665 | previous_symbol = current_symbol; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 666 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 667 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 668 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 669 | //TODO consider use of some generalized function for correlation and placing it in a separate class for signal processing |
| 670 | gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input) |
| 671 | { |
| 672 | gr_complex result(0.0, 0.0); |
| 673 | int sample_number = 0; |
| 674 | |
| 675 | for (int ii = 0; ii < length; ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 676 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 677 | sample_number = (ii * d_OSR) ; |
| 678 | result += sequence[ii] * conj(input[sample_number]); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 679 | } |
| 680 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 681 | result = result / gr_complex(length, 0); |
| 682 | return result; |
| 683 | } |
| 684 | |
| 685 | //computes autocorrelation for positive arguments |
| 686 | //TODO consider placing this funtion in a separate class for signal processing |
| 687 | inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems) |
| 688 | { |
| 689 | int i, k; |
| 690 | for (k = nitems - 1; k >= 0; k--) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 691 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 692 | out[k] = gr_complex(0, 0); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 693 | for (i = k; i < nitems; i++) |
| 694 | { |
| 695 | out[k] += input[i] * conj(input[i-k]); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 696 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 697 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 698 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 699 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 700 | //TODO consider use of some generalized function for filtering and placing it in a separate class for signal processing |
| 701 | inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output) |
| 702 | { |
| 703 | int ii = 0, n, a; |
| 704 | |
| 705 | for (n = 0; n < nitems; n++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 706 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 707 | a = n * d_OSR; |
| 708 | output[n] = 0; |
| 709 | ii = 0; |
| 710 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 711 | while (ii < filter_length) |
| 712 | { |
| 713 | if ((a + ii) >= nitems*d_OSR) |
| 714 | break; |
| 715 | output[n] += input[a+ii] * filter[ii]; |
| 716 | ii++; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 717 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 718 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 719 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 720 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 721 | //TODO: get_norm_chan_imp_resp is similar to get_sch_chan_imp_resp - consider joining this two functions |
| 722 | //TODO: this is place where most errors are introduced and can be corrected by improvements to this fuction |
| 723 | //especially computations of strongest_window_nr |
| 724 | int receiver_impl::get_norm_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp, int bcc) |
| 725 | { |
| 726 | vector_complex correlation_buffer; |
| 727 | vector_float power_buffer; |
| 728 | vector_float window_energy_buffer; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 729 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 730 | int strongest_window_nr; |
| 731 | int burst_start = 0; |
| 732 | int chan_imp_resp_center = 0; |
| 733 | float max_correlation = 0; |
| 734 | float energy = 0; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 735 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 736 | int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR); |
| 737 | int search_start_pos = search_center + 1; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 738 | // int search_start_pos = search_center - d_chan_imp_length * d_OSR; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 739 | int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 2 * d_OSR; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 740 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 741 | for (int ii = search_start_pos; ii < search_stop_pos; ii++) |
| 742 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 743 | gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]); |
| 744 | |
| 745 | correlation_buffer.push_back(correlation); |
| 746 | power_buffer.push_back(std::pow(abs(correlation), 2)); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 747 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 748 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 749 | //compute window energies |
| 750 | vector_float::iterator iter = power_buffer.begin(); |
| 751 | bool loop_end = false; |
| 752 | while (iter != power_buffer.end()) |
| 753 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 754 | vector_float::iterator iter_ii = iter; |
| 755 | energy = 0; |
| 756 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 757 | for (int ii = 0; ii < (d_chan_imp_length - 2)*d_OSR; ii++, iter_ii++) |
| 758 | { |
| 759 | // for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++, iter_ii++) { |
| 760 | if (iter_ii == power_buffer.end()) |
| 761 | { |
| 762 | loop_end = true; |
| 763 | break; |
| 764 | } |
| 765 | energy += (*iter_ii); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 766 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 767 | if (loop_end) |
| 768 | { |
| 769 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 770 | } |
| 771 | iter++; |
| 772 | |
| 773 | window_energy_buffer.push_back(energy); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 774 | } |
| 775 | //!why doesn't this work |
| 776 | int strongest_window_nr_new = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin(); |
| 777 | strongest_window_nr = 3; //! so I have to override it here |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 778 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 779 | max_correlation = 0; |
| 780 | for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++) |
| 781 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 782 | gr_complex correlation = correlation_buffer[strongest_window_nr + ii]; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 783 | if (abs(correlation) > max_correlation) |
| 784 | { |
| 785 | chan_imp_resp_center = ii; |
| 786 | max_correlation = abs(correlation); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 787 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 788 | // d_channel_imp_resp.push_back(correlation); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 789 | chan_imp_resp[ii] = correlation; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 790 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 791 | // We want to use the first sample of the impulseresponse, and the |
| 792 | // corresponding samples of the received signal. |
| 793 | // the variable sync_w should contain the beginning of the used part of |
| 794 | // training sequence, which is 3+57+1+6=67 bits into the burst. That is |
| 795 | // we have that sync_t16 equals first sample in bit number 67. |
| 796 | |
| 797 | burst_start = search_start_pos + chan_imp_resp_center + strongest_window_nr - TRAIN_POS * d_OSR; |
| 798 | |
| 799 | // GMSK modulator introduces ISI - each bit is expanded for 3*Tb |
| 800 | // and it's maximum value is in the last bit period, so burst starts |
| 801 | // 2*Tb earlier |
| 802 | burst_start -= 2 * d_OSR; |
| 803 | burst_start += 2; |
| 804 | //COUT("Poczatek ###############################"); |
| 805 | //std::cout << " burst_start: " << burst_start << " center: " << ((float)(search_start_pos + strongest_window_nr + chan_imp_resp_center)) / d_OSR << " stronegest window nr: " << strongest_window_nr << "\n"; |
| 806 | //COUT("burst_start_new: " << (search_start_pos + strongest_window_nr_new - TRAIN_POS * d_OSR)); |
| 807 | burst_start=(search_start_pos + strongest_window_nr_new - TRAIN_POS * d_OSR); |
| 808 | return burst_start; |
| 809 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 810 | |
| 811 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 812 | void receiver_impl::process_normal_burst(burst_counter burst_nr, const unsigned char * burst_binary) |
| 813 | { |
| 814 | int ii; |
| 815 | //std::cout << "fn:" <<burst_nr.get_frame_nr() << " ts" << burst_nr.get_timeslot_nr() << " "; |
| 816 | for(ii=0; ii<148; ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 817 | { |
piotr | 5f1e1d3 | 2014-02-05 18:10:05 +0100 | [diff] [blame^] | 818 | std::cout << std::setprecision(1) << static_cast<int>(burst_binary[ii]) << ""; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 819 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 820 | std::cout << std::endl; |
| 821 | } |
| 822 | //TODO: this shouldn't be here also - the same reason |
| 823 | void receiver_impl::configure_receiver() |
| 824 | { |
| 825 | d_channel_conf.set_multiframe_type(TSC0, multiframe_51); |
| 826 | d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 827 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 828 | d_channel_conf.set_burst_types(TSC0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal); |
| 829 | d_channel_conf.set_burst_types(TSC0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 830 | |
| 831 | // d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_26); |
| 832 | // d_channel_conf.set_burst_types(TIMESLOT1, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 833 | // d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_26); |
| 834 | // d_channel_conf.set_burst_types(TIMESLOT2, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 835 | // d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_26); |
| 836 | // d_channel_conf.set_burst_types(TIMESLOT3, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 837 | // d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_26); |
| 838 | // d_channel_conf.set_burst_types(TIMESLOT4, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 839 | // d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_26); |
| 840 | // d_channel_conf.set_burst_types(TIMESLOT5, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 841 | // d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_26); |
| 842 | // d_channel_conf.set_burst_types(TIMESLOT6, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 843 | // d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_26); |
| 844 | // d_channel_conf.set_burst_types(TIMESLOT7, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 845 | d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51); |
| 846 | d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 847 | d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51); |
| 848 | d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 849 | d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51); |
| 850 | d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 851 | d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51); |
| 852 | d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 853 | d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51); |
| 854 | d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 855 | d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51); |
| 856 | d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 857 | d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51); |
| 858 | d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 859 | |
| 860 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 861 | |
| 862 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 863 | } /* namespace gsm */ |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 864 | } /* namespace gr */ |
| 865 | |