Paparazzi UAS v7.0_unstable
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
41
48{
49 filter->Ts = 1.0 / frequency;
50}
51
57static 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
69{
70 float theta = 2.0 * M_PI * frequency * filter->Ts;
71 filter->costheta = cosf(theta);
72}
73
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
119static 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);
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
uint16_t foo
Definition main_demo5.c:58
static int32_t notch_filter_get_output(struct SecondOrderNotchFilter *filter)
Get latest notch filter output.
static void notch_filter_update(struct SecondOrderNotchFilter *filter, int32_t *input_signal, int32_t *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 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.
int int32_t
Typedef defining 32 bit int type.
float b
Definition wedgebug.c:202