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.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-2022 LEIDOS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
18
19namespace basic_autonomy
20{
21namespace smoothing
22{
23
24std::vector<double> moving_average_filter(const std::vector<double> input, int window_size, bool ignore_first_point)
25{
26 if (window_size % 2 == 0) {
27 throw std::invalid_argument("moving_average_filter window size must be odd");
28 }
29
30 std::vector<double> output;
31 output.reserve(input.size());
32
33 if (input.size() == 0) {
34 return output;
35 }
36
37 int start_index = 0;
38 if (ignore_first_point) {
39 start_index = 1;
40 output.push_back(input[0]);
41 }
42
43 for (int i = start_index; i < static_cast<int>(input.size()); i++) {
44
45
46 double total = 0;
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);
49
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++) {
54 total += input[j];
55 }
56 output.push_back(total / (double) count);
57
58 }
59
60 return output;
61}
62
63} // namespace smoothing
64} // namespace basic_autonomy
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.cpp:24