Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpStatisticalTestMeanAdjustedCUSUM.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 
39 #include <visp3/core/vpStatisticalTestMeanAdjustedCUSUM.h>
40 
41 BEGIN_VISP_NAMESPACE
43 {
44  setDelta(m_k * m_stdev);
45  float limitDown = m_h * m_stdev;
46  float limitUp = limitDown;
47  setLimits(limitDown, limitUp);
48 }
49 
51 {
52  if (m_sminus >= m_limitDown) {
53  return MEAN_DRIFT_DOWNWARD;
54  }
55  else {
56  return MEAN_DRIFT_NONE;
57  }
58 }
59 
61 {
62  if (m_splus >= m_limitUp) {
63  return MEAN_DRIFT_UPWARD;
64  }
65  else {
66  return MEAN_DRIFT_NONE;
67  }
68 }
69 
71 {
72  bool areStatsAvailable = vpStatisticalTestAbstract::updateStatistics(signal);
73  if (areStatsAvailable) {
74  // Computation of the limits
75  if ((m_limitDown < 0.f) && (m_limitUp < 0.f)) {
77  }
78 
79  // Initialize first values
80  m_sminus = 0.f;
81  m_splus = 0.f;
82  }
83  return areStatsAvailable;
84 }
85 
87 {
88  m_sminus = std::max(0.f, m_sminus - (signal - m_mean) - m_half_delta);
89  m_splus = std::max(0.f, m_splus + (signal - m_mean) - m_half_delta);
90 }
91 
92 vpStatisticalTestMeanAdjustedCUSUM::vpStatisticalTestMeanAdjustedCUSUM(const float &h, const float &k, const unsigned int &nbPtsForStats)
94  , m_delta(-1.f)
95  , m_h(h)
96  , m_half_delta(-1.f)
97  , m_k(k)
98  , m_sminus(0.f)
99  , m_splus(0.f)
100 {
101  init(h, k, nbPtsForStats);
102 }
103 
104 void vpStatisticalTestMeanAdjustedCUSUM::init(const float &h, const float &k, const unsigned int &nbPtsForStats)
105 {
107  setNbSamplesForStat(nbPtsForStats);
108  m_delta = -1.f;
109  m_half_delta = -1.f;
110  setLimits(-1.f, -1.f); // To compute automatically the limits once the signal statistics are available.
111  m_h = h;
112  m_k = k;
113  m_mean = 0.f;
114  m_sminus = 0.f;
115  m_splus = 0.f;
116  m_stdev = 0.f;
117 }
118 
119 void vpStatisticalTestMeanAdjustedCUSUM::init(const float &delta, const float &limitDown, const float &limitUp, const unsigned int &nbPtsForStats)
120 {
122  setDelta(delta);
123  setLimits(limitDown, limitUp);
124  setNbSamplesForStat(nbPtsForStats);
125  m_sminus = 0.f;
126  m_splus = 0.f;
127 }
128 
129 void vpStatisticalTestMeanAdjustedCUSUM::init(const float &h, const float &k, const float &mean, const float &stdev)
130 {
132  setLimits(-1.f, -1.f); // To compute automatically the limits once the signal statistics are available.
133  m_h = h;
134  m_k = k;
135  m_mean = mean;
136  m_sminus = 0.f;
137  m_splus = 0.f;
138  m_stdev = stdev;
139  // Compute delta and limits from m_h, m_k and m_stdev
142 }
143 
144 void vpStatisticalTestMeanAdjustedCUSUM::init(const float &delta, const float &limitDown, const float &limitUp, const float &mean, const float &stdev)
145 {
147  setDelta(delta);
148  setLimits(limitDown, limitUp);
149  m_mean = mean;
150  m_sminus = 0.f;
151  m_splus = 0.f;
152  m_stdev = stdev;
153  m_sumForMean = 0.f;
155 }
156 END_VISP_NAMESPACE
Base class for methods detecting the drift of the mean of a process.
vpMeanDriftType
Enum that indicates if a drift of the mean occurred.
void init()
(Re)Initialize the algorithm.
virtual bool updateStatistics(const float &signal)
Update m_s and if enough values are available, compute the mean, the standard deviation and the limit...
void setNbSamplesForStat(const unsigned int &nbSamples)
Set the number of samples required to compute the mean and standard deviation of the signal and alloc...
vpStatisticalTestMeanAdjustedCUSUM(const float &h=4.f, const float &k=1.f, const unsigned int &nbPtsForStats=30)
Construct a new vpStatisticalTestMeanAdjustedCUSUM object.
virtual bool updateStatistics(const float &signal) override
Update m_s and if enough values are available, compute the mean, the standard deviation and the limit...
virtual vpMeanDriftType detectDownwardMeanDrift() override
Detects if a downward mean drift occurred.
virtual void computeDeltaAndLimits()
Compute the upper and lower limits of the test signal.
void setDelta(const float &delta)
Set the slack of the CUSUM test, i.e. the minimum value of the jumps we want to detect.
void setLimits(const float &limitDown, const float &limitUp)
Set the upward and downward jump limits.
virtual void updateTestSignals(const float &signal) override
Update the test signals.
virtual vpMeanDriftType detectUpwardMeanDrift() override
Detects if a upward jump occurred on the mean.