Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
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, int32_t *input_signal,
84  int32_t *output_signal)
85 {
86  float a = (1 + filter->d2) * 0.5;
87  float b = (1 + filter->d2) * filter->costheta;
88  *output_signal = (b * filter->yn1) - (filter->d2 * filter->yn2) + (a * *input_signal) - (b * filter->xn1) +
89  (a * filter->xn2);
90 
91  /* Update values for next update */
92  filter->xn2 = filter->xn1;
93  filter->xn1 = *input_signal;
94  filter->yn2 = filter->yn1;
95  filter->yn1 = *output_signal;
96 }
97 
106 {
107  return filter->yn1;
108 }
109 
119 static inline void notch_filter_init(struct SecondOrderNotchFilter *filter, float cutoff_frequency, float bandwidth,
120  uint16_t sample_frequency)
121 {
122  notch_filter_set_sampling_frequency(filter, sample_frequency);
123  notch_filter_set_filter_frequency(filter, cutoff_frequency);
124  notch_filter_set_bandwidth(filter, bandwidth);
125  filter->xn1 = 0;
126  filter->xn2 = 0;
127  filter->yn1 = 0;
128  filter->yn2 = 0;
129 }
130 
131 #endif
static int32_t notch_filter_get_output(struct SecondOrderNotchFilter *filter)
Get latest notch filter output.
Definition: notch_filter.h:105
static void notch_filter_update(struct SecondOrderNotchFilter *filter, int32_t *input_signal, int32_t *output_signal)
Notch filter propagate.
Definition: notch_filter.h:83
static void notch_filter_set_sampling_frequency(struct SecondOrderNotchFilter *filter, uint16_t frequency)
Set sampling frequency of the notch filter.
Definition: notch_filter.h:47
static void notch_filter_set_filter_frequency(struct SecondOrderNotchFilter *filter, float frequency)
Set notch filter frequency in Hz.
Definition: notch_filter.h:68
static void notch_filter_set_bandwidth(struct SecondOrderNotchFilter *filter, float bandwidth)
Set bandwidth of the notch filter.
Definition: notch_filter.h:57
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:119
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
float b
Definition: wedgebug.c:202