Larus glider flight sensor system 3.9.2024
Software-In-The-Loop test and validation system
Loading...
Searching...
No Matches
pt2.h
Go to the documentation of this file.
1/***********************************************************************/
25#ifndef PT2_H_
26#define PT2_H_
27
28#include <ringbuffer.h>
29#include "embedded_math.h"
30
31// butterworth filter prototype parameters at Fcutoff/Fsampling = 0.25
32// B coefficients -> nominator
33// A coefficients -> DE-nominator, A0 = 1
34#define B0 0.292893218813452
35#define B1 0.585786437626905
36#define B2 0.292893218813452
37#define A1 ZERO
38#define A2 0.171572875253810
39#define DESIGN_FREQUENCY 0.25
40
42template <class datatype, class basetype> class pt2
43{
44public:
46 : input( datatype()),
47 output( datatype()),
48 old( datatype()),
49 very_old( datatype())
50 {
52 basetype a0x = A2 * SQR(delta) - A1 + ONE;
53 basetype a1x = -2.0 * delta * A2 + (SQR(delta) + ONE) * A1 - 2.0 * delta;
54 basetype a2x = A2 - delta * A1 + SQR(delta);
55
56 basetype b0x = B2 * SQR( delta) - B1 * delta + B0;
57 basetype b1x = - 2.0 * delta * B2 + (SQR(delta) + 1) * B1 - 2.0 * delta * B0;
58 basetype b2x = B2 - delta * B1 + SQR( delta) * B0;
59
60 // normalize denominator a0 = ONE
61 a1 = a1x / a0x;
62 a2 = a2x / a0x;
63 b0 = b0x / a0x;
64 b1 = b1x / a0x;
65 b2 = b2x / a0x;
66
67 // fine-tune DC-gain = 1.0
68 delta = (ONE + a1 + a2) / (b0 + b1 + b2);
69 b0 *= delta;
70 b1 *= delta;
71 b2 *= delta;
72 }
74 {
75 basetype tuning = ONE / ( ONE + a1 + a2);
76 very_old = old = present_input * tuning;
77 input = output = present_input;
78 }
79 datatype respond( const datatype &input)
80 {
81 this->input=input;;
82 datatype x = input - old * a1 - very_old * a2;
83 output = x * b0 + old * b1 + very_old * b2;
84 very_old = old;
85 old = x;
86 return output;
87 }
88 datatype get_output( void) const
89 {
90 return output;
91 }
93 {
94 return input;
95 }
96private:
97 datatype input;
98 datatype output;
99 datatype old;
100 datatype very_old;
101 basetype b0, b1, b2, a1, a2;
102};
103
104#endif /* PT2_H_ */
Second order IIR filter.
Definition pt2.h:43
void settle(const datatype &present_input)
Definition pt2.h:73
pt2(basetype fcutoff)
constructor taking Fc/Fs
Definition pt2.h:45
datatype get_output(void) const
Definition pt2.h:88
datatype get_last_input(void) const
Definition pt2.h:92
datatype respond(const datatype &input)
Definition pt2.h:79
mathematical vector of arbitrary type and size
Definition vector.h:40
defines platform-dependent algorithms and constants
#define ONE
#define SIN(x)
#define A2
Definition pt2.h:38
#define B2
Definition pt2.h:36
#define B1
Definition pt2.h:35
#define B0
Definition pt2.h:34
#define DESIGN_FREQUENCY
Definition pt2.h:39
#define A1
Definition pt2.h:37
ring buffer helper class (template)
#define SQR(x)