forked from andersesbensen/rtl-zwave
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dsp.h
284 lines (254 loc) · 7.11 KB
/
dsp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Butterworth LP/HP IIR filter design
*
* Copyright (C) 2016 Mirko Maischberger <[email protected]>
*
* Portions Copyright (C) 2014 Exstrom Laboratories LLC <stefan(AT)exstrom.com>
* Longmont, CO 80503, USA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GNU General Public License is available on the internet at:
* http://www.gnu.org/copyleft/gpl.html
*
*/
#pragma once
#include <boost/circular_buffer.hpp>
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <complex>
#include <cstdlib>
#include <cstdio>
#include <memory>
#include <vector>
#include <array>
#include <tuple>
#include <cmath>
namespace
{
inline std::vector<std::complex<double>>
binomial_mult(const std::vector<std::complex<double>>& p)
{
std::vector<std::complex<double>> a(p.size(), 0.0);
for (size_t ii(0); ii != p.size(); ++ii) {
for (size_t jj(ii); jj != 0; --jj) {
a[jj] += p[ii] * a[jj - 1];
}
a[0] += p[ii];
}
return a;
}
template <int ORDER>
std::array<double, ORDER + 1>
acof_bwlp(double fcf)
{
std::vector<std::complex<double>> rcof(ORDER);
double theta = M_PI * fcf;
double st = sin(theta);
double ct = cos(theta);
for (int k = 0; k < ORDER; ++k) {
double parg = M_PI * (2.0 * k + 1) / (2.0 * ORDER);
double a = 1.0 + st * sin(parg);
rcof[k].real(-ct / a);
rcof[k].imag(-st * cos(parg) / a);
}
auto ddcof = binomial_mult(rcof);
std::array<double, ORDER + 1> dcof;
dcof[0] = 1.0;
for (int k(1); k != ORDER + 1; ++k) {
dcof[k] = ddcof[k - 1].real();
}
return dcof;
}
template <int ORDER>
std::array<double, ORDER + 1>
acof_bwhp(int n, double fcf)
{
return acof_bwlp<ORDER>(fcf);
}
template <int ORDER>
double
sf_bwlp(double fcf)
{
int k; // loop variables
double omega; // M_PI * fcf
double fomega; // function of omega
double parg0; // zeroth pole angle
double sf; // scaling factor
omega = M_PI * fcf;
fomega = sin(omega);
parg0 = M_PI / (double)(2 * ORDER);
sf = 1.0;
for (k = 0; k < ORDER / 2; ++k)
sf *= 1.0 + fomega * sin((double)(2 * k + 1) * parg0);
fomega = sin(omega / 2.0);
if (ORDER % 2)
sf *= fomega + cos(omega / 2.0);
sf = pow(fomega, ORDER) / sf;
return (sf);
}
template <int ORDER>
double
sf_bwhp(double fcf)
{
int m, k; // loop variables
double omega; // M_PI * fcf
double fomega; // function of omega
double parg0; // zeroth pole angle
double sf; // scaling factor
omega = M_PI * fcf;
fomega = sin(omega);
parg0 = M_PI / (double)(2 * ORDER);
m = ORDER / 2;
sf = 1.0;
for (k = 0; k < ORDER / 2; ++k)
sf *= 1.0 + fomega * sin((double)(2 * k + 1) * parg0);
fomega = cos(omega / 2.0);
if (ORDER % 2)
sf *= fomega + sin(omega / 2.0);
sf = pow(fomega, ORDER) / sf;
return sf;
}
template <int ORDER>
std::array<double, ORDER + 1>
ccof_bwlp()
{
std::array<double, ORDER + 1> ccof;
ccof[0] = 1;
ccof[1] = ORDER;
int m = ORDER / 2;
for (int i = 2; i <= m; ++i) {
ccof[i] = (ORDER - i + 1) * ccof[i - 1] / i;
ccof[ORDER - i] = ccof[i];
}
ccof[ORDER - 1] = ORDER;
ccof[ORDER] = 1;
return ccof;
}
template <int ORDER>
std::array<double, ORDER + 1>
ccof_bwhp()
{
std::array<double, ORDER + 1> ccof;
// TBD
return ccof;
}
} // anonymous namespace
///
/// An IIR Butterworth LP Filter
///
/// Similar to octave-signal butter function, low-pass
///
/// @param sample_rate The desired sample rate (=2*Nyquist)
/// @param cutoff_freq The -3dB cutoff frequency
///
/// @returns [b,a] coefficients ready to be used by the iir_filter class.
///
template <int ORDER>
std::tuple<double, std::array<double, ORDER + 1>, std::array<double, ORDER + 1>>
butter_lp(double sample_rate, double cutoff_freq)
{
return std::make_tuple(sf_bwlp<ORDER>(2.0 * cutoff_freq / sample_rate),
ccof_bwlp<ORDER>(),
acof_bwlp<ORDER>(2.0 * cutoff_freq / sample_rate));
}
///
/// An IIR Butterworth HP Filter
///
/// Similar to octave-signal butter function, high-pass
///
/// @param sample_rate The desired sample rate (=2*Nyquist)
/// @param cutoff_freq The -3dB cutoff frequency
///
/// @returns [b,a] coefficients ready to be used by the iir_filter class.
///
template <int ORDER>
std::tuple<double, std::array<double, ORDER + 1>, std::array<double, ORDER + 1>>
butter_hp(double sample_rate, double cutoff_freq)
{
return std::make_tuple(sf_bwhp<ORDER>(2.0 * cutoff_freq / sample_rate),
ccof_bwhp<ORDER>(),
acof_bwhp<ORDER>(2.0 * cutoff_freq / sample_rate));
}
/// Simple arctan demodulator
struct atan_fm_demodulator
{
atan_fm_demodulator()
: s1(0)
{
}
/// Q&I
double operator()(const std::complex<double>& s)
{
double d = std::arg(std::conj(s1) * s);
s1 = s;
return d;
}
std::complex<double> s1;
};
///
/// Generic IIR filter simulator or specified ORDER
///
template <int ORDER>
struct iir_filter
{
///
/// Creates the filter.
///
/// @param gain input gain
/// @param b Coefficients for the input
/// @param a Coefficients for the output
///
explicit iir_filter(double gain, const std::array<double, ORDER + 1>& b,
const std::array<double, ORDER + 1>& a)
: gain_m(gain)
, b_m(b)
, a_m(a)
, xv_m(ORDER + 1, 0)
, yv_m(ORDER + 1, 0)
{
assert(a[0] == 1.0);
for (size_t ii(0); ii != (ORDER + 1) / 2; ++ii)
assert(b[ii] == b[b.size() - ii - 1]);
}
///
/// Creates the filter.
///
/// @param tuple(gain, b, a) as returned by the butter_lp function
///
explicit iir_filter(const std::tuple< double, std::array<double, ORDER + 1>, std::array<double, ORDER + 1> >& params)
: iir_filter(std::get<0>(params), std::get<1>(params), std::get<2>(params))
{
}
///
/// Feed the filter with samples.
///
/// @param in The latest input to the filter
/// @returns The filtered output
///
double operator()(double in)
{
xv_m.push_front(in);
double yvn =
gain_m * std::inner_product(xv_m.begin(), xv_m.end(), b_m.begin(), 0.0) -
std::inner_product(yv_m.begin(), yv_m.end(), a_m.begin() + 1, 0.0);
yv_m.push_front(yvn);
return yvn;
}
private:
double gain_m;
std::array<double, ORDER + 1> b_m;
std::array<double, ORDER + 1> a_m;
boost::circular_buffer<double> xv_m;
boost::circular_buffer<double> yv_m;
};