Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
filters.hpp
Go to the documentation of this file.
1#pragma once
2
3/*
4 * Copyright (C) 2022 LEIDOS.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7 * use this file except in compliance with the License. You may obtain a copy of
8 * the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 * License for the specific language governing permissions and limitations under
16 * the License.
17 */
18
19#include <vector>
20#include <deque>
21#include <algorithm>
22#include <stdexcept>
24{
25namespace smoothing
26{
27
28// NEW
37std::vector<double> moving_average_filter(const std::vector<double> input, int window_size, bool ignore_first_point=true)
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
50 int start_index = 0;
51 if (ignore_first_point) {
52 start_index = 1;
53 output.push_back(input[0]);
54 }
55
56 for (int i = start_index; i<input.size(); i++) {
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}
75
76}; // namespace smoothing
77}; // namespace inlanecruising_plugin
std::vector< double > moving_average_filter(const std::vector< double > input, int window_size, bool ignore_first_point=true)
Extremely simplie moving average filter.
Definition: filters.hpp:37