Skip to content

Commit c7817ec

Browse files
committed
Added and tested IIR filter design class. TODO: make FIR class a child class.
1 parent e83e335 commit c7817ec

File tree

1 file changed

+80
-4
lines changed

1 file changed

+80
-4
lines changed

arduino/stdafx.h

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,17 @@ class Signals {
173173
/*
174174
175175
FIR Filter Design on Arduino.
176+
177+
To use it, first create the filter with the desired coefficients:
176178
177-
*/
179+
const float h[] = {0.25, 0.25, 0.25, 0.25};
180+
Signals::FIR f(h, sizeof(h)/sizeof(*h));
181+
182+
Now filter the current ref value!
178183
184+
float fref = f.filter(ref);
185+
186+
*/
179187
class FIR
180188
{
181189
float * h;
@@ -184,7 +192,7 @@ class FIR
184192
unsigned int iteration;
185193

186194
public:
187-
FIR(float * h, const unsigned int size)
195+
FIR(const float * h, const unsigned int size)
188196
{
189197
this->h = new float[size];
190198
memcpy(this->h, h, size*sizeof(float));
@@ -195,7 +203,13 @@ class FIR
195203
this->x[i] = 0.0;
196204
}
197205

198-
float filter(float value)
206+
~FIR()
207+
{
208+
delete this->h;
209+
delete this->x;
210+
}
211+
212+
float filter(const float value)
199213
{
200214
float ret = 0;
201215
x[iteration] = value;
@@ -208,6 +222,68 @@ class FIR
208222
}
209223
};
210224

225+
/*
226+
227+
IIR Filter Design on Arduino.
228+
229+
*/
230+
class IIR
231+
{
232+
private:
233+
float * a, * b;
234+
float * x, * y;
235+
unsigned int size_a, size_b;
236+
unsigned int iteration_a, iteration_b;
237+
238+
public:
239+
IIR(const float * b, const unsigned int size_b, const float * a, const unsigned int size_a)
240+
{
241+
// b coefficients
242+
this->size_b = size_b;
243+
this->b = new float[size_b];
244+
memcpy(this->b, b, size_b*sizeof(float));
245+
246+
this->x = new float[size_b];
247+
248+
for (unsigned int i = 0; i < size_b; i++)
249+
this->x[i] = 0.0;
250+
251+
// a coefficients
252+
this->size_a = size_a;
253+
this->a = new float[size_a];
254+
memcpy(this->a, a, size_a*sizeof(float));
255+
256+
this->y = new float[size_a];
257+
258+
for (unsigned int i = 0; i < size_a; i++)
259+
this->y[i] = 0.0;
260+
}
261+
262+
~IIR()
263+
{
264+
delete this->a, this->b;
265+
delete this->x, this->y;
266+
}
267+
268+
float filter(const float value)
269+
{
270+
float ret = 0;
271+
x[iteration_b] = value;
272+
273+
274+
for (int i=0; i < size_b; i++)
275+
ret += b[i] * x[(i + iteration_b) % size_b];
276+
277+
for (int i=0; i < size_a; i++)
278+
ret -= a[i] * y[(i + iteration_a) % size_a];
279+
280+
iteration_b = (iteration_b + 1) % size_b;
281+
iteration_a = (iteration_a + 1) % size_a;
282+
y[iteration_a] = ret;
283+
return ret;
284+
}
285+
};
286+
211287
/*
212288
213289
A simple PI controller for Arduino based on discrete controller.
@@ -304,7 +380,7 @@ class cPI
304380
zf = _zf;
305381
}
306382

307-
int available (unsigned long ts) { return tlast - millis() > ts ? 1 : 0; }
383+
bool available (unsigned long ts) { return tlast - millis() > ts; }
308384
float getFilteredReference () { return kf*ref - zf*refp; }
309385
float getError () { return e; }
310386
float getReference () { return ref; }

0 commit comments

Comments
 (0)