TimeWindowAverageFilter.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 2019
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
25 //#include <ArmarXCore/core/logging/Logging.h>
26 namespace armarx::rtfilters
27 {
28 
29  TimeWindowAverageFilter::TimeWindowAverageFilter(IceUtil::Time timeWindowSize, double maxFrequency) :
30  RTFilterBase(timeWindowSize.toSecondsDouble() * maxFrequency),
31  timeWindowSize(timeWindowSize)
32  {
33  }
34 
36  {
37  if (dataHistory.empty())
38  {
39  return 0;
40  }
41  IceUtil::Time newestTimestamp = dataHistory.back().first;
42  IceUtil::Time timeBorder = (newestTimestamp - timeWindowSize);
43  double sum = 0;
44  int count = 0;
45  for (auto it = dataHistory.rbegin(); it != dataHistory.rend(); it++)
46  {
47  IceUtil::Time& t = it->first;
48  double val = it->second;
49  if (t > timeBorder)
50  {
51  count++;
52  sum += val;
53  }
54  else
55  {
56  // assume that timestamps are ordered
57  break;
58  }
59  }
60  if (count > 0)
61  {
62  return sum / count;
63  }
64  else
65  {
66  return 0;
67  }
68  }
69 
71  {
72  return std::make_shared<TimeWindowAverageFilter>(*this);
73  }
74 
75 }
TimeWindowAverageFilter.h
armarx::rtfilters::RTFilterBasePtr
std::shared_ptr< RTFilterBase > RTFilterBasePtr
Definition: RTFilterBase.h:34
armarx::rtfilters::TimeWindowAverageFilter::calculate
double calculate() override
Definition: TimeWindowAverageFilter.cpp:35
armarx::rtfilters
Definition: AverageFilter.cpp:26
armarx::rtfilters::TimeWindowAverageFilter::clone
RTFilterBasePtr clone() const override
Definition: TimeWindowAverageFilter.cpp:70
armarx::rtfilters::RTFilterBase::dataHistory
boost::circular_buffer< std::pair< IceUtil::Time, double > > dataHistory
Definition: RTFilterBase.h:54
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::rtfilters::TimeWindowAverageFilter::TimeWindowAverageFilter
TimeWindowAverageFilter(IceUtil::Time timeWindowSize, double maxFrequency)
Definition: TimeWindowAverageFilter.cpp:29