Skip to content

Commit ea85680

Browse files
committed
Filter optimizations
1 parent e6fd759 commit ea85680

File tree

2 files changed

+182
-23
lines changed

2 files changed

+182
-23
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#include "AudioTools.h"
2+
3+
4+
double coefd[137] = {
5+
0.000188725585189891, 0.000091879473225926, -0.000016920442768766,
6+
-0.000133280461026043, -0.000251640080720683, -0.000364923626390918,
7+
-0.000464412185430885, -0.000539946399700805, -0.000580528007418057,
8+
-0.000575329172682575, -0.000515050625648420, -0.000393501287598472,
9+
-0.000209212939699419, 0.000033137236602991, 0.000321738209246393,
10+
0.000637316639800423, 0.000953645970946306, 0.001238992395833948,
11+
0.001458495336203241, 0.001577364656923383, 0.001564660155918535,
12+
0.001397314635940224, 0.001063983705740325, 0.000568265297502738,
13+
-0.000069161528118792, -0.000809871255352040, -0.001598785008585991,
14+
-0.002367155523627244, -0.003037394102238113, -0.003529490734672840,
15+
-0.003768583450610565, -0.003693062300853045, -0.003262467087264144,
16+
-0.002464373079849631, -0.001319467692097694, 0.000115890783337027,
17+
0.001750175413602121, 0.003460562753784068, 0.005100198277458642,
18+
0.006508564756602457, 0.007524348715512882, 0.007999914326580530,
19+
0.007816265949382166, 0.006897235277941903, 0.005221586005661731,
20+
0.002831799051373308, -0.000161514004361169, -0.003580331626899644,
21+
-0.007185463286171087, -0.010688262221784543, -0.013767303340739213,
22+
-0.016089076144421111, -0.017331352983485829, -0.017207593283301702,
23+
-0.015490564521582872, -0.012033321379788043, -0.006785795029102398,
24+
0.000194498174472729, 0.008738731003948556, 0.018570633247558595,
25+
0.029318005390909025, 0.040531349219315310, 0.051708529390585928,
26+
0.062323892873769583, 0.071859896969611939, 0.079839064047870847,
27+
0.085854013433190587, 0.089593426008022170, 0.090862068888852371,
28+
0.089593426008022170, 0.085854013433190587, 0.079839064047870847,
29+
0.071859896969611939, 0.062323892873769583, 0.051708529390585928,
30+
0.040531349219315317, 0.029318005390909029, 0.018570633247558595,
31+
0.008738731003948556, 0.000194498174472729, -0.006785795029102398,
32+
-0.012033321379788046, -0.015490564521582872, -0.017207593283301702,
33+
-0.017331352983485829, -0.016089076144421115, -0.013767303340739216,
34+
-0.010688262221784546, -0.007185463286171090, -0.003580331626899644,
35+
-0.000161514004361169, 0.002831799051373308, 0.005221586005661734,
36+
0.006897235277941905, 0.007816265949382166, 0.007999914326580530,
37+
0.007524348715512883, 0.006508564756602457, 0.005100198277458645,
38+
0.003460562753784068, 0.001750175413602121, 0.000115890783337027,
39+
-0.001319467692097694, -0.002464373079849632, -0.003262467087264142,
40+
-0.003693062300853045, -0.003768583450610565, -0.003529490734672841,
41+
-0.003037394102238113, -0.002367155523627246, -0.001598785008585990,
42+
-0.000809871255352040, -0.000069161528118792, 0.000568265297502738,
43+
0.001063983705740327, 0.001397314635940224, 0.001564660155918536,
44+
0.001577364656923383, 0.001458495336203241, 0.001238992395833948,
45+
0.000953645970946307, 0.000637316639800423, 0.000321738209246393,
46+
0.000033137236602991, -0.000209212939699420, -0.000393501287598472,
47+
-0.000515050625648420, -0.000575329172682575, -0.000580528007418057,
48+
-0.000539946399700805, -0.000464412185430885, -0.000364923626390918,
49+
-0.000251640080720683, -0.000133280461026043, -0.000016920442768766,
50+
0.000091879473225926, 0.000188725585189891};
51+
52+
53+
float coeff[137];
54+
int16_t coef16[137];
55+
int32_t coef32[137];
56+
int64_t coef64[137];
57+
58+
static int16_t const max_array = 300;
59+
int16_t result_array[max_array];
60+
61+
void setupCoef() {
62+
for (int j=0;j<137;j++){
63+
coeff[j] = coefd[j];
64+
coef16[j] = coefd[j]*32767;
65+
coef32[j] = coefd[j]*32767;
66+
coef64[j] = coefd[j]*32767;
67+
}
68+
}
69+
70+
void listResult() {
71+
for (int j = 0; j < 200; j++) {
72+
Serial.print(result_array[j]);
73+
Serial.print(" ");
74+
}
75+
}
76+
77+
void processFloat() {
78+
FIR<float> firf(coeff);
79+
unsigned long start = millis();
80+
for (int j = 0; j < 44100; j++) {
81+
int16_t result = firf.process(32767);
82+
if (j < max_array) {
83+
result_array[j] = result;
84+
}
85+
}
86+
Serial.println();
87+
Serial.print("float ms: ");
88+
Serial.println(millis() - start);
89+
}
90+
91+
void processDouble() {
92+
FIR<double> fird(coefd);
93+
unsigned long start = millis();
94+
for (int j = 0; j < 44100; j++) {
95+
int16_t result = fird.process(32767);
96+
if (j < max_array) {
97+
result_array[j] = result;
98+
}
99+
}
100+
Serial.println();
101+
Serial.print("double ms: ");
102+
Serial.println(millis() - start);
103+
}
104+
105+
void processInt16() {
106+
FIR<int16_t> fir16(coef16, 32767);
107+
unsigned long start = millis();
108+
for (int j = 0; j < 44100; j++) {
109+
int16_t result = fir16.process(32767);
110+
if (j < max_array) {
111+
result_array[j] = result;
112+
}
113+
}
114+
Serial.println();
115+
Serial.print("int16 ms: ");
116+
Serial.println(millis() - start);
117+
}
118+
119+
void processInt32() {
120+
FIR<int32_t> fir32(coef32, 32767);
121+
unsigned long start = millis();
122+
for (int j = 0; j < 44100; j++) {
123+
int16_t result = fir32.process(32767);
124+
if (j < max_array) {
125+
result_array[j] = result;
126+
}
127+
}
128+
Serial.println();
129+
Serial.print("int32 ms: ");
130+
Serial.println(millis() - start);
131+
}
132+
133+
void processInt64() {
134+
FIR<int64_t> fir64(coef64, 32767);
135+
unsigned long start = millis();
136+
for (int j = 0; j < 44100; j++) {
137+
int16_t result = fir64.process(32767);
138+
if (j < max_array) {
139+
result_array[j] = result;
140+
}
141+
}
142+
Serial.println();
143+
Serial.print("int64 ms: ");
144+
Serial.println(millis() - start);
145+
}
146+
147+
148+
void setup() {
149+
Serial.begin(119200);
150+
while (!Serial)
151+
;
152+
setupCoef();
153+
processFloat();
154+
listResult();
155+
processDouble();
156+
listResult();
157+
processInt16();
158+
listResult();
159+
processInt32();
160+
listResult();
161+
processInt64();
162+
listResult();
163+
}
164+
165+
void loop() {}

src/AudioTools/Filter.h

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,17 @@ class FIR : public Filter<T> {
5858
x[i_b] = value;
5959
T b_terms = 0;
6060
T *b_shift = &coeff_b[lenB - i_b - 1];
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-
}
61+
for (uint8_t i = 0; i < lenB; i++) {
62+
b_terms += b_shift[i] * x[i] ;
6963
}
7064
i_b++;
7165
if(i_b == lenB)
7266
i_b = 0;
67+
68+
if (!(std::is_same<T, float>::value || std::is_same<T, double>::value)) {
69+
b_terms = b_terms / factor);
70+
}
71+
7372
return b_terms;
7473
}
7574
private:
@@ -122,28 +121,23 @@ class IIR : public Filter<T> {
122121
T a_terms = 0;
123122
T *a_shift = &coeff_a[lenA - i_a - 1];
124123

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-
}
124+
for (uint8_t i = 0; i < lenB; i++) {
125+
b_terms += x[i] * b_shift[i];
126+
}
127+
for (uint8_t i = 0; i < lenA; i++) {
128+
a_terms += y[i] * a_shift[i];
139129
}
140130

141131
T filtered = b_terms - a_terms;
142132
y[i_a] = filtered;
143133
i_b++;
144134
if (i_b == lenB) i_b = 0;
145135
i_a++;
146-
if (i_a == lenA) i_a = 0;
136+
if (i_a == lenA) i_a = 0
137+
138+
if (!(std::is_same<T, float>::value || std::is_same<T, double>::value)) {
139+
filtered = filtered / factor);
140+
}
147141
return filtered;
148142
}
149143

0 commit comments

Comments
 (0)