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_SAVE_PRE_DATA //it will save every sample of the packets
6// (preamble + payload)
7// #define GRLORA_MEASUREMENTS
8
9#include <fstream>
10#include <gnuradio/io_signature.h>
11#include <iostream>
12#include <lora_sdr/frame_sync.h>
13#include "helpers.h"
14#include <volk/volk.h>
15extern "C" {
16#include "kiss_fft.h"
17}
18
19namespace gr {
20namespace lora_sdr {
21
23private:
24 /**
25 * @brief Decoder states:
26 * - DETECT : Detect preamble
27 * - SYNC : synchronize integer part STO,CFO
28 * - FRAC_CFO_CORREC :
29 * - STOP :
30 *
31 */
32 enum DecoderState { DETECT, SYNC, FRAC_CFO_CORREC, STOP };
33 /**
34 * @brief Sync states:
35 * - NET_ID1 : network identifier 1
36 * - NET_ID2 : network identifier 2
37 * - DOWNCHIRP1 : downchirp 1
38 * - DOWNCHIRP2 : downchirp 2
39 * - QUARTER_DOWN : the extra quater downschirp symbol presetn in the LoRa
40 * preamble
41 *
42 */
43 enum SyncState { NET_ID1, NET_ID2, DOWNCHIRP1, DOWNCHIRP2, QUARTER_DOWN };
44 uint8_t m_state; ///< Current state of the synchronization
45 uint32_t m_bw; ///< Bandwidth
46 uint32_t m_samp_rate; ///< Sampling rate
47 uint8_t m_sf; ///< Spreading factor
48 uint8_t m_cr; ///< Coding rate
49 uint32_t m_pay_len; ///< payload length
50 uint8_t m_has_crc; ///< CRC presence
51 uint8_t m_invalid_header; ///< invalid header checksum
52 bool m_impl_head; ///< use implicit header mode
53 std::vector<uint16_t> m_sync_words; ///< vector containing the two sync words
54 ///< (network identifiers)
55
56 uint32_t m_number_of_bins; ///< Number of bins in each lora Symbol
57 uint32_t
58 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
61 ///< received by this block
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 int32_t symbol_cnt; ///< Number of symbols already received
68 int32_t bin_idx; ///< value of previous lora symbol
69 int32_t bin_idx_new; ///< value of newly demodulated symbol
70
71 uint32_t n_up; ///< Number of consecutive upchirps in preamble
72 uint8_t symbols_to_skip; ///< Number of integer symbol to skip after
73 ///< consecutive upchirps
74
75 kiss_fft_cpx *cx_in; ///< input of the FFT
76 kiss_fft_cpx *cx_out; ///< output of the FFT
77
78 int items_to_consume; ///< Number of items to consume after each iteration of
79 ///< the general_work function
80
81 std::vector<gr_complex>
82 preamble_raw; ///< vector containing the preamble
83 ///< upchirps without any synchronization
84 std::vector<gr_complex>
85 preamble_up; ///< vector containing the preamble upchirps
86
87 int up_symb_to_use; ///< number of upchirp symbols to use for CFO and STO frac
88 ///< estimation
89 int k_hat; ///< integer part of CFO+STO
90 float lambda_cfo; ///< fractional part of CFO
91 float lambda_bernier; ///< fractional part of CFO using Berniers algo
92 float lambda_sto; ///< fractional part of CFO
93 bool cfo_sto_est; ///< indicate that the estimation of lambda_cfo/sto has been
94 ///< performed
95 int usFactor; ///< upsampling factor used by the FIR interpolator
96 std::vector<gr_complex> CFO_frac_correc; ///< cfo frac correction vector
97
98 std::vector<gr_complex> symb_corr; ///< symbol with CFO frac corrected
99 int down_val; ///< value of the preamble downchirps
100 int CFOint; ///< integer part of CFO
101 int net_id_off; ///< offset of the network identifier
102
103#ifdef GRLORA_MEASUREMENTS
104 int off_by_one_id; ///< Indicate that the network identifiers where off by one
105 ///< and corrected
106 std::ofstream sync_log; ///< savefile containing the offset estimation and the
107 ///< signal strength estimation
108#endif
109#ifdef GRLORA_DEBUG
110 int numb_symbol_to_save; ///< number of symbol to save for every erroneous
111 ///< frame
112 std::vector<gr_complex>
113 last_frame; ///< vector storing samples of the last received frame
114 std::ofstream
115 samples_file; ///< savefile containing the samples of the erroneous frames
116 std::ofstream preamb_file;
117 std::ofstream payload_file;
118#endif
119 /**
120 * \brief Handle the reception of the explicit header information, received
121 * from the header_decoder block
122 */
123 void frame_info_handler(pmt::pmt_t frame_info);
124
125 /**
126 * \brief Estimate the value of fractional part of the CFO using RCTSL
127 * \param samples
128 * The pointer to the preamble beginning.(We might want to avoid the
129 * first symbol since it might be incomplete)
130 */
131 void estimate_CFO(gr_complex *samples);
132 /**
133 * \brief (not used) Estimate the value of fractional part of the CFO using
134 * Berniers algorithm
135 */
136 void estimate_CFO_Bernier();
137 /**
138 * \brief Estimate the value of fractional part of the STO from m_consec_up
139 **/
140 void estimate_STO();
141 /**
142 * \brief Recover the lora symbol value using argmax of the dechirped symbol
143 * FFT. Returns -1 in case of an fft window containing no energy to handle
144 * noiseless simulations.
145 *
146 * \param samples
147 * The pointer to the symbol beginning.
148 * \param ref_chirp
149 * The reference chirp to use to dechirp the lora symbol.
150 */
151 uint32_t get_symbol_val(const gr_complex *samples, gr_complex *ref_chirp);
152
153 /**
154 * \brief Determine the energy of a symbol.
155 *
156 * \param samples
157 * The complex symbol to analyse.
158 */
159 float determine_energy(const gr_complex *samples);
160
161 /**
162 * \brief Handles the error message coming from the header decoding.
163 */
164 void header_err_handler(pmt::pmt_t payload_len);
165
166public:
167 /**
168 * @brief Construct a new frame sync impl object
169 *
170 * @param samp_rate
171 * @param bandwidth
172 * @param sf
173 * @param impl_head
174 * @param sync_word
175 */
176 frame_sync_impl(float samp_rate, uint32_t bandwidth, uint8_t sf,
177 bool impl_head, std::vector<uint16_t> sync_word);
178 /**
179 * @brief Destroy the frame sync impl object
180 *
181 */
183
184 /**
185 * @brief
186 *
187 * @param noutput_items
188 * @param ninput_items_required
189 */
190 void forecast(int noutput_items, gr_vector_int &ninput_items_required);
191
192 /**
193 * @brief
194 *
195 * @param noutput_items
196 * @param ninput_items
197 * @param input_items
198 * @param output_items
199 * @return int
200 */
201 int general_work(int noutput_items, gr_vector_int &ninput_items,
202 gr_vector_const_void_star &input_items,
203 gr_vector_void_star &output_items);
204};
205
206} // namespace lora_sdr
207} // namespace gr
208
209#endif /* INCLUDED_LORA_SDR_FRAME_SYNC_IMPL_H */
Definition: frame_sync_impl.h:22
frame_sync_impl(float samp_rate, uint32_t bandwidth, uint8_t sf, bool impl_head, std::vector< uint16_t > sync_word)
Construct a new frame sync impl object.
void forecast(int noutput_items, gr_vector_int &ninput_items_required)
~frame_sync_impl()
Destroy the frame sync impl object.
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