nidas v1.2.3
RunningAverage.h
Go to the documentation of this file.
1// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2// vim: set shiftwidth=4 softtabstop=4 expandtab:
3/*
4 ********************************************************************
5 ** NIDAS: NCAR In-situ Data Acquistion Software
6 **
7 ** 2007, Copyright University Corporation for Atmospheric Research
8 **
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** The LICENSE.txt file accompanying this software contains
20 ** a copy of the GNU General Public License. If it is not found,
21 ** write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 **
24 ********************************************************************
25 */
26
27#ifndef _nidis_util_RunningAverage_h_
28#define _nidis_util_RunningAverage_h_
29
30#include <cstring>
31
32namespace nidas { namespace util {
33
38template<class T, int i> class RunningAverage
39{
40public:
42 { memset((char *)_values, 0, sizeof(_values)); }
43
44 T average(T newValue)
45 {
47 _values[_index++] = newValue;
48 _sum += newValue;
49
50 if (_nValues < _size)
51 ++_nValues;
52
53 if (_index >= _size)
54 _index = 0;
55
56 return (T)(_sum / _nValues);
57 }
58
59private:
60 T _values[i];
61 unsigned int _size;
62
63 double _sum;
64
65 unsigned int _nValues;
66 unsigned int _index;
67};
68
69} }
70
71#endif
Basic running average template class.
Definition RunningAverage.h:39
RunningAverage()
Definition RunningAverage.h:41
unsigned int _size
Definition RunningAverage.h:61
unsigned int _index
Definition RunningAverage.h:66
double _sum
Definition RunningAverage.h:63
T average(T newValue)
Definition RunningAverage.h:44
unsigned int _nValues
Definition RunningAverage.h:65
T _values[i]
Definition RunningAverage.h:60
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31