overte/libraries/shared/src/StdDev.cpp

59 lines
No EOL
1.2 KiB
C++

//
// StdDev.cpp
// hifi
//
// Created by Philip Rosedale on 3/12/13.
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#include <limits>
#include <cmath>
#include "StdDev.h"
const int MAX_STDEV_SAMPLES = 1000;
StDev::StDev() {
data = new float[MAX_STDEV_SAMPLES];
sampleCount = 0;
}
void StDev::reset() {
sampleCount = 0;
}
void StDev::addValue(float v) {
data[sampleCount++] = v;
if (sampleCount == MAX_STDEV_SAMPLES) sampleCount = 0;
}
float StDev::getAverage() {
float average = 0;
for (int i = 0; i < sampleCount; i++) {
average += data[i];
}
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() {
float average = getAverage();
float stdev = 0;
for (int i = 0; i < sampleCount; i++) {
stdev += powf(data[i] - average, 2);
}
if (sampleCount > 1)
return sqrt(stdev/(float)(sampleCount - 1.0));
else
return 0;
}