APM:Libraries
LowPassFilter.h
Go to the documentation of this file.
1 /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 //
21 
22 /*
23  Note that this filter can be used in 2 ways:
24 
25  1) providing dt on every sample, and calling apply like this:
26 
27  // call once
28  filter.set_cutoff_frequency(frequency_hz);
29 
30  // then on each sample
31  output = filter.apply(sample, dt);
32 
33  2) providing a sample freq and cutoff_freq once at start
34 
35  // call once
36  filter.set_cutoff_frequency(sample_freq, frequency_hz);
37 
38  // then on each sample
39  output = filter.apply(sample);
40 
41  The second approach is more CPU efficient as it doesn't have to
42  recalculate alpha each time, but it assumes that dt is constant
43  */
44 
45 #pragma once
46 
47 #include <AP_Math/AP_Math.h>
48 #include "FilterClass.h"
49 
50 // DigitalLPF implements the filter math
51 template <class T>
52 class DigitalLPF {
53 public:
54  DigitalLPF();
55  // add a new raw value to the filter, retrieve the filtered result
56  T apply(const T &sample, float cutoff_freq, float dt);
57  T apply(const T &sample);
58 
59  void compute_alpha(float sample_freq, float cutoff_freq);
60 
61  // get latest filtered value from filter (equal to the value returned by latest call to apply method)
62  const T &get() const;
63  void reset(T value);
64 
65 private:
67  float alpha = 1.0f;
68 };
69 
70 // LPF base class
71 template <class T>
73 public:
74  LowPassFilter();
75  LowPassFilter(float cutoff_freq);
76  LowPassFilter(float sample_freq, float cutoff_freq);
77 
78  // change parameters
79  void set_cutoff_frequency(float cutoff_freq);
80  void set_cutoff_frequency(float sample_freq, float cutoff_freq);
81 
82  // return the cutoff frequency
83  float get_cutoff_freq(void) const;
84  T apply(T sample, float dt);
85  T apply(T sample);
86  const T &get() const;
87  void reset(T value);
88  void reset(void) { reset(T()); }
89 
90 protected:
91  float _cutoff_freq;
92 
93 private:
95 };
96 
97 // Uncomment this, if you decide to remove the instantiations in the implementation file
98 /*
99 template <class T>
100 LowPassFilter<T>::LowPassFilter() : _cutoff_freq(0.0f) {
101 
102 }
103 // constructor
104 template <class T>
105 LowPassFilter<T>::LowPassFilter(float cutoff_freq) : _cutoff_freq(cutoff_freq) {
106 
107 }
108 */
109 
110 // typedefs for compatibility
LowPassFilter< Vector3f > LowPassFilterVector3f
LowPassFilter< float > LowPassFilterFloat
float _cutoff_freq
Definition: LowPassFilter.h:91
LowPassFilter< int > LowPassFilterInt
T apply(const T &sample, float cutoff_freq, float dt)
void reset(T value)
DigitalLPF< T > _filter
Definition: LowPassFilter.h:94
void compute_alpha(float sample_freq, float cutoff_freq)
LowPassFilter< Vector2f > LowPassFilterVector2f
LowPassFilter< long > LowPassFilterLong
void reset(void)
Definition: LowPassFilter.h:88
float value
A pure virtual interface class.