24std::vector<double>
moving_average_filter(
const std::vector<double> input,
int window_size,
bool ignore_first_point)
26 if (window_size % 2 == 0) {
27 throw std::invalid_argument(
"moving_average_filter window size must be odd");
30 std::vector<double> output;
31 output.reserve(input.size());
33 if (input.size() == 0) {
38 if (ignore_first_point) {
40 output.push_back(input[0]);
43 for (
int i =
start_index; i < static_cast<int>(input.size());
i++) {
47 int sample_min = std::max(0,
i - window_size / 2);
48 int sample_max = std::min((
int) input.size() - 1 ,
i + window_size / 2);
50 int count = sample_max - sample_min + 1;
51 std::vector<double> sample;
52 sample.reserve(count);
53 for (
int j = sample_min; j <= sample_max; j++) {
56 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.