Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
notch_filter.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;
40 };
41 
46 static inline void notch_filter_set_sampling_frequency(struct SecondOrderNotchFilter *filter, uint16_t frequency)
47 {
48  filter->Ts = 1.0/frequency;
49 }
50 
55 static inline void notch_filter_set_bandwidth(struct SecondOrderNotchFilter *filter, float bandwidth)
56 {
57  float d = expf(-M_PI*bandwidth*filter->Ts);
58  filter->d2 = d*d;
59 }
60 
65 static inline void notch_filter_set_filter_frequency(struct SecondOrderNotchFilter *filter, float frequency)
66 {
67  float theta = 2.0*M_PI*frequency*filter->Ts;
68  filter->costheta = cosf(theta);
69 }
70 
79 static inline void notch_filter_update(struct SecondOrderNotchFilter *filter, int32_t *input_signal, int32_t *output_signal)
80 {
81  float a = (1 + filter->d2) * 0.5;
82  float b = (1 + filter->d2) * filter->costheta;
83  *output_signal = (b * filter->yn1) - (filter->d2 * filter->yn2) + (a * *input_signal) - (b * filter->xn1) + (a * filter->xn2);
84 
85  /* Update values for next update */
86  filter->xn2 = filter->xn1;
87  filter->xn1 = *input_signal;
88  filter->yn2 = filter->yn1;
89  filter->yn1 = *output_signal;
90 }
91 
101 static inline void notch_filter_init(struct SecondOrderNotchFilter *filter, float cutoff_frequency, float bandwidth, uint16_t sample_frequency)
102 {
103  notch_filter_set_sampling_frequency(filter, sample_frequency);
104  notch_filter_set_filter_frequency(filter, cutoff_frequency);
105  notch_filter_set_bandwidth(filter, bandwidth);
106  filter->xn1 = 0;
107  filter->xn2 = 0;
108  filter->yn1 = 0;
109  filter->yn2 = 0;
110 }
111 
112 #endif
unsigned short uint16_t
Definition: types.h:16
static void notch_filter_set_sampling_frequency(struct SecondOrderNotchFilter *filter, uint16_t frequency)
Set sampling frequency of the notch filter.
Definition: notch_filter.h:46
static void notch_filter_init(struct SecondOrderNotchFilter *filter, float cutoff_frequency, float bandwidth, uint16_t sample_frequency)
Initialize second order notch filter.
Definition: notch_filter.h:101
signed long int32_t
Definition: types.h:19
static void notch_filter_set_filter_frequency(struct SecondOrderNotchFilter *filter, float frequency)
Set notch filter frequency in Hz.
Definition: notch_filter.h:65
static void notch_filter_update(struct SecondOrderNotchFilter *filter, int32_t *input_signal, int32_t *output_signal)
Notch filter propagate.
Definition: notch_filter.h:79
static void notch_filter_set_bandwidth(struct SecondOrderNotchFilter *filter, float bandwidth)
Set bandwidth of the notch filter.
Definition: notch_filter.h:55