Test program with the RT black boxes

Dependencies:   mbed

Committer:
pmic
Date:
Thu Apr 05 10:07:29 2018 +0000
Revision:
13:a308f5e6c306
Parent:
12:6287235b2570
implement swing down logic (it is a bit a mess, but it is working)

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 13:a308f5e6c306 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 12:6287235b2570 23 B = (double*)malloc((nb+1)*sizeof(double));
pmic 12:6287235b2570 24 A = (double*)malloc(na*sizeof(double));
pmic 12:6287235b2570 25 B[0] = 2.0/(2.0*(double)T + (double)Ts);
pmic 5:d6c7ccbbce78 26 B[1] = -B[0];
pmic 12:6287235b2570 27 A[0] = -(2.0*(double)T - (double)Ts)/(2.0*(double)T + (double)Ts);
pmic 5:d6c7ccbbce78 28
pmic 5:d6c7ccbbce78 29 // signal arrays
pmic 12:6287235b2570 30 uk = (double*)malloc((nb+1)*sizeof(double));
pmic 12:6287235b2570 31 yk = (double*)malloc(na*sizeof(double));
pmic 12:6287235b2570 32 uk[0]= uk[1] = 0.0;
pmic 12:6287235b2570 33 yk[0] = 0.0;
pmic 5:d6c7ccbbce78 34
pmic 5:d6c7ccbbce78 35 // dc-gain
pmic 12:6287235b2570 36 this->K = 0.0;
pmic 5:d6c7ccbbce78 37 }
pmic 5:d6c7ccbbce78 38
pmic 5:d6c7ccbbce78 39 // G(s) = K/(T*s + 1)
pmic 13:a308f5e6c306 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 12:6287235b2570 47 B = (double*)malloc((nb+1)*sizeof(double));
pmic 12:6287235b2570 48 A = (double*)malloc(na*sizeof(double));
pmic 12:6287235b2570 49 B[0] = (double)Ts/((double)Ts + 2.0*(double)T);
pmic 5:d6c7ccbbce78 50 B[1] = B[0];
pmic 12:6287235b2570 51 A[0] = ((double)Ts - 2.0*(double)T)/((double)Ts + 2.0*(double)T);
pmic 5:d6c7ccbbce78 52
pmic 5:d6c7ccbbce78 53 // signal arrays
pmic 12:6287235b2570 54 uk = (double*)malloc((nb+1)*sizeof(double));
pmic 12:6287235b2570 55 yk = (double*)malloc(na*sizeof(double));
pmic 12:6287235b2570 56 uk[0]= uk[1] = 0.0;
pmic 12:6287235b2570 57 yk[0] = 0.0;
pmic 5:d6c7ccbbce78 58
pmic 5:d6c7ccbbce78 59 // dc-gain
pmic 12:6287235b2570 60 this->K = (double)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 13:a308f5e6c306 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 12:6287235b2570 71 B = (double*)malloc((nb+1)*sizeof(double));
pmic 12:6287235b2570 72 A = (double*)malloc(na*sizeof(double));
pmic 12:6287235b2570 73 double k0 = (double)Ts*(double)Ts*(double)w0*(double)w0;
pmic 12:6287235b2570 74 double k1 = 4.0*(double)D*(double)Ts*(double)w0;
pmic 12:6287235b2570 75 double k2 = k0 + k1 + 4.0;
pmic 12:6287235b2570 76 B[0] = (double)K*k0/k2;
pmic 12:6287235b2570 77 B[1] = 2.0*B[0];
pmic 5:d6c7ccbbce78 78 B[2] = B[0];
pmic 12:6287235b2570 79 A[0] = (2.0*k0 - 8.0)/k2;
pmic 12:6287235b2570 80 A[1] = (k0 - k1 + 4.0)/k2;
pmic 5:d6c7ccbbce78 81
pmic 5:d6c7ccbbce78 82 // signal arrays
pmic 12:6287235b2570 83 uk = (double*)malloc((nb+1)*sizeof(double));
pmic 12:6287235b2570 84 yk = (double*)malloc(na*sizeof(double));
pmic 12:6287235b2570 85 uk[0]= uk[1] = uk[2] = 0.0;
pmic 12:6287235b2570 86 yk[0] = yk[1] = 0.0;
pmic 5:d6c7ccbbce78 87
pmic 5:d6c7ccbbce78 88 // dc-gain
pmic 12:6287235b2570 89 this->K = (double)K;
pmic 5:d6c7ccbbce78 90 }
pmic 10:a28f393c6716 91
pmic 13:a308f5e6c306 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 12:6287235b2570 99 B = (double*)malloc((nb+1)*sizeof(double));
pmic 12:6287235b2570 100 A = (double*)malloc(na*sizeof(double));
pmic 12:6287235b2570 101 uk = (double*)malloc((nb+1)*sizeof(double));
pmic 12:6287235b2570 102 yk = (double*)malloc(na*sizeof(double));
pmic 10:a28f393c6716 103
pmic 10:a28f393c6716 104 for(int k=0;k<=nb;k++){
pmic 10:a28f393c6716 105 B[k]=b[k];
pmic 12:6287235b2570 106 uk[k]=0.0;
pmic 10:a28f393c6716 107 }
pmic 10:a28f393c6716 108 for(int k=0;k<na;k++){
pmic 10:a28f393c6716 109 A[k] = a[k];
pmic 12:6287235b2570 110 yk[k] = 0.0;
pmic 10:a28f393c6716 111 }
pmic 10:a28f393c6716 112
pmic 10:a28f393c6716 113 // dc-gain
pmic 12:6287235b2570 114 this->K = 1.0;
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++)
pmic 12:6287235b2570 122 uk[k] = (double)val;
pmic 5:d6c7ccbbce78 123 for(int k=0;k < na;k++)
pmic 12:6287235b2570 124 yk[k] = (double)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 */
pmic 12:6287235b2570 134 float IIR_filter::filter(double 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;
pmic 12:6287235b2570 138 double ret = 0.0;
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;
pmic 12:6287235b2570 146 return (float)ret;
pmic 5:d6c7ccbbce78 147 }