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