spike detection
statistics.cpp@0:75ea240e0059, 2016-09-28 (annotated)
- Committer:
- otis22894
- Date:
- Wed Sep 28 00:12:53 2016 +0000
- Revision:
- 0:75ea240e0059
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
otis22894 | 0:75ea240e0059 | 1 | #include "statistics.h" |
otis22894 | 0:75ea240e0059 | 2 | |
otis22894 | 0:75ea240e0059 | 3 | RunningStatistics :: RunningStatistics(int numSamples) { |
otis22894 | 0:75ea240e0059 | 4 | sum = 0; |
otis22894 | 0:75ea240e0059 | 5 | sumSquares = 0; |
otis22894 | 0:75ea240e0059 | 6 | average = 0; |
otis22894 | 0:75ea240e0059 | 7 | count = 0; |
otis22894 | 0:75ea240e0059 | 8 | samples = new double [numSamples]; |
otis22894 | 0:75ea240e0059 | 9 | currentNdx = 0; |
otis22894 | 0:75ea240e0059 | 10 | sampleSize = numSamples; |
otis22894 | 0:75ea240e0059 | 11 | } |
otis22894 | 0:75ea240e0059 | 12 | |
otis22894 | 0:75ea240e0059 | 13 | |
otis22894 | 0:75ea240e0059 | 14 | double RunningStatistics :: addSample(double newSample) { |
otis22894 | 0:75ea240e0059 | 15 | double temp; |
otis22894 | 0:75ea240e0059 | 16 | |
otis22894 | 0:75ea240e0059 | 17 | if (count == sampleSize) { |
otis22894 | 0:75ea240e0059 | 18 | temp = samples[(currentNdx) % sampleSize]; |
otis22894 | 0:75ea240e0059 | 19 | sum -= temp; |
otis22894 | 0:75ea240e0059 | 20 | sumSquares -= (temp * temp); |
otis22894 | 0:75ea240e0059 | 21 | } |
otis22894 | 0:75ea240e0059 | 22 | sum += newSample; |
otis22894 | 0:75ea240e0059 | 23 | sumSquares += (newSample * newSample); |
otis22894 | 0:75ea240e0059 | 24 | samples[currentNdx % sampleSize] = newSample; |
otis22894 | 0:75ea240e0059 | 25 | |
otis22894 | 0:75ea240e0059 | 26 | |
otis22894 | 0:75ea240e0059 | 27 | if (count < sampleSize) { |
otis22894 | 0:75ea240e0059 | 28 | ++count; |
otis22894 | 0:75ea240e0059 | 29 | } |
otis22894 | 0:75ea240e0059 | 30 | average = (sum / count); |
otis22894 | 0:75ea240e0059 | 31 | |
otis22894 | 0:75ea240e0059 | 32 | ++currentNdx; |
otis22894 | 0:75ea240e0059 | 33 | if (currentNdx >= sampleSize) { |
otis22894 | 0:75ea240e0059 | 34 | currentNdx = currentNdx % sampleSize; |
otis22894 | 0:75ea240e0059 | 35 | } |
otis22894 | 0:75ea240e0059 | 36 | return temp; |
otis22894 | 0:75ea240e0059 | 37 | } |
otis22894 | 0:75ea240e0059 | 38 | |
otis22894 | 0:75ea240e0059 | 39 | double RunningStatistics :: getAverage() { |
otis22894 | 0:75ea240e0059 | 40 | return average; |
otis22894 | 0:75ea240e0059 | 41 | } |
otis22894 | 0:75ea240e0059 | 42 | |
otis22894 | 0:75ea240e0059 | 43 | double RunningStatistics :: getStandardDeviation() { |
otis22894 | 0:75ea240e0059 | 44 | return sqrt((sumSquares - (count * average * average)) / (count - 1)); |
otis22894 | 0:75ea240e0059 | 45 | } |
otis22894 | 0:75ea240e0059 | 46 | |
otis22894 | 0:75ea240e0059 | 47 | int RunningStatistics :: getCount() { |
otis22894 | 0:75ea240e0059 | 48 | return count; |
otis22894 | 0:75ea240e0059 | 49 | } |
otis22894 | 0:75ea240e0059 | 50 | |
otis22894 | 0:75ea240e0059 | 51 | double RunningStatistics :: getSum() { |
otis22894 | 0:75ea240e0059 | 52 | return sum; |
otis22894 | 0:75ea240e0059 | 53 | } |
otis22894 | 0:75ea240e0059 | 54 | |
otis22894 | 0:75ea240e0059 | 55 | void RunningStatistics :: reset() { |
otis22894 | 0:75ea240e0059 | 56 | delete [] samples; |
otis22894 | 0:75ea240e0059 | 57 | sum = 0; |
otis22894 | 0:75ea240e0059 | 58 | sumSquares = 0; |
otis22894 | 0:75ea240e0059 | 59 | average = 0; |
otis22894 | 0:75ea240e0059 | 60 | count = 0; |
otis22894 | 0:75ea240e0059 | 61 | samples = new double[sampleSize]; |
otis22894 | 0:75ea240e0059 | 62 | currentNdx = 0; |
otis22894 | 0:75ea240e0059 | 63 | } |