This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Average.h
131 lines (112 loc) · 3.98 KB
/
Average.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// average.h
//
//
// This module conveniently implemetns running averages
//
// History:
// 09/03/97 JRD Started.
//
////////////////////////////////////////////////////////////////////////////////
#ifndef AVERAGE_H
#define AVERAGE_H
////////////////////////////////////////////////////////////////////////////////
// This is a fully configurable template. This first version must have a
// static width, which is set upon instantiation. This version also relies
// on an initial seed value, which has a FULL weighting of the average width
// and will take a full interval to remove. Obviously, your template type must
// have overloads for addition, subtraction, assignment, casting and division.
////////////////////////////////////////////////////////////////////////////////
// WARNING: a subtle restriction of this class template is that the maximum
// value of ValueType_data * clFixedWidth should NOT overflow.
////////////////////////////////////////////////////////////////////////////////
// This is a basic box filter!
// The filter width is static so there is no dynamic allocation of data
// Let me know if this kills your heap
//
template <class ValueType, long clFixedWidth>
class CRunningAverage
{
public:
//---------------------------------------
// Member Functions
//---------------------------------------
//=======================================
// set average value
void Seed(ValueType vtSeedValue)
{
short i;
m_lSignificantNumber = clFixedWidth;
m_lNextValue = 0;
for (i = 0; i < m_lSignificantNumber;i++) m_avDataList[i] = vtSeedValue;
m_vCurrentTotal = ValueType(vtSeedValue * m_lSignificantNumber);
}
//=======================================
// returns the current running average
ValueType Average()
{
ASSERT(m_lSignificantNumber > 0);
return ValueType(m_vCurrentTotal / m_lSignificantNumber);
}
//=======================================
// Give it a new value
// The oldest value will drop off the list if the "window" is filled.
// Soon it will suport an expanding width.
//
void AddValue(ValueType vtNewValue)
{
m_vCurrentTotal -= m_avDataList[m_lNextValue]; // remove point
m_avDataList[m_lNextValue++] = vtNewValue;
m_vCurrentTotal += vtNewValue;
if (m_lNextValue >= clFixedWidth) m_lNextValue = 0;
}
//---------------------------------------
// Instantiation
//---------------------------------------
//=======================================
// Give it an initial average value
CRunningAverage(ValueType vtSeedValue)
{
short i;
m_lSignificantNumber = clFixedWidth;
m_lNextValue = 0;
for (i = 0; i < m_lSignificantNumber;i++) m_avDataList[i] = vtSeedValue;
m_vCurrentTotal = vtSeedValue * m_lSignificantNumber;
}
//=======================================
// Currently, this just seeds with xero.
// Soon, it will allow an expanding average option:
CRunningAverage()
{
Seed(ValueType(0));
}
//=======================================
~CRunningAverage()
{
m_lSignificantNumber = 0; // catch bugs fast!
}
//---------------------------------------
// Member Variables
//---------------------------------------
ValueType m_avDataList[clFixedWidth];
ValueType m_vCurrentTotal;
long m_lSignificantNumber;
long m_lNextValue;
};
#endif