piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 2 | /* |
| 3 | * @file |
| 4 | * @author Piotr Krysik <ptrkrysik@gmail.com> |
| 5 | * @section LICENSE |
| 6 | * |
| 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. |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 11 | * |
| 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. |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +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 | #ifndef INCLUDED_GSM_RECEIVER_IMPL_H |
| 24 | #define INCLUDED_GSM_RECEIVER_IMPL_H |
| 25 | |
ptrkrysik | 3be74a7 | 2014-12-13 10:11:00 +0100 | [diff] [blame] | 26 | #include <grgsm/receiver/receiver.h> |
| 27 | #include <grgsm/gsmtap.h> |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 28 | #include <gsm_constants.h> |
| 29 | #include <receiver_config.h> |
| 30 | |
| 31 | namespace gr { |
| 32 | namespace gsm { |
| 33 | |
| 34 | typedef std::vector<gr_complex> vector_complex; |
| 35 | |
| 36 | class receiver_impl : public receiver |
| 37 | { |
| 38 | private: |
ptrkrysik | 5821379 | 2014-10-30 09:05:15 +0100 | [diff] [blame] | 39 | unsigned int d_c0_burst_start; |
| 40 | float d_c0_signal_dbm; |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 41 | /**@name Configuration of the receiver */ |
| 42 | //@{ |
| 43 | const int d_OSR; ///< oversampling ratio |
| 44 | const int d_chan_imp_length; ///< channel impulse length |
ptrkrysik | 5821379 | 2014-10-30 09:05:15 +0100 | [diff] [blame] | 45 | float d_signal_dbm; |
ptrkrysik | e518bbf | 2014-11-06 14:50:59 +0100 | [diff] [blame] | 46 | std::vector<int> d_tseq_nums; ///< stores training sequence numbers for channels different than C0 |
ptrkrysik | 7a7b9b0 | 2014-11-19 11:27:34 +0100 | [diff] [blame] | 47 | std::vector<int> d_cell_allocation; ///< stores cell allocation - absolute rf channel numbers (ARFCNs) assigned to the given cell. The variable should at least contain C0 channel number. |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 48 | //@} |
| 49 | |
| 50 | gr_complex d_sch_training_seq[N_SYNC_BITS]; ///<encoded training sequence of a SCH burst |
ptrkrysik | 5821379 | 2014-10-30 09:05:15 +0100 | [diff] [blame] | 51 | gr_complex d_norm_training_seq[TRAIN_SEQ_NUM][N_TRAIN_BITS]; ///<encoded training sequences of a normal and dummy burst |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 52 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 53 | /** Counts samples consumed by the receiver |
| 54 | * |
| 55 | * It is used in beetween find_fcch_burst and reach_sch_burst calls. |
| 56 | * My intention was to synchronize this counter with some internal sample |
| 57 | * counter of the USRP. Simple access to such USRP's counter isn't possible |
| 58 | * so this variable isn't used in the "synchronized" state of the receiver yet. |
| 59 | */ |
| 60 | unsigned d_counter; |
| 61 | |
| 62 | /**@name Variables used to store result of the find_fcch_burst fuction */ |
| 63 | //@{ |
| 64 | 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] | 65 | float d_freq_offset_setting; ///< frequency offset set in frequency shifter located upstream |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 66 | //@} |
| 67 | std::list<double> d_freq_offset_vals; |
| 68 | |
| 69 | /**@name Identifiers of the BTS extracted from the SCH burst */ |
| 70 | //@{ |
| 71 | int d_ncc; ///< network color code |
| 72 | int d_bcc; ///< base station color code |
| 73 | //@} |
| 74 | |
| 75 | /**@name Internal state of the gsm receiver */ |
| 76 | //@{ |
| 77 | enum states { |
piotr | d6d6687 | 2014-08-06 15:20:33 +0200 | [diff] [blame] | 78 | fcch_search, sch_search, // synchronization search part |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 79 | synchronized // receiver is synchronized in this state |
| 80 | } d_state; |
| 81 | //@} |
| 82 | |
| 83 | /**@name Variables which make internal state in the "synchronized" state */ |
| 84 | //@{ |
| 85 | burst_counter d_burst_nr; ///< frame number and timeslot number |
| 86 | channel_configuration d_channel_conf; ///< mapping of burst_counter to burst_type |
| 87 | //@} |
ptrkrysik | e518bbf | 2014-11-06 14:50:59 +0100 | [diff] [blame] | 88 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 89 | unsigned d_failed_sch; ///< number of subsequent erroneous SCH bursts |
| 90 | |
| 91 | /** Function whis is used to search a FCCH burst and to compute frequency offset before |
| 92 | * "synchronized" state of the receiver |
| 93 | * |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 94 | * @param input vector with input signal |
| 95 | * @param nitems number of samples in the input vector |
| 96 | * @return |
| 97 | */ |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 98 | 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] | 99 | |
| 100 | /** Computes frequency offset from FCCH burst samples |
| 101 | * |
piotr | 4089c1a | 2014-08-06 14:10:56 +0200 | [diff] [blame] | 102 | * @param[in] input vector with input samples |
| 103 | * @param[in] first_sample number of the first sample of the FCCH busrt |
| 104 | * @param[in] last_sample number of the last sample of the FCCH busrt |
| 105 | * @param[out] computed_freq_offset contains frequency offset estimate if FCCH burst was located |
| 106 | * @return true if frequency offset was faound |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 107 | */ |
| 108 | double compute_freq_offset(const gr_complex * input, unsigned first_sample, unsigned last_sample); |
| 109 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 110 | /** Computes angle between two complex numbers |
| 111 | * |
| 112 | * @param val1 first complex number |
| 113 | * @param val2 second complex number |
| 114 | * @return |
| 115 | */ |
| 116 | inline float compute_phase_diff(gr_complex val1, gr_complex val2); |
| 117 | |
| 118 | /** Function whis is used to get near to SCH burst |
| 119 | * |
| 120 | * @param nitems number of samples in the gsm_receiver's buffer |
| 121 | * @return true if SCH burst is near, false otherwise |
| 122 | */ |
| 123 | bool reach_sch_burst(const int nitems); |
| 124 | |
| 125 | /** Extracts channel impulse response from a SCH burst and computes first sample number of this burst |
| 126 | * |
| 127 | * @param input vector with input samples |
| 128 | * @param chan_imp_resp complex vector where channel impulse response will be stored |
| 129 | * @return number of first sample of the burst |
| 130 | */ |
| 131 | int get_sch_chan_imp_resp(const gr_complex *input, gr_complex * chan_imp_resp); |
| 132 | |
| 133 | /** MLSE detection of a burst bits |
| 134 | * |
| 135 | * Detects bits of burst using viterbi algorithm. |
| 136 | * @param input vector with input samples |
| 137 | * @param chan_imp_resp vector with the channel impulse response |
| 138 | * @param burst_start number of the first sample of the burst |
| 139 | * @param output_binary vector with output bits |
| 140 | */ |
| 141 | void detect_burst(const gr_complex * input, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary); |
| 142 | |
| 143 | /** Encodes differentially input bits and maps them into MSK states |
| 144 | * |
| 145 | * @param input vector with input bits |
| 146 | * @param nitems number of samples in the "input" vector |
| 147 | * @param gmsk_output bits mapped into MSK states |
| 148 | * @param start_point first state |
| 149 | */ |
| 150 | void gmsk_mapper(const unsigned char * input, int nitems, gr_complex * gmsk_output, gr_complex start_point); |
| 151 | |
| 152 | /** Correlates MSK mapped sequence with input signal |
| 153 | * |
| 154 | * @param sequence MKS mapped sequence |
| 155 | * @param length length of the sequence |
| 156 | * @param input_signal vector with input samples |
| 157 | * @return correlation value |
| 158 | */ |
| 159 | gr_complex correlate_sequence(const gr_complex * sequence, int length, const gr_complex * input); |
| 160 | |
| 161 | /** Computes autocorrelation of input vector for positive arguments |
| 162 | * |
| 163 | * @param input vector with input samples |
| 164 | * @param out output vector |
| 165 | * @param nitems length of the input vector |
| 166 | */ |
| 167 | inline void autocorrelation(const gr_complex * input, gr_complex * out, int nitems); |
| 168 | |
| 169 | /** Filters input signal through channel impulse response |
| 170 | * |
| 171 | * @param input vector with input samples |
| 172 | * @param nitems number of samples to pass through filter |
| 173 | * @param filter filter taps - channel impulse response |
| 174 | * @param filter_length nember of filter taps |
| 175 | * @param output vector with filtered samples |
| 176 | */ |
| 177 | inline void mafi(const gr_complex * input, int nitems, gr_complex * filter, int filter_length, gr_complex * output); |
| 178 | |
| 179 | /** Extracts channel impulse response from a normal burst and computes first sample number of this burst |
| 180 | * |
| 181 | * @param input vector with input samples |
| 182 | * @param chan_imp_resp complex vector where channel impulse response will be stored |
| 183 | * @param search_range possible absolute offset of a channel impulse response start |
| 184 | * @param bcc base station color code - number of a training sequence |
| 185 | * @return first sample number of normal burst |
| 186 | */ |
piotr | 7e3b0db | 2014-02-05 22:44:30 +0100 | [diff] [blame] | 187 | 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] | 188 | |
| 189 | /** |
ptrkrysik | e518bbf | 2014-11-06 14:50:59 +0100 | [diff] [blame] | 190 | * Sends burst through a C0 (for burst from C0 channel) or Cx (for other bursts) message port |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 191 | * |
ptrkrysik | e518bbf | 2014-11-06 14:50:59 +0100 | [diff] [blame] | 192 | * @param burst_nr - frame number of the burst |
| 193 | * @param burst_binary - content of the burst |
| 194 | * @b_type - type of the burst |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 195 | */ |
ptrkrysik | 617ba03 | 2014-11-21 10:11:05 +0100 | [diff] [blame] | 196 | void send_burst(burst_counter burst_nr, const unsigned char * burst_binary, uint8_t burst_type, unsigned int input_nr); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 197 | |
| 198 | /** |
ptrkrysik | e518bbf | 2014-11-06 14:50:59 +0100 | [diff] [blame] | 199 | * Configures burst types in different channels |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 200 | */ |
| 201 | void configure_receiver(); |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 202 | |
| 203 | |
| 204 | |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 205 | public: |
ptrkrysik | 7a7b9b0 | 2014-11-19 11:27:34 +0100 | [diff] [blame] | 206 | receiver_impl(int osr, const std::vector<int> &cell_allocation, const std::vector<int> &tseq_nums); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 207 | ~receiver_impl(); |
| 208 | |
piotr | c7c249a | 2014-05-02 17:24:08 +0200 | [diff] [blame] | 209 | int work(int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items); |
ptrkrysik | 7a7b9b0 | 2014-11-19 11:27:34 +0100 | [diff] [blame] | 210 | virtual void set_cell_allocation(const std::vector<int> &cell_allocation); |
ptrkrysik | e518bbf | 2014-11-06 14:50:59 +0100 | [diff] [blame] | 211 | virtual void set_tseq_nums(const std::vector<int> & tseq_nums); |
piotr | f2b6a1b | 2014-08-04 11:28:59 +0200 | [diff] [blame] | 212 | virtual void reset(); |
piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 213 | }; |
| 214 | } // namespace gsm |
| 215 | } // namespace gr |
| 216 | |
| 217 | #endif /* INCLUDED_GSM_RECEIVER_IMPL_H */ |
| 218 | |