GNU Radio's LORA_SDR Package
frame_detector_threshold_impl.h
Go to the documentation of this file.
1 /**
2  * @file frame_detector_threshold_impl.h
3  * @author Martyn van Dijke (martijnvdijke600@gmail.com)
4  * @brief
5  * @version 0.3
6  * @date 2021-03-23
7  *
8  *
9  */
10 #ifndef INCLUDED_LORA_SDR_FRAME_DETECTOR_THRESHOLD_IMPL_H
11 #define INCLUDED_LORA_SDR_FRAME_DETECTOR_THRESHOLD_IMPL_H
12 #include <gnuradio/io_signature.h>
14 #include <fstream>
15 #include <iostream>
16 #include <math.h>
17 extern "C" {
18 #include "kiss_fft.h"
19 }
20 #define GRLORA_DEBUGV
21 #define GRLORA_SIM
22 // #define GRLORA_FILE
23 namespace gr {
24 namespace lora_sdr {
25 
27 private:
28  /**
29  * @brief State the frame finder can be in
30  * - FIND_PREAMLBE : find the preamble
31  * - SEND_PREAMBLE : send the buffered preamble symbols
32  * - SEND_FRAME : send frame
33  * - SEND_END_FRAME : send the last part of the frame
34  *
35  */
36  enum State { FIND_PREAMBLE, SEND_PREAMBLE, SEND_FRAME, SEND_END_FRAME };
37 
38  /**
39  * @brief Current state of the frame finder
40  *
41  */
42  uint8_t m_state;
43 
44  /**
45  * @brief Spreading factor
46  *
47  */
48  uint8_t m_sf;
49 
50  /**
51  * @brief Number of samples per LoRa symbol
52  *
53  */
54  uint32_t m_samples_per_symbol;
55 
56  /**
57  * @brief 2^sf
58  *
59  */
60  uint32_t m_N;
61 
62  /**
63  * @brief detection threshold
64  *
65  */
66  float m_threshold;
67 
68  /**
69  * @brief the reference downchirp
70  *
71  */
72  std::vector<gr_complex> m_downchirp;
73 
74  /**
75  * @brief the dechirped symbols on which we need to perform the FFT.
76  *
77  */
78  std::vector<gr_complex> m_dechirped;
79 
80  /**
81  * @brief the output of the FFT
82  *
83  */
84  std::vector<gr_complex> cx_out;
85 
86  /**
87  * @brief the configuration of the FFT
88  *
89  */
90  kiss_fft_cfg fft_cfg;
91 
92  /**
93  * @briefiterator used to find max and argmax of FFT
94  *
95  */
96  std::vector<float>::iterator m_max_it;
97 
98  /**
99  * @brief vector containing the magnitude of the FFT.
100  *
101  */
102  std::vector<float> m_dfts_mag;
103 
104  /**
105  * @brief value of previous lora demodulated symbol
106  *
107  */
108  int32_t bin_idx;
109 
110  /**
111  * @brief value of newly demodulated symbol
112  *
113  */
114  int32_t bin_idx_new;
115 
116  /**
117  * @brief Number of consecutive upchirps in preamble
118  *
119  */
120  uint32_t n_up;
121 
122  /**
123  * @brief Temporary memory vector
124  *
125  */
126  std::vector<gr_complex> buffer;
127 
128  /**
129  * @brief LoRa symbol count
130  *
131  */
132  uint16_t symbol_cnt;
133 
134  /**
135  * @brief Power of a detected LoRa preamble to compare against
136  *
137  */
138  float m_power;
139 
140  /**
141  * @brief Counter for counting if we are past the net identifier and
142  * downchirps once we have found the preamble
143  *
144  */
145  int m_cnt;
146 
147  /**
148  * @brief boolean variables to tell if we are still in a LoRa frame or not
149  *
150  */
151  bool in_frame;
152 
153  /**
154  * @brief length in samples of zero append to each frame
155  *
156  */
157  int m_inter_frame_padding;
158 
159  /**
160  * @brief Counter variable to tell how many extra padding symbols we have
161  * processed
162  *
163  */
164  int cnt_padding;
165 
166  /**
167  * @brief lora symbols per second
168  *
169  */
170  double m_symbols_per_second;
171 
172  /**
173  * @brief Transmission sampling rate
174  *
175  */
176  uint32_t m_samp_rate;
177 
178  /**
179  * @brief Transmission bandwidth (Works only for samp_rate=bw)
180  *
181  */
182  uint32_t m_bw;
183 
184  /**
185  * @brief Temporary gr_complex vector for processing the input per
186  * m_samples_processed
187  *
188  */
189  std::vector<gr_complex> m_temp;
190 
191  std::vector<float> avg_ratio;
192 
193  int m_n_avg;
194 
195 #ifdef GRLORA_SIM
196  /**
197  * @brief Tags vector to hold gnuradio simulation tags
198  *
199  */
200  std::vector <tag_t> m_tags_vector;
201 
202  /**
203  * @brief Boolean to tell if the beginning of pakcet tag has been detected
204  *
205  */
206  bool m_detected_tag_begin;
207 
208  /**
209  * @brief Boolean to tell if the end of pakcet tag has been detected
210  *
211  */
212  bool m_detected_tag_end;
213 
214  /**
215  * @brief Variable to hold the packet offset of the end tag
216  *
217  */
218  u_int32_t m_end_offset;
219 
220  /**
221  * @brief Variable to hold the packet offset of the end tag
222  *
223  */
224  u_int32_t m_begin_offset;
225 
226  u_int32_t m_begin_store;
227 
228 #endif
229 
230 #ifdef GRLORA_FILE
231  std::ofstream file_ratio;
232 #endif
233 
234  /**
235  * @brief Get the symbol object value (aka decoded LoRa symbol value)
236  * Function consumes vectors of length m_N
237  *
238  * @param input : complex samples
239  * @return int32_t : LoRa symbol value
240  */
241  int32_t get_symbol_val(const gr_complex *input);
242 
243  /**
244  * @brief Checks if current samples have the right
245  *
246  * @param input : complex input samples
247  * @return true : we are in a LoRa frame
248  * @return false : we are not in a LoRa frame
249  */
250  bool check_in_frame(const gr_complex *input);
251 
252  /**
253  * @brief Calculates the LoRa frame peak power
254  *
255  * @param input : complex input samples
256  * @return float : peak power
257  */
258  float calc_power(const gr_complex *input);
259 
260  /**
261  * @brief Set the current LoRa symbol energy
262  * Function uses vectors of length m_N
263  *
264  * @param input : complex samples
265  */
266  void set_power(const gr_complex *input);
267 
268 public:
269  /**
270  * @brief Construct a new frame detector impl object
271  *
272  * @param samp_rate : sampling rate
273  * @param bandwidth : bandwith
274  * @param sf : spreading factor
275  * @param threshold : threshold value to use
276  */
277  frame_detector_threshold_impl(uint8_t sf, uint32_t samp_rate, uint32_t bw,
278  float threshold);
279 
280  /**
281  * @brief Destroy the frame detector impl object
282  *
283  */
285 
286  /**
287  * @brief
288  *
289  * @param noutput_items : number of output items
290  * @param ninput_items_required : required input items (how many items must we
291  * have for we can do something)
292  */
293  void forecast(int noutput_items, gr_vector_int &ninput_items_required);
294 
295  /**
296  * @brief General work function.
297  * Main gnuradio function that does the heavy lifting
298  *
299  * @param noutput_items : number of output items
300  * @param ninput_items : number of input items
301  * @param input_items : input items
302  * @param output_items : output items
303  * @return int
304  */
305  int general_work(int noutput_items, gr_vector_int &ninput_items,
306  gr_vector_const_void_star &input_items,
307  gr_vector_void_star &output_items);
308 };
309 
310 } // namespace lora_sdr
311 } // namespace gr
312 
313 #endif /* INCLUDED_LORA_SDR_FRAME_DETECTOR_THRESHOLD_IMPL_H */
kiss_fft.h
gr::lora_sdr::frame_detector_threshold_impl::frame_detector_threshold_impl
frame_detector_threshold_impl(uint8_t sf, uint32_t samp_rate, uint32_t bw, float threshold)
Construct a new frame detector impl object.
kiss_fft_state
Definition: _kiss_fft_guts.h:22
gr::lora_sdr::frame_detector_threshold_impl::forecast
void forecast(int noutput_items, gr_vector_int &ninput_items_required)
gr
Definition: add_crc.h:28
gr::lora_sdr::frame_detector_threshold_impl
Definition: frame_detector_threshold_impl.h:26
frame_detector_threshold.h
gr::lora_sdr::frame_detector_threshold
LoRa frame detector threshold, this block detects a LoRa frames using a preamble detection to find th...
Definition: frame_detector_threshold.h:27
gr::lora_sdr::frame_detector_threshold_impl::general_work
int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
General work function. Main gnuradio function that does the heavy lifting.
gr::lora_sdr::frame_detector_threshold_impl::~frame_detector_threshold_impl
~frame_detector_threshold_impl()
Destroy the frame detector impl object.