Skip to content

Commit a5d120f

Browse files
committed
Use int type s with filters
1 parent 3318758 commit a5d120f

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/AudioTools/Filter.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ template <typename T>
4242
class FIR : public Filter<T> {
4343
public:
4444
template <size_t B>
45-
FIR(const T (&b)[B]) : lenB(B) {
45+
FIR(const T (&b)[B], const T factor=1.0) : lenB(B), factor(factor) {
4646
x = new T[lenB]();
4747
coeff_b = new T[2*lenB-1];
4848
for (uint8_t i = 0; i < 2*lenB-1; i++) {
@@ -58,7 +58,7 @@ class FIR : public Filter<T> {
5858
T b_terms = 0;
5959
T *b_shift = &coeff_b[lenB - i_b - 1];
6060
for (uint8_t i = 0; i < lenB; i++) {
61-
b_terms += x[i] * b_shift[i];
61+
b_terms += b_shift[i] * x[i] / factor;
6262
}
6363
i_b++;
6464
if(i_b == lenB)
@@ -70,6 +70,7 @@ class FIR : public Filter<T> {
7070
uint8_t i_b = 0;
7171
T *x;
7272
T *coeff_b;
73+
T factor;
7374
};
7475

7576

@@ -84,7 +85,7 @@ template <typename T>
8485
class IIR : public Filter<T> {
8586
public:
8687
template <size_t B, size_t A>
87-
IIR(const T (&b)[B], const T (&_a)[A]) : lenB(B), lenA(A - 1) {
88+
IIR(const T (&b)[B], const T (&_a)[A], T factor=1.0) : factor(factor), lenB(B), lenA(A - 1) {
8889
x = new T[lenB]();
8990
y = new T[lenA]();
9091
coeff_b = new T[2 * lenB - 1];
@@ -111,12 +112,12 @@ class IIR : public Filter<T> {
111112
T b_terms = 0;
112113
T *b_shift = &coeff_b[lenB - i_b - 1];
113114
for (uint8_t i = 0; i < lenB; i++) {
114-
b_terms += x[i] * b_shift[i];
115+
b_terms += x[i] * b_shift[i] / factor;
115116
}
116117
T a_terms = 0;
117118
T *a_shift = &coeff_a[lenA - i_a - 1];
118119
for (uint8_t i = 0; i < lenA; i++) {
119-
a_terms += y[i] * a_shift[i];
120+
a_terms += y[i] * a_shift[i] / factor;
120121
}
121122
T filtered = b_terms - a_terms;
122123
y[i_a] = filtered;
@@ -128,6 +129,7 @@ class IIR : public Filter<T> {
128129
}
129130

130131
private:
132+
T factor;
131133
const uint8_t lenB, lenA;
132134
uint8_t i_b = 0, i_a = 0;
133135
T *x;
@@ -139,12 +141,13 @@ class IIR : public Filter<T> {
139141
/**
140142
* @brief Biquad DF1 Filter.
141143
* converted from https://github.com/tttapa/Filters/blob/master/src/BiQuad.h
144+
* Use float or double (and not a integer type) as type parameter
142145
* @author Pieter P tttapa / pschatzmann
143146
* @copyright GNU General Public License v3.0
144147
* @tparam T
145148
*/
146149
template <typename T>
147-
class BiQuadDF1 : public Filter<T> {
150+
class BiQuadDF1 : public Filter<float> {
148151
public:
149152
BiQuadDF1(const T (&b)[3], const T (&a)[3])
150153
: b_0(b[0] / a[0]),
@@ -196,6 +199,7 @@ class BiQuadDF1 : public Filter<T> {
196199
* @brief Biquad DF2 Filter. When dealing with high-order IIR filters, they can get unstable.
197200
* To prevent this, BiQuadratic filters (second order) are used.
198201
* Converted from https://github.com/tttapa/Filters/blob/master/src/BiQuad.h
202+
* Use float or double (and not a integer type) as type parameter
199203
* @author Pieter P tttapa / pschatzmann
200204
* @copyright GNU General Public License v3.0
201205
* @tparam T
@@ -246,6 +250,7 @@ class BiQuadDF2 : public Filter<T> {
246250
/**
247251
* Second Order Filter: Instead of manually cascading BiQuad filters, you can use a Second Order Sections filter (SOS).
248252
* converted from https://github.com/tttapa/Filters/blob/master/src/SOSFilter.h
253+
* Use float or double (and not a integer type) as type parameter
249254
* @author Pieter P tttapa / pschatzmann
250255
* @copyright GNU General Public License v3.0
251256
*/

0 commit comments

Comments
 (0)