Average.h 4.0 KB

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