SlidingWindowVectorMedian.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::Core
17 * @author Simon Ottenhaus ( simon dot ottenhaus at kit dot edu )
18 * @date 2015
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #pragma once
24 
25 #include "StatUtils.h"
26 
27 #include <math.h>
28 #include <vector>
29 
31 
32 namespace armarx::math
33 {
34  class SlidingWindowVectorMedian;
35  using SlidingWindowVectorMedianPtr = std::shared_ptr<SlidingWindowVectorMedian>;
36 
38  {
39  private:
40  size_t windowSize;
41  size_t vectorSize;
42  std::vector<float> data;
43  size_t currentIndex;
44  bool fullCycle;
45 
46  public:
47  SlidingWindowVectorMedian(size_t vectorSize, size_t windowSize)
48  : windowSize(windowSize),
49  vectorSize(vectorSize),
50  data(vectorSize * windowSize, 0), // initialize all data to 0
51  currentIndex(0),
52  fullCycle(false)
53  {
54  }
55 
56  void addEntry(const std::vector<float>& entry)
57  {
58  if (entry.size() != vectorSize)
59  {
60  throw LocalException("Vector of wrong size added. Execting: ") << vectorSize << "; Actual: " << entry.size();
61  }
62 
63  for (size_t i = 0; i < entry.size(); i++)
64  {
65  data.at(i + currentIndex * vectorSize) = entry.at(i);
66  }
67 
68  currentIndex = (currentIndex + 1) % windowSize;
69  fullCycle = fullCycle || currentIndex == 0;
70  }
71 
72  std::vector<float> getMedian()
73  {
74  std::vector<float> median;
75 
76  for (size_t i = 0; i < vectorSize; i++)
77  {
78  std::vector<float> samples;
79 
80  for (size_t n = 0; n < windowSize; n++)
81  {
82  samples.push_back(data.at(i + n * vectorSize));
83  }
84 
85  std::sort(samples.begin(), samples.end());
86  median.push_back(StatUtils::GetMedian(samples));
87  }
88 
89  return median;
90  }
91 
92  };
93 }
94 
95 
StatUtils.h
armarx::math::SlidingWindowVectorMedian::getMedian
std::vector< float > getMedian()
Definition: SlidingWindowVectorMedian.h:72
armarx::math::SlidingWindowVectorMedian::SlidingWindowVectorMedian
SlidingWindowVectorMedian(size_t vectorSize, size_t windowSize)
Definition: SlidingWindowVectorMedian.h:47
armarx::math::SlidingWindowVectorMedian::addEntry
void addEntry(const std::vector< float > &entry)
Definition: SlidingWindowVectorMedian.h:56
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::math::SlidingWindowVectorMedian
Definition: SlidingWindowVectorMedian.h:37
armarx::math::SlidingWindowVectorMedianPtr
std::shared_ptr< SlidingWindowVectorMedian > SlidingWindowVectorMedianPtr
Definition: SlidingWindowVectorMedian.h:35
armarx::math
Definition: LinearizeAngularTrajectory.cpp:27
armarx::math::StatUtils::GetMedian
static float GetMedian(const std::vector< float > &sortedData)
Definition: StatUtils.h:67
Exception.h