Cheetah Software  1.0
filters.cpp
Go to the documentation of this file.
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #define BUDDA_Q_SCALE 6.f
8 
9 template <typename T>
11  : num_data_(num_data), idx_(0), sum_(0.0) {
12  buffer_ = new T[num_data_];
13  memset((void *)buffer_, 0.0, sizeof(T) * num_data_);
14 }
15 
16 template <typename T>
17 void moving_average_filter<T>::input(T input_value) {
18  sum_ -= buffer_[idx_];
19  sum_ += input_value;
20  buffer_[idx_] = input_value;
21  ++idx_;
22  idx_ %= num_data_;
23 }
24 
25 template <typename T>
27  return sum_ / num_data_;
28 }
29 
30 template <typename T>
32  sum_ = 0.0;
33  memset((void *)buffer_, 0.0, sizeof(T) * num_data_);
34 }
35 
36 template <typename T>
38  delete[] buffer_;
39 }
40 
41 template class moving_average_filter<double>;
42 template class moving_average_filter<float>;
43 
44 /*============================================================================*/
45 
46 template <typename T>
48  T cutoff_frequency) {
49  mNumSample = num_sample;
50  mDt = dt;
51  mCutoffFreq = cutoff_frequency;
52 
53  mpBuffer = new T[num_sample];
54  memset((void *)mpBuffer, 0, sizeof(T) * num_sample);
55 
56  mCurIdx = 0;
57 }
58 
59 template <typename T>
61  delete[] mpBuffer;
62 }
63 
64 template <typename T>
65 void butterworth_filter<T>::input(T input_value) {
66  int j;
67  T sqrt_2 = sqrt(2);
68  T value = 0;
69  for (j = mNumSample - 2; j >= 0; j--) {
70  mpBuffer[j + 1] = mpBuffer[j];
71  }
72 
73  mpBuffer[0] = input_value;
74  for (j = 0; j < mNumSample; j++) {
75  T t = (T)j * mDt;
76  value += sqrt_2 / mCutoffFreq * mpBuffer[j] * exp(-1. / sqrt_2 * t) *
77  sin(mCutoffFreq / sqrt_2 * t) * mDt;
78  // value += sqrt_2 * exp(-1./sqrt_2*t) * sin(1./sqrt_2*t ) * mDt;
79  }
80  mValue = value;
81 }
82 
83 template <typename T>
85  return mValue;
86 }
87 
88 template <typename T>
90  for (int i(0); i < mNumSample; ++i) {
91  mpBuffer[i] = 0.0;
92  }
93 }
94 
95 template class butterworth_filter<double>;
96 template class butterworth_filter<float>;
97 
98 /*============================================================================*/
99 
100 template <typename T>
102  Lpf_in_prev[0] = Lpf_in_prev[1] = 0;
103  Lpf_out_prev[0] = Lpf_out_prev[1] = 0;
104  Lpf_in1 = 0, Lpf_in2 = 0, Lpf_in3 = 0, Lpf_out1 = 0, Lpf_out2 = 0;
105  float den = 2500 * t_s * t_s * w_c * w_c + 7071 * t_s * w_c + 10000;
106 
107  Lpf_in1 = 2500 * t_s * t_s * w_c * w_c / den;
108  Lpf_in2 = 5000 * t_s * t_s * w_c * w_c / den;
109  Lpf_in3 = 2500 * t_s * t_s * w_c * w_c / den;
110  Lpf_out1 = -(5000 * t_s * t_s * w_c * w_c - 20000) / den;
111  Lpf_out2 = -(2500 * t_s * t_s * w_c * w_c - 7071 * t_s * w_c + 10000) / den;
112 }
113 
114 template <typename T>
116 
117 template <typename T>
119  lpf_out = Lpf_in1 * lpf_in + Lpf_in2 * Lpf_in_prev[0] +
120  Lpf_in3 * Lpf_in_prev[1] + // input component
121  Lpf_out1 * Lpf_out_prev[0] +
122  Lpf_out2 * Lpf_out_prev[1]; // output component
123  Lpf_in_prev[1] = Lpf_in_prev[0];
124  Lpf_in_prev[0] = lpf_in;
125  Lpf_out_prev[1] = Lpf_out_prev[0];
126  Lpf_out_prev[0] = lpf_out;
127 }
128 
129 template <typename T>
131  return lpf_out;
132 }
133 
134 template <typename T>
136  Lpf_in_prev[1] = 0;
137  Lpf_in_prev[0] = 0;
138  Lpf_out_prev[1] = 0;
139  Lpf_out_prev[0] = 0;
140 }
141 
142 template class digital_lp_filter<double>;
143 template class digital_lp_filter<float>;
144 
145 /*============================================================================*/
146 
147 template <typename T>
149  Lpf_in_prev[0] = 0;
150  Lpf_in_prev[1] = 0;
151  Lpf_out_prev[0] = 0;
152  Lpf_out_prev[1] = 0;
153  Lpf_in1 = 0;
154  Lpf_in2 = 0;
155  Lpf_in3 = 0;
156  Lpf_out1 = 0;
157  Lpf_out2 = 0;
158  T a = 1.4142;
159  T den = 4 + 2 * a * w_c * t_s + t_s * t_s * w_c * w_c;
160 
161  Lpf_in1 = 2 * t_s * w_c * w_c / den;
162  Lpf_in2 = 0;
163  Lpf_in3 = -2. * t_s * w_c * w_c / den;
164  Lpf_out1 = -1. * (-8 + t_s * t_s * w_c * w_c * 2) / den;
165  Lpf_out2 = -1. * (4 - 2 * a * w_c * t_s + t_s * t_s * w_c * w_c) / den;
166  lpf_out = 0.0;
167  clear();
168 }
169 
170 template <typename T>
172 
173 template <typename T>
175  // static int i(0);
176  lpf_out = Lpf_in1 * lpf_in + Lpf_in2 * Lpf_in_prev[0] +
177  Lpf_in3 * Lpf_in_prev[1] + // input component
178  Lpf_out1 * Lpf_out_prev[0] +
179  Lpf_out2 * Lpf_out_prev[1]; // output component
180 
181  // printf("%i th filter (%f): %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n",i,
182  // lpf_out,
183  // Lpf_in1, lpf_in, Lpf_in2,
184  // Lpf_in_prev[0], Lpf_in3, Lpf_in_prev[1],
185  // Lpf_out1, Lpf_out_prev[0], Lpf_out2, Lpf_out_prev[1]);
186 
187  // if(lpf_out>100){
188  // exit(0);
189  // }
190 
191  Lpf_in_prev[1] = Lpf_in_prev[0];
192  Lpf_in_prev[0] = lpf_in;
193  Lpf_out_prev[1] = Lpf_out_prev[0];
194  Lpf_out_prev[0] = lpf_out;
195  // ++i;
196 }
197 
198 template <typename T>
200  return lpf_out;
201 }
202 
203 template <typename T>
205  Lpf_in_prev[1] = 0;
206  Lpf_in_prev[0] = 0;
207  Lpf_out_prev[1] = 0;
208  Lpf_out_prev[0] = 0;
209 }
210 
211 template class deriv_lp_filter<double>;
212 template class deriv_lp_filter<float>;
213 
214 /*============================================================================*/
215 
216 template <typename T>
217 ff01_filter<T>::ff01_filter(float t_s, float w_c) {
218  Lpf_in_prev[0] = Lpf_in_prev[1] = 0;
219  Lpf_out_prev[0] = Lpf_out_prev[1] = 0;
220  Lpf_in1 = 0, Lpf_in2 = 0, Lpf_in3 = 0, Lpf_out1 = 0, Lpf_out2 = 0;
221  T a = 1.4142;
222  T den = 4 + 2 * a * w_c * t_s + t_s * t_s * w_c * w_c;
223  T J = 0.00008;
224  T B = 0.0002;
225 
226  Lpf_in1 = B * t_s * t_s * w_c * w_c + 2 * J * t_s * w_c * w_c;
227  Lpf_in2 = 2 * B * t_s * t_s * w_c * w_c;
228  Lpf_in3 = B * t_s * t_s * w_c * w_c - 2 * J * t_s * w_c * w_c;
229  Lpf_out1 = -1. * (-8 + t_s * t_s * w_c * w_c * 2) / den;
230  Lpf_out2 = -1. * (4 - 2 * a * w_c * t_s + t_s * t_s * w_c * w_c) / den;
231 }
232 
233 template <typename T>
235 
236 template <typename T>
237 void ff01_filter<T>::input(T lpf_in) {
238  lpf_out = Lpf_in1 * lpf_in + Lpf_in2 * Lpf_in_prev[0] +
239  Lpf_in3 * Lpf_in_prev[1] + // input component
240  Lpf_out1 * Lpf_out_prev[0] +
241  Lpf_out2 * Lpf_out_prev[1]; // output component
242  Lpf_in_prev[1] = Lpf_in_prev[0];
243  Lpf_in_prev[0] = lpf_in;
244  Lpf_out_prev[1] = Lpf_out_prev[0];
245  Lpf_out_prev[0] = lpf_out;
246 }
247 
248 template <typename T>
250  return lpf_out;
251 }
252 
253 template <typename T>
255  Lpf_in_prev[1] = 0;
256  Lpf_in_prev[0] = 0;
257  Lpf_out_prev[1] = 0;
258  Lpf_out_prev[0] = 0;
259 }
260 
261 template class ff01_filter<float>;
262 template class ff01_filter<double>;
263 
264 /*============================================================================*/
265 
266 template <typename T>
267 ff02_filter<T>::ff02_filter(float t_s, float w_c) {
268  T J = 0.003216;
269 
270  Lpf_in_prev[0] = Lpf_in_prev[1] = 0;
271  Lpf_out_prev[0] = Lpf_out_prev[1] = 0;
272  Lpf_in1 = 0, Lpf_in2 = 0, Lpf_in3 = 0, Lpf_out1 = 0, Lpf_out2 = 0;
273 
274  T a = 1.4142;
275  T den = 4 + 2 * a * w_c * t_s + t_s * t_s * w_c * w_c;
276 
277  Lpf_in1 = J * 2 * t_s * w_c * w_c / den;
278  Lpf_in2 = 0;
279  Lpf_in3 = -2. * J * t_s * w_c * w_c / den;
280  Lpf_out1 = -1. * (-8 + t_s * t_s * w_c * w_c * 2) / den;
281  Lpf_out2 = -1. * (4 - 2 * a * w_c * t_s + t_s * t_s * w_c * w_c) / den;
282 
283  clear();
284 }
285 
286 template <typename T>
288 
289 template <typename T>
290 void ff02_filter<T>::input(T lpf_in) {
291  lpf_out = Lpf_in1 * lpf_in + Lpf_in2 * Lpf_in_prev[0] +
292  Lpf_in3 * Lpf_in_prev[1] + // input component
293  Lpf_out1 * Lpf_out_prev[0] +
294  Lpf_out2 * Lpf_out_prev[1]; // output component
295  Lpf_in_prev[0] = lpf_in;
296  Lpf_in_prev[1] = Lpf_in_prev[0];
297  Lpf_out_prev[0] = lpf_out;
298  Lpf_out_prev[1] = Lpf_out_prev[0];
299 }
300 
301 template <typename T>
303  return lpf_out;
304 }
305 
306 template <typename T>
308  Lpf_in_prev[1] = 0;
309  Lpf_in_prev[0] = 0;
310  Lpf_out_prev[1] = 0;
311  Lpf_out_prev[0] = 0;
312 }
313 
314 template class ff02_filter<float>;
315 template class ff02_filter<double>;
316 
317 /*============================================================================*/
318 
319 template <typename T>
320 AverageFilter<T>::AverageFilter(T dt, T t_const, T limit)
321  : dt_(dt), t_const_(t_const), limit_(limit) {
322  est_value_ = 0.;
323 }
324 
325 template <typename T>
327  est_value_ = 0;
328 }
329 
330 template <typename T>
332  est_value_ = 0.;
333 }
334 
335 template <typename T>
337  T update_value = input - est_value_;
338  if (fabs(update_value) > limit_) {
339  update_value = 0.;
340  }
341  est_value_ += (dt_ / (dt_ + t_const_)) * update_value;
342 }
343 
344 template <typename T>
346  return est_value_;
347 }
348 
349 template class AverageFilter<float>;
350 template class AverageFilter<double>;
moving_average_filter(int num_data)
Definition: filters.cpp:10
ff01_filter(float t_s, float w_c)
Definition: filters.cpp:217
deriv_lp_filter(T w_c, T t_s)
Definition: filters.cpp:148
virtual T output(void)
Definition: filters.cpp:199
virtual ~moving_average_filter()
Definition: filters.cpp:37
virtual T output(void)
Definition: filters.cpp:84
virtual void clear(void)
Definition: filters.cpp:331
virtual T output(void)
Definition: filters.cpp:130
virtual void clear(void)
Definition: filters.cpp:89
virtual ~butterworth_filter(void)
Definition: filters.cpp:60
virtual void clear(void)
Definition: filters.cpp:204
virtual void input(T input_value)
Definition: filters.cpp:237
virtual ~ff02_filter(void)
Definition: filters.cpp:287
virtual ~ff01_filter(void)
Definition: filters.cpp:234
virtual T output(void)
Definition: filters.cpp:26
virtual ~digital_lp_filter(void)
Definition: filters.cpp:115
virtual void clear(void)
Definition: filters.cpp:254
virtual void clear(void)
Definition: filters.cpp:307
virtual void input(T input_value)
Definition: filters.cpp:65
virtual void input(T input_value)
Definition: filters.cpp:118
virtual void input(T input_value)
Definition: filters.cpp:174
virtual void input(T input_value)
Definition: filters.cpp:336
virtual ~deriv_lp_filter(void)
Definition: filters.cpp:171
virtual T output(void)
Definition: filters.cpp:249
ff02_filter(float t_s, float w_c)
Definition: filters.cpp:267
digital_lp_filter(T w_c, T t_s)
Definition: filters.cpp:101
virtual void clear(void)
Definition: filters.cpp:135
virtual void clear(void)
Definition: filters.cpp:31
virtual T output(void)
Definition: filters.cpp:345
virtual void input(T input_value)
Definition: filters.cpp:290
AverageFilter(T dt, T t_const, T limit)
Definition: filters.cpp:320
butterworth_filter(int num_sample, T dt, T cutoff_frequency)
Definition: filters.cpp:47
virtual ~AverageFilter()
Definition: filters.cpp:326
virtual void input(T input_value)
Definition: filters.cpp:17
virtual T output(void)
Definition: filters.cpp:302