mirror of
https://github.com/lubosz/overte.git
synced 2025-04-23 12:14:00 +02:00
update stDev (standard deviation) class to coding standard
This commit is contained in:
parent
597871442e
commit
f66f565f53
2 changed files with 24 additions and 34 deletions
|
@ -13,50 +13,40 @@
|
|||
#include <cmath>
|
||||
#include "StdDev.h"
|
||||
|
||||
const int MAX_STDEV_SAMPLES = 1000;
|
||||
const int NUM_SAMPLES = 1000;
|
||||
|
||||
StDev::StDev() {
|
||||
data = new float[MAX_STDEV_SAMPLES];
|
||||
sampleCount = 0;
|
||||
_data = new float[NUM_SAMPLES];
|
||||
_sampleCount = 0;
|
||||
}
|
||||
|
||||
void StDev::reset() {
|
||||
sampleCount = 0;
|
||||
_sampleCount = 0;
|
||||
}
|
||||
|
||||
void StDev::addValue(float v) {
|
||||
data[sampleCount++] = v;
|
||||
if (sampleCount == MAX_STDEV_SAMPLES) sampleCount = 0;
|
||||
_data[_sampleCount++] = v;
|
||||
if (_sampleCount == NUM_SAMPLES) _sampleCount = 0;
|
||||
}
|
||||
|
||||
float StDev::getAverage() const {
|
||||
float average = 0;
|
||||
for (int i = 0; i < sampleCount; i++) {
|
||||
average += data[i];
|
||||
for (int i = 0; i < _sampleCount; i++) {
|
||||
average += _data[i];
|
||||
}
|
||||
if (sampleCount > 0)
|
||||
return average/(float)sampleCount;
|
||||
if (_sampleCount > 0)
|
||||
return average / (float)_sampleCount;
|
||||
else return 0;
|
||||
}
|
||||
/*
|
||||
float StDev::getMax() {
|
||||
float average = -FLT_MAX;
|
||||
for (int i = 0; i < sampleCount; i++) {
|
||||
average += data[i];
|
||||
}
|
||||
if (sampleCount > 0)
|
||||
return average/(float)sampleCount;
|
||||
else return 0;
|
||||
}*/
|
||||
|
||||
float StDev::getStDev() const {
|
||||
float average = getAverage();
|
||||
float stdev = 0;
|
||||
for (int i = 0; i < sampleCount; i++) {
|
||||
stdev += powf(data[i] - average, 2);
|
||||
for (int i = 0; i < _sampleCount; i++) {
|
||||
stdev += powf(_data[i] - average, 2);
|
||||
}
|
||||
if (sampleCount > 1)
|
||||
return sqrt(stdev/(float)(sampleCount - 1.0));
|
||||
if (_sampleCount > 1)
|
||||
return sqrt(stdev / (float)(_sampleCount - 1.0f));
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -13,16 +13,16 @@
|
|||
#define hifi_StdDev_h
|
||||
|
||||
class StDev {
|
||||
public:
|
||||
StDev();
|
||||
void reset();
|
||||
void addValue(float v);
|
||||
float getAverage() const;
|
||||
float getStDev() const;
|
||||
int getSamples() const { return sampleCount; }
|
||||
private:
|
||||
float * data;
|
||||
int sampleCount;
|
||||
public:
|
||||
StDev();
|
||||
void reset();
|
||||
void addValue(float v);
|
||||
float getAverage() const;
|
||||
float getStDev() const;
|
||||
int getSamples() const { return _sampleCount; }
|
||||
private:
|
||||
float* _data;
|
||||
int _sampleCount;
|
||||
};
|
||||
|
||||
#endif // hifi_StdDev_h
|
||||
|
|
Loading…
Reference in a new issue