Open FFBoard
Open source force feedback firmware
Filters.cpp
Go to the documentation of this file.
1/*
2 * Filters.cpp
3 *
4 * Created on: Feb 13, 2020
5 * Author: Yannick
6 */
7
8#include "Filters.h"
9
10#include <math.h>
11
12
14 z1 = z2 = 0.0;
15}
16Biquad::Biquad(BiquadType type, float Fc, float Q, float peakGainDB) {
17 setBiquad(type, Fc, Q, peakGainDB);
18}
19
21}
22
28void Biquad::setFc(float Fc) {
29 Fc = clip<float,float>(Fc,0,0.5);
30 this->Fc = Fc;
31 calcBiquad();
32}
33
34float Biquad::getFc() const {
35 return this->Fc;
36}
37
41void Biquad::setQ(float Q) {
42 this->Q = Q;
43 calcBiquad();
44}
45
46float Biquad::getQ() const {
47 return this->Q;
48}
49
53float Biquad::process(float in) {
54 float out = in * a0 + z1;
55 z1 = in * a1 + z2 - b1 * out;
56 z2 = in * a2 - b2 * out;
57 return out;
58}
59
60void Biquad::setBiquad(BiquadType type, float Fc, float Q, float peakGainDB) {
61 Fc = clip<float,float>(Fc,0,0.5);
62 this->type = type;
63 this->Q = Q;
64 this->Fc = Fc;
65 this->peakGain = peakGainDB;
66 calcBiquad();
67}
68
69/*
70 * Updates parameters and resets the biquad filter
71 */
73 z1 = 0.0;
74 z2 = 0.0;
75 float norm;
76 float V = pow(10, fabs(peakGain) / 20.0);
77 float K = tan(M_PI * Fc);
78 switch (this->type) {
80 norm = 1 / (1 + K / Q + K * K);
81 a0 = K * K * norm;
82 a1 = 2 * a0;
83 a2 = a0;
84 b1 = 2 * (K * K - 1) * norm;
85 b2 = (1 - K / Q + K * K) * norm;
86 break;
87
89 norm = 1 / (1 + K / Q + K * K);
90 a0 = 1 * norm;
91 a1 = -2 * a0;
92 a2 = a0;
93 b1 = 2 * (K * K - 1) * norm;
94 b2 = (1 - K / Q + K * K) * norm;
95 break;
96
98 norm = 1 / (1 + K / Q + K * K);
99 a0 = K / Q * norm;
100 a1 = 0;
101 a2 = -a0;
102 b1 = 2 * (K * K - 1) * norm;
103 b2 = (1 - K / Q + K * K) * norm;
104 break;
105
107 norm = 1 / (1 + K / Q + K * K);
108 a0 = (1 + K * K) * norm;
109 a1 = 2 * (K * K - 1) * norm;
110 a2 = a0;
111 b1 = a1;
112 b2 = (1 - K / Q + K * K) * norm;
113 break;
114
115 case BiquadType::peak:
116 if (peakGain >= 0) { // boost
117 norm = 1 / (1 + 1/Q * K + K * K);
118 a0 = (1 + V/Q * K + K * K) * norm;
119 a1 = 2 * (K * K - 1) * norm;
120 a2 = (1 - V/Q * K + K * K) * norm;
121 b1 = a1;
122 b2 = (1 - 1/Q * K + K * K) * norm;
123 }
124 else { // cut
125 norm = 1 / (1 + V/Q * K + K * K);
126 a0 = (1 + 1/Q * K + K * K) * norm;
127 a1 = 2 * (K * K - 1) * norm;
128 a2 = (1 - 1/Q * K + K * K) * norm;
129 b1 = a1;
130 b2 = (1 - V/Q * K + K * K) * norm;
131 }
132 break;
134 if (peakGain >= 0) { // boost
135 norm = 1 / (1 + sqrt(2) * K + K * K);
136 a0 = (1 + sqrt(2*V) * K + V * K * K) * norm;
137 a1 = 2 * (V * K * K - 1) * norm;
138 a2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
139 b1 = 2 * (K * K - 1) * norm;
140 b2 = (1 - sqrt(2) * K + K * K) * norm;
141 }
142 else { // cut
143 norm = 1 / (1 + sqrt(2*V) * K + V * K * K);
144 a0 = (1 + sqrt(2) * K + K * K) * norm;
145 a1 = 2 * (K * K - 1) * norm;
146 a2 = (1 - sqrt(2) * K + K * K) * norm;
147 b1 = 2 * (V * K * K - 1) * norm;
148 b2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
149 }
150 break;
152 if (peakGain >= 0) { // boost
153 norm = 1 / (1 + sqrt(2) * K + K * K);
154 a0 = (V + sqrt(2*V) * K + K * K) * norm;
155 a1 = 2 * (K * K - V) * norm;
156 a2 = (V - sqrt(2*V) * K + K * K) * norm;
157 b1 = 2 * (K * K - 1) * norm;
158 b2 = (1 - sqrt(2) * K + K * K) * norm;
159 }
160 else { // cut
161 norm = 1 / (V + sqrt(2*V) * K + K * K);
162 a0 = (1 + sqrt(2) * K + K * K) * norm;
163 a1 = 2 * (K * K - 1) * norm;
164 a2 = (1 - sqrt(2) * K + K * K) * norm;
165 b1 = 2 * (K * K - V) * norm;
166 b2 = (V - sqrt(2*V) * K + K * K) * norm;
167 }
168 break;
169 }
170
171 return;
172}
BiquadType
Definition: Filters.h:20
float a0
Definition: Filters.h:48
float b1
Definition: Filters.h:48
float z1
Definition: Filters.h:50
void setFc(float Fc)
Definition: Filters.cpp:28
float z2
Definition: Filters.h:50
float b2
Definition: Filters.h:48
float process(float in)
Definition: Filters.cpp:53
BiquadType type
Definition: Filters.h:47
float getQ() const
Definition: Filters.cpp:46
void setQ(float Q)
Definition: Filters.cpp:41
Biquad()
Definition: Filters.cpp:13
void calcBiquad(void)
Definition: Filters.cpp:72
float a1
Definition: Filters.h:48
float Q
Definition: Filters.h:49
float getFc() const
Definition: Filters.cpp:34
float a2
Definition: Filters.h:48
float peakGain
Definition: Filters.h:49
void setBiquad(BiquadType type, float Fc, float Q, float peakGain)
Definition: Filters.cpp:60
float Fc
Definition: Filters.h:49
~Biquad()
Definition: Filters.cpp:20