ButterworthFilter.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX
19 * @author Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2017
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "ButterworthFilter.h"
25
26#include <cmath>
27
30
31namespace armarx::rtfilters
32{
33
35 int sampleRate,
36 PassType filterPassType,
37 double resonance) :
39 {
40 this->resonance = resonance;
41 this->frequency = frequency;
42 this->sampleRate = sampleRate;
43 this->filterPassType = filterPassType;
44 switch (filterPassType)
45 {
46 case Lowpass:
47 {
48 c = 1.0 / tan(M_PI * frequency / sampleRate);
50 a1 = 1.0 / (1.0 + resonance * c + c * c);
51 ARMARX_CHECK_EXPRESSION(!std::isnan(a1))
53 a2 = 2.0 * a1;
54 a3 = a1;
55 b1 = 2.0 * (1.0 - c * c) * a1;
56 b2 = (1.0 - resonance * c + c * c) * a1;
57 }
58 break;
59 case Highpass:
60 {
61 c = tan(M_PI * frequency / sampleRate);
62 a1 = 1.0 / (1.0 + resonance * c + c * c);
63 a2 = -2.0 * a1;
64 a3 = a1;
65 b1 = 2.0 * (c * c - 1.0) * a1;
66 b2 = (1.0 - resonance * c + c * c) * a1;
67 break;
68 }
69 default:
70 ARMARX_INFO << "Unknown pass type";
71 }
72 }
73
76 {
77 RTFilterBase* v = new ButterworthFilter(*this);
78 return RTFilterBasePtr(v);
79 }
80
81 void
83 {
84 for (auto& v : inputHistory)
85 {
86 v = value;
87 }
88 for (auto& v : outputHistory)
89 {
90 v = value;
91 }
92 newestValue = value;
93 }
94
95 double
97 {
99 double newOutput = a1 * newestValue + a2 * this->inputHistory[0] +
100 a3 * this->inputHistory[1] - b1 * this->outputHistory[0] -
101 b2 * this->outputHistory[1];
102 ARMARX_CHECK_EXPRESSION(!std::isnan(newOutput));
103 this->inputHistory[1] = this->inputHistory[0];
104 this->inputHistory[0] = newestValue;
105
106 this->outputHistory[2] = this->outputHistory[1];
107 this->outputHistory[1] = this->outputHistory[0];
108 this->outputHistory[0] = newOutput;
109 return newOutput;
110 }
111
112 double
113 ButterworthFilter::update(const IceUtil::Time& timestamp, double newValue)
114 {
115 newestValue = newValue;
116 return calculate();
117 }
118
119
120} // namespace armarx::rtfilters
std::string timestamp()
#define M_PI
Definition MathTools.h:17
#define VAROUT(x)
std::vector< double > inputHistory
Array of input values, latest are in front.
ButterworthFilter(double frequency, int sampleRate, PassType filterPassType, double resonance)
double update(const IceUtil::Time &timestamp, double newValue) override
std::vector< double > outputHistory
Array of output values, latest are in front.
RTFilterBasePtr clone() const override
RTFilterBase(size_t historySize)
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
std::shared_ptr< RTFilterBase > RTFilterBasePtr