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 
31 namespace armarx::rtfilters
32 {
33 
35  int sampleRate,
36  PassType filterPassType,
37  double resonance) :
38  RTFilterBase(0)
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  }
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
114  {
115  newestValue = newValue;
116  return calculate();
117  }
118 
119 
120 } // namespace armarx::rtfilters
armarx::rtfilters::ButterworthFilter::a2
double a2
Definition: ButterworthFilter.h:68
armarx::rtfilters::ButterworthFilter::outputHistory
std::vector< double > outputHistory
Array of output values, latest are in front.
Definition: ButterworthFilter.h:54
armarx::rtfilters::RTFilterBasePtr
std::shared_ptr< RTFilterBase > RTFilterBasePtr
Definition: RTFilterBase.h:34
armarx::rtfilters::ButterworthFilter::newestValue
double newestValue
Definition: ButterworthFilter.h:49
armarx::rtfilters::ButterworthFilter::b2
double b2
Definition: ButterworthFilter.h:74
armarx::rtfilters::ButterworthFilter::resonance
double resonance
Definition: ButterworthFilter.h:56
armarx::rtfilters::ButterworthFilter::frequency
double frequency
Definition: ButterworthFilter.h:58
armarx::rtfilters::ButterworthFilter::setInitialValue
void setInitialValue(double value)
Definition: ButterworthFilter.cpp:82
armarx::rtfilters::ButterworthFilter::sampleRate
int sampleRate
Definition: ButterworthFilter.h:60
armarx::rtfilters
Definition: AverageFilter.cpp:26
armarx::rtfilters::ButterworthFilter::calculate
double calculate() override
Definition: ButterworthFilter.cpp:96
armarx::rtfilters::ButterworthFilter::update
double update(const IceUtil::Time &timestamp, double newValue) override
Definition: ButterworthFilter.cpp:113
armarx::rtfilters::ButterworthFilter::a1
double a1
Definition: ButterworthFilter.h:66
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::rtfilters::ButterworthFilter::filterPassType
::armarx::PassType filterPassType
Definition: ButterworthFilter.h:62
ButterworthFilter.h
armarx::rtfilters::ButterworthFilter::clone
RTFilterBasePtr clone() const override
Definition: ButterworthFilter.cpp:75
armarx::rtfilters::ButterworthFilter::c
double c
Definition: ButterworthFilter.h:64
armarx::rtfilters::ButterworthFilter::inputHistory
std::vector< double > inputHistory
Array of input values, latest are in front.
Definition: ButterworthFilter.h:51
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
armarx::rtfilters::ButterworthFilter::ButterworthFilter
ButterworthFilter(double frequency, int sampleRate, PassType filterPassType, double resonance)
Definition: ButterworthFilter.cpp:34
armarx::rtfilters::RTFilterBase
The RTFilterBase class is the base class for all real time capable filters.
Definition: RTFilterBase.h:41
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
ExpressionException.h
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:198
Logging.h
armarx::rtfilters::ButterworthFilter::b1
double b1
Definition: ButterworthFilter.h:72
armarx::rtfilters::ButterworthFilter::a3
double a3
Definition: ButterworthFilter.h:70