Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpStatisticalTestAbstract.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
37
38#include <visp3/core/vpStatisticalTestAbstract.h>
39#include <visp3/core/vpException.h>
40
43{
44 std::string name;
45 switch (type) {
46 case MEAN_DRIFT_NONE:
47 name = "no_drift";
48 break;
50 name = "downward_drift";
51 break;
53 name = "upward_drift";
54 break;
55 case MEAN_DRIFT_BOTH:
56 name = "both_drift";
57 break;
59 name = "undefined_drift";
60 break;
61 default: {
62 throw(vpException(vpException::fatalError, "Unsupported mean drift type in vpStatisticalTestAbstract::vpMeanDriftTypeToString()"));
63 }
64 }
65 return name;
66}
67
69{
71 unsigned int count = static_cast<unsigned int>(MEAN_DRIFT_COUNT);
72 unsigned int id = 0;
73 bool hasNotFound = true;
74 while ((id < count) && hasNotFound) {
75 vpMeanDriftType temp = static_cast<vpMeanDriftType>(id);
76 if (vpMeanDriftTypeToString(temp) == name) {
77 result = temp;
78 hasNotFound = false;
79 }
80 ++id;
81 }
82 return result;
83}
84
85std::string vpStatisticalTestAbstract::getAvailableMeanDriftType(const std::string &prefix, const std::string &sep,
86 const std::string &suffix)
87{
88 std::string msg(prefix);
89 unsigned int count = static_cast<unsigned int>(MEAN_DRIFT_COUNT);
90 unsigned int lastId = count - 1;
91 for (unsigned int i = 0; i < lastId; i++) {
92 msg += vpMeanDriftTypeToString(static_cast<vpMeanDriftType>(i)) + sep;
93 }
94 msg += vpMeanDriftTypeToString(static_cast<vpMeanDriftType>(lastId)) + suffix;
95 return msg;
96}
97
99{
100 std::cout << vpMeanDriftTypeToString(type) << " detected" << std::endl;
101}
102
104{
105 m_s[static_cast<unsigned int>(m_count)] = signal;
106 m_count += 1.f;
107 m_sumForMean += signal;
108 if (static_cast<unsigned int> (m_count) >= m_nbSamplesForStatistics) {
109 // Computation of the mean
111
112 // Computation of the stdev
113 float sumSquaredDiff = 0.f;
114 unsigned int count = static_cast<unsigned int>(m_nbSamplesForStatistics);
115 for (unsigned int i = 0; i < count; ++i) {
116 sumSquaredDiff += (m_s[i] - m_mean) * (m_s[i] - m_mean);
117 }
118 float stdev = std::sqrt(sumSquaredDiff / m_count);
119 if (m_stdevmin < 0) {
120 m_stdev = stdev;
121 }
122 else {
123 m_stdev = std::max<float>(m_stdev, m_stdevmin);
124 }
125
127 }
129}
130
133 , m_count(0.f)
134 , m_limitDown(0.f)
135 , m_limitUp(0.f)
136 , m_mean(0.f)
138 , m_s(nullptr)
139 , m_stdev(0.f)
140 , m_stdevmin(-1.f)
141 , m_sumForMean(0.f)
142{ }
143
148
150{
151 if (m_s != nullptr) {
152 delete[] m_s;
153 m_s = nullptr;
154 }
155}
156
158{
160 m_count = 0.f;
161 m_limitDown = 0.f;
162 m_limitUp = 0.f;
163 m_mean = 0.f;
165 if (m_s != nullptr) {
166 delete[] m_s;
167 m_s = nullptr;
168 }
169 m_stdev = 0.f;
170 m_sumForMean = 0.f;
171}
172
174{
176 m_count = other.m_count;
177 m_limitDown = other.m_limitDown;
178 m_limitUp = other.m_limitUp;
179 m_mean = other.m_mean;
180 if (other.m_nbSamplesForStatistics > 0) {
182 }
183 else if (m_s != nullptr) {
185 delete[] m_s;
186 m_s = nullptr;
187 }
188 m_stdev = 0.f;
189 m_sumForMean = 0.f;
190 return *this;
191}
192
193void vpStatisticalTestAbstract::setNbSamplesForStat(const unsigned int &nbSamples)
194{
195 m_nbSamplesForStatistics = nbSamples;
196 if (m_s != nullptr) {
197 delete[] m_s;
198 }
199 m_s = new float[nbSamples];
200}
201
203{
205 updateTestSignals(signal);
208 if ((jumpDown != MEAN_DRIFT_NONE) && (jumpUp != MEAN_DRIFT_NONE)) {
209 return MEAN_DRIFT_BOTH;
210 }
211 else if (jumpDown != MEAN_DRIFT_NONE) {
212 return jumpDown;
213 }
214 else if (jumpUp != MEAN_DRIFT_NONE) {
215 return jumpUp;
216 }
217 else {
218 return MEAN_DRIFT_NONE;
219 }
220 }
221 else {
222 updateStatistics(signal);
223 return MEAN_DRIFT_NONE;
224 }
225}
226
238
240{
242 updateTestSignals(signal);
243 return detectUpwardMeanDrift();
244 }
245 else {
246 updateStatistics(signal);
247 return MEAN_DRIFT_NONE;
248 }
249}
250END_VISP_NAMESPACE
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ fatalError
Fatal error.
Definition vpException.h:72
static std::string vpMeanDriftTypeToString(const vpMeanDriftType &type)
Cast a vpMeanDriftType into a string.
virtual ~vpStatisticalTestAbstract()
Destroy the vpStatisticalTestAbstract object.
vpMeanDriftType
Enum that indicates if a drift of the mean occurred.
static vpMeanDriftType vpMeanDriftTypeFromString(const std::string &name)
Cast a string into a vpMeanDriftType.
virtual vpMeanDriftType detectUpwardMeanDrift()=0
Detects if a upward mean drift occurred.
vpMeanDriftType testUpwardMeanDrift(const float &signal)
Test if an upward mean drift occurred according to the new value of the signal.
vpMeanDriftType testDownwardMeanDrift(const float &signal)
Test if a downward mean drift occurred according to the new value of the signal.
void init()
(Re)Initialize the algorithm.
vpStatisticalTestAbstract()
Construct a new vpStatisticalTestAbstract object.
virtual bool updateStatistics(const float &signal)
Update m_s and if enough values are available, compute the mean, the standard deviation and the limit...
virtual void updateTestSignals(const float &signal)=0
Update the test signals.
void setNbSamplesForStat(const unsigned int &nbSamples)
Set the number of samples required to compute the mean and standard deviation of the signal and alloc...
vpMeanDriftType testDownUpwardMeanDrift(const float &signal)
Test if a downward or an upward mean drift occurred according to the new value of the signal.
static void print(const vpMeanDriftType &type)
Print the message corresponding to the type of mean drift.
virtual vpMeanDriftType detectDownwardMeanDrift()=0
Detects if a downward mean drift occurred.
vpStatisticalTestAbstract & operator=(const vpStatisticalTestAbstract &other)
Copy operator of a vpStatisticalTestAbstract.
static std::string getAvailableMeanDriftType(const std::string &prefix="<", const std::string &sep=" , ", const std::string &suffix=">")
Get the list of available vpMeanDriftType objects that are handled.