APM:Libraries
NotchFilter.cpp
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 #include "NotchFilter.h"
17 
18 /*
19  initialise filter
20  */
21 template <class T>
22 void NotchFilter<T>::init(float sample_freq_hz, float center_freq_hz, float bandwidth_hz, float attenuation_dB)
23 {
24  float omega = 2.0 * M_PI * center_freq_hz / sample_freq_hz;
25  float octaves = log2f(center_freq_hz / (center_freq_hz - bandwidth_hz/2)) * 2;
26  float A = powf(10, -attenuation_dB/40);
27  float Q = sqrtf(powf(2, octaves)) / (powf(2,octaves) - 1);
28  float alpha = sinf(omega) / (2 * Q/A);
29  b0 = 1.0 + alpha*A;
30  b1 = -2.0 * cosf(omega);
31  b2 = 1.0 - alpha*A;
32  a0_inv = 1.0/(1.0 + alpha/A);
33  a1 = -2.0 * cosf(omega);
34  a2 = 1.0 - alpha/A;
35  initialised = true;
36 }
37 
38 /*
39  apply a new input sample, returning new output
40  */
41 template <class T>
42 T NotchFilter<T>::apply(const T &sample)
43 {
44  if (!initialised) {
45  // if we have not been initialised when return the input
46  // sample as output
47  return sample;
48  }
49  ntchsig2 = ntchsig1;
50  ntchsig1 = ntchsig;
51  ntchsig = sample;
52  T output = (ntchsig*b0 + ntchsig1*b1 + ntchsig2*b2 - signal1*a1 - signal2*a2) * a0_inv;
53  signal2 = signal1;
54  signal1 = output;
55  return output;
56 }
57 
58 // table of user settable parameters
60 
61  // @Param: ENABLE
62  // @DisplayName: Enable
63  // @Description: Enable notch filter
64  // @Values: 0:Disabled,1:Enabled
65  // @User: Advanced
67 
68  // @Param: FREQ
69  // @DisplayName: Frequency
70  // @Description: Notch center frequency in Hz
71  // @Range: 10 200
72  // @Units: Hz
73  // @User: Advanced
74  AP_GROUPINFO("FREQ", 2, NotchFilterVector3fParam, center_freq_hz, 80),
75 
76  // @Param: BW
77  // @DisplayName: Bandwidth
78  // @Description: Notch bandwidth in Hz
79  // @Range: 5 50
80  // @Units: Hz
81  // @User: Advanced
82  AP_GROUPINFO("BW", 3, NotchFilterVector3fParam, bandwidth_hz, 20),
83 
84  // @Param: ATT
85  // @DisplayName: Attenuation
86  // @Description: Notch attenuation in dB
87  // @Range: 5 30
88  // @Units: dB
89  // @User: Advanced
90  AP_GROUPINFO("ATT", 4, NotchFilterVector3fParam, attenuation_dB, 15),
91 
93 };
94 
95 /*
96  a notch filter with enable and filter parameters - constructor
97  */
99 {
101 }
102 
103 /*
104  initialise filter
105  */
106 void NotchFilterVector3fParam::init(float _sample_freq_hz)
107 {
108  filter.init(_sample_freq_hz, center_freq_hz, bandwidth_hz, attenuation_dB);
109 
110  sample_freq_hz = _sample_freq_hz;
114 }
115 
116 /*
117  apply a filter sample
118  */
120 {
121  if (!enable) {
122  // when not enabled it is a simple pass-through
123  return sample;
124  }
125 
126  // check for changed parameters
130  if (!is_zero(sample_freq_hz)) {
132  }
133  }
134 
135  return filter.apply(sample);
136 }
137 
138 /*
139  instantiate template classes
140  */
141 template class NotchFilter<float>;
142 template class NotchFilter<Vector3f>;
#define AP_PARAM_FLAG_ENABLE
Definition: AP_Param.h:56
void init(float sample_freq_hz, float center_freq_hz, float bandwidth_hz, float attenuation_dB)
Definition: NotchFilter.cpp:22
void init(float sample_freq_hz)
T apply(const T &sample)
Definition: NotchFilter.cpp:42
#define AP_GROUPINFO(name, idx, clazz, element, def)
Definition: AP_Param.h:102
Vector3f apply(const Vector3f &sample)
#define AP_GROUPINFO_FLAGS(name, idx, clazz, element, def, flags)
Definition: AP_Param.h:93
bool is_zero(const T fVal1)
Definition: AP_Math.h:40
static const struct AP_Param::GroupInfo var_info[]
Definition: NotchFilter.h:51
std::enable_if< std::is_integral< typename std::common_type< Arithmetic1, Arithmetic2 >::type >::value,bool >::type is_equal(const Arithmetic1 v_1, const Arithmetic2 v_2)
Definition: AP_Math.cpp:12
#define M_PI
Definition: definitions.h:10
NotchFilter< Vector3f > filter
Definition: NotchFilter.h:65
#define AP_GROUPEND
Definition: AP_Param.h:121
static void setup_object_defaults(const void *object_pointer, const struct GroupInfo *group_info)
Definition: AP_Param.cpp:1214