Apollo 10.0
自动驾驶开放平台
digital_filter_coefficients.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <cmath>
20#include <vector>
21
22#include "cyber/common/log.h"
23
24namespace apollo {
25namespace common {
26
27void LpfCoefficients(const double ts, const double cutoff_freq,
28 std::vector<double> *denominators,
29 std::vector<double> *numerators) {
30 denominators->clear();
31 numerators->clear();
32 denominators->reserve(3);
33 numerators->reserve(3);
34
35 double wa = 2.0 * M_PI * cutoff_freq; // Analog frequency in rad/s
36 double alpha = wa * ts / 2.0; // tan(Wd/2), Wd is discrete frequency
37 double alpha_sqr = alpha * alpha;
38 double tmp_term = std::sqrt(2.0) * alpha + alpha_sqr;
39 double gain = alpha_sqr / (1.0 + tmp_term);
40
41 denominators->push_back(1.0);
42 denominators->push_back(2.0 * (alpha_sqr - 1.0) / (1.0 + tmp_term));
43 denominators->push_back((1.0 - std::sqrt(2.0) * alpha + alpha_sqr) /
44 (1.0 + tmp_term));
45
46 numerators->push_back(gain);
47 numerators->push_back(2.0 * gain);
48 numerators->push_back(gain);
49}
50
51void LpFirstOrderCoefficients(const double ts, const double settling_time,
52 const double dead_time,
53 std::vector<double> *denominators,
54 std::vector<double> *numerators) {
55 // sanity check
56 if (ts <= 0.0 || settling_time < 0.0 || dead_time < 0.0) {
57 AERROR << "time cannot be negative";
58 return;
59 }
60
61 const size_t k_d = static_cast<size_t>(dead_time / ts);
62 double a_term;
63
64 denominators->clear();
65 numerators->clear();
66 denominators->reserve(2);
67 numerators->reserve(k_d + 1); // size depends on dead-time
68
69 if (settling_time == 0.0) {
70 a_term = 0.0;
71 } else {
72 a_term = exp(-1 * ts / settling_time);
73 }
74
75 denominators->push_back(1.0);
76 denominators->push_back(-a_term);
77 numerators->insert(numerators->end(), k_d, 0.0);
78 numerators->push_back(1 - a_term);
79}
80
81} // namespace common
82} // namespace apollo
Functions to generate coefficients for digital filter.
#define AERROR
Definition log.h:44
void LpFirstOrderCoefficients(const double ts, const double settling_time, const double dead_time, std::vector< double > *denominators, std::vector< double > *numerators)
Get first order low-pass coefficients for ZOH digital filter.
void LpfCoefficients(const double ts, const double cutoff_freq, std::vector< double > *denominators, std::vector< double > *numerators)
Get low-pass coefficients for digital filter.
class register implement
Definition arena_queue.h:37