Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
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
41
BEGIN_VISP_NAMESPACE
42
std::string
vpStatisticalTestAbstract::vpMeanDriftTypeToString
(
const
vpStatisticalTestAbstract::vpMeanDriftType
&type)
43
{
44
std::string name;
45
switch
(type) {
46
case
MEAN_DRIFT_NONE
:
47
name =
"no_drift"
;
48
break
;
49
case
MEAN_DRIFT_DOWNWARD
:
50
name =
"downward_drift"
;
51
break
;
52
case
MEAN_DRIFT_UPWARD
:
53
name =
"upward_drift"
;
54
break
;
55
case
MEAN_DRIFT_BOTH
:
56
name =
"both_drift"
;
57
break
;
58
case
MEAN_DRIFT_UNKNOWN
:
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
68
vpStatisticalTestAbstract::vpMeanDriftType
vpStatisticalTestAbstract::vpMeanDriftTypeFromString
(
const
std::string &name)
69
{
70
vpMeanDriftType
result =
MEAN_DRIFT_UNKNOWN
;
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
85
std::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
98
void
vpStatisticalTestAbstract::print
(
const
vpStatisticalTestAbstract::vpMeanDriftType
&type)
99
{
100
std::cout <<
vpMeanDriftTypeToString
(type) <<
" detected"
<< std::endl;
101
}
102
103
bool
vpStatisticalTestAbstract::updateStatistics
(
const
float
&signal)
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
110
m_mean
=
m_sumForMean
/
m_count
;
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
126
m_areStatisticsComputed
=
true
;
127
}
128
return
m_areStatisticsComputed
;
129
}
130
131
vpStatisticalTestAbstract::vpStatisticalTestAbstract
()
132
:
m_areStatisticsComputed
(false)
133
,
m_count
(0.f)
134
,
m_limitDown
(0.f)
135
,
m_limitUp
(0.f)
136
,
m_mean
(0.f)
137
,
m_nbSamplesForStatistics
(0)
138
,
m_s
(nullptr)
139
,
m_stdev
(0.f)
140
,
m_stdevmin
(-1.f)
141
,
m_sumForMean
(0.f)
142
{ }
143
144
vpStatisticalTestAbstract::vpStatisticalTestAbstract
(
const
vpStatisticalTestAbstract
&other)
145
{
146
*
this
= other;
147
}
148
149
vpStatisticalTestAbstract::~vpStatisticalTestAbstract
()
150
{
151
if
(
m_s
!=
nullptr
) {
152
delete
[]
m_s
;
153
m_s
=
nullptr
;
154
}
155
}
156
157
void
vpStatisticalTestAbstract::init
()
158
{
159
m_areStatisticsComputed
=
false
;
160
m_count
= 0.f;
161
m_limitDown
= 0.f;
162
m_limitUp
= 0.f;
163
m_mean
= 0.f;
164
m_nbSamplesForStatistics
= 0;
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
173
vpStatisticalTestAbstract
&
vpStatisticalTestAbstract::operator=
(
const
vpStatisticalTestAbstract
&other)
174
{
175
m_areStatisticsComputed
= other.
m_areStatisticsComputed
;
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) {
181
setNbSamplesForStat
(other.
m_nbSamplesForStatistics
);
182
}
183
else
if
(
m_s
!=
nullptr
) {
184
m_nbSamplesForStatistics
= 0;
185
delete
[]
m_s
;
186
m_s
=
nullptr
;
187
}
188
m_stdev
= 0.f;
189
m_sumForMean
= 0.f;
190
return
*
this
;
191
}
192
193
void
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
202
vpStatisticalTestAbstract::vpMeanDriftType
vpStatisticalTestAbstract::testDownUpwardMeanDrift
(
const
float
&signal)
203
{
204
if
(
m_areStatisticsComputed
) {
205
updateTestSignals
(signal);
206
vpMeanDriftType
jumpDown =
detectDownwardMeanDrift
();
207
vpMeanDriftType
jumpUp =
detectUpwardMeanDrift
();
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
227
vpStatisticalTestAbstract::vpMeanDriftType
vpStatisticalTestAbstract::testDownwardMeanDrift
(
const
float
&signal)
228
{
229
if
(
m_areStatisticsComputed
) {
230
updateTestSignals
(signal);
231
return
detectDownwardMeanDrift
();
232
}
233
else
{
234
updateStatistics
(signal);
235
return
MEAN_DRIFT_NONE
;
236
}
237
}
238
239
vpStatisticalTestAbstract::vpMeanDriftType
vpStatisticalTestAbstract::testUpwardMeanDrift
(
const
float
&signal)
240
{
241
if
(
m_areStatisticsComputed
) {
242
updateTestSignals
(signal);
243
return
detectUpwardMeanDrift
();
244
}
245
else
{
246
updateStatistics
(signal);
247
return
MEAN_DRIFT_NONE
;
248
}
249
}
250
END_VISP_NAMESPACE
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpException::fatalError
@ fatalError
Fatal error.
Definition
vpException.h:72
vpStatisticalTestAbstract::m_sumForMean
float m_sumForMean
Definition
vpStatisticalTestAbstract.h:123
vpStatisticalTestAbstract::vpMeanDriftTypeToString
static std::string vpMeanDriftTypeToString(const vpMeanDriftType &type)
Cast a vpMeanDriftType into a string.
Definition
vpStatisticalTestAbstract.cpp:42
vpStatisticalTestAbstract::m_stdevmin
float m_stdevmin
Definition
vpStatisticalTestAbstract.h:122
vpStatisticalTestAbstract::m_limitUp
float m_limitUp
Definition
vpStatisticalTestAbstract.h:117
vpStatisticalTestAbstract::~vpStatisticalTestAbstract
virtual ~vpStatisticalTestAbstract()
Destroy the vpStatisticalTestAbstract object.
Definition
vpStatisticalTestAbstract.cpp:149
vpStatisticalTestAbstract::vpMeanDriftType
vpMeanDriftType
Enum that indicates if a drift of the mean occurred.
Definition
vpStatisticalTestAbstract.h:70
vpStatisticalTestAbstract::MEAN_DRIFT_UPWARD
@ MEAN_DRIFT_UPWARD
Definition
vpStatisticalTestAbstract.h:73
vpStatisticalTestAbstract::MEAN_DRIFT_COUNT
@ MEAN_DRIFT_COUNT
Definition
vpStatisticalTestAbstract.h:75
vpStatisticalTestAbstract::MEAN_DRIFT_UNKNOWN
@ MEAN_DRIFT_UNKNOWN
Definition
vpStatisticalTestAbstract.h:76
vpStatisticalTestAbstract::MEAN_DRIFT_DOWNWARD
@ MEAN_DRIFT_DOWNWARD
Definition
vpStatisticalTestAbstract.h:72
vpStatisticalTestAbstract::MEAN_DRIFT_BOTH
@ MEAN_DRIFT_BOTH
Definition
vpStatisticalTestAbstract.h:74
vpStatisticalTestAbstract::MEAN_DRIFT_NONE
@ MEAN_DRIFT_NONE
Definition
vpStatisticalTestAbstract.h:71
vpStatisticalTestAbstract::vpMeanDriftTypeFromString
static vpMeanDriftType vpMeanDriftTypeFromString(const std::string &name)
Cast a string into a vpMeanDriftType.
Definition
vpStatisticalTestAbstract.cpp:68
vpStatisticalTestAbstract::detectUpwardMeanDrift
virtual vpMeanDriftType detectUpwardMeanDrift()=0
Detects if a upward mean drift occurred.
vpStatisticalTestAbstract::testUpwardMeanDrift
vpMeanDriftType testUpwardMeanDrift(const float &signal)
Test if an upward mean drift occurred according to the new value of the signal.
Definition
vpStatisticalTestAbstract.cpp:239
vpStatisticalTestAbstract::m_s
float * m_s
Definition
vpStatisticalTestAbstract.h:120
vpStatisticalTestAbstract::testDownwardMeanDrift
vpMeanDriftType testDownwardMeanDrift(const float &signal)
Test if a downward mean drift occurred according to the new value of the signal.
Definition
vpStatisticalTestAbstract.cpp:227
vpStatisticalTestAbstract::init
void init()
(Re)Initialize the algorithm.
Definition
vpStatisticalTestAbstract.cpp:157
vpStatisticalTestAbstract::m_limitDown
float m_limitDown
Definition
vpStatisticalTestAbstract.h:116
vpStatisticalTestAbstract::vpStatisticalTestAbstract
vpStatisticalTestAbstract()
Construct a new vpStatisticalTestAbstract object.
Definition
vpStatisticalTestAbstract.cpp:131
vpStatisticalTestAbstract::updateStatistics
virtual bool updateStatistics(const float &signal)
Update m_s and if enough values are available, compute the mean, the standard deviation and the limit...
Definition
vpStatisticalTestAbstract.cpp:103
vpStatisticalTestAbstract::m_mean
float m_mean
Definition
vpStatisticalTestAbstract.h:118
vpStatisticalTestAbstract::updateTestSignals
virtual void updateTestSignals(const float &signal)=0
Update the test signals.
vpStatisticalTestAbstract::setNbSamplesForStat
void setNbSamplesForStat(const unsigned int &nbSamples)
Set the number of samples required to compute the mean and standard deviation of the signal and alloc...
Definition
vpStatisticalTestAbstract.cpp:193
vpStatisticalTestAbstract::m_stdev
float m_stdev
Definition
vpStatisticalTestAbstract.h:121
vpStatisticalTestAbstract::m_areStatisticsComputed
bool m_areStatisticsComputed
Definition
vpStatisticalTestAbstract.h:114
vpStatisticalTestAbstract::m_count
float m_count
Definition
vpStatisticalTestAbstract.h:115
vpStatisticalTestAbstract::testDownUpwardMeanDrift
vpMeanDriftType testDownUpwardMeanDrift(const float &signal)
Test if a downward or an upward mean drift occurred according to the new value of the signal.
Definition
vpStatisticalTestAbstract.cpp:202
vpStatisticalTestAbstract::print
static void print(const vpMeanDriftType &type)
Print the message corresponding to the type of mean drift.
Definition
vpStatisticalTestAbstract.cpp:98
vpStatisticalTestAbstract::m_nbSamplesForStatistics
unsigned int m_nbSamplesForStatistics
Definition
vpStatisticalTestAbstract.h:119
vpStatisticalTestAbstract::detectDownwardMeanDrift
virtual vpMeanDriftType detectDownwardMeanDrift()=0
Detects if a downward mean drift occurred.
vpStatisticalTestAbstract::operator=
vpStatisticalTestAbstract & operator=(const vpStatisticalTestAbstract &other)
Copy operator of a vpStatisticalTestAbstract.
Definition
vpStatisticalTestAbstract.cpp:173
vpStatisticalTestAbstract::getAvailableMeanDriftType
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.
Definition
vpStatisticalTestAbstract.cpp:85
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
core
src
math
misc
vpStatisticalTestAbstract.cpp
Generated by
1.17.0