SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
math.h
Go to the documentation of this file.
1 // This file is part of LeanSDR Copyright (C) 2016-2018 <pabr@pabr.org>.
2 // See the toplevel README for more information.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef LEANSDR_MATH_H
18 #define LEANSDR_MATH_H
19 
20 #include <math.h>
21 #include <stdint.h>
22 
23 #ifndef M_PI
24 # define M_PI 3.14159265358979323846
25 #endif
26 
27 namespace leansdr
28 {
29 
30 template <typename T>
31 struct complex
32 {
33  T re, im;
34  complex() {}
35  complex(T x) : re(x), im(0) {}
36  complex(T x, T y) : re(x), im(y) {}
37  inline void operator+=(const complex<T> &x)
38  {
39  re += x.re;
40  im += x.im;
41  }
42  inline void operator*=(const complex<T> &c)
43  {
44  T tre = re * c.re - im * c.im;
45  im = re * c.im + im * c.re;
46  re = tre;
47  }
48  inline void operator*=(const T &k)
49  {
50  re *= k;
51  im *= k;
52  }
53 };
54 
55 template <typename T>
57 {
58  return complex<T>(a.re + b.re, a.im + b.im);
59 }
60 
61 template <typename T>
63 {
64  return complex<T>(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re);
65 }
66 
67 template <typename T>
68 complex<T> operator*(const complex<T> &a, const T &k)
69 {
70  return complex<T>(a.re * k, a.im * k);
71 }
72 
73 template <typename T>
74 complex<T> operator*(const T &k, const complex<T> &a)
75 {
76  return complex<T>(k * a.re, k * a.im);
77 }
78 
79 template <typename T>
80 T dotprod(const T *u, const T *v, int n)
81 {
82  T acc = 0;
83  while (n--)
84  acc += (*u++) * (*v++);
85  return acc;
86 }
87 
88 template <typename T>
89 inline T cnorm2(const complex<T> &u)
90 {
91  return u.re * u.re + u.im * u.im;
92 }
93 
94 template <typename T>
95 T cnorm2(const complex<T> *p, int n)
96 {
97  T res = 0;
98  for (; n--; ++p)
99  res += cnorm2(*p);
100  return res;
101 }
102 
103 // Return conj(u)*v
104 template <typename T>
105 inline complex<T> conjprod(const complex<T> &u, const complex<T> &v)
106 {
107  return complex<T>(u.re * v.re + u.im * v.im,
108  u.re * v.im - u.im * v.re);
109 }
110 
111 // Return sum(conj(u[i])*v[i])
112 template <typename T>
113 complex<T> conjprod(const complex<T> *u, const complex<T> *v, int n)
114 {
115  complex<T> acc = 0;
116  while (n--)
117  acc += conjprod(*u++, *v++);
118  return acc;
119 }
120 
121 // TBD Optimize with dedicated instructions
122 int hamming_weight(uint8_t x);
123 int hamming_weight(uint16_t x);
124 int hamming_weight(uint32_t x);
125 int hamming_weight(uint64_t x);
126 unsigned char parity(uint8_t x);
127 unsigned char parity(uint16_t x);
128 unsigned char parity(uint32_t x);
129 unsigned char parity(uint64_t x);
130 int log2i(uint64_t x);
131 
132 // Pre-computed sin/cos for 16-bit angles
133 
134 struct trig16
135 {
136  complex<float> lut[65536]; // TBD static and shared
138  {
139  for (int a = 0; a < 65536; ++a)
140  {
141  float af = a * 2 * M_PI / 65536;
142  lut[a].re = cosf(af);
143  lut[a].im = sinf(af);
144  }
145  }
146  inline const complex<float> &expi(uint16_t a) const
147  {
148  return lut[a];
149  }
150  // a must fit in a int32_t, otherwise behaviour is undefined
151  inline const complex<float> &expi(float a) const
152  {
153  return expi((uint16_t)(int16_t)(int32_t)a);
154  }
155 };
156 
157 } // namespace leansdr
158 
159 #endif // LEANSDR_MATH_H
short int16_t
Definition: rtptypes_win.h:43
bitvect< T, N > operator*(bitvect< T, N > a, const bitvect< T, NB > &b)
Definition: discrmath.h:186
int log2i(uint64_t x)
Definition: math.cpp:48
int hamming_weight(uint8_t x)
Definition: math.cpp:6
#define M_PI
Definition: rdsdemod.cpp:27
complex(T x, T y)
Definition: math.h:36
unsigned int uint32_t
Definition: rtptypes_win.h:46
unsigned char uint8_t
Definition: rtptypes_win.h:42
unsigned short uint16_t
Definition: rtptypes_win.h:44
complex(T x)
Definition: math.h:35
int int32_t
Definition: rtptypes_win.h:45
void operator+=(const complex< T > &x)
Definition: math.h:37
void operator*=(const complex< T > &c)
Definition: math.h:42
T dotprod(const T *u, const T *v, int n)
Definition: math.h:80
void operator*=(const T &k)
Definition: math.h:48
const complex< float > & expi(uint16_t a) const
Definition: math.h:146
unsigned char parity(uint8_t x)
Definition: math.cpp:27
bitvect< T, N > operator+(const bitvect< T, N > &a, const bitvect< T, N > &b)
Definition: discrmath.h:175
complex< T > conjprod(const complex< T > &u, const complex< T > &v)
Definition: math.h:105
T cnorm2(const complex< T > &u)
Definition: math.h:89
const complex< float > & expi(float a) const
Definition: math.h:151
unsigned __int64 uint64_t
Definition: rtptypes_win.h:48