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 GRLORA_PRINT_DEBUG
6 // #define GRLORA_SAVE_PRE_DATA //it will save every sample of the packets (preamble + payload)
7 #define GRLORA_MEASUREMENTS
8 
9 
10 #include <lora_sdr/frame_sync.h>
11 #include <iostream>
12 #include <fstream>
13 #include <volk/volk.h>
14 #include <lora_sdr/utilities.h>
15 #include <gnuradio/io_signature.h>
16 extern "C" {
17  #include "kiss_fft.h"
18 }
19 
20 namespace gr {
21  namespace lora_sdr {
22 
23  class frame_sync_impl : public frame_sync
24  {
25  private:
26  enum DecoderState {
27  DETECT,
28  SYNC,
29  FRAC_CFO_CORREC,
30  STOP
31  };
32  enum SyncState {
33  NET_ID1,
34  NET_ID2,
35  DOWNCHIRP1,
36  DOWNCHIRP2,
37  QUARTER_DOWN
38  };
39  uint8_t m_state; ///< Current state of the synchronization
40  uint32_t m_bw; ///< Bandwidth
41  uint32_t m_samp_rate; ///< Sampling rate
42  uint8_t m_sf; ///< Spreading factor
43  uint8_t m_cr; ///< Coding rate
44  uint32_t m_pay_len; ///< payload length
45  uint8_t m_has_crc; ///< CRC presence
46  uint8_t m_invalid_header;///< invalid header checksum
47  bool m_impl_head; ///< use implicit header mode
48  std::vector<uint16_t> m_sync_words; ///< vector containing the two sync words (network identifiers)
49 
50 
51 
52  uint32_t m_number_of_bins; ///< Number of bins in each lora Symbol
53  uint32_t m_samples_per_symbol; ///< Number of samples received per lora symbols
54  uint32_t m_symb_numb; ///<number of payload lora symbols
55  bool m_received_head; ///< indicate that the header has be decoded and received by this block
56 
57  std::vector<gr_complex> in_down; ///< downsampled input
58  std::vector<gr_complex> m_downchirp; ///< Reference downchirp
59  std::vector<gr_complex> m_upchirp; ///< Reference upchirp
60 
61  int32_t symbol_cnt; ///< Number of symbols already received
62  int32_t bin_idx; ///< value of previous lora symbol
63  int32_t bin_idx_new; ///< value of newly demodulated symbol
64 
65  uint32_t n_up; ///< Number of consecutive upchirps in preamble
66  uint8_t symbols_to_skip; ///< Number of integer symbol to skip after consecutive upchirps
67 
68  kiss_fft_cpx *cx_in; ///<input of the FFT
69  kiss_fft_cpx *cx_out; ///<output of the FFT
70 
71  int items_to_consume; ///< Number of items to consume after each iteration of the general_work function
72 
73  std::vector<gr_complex> preamble_raw;///<vector containing the preamble upchirps without any synchronization
74  std::vector<gr_complex> preamble_up; ///<vector containing the preamble upchirps
75 
76  int up_symb_to_use; ///<number of upchirp symbols to use for CFO and STO frac estimation
77  int k_hat; ///<integer part of CFO+STO
78  float lambda_cfo; ///<fractional part of CFO
79  float lambda_bernier; ///<fractional part of CFO using Berniers algo
80  float lambda_sto; ///<fractional part of CFO
81  bool cfo_sto_est; ///< indicate that the estimation of lambda_cfo/sto has been performed
82  int usFactor; ///<upsampling factor used by the FIR interpolator
83  std::vector<gr_complex> CFO_frac_correc; ///<cfo frac correction vector
84 
85 
86  std::vector<gr_complex> symb_corr; ///< symbol with CFO frac corrected
87  int down_val; ///< value of the preamble downchirps
88  int CFOint; ///< integer part of CFO
89  int net_id_off; ///<offset of the network identifier
90 
91  #ifdef GRLORA_MEASUREMENTS
92  int off_by_one_id; ///< Indicate that the network identifiers where off by one and corrected
93  std::ofstream sync_log; ///< savefile containing the offset estimation and the signal strength estimation
94  #endif
95  #ifdef GRLORA_DEBUG
96  int numb_symbol_to_save;///< number of symbol to save for every erroneous frame
97  std::vector<gr_complex> last_frame;///< vector storing samples of the last received frame
98  std::ofstream samples_file;///< savefile containing the samples of the erroneous frames
99  std::ofstream preamb_file;
100  std::ofstream payload_file;
101  #endif
102  /**
103  * \brief Handle the reception of the explicit header information, received from the header_decoder block
104  */
105  void frame_info_handler(pmt::pmt_t frame_info);
106 
107  /**
108  * \brief Estimate the value of fractional part of the CFO using RCTSL
109  * \param samples
110  * The pointer to the preamble beginning.(We might want to avoid the
111  * first symbol since it might be incomplete)
112  */
113  void estimate_CFO(gr_complex* samples);
114  /**
115  * \brief (not used) Estimate the value of fractional part of the CFO using Berniers algorithm
116  */
117  void estimate_CFO_Bernier();
118  /**
119  * \brief Estimate the value of fractional part of the STO from m_consec_up
120  **/
121  void estimate_STO();
122  /**
123  * \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.
124  *
125  * \param samples
126  * The pointer to the symbol beginning.
127  * \param ref_chirp
128  * The reference chirp to use to dechirp the lora symbol.
129  */
130  uint32_t get_symbol_val(const gr_complex *samples,gr_complex *ref_chirp);
131 
132  /**
133  * \brief Determine the energy of a symbol.
134  *
135  * \param samples
136  * The complex symbol to analyse.
137  */
138  float determine_energy(const gr_complex *samples);
139 
140  /**
141  * \brief Handles the error message coming from the header decoding.
142  */
143  void header_err_handler(pmt::pmt_t payload_len);
144 
145  public:
146  frame_sync_impl(float samp_rate, uint32_t bandwidth, uint8_t sf, bool impl_head, std::vector<uint16_t> sync_word);
148 
149  // Where all the action really happens
150  void forecast (int noutput_items, gr_vector_int &ninput_items_required);
151 
152  int general_work(int noutput_items,
153  gr_vector_int &ninput_items,
154  gr_vector_const_void_star &input_items,
155  gr_vector_void_star &output_items);
156  };
157 
158  } // namespace lora_sdr
159 } // namespace gr
160 
161 #endif /* INCLUDED_LORA_SDR_FRAME_SYNC_IMPL_H */
int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
Definition: add_crc.h:28
void forecast(int noutput_items, gr_vector_int &ninput_items_required)
Definition: frame_sync_impl.h:23
Definition: kiss_fft.h:67
<+description of block+>
Definition: frame_sync.h:36
frame_sync_impl(float samp_rate, uint32_t bandwidth, uint8_t sf, bool impl_head, std::vector< uint16_t > sync_word)