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 <math.h>
26 
27 #include <vector>
28 
30 
31 #include "StatUtils.h"
32 
33 namespace armarx::math
34 {
35  class SlidingWindowVectorMedian;
36  using SlidingWindowVectorMedianPtr = std::shared_ptr<SlidingWindowVectorMedian>;
37 
39  {
40  private:
41  size_t windowSize;
42  size_t vectorSize;
43  std::vector<float> data;
44  size_t currentIndex;
45  bool fullCycle;
46 
47  public:
48  SlidingWindowVectorMedian(size_t vectorSize, size_t windowSize) :
49  windowSize(windowSize),
50  vectorSize(vectorSize),
51  data(vectorSize * windowSize, 0), // initialize all data to 0
52  currentIndex(0),
53  fullCycle(false)
54  {
55  }
56 
57  void
58  addEntry(const std::vector<float>& entry)
59  {
60  if (entry.size() != vectorSize)
61  {
62  throw LocalException("Vector of wrong size added. Execting: ")
63  << vectorSize << "; Actual: " << entry.size();
64  }
65 
66  for (size_t i = 0; i < entry.size(); i++)
67  {
68  data.at(i + currentIndex * vectorSize) = entry.at(i);
69  }
70 
71  currentIndex = (currentIndex + 1) % windowSize;
72  fullCycle = fullCycle || currentIndex == 0;
73  }
74 
75  std::vector<float>
77  {
78  std::vector<float> median;
79 
80  for (size_t i = 0; i < vectorSize; i++)
81  {
82  std::vector<float> samples;
83 
84  for (size_t n = 0; n < windowSize; n++)
85  {
86  samples.push_back(data.at(i + n * vectorSize));
87  }
88 
89  std::sort(samples.begin(), samples.end());
90  median.push_back(StatUtils::GetMedian(samples));
91  }
92 
93  return median;
94  }
95  };
96 } // namespace armarx::math
StatUtils.h
armarx::math::SlidingWindowVectorMedian::getMedian
std::vector< float > getMedian()
Definition: SlidingWindowVectorMedian.h:76
armarx::math::SlidingWindowVectorMedian::SlidingWindowVectorMedian
SlidingWindowVectorMedian(size_t vectorSize, size_t windowSize)
Definition: SlidingWindowVectorMedian.h:48
armarx::math::SlidingWindowVectorMedian::addEntry
void addEntry(const std::vector< float > &entry)
Definition: SlidingWindowVectorMedian.h:58
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::math::SlidingWindowVectorMedian
Definition: SlidingWindowVectorMedian.h:38
armarx::math::SlidingWindowVectorMedianPtr
std::shared_ptr< SlidingWindowVectorMedian > SlidingWindowVectorMedianPtr
Definition: SlidingWindowVectorMedian.h:36
armarx::math
Definition: LinearizeAngularTrajectory.cpp:28
armarx::math::StatUtils::GetMedian
static float GetMedian(const std::vector< float > &sortedData)
Definition: StatUtils.h:72
Exception.h