libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
hp_filter.h
Go to the documentation of this file.
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license in the file COPYING
10 * or http://www.opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file COPYING.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2023 Saso Kiselkov. All rights reserved.
24 */
33#ifndef _ACF_UTILS_HP_FILTER_H_
34#define _ACF_UTILS_HP_FILTER_H_
35
36#include "core.h"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
52typedef struct {
53 double state;
54 double prev;
55 double RC;
57
64static inline void
65hp_filter_init(hp_filter_t *filt, double f_cutoff)
66{
67 ASSERT(filt != NULL);
68 ASSERT3F(f_cutoff, >, 0);
69 filt->state = NAN;
70 filt->prev = NAN;
71 filt->RC = 1.0 / (2 * M_PI * f_cutoff);
72}
73
87static inline double
88hp_filter_update(hp_filter_t *filt, double m, double d_t)
89{
90 ASSERT(filt != NULL);
91 ASSERT3F(d_t, >, 0);
92 if (isnan(filt->state)) {
93 filt->state = m;
94 filt->prev = m;
95 } else {
96 double alpha = filt->RC / (filt->RC + d_t);
97 ASSERT(!isnan(filt->prev));
98 filt->state = alpha * filt->state + alpha * (m - filt->prev);
99 filt->prev = m;
100 }
101 return (filt->state);
102}
103
112static inline double
114{
115 ASSERT(filt != NULL);
116 return (filt->state);
117}
118
124static inline void
125hp_filter_set_f_cutoff(hp_filter_t *filt, double f_cutoff)
126{
127 ASSERT(filt != NULL);
128 ASSERT3F(f_cutoff, >, 0);
129 filt->RC = 1.0 / (2 * M_PI * f_cutoff);
130}
131
135static inline double
137{
138 ASSERT(filt != NULL);
139 return (1.0 / (2 * M_PI * filt->RC));
140}
141
142#ifdef __cplusplus
143}
144#endif
145
146#endif /* _ACF_UTILS_HP_FILTER_H_ */
#define ASSERT(x)
Definition assert.h:208
#define ASSERT3F(x, op, y)
Definition assert.h:211
static double hp_filter_update(hp_filter_t *filt, double m, double d_t)
Definition hp_filter.h:88
static void hp_filter_set_f_cutoff(hp_filter_t *filt, double f_cutoff)
Definition hp_filter.h:125
static double hp_filter_get_f_cutoff(const hp_filter_t *filt)
Definition hp_filter.h:136
static double hp_filter_get(const hp_filter_t *filt)
Definition hp_filter.h:113
static void hp_filter_init(hp_filter_t *filt, double f_cutoff)
Definition hp_filter.h:65
double RC
Definition hp_filter.h:55
double state
Definition hp_filter.h:53
double prev
Definition hp_filter.h:54