GNU Radio's LORA_SDR Package
utilities.h
Go to the documentation of this file.
1 #ifndef UTILITIES_H
2 #define UTILITIES_H
3 
4 #include <cstdint>
5 #include <string.h>
6 #include <iomanip>
7 #include <numeric>
8 #include <gnuradio/expj.h>
9 #include <volk/volk.h>
10 #include <algorithm>
11 
12 // Undefine VOID macro (happens on Windows) so we can use VOID as a symbol type below
13 #ifdef VOID
14 #undef VOID
15 #endif
16 
17 #define print(message) std::cout<< message <<std::endl
18 namespace gr {
19  namespace lora_sdr {
20 
21  // #define THREAD_MEASURE
22 
23  #define RESET "\033[0m"
24  #define RED "\033[31m" /* Red */
25 
26  #define MIN_SF 5 //minimum and maximum SF
27  #define MAX_SF 12
28 
29  typedef double LLR; ///< Log-Likelihood Ratio type
30  //typedef long double LLR; // 16 Bytes
31 
32  enum Symbol_type {
40  };
41  #define LDRO_MAX_DURATION_MS 16
42  enum ldro_mode {
45  AUTO
46  };
47  /**
48  * \brief return the modulus a%b between 0 and (b-1)
49  */
50  inline long mod(long a, long b)
51  { return (a%b+b)%b; }
52 
53  inline double double_mod(double a, long b)
54  { return fmod(fmod(a,b)+b,b);}
55 
56  /**
57  * \brief Convert an integer into a MSB first vector of bool
58  *
59  * \param integer
60  * The integer to convert
61  * \param n_bits
62  * The output number of bits
63  */
64  inline std::vector<bool> int2bool(unsigned int integer,uint8_t n_bits){
65  std::vector<bool> vec(n_bits,0);
66  int j=n_bits;
67  for(int i=0 ;i<n_bits;i++) {
68  vec[--j]=((integer>>i)& 1);
69  }
70  return vec;
71 
72  };
73  /**
74  * \brief Convert a MSB first vector of bool to a integer
75  *
76  * \param b
77  * The boolean vector to convert
78  */
79  inline uint32_t bool2int(std::vector<bool> b){
80  uint32_t integer = std::accumulate(b.begin(), b.end(), 0, [](int x, int y) { return (x << 1) + y; });
81  return integer;
82  };
83  /**
84  * \brief Return an modulated upchirp using s_f=bw
85  *
86  * \param chirp
87  * The pointer to the modulated upchirp
88  * \param id
89  * The number used to modulate the chirp
90  * \param sf
91  * The spreading factor to use
92  * \param os_factor
93  * The oversampling factor used to generate the upchirp
94  */
95  inline void build_upchirp(gr_complex* chirp, uint32_t id, uint8_t sf, uint8_t os_factor = 1){
96  double N = (1 << sf) ;
97  int n_fold = N* os_factor - id*os_factor;
98  for(int n = 0; n < N* os_factor; n++){
99  if(n<n_fold)
100  chirp[n] = gr_complex(1.0,0.0)*gr_expj(2.0*M_PI *(n*n/(2*N)/pow(os_factor,2)+(id/N-0.5)*n/os_factor));
101  else
102  chirp[n] = gr_complex(1.0,0.0)*gr_expj(2.0*M_PI *(n*n/(2*N)/pow(os_factor,2)+(id/N-1.5)*n/os_factor));
103 
104  }
105  }
106 
107  /**
108  * \brief Return the reference chirps using s_f=bw
109  *
110  * \param upchirp
111  * The pointer to the reference upchirp
112  * \param downchirp
113  * The pointer to the reference downchirp
114  * \param sf
115  * The spreading factor to use
116  */
117  inline void build_ref_chirps(gr_complex* upchirp, gr_complex* downchirp, uint8_t sf, uint8_t os_factor = 1){
118  double N = (1 << sf);
119  build_upchirp(upchirp,0,sf,os_factor);
120  volk_32fc_conjugate_32fc(&downchirp[0], &upchirp[0], N*os_factor);
121 
122  // for(uint32_t n = 0; n < N ;n++){
123  // //the scaling factor of 0.9 is here to avoid to saturate the USRP_SINK
124  // upchirp[n] = gr_complex(0.9f, 0.0f)*gr_expj(2.0 * M_PI * (n*n/(2*N)-0.5*n));
125  // downchirp[n] = gr_complex(0.9f, 0.0f)*gr_expj(-2.0 * M_PI * (n*n/(2*N)-0.5*n));
126  // }
127  }
128  // find most frequency number in vector
129  inline int most_frequent(int arr[], int n)
130  {
131  // Insert all elements in hash.
132  std::unordered_map<int, int> hash;
133  for (int i = 0; i < n; i++)
134  hash[arr[i]]++;
135 
136  // find the max frequency
137  int max_count = 0, res = -1;
138  for (auto i : hash) {
139  if (max_count < i.second) {
140  res = i.first;
141  max_count = i.second;
142  }
143  }
144 
145  return res;
146  }
147 
148 
149  inline std::string random_string(int Nbytes){
150  const char* charmap = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
151  const size_t charmapLength = strlen(charmap);
152  auto generator = [&](){ return charmap[rand()%charmapLength]; };
153  std::string result;
154  result.reserve(Nbytes);
155  std::generate_n(std::back_inserter(result), Nbytes, generator);
156  return result;
157  }
158  }
159 }
160 #endif /* UTILITIES_H */
void build_ref_chirps(gr_complex *upchirp, gr_complex *downchirp, uint8_t sf, uint8_t os_factor=1)
Return the reference chirps using s_f=bw.
Definition: utilities.h:117
ldro_mode
Definition: utilities.h:42
@ DISABLE
Definition: utilities.h:43
@ ENABLE
Definition: utilities.h:44
@ AUTO
Definition: utilities.h:45
Symbol_type
Definition: utilities.h:32
@ SYNC_WORD
Definition: utilities.h:35
@ DOWNCHIRP
Definition: utilities.h:36
@ UNDETERMINED
Definition: utilities.h:39
@ VOID
Definition: utilities.h:33
@ QUARTER_DOWN
Definition: utilities.h:37
@ PAYLOAD
Definition: utilities.h:38
@ UPCHIRP
Definition: utilities.h:34
std::string random_string(int Nbytes)
Definition: utilities.h:149
void build_upchirp(gr_complex *chirp, uint32_t id, uint8_t sf, uint8_t os_factor=1)
Return an modulated upchirp using s_f=bw.
Definition: utilities.h:95
int most_frequent(int arr[], int n)
Definition: utilities.h:129
long mod(long a, long b)
return the modulus ab between 0 and (b-1)
Definition: utilities.h:50
std::vector< bool > int2bool(unsigned int integer, uint8_t n_bits)
Convert an integer into a MSB first vector of bool.
Definition: utilities.h:64
double double_mod(double a, long b)
Definition: utilities.h:53
double LLR
Log-Likelihood Ratio type.
Definition: utilities.h:29
uint32_t bool2int(std::vector< bool > b)
Convert a MSB first vector of bool to a integer.
Definition: utilities.h:79
Definition: add_crc.h:28