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