Skip to content

Commit e6fd759

Browse files
committed
Performance optimization for float and double
1 parent afe4c71 commit e6fd759

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/AudioTools/Filter.h

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "AudioTools/Converter.h"
3+
#include <type_traits>
34

45
namespace audio_tools {
56

@@ -57,8 +58,14 @@ class FIR : public Filter<T> {
5758
x[i_b] = value;
5859
T b_terms = 0;
5960
T *b_shift = &coeff_b[lenB - i_b - 1];
60-
for (uint8_t i = 0; i < lenB; i++) {
61-
b_terms += b_shift[i] * x[i] / factor;
61+
if (std::is_same<T, float>::value || std::is_same<T, double>::value) {
62+
for (uint8_t i = 0; i < lenB; i++) {
63+
b_terms += b_shift[i] * x[i] ;
64+
}
65+
} else {
66+
for (uint8_t i = 0; i < lenB; i++) {
67+
b_terms += b_shift[i] * x[i] / factor;
68+
}
6269
}
6370
i_b++;
6471
if(i_b == lenB)
@@ -111,14 +118,26 @@ class IIR : public Filter<T> {
111118
x[i_b] = value;
112119
T b_terms = 0;
113120
T *b_shift = &coeff_b[lenB - i_b - 1];
114-
for (uint8_t i = 0; i < lenB; i++) {
115-
b_terms += x[i] * b_shift[i] / factor;
116-
}
121+
117122
T a_terms = 0;
118123
T *a_shift = &coeff_a[lenA - i_a - 1];
119-
for (uint8_t i = 0; i < lenA; i++) {
120-
a_terms += y[i] * a_shift[i] / factor;
124+
125+
if (std::is_same<T, float>::value || std::is_same<T, double>::value) {
126+
for (uint8_t i = 0; i < lenB; i++) {
127+
b_terms += x[i] * b_shift[i];
128+
}
129+
for (uint8_t i = 0; i < lenA; i++) {
130+
a_terms += y[i] * a_shift[i];
131+
}
132+
} else {
133+
for (uint8_t i = 0; i < lenB; i++) {
134+
b_terms += x[i] * b_shift[i] / factor;
135+
}
136+
for (uint8_t i = 0; i < lenA; i++) {
137+
a_terms += y[i] * a_shift[i] / factor;
138+
}
121139
}
140+
122141
T filtered = b_terms - a_terms;
123142
y[i_a] = filtered;
124143
i_b++;

0 commit comments

Comments
 (0)