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