filter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2017, the mcl_3dl authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef MCL_3DL_FILTER_H
31 #define MCL_3DL_FILTER_H
32 
33 #include <cassert>
34 #include <cmath>
35 
36 namespace mcl_3dl
37 {
38 class Filter
39 {
40 public:
41  enum type_t
42  {
45  };
46 
47 protected:
48  bool angle_;
49  float x_;
50  float out_;
51  float k_[4];
52 
53 public:
54  inline Filter(const enum type_t type, const float time_const, const float out0, const bool angle = false)
55  : angle_(angle)
56  , out_(out0)
57  {
58  switch (type)
59  {
60  case FILTER_LPF:
61  k_[3] = -1 / (1.0 + 2 * time_const);
62  k_[2] = -k_[3];
63  k_[1] = (1.0 - 2 * time_const) * k_[3];
64  k_[0] = -k_[1] - 1.0;
65  x_ = (1 - k_[2]) * out0 / k_[3];
66  break;
67  case FILTER_HPF:
68  k_[3] = -1 / (1.0 + 2 * time_const);
69  k_[2] = -k_[3] * 2 * time_const;
70  k_[1] = (1.0 - 2 * time_const) * k_[3];
71  k_[0] = 2 * time_const * (-k_[1] + 1.0);
72  x_ = (1 - k_[2]) * out0 / k_[3];
73  break;
74  }
75  }
76  inline void set(const float out0)
77  {
78  x_ = (1 - k_[2]) * out0 / k_[3];
79  out_ = out0;
80  }
81  inline float in(float in)
82  {
83  assert(std::isfinite(in));
84 
85  if (angle_)
86  {
87  in = out_ + remainder(in - out_, M_PI * 2.0);
88  }
89  x_ = k_[0] * in + k_[1] * x_;
90  out_ = k_[2] * in + k_[3] * x_;
91 
92  assert(std::isfinite(out_));
93  return out_;
94  }
95  inline float get() const
96  {
97  return out_;
98  }
99 };
100 } // namespace mcl_3dl
101 
102 #endif // MCL_3DL_FILTER_H
float x_
Definition: filter.h:49
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
float k_[4]
Definition: filter.h:51
float in(float in)
Definition: filter.h:81
float out_
Definition: filter.h:50
bool angle_
Definition: filter.h:48
Filter(const enum type_t type, const float time_const, const float out0, const bool angle=false)
Definition: filter.h:54


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:16:29