piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 2 | /* |
piotr | c1d47df | 2014-04-17 09:45:50 +0200 | [diff] [blame] | 3 | * Copyright 2014 Piotr Krysik <pkrysik@elka.pw.edu.pl>. |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 4 | * |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3, or (at your option) |
| 8 | * any later version. |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 9 | * |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 14 | * |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; see the file COPYING. If not, write to |
| 17 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include "config.h" |
| 23 | #endif |
| 24 | |
| 25 | #include <gnuradio/io_signature.h> |
| 26 | #include "receiver_impl.h" |
| 27 | |
| 28 | #include <gnuradio/io_signature.h> |
| 29 | #include <gnuradio/math.h> |
| 30 | #include <math.h> |
| 31 | #include <boost/circular_buffer.hpp> |
| 32 | #include <algorithm> |
| 33 | #include <numeric> |
| 34 | #include <viterbi_detector.h> |
| 35 | #include <string.h> |
| 36 | #include <sch.h> |
| 37 | #include <iostream> |
| 38 | #include <iomanip> |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 39 | #include <assert.h> |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 40 | #include <boost/scoped_ptr.hpp> |
piotr | 7f3f366 | 2014-07-08 16:47:53 +0200 | [diff] [blame] | 41 | //#include "plotting/plotting.hpp" |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 42 | |
| 43 | #define SYNC_SEARCH_RANGE 30 |
| 44 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 45 | namespace gr |
| 46 | { |
| 47 | namespace gsm |
| 48 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 49 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 50 | typedef std::list<float> list_float; |
| 51 | typedef std::vector<float> vector_float; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 52 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 53 | typedef boost::circular_buffer<float> circular_buffer_float; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 54 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 55 | receiver::sptr |
Piotr K | 608a08e | 2014-08-07 17:01:55 +0200 | [diff] [blame^] | 56 | receiver::make(int osr, int arfcn) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 57 | { |
| 58 | return gnuradio::get_initial_sptr |
Piotr K | 608a08e | 2014-08-07 17:01:55 +0200 | [diff] [blame^] | 59 | (new receiver_impl(osr, arfcn)); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
| 63 | * The private constructor |
| 64 | */ |
Piotr K | 608a08e | 2014-08-07 17:01:55 +0200 | [diff] [blame^] | 65 | receiver_impl::receiver_impl(int osr, int arfcn) |
piotr | c7c249a | 2014-05-02 17:24:08 +0200 | [diff] [blame] | 66 | : gr::sync_block("receiver", |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 67 | gr::io_signature::make(1, 1, sizeof(gr_complex)), |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 68 | gr::io_signature::make(0, 0, 0)), |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 69 | d_OSR(osr), |
| 70 | d_chan_imp_length(CHAN_IMP_RESP_LENGTH), |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 71 | d_counter(0), |
| 72 | d_fcch_start_pos(0), |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 73 | d_freq_offset_setting(0), |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 74 | d_state(fcch_search), |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 75 | d_burst_nr(osr), |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 76 | d_failed_sch(0), |
| 77 | d_arfcn((int)(arfcn)), |
| 78 | d_signal_dbm(-120) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 79 | { |
| 80 | int i; |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 81 | //don't send samples to the receiver until there are at least samples for one |
piotr | 7f3f366 | 2014-07-08 16:47:53 +0200 | [diff] [blame] | 82 | set_output_multiple(floor((TS_BITS + 2 * GUARD_PERIOD) * d_OSR)); // burst and two gurad periods (one gurard period is an arbitrary overlap) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 83 | gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0, -1.0)); |
| 84 | for (i = 0; i < TRAIN_SEQ_NUM; i++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 85 | { |
piotr | f502e0f | 2014-04-24 10:28:29 +0200 | [diff] [blame] | 86 | gr_complex startpoint = (train_seq[i][0]==0) ? gr_complex(1.0, 0.0) : gr_complex(-1.0, 0.0); //if first bit of the seqeunce ==0 first symbol ==1 |
piotr | 7f3f366 | 2014-07-08 16:47:53 +0200 | [diff] [blame] | 87 | //if first bit of the seqeunce ==1 first symbol ==-1 |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 88 | gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], startpoint); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 89 | } |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 90 | message_port_register_out(pmt::mp("bursts")); |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 91 | message_port_register_out(pmt::mp("measurements")); |
piotr | 903b1d6 | 2014-04-17 11:33:27 +0200 | [diff] [blame] | 92 | configure_receiver(); //configure the receiver - tell it where to find which burst type |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 93 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 94 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 95 | /* |
| 96 | * Our virtual destructor. |
| 97 | */ |
| 98 | receiver_impl::~receiver_impl() |
| 99 | { |
| 100 | } |
| 101 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 102 | int |
piotr | c7c249a | 2014-05-02 17:24:08 +0200 | [diff] [blame] | 103 | receiver_impl::work(int noutput_items, |
| 104 | gr_vector_const_void_star &input_items, |
| 105 | gr_vector_void_star &output_items) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 106 | { |
| 107 | const gr_complex *input = (const gr_complex *) input_items[0]; |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 108 | std::vector<tag_t> freq_offset_tags; |
| 109 | uint64_t start = nitems_read(0); |
| 110 | uint64_t stop = start + noutput_items; |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 111 | |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 112 | pmt::pmt_t key = pmt::string_to_symbol("setting_freq_offset"); |
| 113 | get_tags_in_range(freq_offset_tags, 0, start, stop, key); |
| 114 | bool freq_offset_tag_in_fcch = false; |
| 115 | uint64_t tag_offset=-1; //-1 - just some clearly invalid value |
| 116 | |
| 117 | if(!freq_offset_tags.empty()){ |
| 118 | tag_t freq_offset_tag = freq_offset_tags[0]; |
| 119 | tag_offset = freq_offset_tag.offset - start; |
| 120 | |
| 121 | burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr); |
| 122 | if(d_state == synchronized && b_type == fcch_burst){ |
| 123 | uint64_t last_sample_nr = ceil((GUARD_PERIOD + 2.0 * TAIL_BITS + 156.25) * d_OSR) + 1; |
| 124 | if(tag_offset < last_sample_nr){ |
| 125 | DCOUT("Freq change inside FCCH burst!!!!!!!!!!!!!!"); |
| 126 | freq_offset_tag_in_fcch = true; |
| 127 | } |
| 128 | d_freq_offset_setting = pmt::to_double(freq_offset_tag.value); |
| 129 | } else { |
| 130 | d_freq_offset_setting = pmt::to_double(freq_offset_tag.value); |
| 131 | } |
| 132 | } |
| 133 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 134 | switch (d_state) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 135 | { |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 136 | //bootstrapping |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 137 | case fcch_search: //this state is used because it takes some time (a bunch of buffered samples) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 138 | { |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 139 | DCOUT("FCCH search"); |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 140 | double freq_offset_tmp; |
| 141 | if (find_fcch_burst(input, noutput_items,freq_offset_tmp)) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 142 | { |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 143 | pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("fcch_search")); |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 144 | message_port_pub(pmt::mp("measurements"), msg); |
| 145 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 146 | d_state = sch_search; |
| 147 | } |
| 148 | else |
| 149 | { |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 150 | d_state = fcch_search; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 151 | } |
| 152 | break; |
| 153 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 154 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 155 | case sch_search: |
| 156 | { |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 157 | DCOUT("SCH search"); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 158 | vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR); |
| 159 | int t1, t2, t3; |
| 160 | int burst_start = 0; |
| 161 | unsigned char output_binary[BURST_SIZE]; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 162 | |
piotr | c7c249a | 2014-05-02 17:24:08 +0200 | [diff] [blame] | 163 | if (reach_sch_burst(noutput_items)) //wait for a SCH burst |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 164 | { |
| 165 | burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response from it |
| 166 | detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //detect bits using MLSE detection |
| 167 | if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //decode SCH burst |
| 168 | { |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 169 | DCOUT("sch burst_start: " << burst_start); |
| 170 | DCOUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 171 | d_burst_nr.set(t1, t2, t3, 0); //set counter of bursts value |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 172 | d_burst_nr++; |
| 173 | |
piotr | 7f3f366 | 2014-07-08 16:47:53 +0200 | [diff] [blame] | 174 | consume_each(burst_start + BURST_SIZE * d_OSR + 4*d_OSR); //consume samples up to next guard period |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 175 | d_state = synchronized; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 179 | d_state = fcch_search; //if there is error in the sch burst go back to fcch search phase |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 180 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 181 | } |
| 182 | else |
| 183 | { |
| 184 | d_state = sch_search; |
| 185 | } |
| 186 | break; |
| 187 | } |
| 188 | //in this state receiver is synchronized and it processes bursts according to burst type for given burst number |
| 189 | case synchronized: |
| 190 | { |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 191 | DCOUT("Synchronized"); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 192 | vector_complex channel_imp_resp(CHAN_IMP_RESP_LENGTH*d_OSR); |
| 193 | int burst_start; |
| 194 | int offset = 0; |
| 195 | int to_consume = 0; |
| 196 | unsigned char output_binary[BURST_SIZE]; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 197 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 198 | burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr); //get burst type for given burst number |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 199 | double signal_pwr = 0; |
piotr | c7c249a | 2014-05-02 17:24:08 +0200 | [diff] [blame] | 200 | for(int ii=0;ii<noutput_items;ii++) |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 201 | { |
| 202 | signal_pwr += abs(input[ii])*abs(input[ii]); |
| 203 | } |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 204 | d_signal_dbm=static_cast<int8_t>(round(10*log10(signal_pwr/50/noutput_items))); |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 205 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 206 | switch (b_type) |
| 207 | { |
| 208 | case fcch_burst: //if it's FCCH burst |
| 209 | { |
| 210 | const unsigned first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1; |
| 211 | const unsigned last_sample = first_sample + USEFUL_BITS * d_OSR - TAIL_BITS * d_OSR; |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 212 | double freq_offset_tmp = compute_freq_offset(input, first_sample, last_sample); //extract frequency offset from it |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 213 | |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 214 | send_burst(d_burst_nr, fc_fb, b_type); |
| 215 | |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 216 | pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(freq_offset_tmp-d_freq_offset_setting),pmt::mp("synchronized")); |
| 217 | message_port_pub(pmt::mp("measurements"), msg); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 218 | } |
| 219 | break; |
| 220 | case sch_burst: //if it's SCH burst |
| 221 | { |
| 222 | int t1, t2, t3, d_ncc, d_bcc; |
| 223 | burst_start = get_sch_chan_imp_resp(input, &channel_imp_resp[0]); //get channel impulse response |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 224 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 225 | detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 226 | send_burst(d_burst_nr, output_binary, b_type); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 227 | if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) //and decode SCH data |
| 228 | { |
| 229 | // d_burst_nr.set(t1, t2, t3, 0); //but only to check if burst_start value is correct |
| 230 | d_failed_sch = 0; |
| 231 | DCOUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3); |
| 232 | offset = burst_start - floor((GUARD_PERIOD) * d_OSR); //compute offset from burst_start - burst should start after a guard period |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 233 | DCOUT("offset: "<<offset); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 234 | to_consume += offset; //adjust with offset number of samples to be consumed |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | d_failed_sch++; |
| 239 | if (d_failed_sch >= MAX_SCH_ERRORS) |
| 240 | { |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 241 | d_state = fcch_search; |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 242 | pmt::pmt_t msg = pmt::make_tuple(pmt::mp("freq_offset"),pmt::from_double(0.0),pmt::mp("sync_loss")); |
| 243 | message_port_pub(pmt::mp("measurements"), msg); |
| 244 | DCOUT("Re-Synchronization!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 245 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 246 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 247 | } |
| 248 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 249 | |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 250 | case normal_burst: |
| 251 | { |
| 252 | float normal_corr_max; //if it's normal burst |
| 253 | burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc); //get channel impulse response for given training sequence number - d_bcc |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 254 | detect_burst(input, &channel_imp_resp[0], burst_start, output_binary); //MLSE detection of bits |
piotr | 8dc74a4 | 2014-04-17 09:48:46 +0200 | [diff] [blame] | 255 | send_burst(d_burst_nr, output_binary, b_type); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 256 | break; |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 257 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 258 | case dummy_or_normal: |
| 259 | { |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 260 | unsigned int normal_burst_start; |
| 261 | float dummy_corr_max, normal_corr_max; |
| 262 | get_norm_chan_imp_resp(input, &channel_imp_resp[0], &dummy_corr_max, TS_DUMMY); |
| 263 | normal_burst_start = get_norm_chan_imp_resp(input, &channel_imp_resp[0], &normal_corr_max, d_bcc); |
| 264 | |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 265 | DCOUT("normal_corr_max: " << normal_corr_max << " dummy_corr_max:" << dummy_corr_max); |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 266 | if (normal_corr_max > dummy_corr_max) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 267 | { |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 268 | detect_burst(input, &channel_imp_resp[0], normal_burst_start, output_binary); |
piotr | 8dc74a4 | 2014-04-17 09:48:46 +0200 | [diff] [blame] | 269 | send_burst(d_burst_nr, output_binary, b_type); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 270 | } |
| 271 | else |
| 272 | { |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 273 | send_burst(d_burst_nr, dummy_burst, b_type); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | case rach_burst: |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 277 | break; |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 278 | case dummy: |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 279 | send_burst(d_burst_nr, dummy_burst, b_type); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 280 | break; |
| 281 | case empty: //if it's empty burst |
| 282 | break; //do nothing |
| 283 | } |
| 284 | |
| 285 | d_burst_nr++; //go to next burst |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 286 | to_consume += TS_BITS * d_OSR + d_burst_nr.get_offset(); //consume samples of the burst up to next guard period |
| 287 | //and add offset which is introduced by |
| 288 | //0.25 fractional part of a guard period |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 289 | consume_each(to_consume); |
| 290 | } |
| 291 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 292 | } |
| 293 | |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 294 | return 0; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 295 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 296 | |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 297 | bool receiver_impl::find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 298 | { |
| 299 | circular_buffer_float phase_diff_buffer(FCCH_HITS_NEEDED * d_OSR); //circular buffer used to scan throug signal to find |
| 300 | //best match for FCCH burst |
| 301 | float phase_diff = 0; |
| 302 | gr_complex conjprod; |
| 303 | int start_pos = -1; |
| 304 | int hit_count = 0; |
| 305 | int miss_count = 0; |
| 306 | float min_phase_diff; |
| 307 | float max_phase_diff; |
| 308 | double best_sum = 0; |
| 309 | float lowest_max_min_diff = 99999; |
| 310 | |
| 311 | int to_consume = 0; |
| 312 | int sample_number = 0; |
| 313 | bool end = false; |
| 314 | bool result = false; |
| 315 | circular_buffer_float::iterator buffer_iter; |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 316 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 317 | /**@name Possible states of FCCH search algorithm*/ |
| 318 | //@{ |
| 319 | enum states |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 320 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 321 | init, ///< initialize variables |
| 322 | search, ///< search for positive samples |
| 323 | found_something, ///< search for FCCH and the best position of it |
| 324 | fcch_found, ///< when FCCH was found |
| 325 | search_fail ///< when there is no FCCH in the input vector |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 326 | } fcch_search_state; |
| 327 | //@} |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 328 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 329 | fcch_search_state = init; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 330 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 331 | while (!end) |
| 332 | { |
| 333 | switch (fcch_search_state) |
| 334 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 335 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 336 | case init: //initialize variables |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 337 | hit_count = 0; |
| 338 | miss_count = 0; |
| 339 | start_pos = -1; |
| 340 | lowest_max_min_diff = 99999; |
| 341 | phase_diff_buffer.clear(); |
| 342 | fcch_search_state = search; |
| 343 | |
| 344 | break; |
| 345 | |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 346 | case search: // search for positive samples |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 347 | sample_number++; |
| 348 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 349 | if (sample_number > nitems - FCCH_HITS_NEEDED * d_OSR) //if it isn't possible to find FCCH because |
| 350 | { |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 351 | //there's too few samples left to look into, |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 352 | to_consume = sample_number; //don't do anything with those samples which are left |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 353 | //and consume only those which were checked |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 354 | fcch_search_state = search_fail; |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 359 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 360 | if (phase_diff > 0) //if a positive phase difference was found |
| 361 | { |
| 362 | to_consume = sample_number; |
| 363 | fcch_search_state = found_something; //switch to state in which searches for FCCH |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | fcch_search_state = search; |
| 368 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | break; |
| 372 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 373 | case found_something: // search for FCCH and the best position of it |
| 374 | { |
| 375 | if (phase_diff > 0) |
| 376 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 377 | hit_count++; //positive phase differencies increases hits_count |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 378 | } |
| 379 | else |
| 380 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 381 | miss_count++; //negative increases miss_count |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 382 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 383 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 384 | if ((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count <= FCCH_HITS_NEEDED * d_OSR)) |
| 385 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 386 | //if miss_count exceeds limit before hit_count |
| 387 | fcch_search_state = init; //go to init |
| 388 | continue; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 389 | } |
| 390 | else if (((miss_count >= FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) || (hit_count > 2 * FCCH_HITS_NEEDED * d_OSR)) |
| 391 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 392 | //if hit_count and miss_count exceeds limit then FCCH was found |
| 393 | fcch_search_state = fcch_found; |
| 394 | continue; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 395 | } |
| 396 | else if ((miss_count < FCCH_MAX_MISSES * d_OSR) && (hit_count > FCCH_HITS_NEEDED * d_OSR)) |
| 397 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 398 | //find difference between minimal and maximal element in the buffer |
| 399 | //for FCCH this value should be low |
| 400 | //this part is searching for a region where this value is lowest |
| 401 | min_phase_diff = * (min_element(phase_diff_buffer.begin(), phase_diff_buffer.end())); |
| 402 | max_phase_diff = * (max_element(phase_diff_buffer.begin(), phase_diff_buffer.end())); |
| 403 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 404 | if (lowest_max_min_diff > max_phase_diff - min_phase_diff) |
| 405 | { |
| 406 | lowest_max_min_diff = max_phase_diff - min_phase_diff; |
| 407 | start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; //store start pos |
| 408 | best_sum = 0; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 409 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 410 | for (buffer_iter = phase_diff_buffer.begin(); |
| 411 | buffer_iter != (phase_diff_buffer.end()); |
| 412 | buffer_iter++) |
| 413 | { |
| 414 | best_sum += *buffer_iter - (M_PI / 2) / d_OSR; //store best value of phase offset sum |
| 415 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 416 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 417 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 418 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 419 | sample_number++; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 420 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 421 | if (sample_number >= nitems) //if there's no single sample left to check |
| 422 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 423 | fcch_search_state = search_fail;//FCCH search failed |
| 424 | continue; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 425 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 426 | |
| 427 | phase_diff = compute_phase_diff(input[sample_number], input[sample_number-1]); |
| 428 | phase_diff_buffer.push_back(phase_diff); |
| 429 | fcch_search_state = found_something; |
| 430 | } |
| 431 | break; |
| 432 | |
| 433 | case fcch_found: |
| 434 | { |
| 435 | DCOUT("fcch found on position: " << d_counter + start_pos); |
| 436 | to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; //consume one FCCH burst |
| 437 | |
| 438 | d_fcch_start_pos = d_counter + start_pos; |
| 439 | |
| 440 | //compute frequency offset |
| 441 | double phase_offset = best_sum / FCCH_HITS_NEEDED; |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 442 | double freq_offset = phase_offset * 1625000.0/6 / (2 * M_PI); //1625000.0/6 - GMSK symbol rate in GSM |
| 443 | computed_freq_offset = freq_offset; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 444 | |
| 445 | end = true; |
| 446 | result = true; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 447 | break; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 448 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 449 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 450 | case search_fail: |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 451 | end = true; |
| 452 | result = false; |
| 453 | break; |
| 454 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 455 | } |
| 456 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 457 | d_counter += to_consume; |
| 458 | consume_each(to_consume); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 459 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 460 | return result; |
| 461 | } |
| 462 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 463 | double receiver_impl::compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample) |
| 464 | { |
| 465 | double phase_sum = 0; |
| 466 | unsigned ii; |
| 467 | |
| 468 | for (ii = first_sample; ii < last_sample; ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 469 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 470 | double phase_diff = compute_phase_diff(input[ii], input[ii-1]) - (M_PI / 2) / d_OSR; |
| 471 | phase_sum += phase_diff; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 472 | } |
| 473 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 474 | double phase_offset = phase_sum / (last_sample - first_sample); |
| 475 | double freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI); |
| 476 | return freq_offset; |
| 477 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 478 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 479 | inline float receiver_impl::compute_phase_diff(gr_complex val1, gr_complex val2) |
| 480 | { |
| 481 | gr_complex conjprod = val1 * conj(val2); |
| 482 | return fast_atan2f(imag(conjprod), real(conjprod)); |
| 483 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 484 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 485 | bool receiver_impl::reach_sch_burst(const int nitems) |
| 486 | { |
| 487 | //it just consumes samples to get near to a SCH burst |
| 488 | int to_consume = 0; |
| 489 | bool result = false; |
| 490 | unsigned sample_nr_near_sch_start = d_fcch_start_pos + (FRAME_BITS - SAFETY_MARGIN) * d_OSR; |
| 491 | |
| 492 | //consume samples until d_counter will be equal to sample_nr_near_sch_start |
| 493 | if (d_counter < sample_nr_near_sch_start) |
| 494 | { |
| 495 | if (d_counter + nitems >= sample_nr_near_sch_start) |
| 496 | { |
| 497 | to_consume = sample_nr_near_sch_start - d_counter; |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | to_consume = nitems; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 502 | } |
| 503 | result = false; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 504 | } |
| 505 | else |
| 506 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 507 | to_consume = 0; |
| 508 | result = true; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 509 | } |
| 510 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 511 | d_counter += to_consume; |
| 512 | consume_each(to_consume); |
| 513 | return result; |
| 514 | } |
| 515 | |
| 516 | int receiver_impl::get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp) |
| 517 | { |
| 518 | vector_complex correlation_buffer; |
| 519 | vector_float power_buffer; |
| 520 | vector_float window_energy_buffer; |
| 521 | |
| 522 | int strongest_window_nr; |
| 523 | int burst_start = 0; |
| 524 | int chan_imp_resp_center = 0; |
| 525 | float max_correlation = 0; |
| 526 | float energy = 0; |
| 527 | |
| 528 | for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 529 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 530 | gr_complex correlation = correlate_sequence(&d_sch_training_seq[5], N_SYNC_BITS - 10, &input[ii]); |
| 531 | correlation_buffer.push_back(correlation); |
| 532 | power_buffer.push_back(std::pow(abs(correlation), 2)); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 533 | } |
piotr | 7f3f366 | 2014-07-08 16:47:53 +0200 | [diff] [blame] | 534 | //plot(power_buffer); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 535 | //compute window energies |
| 536 | vector_float::iterator iter = power_buffer.begin(); |
| 537 | bool loop_end = false; |
| 538 | while (iter != power_buffer.end()) |
| 539 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 540 | vector_float::iterator iter_ii = iter; |
| 541 | energy = 0; |
| 542 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 543 | for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++, iter_ii++) |
| 544 | { |
| 545 | if (iter_ii == power_buffer.end()) |
| 546 | { |
| 547 | loop_end = true; |
| 548 | break; |
| 549 | } |
| 550 | energy += (*iter_ii); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 551 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 552 | if (loop_end) |
| 553 | { |
| 554 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 555 | } |
| 556 | iter++; |
| 557 | window_energy_buffer.push_back(energy); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 558 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 559 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 560 | strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin(); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 561 | // d_channel_imp_resp.clear(); |
| 562 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 563 | max_correlation = 0; |
| 564 | for (int ii = 0; ii < (d_chan_imp_length) *d_OSR; ii++) |
| 565 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 566 | gr_complex correlation = correlation_buffer[strongest_window_nr + ii]; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 567 | if (abs(correlation) > max_correlation) |
| 568 | { |
| 569 | chan_imp_resp_center = ii; |
| 570 | max_correlation = abs(correlation); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 571 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 572 | // d_channel_imp_resp.push_back(correlation); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 573 | chan_imp_resp[ii] = correlation; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 574 | } |
| 575 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 576 | burst_start = strongest_window_nr + chan_imp_resp_center - 48 * d_OSR - 2 * d_OSR + 2 + SYNC_POS * d_OSR; |
| 577 | return burst_start; |
| 578 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 579 | |
| 580 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 581 | |
| 582 | void receiver_impl::detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary) |
| 583 | { |
| 584 | float output[BURST_SIZE]; |
| 585 | gr_complex rhh_temp[CHAN_IMP_RESP_LENGTH*d_OSR]; |
| 586 | gr_complex rhh[CHAN_IMP_RESP_LENGTH]; |
| 587 | gr_complex filtered_burst[BURST_SIZE]; |
| 588 | int start_state = 3; |
| 589 | unsigned int stop_states[2] = {4, 12}; |
| 590 | |
| 591 | autocorrelation(chan_imp_resp, rhh_temp, d_chan_imp_length*d_OSR); |
| 592 | for (int ii = 0; ii < (d_chan_imp_length); ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 593 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 594 | rhh[ii] = conj(rhh_temp[ii*d_OSR]); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 595 | } |
| 596 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 597 | mafi(&input[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst); |
| 598 | |
| 599 | viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output); |
| 600 | |
| 601 | for (int i = 0; i < BURST_SIZE ; i++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 602 | { |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 603 | output_binary[i] = (output[i] > 0); |
| 604 | } |
| 605 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 606 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 607 | void receiver_impl::gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point) |
| 608 | { |
| 609 | gr_complex j = gr_complex(0.0, 1.0); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 610 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 611 | int current_symbol; |
| 612 | int encoded_symbol; |
| 613 | int previous_symbol = 2 * input[0] - 1; |
| 614 | gmsk_output[0] = start_point; |
| 615 | |
| 616 | for (int i = 1; i < nitems; i++) |
| 617 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 618 | //change bits representation to NRZ |
| 619 | current_symbol = 2 * input[i] - 1; |
| 620 | //differentially encode |
| 621 | encoded_symbol = current_symbol * previous_symbol; |
| 622 | //and do gmsk mapping |
| 623 | gmsk_output[i] = j * gr_complex(encoded_symbol, 0.0) * gmsk_output[i-1]; |
| 624 | previous_symbol = current_symbol; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 625 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 626 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 627 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 628 | gr_complex receiver_impl::correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input) |
| 629 | { |
| 630 | gr_complex result(0.0, 0.0); |
| 631 | int sample_number = 0; |
| 632 | |
| 633 | for (int ii = 0; ii < length; ii++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 634 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 635 | sample_number = (ii * d_OSR) ; |
| 636 | result += sequence[ii] * conj(input[sample_number]); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 637 | } |
| 638 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 639 | result = result / gr_complex(length, 0); |
| 640 | return result; |
| 641 | } |
| 642 | |
| 643 | //computes autocorrelation for positive arguments |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 644 | inline void receiver_impl::autocorrelation(const gr_complex * input, gr_complex * out, int nitems) |
| 645 | { |
| 646 | int i, k; |
| 647 | for (k = nitems - 1; k >= 0; k--) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 648 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 649 | out[k] = gr_complex(0, 0); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 650 | for (i = k; i < nitems; i++) |
| 651 | { |
| 652 | out[k] += input[i] * conj(input[i-k]); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 653 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 654 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 655 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 656 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 657 | inline void receiver_impl::mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output) |
| 658 | { |
| 659 | int ii = 0, n, a; |
| 660 | |
| 661 | for (n = 0; n < nitems; n++) |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 662 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 663 | a = n * d_OSR; |
| 664 | output[n] = 0; |
| 665 | ii = 0; |
| 666 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 667 | while (ii < filter_length) |
| 668 | { |
piotr | da8a066 | 2014-04-24 10:29:38 +0200 | [diff] [blame] | 669 | if ((a + ii) >= nitems*d_OSR){ |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 670 | break; |
piotr | da8a066 | 2014-04-24 10:29:38 +0200 | [diff] [blame] | 671 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 672 | output[n] += input[a+ii] * filter[ii]; |
| 673 | ii++; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 674 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 675 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 676 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 677 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 678 | //especially computations of strongest_window_nr |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 679 | int receiver_impl::get_norm_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp, float *corr_max, int bcc) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 680 | { |
| 681 | vector_complex correlation_buffer; |
| 682 | vector_float power_buffer; |
| 683 | vector_float window_energy_buffer; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 684 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 685 | int strongest_window_nr; |
| 686 | int burst_start = 0; |
| 687 | int chan_imp_resp_center = 0; |
| 688 | float max_correlation = 0; |
| 689 | float energy = 0; |
piotr | 5c82025 | 2014-04-17 09:43:02 +0200 | [diff] [blame] | 690 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 691 | int search_center = (int)((TRAIN_POS + GUARD_PERIOD) * d_OSR); |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 692 | int search_start_pos = search_center + 1 - 5*d_OSR; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 693 | // int search_start_pos = search_center - d_chan_imp_length * d_OSR; |
piotr | 5c82025 | 2014-04-17 09:43:02 +0200 | [diff] [blame] | 694 | int search_stop_pos = search_center + d_chan_imp_length * d_OSR + 5 * d_OSR; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 695 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 696 | for (int ii = search_start_pos; ii < search_stop_pos; ii++) |
| 697 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 698 | gr_complex correlation = correlate_sequence(&d_norm_training_seq[bcc][TRAIN_BEGINNING], N_TRAIN_BITS - 10, &input[ii]); |
| 699 | |
| 700 | correlation_buffer.push_back(correlation); |
| 701 | power_buffer.push_back(std::pow(abs(correlation), 2)); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 702 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 703 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 704 | //compute window energies |
| 705 | vector_float::iterator iter = power_buffer.begin(); |
| 706 | bool loop_end = false; |
| 707 | while (iter != power_buffer.end()) |
| 708 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 709 | vector_float::iterator iter_ii = iter; |
| 710 | energy = 0; |
| 711 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 712 | for (int ii = 0; ii < (d_chan_imp_length - 2)*d_OSR; ii++, iter_ii++) |
| 713 | { |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 714 | if (iter_ii == power_buffer.end()) |
| 715 | { |
| 716 | loop_end = true; |
| 717 | break; |
| 718 | } |
| 719 | energy += (*iter_ii); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 720 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 721 | if (loop_end) |
| 722 | { |
| 723 | break; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 724 | } |
| 725 | iter++; |
| 726 | |
| 727 | window_energy_buffer.push_back(energy); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 728 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 729 | |
piotr | 5c82025 | 2014-04-17 09:43:02 +0200 | [diff] [blame] | 730 | strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()-((d_chan_imp_length)*d_OSR)) - window_energy_buffer.begin(); |
| 731 | //strongest_window_nr = strongest_window_nr-d_OSR; |
| 732 | if(strongest_window_nr<0){ |
| 733 | strongest_window_nr = 0; |
| 734 | } |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 735 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 736 | max_correlation = 0; |
| 737 | for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++) |
| 738 | { |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 739 | gr_complex correlation = correlation_buffer[strongest_window_nr + ii]; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 740 | if (abs(correlation) > max_correlation) |
| 741 | { |
| 742 | chan_imp_resp_center = ii; |
| 743 | max_correlation = abs(correlation); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 744 | } |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 745 | // d_channel_imp_resp.push_back(correlation); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 746 | chan_imp_resp[ii] = correlation; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 747 | } |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 748 | |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 749 | *corr_max = max_correlation; |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 750 | |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 751 | DCOUT("strongest_window_nr_new: " << strongest_window_nr); |
piotr | c7c249a | 2014-05-02 17:24:08 +0200 | [diff] [blame] | 752 | burst_start = search_start_pos + strongest_window_nr - TRAIN_POS * d_OSR; //compute first sample posiiton which corresponds to the first sample of the impulse response |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 753 | |
| 754 | DCOUT("burst_start: " << burst_start); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 755 | return burst_start; |
| 756 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 757 | |
| 758 | |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 759 | void receiver_impl::send_burst(burst_counter burst_nr, const unsigned char * burst_binary, burst_type b_type) |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 760 | { |
piotr | 7c82b17 | 2014-02-08 14:15:27 +0100 | [diff] [blame] | 761 | |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 762 | boost::scoped_ptr<gsmtap_hdr> tap_header(new gsmtap_hdr()); |
| 763 | |
| 764 | tap_header->version = GSMTAP_VERSION; |
| 765 | tap_header->hdr_len = BURST_SIZE/4; |
| 766 | tap_header->type = GSMTAP_TYPE_UM_BURST; |
| 767 | tap_header->timeslot = static_cast<uint8_t>(d_burst_nr.get_timeslot_nr()); |
| 768 | tap_header->frame_number = d_burst_nr.get_frame_nr(); |
| 769 | tap_header->sub_type = static_cast<uint8_t>(b_type); |
| 770 | tap_header->arfcn = d_arfcn; |
| 771 | tap_header->signal_dbm = static_cast<int8_t>(d_signal_dbm); |
| 772 | pmt::pmt_t header_blob=pmt::make_blob(tap_header.get(),sizeof(gsmtap_hdr)); |
| 773 | pmt::pmt_t burst_binary_blob=pmt::make_blob(burst_binary,BURST_SIZE); |
| 774 | pmt::pmt_t msg = pmt::cons(header_blob, burst_binary_blob); |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 775 | |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 776 | message_port_pub(pmt::mp("bursts"), msg); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 777 | } |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 778 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 779 | void receiver_impl::configure_receiver() |
| 780 | { |
piotr | ce92f98 | 2014-04-17 23:37:18 +0200 | [diff] [blame] | 781 | d_channel_conf.set_multiframe_type(TIMESLOT0, multiframe_51); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 782 | d_channel_conf.set_burst_types(TIMESLOT0, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 783 | |
piotr | ce92f98 | 2014-04-17 23:37:18 +0200 | [diff] [blame] | 784 | d_channel_conf.set_burst_types(TIMESLOT0, TEST_CCH_FRAMES, sizeof(TEST_CCH_FRAMES) / sizeof(unsigned), dummy_or_normal); |
| 785 | d_channel_conf.set_burst_types(TIMESLOT0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst); |
| 786 | d_channel_conf.set_burst_types(TIMESLOT0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 787 | |
| 788 | // d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_26); |
| 789 | // d_channel_conf.set_burst_types(TIMESLOT1, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 790 | // d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_26); |
| 791 | // d_channel_conf.set_burst_types(TIMESLOT2, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 792 | // d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_26); |
| 793 | // d_channel_conf.set_burst_types(TIMESLOT3, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 794 | // d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_26); |
| 795 | // d_channel_conf.set_burst_types(TIMESLOT4, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 796 | // d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_26); |
| 797 | // d_channel_conf.set_burst_types(TIMESLOT5, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 798 | // d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_26); |
| 799 | // d_channel_conf.set_burst_types(TIMESLOT6, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
| 800 | // d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_26); |
| 801 | // d_channel_conf.set_burst_types(TIMESLOT7, TRAFFIC_CHANNEL_F, sizeof(TRAFFIC_CHANNEL_F) / sizeof(unsigned), dummy_or_normal); |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 802 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 803 | d_channel_conf.set_multiframe_type(TIMESLOT1, multiframe_51); |
| 804 | d_channel_conf.set_burst_types(TIMESLOT1, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 805 | d_channel_conf.set_multiframe_type(TIMESLOT2, multiframe_51); |
| 806 | d_channel_conf.set_burst_types(TIMESLOT2, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 807 | d_channel_conf.set_multiframe_type(TIMESLOT3, multiframe_51); |
| 808 | d_channel_conf.set_burst_types(TIMESLOT3, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 809 | d_channel_conf.set_multiframe_type(TIMESLOT4, multiframe_51); |
| 810 | d_channel_conf.set_burst_types(TIMESLOT4, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 811 | d_channel_conf.set_multiframe_type(TIMESLOT5, multiframe_51); |
| 812 | d_channel_conf.set_burst_types(TIMESLOT5, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 813 | d_channel_conf.set_multiframe_type(TIMESLOT6, multiframe_51); |
| 814 | d_channel_conf.set_burst_types(TIMESLOT6, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
| 815 | d_channel_conf.set_multiframe_type(TIMESLOT7, multiframe_51); |
| 816 | d_channel_conf.set_burst_types(TIMESLOT7, TEST51, sizeof(TEST51) / sizeof(unsigned), dummy_or_normal); |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 817 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 818 | |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 819 | void receiver_impl::set_arfcn(int arfcn) //!! |
| 820 | { |
| 821 | d_arfcn = arfcn; |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | void receiver_impl::reset() |
| 825 | { |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 826 | d_state = fcch_search; |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 827 | } |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 828 | |
piotr | d0bf149 | 2014-02-05 17:27:32 +0100 | [diff] [blame] | 829 | } /* namespace gsm */ |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 830 | } /* namespace gr */ |
| 831 | |