趣味で作ってるロボット用ソフトウェア
 All Classes Files Functions Enumerations Enumerator Friends Pages
Moving_average.hpp
Go to the documentation of this file.
1 #ifndef HRK_MOVING_AVERAGE_H
2 #define HRK_MOVING_AVERAGE_H
3 
9 #include <deque>
10 #include <cstddef>
11 
12 
13 namespace hrk
14 {
20  template <class T>
22  {
23  public:
29  Moving_average(size_t size, T initial_value = 0)
30  : buffer_size_(size), total_(0)
31  {
32  if (buffer_size_ == 0) {
33  buffer_size_ = 1;
34  }
35  buffer_set(buffer_size_, initial_value);
36  }
37 
43  void set_average_value(T value)
44  {
45  buffer_set(buffer_size_, value);
46  }
47 
53  T average(void)
54  {
55  return average_;
56  }
57 
65  T push_back(T value)
66  {
67  total_ -= buffer_.front();
68  buffer_.pop_front();
69  total_ += value;
70  buffer_.push_back(value);
71 
72  average_ = total_ / buffer_size_;
73 
74  return average_;
75  }
76 
77  private:
78  Moving_average(const Moving_average& rhs);
79  Moving_average& operator = (const Moving_average& rhs);
80 
81  std::deque<T> buffer_;
82  long buffer_size_;
83  T average_;
84  T total_;
85 
86  void buffer_set(size_t size, T value)
87  {
88  buffer_.assign(size, value);
89  average_ = value;
90  total_ = average_ * size;
91  }
92  };
93 }
94 
95 #endif
T average(void)
平均値の取得
Definition: Moving_average.hpp:53
T push_back(T value)
値の追加
Definition: Moving_average.hpp:65
void set_average_value(T value)
格納されている平均値の更新
Definition: Moving_average.hpp:43
Moving_average(size_t size, T initial_value=0)
コンストラクタ
Definition: Moving_average.hpp:29
移動平均のテンプレート
Definition: Moving_average.hpp:21