![](/media/cache/profiles/altb_JGUSAOG.jpg.50x50_q85.jpg)
corrections altb2
Lib_Misc/IIR_filter.cpp@0:d2e117716219, 2021-05-02 (annotated)
- Committer:
- altb2
- Date:
- Sun May 02 19:32:30 2021 +0000
- Revision:
- 0:d2e117716219
1st commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
altb2 | 0:d2e117716219 | 1 | #include "IIR_filter.h" |
altb2 | 0:d2e117716219 | 2 | #include "mbed.h" |
altb2 | 0:d2e117716219 | 3 | using namespace std; |
altb2 | 0:d2e117716219 | 4 | |
altb2 | 0:d2e117716219 | 5 | /* |
altb2 | 0:d2e117716219 | 6 | IIR filter implemention for the following filter types: |
altb2 | 0:d2e117716219 | 7 | init for: first order differentiatior: G(s) = s/(T*s + 1) |
altb2 | 0:d2e117716219 | 8 | first order lowpass with gain G(s) = K/(T*s + 1) |
altb2 | 0:d2e117716219 | 9 | second order lowpass with gain G(s) = K*w0^2/(s^2 + 2*D*w0*s + w0*w0) |
altb2 | 0:d2e117716219 | 10 | nth order, with arbitrary values |
altb2 | 0:d2e117716219 | 11 | the billinear transformation is used for s -> z |
altb2 | 0:d2e117716219 | 12 | reseting the filter only makes sence for static signals, whatch out if you're using the differnetiator |
altb2 | 0:d2e117716219 | 13 | */ |
altb2 | 0:d2e117716219 | 14 | |
altb2 | 0:d2e117716219 | 15 | // G(s) = s/(T*s + 1) |
altb2 | 0:d2e117716219 | 16 | IIR_filter::IIR_filter(float T, float Ts){ |
altb2 | 0:d2e117716219 | 17 | |
altb2 | 0:d2e117716219 | 18 | // filter orders |
altb2 | 0:d2e117716219 | 19 | nb = 1; // Filter Order |
altb2 | 0:d2e117716219 | 20 | na = 1; // Filter Order |
altb2 | 0:d2e117716219 | 21 | |
altb2 | 0:d2e117716219 | 22 | // filter coefficients |
altb2 | 0:d2e117716219 | 23 | B = (double*)malloc((nb+1)*sizeof(double)); |
altb2 | 0:d2e117716219 | 24 | A = (double*)malloc(na*sizeof(double)); |
altb2 | 0:d2e117716219 | 25 | B[0] = 2.0/(2.0*(double)T + (double)Ts); |
altb2 | 0:d2e117716219 | 26 | B[1] = -B[0]; |
altb2 | 0:d2e117716219 | 27 | A[0] = -(2.0*(double)T - (double)Ts)/(2.0*(double)T + (double)Ts); |
altb2 | 0:d2e117716219 | 28 | |
altb2 | 0:d2e117716219 | 29 | // signal arrays |
altb2 | 0:d2e117716219 | 30 | uk = (double*)malloc((nb+1)*sizeof(double)); |
altb2 | 0:d2e117716219 | 31 | yk = (double*)malloc(na*sizeof(double)); |
altb2 | 0:d2e117716219 | 32 | uk[0]= uk[1] = 0.0; |
altb2 | 0:d2e117716219 | 33 | yk[0] = 0.0; |
altb2 | 0:d2e117716219 | 34 | |
altb2 | 0:d2e117716219 | 35 | // dc-gain |
altb2 | 0:d2e117716219 | 36 | this->K = 0.0; |
altb2 | 0:d2e117716219 | 37 | } |
altb2 | 0:d2e117716219 | 38 | |
altb2 | 0:d2e117716219 | 39 | // G(s) = K/(T*s + 1) |
altb2 | 0:d2e117716219 | 40 | IIR_filter::IIR_filter(float T, float Ts, float K){ |
altb2 | 0:d2e117716219 | 41 | |
altb2 | 0:d2e117716219 | 42 | // filter orders |
altb2 | 0:d2e117716219 | 43 | nb = 1; // Filter Order |
altb2 | 0:d2e117716219 | 44 | na = 1; // Filter Order |
altb2 | 0:d2e117716219 | 45 | |
altb2 | 0:d2e117716219 | 46 | // filter coefficients |
altb2 | 0:d2e117716219 | 47 | B = (double*)malloc((nb+1)*sizeof(double)); |
altb2 | 0:d2e117716219 | 48 | A = (double*)malloc(na*sizeof(double)); |
altb2 | 0:d2e117716219 | 49 | B[0] = (double)Ts/((double)Ts + 2.0*(double)T); |
altb2 | 0:d2e117716219 | 50 | B[1] = B[0]; |
altb2 | 0:d2e117716219 | 51 | A[0] = ((double)Ts - 2.0*(double)T)/((double)Ts + 2.0*(double)T); |
altb2 | 0:d2e117716219 | 52 | |
altb2 | 0:d2e117716219 | 53 | // signal arrays |
altb2 | 0:d2e117716219 | 54 | uk = (double*)malloc((nb+1)*sizeof(double)); |
altb2 | 0:d2e117716219 | 55 | yk = (double*)malloc(na*sizeof(double)); |
altb2 | 0:d2e117716219 | 56 | uk[0]= uk[1] = 0.0; |
altb2 | 0:d2e117716219 | 57 | yk[0] = 0.0; |
altb2 | 0:d2e117716219 | 58 | |
altb2 | 0:d2e117716219 | 59 | // dc-gain |
altb2 | 0:d2e117716219 | 60 | this->K = (double)K; |
altb2 | 0:d2e117716219 | 61 | } |
altb2 | 0:d2e117716219 | 62 | |
altb2 | 0:d2e117716219 | 63 | // G(s) = K*w0^2/(s^2 + 2*D*w0*s + w0^2) |
altb2 | 0:d2e117716219 | 64 | IIR_filter::IIR_filter(float w0, float D, float Ts, float K){ |
altb2 | 0:d2e117716219 | 65 | |
altb2 | 0:d2e117716219 | 66 | // filter orders |
altb2 | 0:d2e117716219 | 67 | nb = 2; // Filter Order |
altb2 | 0:d2e117716219 | 68 | na = 2; // Filter Order |
altb2 | 0:d2e117716219 | 69 | |
altb2 | 0:d2e117716219 | 70 | // filter coefficients |
altb2 | 0:d2e117716219 | 71 | B = (double*)malloc((nb+1)*sizeof(double)); |
altb2 | 0:d2e117716219 | 72 | A = (double*)malloc(na*sizeof(double)); |
altb2 | 0:d2e117716219 | 73 | double k0 = (double)Ts*(double)Ts*(double)w0*(double)w0; |
altb2 | 0:d2e117716219 | 74 | double k1 = 4.0*(double)D*(double)Ts*(double)w0; |
altb2 | 0:d2e117716219 | 75 | double k2 = k0 + k1 + 4.0; |
altb2 | 0:d2e117716219 | 76 | B[0] = (double)K*k0/k2; |
altb2 | 0:d2e117716219 | 77 | B[1] = 2.0*B[0]; |
altb2 | 0:d2e117716219 | 78 | B[2] = B[0]; |
altb2 | 0:d2e117716219 | 79 | A[0] = (2.0*k0 - 8.0)/k2; |
altb2 | 0:d2e117716219 | 80 | A[1] = (k0 - k1 + 4.0)/k2; |
altb2 | 0:d2e117716219 | 81 | |
altb2 | 0:d2e117716219 | 82 | // signal arrays |
altb2 | 0:d2e117716219 | 83 | uk = (double*)malloc((nb+1)*sizeof(double)); |
altb2 | 0:d2e117716219 | 84 | yk = (double*)malloc(na*sizeof(double)); |
altb2 | 0:d2e117716219 | 85 | uk[0]= uk[1] = uk[2] = 0.0; |
altb2 | 0:d2e117716219 | 86 | yk[0] = yk[1] = 0.0; |
altb2 | 0:d2e117716219 | 87 | |
altb2 | 0:d2e117716219 | 88 | // dc-gain |
altb2 | 0:d2e117716219 | 89 | this->K = (double)K; |
altb2 | 0:d2e117716219 | 90 | } |
altb2 | 0:d2e117716219 | 91 | |
altb2 | 0:d2e117716219 | 92 | IIR_filter::IIR_filter(float *b, float *a, int nb_, int na_){ |
altb2 | 0:d2e117716219 | 93 | |
altb2 | 0:d2e117716219 | 94 | // filter orders |
altb2 | 0:d2e117716219 | 95 | this->nb = nb_-1; // Filter Order |
altb2 | 0:d2e117716219 | 96 | this->na = na_; // Filter Order |
altb2 | 0:d2e117716219 | 97 | |
altb2 | 0:d2e117716219 | 98 | // filter coefficients |
altb2 | 0:d2e117716219 | 99 | B = (double*)malloc((nb+1)*sizeof(double)); |
altb2 | 0:d2e117716219 | 100 | A = (double*)malloc(na*sizeof(double)); |
altb2 | 0:d2e117716219 | 101 | uk = (double*)malloc((nb+1)*sizeof(double)); |
altb2 | 0:d2e117716219 | 102 | yk = (double*)malloc(na*sizeof(double)); |
altb2 | 0:d2e117716219 | 103 | |
altb2 | 0:d2e117716219 | 104 | for(int k=0;k<=nb;k++){ |
altb2 | 0:d2e117716219 | 105 | B[k]=b[k]; |
altb2 | 0:d2e117716219 | 106 | uk[k]=0.0; |
altb2 | 0:d2e117716219 | 107 | } |
altb2 | 0:d2e117716219 | 108 | for(int k=0;k<na;k++){ |
altb2 | 0:d2e117716219 | 109 | A[k] = a[k]; |
altb2 | 0:d2e117716219 | 110 | yk[k] = 0.0; |
altb2 | 0:d2e117716219 | 111 | } |
altb2 | 0:d2e117716219 | 112 | |
altb2 | 0:d2e117716219 | 113 | // dc-gain |
altb2 | 0:d2e117716219 | 114 | this->K = 1.0; |
altb2 | 0:d2e117716219 | 115 | } |
altb2 | 0:d2e117716219 | 116 | |
altb2 | 0:d2e117716219 | 117 | |
altb2 | 0:d2e117716219 | 118 | IIR_filter::~IIR_filter() {} |
altb2 | 0:d2e117716219 | 119 | |
altb2 | 0:d2e117716219 | 120 | void IIR_filter::reset(float val) { |
altb2 | 0:d2e117716219 | 121 | for(int k=0;k < nb;k++) |
altb2 | 0:d2e117716219 | 122 | uk[k] = (double)val; |
altb2 | 0:d2e117716219 | 123 | for(int k=0;k < na;k++) |
altb2 | 0:d2e117716219 | 124 | yk[k] = (double)val*K; |
altb2 | 0:d2e117716219 | 125 | |
altb2 | 0:d2e117716219 | 126 | } |
altb2 | 0:d2e117716219 | 127 | |
altb2 | 0:d2e117716219 | 128 | /* |
altb2 | 0:d2e117716219 | 129 | the filter is operating as follows: |
altb2 | 0:d2e117716219 | 130 | (B[0] + B[1]*z^-1 + ... + B[nb]*z^-nb)*U(z) = (1 + A[0]*z^-1 + ... + A[na-1]*z^-na))*Y(z) |
altb2 | 0:d2e117716219 | 131 | y(n) = B[0]*u(k) + B[1]*u(k-1) + ... + B[nb]*u(k-nb) + ... |
altb2 | 0:d2e117716219 | 132 | - A[0]*y(k-1) - A[1]*y(k-2) - ... - A[na]*y(n-na) |
altb2 | 0:d2e117716219 | 133 | */ |
altb2 | 0:d2e117716219 | 134 | float IIR_filter::filter(double input){ |
altb2 | 0:d2e117716219 | 135 | for(int k = nb;k > 0;k--) // shift input values back |
altb2 | 0:d2e117716219 | 136 | uk[k] = uk[k-1]; |
altb2 | 0:d2e117716219 | 137 | uk[0] = input; |
altb2 | 0:d2e117716219 | 138 | double ret = 0.0; |
altb2 | 0:d2e117716219 | 139 | for(int k = 0;k <= nb;k++) |
altb2 | 0:d2e117716219 | 140 | ret += B[k] * uk[k]; |
altb2 | 0:d2e117716219 | 141 | for(int k = 0;k < na;k++) |
altb2 | 0:d2e117716219 | 142 | ret -= A[k] * yk[k]; |
altb2 | 0:d2e117716219 | 143 | for(int k = na;k > 1;k--) |
altb2 | 0:d2e117716219 | 144 | yk[k-1] = yk[k-2]; |
altb2 | 0:d2e117716219 | 145 | yk[0] = ret; |
altb2 | 0:d2e117716219 | 146 | return (float)ret; |
altb2 | 0:d2e117716219 | 147 | } |
altb2 | 0:d2e117716219 | 148 |