GaussianFilter.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 "GaussianFilter.h"
25 
26 #include <cmath>
27 
28 namespace armarx::rtfilters
29 {
30 
31  GaussianFilter::GaussianFilter(size_t windowSize, double filterWidthInMs) :
32  RTFilterBase(windowSize), filterWidthInSec(0.001f * filterWidthInMs)
33  {
34  }
35 
36  GaussianFilter::GaussianFilter(size_t windowSize, IceUtil::Time filterWidth) :
37  RTFilterBase(windowSize), filterWidthInSec(filterWidth.toSecondsDouble())
38  {
39  }
40 
43  {
44  RTFilterBase* v = new GaussianFilter(*this);
45  return RTFilterBasePtr(v);
46  }
47 
48  double
50  {
51  if (dataHistory.empty())
52  {
53  return 0.0;
54  }
55 
56  const double sigma = filterWidthInSec / 3.0;
57  //-log(0.05) / pow(itemsToCheck+1+centerIndex,2) * ( sqrt(1.0/h) * sqrt(2*3.14159265));
58 
59  double weightedSum = 0;
60  double sumOfWeight = 0;
61 
62 
63  auto newestTimestamp = dataHistory.rbegin()->first.toMilliSecondsDouble();
64 
65  const double sqrt2PI = std::sqrt(2 * M_PI);
66  double quotient = (2 * sigma * sigma);
67  double quotient2 = sigma * sqrt2PI;
68 
69  for (auto it = dataHistory.begin(); it != dataHistory.end(); it++)
70  {
71  double value = it->second;
72  double diff = 0.001 * (it->first.toMilliSecondsDouble() - newestTimestamp);
73 
74  double squared = diff * diff;
75  const double gaussValue = std::exp(-squared / quotient) / (quotient2);
76  sumOfWeight += gaussValue;
77  weightedSum += gaussValue * value;
78  }
79 
80  double result;
81  result = weightedSum / sumOfWeight;
82  return result;
83  }
84 
85 } // namespace armarx::rtfilters
GaussianFilter.h
armarx::rtfilters::RTFilterBasePtr
std::shared_ptr< RTFilterBase > RTFilterBasePtr
Definition: RTFilterBase.h:34
armarx::rtfilters
Definition: AverageFilter.cpp:26
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::rtfilters::GaussianFilter::GaussianFilter
GaussianFilter(size_t windowSize, double filterWidthInMs)
Definition: GaussianFilter.cpp:31
armarx::rtfilters::GaussianFilter::clone
RTFilterBasePtr clone() const override
Definition: GaussianFilter.cpp:42
armarx::rtfilters::RTFilterBase::dataHistory
boost::circular_buffer< std::pair< IceUtil::Time, double > > dataHistory
Definition: RTFilterBase.h:59
armarx::rtfilters::GaussianFilter::filterWidthInSec
double filterWidthInSec
Definition: GaussianFilter.h:44
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
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:704
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::rtfilters::GaussianFilter::calculate
double calculate() override
Definition: GaussianFilter.cpp:49