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> |
piotr | faacc72 | 2014-07-20 23:48:32 +0200 | [diff] [blame] | 25 | #include <gsm/gsmtap.h> |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 26 | #include <gsm_constants.h> |
| 27 | #include <receiver_config.h> |
| 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 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 48 | /** Counts samples consumed by the receiver |
| 49 | * |
| 50 | * It is used in beetween find_fcch_burst and reach_sch_burst calls. |
| 51 | * My intention was to synchronize this counter with some internal sample |
| 52 | * counter of the USRP. Simple access to such USRP's counter isn't possible |
| 53 | * so this variable isn't used in the "synchronized" state of the receiver yet. |
| 54 | */ |
| 55 | unsigned d_counter; |
| 56 | |
| 57 | /**@name Variables used to store result of the find_fcch_burst fuction */ |
| 58 | //@{ |
| 59 | unsigned d_fcch_start_pos; ///< position of the first sample of the fcch burst |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 60 | float d_freq_offset_setting; ///< frequency offset set in frequency shifter located upstream |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 61 | //@} |
| 62 | std::list<double> d_freq_offset_vals; |
| 63 | |
| 64 | /**@name Identifiers of the BTS extracted from the SCH burst */ |
| 65 | //@{ |
| 66 | int d_ncc; ///< network color code |
| 67 | int d_bcc; ///< base station color code |
| 68 | //@} |
| 69 | |
| 70 | /**@name Internal state of the gsm receiver */ |
| 71 | //@{ |
| 72 | enum states { |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 73 | fcch_search, sch_search, // synchronization search part |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 74 | synchronized // receiver is synchronized in this state |
| 75 | } d_state; |
| 76 | //@} |
| 77 | |
| 78 | /**@name Variables which make internal state in the "synchronized" state */ |
| 79 | //@{ |
| 80 | burst_counter d_burst_nr; ///< frame number and timeslot number |
| 81 | channel_configuration d_channel_conf; ///< mapping of burst_counter to burst_type |
| 82 | //@} |
| 83 | |
| 84 | unsigned d_failed_sch; ///< number of subsequent erroneous SCH bursts |
| 85 | |
| 86 | /** Function whis is used to search a FCCH burst and to compute frequency offset before |
| 87 | * "synchronized" state of the receiver |
| 88 | * |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 89 | * @param input vector with input signal |
| 90 | * @param nitems number of samples in the input vector |
| 91 | * @return |
| 92 | */ |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 93 | bool find_fcch_burst(const gr_complex *input, const int nitems, double & computed_freq_offset); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 94 | |
| 95 | /** Computes frequency offset from FCCH burst samples |
| 96 | * |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 97 | * @param[in] input vector with input samples |
| 98 | * @param[in] first_sample number of the first sample of the FCCH busrt |
| 99 | * @param[in] last_sample number of the last sample of the FCCH busrt |
| 100 | * @param[out] computed_freq_offset contains frequency offset estimate if FCCH burst was located |
| 101 | * @return true if frequency offset was faound |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 102 | */ |
| 103 | double compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample); |
| 104 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 105 | /** Computes angle between two complex numbers |
| 106 | * |
| 107 | * @param val1 first complex number |
| 108 | * @param val2 second complex number |
| 109 | * @return |
| 110 | */ |
| 111 | inline float compute_phase_diff(gr_complex val1, gr_complex val2); |
| 112 | |
| 113 | /** Function whis is used to get near to SCH burst |
| 114 | * |
| 115 | * @param nitems number of samples in the gsm_receiver's buffer |
| 116 | * @return true if SCH burst is near, false otherwise |
| 117 | */ |
| 118 | bool reach_sch_burst(const int nitems); |
| 119 | |
| 120 | /** Extracts channel impulse response from a SCH burst and computes first sample number of this burst |
| 121 | * |
| 122 | * @param input vector with input samples |
| 123 | * @param chan_imp_resp complex vector where channel impulse response will be stored |
| 124 | * @return number of first sample of the burst |
| 125 | */ |
| 126 | int get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp); |
| 127 | |
| 128 | /** MLSE detection of a burst bits |
| 129 | * |
| 130 | * Detects bits of burst using viterbi algorithm. |
| 131 | * @param input vector with input samples |
| 132 | * @param chan_imp_resp vector with the channel impulse response |
| 133 | * @param burst_start number of the first sample of the burst |
| 134 | * @param output_binary vector with output bits |
| 135 | */ |
| 136 | void detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary); |
| 137 | |
| 138 | /** Encodes differentially input bits and maps them into MSK states |
| 139 | * |
| 140 | * @param input vector with input bits |
| 141 | * @param nitems number of samples in the "input" vector |
| 142 | * @param gmsk_output bits mapped into MSK states |
| 143 | * @param start_point first state |
| 144 | */ |
| 145 | void gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point); |
| 146 | |
| 147 | /** Correlates MSK mapped sequence with input signal |
| 148 | * |
| 149 | * @param sequence MKS mapped sequence |
| 150 | * @param length length of the sequence |
| 151 | * @param input_signal vector with input samples |
| 152 | * @return correlation value |
| 153 | */ |
| 154 | gr_complex correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input); |
| 155 | |
| 156 | /** Computes autocorrelation of input vector for positive arguments |
| 157 | * |
| 158 | * @param input vector with input samples |
| 159 | * @param out output vector |
| 160 | * @param nitems length of the input vector |
| 161 | */ |
| 162 | inline void autocorrelation(const gr_complex * input, gr_complex * out, int nitems); |
| 163 | |
| 164 | /** Filters input signal through channel impulse response |
| 165 | * |
| 166 | * @param input vector with input samples |
| 167 | * @param nitems number of samples to pass through filter |
| 168 | * @param filter filter taps - channel impulse response |
| 169 | * @param filter_length nember of filter taps |
| 170 | * @param output vector with filtered samples |
| 171 | */ |
| 172 | inline void mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output); |
| 173 | |
| 174 | /** Extracts channel impulse response from a normal burst and computes first sample number of this burst |
| 175 | * |
| 176 | * @param input vector with input samples |
| 177 | * @param chan_imp_resp complex vector where channel impulse response will be stored |
| 178 | * @param search_range possible absolute offset of a channel impulse response start |
| 179 | * @param bcc base station color code - number of a training sequence |
| 180 | * @return first sample number of normal burst |
| 181 | */ |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 182 | 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] | 183 | |
| 184 | /** |
| 185 | * |
| 186 | */ |
piotr | 6d152d9 | 2014-02-21 00:02:44 +0100 | [diff] [blame] | 187 | 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] | 188 | |
| 189 | /** |
| 190 | * |
| 191 | */ |
| 192 | void configure_receiver(); |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 193 | |
| 194 | |
| 195 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 196 | public: |
Piotr K | 608a08e | 2014-08-07 17:01:55 +0200 | [diff] [blame] | 197 | receiver_impl(int osr, int arfcn); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 198 | ~receiver_impl(); |
| 199 | |
piotr | c7c249a | 2014-05-02 17:24:08 +0200 | [diff] [blame] | 200 | int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items); |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 201 | virtual void set_arfcn(int arfcn); |
| 202 | virtual void reset(); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 203 | }; |
| 204 | } // namespace gsm |
| 205 | } // namespace gr |
| 206 | |
| 207 | #endif /* INCLUDED_GSM_RECEIVER_IMPL_H */ |
| 208 | |