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 | /* |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 3 | * @file |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 4 | * @author (C) 2009-2017 by Piotr Krysik <ptrkrysik@gmail.com> |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 5 | * @section LICENSE |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 6 | * |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 7 | * Gr-gsm is free software; you can redistribute it and/or modify |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3, or (at your option) |
| 10 | * any later version. |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 11 | * |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 12 | * Gr-gsm is distributed in the hope that it will be useful, |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 16 | * |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 18 | * along with gr-gsm; see the file COPYING. If not, write to |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 19 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | * Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include "config.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <gnuradio/io_signature.h> |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 28 | #include <gnuradio/math.h> |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 29 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 30 | #include <algorithm> |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 31 | #include <string.h> |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 32 | #include <iostream> |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 33 | #include <numeric> |
| 34 | #include <math.h> |
| 35 | #include <vector> |
ptrkrysik | 3be74a7 | 2014-12-13 10:11:00 +0100 | [diff] [blame] | 36 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 37 | #include <boost/circular_buffer.hpp> |
| 38 | #include <boost/scoped_ptr.hpp> |
ptrkrysik | 3be74a7 | 2014-12-13 10:11:00 +0100 | [diff] [blame] | 39 | #include <grgsm/endian.h> |
ptrkrysik | 5821379 | 2014-10-30 09:05:15 +0100 | [diff] [blame] | 40 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 41 | #include "receiver_impl.h" |
| 42 | #include "viterbi_detector.h" |
| 43 | #include "sch.h" |
| 44 | |
| 45 | #if 0 |
| 46 | /* Files included for debuging */ |
| 47 | #include "plotting/plotting.hpp" |
| 48 | #include <pthread.h> |
| 49 | #include <iomanip> |
| 50 | #endif |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 51 | |
| 52 | #define SYNC_SEARCH_RANGE 30 |
| 53 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 54 | namespace gr |
| 55 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 56 | namespace gsm |
| 57 | { |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 58 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 59 | /* The public constructor */ |
| 60 | receiver::sptr |
| 61 | receiver::make( |
| 62 | int osr, const std::vector<int> &cell_allocation, |
| 63 | const std::vector<int> &tseq_nums, bool process_uplink) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 64 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 65 | return gnuradio::get_initial_sptr |
| 66 | (new receiver_impl(osr, cell_allocation, |
| 67 | tseq_nums, process_uplink)); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 70 | /* The private constructor */ |
| 71 | receiver_impl::receiver_impl( |
| 72 | int osr, const std::vector<int> &cell_allocation, |
| 73 | const std::vector<int> &tseq_nums, bool process_uplink |
| 74 | ) : gr::sync_block("receiver", |
| 75 | gr::io_signature::make(1, -1, sizeof(gr_complex)), |
| 76 | gr::io_signature::make(0, 0, 0)), |
| 77 | d_OSR(osr), |
| 78 | d_process_uplink(process_uplink), |
| 79 | d_chan_imp_length(CHAN_IMP_RESP_LENGTH), |
| 80 | d_counter(0), |
| 81 | d_fcch_start_pos(0), |
| 82 | d_freq_offset_setting(0), |
| 83 | d_state(fcch_search), |
| 84 | d_burst_nr(osr), |
| 85 | d_failed_sch(0), |
| 86 | d_signal_dbm(-120), |
| 87 | d_tseq_nums(tseq_nums), |
| 88 | d_cell_allocation(cell_allocation), |
| 89 | d_last_time(0.0) |
ptrkrysik | 32c2116 | 2015-04-04 14:01:52 +0200 | [diff] [blame] | 90 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 91 | /** |
| 92 | * Don't send samples to the receiver |
| 93 | * until there are at least samples for one |
| 94 | */ |
| 95 | set_output_multiple(floor((TS_BITS + 2 * GUARD_PERIOD) * d_OSR)); |
| 96 | |
| 97 | /** |
| 98 | * Prepare SCH sequence bits |
| 99 | * |
| 100 | * (TS_BITS + 2 * GUARD_PERIOD) |
| 101 | * Burst and two guard periods |
| 102 | * (one guard period is an arbitrary overlap) |
| 103 | */ |
| 104 | gmsk_mapper(SYNC_BITS, N_SYNC_BITS, |
| 105 | d_sch_training_seq, gr_complex(0.0, -1.0)); |
| 106 | |
| 107 | /* Prepare bits of training sequences */ |
| 108 | for (int i = 0; i < TRAIN_SEQ_NUM; i++) { |
| 109 | /** |
| 110 | * If first bit of the sequence is 0 |
| 111 | * => first symbol is 1, else -1 |
| 112 | */ |
| 113 | gr_complex startpoint = train_seq[i][0] == 0 ? |
| 114 | gr_complex(1.0, 0.0) : gr_complex(-1.0, 0.0); |
| 115 | gmsk_mapper(train_seq[i], N_TRAIN_BITS, |
| 116 | d_norm_training_seq[i], startpoint); |
| 117 | } |
| 118 | |
| 119 | /* Register output ports */ |
| 120 | message_port_register_out(pmt::mp("C0")); |
| 121 | message_port_register_out(pmt::mp("CX")); |
| 122 | message_port_register_out(pmt::mp("measurements")); |
| 123 | |
| 124 | /** |
| 125 | * Configure the receiver, |
| 126 | * i.e. tell it where to find which burst type |
| 127 | */ |
| 128 | configure_receiver(); |
| 129 | } |
| 130 | |
| 131 | /* Our virtual destructor */ |
| 132 | receiver_impl::~receiver_impl() {} |
| 133 | |
| 134 | int |
| 135 | receiver_impl::work( |
| 136 | int noutput_items, |
| 137 | gr_vector_const_void_star &input_items, |
| 138 | gr_vector_void_star &output_items) |
| 139 | { |
| 140 | gr_complex *input = (gr_complex *) input_items[0]; |
| 141 | uint64_t start = nitems_read(0); |
| 142 | uint64_t stop = start + noutput_items; |
| 143 | d_freq_offset_tag_in_fcch = false; |
| 144 | |
| 145 | #if 0 |
| 146 | /* FIXME: jak zrobić to rzutowanie poprawnie */ |
| 147 | std::vector<const gr_complex *> iii = |
| 148 | (std::vector<const gr_complex *>) input_items; |
| 149 | #endif |
| 150 | |
| 151 | /* Time synchronization loop */ |
| 152 | float current_time = |
| 153 | static_cast<float>(start / (GSM_SYMBOL_RATE * d_OSR)); |
| 154 | if ((current_time - d_last_time) > 0.1) { |
| 155 | pmt::pmt_t msg = pmt::make_tuple(pmt::mp("current_time"), |
| 156 | pmt::from_double(current_time)); |
ptrkrysik | 32c2116 | 2015-04-04 14:01:52 +0200 | [diff] [blame] | 157 | message_port_pub(pmt::mp("measurements"), msg); |
| 158 | d_last_time = current_time; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 159 | } |
ptrkrysik | 32c2116 | 2015-04-04 14:01:52 +0200 | [diff] [blame] | 160 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 161 | /* Frequency correction loop */ |
| 162 | std::vector<tag_t> freq_offset_tags; |
| 163 | pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset"); |
| 164 | get_tags_in_range(freq_offset_tags, 0, start, stop, key); |
| 165 | |
| 166 | if (!freq_offset_tags.empty()) { |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 167 | tag_t freq_offset_tag = freq_offset_tags[0]; |
Piotr Krysik | 43af70d | 2016-07-20 21:37:24 +0200 | [diff] [blame] | 168 | uint64_t tag_offset = freq_offset_tag.offset - start; |
Piotr Krysik | 43af70d | 2016-07-20 21:37:24 +0200 | [diff] [blame] | 169 | d_freq_offset_setting = pmt::to_double(freq_offset_tag.value); |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 170 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 171 | burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr); |
| 172 | if (d_state == synchronized && b_type == fcch_burst){ |
| 173 | uint64_t last_sample_nr = |
| 174 | ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1; |
| 175 | d_freq_offset_tag_in_fcch = tag_offset < last_sample_nr; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 176 | } |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Main state machine */ |
| 180 | switch (d_state) { |
| 181 | case fcch_search: |
| 182 | fcch_search_handler(input, noutput_items); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 183 | break; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 184 | case sch_search: |
| 185 | sch_search_handler(input, noutput_items); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 186 | break; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 187 | case synchronized: |
| 188 | synchronized_handler(input, input_items, noutput_items); |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | return 0; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 193 | } |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 194 | |
| 195 | void |
| 196 | receiver_impl::fcch_search_handler(gr_complex *input, int noutput_items) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 197 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 198 | double freq_offset_tmp; |
ptrkrysik | 5821379 | 2014-10-30 09:05:15 +0100 | [diff] [blame] | 199 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 200 | /* Check if received samples is a FCCN burst */ |
| 201 | if (!find_fcch_burst(input, noutput_items, freq_offset_tmp)) |
| 202 | return; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 203 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 204 | /* We found it, compose a message */ |
| 205 | pmt::pmt_t msg = pmt::make_tuple( |
| 206 | pmt::mp("freq_offset"), |
| 207 | pmt::from_double(freq_offset_tmp - d_freq_offset_setting), |
| 208 | pmt::mp("fcch_search") |
| 209 | ); |
Piotr Krysik | 9bc0fc0 | 2017-01-18 21:53:17 +0100 | [diff] [blame] | 210 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 211 | /* Notify FCCH loop */ |
| 212 | message_port_pub(pmt::mp("measurements"), msg); |
| 213 | |
| 214 | /* Update current state */ |
| 215 | d_state = sch_search; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 216 | } |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 217 | |
| 218 | void |
| 219 | receiver_impl::sch_search_handler(gr_complex *input, int noutput_items) |
| 220 | { |
| 221 | std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH * d_OSR); |
| 222 | unsigned char burst_buf[BURST_SIZE]; |
| 223 | int rc, t1, t2, t3; |
| 224 | int burst_start; |
| 225 | |
| 226 | /* Wait until we get a SCH burst */ |
| 227 | if (!reach_sch_burst(noutput_items)) |
| 228 | return; |
| 229 | |
| 230 | /* Get channel impulse response from it */ |
| 231 | burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); |
| 232 | |
| 233 | /* Detect bits using MLSE detection */ |
| 234 | detect_burst(input, &channel_imp_resp[0], burst_start, burst_buf); |
| 235 | |
| 236 | /* Attempt to decode BSIC and frame number */ |
| 237 | rc = decode_sch(&burst_buf[3], &t1, &t2, &t3, &d_ncc, &d_bcc); |
| 238 | if (rc) { |
| 239 | /** |
| 240 | * There is error in the SCH burst, |
| 241 | * go back to the FCCH search state |
| 242 | */ |
| 243 | d_state = fcch_search; |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | /* Set counter of bursts value */ |
| 248 | d_burst_nr.set(t1, t2, t3, 0); |
| 249 | d_burst_nr++; |
| 250 | |
| 251 | /* Consume samples up to the next guard period */ |
| 252 | consume_each(burst_start + BURST_SIZE * d_OSR + 4 * d_OSR); |
| 253 | |
| 254 | /* Update current state */ |
| 255 | d_state = synchronized; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 256 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 257 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 258 | void |
| 259 | receiver_impl::synchronized_handler(gr_complex *input, |
| 260 | gr_vector_const_void_star &input_items, int noutput_items) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 261 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 262 | /** |
| 263 | * In this state receiver is synchronized and it processes |
| 264 | * bursts according to burst type for given burst number |
| 265 | */ |
| 266 | std::vector<gr_complex> channel_imp_resp(CHAN_IMP_RESP_LENGTH * d_OSR); |
| 267 | unsigned int inputs_to_process = d_cell_allocation.size(); |
| 268 | unsigned char output_binary[BURST_SIZE]; |
| 269 | burst_type b_type; |
| 270 | int to_consume = 0; |
| 271 | int offset = 0; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 272 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 273 | if (d_process_uplink) |
| 274 | inputs_to_process *= 2; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 275 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 276 | /* Process all connected inputs */ |
| 277 | for (int input_nr = 0; input_nr < inputs_to_process; input_nr++) { |
| 278 | input = (gr_complex *) input_items[input_nr]; |
| 279 | double signal_pwr = 0; |
| 280 | |
| 281 | for (int ii = GUARD_PERIOD; ii < TS_BITS; ii++) |
| 282 | signal_pwr += abs(input[ii]) * abs(input[ii]); |
| 283 | |
| 284 | signal_pwr = signal_pwr / (TS_BITS); |
| 285 | d_signal_dbm = round(10 * log10(signal_pwr / 50)); |
| 286 | |
| 287 | if (input_nr == 0) |
| 288 | d_c0_signal_dbm = d_signal_dbm; |
| 289 | |
| 290 | /* Get burst type for given burst number */ |
| 291 | b_type = input_nr == 0 ? |
| 292 | d_channel_conf.get_burst_type(d_burst_nr) : normal_or_noise; |
| 293 | |
| 294 | /* Process burst according to its type */ |
| 295 | switch (b_type) { |
| 296 | case fcch_burst: |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 297 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 298 | if (d_freq_offset_tag_in_fcch) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 299 | break; |
| 300 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 301 | /* Send all-zero sequence message */ |
| 302 | send_burst(d_burst_nr, fc_fb, GSMTAP_BURST_FCCH, input_nr); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 303 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 304 | /* Extract frequency offset */ |
| 305 | const unsigned first_sample = |
| 306 | ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1; |
| 307 | const unsigned last_sample = |
| 308 | first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR; |
| 309 | double freq_offset_tmp = |
| 310 | compute_freq_offset(input, first_sample, last_sample); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 311 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 312 | /* Frequency correction loop */ |
| 313 | pmt::pmt_t msg = pmt::make_tuple( |
| 314 | pmt::mp("freq_offset"), |
| 315 | pmt::from_double(freq_offset_tmp - d_freq_offset_setting), |
| 316 | pmt::mp("synchronized")); |
| 317 | message_port_pub(pmt::mp("measurements"), msg); |
| 318 | |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | case sch_burst: |
| 323 | { |
| 324 | int d_ncc, d_bcc; |
| 325 | int t1, t2, t3; |
| 326 | int rc; |
| 327 | |
| 328 | /* Get channel impulse response */ |
| 329 | d_c0_burst_start = get_sch_chan_imp_resp(input, |
| 330 | &channel_imp_resp[0]); |
| 331 | |
| 332 | /* Perform MLSE detection */ |
| 333 | detect_burst(input, &channel_imp_resp[0], |
| 334 | d_c0_burst_start, output_binary); |
| 335 | |
| 336 | /* Compose a message with GSMTAP header and bits */ |
| 337 | send_burst(d_burst_nr, output_binary, |
| 338 | GSMTAP_BURST_SCH, input_nr); |
| 339 | |
| 340 | /* Attempt to decode SCH burst */ |
| 341 | rc = decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc); |
| 342 | if (rc) { |
| 343 | if (++d_failed_sch >= MAX_SCH_ERRORS) { |
| 344 | /* We have to resynchronize, change state */ |
| 345 | d_state = fcch_search; |
| 346 | |
| 347 | /* Frequency correction loop */ |
| 348 | pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"), |
| 349 | pmt::from_double(0.0),pmt::mp("sync_loss")); |
| 350 | message_port_pub(pmt::mp("measurements"), msg); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | break; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 354 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 355 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 356 | /** |
| 357 | * Decoding was successful, now |
| 358 | * compute offset from burst_start, |
| 359 | * burst should start after a guard period. |
| 360 | */ |
| 361 | offset = d_c0_burst_start - floor((GUARD_PERIOD) * d_OSR); |
| 362 | to_consume += offset; |
| 363 | d_failed_sch = 0; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 364 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 365 | break; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 366 | } |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 367 | |
| 368 | case normal_burst: |
| 369 | { |
| 370 | float normal_corr_max; |
| 371 | /** |
| 372 | * Get channel impulse response for given |
| 373 | * training sequence number - d_bcc |
| 374 | */ |
| 375 | d_c0_burst_start = get_norm_chan_imp_resp(input, |
| 376 | &channel_imp_resp[0], &normal_corr_max, d_bcc); |
| 377 | |
| 378 | /* Perform MLSE detection */ |
| 379 | detect_burst(input, &channel_imp_resp[0], |
| 380 | d_c0_burst_start, output_binary); |
| 381 | |
| 382 | /* Compose a message with GSMTAP header and bits */ |
| 383 | send_burst(d_burst_nr, output_binary, |
| 384 | GSMTAP_BURST_NORMAL, input_nr); |
| 385 | |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | case dummy_or_normal: |
| 390 | { |
| 391 | unsigned int normal_burst_start, dummy_burst_start; |
| 392 | float dummy_corr_max, normal_corr_max; |
| 393 | |
| 394 | dummy_burst_start = get_norm_chan_imp_resp(input, |
| 395 | &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY); |
| 396 | normal_burst_start = get_norm_chan_imp_resp(input, |
| 397 | &channel_imp_resp[0], &normal_corr_max, d_bcc); |
| 398 | |
| 399 | if (normal_corr_max > dummy_corr_max) { |
| 400 | d_c0_burst_start = normal_burst_start; |
| 401 | |
| 402 | /* Perform MLSE detection */ |
| 403 | detect_burst(input, &channel_imp_resp[0], |
| 404 | normal_burst_start, output_binary); |
| 405 | |
| 406 | /* Compose a message with GSMTAP header and bits */ |
| 407 | send_burst(d_burst_nr, output_binary, |
| 408 | GSMTAP_BURST_NORMAL, input_nr); |
| 409 | } else { |
| 410 | d_c0_burst_start = dummy_burst_start; |
| 411 | |
| 412 | /* Compose a message with GSMTAP header and bits */ |
| 413 | send_burst(d_burst_nr, dummy_burst, |
| 414 | GSMTAP_BURST_DUMMY, input_nr); |
| 415 | } |
| 416 | |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | case normal_or_noise: |
| 421 | { |
| 422 | std::vector<gr_complex> v(input, input + noutput_items); |
| 423 | float normal_corr_max = -1e6; |
| 424 | float normal_corr_max_tmp; |
| 425 | unsigned int burst_start; |
| 426 | int max_tn, tseq_num; |
| 427 | |
| 428 | if (d_tseq_nums.size() == 0) { |
| 429 | /** |
| 430 | * There is no information about training sequence, |
| 431 | * however the receiver can detect it with use of a |
| 432 | * very simple algorithm based on finding |
| 433 | */ |
| 434 | get_norm_chan_imp_resp(input, &channel_imp_resp[0], |
| 435 | &normal_corr_max, 0); |
| 436 | |
| 437 | float ts_max = normal_corr_max; |
| 438 | int ts_max_num = 0; |
| 439 | |
| 440 | for (int ss = 1; ss <= 7; ss++) { |
| 441 | get_norm_chan_imp_resp(input, &channel_imp_resp[0], |
| 442 | &normal_corr_max, ss); |
| 443 | |
| 444 | if (ts_max < normal_corr_max) { |
| 445 | ts_max = normal_corr_max; |
| 446 | ts_max_num = ss; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | d_tseq_nums.push_back(ts_max_num); |
| 451 | } |
| 452 | |
| 453 | /* Choose proper training sequence number */ |
| 454 | tseq_num = input_nr <= d_tseq_nums.size() ? |
| 455 | d_tseq_nums[input_nr - 1] : d_tseq_nums.back(); |
| 456 | |
| 457 | /* Get channel impulse response */ |
| 458 | burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], |
| 459 | &normal_corr_max, tseq_num); |
| 460 | |
| 461 | /* Perform MLSE detection */ |
| 462 | detect_burst(input, &channel_imp_resp[0], |
| 463 | burst_start, output_binary); |
| 464 | |
| 465 | /* Compose a message with GSMTAP header and bits */ |
| 466 | send_burst(d_burst_nr, output_binary, GSMTAP_BURST_NORMAL, input_nr); |
| 467 | |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | case dummy: |
| 472 | send_burst(d_burst_nr, dummy_burst, GSMTAP_BURST_DUMMY, input_nr); |
| 473 | break; |
| 474 | |
| 475 | case rach_burst: |
| 476 | case empty: |
| 477 | /* Do nothing */ |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | if (input_nr == input_items.size() - 1) { |
| 482 | /* Go to the next burst */ |
| 483 | d_burst_nr++; |
| 484 | |
| 485 | /* Consume samples of the burst up to next guard period */ |
| 486 | to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); |
| 487 | consume_each(to_consume); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | bool |
| 493 | receiver_impl::find_fcch_burst(const gr_complex *input, |
| 494 | const int nitems, double &computed_freq_offset) |
| 495 | { |
| 496 | /* Circular buffer used to scan through signal to find */ |
| 497 | boost::circular_buffer<float> |
| 498 | phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR); |
| 499 | boost::circular_buffer<float>::iterator buffer_iter; |
| 500 | |
| 501 | float lowest_max_min_diff; |
| 502 | float phase_diff; /* Best match for FCCH burst */ |
| 503 | float min_phase_diff; |
| 504 | float max_phase_diff; |
| 505 | double best_sum = 0; |
| 506 | gr_complex conjprod; |
| 507 | int start_pos; |
| 508 | int hit_count; |
| 509 | int miss_count; |
| 510 | |
| 511 | int sample_number = 0; |
| 512 | int to_consume = 0; |
| 513 | bool result = false; |
| 514 | bool end = false; |
| 515 | |
| 516 | /* Possible states of FCCH search algorithm */ |
| 517 | enum states |
| 518 | { |
| 519 | init, /* initialize variables */ |
| 520 | search, /* search for positive samples */ |
| 521 | found_something, /* search for FCCH and the best position of it */ |
| 522 | fcch_found, /* when FCCH was found */ |
| 523 | search_fail /* when there is no FCCH in the input vector */ |
| 524 | } fcch_search_state; |
| 525 | |
| 526 | /* Set initial state */ |
| 527 | fcch_search_state = init; |
| 528 | |
| 529 | while (!end) |
| 530 | { |
| 531 | switch (fcch_search_state) { |
| 532 | case init: |
| 533 | { |
| 534 | hit_count = 0; |
| 535 | miss_count = 0; |
| 536 | start_pos = -1; |
| 537 | lowest_max_min_diff = 99999; |
| 538 | phase_diff_buffer.clear(); |
| 539 | |
| 540 | /* Change current state */ |
| 541 | fcch_search_state = search; |
| 542 | |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | case search: |
| 547 | { |
| 548 | sample_number++; |
| 549 | |
| 550 | if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) { |
| 551 | /** |
| 552 | * If it isn't possible to find FCCH, because |
| 553 | * there is too few samples left to look into, |
| 554 | * don't do anything with those samples which are left |
| 555 | * and consume only those which were checked |
| 556 | */ |
| 557 | to_consume = sample_number; |
| 558 | fcch_search_state = search_fail; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | phase_diff = compute_phase_diff(input[sample_number], |
| 563 | input[sample_number - 1]); |
| 564 | |
| 565 | /** |
| 566 | * If a positive phase difference was found |
| 567 | * switch to state in which searches for FCCH |
| 568 | */ |
| 569 | if (phase_diff > 0) { |
| 570 | to_consume = sample_number; |
| 571 | fcch_search_state = found_something; |
| 572 | } else { |
| 573 | fcch_search_state = search; |
| 574 | } |
| 575 | |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | case found_something: |
| 580 | { |
| 581 | if (phase_diff > 0) |
| 582 | hit_count++; |
| 583 | else |
| 584 | miss_count++; |
| 585 | |
| 586 | if ((miss_count >= FCCH_MAX_MISSES * d_OSR) |
| 587 | && (hit_count <= FCCH_HITS_NEEDED * d_OSR)) |
| 588 | { |
| 589 | /* If miss_count exceeds limit before hit_count */ |
| 590 | fcch_search_state = init; |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | if (((miss_count >= FCCH_MAX_MISSES * d_OSR) |
| 595 | && (hit_count > FCCH_HITS_NEEDED * d_OSR)) |
| 596 | || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR)) |
| 597 | { |
| 598 | /** |
| 599 | * If hit_count and miss_count exceeds |
| 600 | * limit then FCCH was found |
| 601 | */ |
| 602 | fcch_search_state = fcch_found; |
| 603 | continue; |
| 604 | } |
| 605 | |
| 606 | if ((miss_count < FCCH_MAX_MISSES * d_OSR) |
| 607 | && (hit_count > FCCH_HITS_NEEDED * d_OSR)) |
| 608 | { |
| 609 | /** |
| 610 | * Find difference between minimal and maximal |
| 611 | * element in the buffer. For FCCH this value |
| 612 | * should be low. This part is searching for |
| 613 | * a region where this value is lowest. |
| 614 | */ |
| 615 | min_phase_diff = *(min_element(phase_diff_buffer.begin(), |
| 616 | phase_diff_buffer.end())); |
| 617 | max_phase_diff = *(max_element(phase_diff_buffer.begin(), |
| 618 | phase_diff_buffer.end())); |
| 619 | |
| 620 | if (lowest_max_min_diff > max_phase_diff - min_phase_diff) { |
| 621 | lowest_max_min_diff = max_phase_diff - min_phase_diff; |
| 622 | start_pos = sample_number - FCCH_HITS_NEEDED |
| 623 | * d_OSR - FCCH_MAX_MISSES * d_OSR; |
| 624 | best_sum = 0; |
| 625 | |
| 626 | for (buffer_iter = phase_diff_buffer.begin(); |
| 627 | buffer_iter != (phase_diff_buffer.end()); |
| 628 | buffer_iter++) { |
| 629 | /* Store best value of phase offset sum */ |
| 630 | best_sum += *buffer_iter - (M_PI / 2) / d_OSR; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /* If there is no single sample left to check */ |
| 636 | if (++sample_number >= nitems) { |
| 637 | fcch_search_state = search_fail; |
| 638 | continue; |
| 639 | } |
| 640 | |
| 641 | phase_diff = compute_phase_diff(input[sample_number], |
| 642 | input[sample_number-1]); |
| 643 | phase_diff_buffer.push_back(phase_diff); |
| 644 | fcch_search_state = found_something; |
| 645 | |
| 646 | break; |
| 647 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 648 | |
| 649 | case fcch_found: |
| 650 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 651 | /* Consume one FCCH burst */ |
| 652 | to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; |
| 653 | d_fcch_start_pos = d_counter + start_pos; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 654 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 655 | /** |
| 656 | * Compute frequency offset |
| 657 | * |
| 658 | * 1625000.0 / 6 - GMSK symbol rate in GSM |
| 659 | */ |
| 660 | double phase_offset = best_sum / FCCH_HITS_NEEDED; |
| 661 | double freq_offset = phase_offset * 1625000.0 / 6 / (2 * M_PI); |
| 662 | computed_freq_offset = freq_offset; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 663 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 664 | end = true; |
| 665 | result = true; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 666 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 667 | break; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 668 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 669 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 670 | case search_fail: |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 671 | end = true; |
| 672 | result = false; |
| 673 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 674 | } |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | d_counter += to_consume; |
| 678 | consume_each(to_consume); |
| 679 | |
| 680 | return result; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 683 | double |
| 684 | receiver_impl::compute_freq_offset(const gr_complex * input, |
| 685 | unsigned first_sample, unsigned last_sample) |
Piotr Krysik | 654d652 | 2017-01-23 21:53:48 +0100 | [diff] [blame] | 686 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 687 | double phase_sum = 0; |
| 688 | unsigned ii; |
| 689 | |
| 690 | for (ii = first_sample; ii < last_sample; ii++) |
| 691 | { |
| 692 | double phase_diff = compute_phase_diff(input[ii], |
| 693 | input[ii-1]) - (M_PI / 2) / d_OSR; |
Piotr Krysik | 654d652 | 2017-01-23 21:53:48 +0100 | [diff] [blame] | 694 | phase_sum += phase_diff; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | double phase_offset = phase_sum / (last_sample - first_sample); |
| 698 | double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI); |
| 699 | |
| 700 | return freq_offset; |
Piotr Krysik | 654d652 | 2017-01-23 21:53:48 +0100 | [diff] [blame] | 701 | } |
| 702 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 703 | inline float |
| 704 | receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 705 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 706 | gr_complex conjprod = val1 * conj(val2); |
| 707 | return fast_atan2f(imag(conjprod), real(conjprod)); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 708 | } |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 709 | |
| 710 | bool |
| 711 | receiver_impl::reach_sch_burst(const int nitems) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 712 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 713 | /* It just consumes samples to get near to a SCH burst */ |
| 714 | int to_consume = 0; |
| 715 | bool result = false; |
| 716 | unsigned sample_nr = d_fcch_start_pos |
| 717 | + (FRAME_BITS - SAFETY_MARGIN) * d_OSR; |
| 718 | |
| 719 | /* Consume samples until d_counter will be equal to sample_nr */ |
| 720 | if (d_counter < sample_nr) { |
| 721 | to_consume = d_counter + nitems >= sample_nr ? |
| 722 | sample_nr - d_counter : nitems; |
| 723 | } else { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 724 | to_consume = 0; |
| 725 | result = true; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | d_counter += to_consume; |
| 729 | consume_each(to_consume); |
| 730 | |
| 731 | return result; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 732 | } |
| 733 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 734 | int |
| 735 | receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, |
| 736 | gr_complex * chan_imp_resp) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 737 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 738 | std::vector<gr_complex> correlation_buffer; |
| 739 | std::vector<float> window_energy_buffer; |
| 740 | std::vector<float> power_buffer; |
| 741 | |
| 742 | int chan_imp_resp_center = 0; |
| 743 | int strongest_window_nr; |
| 744 | int burst_start; |
| 745 | float energy = 0; |
| 746 | |
| 747 | int len = (SYNC_POS + SYNC_SEARCH_RANGE) * d_OSR; |
| 748 | for (int ii = SYNC_POS * d_OSR; ii < len; ii++) { |
| 749 | gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], |
| 750 | N_SYNC_BITS - 10, &input[ii]); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 751 | correlation_buffer.push_back(correlation); |
| 752 | power_buffer.push_back(std::pow(abs(correlation), 2)); |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* Compute window energies */ |
| 756 | std::vector<float>::iterator iter = power_buffer.begin(); |
| 757 | while (iter != power_buffer.end()) { |
ptrkrysik | ef5e2db | 2015-01-03 12:10:14 +0100 | [diff] [blame] | 758 | std::vector<float>::iterator iter_ii = iter; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 759 | bool loop_end = false; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 760 | energy = 0; |
| 761 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 762 | for (int ii = 0; ii < (d_chan_imp_length) * d_OSR; ii++, iter_ii++) { |
| 763 | if (iter_ii == power_buffer.end()) { |
| 764 | loop_end = true; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 765 | break; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | energy += (*iter_ii); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 769 | } |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 770 | |
| 771 | if (loop_end) |
| 772 | break; |
| 773 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 774 | window_energy_buffer.push_back(energy); |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 775 | iter++; |
| 776 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 777 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 778 | strongest_window_nr = max_element(window_energy_buffer.begin(), |
| 779 | window_energy_buffer.end()) - window_energy_buffer.begin(); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 780 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 781 | #if 0 |
| 782 | d_channel_imp_resp.clear(); |
| 783 | #endif |
| 784 | |
| 785 | float max_correlation = 0; |
| 786 | for (int ii = 0; ii < (d_chan_imp_length) * d_OSR; ii++) { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 787 | gr_complex correlation = correlation_buffer[strongest_window_nr + ii]; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 788 | if (abs(correlation) > max_correlation) { |
| 789 | chan_imp_resp_center = ii; |
| 790 | max_correlation = abs(correlation); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 791 | } |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 792 | |
| 793 | #if 0 |
| 794 | d_channel_imp_resp.push_back(correlation); |
| 795 | #endif |
| 796 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 797 | chan_imp_resp[ii] = correlation; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | burst_start = strongest_window_nr + chan_imp_resp_center |
| 801 | - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR; |
| 802 | return burst_start; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 806 | void |
| 807 | receiver_impl::detect_burst(const gr_complex * input, |
| 808 | gr_complex * chan_imp_resp, int burst_start, |
| 809 | unsigned char * output_binary) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 810 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 811 | std::vector<gr_complex> rhh_temp(CHAN_IMP_RESP_LENGTH * d_OSR); |
| 812 | unsigned int stop_states[2] = {4, 12}; |
| 813 | gr_complex filtered_burst[BURST_SIZE]; |
| 814 | gr_complex rhh[CHAN_IMP_RESP_LENGTH]; |
| 815 | float output[BURST_SIZE]; |
| 816 | int start_state = 3; |
| 817 | |
| 818 | autocorrelation(chan_imp_resp, &rhh_temp[0], d_chan_imp_length*d_OSR); |
| 819 | for (int ii = 0; ii < d_chan_imp_length; ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 820 | rhh[ii] = conj(rhh_temp[ii*d_OSR]); |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 821 | |
| 822 | mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, |
| 823 | d_chan_imp_length * d_OSR, filtered_burst); |
| 824 | |
| 825 | viterbi_detector(filtered_burst, BURST_SIZE, rhh, |
| 826 | start_state, stop_states, 2, output); |
| 827 | |
| 828 | for (int i = 0; i < BURST_SIZE; i++) |
| 829 | output_binary[i] = output[i] > 0; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 830 | } |
| 831 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 832 | void |
| 833 | receiver_impl::gmsk_mapper(const unsigned char * input, |
| 834 | int nitems, gr_complex * gmsk_output, gr_complex start_point) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 835 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 836 | gr_complex j = gr_complex(0.0, 1.0); |
| 837 | gmsk_output[0] = start_point; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 838 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 839 | int previous_symbol = 2 * input[0] - 1; |
| 840 | int current_symbol; |
| 841 | int encoded_symbol; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 842 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 843 | for (int i = 1; i < nitems; i++) { |
| 844 | /* Change bits representation to NRZ */ |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 845 | current_symbol = 2 * input[i] - 1; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 846 | |
| 847 | /* Differentially encode */ |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 848 | encoded_symbol = current_symbol * previous_symbol; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 849 | |
| 850 | /* And do GMSK mapping */ |
| 851 | gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) |
| 852 | * gmsk_output[i-1]; |
| 853 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 854 | previous_symbol = current_symbol; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 855 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 856 | } |
| 857 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 858 | gr_complex |
| 859 | receiver_impl::correlate_sequence(const gr_complex * sequence, |
| 860 | int length, const gr_complex * input) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 861 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 862 | gr_complex result(0.0, 0.0); |
| 863 | |
| 864 | for (int ii = 0; ii < length; ii++) |
| 865 | result += sequence[ii] * conj(input[ii * d_OSR]); |
| 866 | |
| 867 | return result / gr_complex(length, 0); |
| 868 | } |
| 869 | |
| 870 | /* Computes autocorrelation for positive arguments */ |
| 871 | inline void |
| 872 | receiver_impl::autocorrelation(const gr_complex * input, |
| 873 | gr_complex * out, int nitems) |
| 874 | { |
| 875 | for (int k = nitems - 1; k >= 0; k--) { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 876 | out[k] = gr_complex(0, 0); |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 877 | for (int i = k; i < nitems; i++) |
| 878 | out[k] += input[i] * conj(input[i - k]); |
| 879 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 880 | } |
| 881 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 882 | inline void |
| 883 | receiver_impl::mafi(const gr_complex * input, int nitems, |
| 884 | gr_complex * filter, int filter_length, gr_complex * output) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 885 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 886 | for (int n = 0; n < nitems; n++) { |
| 887 | int a = n * d_OSR; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 888 | output[n] = 0; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 889 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 890 | for (int ii = 0; ii < filter_length; ii++) { |
| 891 | if ((a + ii) >= nitems * d_OSR) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 892 | break; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 893 | |
| 894 | output[n] += input[a + ii] * filter[ii]; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 895 | } |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | |
| 899 | /* Especially computations of strongest_window_nr */ |
| 900 | int |
| 901 | receiver_impl::get_norm_chan_imp_resp(const gr_complex *input, |
| 902 | gr_complex *chan_imp_resp, float *corr_max, int bcc) |
| 903 | { |
| 904 | std::vector<gr_complex> correlation_buffer; |
| 905 | std::vector<float> window_energy_buffer; |
| 906 | std::vector<float> power_buffer; |
| 907 | |
| 908 | int search_center = (int) (TRAIN_POS + GUARD_PERIOD) * d_OSR; |
| 909 | int search_start_pos = search_center + 1 - 5 * d_OSR; |
| 910 | int search_stop_pos = search_center |
| 911 | + d_chan_imp_length * d_OSR + 5 * d_OSR; |
| 912 | |
| 913 | for (int ii = search_start_pos; ii < search_stop_pos; ii++) { |
| 914 | gr_complex correlation = correlate_sequence( |
| 915 | &d_norm_training_seq[bcc][TRAIN_BEGINNING], |
| 916 | N_TRAIN_BITS - 10, &input[ii]); |
| 917 | correlation_buffer.push_back(correlation); |
| 918 | power_buffer.push_back(std::pow(abs(correlation), 2)); |
| 919 | } |
| 920 | |
| 921 | #if 0 |
| 922 | plot(power_buffer); |
| 923 | #endif |
| 924 | |
| 925 | /* Compute window energies */ |
| 926 | std::vector<float>::iterator iter = power_buffer.begin(); |
| 927 | while (iter != power_buffer.end()) { |
| 928 | std::vector<float>::iterator iter_ii = iter; |
| 929 | bool loop_end = false; |
| 930 | float energy = 0; |
| 931 | |
| 932 | int len = d_chan_imp_length * d_OSR; |
| 933 | for (int ii = 0; ii < len; ii++, iter_ii++) { |
| 934 | if (iter_ii == power_buffer.end()) { |
| 935 | loop_end = true; |
| 936 | break; |
| 937 | } |
| 938 | |
| 939 | energy += (*iter_ii); |
| 940 | } |
| 941 | |
| 942 | if (loop_end) |
| 943 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 944 | |
| 945 | window_energy_buffer.push_back(energy); |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 946 | iter++; |
| 947 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 948 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 949 | /* Calculate the strongest window number */ |
| 950 | int strongest_window_nr = max_element(window_energy_buffer.begin(), |
| 951 | window_energy_buffer.end() - d_chan_imp_length * d_OSR) |
| 952 | - window_energy_buffer.begin(); |
| 953 | |
| 954 | if (strongest_window_nr < 0) |
| 955 | strongest_window_nr = 0; |
| 956 | |
| 957 | float max_correlation = 0; |
| 958 | for (int ii = 0; ii < d_chan_imp_length * d_OSR; ii++) { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 959 | gr_complex correlation = correlation_buffer[strongest_window_nr + ii]; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 960 | if (abs(correlation) > max_correlation) |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 961 | max_correlation = abs(correlation); |
| 962 | |
| 963 | #if 0 |
| 964 | d_channel_imp_resp.push_back(correlation); |
| 965 | #endif |
| 966 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 967 | chan_imp_resp[ii] = correlation; |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | *corr_max = max_correlation; |
| 971 | |
| 972 | /** |
| 973 | * Compute first sample position, which corresponds |
| 974 | * to the first sample of the impulse response |
| 975 | */ |
| 976 | return search_start_pos + strongest_window_nr - TRAIN_POS * d_OSR; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 980 | void |
| 981 | receiver_impl::send_burst(burst_counter burst_nr, |
| 982 | const unsigned char * burst_binary, uint8_t burst_type, |
| 983 | unsigned int input_nr) |
ptrkrysik | 380dea8 | 2015-08-06 10:11:58 +0200 | [diff] [blame] | 984 | { |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 985 | /* Buffer for GSMTAP header and burst */ |
| 986 | uint8_t buf[sizeof(gsmtap_hdr) + BURST_SIZE]; |
| 987 | uint32_t frame_number; |
| 988 | uint16_t arfcn; |
| 989 | uint8_t tn; |
ptrkrysik | 617ba03 | 2014-11-21 10:11:05 +0100 | [diff] [blame] | 990 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 991 | /* Set pointers to GSMTAP header and burst inside buffer */ |
| 992 | struct gsmtap_hdr *tap_header = (struct gsmtap_hdr *) buf; |
| 993 | uint8_t *burst = buf + sizeof(gsmtap_hdr); |
| 994 | |
| 995 | tap_header->version = GSMTAP_VERSION; |
| 996 | tap_header->hdr_len = sizeof(gsmtap_hdr) / 4; |
| 997 | tap_header->type = GSMTAP_TYPE_UM_BURST; |
| 998 | tap_header->sub_type = burst_type; |
| 999 | |
| 1000 | bool dl_burst = !(input_nr >= d_cell_allocation.size()); |
| 1001 | if (dl_burst) { |
| 1002 | tn = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr()); |
| 1003 | frame_number = htobe32(d_burst_nr.get_frame_nr()); |
| 1004 | arfcn = htobe16(d_cell_allocation[input_nr]); |
| 1005 | } else { |
| 1006 | input_nr -= d_cell_allocation.size(); |
| 1007 | tn = static_cast<uint8_t> |
| 1008 | (d_burst_nr.subtract_timeslots(3).get_timeslot_nr()); |
| 1009 | frame_number = htobe32( |
| 1010 | d_burst_nr.subtract_timeslots(3).get_frame_nr()); |
| 1011 | arfcn = htobe16( |
| 1012 | d_cell_allocation[input_nr] | GSMTAP_ARFCN_F_UPLINK); |
| 1013 | } |
| 1014 | |
| 1015 | tap_header->frame_number = frame_number; |
| 1016 | tap_header->timeslot = tn; |
| 1017 | tap_header->arfcn = arfcn; |
| 1018 | |
| 1019 | tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm); |
| 1020 | tap_header->snr_db = 0; /* FIXME: Can we calculate this? */ |
| 1021 | |
| 1022 | /* Copy burst to the buffer */ |
| 1023 | memcpy(burst, burst_binary, BURST_SIZE); |
| 1024 | |
| 1025 | /* Allocate a new message */ |
| 1026 | pmt::pmt_t blob = pmt::make_blob(buf, sizeof(gsmtap_hdr) + BURST_SIZE); |
| 1027 | pmt::pmt_t msg = pmt::cons(pmt::PMT_NIL, blob); |
| 1028 | |
| 1029 | /* Send message */ |
| 1030 | if (input_nr == 0) |
ptrkrysik | e518bbf | 2014-11-06 14:50:59 +0100 | [diff] [blame] | 1031 | message_port_pub(pmt::mp("C0"), msg); |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 1032 | else |
ptrkrysik | e518bbf | 2014-11-06 14:50:59 +0100 | [diff] [blame] | 1033 | message_port_pub(pmt::mp("CX"), msg); |
| 1034 | } |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 1035 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 1036 | void |
| 1037 | receiver_impl::configure_receiver(void) |
| 1038 | { |
| 1039 | d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51); |
| 1040 | d_channel_conf.set_burst_types(TIMESLOT0, TEST51, |
| 1041 | sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 1042 | d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES, |
| 1043 | sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal); |
| 1044 | d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, |
| 1045 | sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst); |
| 1046 | d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, |
| 1047 | sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1048 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 1049 | d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51); |
| 1050 | d_channel_conf.set_burst_types(TIMESLOT1, TEST51, |
| 1051 | sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1052 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 1053 | d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51); |
| 1054 | d_channel_conf.set_burst_types(TIMESLOT2, TEST51, |
| 1055 | sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1056 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 1057 | d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51); |
| 1058 | d_channel_conf.set_burst_types(TIMESLOT3, TEST51, |
| 1059 | sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
ptrkrysik | e518bbf | 2014-11-06 14:50:59 +0100 | [diff] [blame] | 1060 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 1061 | d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51); |
| 1062 | d_channel_conf.set_burst_types(TIMESLOT4, TEST51, |
| 1063 | sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 1064 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 1065 | d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51); |
| 1066 | d_channel_conf.set_burst_types(TIMESLOT5, TEST51, |
| 1067 | sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1068 | |
Vadim Yanitskiy | cc82cf0 | 2017-07-24 19:21:02 +0700 | [diff] [blame] | 1069 | d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51); |
| 1070 | d_channel_conf.set_burst_types(TIMESLOT6, TEST51, |
| 1071 | sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 1072 | |
| 1073 | d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51); |
| 1074 | d_channel_conf.set_burst_types(TIMESLOT7, TEST51, |
| 1075 | sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 1076 | } |
| 1077 | |
| 1078 | void |
| 1079 | receiver_impl::set_cell_allocation( |
| 1080 | const std::vector<int> &cell_allocation) |
| 1081 | { |
| 1082 | d_cell_allocation = cell_allocation; |
| 1083 | } |
| 1084 | |
| 1085 | void |
| 1086 | receiver_impl::set_tseq_nums(const std::vector<int> &tseq_nums) |
| 1087 | { |
| 1088 | d_tseq_nums = tseq_nums; |
| 1089 | } |
| 1090 | |
| 1091 | void |
| 1092 | receiver_impl::reset(void) |
| 1093 | { |
| 1094 | d_state = fcch_search; |
| 1095 | } |
| 1096 | |
| 1097 | } /* namespace gsm */ |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1098 | } /* namespace gr */ |