Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
notch_filter_float.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Bart Slinger
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #ifndef NOTCH_FILTER_H
28 #define NOTCH_FILTER_H
29 
30 #include "std.h"
31 
33  float Ts;
34  float d2;
35  float costheta;
36  float xn1;
37  float xn2;
38  float yn1;
39  float yn2;
40 };
41 
47 static inline void notch_filter_set_sampling_frequency(struct SecondOrderNotchFilter *filter, uint16_t frequency)
48 {
49  filter->Ts = 1.0 / frequency;
50 }
51 
57 static inline void notch_filter_set_bandwidth(struct SecondOrderNotchFilter *filter, float bandwidth)
58 {
59  float d = expf(-M_PI * bandwidth * filter->Ts);
60  filter->d2 = d * d;
61 }
62 
68 static inline void notch_filter_set_filter_frequency(struct SecondOrderNotchFilter *filter, float frequency)
69 {
70  float theta = 2.0 * M_PI * frequency * filter->Ts;
71  filter->costheta = cosf(theta);
72 }
73 
83 static inline void notch_filter_update(struct SecondOrderNotchFilter *filter, float *input_signal, float *output_signal)
84 {
85  float a = (1 + filter->d2) * 0.5;
86  float b = (1 + filter->d2) * filter->costheta;
87  *output_signal = (b * filter->yn1) - (filter->d2 * filter->yn2) + (a * *input_signal) - (b * filter->xn1) +
88  (a * filter->xn2);
89 
90  /* Update values for next update */
91  filter->xn2 = filter->xn1;
92  filter->xn1 = *input_signal;
93  filter->yn2 = filter->yn1;
94  filter->yn1 = *output_signal;
95 }
96 
104 static inline float notch_filter_get_output(struct SecondOrderNotchFilter *filter)
105 {
106  return filter->yn1;
107 }
108 
109 
110 
121 static inline void notch_filter_init(struct SecondOrderNotchFilter *filter, float cutoff_frequency, float bandwidth,
122  uint16_t sample_frequency)
123 {
124  notch_filter_set_sampling_frequency(filter, sample_frequency);
125  notch_filter_set_filter_frequency(filter, cutoff_frequency);
126  notch_filter_set_bandwidth(filter, bandwidth);
127  filter->xn1 = 0;
128  filter->xn2 = 0;
129  filter->yn1 = 0;
130  filter->yn2 = 0;
131 }
132 
133 #endif
static void notch_filter_update(struct SecondOrderNotchFilter *filter, float *input_signal, float *output_signal)
Notch filter propagate.
static void notch_filter_set_sampling_frequency(struct SecondOrderNotchFilter *filter, uint16_t frequency)
Set sampling frequency of the notch filter.
static void notch_filter_set_filter_frequency(struct SecondOrderNotchFilter *filter, float frequency)
Set notch filter frequency in Hz.
static void notch_filter_set_bandwidth(struct SecondOrderNotchFilter *filter, float bandwidth)
Set bandwidth of the notch filter.
static float notch_filter_get_output(struct SecondOrderNotchFilter *filter)
Get notch filter output.
static void notch_filter_init(struct SecondOrderNotchFilter *filter, float cutoff_frequency, float bandwidth, uint16_t sample_frequency)
Initialize second order notch filter.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
float b
Definition: wedgebug.c:202