ButterworthFilter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, 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 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "ButterworthFilter.h"
28 
29 #include <cmath>
30 
31 namespace armarx
32 {
34 
35  void armarx::filters::ButterworthFilter::reset(double frequency, int sampleRate, PassType filterPassType, double resonance)
36  {
37  this->resonance = resonance;
38  this->frequency = frequency;
39  this->sampleRate = sampleRate;
40  this->filterPassType = filterPassType;
41  switch (filterPassType)
42  {
43  case Lowpass:
44  c = 1.0 / tan(M_PI * frequency / sampleRate);
45  a1 = 1.0 / (1.0 + resonance * c + c * c);
46  a2 = 2.0 * a1;
47  a3 = a1;
48  b1 = 2.0 * (1.0 - c * c) * a1;
49  b2 = (1.0 - resonance * c + c * c) * a1;
50  break;
51  case Highpass:
52  c = tan(M_PI * frequency / sampleRate);
53  a1 = 1.0 / (1.0 + resonance * c + c * c);
54  a2 = -2.0 * a1;
55  a3 = a1;
56  b1 = 2.0 * (c * c - 1.0) * a1;
57  b2 = (1.0 - resonance * c + c * c) * a1;
58  break;
59  default:
60  ARMARX_INFO << "Unknown pass type";
61 
62  }
63  }
64 
65  armarx::filters::ButterworthFilter::ButterworthFilter(double frequency, int sampleRate, armarx::PassType filterPassType, double resonance)
66  {
67  reset(frequency, sampleRate, filterPassType, resonance);
68  }
69 
71  {
72  for (auto& v : inputHistory)
73  {
74  v = value;
75  }
76  for (auto& v : outputHistory)
77  {
78  v = value;
79  }
80  }
81 
82  armarx::ParameterTypeList armarx::filters::ButterworthFilter::getSupportedTypes(const Ice::Current&) const
83  {
84  ParameterTypeList result;
85  result.push_back(VariantType::Int);
86  result.push_back(VariantType::Long);
87  result.push_back(VariantType::Float);
88  result.push_back(VariantType::Double);
89  return result;
90  }
91 
93  {
94  //no lock required since no member data is changed
95  VariantPtr var = VariantPtr::dynamicCast(value);
96  if (!var->getInitialized())
97  {
98  return;
99  }
100 
101  double newInput = 0.0;
102  auto type = var->getType();
103  if (type == VariantType::Float)
104  {
105  newInput = static_cast<double>(var->getFloat());
106  }
107  else if (type == VariantType::Double)
108  {
109  newInput = var->getDouble();
110  }
111  else if (type == VariantType::Int)
112  {
113  newInput = static_cast<double>(var->getInt());
114  }
115  else if (type == VariantType::Long)
116  {
117  newInput = static_cast<double>(var->getLong());
118  }
119  update(newInput);
120  }
121 
123  {
124  std::unique_lock lock(historyMutex);
125  ARMARX_CHECK_EXPRESSION(!std::isnan(newInput));
126  double newOutput = a1 * newInput + a2 * this->inputHistory[0] + a3 * this->inputHistory[1] - b1 * this->outputHistory[0] - b2 * this->outputHistory[1];
127  ARMARX_CHECK_EXPRESSION(!std::isnan(newOutput));
128  this->inputHistory[1] = this->inputHistory[0];
129  this->inputHistory[0] = newInput;
130 
131  this->outputHistory[2] = this->outputHistory[1];
132  this->outputHistory[1] = this->outputHistory[0];
133  this->outputHistory[0] = newOutput;
134  }
135 
137  {
138  std::unique_lock lock(historyMutex);
139 
140  return new Variant(outputHistory[0]);
141  }
142 
144  {
145  std::unique_lock lock(historyMutex);
146  return outputHistory[0];
147  }
148 
150  {
151  std::unique_lock lock(historyMutex);
152 
153  return new Variant(outputHistory[0]);
154  }
155 
156 
157  StringFloatDictionary armarx::filters::ButterworthFilter::getProperties(const Ice::Current&) const
158  {
159  StringFloatDictionary props;
160  props["resonance"] = resonance;
161  props["frequency"] = frequency;
162  props["sampleRate"] = sampleRate;
163  props["filterPassType"] = (int)(filterPassType);
164  return props;
165  }
166 
167  void armarx::filters::ButterworthFilter::setProperties(const StringFloatDictionary& newValues, const Ice::Current&)
168  {
169  auto it = newValues.find("resonance");
170  if (it != newValues.end())
171  {
172  resonance = it->second;
173  }
174 
175  it = newValues.find("frequency");
176  if (it != newValues.end())
177  {
178  frequency = it->second;
179  }
180 
181  it = newValues.find("sampleRate");
182  if (it != newValues.end())
183  {
184  sampleRate = it->second;
185  }
186 
187  it = newValues.find("filterPassType");
188  if (it != newValues.end())
189  {
190  filterPassType = static_cast<PassType>(it->second);
191  }
192  reset(frequency, sampleRate, filterPassType, resonance);
193  }
194 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::filters::ButterworthFilter::update
void update(Ice::Long, const VariantBasePtr &value, const Ice::Current &=Ice::emptyCurrent) override
Definition: ButterworthFilter.cpp:92
armarx::filters::ButterworthFilter::setProperties
void setProperties(const StringFloatDictionary &values, const Ice::Current &) override
Definition: ButterworthFilter.cpp:167
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
ButterworthFilter.h
IceInternal::Handle<::armarx::VariantBase >
armarx::filters::ButterworthFilter::getProperties
StringFloatDictionary getProperties(const Ice::Current &) const override
Definition: ButterworthFilter.cpp:157
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:919
armarx::filters::ButterworthFilter::calculate
VariantBasePtr calculate(const Ice::Current &=Ice::emptyCurrent) const override
Definition: ButterworthFilter.cpp:149
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::filters::ButterworthFilter::reset
void reset(double frequency, int sampleRate, PassType passType, double resonance)
Definition: ButterworthFilter.cpp:35
armarx::filters::ButterworthFilter::getValue
VariantBasePtr getValue(const Ice::Current &=Ice::emptyCurrent) const override
Definition: ButterworthFilter.cpp:136
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
ExpressionException.h
armarx::filters::ButterworthFilter::ButterworthFilter
ButterworthFilter()
Definition: ButterworthFilter.cpp:33
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
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
Logging.h
armarx::filters::ButterworthFilter::setInitialValue
void setInitialValue(double value)
Definition: ButterworthFilter.cpp:70
armarx::filters::ButterworthFilter::getRawValue
double getRawValue() const
Definition: ButterworthFilter.cpp:143
armarx::filters::ButterworthFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &=Ice::emptyCurrent) const override
This filter supports: Int, Long, Float, Double.
Definition: ButterworthFilter.cpp:82
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28