giuliomoro@233
|
1 /*
|
giuliomoro@233
|
2 * IirFilter.h
|
giuliomoro@233
|
3 *
|
giuliomoro@233
|
4 * Created on: 17 Sep 2015
|
giuliomoro@233
|
5 * Author: giulio
|
giuliomoro@233
|
6 */
|
giuliomoro@233
|
7
|
giuliomoro@233
|
8 #ifndef IIRFILTER_H_
|
giuliomoro@233
|
9 #define IIRFILTER_H_
|
giuliomoro@233
|
10
|
giuliomoro@233
|
11 #define IIR_FILTER_STAGE_COEFFICIENTS (5)
|
giuliomoro@233
|
12 #define IIR_FILTER_STAGE_STATES (IIR_FILTER_STAGE_COEFFICIENTS - 1)
|
giuliomoro@233
|
13
|
giuliomoro@233
|
14 class IirFilterStage{ //TODO : to save (some) memory we should only store the coefficients pointers here,
|
giuliomoro@233
|
15 //so that IirFilter can share them among multiple stages if needbe)
|
giuliomoro@233
|
16 private:
|
giuliomoro@233
|
17 double coefficients[IIR_FILTER_STAGE_COEFFICIENTS]; // these are b0,b1,b2,a1,a2
|
giuliomoro@233
|
18 double states[IIR_FILTER_STAGE_STATES]; // these are xprev, xprevprev, yprev, yprevprev
|
giuliomoro@233
|
19 public:
|
giuliomoro@233
|
20 IirFilterStage();
|
giuliomoro@233
|
21 void setCoefficients(double* newCoefficients);
|
giuliomoro@233
|
22 void setStates(double* newStates);
|
giuliomoro@233
|
23 // this is not meant to be efficient, just of practical use!
|
giuliomoro@233
|
24 double process(double in){
|
giuliomoro@233
|
25 process(&in, 1);
|
giuliomoro@233
|
26 return in;
|
giuliomoro@233
|
27 }
|
giuliomoro@233
|
28
|
giuliomoro@233
|
29 void process(double* inout, int length){
|
giuliomoro@233
|
30 // make variables explicit. A smart compiler will optimize these out, right?
|
giuliomoro@233
|
31 double b0=coefficients[0];
|
giuliomoro@233
|
32 double b1=coefficients[1];
|
giuliomoro@233
|
33 double b2=coefficients[2];
|
giuliomoro@233
|
34 double a1=coefficients[3];
|
giuliomoro@233
|
35 double a2=coefficients[4];
|
giuliomoro@233
|
36 double x1 = states[0];
|
giuliomoro@233
|
37 double x2= states[1];
|
giuliomoro@233
|
38 double y1 = states[2];
|
giuliomoro@233
|
39 double y2 = states[3];
|
giuliomoro@233
|
40 for(int n = 0; n < length; n++){
|
giuliomoro@233
|
41 double x0 = inout[n];
|
giuliomoro@233
|
42 double y = x0 * b0 + x1 * b1 + x2 * b2 +
|
giuliomoro@233
|
43 - y1 * a1 - y2 * a2;
|
giuliomoro@233
|
44 inout[n] = y;
|
giuliomoro@233
|
45 x2 = x1;
|
giuliomoro@233
|
46 x1 = x0;
|
giuliomoro@233
|
47 y2 = y1;
|
giuliomoro@233
|
48 y1 = y;
|
giuliomoro@233
|
49 }
|
giuliomoro@233
|
50 states[0] = x1;
|
giuliomoro@233
|
51 states[1] = x2;
|
giuliomoro@233
|
52 states[2] = y1;
|
giuliomoro@233
|
53 states[3] = y2;
|
giuliomoro@233
|
54 }
|
giuliomoro@233
|
55 };
|
giuliomoro@233
|
56
|
giuliomoro@233
|
57 class IirFilter{
|
giuliomoro@233
|
58 private:
|
giuliomoro@233
|
59 struct IirFilterStage** stages;
|
giuliomoro@233
|
60 int numberOfStages;
|
giuliomoro@233
|
61 void dealloc();
|
giuliomoro@233
|
62 public:
|
giuliomoro@233
|
63 IirFilter();
|
giuliomoro@233
|
64 IirFilter(int newNumberOfStages);
|
giuliomoro@233
|
65 IirFilter(int newNumberOfStages, double *newCoefficients);
|
giuliomoro@233
|
66 void setCoefficients(double* newCoefficients);
|
giuliomoro@233
|
67 void setStates(double* newStates);
|
giuliomoro@233
|
68 void setNumberOfStages(int newNumberOfStages);
|
giuliomoro@233
|
69 // double process(double in);
|
giuliomoro@233
|
70 // inline void process(double* inout, int length);
|
giuliomoro@233
|
71 // inline void process(double* in, double* out, int length);
|
giuliomoro@233
|
72 double process(double in){
|
giuliomoro@233
|
73 process(&in, 1);
|
giuliomoro@233
|
74 return in;
|
giuliomoro@233
|
75 };
|
giuliomoro@233
|
76 void process(double* inout, int length){
|
giuliomoro@233
|
77 for(int n = 0; n < numberOfStages && n < 8/* TODO: this "8" compensates for a memory corruption somewhere on the BBB*/; n++){
|
giuliomoro@233
|
78 stages[n]->process(inout, length);
|
giuliomoro@233
|
79 }
|
giuliomoro@233
|
80 }
|
giuliomoro@233
|
81 };
|
giuliomoro@233
|
82
|
giuliomoro@233
|
83
|
giuliomoro@233
|
84 //void IirFilter::process(double* in, double* out, int length);
|
giuliomoro@233
|
85
|
giuliomoro@233
|
86 #endif /* IIRFILTER_H_ */
|