hack gt final code

Dependencies:   4DGL-uLCD-SE BMP085 PinDetect SDFileSystem mbed wave_player

Committer:
otis22894
Date:
Wed Sep 28 00:16:16 2016 +0000
Revision:
14:23390a020d1c
Parent:
12:b301cb3cef44
initial commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
otis22894 12:b301cb3cef44 1 #include "statistics.h"
otis22894 12:b301cb3cef44 2
otis22894 12:b301cb3cef44 3 RunningStatistics :: RunningStatistics(int numSamples) {
otis22894 12:b301cb3cef44 4 sum = 0;
otis22894 12:b301cb3cef44 5 sumSquares = 0;
otis22894 12:b301cb3cef44 6 average = 0;
otis22894 12:b301cb3cef44 7 count = 0;
otis22894 12:b301cb3cef44 8 samples = new double [numSamples];
otis22894 12:b301cb3cef44 9 currentNdx = 0;
otis22894 12:b301cb3cef44 10 sampleSize = numSamples;
otis22894 12:b301cb3cef44 11 }
otis22894 12:b301cb3cef44 12
otis22894 12:b301cb3cef44 13
otis22894 12:b301cb3cef44 14 double RunningStatistics :: addSample(double newSample) {
otis22894 12:b301cb3cef44 15 double temp;
otis22894 12:b301cb3cef44 16
otis22894 12:b301cb3cef44 17 if (count == sampleSize) {
otis22894 12:b301cb3cef44 18 temp = samples[(currentNdx) % sampleSize];
otis22894 12:b301cb3cef44 19 sum -= temp;
otis22894 12:b301cb3cef44 20 sumSquares -= (temp * temp);
otis22894 12:b301cb3cef44 21 }
otis22894 12:b301cb3cef44 22 sum += newSample;
otis22894 12:b301cb3cef44 23 sumSquares += (newSample * newSample);
otis22894 12:b301cb3cef44 24 samples[currentNdx % sampleSize] = newSample;
otis22894 12:b301cb3cef44 25
otis22894 12:b301cb3cef44 26
otis22894 12:b301cb3cef44 27 if (count < sampleSize) {
otis22894 12:b301cb3cef44 28 ++count;
otis22894 12:b301cb3cef44 29 }
otis22894 12:b301cb3cef44 30 average = (sum / count);
otis22894 12:b301cb3cef44 31
otis22894 12:b301cb3cef44 32 ++currentNdx;
otis22894 12:b301cb3cef44 33 if (currentNdx >= sampleSize) {
otis22894 12:b301cb3cef44 34 currentNdx = currentNdx % sampleSize;
otis22894 12:b301cb3cef44 35 }
otis22894 12:b301cb3cef44 36 return temp;
otis22894 12:b301cb3cef44 37 }
otis22894 12:b301cb3cef44 38
otis22894 12:b301cb3cef44 39 double RunningStatistics :: getAverage() {
otis22894 12:b301cb3cef44 40 return average;
otis22894 12:b301cb3cef44 41 }
otis22894 12:b301cb3cef44 42
otis22894 12:b301cb3cef44 43 double RunningStatistics :: getStandardDeviation() {
otis22894 12:b301cb3cef44 44 return sqrt((sumSquares - (count * average * average)) / (count - 1));
otis22894 12:b301cb3cef44 45 }
otis22894 12:b301cb3cef44 46
otis22894 12:b301cb3cef44 47 int RunningStatistics :: getCount() {
otis22894 12:b301cb3cef44 48 return count;
otis22894 12:b301cb3cef44 49 }
otis22894 12:b301cb3cef44 50
otis22894 12:b301cb3cef44 51 double RunningStatistics :: getSum() {
otis22894 12:b301cb3cef44 52 return sum;
otis22894 12:b301cb3cef44 53 }
otis22894 12:b301cb3cef44 54
otis22894 12:b301cb3cef44 55 void RunningStatistics :: reset() {
otis22894 12:b301cb3cef44 56 delete [] samples;
otis22894 12:b301cb3cef44 57 sum = 0;
otis22894 12:b301cb3cef44 58 sumSquares = 0;
otis22894 12:b301cb3cef44 59 average = 0;
otis22894 12:b301cb3cef44 60 count = 0;
otis22894 12:b301cb3cef44 61 samples = new double[sampleSize];
otis22894 12:b301cb3cef44 62 currentNdx = 0;
otis22894 12:b301cb3cef44 63 }