Main MRPT website > C++ reference for MRPT 1.9.9
TSlidingWindow.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 // Implementattion file for TSlidingWindow struct
11 #include "graphslam-precomp.h" // Precompiled headers
12 
14 #include <mrpt/utils/CStream.h>
16 
17 using namespace mrpt::graphslam;
18 
20 {
21  MRPT_START;
22 
23  m_win_size = 5; // just a default value
24  m_name = name;
25 
26  m_mean_cached = 0;
27  m_median_cached = 0;
28 
29  m_is_initialized = false;
30  m_mean_updated = false;
31  m_median_updated = false;
32  m_std_dev_updated = false;
33 
34  MRPT_END;
35 }
38 {
39  MRPT_START;
40 
41  double median_out = 0.0;
42  if (m_measurements_vec.empty())
43  {
44  return 0.0;
45  }
46 
47  if (m_median_updated)
48  {
49  median_out = m_median_cached;
50  }
51  else
52  {
53  // copy the current vector, sort it and return value in middle
54  std::vector<double> vec_sorted(m_measurements_vec);
55  std::sort(vec_sorted.begin(), vec_sorted.end());
56 
57  median_out = vec_sorted.at(vec_sorted.size() / 2);
58 
59  m_median_cached = median_out;
60  m_median_updated = true;
61  }
62 
63  return median_out;
64 
65  MRPT_END;
66 }
68 {
69  MRPT_START;
70 
71  double mean_out = 0.0;
72 
73  if (m_mean_updated)
74  {
75  mean_out = m_mean_cached;
76  }
77  else
78  {
79  mean_out = std::accumulate(
80  m_measurements_vec.begin(), m_measurements_vec.end(), 0.0);
81  mean_out /= m_measurements_vec.size();
82 
83  m_mean_cached = mean_out;
84  m_mean_updated = true;
85  }
86 
87  return mean_out;
88 
89  MRPT_END;
90 }
92 {
93  MRPT_START;
94 
95  double std_dev_out = 0.0;
96 
98  { // return the cached version?
99  std_dev_out = m_std_dev_cached;
100  }
101  else
102  {
103  double mean = this->getMean();
104 
105  double sum_of_sq_diffs = 0;
107  m_measurements_vec.begin();
108  it != m_measurements_vec.end(); ++it)
109  {
110  sum_of_sq_diffs += std::pow(*it - mean, 2);
111  }
112  std_dev_out = sqrt(sum_of_sq_diffs / m_win_size);
113 
114  m_std_dev_cached = std_dev_out;
115  m_std_dev_updated = true;
116  }
117 
118  return std_dev_out;
119  MRPT_END;
120 }
121 
123 {
124  // get the boundaries for acceptance of measurements - [-3sigma, 3sigma]
125  // with
126  // regards to the mean
127  double low_lim = this->getMean() - 3 * this->getStdDev();
128  double upper_lim = this->getMean() + 3 * this->getStdDev();
129 
130  return measurement > low_lim && measurement < upper_lim;
131 }
133 {
134  MRPT_START;
135 
136  double threshold = this->getMean();
137  return (value > threshold);
138 
139  MRPT_END;
140 }
142 {
144 }
145 
146 void TSlidingWindow::addNewMeasurement(double measurement)
147 {
148  MRPT_START;
149 
150  m_is_initialized = true;
151 
152  // if I haven't already filled up to win_size the vector, just add it
153  if (m_win_size > m_measurements_vec.size())
154  {
155  m_measurements_vec.push_back(measurement);
156  }
157  else
158  {
159  // remove first element - add it as last element
160  m_measurements_vec.erase(m_measurements_vec.begin());
161  m_measurements_vec.push_back(measurement);
162  }
163 
164  m_mean_updated = false;
165  m_median_updated = false;
166  m_std_dev_updated = false;
167 
168  MRPT_END;
169 }
170 void TSlidingWindow::resizeWindow(size_t new_size)
171 {
172  MRPT_START;
173 
174  size_t curr_size = m_measurements_vec.size();
175  if (new_size < curr_size)
176  {
177  // remove (curr_size - new_size) elements from the beginning of the
178  // measurements vector
179  m_measurements_vec.erase(
180  m_measurements_vec.begin(),
181  m_measurements_vec.begin() + (curr_size - new_size));
182 
183  m_mean_updated = false;
184  m_median_updated = false;
185  }
186 
187  m_win_size = new_size;
188 
189  MRPT_END;
190 }
192  const mrpt::utils::CConfigFileBase& source, const std::string& section)
193 {
194  MRPT_START;
195 
196  size_t sliding_win_size =
197  source.read_int(section, "sliding_win_size", 10, false);
198  this->resizeWindow(sliding_win_size);
199 
200  MRPT_END;
201 }
203 {
204  MRPT_START;
205 
206  out.printf(
207  "-----------[ %s: Sliding Window Properties ]-----------\n",
208  m_name.c_str());
209  out.printf("Measurements Vector: \n");
211  it != m_measurements_vec.end(); ++it)
212  {
213  out.printf("\t%.2f\n", *it);
214  }
215  out.printf("\n");
216 
217  out.printf("m_name : %s\n", m_name.c_str());
218  out.printf("m_mean_cached : %.2f\n", m_mean_cached);
219  out.printf("m_median_cached : %.2f\n", m_median_cached);
220  out.printf("m_std_dev_cached : %.2f\n", m_std_dev_cached);
221  out.printf("m_mean_updated : %s\n", m_mean_updated ? "TRUE" : "FALSE");
222  out.printf(
223  "m_median_updated : %s\n", m_median_updated ? "TRUE" : "FALSE");
224  out.printf(
225  "m_std_dev_updated : %s\n", m_std_dev_updated ? "TRUE" : "FALSE");
226  out.printf("m_win_size : %lu\n", m_win_size);
227  out.printf(
228  "m_is_initialized : %s\n", m_is_initialized ? "TRUE" : "FALSE");
229 
230  MRPT_END;
231 }
232 
233 size_t TSlidingWindow::getWindowSize() const { return m_win_size; }
235 {
236  return (m_win_size == m_measurements_vec.size());
237 }
This class allows loading and storing values and vectors of different types from a configuration text...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:42
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
Definition: CStream.cpp:597
EIGEN_STRONG_INLINE double mean() const
Computes the mean of the entire matrix.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
GLuint const GLchar * name
Definition: glext.h:4054
GLsizei const GLfloat * value
Definition: glext.h:4117
GLsizei const GLchar ** string
Definition: glext.h:4101
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
#define MRPT_START
Definition: mrpt_macros.h:425
#define MRPT_END
Definition: mrpt_macros.h:429
SLAM methods related to graphs of pose constraints.
bool m_is_initialized
flag is raised the first time that TSlidingWindow::addNewMeasurement is called
bool evaluateMeasurementBelow(double value)
Determine whether the incoming measurement is less or equal to the current mean value.
double getMean()
Return the current mean value.
bool evaluateMeasurementInGaussian(double measurement)
Determine whether the incoming measurement is inside the [-3sigma, +3sigma] boundaries from the curre...
double m_mean_cached
Cached mean value.
TSlidingWindow(std::string name="window")
std::string m_name
Name of the TSlidingWindow Instance at hand.
void dumpToTextStream(mrpt::utils::CStream &out) const
This method should clearly display all the contents of the structure in textual form,...
bool windowIsFull() const
Check if the window has reached its limit.
double m_median_cached
Cached median value.
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &section)
This method load the options from a ".ini"-like file or memory-stored string list.
bool evaluateMeasurementAbove(double value)
Determine whether the incoming measurement is over the current mean value.
void resizeWindow(size_t new_size)
Resize the window.
bool m_median_updated
Is the median up-to-date?
void addNewMeasurement(double measurement)
Update the sliding window by appending a new measurement.
bool m_mean_updated
Is the mean up-to-date?
bool m_std_dev_updated
Is the standard deviation up-to-date?
std::vector< double > m_measurements_vec
double m_std_dev_cached
Cached version of the standard deviation.
double getMedian()
Return the current median value.
double getStdDev()
Return the Standard deviation of the current measurement vector.
size_t getWindowSize() const
Return the size of the window.



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST