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