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"
27 
28 #include <cmath>
29 
30 namespace armarx::rtfilters
31 {
32 
33  ButterworthFilter::ButterworthFilter(double frequency, int sampleRate, PassType filterPassType, double resonance) :
34  RTFilterBase(0)
35  {
36  this->resonance = resonance;
37  this->frequency = frequency;
38  this->sampleRate = sampleRate;
39  this->filterPassType = filterPassType;
40  switch (filterPassType)
41  {
42  case Lowpass:
43  {
44  c = 1.0 / tan(M_PI * frequency / sampleRate);
46  a1 = 1.0 / (1.0 + resonance * c + c * c);
48  a2 = 2.0 * a1;
49  a3 = a1;
50  b1 = 2.0 * (1.0 - c * c) * a1;
51  b2 = (1.0 - resonance * c + c * c) * a1;
52  }
53  break;
54  case Highpass:
55  {
56  c = tan(M_PI * frequency / sampleRate);
57  a1 = 1.0 / (1.0 + resonance * c + c * c);
58  a2 = -2.0 * a1;
59  a3 = a1;
60  b1 = 2.0 * (c * c - 1.0) * a1;
61  b2 = (1.0 - resonance * c + c * c) * a1;
62  break;
63  }
64  default:
65  ARMARX_INFO << "Unknown pass type";
66 
67  }
68  }
69 
71  {
72  RTFilterBase* v = new ButterworthFilter(*this);
73  return RTFilterBasePtr(v);
74  }
75 
77  {
78  for (auto& v : inputHistory)
79  {
80  v = value;
81  }
82  for (auto& v : outputHistory)
83  {
84  v = value;
85  }
87  }
88 
90  {
92  double newOutput = a1 * newestValue + a2 * this->inputHistory[0] + a3 * this->inputHistory[1] - b1 * this->outputHistory[0] - b2 * this->outputHistory[1];
93  ARMARX_CHECK_EXPRESSION(!std::isnan(newOutput));
94  this->inputHistory[1] = this->inputHistory[0];
95  this->inputHistory[0] = newestValue;
96 
97  this->outputHistory[2] = this->outputHistory[1];
98  this->outputHistory[1] = this->outputHistory[0];
99  this->outputHistory[0] = newOutput;
100  return newOutput;
101  }
102 
103  double ButterworthFilter::update(const IceUtil::Time& timestamp, double newValue)
104  {
105  newestValue = newValue;
106  return calculate();
107  }
108 
109 
110 }
armarx::rtfilters::ButterworthFilter::a2
double a2
Definition: ButterworthFilter.h:64
armarx::rtfilters::ButterworthFilter::outputHistory
std::vector< double > outputHistory
Array of output values, latest are in front.
Definition: ButterworthFilter.h:50
armarx::rtfilters::RTFilterBasePtr
std::shared_ptr< RTFilterBase > RTFilterBasePtr
Definition: RTFilterBase.h:34
armarx::rtfilters::ButterworthFilter::newestValue
double newestValue
Definition: ButterworthFilter.h:45
armarx::rtfilters::ButterworthFilter::b2
double b2
Definition: ButterworthFilter.h:70
armarx::rtfilters::ButterworthFilter::resonance
double resonance
Definition: ButterworthFilter.h:52
armarx::rtfilters::ButterworthFilter::frequency
double frequency
Definition: ButterworthFilter.h:54
armarx::rtfilters::ButterworthFilter::setInitialValue
void setInitialValue(double value)
Definition: ButterworthFilter.cpp:76
armarx::rtfilters::ButterworthFilter::sampleRate
int sampleRate
Definition: ButterworthFilter.h:56
armarx::rtfilters
Definition: AverageFilter.cpp:26
armarx::rtfilters::ButterworthFilter::calculate
double calculate() override
Definition: ButterworthFilter.cpp:89
armarx::rtfilters::ButterworthFilter::update
double update(const IceUtil::Time &timestamp, double newValue) override
Definition: ButterworthFilter.cpp:103
armarx::rtfilters::ButterworthFilter::a1
double a1
Definition: ButterworthFilter.h:62
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::rtfilters::ButterworthFilter::filterPassType
::armarx::PassType filterPassType
Definition: ButterworthFilter.h:58
ButterworthFilter.h
armarx::rtfilters::ButterworthFilter::clone
RTFilterBasePtr clone() const override
Definition: ButterworthFilter.cpp:70
armarx::rtfilters::ButterworthFilter::c
double c
Definition: ButterworthFilter.h:60
armarx::rtfilters::ButterworthFilter::inputHistory
std::vector< double > inputHistory
Array of input values, latest are in front.
Definition: ButterworthFilter.h:47
armarx::rtfilters::ButterworthFilter::ButterworthFilter
ButterworthFilter(double frequency, int sampleRate, PassType filterPassType, double resonance)
Definition: ButterworthFilter.cpp:33
armarx::rtfilters::RTFilterBase
The RTFilterBase class is the base class for all real time capable filters.
Definition: RTFilterBase.h:40
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:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
Logging.h
armarx::rtfilters::ButterworthFilter::b1
double b1
Definition: ButterworthFilter.h:68
armarx::rtfilters::ButterworthFilter::a3
double a3
Definition: ButterworthFilter.h:66