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 <cmath>
34 
35 namespace mcl_3dl
36 {
37 class Filter
38 {
39 public:
40  enum type_t
41  {
44  };
45 
46 protected:
48  float time_const_;
49  float x_;
50  float out_;
51  float k_[4];
52  bool angle_;
53 
54 public:
55  Filter(const enum type_t type, const float tc, const float out0, const bool angle = false)
56  {
57  angle_ = angle;
58  time_const_ = tc;
59  type_ = type;
60  switch (type_)
61  {
62  case FILTER_LPF:
63  k_[3] = -1 / (1.0 + 2 * time_const_);
64  k_[2] = -k_[3];
65  k_[1] = (1.0 - 2 * time_const_) * k_[3];
66  k_[0] = -k_[1] - 1.0;
67  x_ = (1 - k_[2]) * out0 / k_[3];
68  break;
69  case FILTER_HPF:
70  k_[3] = -1 / (1.0 + 2 * time_const_);
71  k_[2] = -k_[3] * 2 * time_const_;
72  k_[1] = (1.0 - 2 * time_const_) * k_[3];
73  k_[0] = 2 * time_const_ * (-k_[1] + 1.0);
74  x_ = (1 - k_[2]) * out0 / k_[3];
75  break;
76  }
77  out_ = out0;
78  }
79  void set(const float& out0)
80  {
81  x_ = (1 - k_[2]) * out0 / k_[3];
82  out_ = out0;
83  }
84  float in(const float& i)
85  {
86  float in = i;
87  assert(std::isfinite(in));
88 
89  if (angle_)
90  {
91  in = out_ + remainder(in - out_, M_PI * 2.0);
92  }
93  x_ = k_[0] * in + k_[1] * x_;
94  out_ = k_[2] * in + k_[3] * x_;
95 
96  assert(std::isfinite(out_));
97  return out_;
98  }
99  float get()
100  {
101  return out_;
102  }
103 };
104 } // namespace mcl_3dl
105 
106 #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
type_t type_
Definition: filter.h:47
float time_const_
Definition: filter.h:48
Filter(const enum type_t type, const float tc, const float out0, const bool angle=false)
Definition: filter.h:55
float in(const float &i)
Definition: filter.h:84
float out_
Definition: filter.h:50
bool angle_
Definition: filter.h:52


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Mon Jul 8 2019 03:32:36