Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
Loading...
Searching...
No Matches
IirFilter.h
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
14class 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)
16private:
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
19public:
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
57class IirFilter{
58private:
59 IirFilterStage** stages = 0;
60 int numberOfStages = 0;
61 void dealloc();
62public:
63 IirFilter();
64 IirFilter(int newNumberOfStages);
65 IirFilter(int newNumberOfStages, double *newCoefficients);
66 void setCoefficients(double* newCoefficients);
67 void setCoefficients(double* newCoefficients, unsigned int stage);
68 void setStates(double* newStates);
69 void setStates(double* newStates, unsigned int stage);
70 void setNumberOfStages(int newNumberOfStages);
71// double process(double in);
72// inline void process(double* inout, int length);
73// inline void process(double* in, double* out, int length);
74 double process(double in){
75 process(&in, 1);
76 return in;
77 };
78 void process(double* inout, int length){
79 for(int n = 0; n < numberOfStages && n < 8/* TODO: this "8" compensates for a memory corruption somewhere on the BBB*/; n++){
80 stages[n]->process(inout, length);
81 }
82 }
83};
84
85
86//void IirFilter::process(double* in, double* out, int length);
87
88#endif /* IIRFILTER_H_ */
Definition IirFilter.h:14