Non-variable (TESTEDandWORKING)

Dependencies:   mbed-rtos mbed

Fork of ELEC347_Coursework by CMST

Committer:
mwthewsey
Date:
Thu Nov 23 10:41:18 2017 +0000
Revision:
0:d39a06ca6bf1
Child:
1:b088b771a591
Initial publish. (not working);

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mwthewsey 0:d39a06ca6bf1 1 #include "Filter.hpp"
mwthewsey 0:d39a06ca6bf1 2
mwthewsey 0:d39a06ca6bf1 3 FILTER::FILTER(int Fs, int Fo, int Boost, int Q) //Constuctor
mwthewsey 0:d39a06ca6bf1 4 {
mwthewsey 0:d39a06ca6bf1 5 _Fs = Fs;
mwthewsey 0:d39a06ca6bf1 6 this-> _Fo = Fo;
mwthewsey 0:d39a06ca6bf1 7 this-> _Boost = Boost;
mwthewsey 0:d39a06ca6bf1 8 this-> _Q = Q;
mwthewsey 0:d39a06ca6bf1 9
mwthewsey 0:d39a06ca6bf1 10 // B - Upnominator coefficients
mwthewsey 0:d39a06ca6bf1 11 _b0 = 0.0201;
mwthewsey 0:d39a06ca6bf1 12 _b1 = 0.0;
mwthewsey 0:d39a06ca6bf1 13 _b2 = -0.0402;
mwthewsey 0:d39a06ca6bf1 14 _b3 = 0.0;
mwthewsey 0:d39a06ca6bf1 15 _b4 = 0.0201;
mwthewsey 0:d39a06ca6bf1 16
mwthewsey 0:d39a06ca6bf1 17 // A - Denominator coefficients
mwthewsey 0:d39a06ca6bf1 18 _a0 = 1.0000;
mwthewsey 0:d39a06ca6bf1 19 _a1 = -2.5494;
mwthewsey 0:d39a06ca6bf1 20 _a2 = 3.2024;
mwthewsey 0:d39a06ca6bf1 21 _a3 = -2.0359;
mwthewsey 0:d39a06ca6bf1 22 _a4 = 0.6414;
mwthewsey 0:d39a06ca6bf1 23
mwthewsey 0:d39a06ca6bf1 24 _centreTap = 1;
mwthewsey 0:d39a06ca6bf1 25 _G = 0;
mwthewsey 0:d39a06ca6bf1 26 _k = 0;
mwthewsey 0:d39a06ca6bf1 27 _Wo = 0;
mwthewsey 0:d39a06ca6bf1 28
mwthewsey 0:d39a06ca6bf1 29 _xn = 0; //this value is the input value
mwthewsey 0:d39a06ca6bf1 30 _xnm1 = 0;
mwthewsey 0:d39a06ca6bf1 31 _xnm2 = 0;
mwthewsey 0:d39a06ca6bf1 32 _xnm3 = 0;
mwthewsey 0:d39a06ca6bf1 33 _xnm4 = 0;
mwthewsey 0:d39a06ca6bf1 34
mwthewsey 0:d39a06ca6bf1 35 _yn = 0;
mwthewsey 0:d39a06ca6bf1 36 _ynm1 = 0;
mwthewsey 0:d39a06ca6bf1 37 _ynm2 = 0;
mwthewsey 0:d39a06ca6bf1 38 _ynm3 = 0;
mwthewsey 0:d39a06ca6bf1 39 _ynm4 = 0;
mwthewsey 0:d39a06ca6bf1 40 }
mwthewsey 0:d39a06ca6bf1 41
mwthewsey 0:d39a06ca6bf1 42 FILTER::~FILTER() {}
mwthewsey 0:d39a06ca6bf1 43
mwthewsey 0:d39a06ca6bf1 44 void FILTER::setvalue(int RAW_input)
mwthewsey 0:d39a06ca6bf1 45 {
mwthewsey 0:d39a06ca6bf1 46
mwthewsey 0:d39a06ca6bf1 47 _xn = RAW_input;
mwthewsey 0:d39a06ca6bf1 48 _centreTap = _xn*_b0 + _xnm1*_b1 + _xnm2*_b2 + _xnm3*_b3 + _xnm4*_b4; //IIR Filter
mwthewsey 0:d39a06ca6bf1 49 _yn = _centreTap*_a0 - _a1*_ynm1 - _a2*_ynm2 - _a3*_ynm3 - _a4*_ynm4; //Result in yn
mwthewsey 0:d39a06ca6bf1 50
mwthewsey 0:d39a06ca6bf1 51 FilterOutput=_yn+0.5f; //Output resultant to DAC. Again MBED uses 0.0 to 1.0 float!!!!!! and Offset to give 0 to 3V3 range
mwthewsey 0:d39a06ca6bf1 52
mwthewsey 0:d39a06ca6bf1 53 //THESE NEED TO BE LOADED IN THIS ORDER OTHERWISE ALL xnm VALUES WILL BECOME THE SAME AS xn
mwthewsey 0:d39a06ca6bf1 54 _xnm4 = _xnm3;
mwthewsey 0:d39a06ca6bf1 55 _xnm3 = _xnm2;
mwthewsey 0:d39a06ca6bf1 56 _xnm2 = _xnm1;
mwthewsey 0:d39a06ca6bf1 57 _xnm1 = _xn;
mwthewsey 0:d39a06ca6bf1 58
mwthewsey 0:d39a06ca6bf1 59 //THESE NEED TO BE LOADED IN THIS ORDER OTHERWISE ALL ynm VALUES WILL BECOME THE SAME AS yn
mwthewsey 0:d39a06ca6bf1 60 _ynm4 = _ynm3;
mwthewsey 0:d39a06ca6bf1 61 _ynm3 = _ynm2;
mwthewsey 0:d39a06ca6bf1 62 _ynm2 = _ynm1;
mwthewsey 0:d39a06ca6bf1 63 _ynm1 = _yn;
mwthewsey 0:d39a06ca6bf1 64
mwthewsey 0:d39a06ca6bf1 65 }
mwthewsey 0:d39a06ca6bf1 66
mwthewsey 0:d39a06ca6bf1 67 float FILTER::getvalue(void)
mwthewsey 0:d39a06ca6bf1 68 {
mwthewsey 0:d39a06ca6bf1 69 return FilterOutput;
mwthewsey 0:d39a06ca6bf1 70 }
mwthewsey 0:d39a06ca6bf1 71