Open FFBoard
Open source force feedback firmware
Loading...
Searching...
No Matches
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 * Based on http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/
8 */
9
10#include "Filters.h"
11
12#include <math.h>
13
14
16 z1 = z2 = 0.0;
17}
18Biquad::Biquad(BiquadType type, float Fc, float Q, float peakGainDB) {
19 setBiquad(type, Fc, Q, peakGainDB);
20}
21
24
30void Biquad::setFc(float Fc) {
31 Fc = clip<float,float>(Fc,0,0.5);
32 this->Fc = Fc;
33 calcBiquad();
34}
35
36float Biquad::getFc() const {
37 return this->Fc;
38}
39
43void Biquad::setQ(float Q) {
44 this->Q = Q;
45 calcBiquad();
46}
47
48float Biquad::getQ() const {
49 return this->Q;
50}
51
55float Biquad::process(float in) {
56 float out = in * a0 + z1;
57 z1 = in * a1 + z2 - b1 * out;
58 z2 = in * a2 - b2 * out;
59 return out;
60}
61
62void Biquad::setBiquad(BiquadType type, float Fc, float Q, float peakGainDB) {
63 Fc = clip<float,float>(Fc,0,0.5);
64 this->type = type;
65 this->Q = Q;
66 this->Fc = Fc;
67 this->peakGain = peakGainDB;
68 calcBiquad();
69}
70
71/*
72 * Updates parameters and resets the biquad filter
73 */
75 z1 = 0.0;
76 z2 = 0.0;
77 float norm;
78 float V = pow(10, fabs(peakGain) / 20.0);
79 float K = tan(M_PI * Fc);
80 switch (this->type) {
82 norm = 1 / (1 + K / Q + K * K);
83 a0 = K * K * norm;
84 a1 = 2 * a0;
85 a2 = a0;
86 b1 = 2 * (K * K - 1) * norm;
87 b2 = (1 - K / Q + K * K) * norm;
88 break;
89
91 norm = 1 / (1 + K / Q + K * K);
92 a0 = 1 * norm;
93 a1 = -2 * a0;
94 a2 = a0;
95 b1 = 2 * (K * K - 1) * norm;
96 b2 = (1 - K / Q + K * K) * norm;
97 break;
98
100 norm = 1 / (1 + K / Q + K * K);
101 a0 = K / Q * norm;
102 a1 = 0;
103 a2 = -a0;
104 b1 = 2 * (K * K - 1) * norm;
105 b2 = (1 - K / Q + K * K) * norm;
106 break;
107
109 norm = 1 / (1 + K / Q + K * K);
110 a0 = (1 + K * K) * norm;
111 a1 = 2 * (K * K - 1) * norm;
112 a2 = a0;
113 b1 = a1;
114 b2 = (1 - K / Q + K * K) * norm;
115 break;
116
117 case BiquadType::peak:
118 if (peakGain >= 0) { // boost
119 norm = 1 / (1 + 1/Q * K + K * K);
120 a0 = (1 + V/Q * K + K * K) * norm;
121 a1 = 2 * (K * K - 1) * norm;
122 a2 = (1 - V/Q * K + K * K) * norm;
123 b1 = a1;
124 b2 = (1 - 1/Q * K + K * K) * norm;
125 }
126 else { // cut
127 norm = 1 / (1 + V/Q * K + K * K);
128 a0 = (1 + 1/Q * K + K * K) * norm;
129 a1 = 2 * (K * K - 1) * norm;
130 a2 = (1 - 1/Q * K + K * K) * norm;
131 b1 = a1;
132 b2 = (1 - V/Q * K + K * K) * norm;
133 }
134 break;
136 if (peakGain >= 0) { // boost
137 norm = 1 / (1 + sqrt(2) * K + K * K);
138 a0 = (1 + sqrt(2*V) * K + V * K * K) * norm;
139 a1 = 2 * (V * K * K - 1) * norm;
140 a2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
141 b1 = 2 * (K * K - 1) * norm;
142 b2 = (1 - sqrt(2) * K + K * K) * norm;
143 }
144 else { // cut
145 norm = 1 / (1 + sqrt(2*V) * K + V * K * K);
146 a0 = (1 + sqrt(2) * K + K * K) * norm;
147 a1 = 2 * (K * K - 1) * norm;
148 a2 = (1 - sqrt(2) * K + K * K) * norm;
149 b1 = 2 * (V * K * K - 1) * norm;
150 b2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
151 }
152 break;
154 if (peakGain >= 0) { // boost
155 norm = 1 / (1 + sqrt(2) * K + K * K);
156 a0 = (V + sqrt(2*V) * K + K * K) * norm;
157 a1 = 2 * (K * K - V) * norm;
158 a2 = (V - sqrt(2*V) * K + K * K) * norm;
159 b1 = 2 * (K * K - 1) * norm;
160 b2 = (1 - sqrt(2) * K + K * K) * norm;
161 }
162 else { // cut
163 norm = 1 / (V + sqrt(2*V) * K + K * K);
164 a0 = (1 + sqrt(2) * K + K * K) * norm;
165 a1 = 2 * (K * K - 1) * norm;
166 a2 = (1 - sqrt(2) * K + K * K) * norm;
167 b1 = 2 * (K * K - V) * norm;
168 b2 = (V - sqrt(2*V) * K + K * K) * norm;
169 }
170 break;
171 }
172
173 return;
174}
BiquadType
Definition Filters.h:20
@ highshelf
Definition Filters.h:27
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:30
float z2
Definition Filters.h:50
float b2
Definition Filters.h:48
float process(float in)
Definition Filters.cpp:55
BiquadType type
Definition Filters.h:47
float getQ() const
Definition Filters.cpp:48
void setQ(float Q)
Definition Filters.cpp:43
Biquad()
Definition Filters.cpp:15
void calcBiquad(void)
Definition Filters.cpp:74
float a1
Definition Filters.h:48
float Q
Definition Filters.h:49
float getFc() const
Definition Filters.cpp:36
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:62
float Fc
Definition Filters.h:49
~Biquad()
Definition Filters.cpp:22
T clip(T v, C l, C h)
Definition cppmain.h:58