Test program with the RT black boxes

Dependencies:   mbed

Committer:
pmic
Date:
Wed Apr 04 14:18:43 2018 +0000
Revision:
10:a28f393c6716
Parent:
5:d6c7ccbbce78
Child:
12:6287235b2570
introduce updated classes, e.g. double prescision integral controler in biquad formulation

Who changed what in which revision?

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