Extremely simplie moving average filter.
38{
39 if (window_size % 2 == 0) {
40 throw std::invalid_argument("moving_average_filter window size must be odd");
41 }
42
43 std::vector<double> output;
44 output.reserve(input.size());
45
46 if (input.size() == 0) {
47 return output;
48 }
49
51 if (ignore_first_point) {
53 output.push_back(input[0]);
54 }
55
57
58
59 double total = 0;
60 int sample_min = std::max(0,
i - window_size / 2);
61 int sample_max = std::min((
int) input.size() - 1 ,
i + window_size / 2);
62
63 int count = sample_max - sample_min + 1;
64 std::vector<double> sample;
65 sample.reserve(count);
66 for (int j = sample_min; j <= sample_max; j++) {
67 total += input[j];
68 }
69 output.push_back(total / (double) count);
70
71 }
72
73 return output;
74}