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