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