Skip to content

Commit afe4c71

Browse files
committed
Filter Corrections
1 parent a5d120f commit afe4c71

File tree

2 files changed

+149
-22
lines changed

2 files changed

+149
-22
lines changed

src/AudioTools/Filter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class FIR : public Filter<T> {
4545
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];
48-
for (uint8_t i = 0; i < 2*lenB-1; i++) {
48+
for (uint16_t i = 0; i < 2*lenB-1; i++) {
4949
coeff_b[i] = b[(2*lenB - 1 - i)%lenB];
5050
}
5151
}
@@ -92,10 +92,10 @@ class IIR : public Filter<T> {
9292
coeff_a = new T[2 * lenA - 1];
9393
T a0 = _a[0];
9494
const T *a = &_a[1];
95-
for (uint8_t i = 0; i < 2 * lenB - 1; i++) {
95+
for (uint16_t i = 0; i < 2 * lenB - 1; i++) {
9696
coeff_b[i] = b[(2 * lenB - 1 - i) % lenB] / a0;
9797
}
98-
for (uint8_t i = 0; i < 2 * lenA - 1; i++) {
98+
for (uint16_t i = 0; i < 2 * lenA - 1; i++) {
9999
coeff_a[i] = a[(2 * lenA - 2 - i) % lenA] / a0;
100100
}
101101
}

tests/filter-wav/filter-wav.cpp

Lines changed: 146 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,155 @@
44
#include "AudioLibs/PortAudioStream.h"
55
#include "SdFat.h"
66

7-
// define FIR filter
8-
int16_t coef[] = {
9-
-600,
10-
-1859,
11-
608,
12-
3200,
13-
-612,
14-
-9752,
15-
18032,
16-
-9752,
17-
-612,
18-
3200,
19-
608,
20-
-1859,
21-
-600
7+
// define FIR filter https://fiiir.com/
8+
// sampleing rate : 44100
9+
// cutoff frequency: 2000
10+
// Transition bandwidth: 2000
11+
float coef[] = {
12+
0.000188725585189891,
13+
0.000091879473225926,
14+
-0.000016920442768766,
15+
-0.000133280461026043,
16+
-0.000251640080720683,
17+
-0.000364923626390918,
18+
-0.000464412185430885,
19+
-0.000539946399700805,
20+
-0.000580528007418057,
21+
-0.000575329172682575,
22+
-0.000515050625648420,
23+
-0.000393501287598472,
24+
-0.000209212939699419,
25+
0.000033137236602991,
26+
0.000321738209246393,
27+
0.000637316639800423,
28+
0.000953645970946306,
29+
0.001238992395833948,
30+
0.001458495336203241,
31+
0.001577364656923383,
32+
0.001564660155918535,
33+
0.001397314635940224,
34+
0.001063983705740325,
35+
0.000568265297502738,
36+
-0.000069161528118792,
37+
-0.000809871255352040,
38+
-0.001598785008585991,
39+
-0.002367155523627244,
40+
-0.003037394102238113,
41+
-0.003529490734672840,
42+
-0.003768583450610565,
43+
-0.003693062300853045,
44+
-0.003262467087264144,
45+
-0.002464373079849631,
46+
-0.001319467692097694,
47+
0.000115890783337027,
48+
0.001750175413602121,
49+
0.003460562753784068,
50+
0.005100198277458642,
51+
0.006508564756602457,
52+
0.007524348715512882,
53+
0.007999914326580530,
54+
0.007816265949382166,
55+
0.006897235277941903,
56+
0.005221586005661731,
57+
0.002831799051373308,
58+
-0.000161514004361169,
59+
-0.003580331626899644,
60+
-0.007185463286171087,
61+
-0.010688262221784543,
62+
-0.013767303340739213,
63+
-0.016089076144421111,
64+
-0.017331352983485829,
65+
-0.017207593283301702,
66+
-0.015490564521582872,
67+
-0.012033321379788043,
68+
-0.006785795029102398,
69+
0.000194498174472729,
70+
0.008738731003948556,
71+
0.018570633247558595,
72+
0.029318005390909025,
73+
0.040531349219315310,
74+
0.051708529390585928,
75+
0.062323892873769583,
76+
0.071859896969611939,
77+
0.079839064047870847,
78+
0.085854013433190587,
79+
0.089593426008022170,
80+
0.090862068888852371,
81+
0.089593426008022170,
82+
0.085854013433190587,
83+
0.079839064047870847,
84+
0.071859896969611939,
85+
0.062323892873769583,
86+
0.051708529390585928,
87+
0.040531349219315317,
88+
0.029318005390909029,
89+
0.018570633247558595,
90+
0.008738731003948556,
91+
0.000194498174472729,
92+
-0.006785795029102398,
93+
-0.012033321379788046,
94+
-0.015490564521582872,
95+
-0.017207593283301702,
96+
-0.017331352983485829,
97+
-0.016089076144421115,
98+
-0.013767303340739216,
99+
-0.010688262221784546,
100+
-0.007185463286171090,
101+
-0.003580331626899644,
102+
-0.000161514004361169,
103+
0.002831799051373308,
104+
0.005221586005661734,
105+
0.006897235277941905,
106+
0.007816265949382166,
107+
0.007999914326580530,
108+
0.007524348715512883,
109+
0.006508564756602457,
110+
0.005100198277458645,
111+
0.003460562753784068,
112+
0.001750175413602121,
113+
0.000115890783337027,
114+
-0.001319467692097694,
115+
-0.002464373079849632,
116+
-0.003262467087264142,
117+
-0.003693062300853045,
118+
-0.003768583450610565,
119+
-0.003529490734672841,
120+
-0.003037394102238113,
121+
-0.002367155523627246,
122+
-0.001598785008585990,
123+
-0.000809871255352040,
124+
-0.000069161528118792,
125+
0.000568265297502738,
126+
0.001063983705740327,
127+
0.001397314635940224,
128+
0.001564660155918536,
129+
0.001577364656923383,
130+
0.001458495336203241,
131+
0.001238992395833948,
132+
0.000953645970946307,
133+
0.000637316639800423,
134+
0.000321738209246393,
135+
0.000033137236602991,
136+
-0.000209212939699420,
137+
-0.000393501287598472,
138+
-0.000515050625648420,
139+
-0.000575329172682575,
140+
-0.000580528007418057,
141+
-0.000539946399700805,
142+
-0.000464412185430885,
143+
-0.000364923626390918,
144+
-0.000251640080720683,
145+
-0.000133280461026043,
146+
-0.000016920442768766,
147+
0.000091879473225926,
148+
0.000188725585189891
22149
};
23150
//
24151
uint16_t sample_rate=44100;
25152
uint8_t channels = 2; // The stream will have 2 channels
26153
SineWaveGenerator<int16_t> wave(32000); // subclass of SoundGenerator with max amplitude of 32000
27154
GeneratedSoundStream<int16_t> in_stream(wave); // Stream generated from sine wave
28-
FilteredStream<int16_t, int16_t> in_filtered(in_stream, channels); // Defiles the filter as BaseConverter
155+
FilteredStream<int16_t, float> in_filtered(in_stream, channels); // Defiles the filter as BaseConverter
29156
SdFat sd;
30157
SdFile file;
31158
EncodedAudioStream out(&file, new WAVEncoder()); // encode as wav file
@@ -45,16 +172,16 @@ void setup(){
45172
out.begin();
46173

47174
// setup filters on channel 1 only
48-
in_filtered.setFilter(1, new FIR<int16_t>(coef));
175+
in_filtered.setFilter(1, new FIR<float>(coef));
49176

50177
if (!sd.begin(SS, SPI_HALF_SPEED)) sd.initErrorHalt();
51178
if (!file.open("wave.wav", O_RDWR | O_CREAT)) {
52179
sd.errorHalt("opening wave.wav for write failed");
53180
}
54181

55-
for (int j=0;j<notes.frequencyCount();j++){
182+
for (int j=45;j<notes.frequencyCount();j++){
56183
wave.setFrequency(notes.frequency(j));
57-
for (int i=0;i<20;i++){
184+
for (int i=0;i<10;i++){
58185
copier.copy();
59186
}
60187
}

0 commit comments

Comments
 (0)