piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* |
| 3 | * Copyright 2014 <+YOU OR YOUR COMPANY+>. |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 | #ifndef INCLUDED_GSM_RECEIVER_IMPL_H |
| 22 | #define INCLUDED_GSM_RECEIVER_IMPL_H |
| 23 | |
| 24 | #include <gsm/receiver.h> |
| 25 | #include <gsm_constants.h> |
| 26 | #include <receiver_config.h> |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 27 | #include <gsmtap.h> |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 28 | |
| 29 | namespace gr { |
| 30 | namespace gsm { |
| 31 | |
| 32 | typedef std::vector<gr_complex> vector_complex; |
| 33 | |
| 34 | class receiver_impl : public receiver |
| 35 | { |
| 36 | private: |
| 37 | /**@name Configuration of the receiver */ |
| 38 | //@{ |
| 39 | const int d_OSR; ///< oversampling ratio |
| 40 | const int d_chan_imp_length; ///< channel impulse length |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 41 | uint16_t d_arfcn; |
| 42 | int8_t d_signal_dbm; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 43 | //@} |
| 44 | |
| 45 | gr_complex d_sch_training_seq[N_SYNC_BITS]; ///<encoded training sequence of a SCH burst |
| 46 | gr_complex d_norm_training_seq[TRAIN_SEQ_NUM][N_TRAIN_BITS]; ///<encoded training sequences of a normal bursts and dummy bursts |
| 47 | |
| 48 | feval_dd *d_tuner; ///<callback to a python object which is used for frequency tunning |
| 49 | |
| 50 | /** Counts samples consumed by the receiver |
| 51 | * |
| 52 | * It is used in beetween find_fcch_burst and reach_sch_burst calls. |
| 53 | * My intention was to synchronize this counter with some internal sample |
| 54 | * counter of the USRP. Simple access to such USRP's counter isn't possible |
| 55 | * so this variable isn't used in the "synchronized" state of the receiver yet. |
| 56 | */ |
| 57 | unsigned d_counter; |
| 58 | |
| 59 | /**@name Variables used to store result of the find_fcch_burst fuction */ |
| 60 | //@{ |
| 61 | unsigned d_fcch_start_pos; ///< position of the first sample of the fcch burst |
| 62 | float d_freq_offset; ///< frequency offset of the received signal |
| 63 | //@} |
| 64 | std::list<double> d_freq_offset_vals; |
| 65 | |
| 66 | /**@name Identifiers of the BTS extracted from the SCH burst */ |
| 67 | //@{ |
| 68 | int d_ncc; ///< network color code |
| 69 | int d_bcc; ///< base station color code |
| 70 | //@} |
| 71 | |
| 72 | /**@name Internal state of the gsm receiver */ |
| 73 | //@{ |
| 74 | enum states { |
| 75 | first_fcch_search, next_fcch_search, sch_search, // synchronization search part |
| 76 | synchronized // receiver is synchronized in this state |
| 77 | } d_state; |
| 78 | //@} |
| 79 | |
| 80 | /**@name Variables which make internal state in the "synchronized" state */ |
| 81 | //@{ |
| 82 | burst_counter d_burst_nr; ///< frame number and timeslot number |
| 83 | channel_configuration d_channel_conf; ///< mapping of burst_counter to burst_type |
| 84 | //@} |
| 85 | |
| 86 | unsigned d_failed_sch; ///< number of subsequent erroneous SCH bursts |
| 87 | |
| 88 | /** Function whis is used to search a FCCH burst and to compute frequency offset before |
| 89 | * "synchronized" state of the receiver |
| 90 | * |
| 91 | * TODO: Describe the FCCH search algorithm in the documentation |
| 92 | * @param input vector with input signal |
| 93 | * @param nitems number of samples in the input vector |
| 94 | * @return |
| 95 | */ |
| 96 | bool find_fcch_burst(const gr_complex *input, const int nitems); |
| 97 | |
| 98 | /** Computes frequency offset from FCCH burst samples |
| 99 | * |
| 100 | * @param input vector with input samples |
| 101 | * @param first_sample number of the first sample of the FCCH busrt |
| 102 | * @param last_sample number of the last sample of the FCCH busrt |
| 103 | * @return frequency offset |
| 104 | */ |
| 105 | double compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample); |
| 106 | |
| 107 | /** Calls d_tuner's method to set frequency offset from Python level |
| 108 | * |
| 109 | * @param freq_offset absolute frequency offset of the received signal |
| 110 | */ |
| 111 | void set_frequency(double freq_offset); |
| 112 | |
| 113 | /** Computes angle between two complex numbers |
| 114 | * |
| 115 | * @param val1 first complex number |
| 116 | * @param val2 second complex number |
| 117 | * @return |
| 118 | */ |
| 119 | inline float compute_phase_diff(gr_complex val1, gr_complex val2); |
| 120 | |
| 121 | /** Function whis is used to get near to SCH burst |
| 122 | * |
| 123 | * @param nitems number of samples in the gsm_receiver's buffer |
| 124 | * @return true if SCH burst is near, false otherwise |
| 125 | */ |
| 126 | bool reach_sch_burst(const int nitems); |
| 127 | |
| 128 | /** Extracts channel impulse response from a SCH burst and computes first sample number of this burst |
| 129 | * |
| 130 | * @param input vector with input samples |
| 131 | * @param chan_imp_resp complex vector where channel impulse response will be stored |
| 132 | * @return number of first sample of the burst |
| 133 | */ |
| 134 | int get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp); |
| 135 | |
| 136 | /** MLSE detection of a burst bits |
| 137 | * |
| 138 | * Detects bits of burst using viterbi algorithm. |
| 139 | * @param input vector with input samples |
| 140 | * @param chan_imp_resp vector with the channel impulse response |
| 141 | * @param burst_start number of the first sample of the burst |
| 142 | * @param output_binary vector with output bits |
| 143 | */ |
| 144 | void detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary); |
| 145 | |
| 146 | /** Encodes differentially input bits and maps them into MSK states |
| 147 | * |
| 148 | * @param input vector with input bits |
| 149 | * @param nitems number of samples in the "input" vector |
| 150 | * @param gmsk_output bits mapped into MSK states |
| 151 | * @param start_point first state |
| 152 | */ |
| 153 | void gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point); |
| 154 | |
| 155 | /** Correlates MSK mapped sequence with input signal |
| 156 | * |
| 157 | * @param sequence MKS mapped sequence |
| 158 | * @param length length of the sequence |
| 159 | * @param input_signal vector with input samples |
| 160 | * @return correlation value |
| 161 | */ |
| 162 | gr_complex correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input); |
| 163 | |
| 164 | /** Computes autocorrelation of input vector for positive arguments |
| 165 | * |
| 166 | * @param input vector with input samples |
| 167 | * @param out output vector |
| 168 | * @param nitems length of the input vector |
| 169 | */ |
| 170 | inline void autocorrelation(const gr_complex * input, gr_complex * out, int nitems); |
| 171 | |
| 172 | /** Filters input signal through channel impulse response |
| 173 | * |
| 174 | * @param input vector with input samples |
| 175 | * @param nitems number of samples to pass through filter |
| 176 | * @param filter filter taps - channel impulse response |
| 177 | * @param filter_length nember of filter taps |
| 178 | * @param output vector with filtered samples |
| 179 | */ |
| 180 | inline void mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output); |
| 181 | |
| 182 | /** Extracts channel impulse response from a normal burst and computes first sample number of this burst |
| 183 | * |
| 184 | * @param input vector with input samples |
| 185 | * @param chan_imp_resp complex vector where channel impulse response will be stored |
| 186 | * @param search_range possible absolute offset of a channel impulse response start |
| 187 | * @param bcc base station color code - number of a training sequence |
| 188 | * @return first sample number of normal burst |
| 189 | */ |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 190 | int get_norm_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp, float *corr_max, int bcc); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * |
| 194 | */ |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 195 | void send_burst(burst_counter burst_nr, const unsigned char * burst_binary, burst_type b_type); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * |
| 199 | */ |
| 200 | void configure_receiver(); |
| 201 | |
| 202 | public: |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 203 | receiver_impl(feval_dd * tuner, int osr, int arfcn); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 204 | ~receiver_impl(); |
| 205 | |
| 206 | void forecast(int noutput_items, gr_vector_int &ninput_items_required); |
| 207 | |
| 208 | // Where all the action really happens |
| 209 | int general_work(int noutput_items, |
| 210 | gr_vector_int &ninput_items, |
| 211 | gr_vector_const_void_star &input_items, |
| 212 | gr_vector_void_star &output_items); |
| 213 | }; |
| 214 | } // namespace gsm |
| 215 | } // namespace gr |
| 216 | |
| 217 | #endif /* INCLUDED_GSM_RECEIVER_IMPL_H */ |
| 218 | |