OpenCPN Partial API docs
Loading...
Searching...
No Matches
iirfilter.h
1/******************************************************************************
2 * iirfilter.h
3 *
4 * Project: Many
5 * Purpose: Class that implements single order inifinite-impulse-response
6 *filter Author: Transmitterdan
7 ***************************************************************************
8 * Copyright (C) 2016 *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 ***************************************************************************
25 */
26
27/**************************************************************************
28 * How to use: *
29 * *
30 * To create a filter object declare on instance of iirfilter. There are *
31 * 2 optional parameters to the constructor. The first (Fc) determines *
32 * the filter cutoff frequency. A value of 0.5 is basically no filtering *
33 * and smaller values decrease the cutoff frequency. If you think of the *
34 * filter as being "fast" or "slow" then 0.5 is fastest and smaller values*
35 * are "slower". The second parameter (tp) selects whether the underlying *
36 * filtered values represent a linear value (such as speed) or a circular *
37 * angle such as direction. The angle can be either in radians or degrees.*
38 * These values can be changed on the "fly" with the setFC() and setType()*
39 * methods. *
40 * The main method is filter() which accepts a new unfiltered value and *
41 * returns a fitered value. To obtain the most recent filter output use *
42 * the get() method. Lesser used methods are getType (returns tp) and *
43 * getFC() (returns FC). The reset() method resets the filter to zero. *
44 **************************************************************************
45 */
46#if !defined(IIRFILTER_CLASS_HEADER)
47#define IIRFILTER_CLASS_HEADER
48
49// Define filter types
50enum {
51 IIRFILTER_TYPE_LINEAR = 1 << 0,
52 IIRFILTER_TYPE_DEG = 1 << 1,
53 IIRFILTER_TYPE_RAD = 1 << 2
54};
55
56class iirfilter {
57public:
58 // iirfilter() {setFC(0.5); type = IIRFILTER_TYPE_LINEAR; reset();};
59
60 iirfilter(double fc = 0.5, int tp = IIRFILTER_TYPE_LINEAR);
61 ~iirfilter() {};
62 double filter(double data); // Return filtered data given new data point
63 void reset(double a = 0.0); // Clear filter
64 void setFC(double fc = 0.1); // Set cutoff frequency
65 void setType(int tp); // Set type of filter (linear or angle type)
66 double getFc(void); // Return cutoff frequency
67 int getType(void); // Return type of filter
68 double get(void); // Return the current filtered data
69
70protected:
71 void unwrapDeg(double deg);
72 void unwrapRad(double rad);
73
74private:
75 double a0;
76 double b1;
77 double accum;
78 double oldDeg;
79 double oldRad;
80 int wraps;
81 int type;
82};
83
84#endif