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