-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMA.hpp
More file actions
67 lines (58 loc) · 1.65 KB
/
SMA.hpp
File metadata and controls
67 lines (58 loc) · 1.65 KB
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
// sma.hpp
#ifndef SMA_HPP
#define SMA_HPP
#include <vector>
#include <cstddef>
#include <stdexcept>
class SimpleMovingAverage {
public:
/// Construct an SMA that averages over the last 'window' samples.
explicit SimpleMovingAverage(std::size_t window)
: buf_(window, 0.0),
window_(window),
count_(0),
idx_(0),
sum_(0.0)
{
if (window_ == 0) {
throw std::invalid_argument("Window size must be > 0");
}
}
/// Feed a new sample, return the updated average.
double update(double sample) {
if (count_ < window_) {
// still filling buffer
sum_ += sample;
buf_[idx_] = sample;
++count_;
} else {
// buffer full: subtract oldest, add newest
sum_ += sample - buf_[idx_];
buf_[idx_] = sample;
}
// advance circular index
idx_ = (idx_ + 1) % window_;
return sum_ / static_cast<double>(count_);
}
double get() {
return count_ != 0 ? sum_ / static_cast<double>(count_) : 0.0;
}
/// Reset the filter (clears history)
void reset() {
std::fill(buf_.begin(), buf_.end(), 0.0);
count_ = 0;
idx_ = 0;
sum_ = 0.0;
}
/// Get current window size
std::size_t window() const { return window_; }
/// Get number of samples seen so far (<= window)
std::size_t count() const { return count_; }
private:
std::vector<double> buf_;
std::size_t window_;
std::size_t count_;
std::size_t idx_;
double sum_;
};
#endif // SMA_HPP