update stDev (standard deviation) class to coding standard

This commit is contained in:
Philip Rosedale 2014-09-25 18:14:02 -07:00
parent 597871442e
commit f66f565f53
2 changed files with 24 additions and 34 deletions

View file

@ -13,50 +13,40 @@
#include <cmath> #include <cmath>
#include "StdDev.h" #include "StdDev.h"
const int MAX_STDEV_SAMPLES = 1000; const int NUM_SAMPLES = 1000;
StDev::StDev() { StDev::StDev() {
data = new float[MAX_STDEV_SAMPLES]; _data = new float[NUM_SAMPLES];
sampleCount = 0; _sampleCount = 0;
} }
void StDev::reset() { void StDev::reset() {
sampleCount = 0; _sampleCount = 0;
} }
void StDev::addValue(float v) { void StDev::addValue(float v) {
data[sampleCount++] = v; _data[_sampleCount++] = v;
if (sampleCount == MAX_STDEV_SAMPLES) sampleCount = 0; if (_sampleCount == NUM_SAMPLES) _sampleCount = 0;
} }
float StDev::getAverage() const { float StDev::getAverage() const {
float average = 0; float average = 0;
for (int i = 0; i < sampleCount; i++) { for (int i = 0; i < _sampleCount; i++) {
average += data[i]; average += _data[i];
} }
if (sampleCount > 0) if (_sampleCount > 0)
return average/(float)sampleCount; return average / (float)_sampleCount;
else return 0; 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 StDev::getStDev() const {
float average = getAverage(); float average = getAverage();
float stdev = 0; float stdev = 0;
for (int i = 0; i < sampleCount; i++) { for (int i = 0; i < _sampleCount; i++) {
stdev += powf(data[i] - average, 2); stdev += powf(_data[i] - average, 2);
} }
if (sampleCount > 1) if (_sampleCount > 1)
return sqrt(stdev/(float)(sampleCount - 1.0)); return sqrt(stdev / (float)(_sampleCount - 1.0f));
else else
return 0; return 0;
} }

View file

@ -13,16 +13,16 @@
#define hifi_StdDev_h #define hifi_StdDev_h
class StDev { class StDev {
public: public:
StDev(); StDev();
void reset(); void reset();
void addValue(float v); void addValue(float v);
float getAverage() const; float getAverage() const;
float getStDev() const; float getStDev() const;
int getSamples() const { return sampleCount; } int getSamples() const { return _sampleCount; }
private: private:
float * data; float* _data;
int sampleCount; int _sampleCount;
}; };
#endif // hifi_StdDev_h #endif // hifi_StdDev_h