GNU Radio's LORA_SDR Package
frame_sync_impl.h
Go to the documentation of this file.
1 
2 #ifndef INCLUDED_LORA_SDR_FRAME_SYNC_IMPL_H
3 #define INCLUDED_LORA_SDR_FRAME_SYNC_IMPL_H
4 // #define GRLORA_DEBUG
5 // #define PRINT_INFO
6 
8 #include <iostream>
9 #include <fstream>
10 #include <volk/volk.h>
12 #include <gnuradio/io_signature.h>
13 extern "C"
14 {
15 #include "kiss_fft.h"
16 }
17 
18 namespace gr
19 {
20  namespace lora_sdr
21  {
22 
23  class frame_sync_impl : public frame_sync
24  {
25  private:
26  enum DecoderState
27  {
28  DETECT,
29  SYNC,
30  SFO_COMPENSATION,
31  STOP
32  };
33  enum SyncState
34  {
35  NET_ID1,
36  NET_ID2,
37  DOWNCHIRP1,
38  DOWNCHIRP2,
39  QUARTER_DOWN
40  };
41  uint8_t m_state; ///< Current state of the synchronization
42  uint32_t m_center_freq; ///< RF center frequency
43  uint32_t m_bw; ///< Bandwidth
44  uint32_t m_samp_rate; ///< Sampling rate
45  uint8_t m_sf; ///< Spreading factor
46  uint8_t m_cr; ///< Coding rate
47  uint32_t m_pay_len; ///< payload length
48  uint8_t m_has_crc; ///< CRC presence
49  uint8_t m_invalid_header; ///< invalid header checksum
50  bool m_impl_head; ///< use implicit header mode
51  uint8_t m_os_factor; ///< oversampling factor
52  std::vector<uint16_t> m_sync_words; ///< vector containing the two sync words (network identifiers)
53  bool m_ldro; ///< use of low datarate optimisation mode
54 
55  uint8_t m_n_up_req; ///< number of consecutive upchirps required to trigger a detection
56 
57  uint32_t m_number_of_bins; ///< Number of bins in each lora Symbol
58  uint32_t m_samples_per_symbol; ///< Number of samples received per lora symbols
59  uint32_t m_symb_numb; ///<number of payload lora symbols
60  bool m_received_head; ///< indicate that the header has be decoded and received by this block
61  double m_noise_est; ///< estimate of the noise
62 
63  std::vector<gr_complex> in_down; ///< downsampled input
64  std::vector<gr_complex> m_downchirp; ///< Reference downchirp
65  std::vector<gr_complex> m_upchirp; ///< Reference upchirp
66 
67  unsigned int frame_cnt; ///< Number of frame received
68  int32_t symbol_cnt; ///< Number of symbols already received
69  int32_t bin_idx; ///< value of previous lora symbol
70  int32_t bin_idx_new; ///< value of newly demodulated symbol
71 
72  uint16_t m_preamb_len; ///< Number of consecutive upchirps in preamble
73  uint8_t additional_upchirps; ///< indicate the number of additional upchirps found in preamble (in addition to the minimum required to trigger a detection)
74 
75  kiss_fft_cfg m_kiss_fft_cfg; ///< FFT configuration for symbols processing
76  kiss_fft_cpx *cx_in; ///<input of the FFT
77  kiss_fft_cpx *cx_out; ///<output of the FFT
78 
79  int items_to_consume; ///< Number of items to consume after each iteration of the general_work function
80 
81  int one_symbol_off; ///< indicate that we are offset by one symbol after the preamble
82  std::vector<gr_complex> additional_symbol_samp; ///< save the value of the last 1.25 downchirp as it might contain the first payload symbol
83  std::vector<gr_complex> preamble_raw; ///<vector containing the preamble upchirps without any synchronization
84  std::vector<gr_complex> preamble_raw_up; ///<vector containing the upsampled preamble upchirps without any synchronization
85  std::vector<gr_complex> downchirp_raw; ///< vetor containing the preamble downchirps without any synchronization
86  std::vector<gr_complex> preamble_upchirps; ///<vector containing the preamble upchirps
87  std::vector<gr_complex> net_id_samp; ///< vector of the oversampled network identifier samples
88  std::vector<int> net_ids; ///< values of the network identifiers received
89 
90  int up_symb_to_use; ///< number of upchirp symbols to use for CFO and STO frac estimation
91  int k_hat; ///< integer part of CFO+STO
92  std::vector<int> preamb_up_vals; ///< value of the preamble upchirps
93 
94  float m_cfo_frac; ///< fractional part of CFO
95  float m_cfo_frac_bernier; ///< fractional part of CFO using Berniers algo
96  int m_cfo_int; ///< integer part of CFO
97  float m_sto_frac; ///< fractional part of CFO
98  float sfo_hat; ///< estimated sampling frequency offset
99  float sfo_cum; ///< cumulation of the sfo
100  bool cfo_frac_sto_frac_est; ///< indicate that the estimation of CFO_frac and STO_frac has been performed
101  std::vector<gr_complex> CFO_frac_correc; ///< cfo frac correction vector
102  std::vector<gr_complex> CFO_SFO_frac_correc; ///< correction vector accounting for cfo and sfo
103 
104  std::vector<gr_complex> symb_corr; ///< symbol with CFO frac corrected
105  int down_val; ///< value of the preamble downchirps
106  int net_id_off; ///< offset of the network identifier
107 
108  bool m_should_log; ///< indicate that the sync values should be logged
109  float off_by_one_id; ///< Indicate that the network identifiers where off by one and corrected (float used as saved in a float32 bin file)
110 #ifdef GRLORA_DEBUG
111  std::ofstream preamb_file;
112 #endif
113  // std::ofstream start_off_file;
114  // std::ofstream netid_file;
115  int my_roundf(float number);
116 
117  /**
118  * \brief Estimate the value of fractional part of the CFO using RCTSL and correct the received preamble accordingly
119  * \param samples
120  * The pointer to the preamble beginning.(We might want to avoid the
121  * first symbol since it might be incomplete)
122  */
123  float estimate_CFO_frac(gr_complex *samples);
124  /**
125  * \brief (not used) Estimate the value of fractional part of the CFO using Berniers algorithm and correct the received preamble accordingly
126  * \param samples
127  * The pointer to the preamble beginning.(We might want to avoid the
128  * first symbol since it might be incomplete)
129  */
130  float estimate_CFO_frac_Bernier(gr_complex *samples);
131  /**
132  * \brief Estimate the value of fractional part of the STO from m_consec_up and returns the estimated value
133  *
134  **/
135  float estimate_STO_frac();
136  /**
137  * \brief Recover the lora symbol value using argmax of the dechirped symbol FFT. Returns -1 in case of an fft window containing no energy to handle noiseless simulations.
138  *
139  * \param samples
140  * The pointer to the symbol beginning.
141  * \param ref_chirp
142  * The reference chirp to use to dechirp the lora symbol.
143  */
144  uint32_t get_symbol_val(const gr_complex *samples, gr_complex *ref_chirp);
145 
146 
147  /**
148  * \brief Determine the energy of a symbol.
149  *
150  * \param samples
151  * The complex symbol to analyse.
152  * length
153  * The number of LoRa symbols used for the estimation
154  */
155  float determine_energy(const gr_complex *samples, int length);
156 
157  /**
158  * \brief Handle the reception of the explicit header information, received from the header_decoder block
159  */
160  void frame_info_handler(pmt::pmt_t frame_info);
161 
162  /**
163  * \brief Handles reception of the noise estimate
164  */
165  void noise_est_handler(pmt::pmt_t noise_est);
166  /**
167  * \brief Set new SF received in a tag (used for CRAN)
168  */
169  void set_sf(int sf);
170 
171  float determine_snr(const gr_complex *samples);
172 
173  public:
174  frame_sync_impl(uint32_t center_freq, uint32_t bandwidth, uint8_t sf, bool impl_head, std::vector<uint16_t> sync_word, uint8_t os_factor, uint16_t preamb_len);
176 
177  // Where all the action really happens
178  void forecast(int noutput_items, gr_vector_int &ninput_items_required);
179 
180  int general_work(int noutput_items,
181  gr_vector_int &ninput_items,
182  gr_vector_const_void_star &input_items,
183  gr_vector_void_star &output_items);
184  };
185 
186  } // namespace lora_sdr
187 } // namespace gr
188 
189 #endif /* INCLUDED_LORA_SDR_FRAME_SYNC_IMPL_H */
Definition: frame_sync_impl.h:24
frame_sync_impl(uint32_t center_freq, uint32_t bandwidth, uint8_t sf, bool impl_head, std::vector< uint16_t > sync_word, uint8_t os_factor, uint16_t preamb_len)
void forecast(int noutput_items, gr_vector_int &ninput_items_required)
int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
<+description of block+>
Definition: frame_sync.h:37
Definition: add_crc.h:28
Definition: kiss_fft.h:67
Definition: _kiss_fft_guts.h:22