Mercurial > hg > beaglert
changeset 137:aac018615930 ClockSync
MovingAverage now returns a meaningful value also when the buffer is not full
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sun, 13 Sep 2015 21:31:37 +0100 |
parents | 772dbb57442b |
children | 53e3c0a3295d |
files | include/stats.hpp |
diffstat | 1 files changed, 13 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/include/stats.hpp Sun Sep 13 21:26:38 2015 +0100 +++ b/include/stats.hpp Sun Sep 13 21:31:37 2015 +0100 @@ -9,12 +9,13 @@ private: TYPE* array; int length; + bool bufferFull; int pointer; TYPE sum; double scale; double average; void dealloc(){ - free(array); + free(array); // delete array; } @@ -28,6 +29,7 @@ printf("Error while allocating array\n"); memset(array, 0, sizeof(TYPE)*length); pointer=0; + bufferFull=false; } public: MovingAverage(){ @@ -50,10 +52,17 @@ sum-=array[pointer]; array[pointer]=newElement; sum+=newElement; - average=sum*scale; + if(bufferFull==true){ + average=sum*scale; + } + else{ + average=sum/(double)(1+pointer); + } pointer++; - if(pointer==length) + if(pointer==length){ pointer=0; + bufferFull=true; + } return average; } double getAverage(){ @@ -61,4 +70,4 @@ } }; -#endif /* STATS_HPP_INCLUDED */ \ No newline at end of file +#endif /* STATS_HPP_INCLUDED */