iir1

State.h
1
36 #ifndef IIR1_STATE_H
37 #define IIR1_STATE_H
38
39 #include "Common.h"
40 #include "Biquad.h"
41
42
43 #define DEFAULT_STATE DirectFormII
44
45 namespace Iir {
46
55 class IIR_EXPORT DirectFormI
56 {
57 public:
58 DirectFormI () = default;
59
60 void reset ()
61 {
62 m_x1 = 0;
63 m_x2 = 0;
64 m_y1 = 0;
65 m_y2 = 0;
66 }
67
68 inline double filter(const double in,
69 const Biquad& s)
70 {
71 const double out = s.m_b0*in + s.m_b1*m_x1 + s.m_b2*m_x2
72 - s.m_a1*m_y1 - s.m_a2*m_y2;
73 m_x2 = m_x1;
74 m_y2 = m_y1;
75 m_x1 = in;
76 m_y1 = out;
77
78 return out;
79 }
80
81 protected:
82 double m_x2 = 0.0; // x[n-2]
83 double m_y2 = 0.0; // y[n-2]
84 double m_x1 = 0.0; // x[n-1]
85 double m_y1 = 0.0; // y[n-1]
86 };
87
88 //------------------------------------------------------------------------------
89
99 class IIR_EXPORT DirectFormII
100 {
101 public:
102 DirectFormII () = default;
103
104 void reset ()
105 {
106 m_v1 = 0.0;
107 m_v2 = 0.0;
108 }
109
110 inline double filter(const double in,
111 const Biquad& s)
112 {
113 const double w = in - s.m_a1*m_v1 - s.m_a2*m_v2;
114 const double out = s.m_b0*w + s.m_b1*m_v1 + s.m_b2*m_v2;
115
116 m_v2 = m_v1;
117 m_v1 = w;
118
119 return out;
120 }
121
122 private:
123 double m_v1 = 0.0; // v[-1]
124 double m_v2 = 0.0; // v[-2]
125 };
126
127
128 //------------------------------------------------------------------------------
129
130 class IIR_EXPORT TransposedDirectFormII
131 {
132 public:
133 TransposedDirectFormII() = default;
134
135 void reset ()
136 {
137 m_s1 = 0.0;
138 m_s1_1 = 0.0;
139 m_s2 = 0.0;
140 m_s2_1 = 0.0;
141 }
142
143 inline double filter(const double in,
144 const Biquad& s)
145 {
146 const double out = m_s1_1 + s.m_b0*in;
147 m_s1 = m_s2_1 + s.m_b1*in - s.m_a1*out;
148 m_s2 = s.m_b2*in - s.m_a2*out;
149 m_s1_1 = m_s1;
150 m_s2_1 = m_s2;
151
152 return out;
153 }
154
155 private:
156 double m_s1 = 0.0;
157 double m_s1_1 = 0.0;
158 double m_s2 = 0.0;
159 double m_s2_1 = 0.0;
160 };
161
162 }
163
164 #endif
Iir::Biquad
Definition: Biquad.h:52
Iir::DirectFormII
Definition: State.h:100
Iir::DirectFormI
Definition: State.h:56
Iir::TransposedDirectFormII
Definition: State.h:131
Iir
Definition: Biquad.cpp:40