@@ -173,9 +173,17 @@ class Signals {
173
173
/*
174
174
175
175
FIR Filter Design on Arduino.
176
+
177
+ To use it, first create the filter with the desired coefficients:
176
178
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!
178
183
184
+ float fref = f.filter(ref);
185
+
186
+ */
179
187
class FIR
180
188
{
181
189
float * h;
@@ -184,7 +192,7 @@ class FIR
184
192
unsigned int iteration;
185
193
186
194
public:
187
- FIR (float * h, const unsigned int size)
195
+ FIR (const float * h, const unsigned int size)
188
196
{
189
197
this ->h = new float [size];
190
198
memcpy (this ->h , h, size*sizeof (float ));
@@ -195,7 +203,13 @@ class FIR
195
203
this ->x [i] = 0.0 ;
196
204
}
197
205
198
- float filter (float value)
206
+ ~FIR ()
207
+ {
208
+ delete this ->h ;
209
+ delete this ->x ;
210
+ }
211
+
212
+ float filter (const float value)
199
213
{
200
214
float ret = 0 ;
201
215
x[iteration] = value;
@@ -208,6 +222,68 @@ class FIR
208
222
}
209
223
};
210
224
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
+
211
287
/*
212
288
213
289
A simple PI controller for Arduino based on discrete controller.
@@ -304,7 +380,7 @@ class cPI
304
380
zf = _zf;
305
381
}
306
382
307
- int available (unsigned long ts) { return tlast - millis () > ts ? 1 : 0 ; }
383
+ bool available (unsigned long ts) { return tlast - millis () > ts; }
308
384
float getFilteredReference () { return kf*ref - zf*refp; }
309
385
float getError () { return e; }
310
386
float getReference () { return ref; }
0 commit comments