37std::vector<double>
moving_average_filter(
const std::vector<double> input,
int window_size,
bool ignore_first_point=
true)
39 if (window_size % 2 == 0) {
40 throw std::invalid_argument(
"moving_average_filter window size must be odd");
43 std::vector<double> output;
44 output.reserve(input.size());
46 if (input.size() == 0) {
51 if (ignore_first_point) {
53 output.push_back(input[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);
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++) {
69 output.push_back(total / (
double) count);
std::vector< double > moving_average_filter(const std::vector< double > input, int window_size, bool ignore_first_point=true)
Extremely simplie moving average filter.