V001. 2017_11_30 10:21 Working code from Tuesday's lab session.
Fork of 2017_11_28_ELEC347_Coursework by
DSP Coursework ELEC347 2017-2018 Group members: Matthew Thewsey, Thomas Morris, Samuel Waggett, Christopher Hills .
Filter.cpp@11:7efb6e4759cc, 2017-12-02 (annotated)
- Committer:
- thomasmorris
- Date:
- Sat Dec 02 13:35:40 2017 +0000
- Revision:
- 11:7efb6e4759cc
- Parent:
- 8:192b376a6da7
- Child:
- 13:db76473a3d76
Commented Version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chills | 6:a2737b51424c | 1 | #include "mbed.h" |
mwthewsey | 0:d39a06ca6bf1 | 2 | #include "Filter.hpp" |
mwthewsey | 2:f6e49378dd8a | 3 | #include <math.h> |
mwthewsey | 0:d39a06ca6bf1 | 4 | |
chills | 6:a2737b51424c | 5 | |
mwthewsey | 2:f6e49378dd8a | 6 | #define PI 3.14159 |
mwthewsey | 2:f6e49378dd8a | 7 | |
mwthewsey | 2:f6e49378dd8a | 8 | FILTER::FILTER(int Fs, int Fo, double Boost, int Q) //Constuctor |
mwthewsey | 0:d39a06ca6bf1 | 9 | { |
thomasmorris | 11:7efb6e4759cc | 10 | //Storing Paremeters into the Class |
mwthewsey | 2:f6e49378dd8a | 11 | this-> _Fs = Fs; |
mwthewsey | 0:d39a06ca6bf1 | 12 | this-> _Fo = Fo; |
mwthewsey | 0:d39a06ca6bf1 | 13 | this-> _Boost = Boost; |
mwthewsey | 0:d39a06ca6bf1 | 14 | this-> _Q = Q; |
thomasmorris | 11:7efb6e4759cc | 15 | |
thomasmorris | 11:7efb6e4759cc | 16 | //Initializing Variables |
thomasmorris | 11:7efb6e4759cc | 17 | |
mwthewsey | 0:d39a06ca6bf1 | 18 | _centreTap = 1; |
mwthewsey | 0:d39a06ca6bf1 | 19 | _G = 0; |
mwthewsey | 0:d39a06ca6bf1 | 20 | _k = 0; |
mwthewsey | 0:d39a06ca6bf1 | 21 | _Wo = 0; |
mwthewsey | 2:f6e49378dd8a | 22 | _Wo_d = 0; |
mwthewsey | 2:f6e49378dd8a | 23 | _Wo_d2 = 0; |
mwthewsey | 2:f6e49378dd8a | 24 | _Com_Den = 0; |
mwthewsey | 2:f6e49378dd8a | 25 | _NC0 = 0; |
mwthewsey | 2:f6e49378dd8a | 26 | _NC1 = 0; |
mwthewsey | 2:f6e49378dd8a | 27 | _NC2 = 0; |
mwthewsey | 2:f6e49378dd8a | 28 | _DC1 = 0; |
mwthewsey | 2:f6e49378dd8a | 29 | _DC2 = 0; |
mwthewsey | 0:d39a06ca6bf1 | 30 | |
mwthewsey | 2:f6e49378dd8a | 31 | _xn = 0; //This value is the input value |
mwthewsey | 0:d39a06ca6bf1 | 32 | _xnm1 = 0; |
mwthewsey | 0:d39a06ca6bf1 | 33 | _xnm2 = 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 | } |
mwthewsey | 0:d39a06ca6bf1 | 39 | |
thomasmorris | 11:7efb6e4759cc | 40 | FILTER::~FILTER()//Destructor |
thomasmorris | 11:7efb6e4759cc | 41 | { |
thomasmorris | 11:7efb6e4759cc | 42 | |
thomasmorris | 11:7efb6e4759cc | 43 | } |
mwthewsey | 0:d39a06ca6bf1 | 44 | |
thomasmorris | 11:7efb6e4759cc | 45 | void FILTER::setvalue(float RAW_input)//Sets the raw input value to the class |
thomasmorris | 11:7efb6e4759cc | 46 | { |
mwthewsey | 0:d39a06ca6bf1 | 47 | _xn = RAW_input; |
mwthewsey | 2:f6e49378dd8a | 48 | _centreTap = _xn*_b0 + _xnm1*_b1 + _xnm2*_b2; //IIR Filter |
mwthewsey | 2:f6e49378dd8a | 49 | _yn = _centreTap*_a0 - _a1*_ynm1 - _a2*_ynm2; //Result in yn |
mwthewsey | 0:d39a06ca6bf1 | 50 | |
mwthewsey | 2:f6e49378dd8a | 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 | _xnm2 = _xnm1; |
mwthewsey | 0:d39a06ca6bf1 | 55 | _xnm1 = _xn; |
mwthewsey | 0:d39a06ca6bf1 | 56 | |
mwthewsey | 0:d39a06ca6bf1 | 57 | //THESE NEED TO BE LOADED IN THIS ORDER OTHERWISE ALL ynm VALUES WILL BECOME THE SAME AS yn |
mwthewsey | 0:d39a06ca6bf1 | 58 | _ynm2 = _ynm1; |
mwthewsey | 0:d39a06ca6bf1 | 59 | _ynm1 = _yn; |
mwthewsey | 0:d39a06ca6bf1 | 60 | } |
mwthewsey | 0:d39a06ca6bf1 | 61 | |
thomasmorris | 11:7efb6e4759cc | 62 | float FILTER::getvalue(void)//Returns the current filter output value |
mwthewsey | 0:d39a06ca6bf1 | 63 | { |
mwthewsey | 2:f6e49378dd8a | 64 | return _FilterOutput; |
mwthewsey | 0:d39a06ca6bf1 | 65 | } |
mwthewsey | 0:d39a06ca6bf1 | 66 | |
mwthewsey | 2:f6e49378dd8a | 67 | void FILTER::Define_Filter()//Define the filter coefficients |
mwthewsey | 2:f6e49378dd8a | 68 | { |
thomasmorris | 11:7efb6e4759cc | 69 | //Calculate the values for the equation parameters |
mwthewsey | 2:f6e49378dd8a | 70 | _G = pow(10,_Boost/20.0); |
mwthewsey | 2:f6e49378dd8a | 71 | _k = 3 * ((_G - 1) / (_G+1)); |
mwthewsey | 2:f6e49378dd8a | 72 | _Wo = 2 * PI * _Fo; |
mwthewsey | 2:f6e49378dd8a | 73 | _Wo_d = tan(_Wo / (2*_Fs)); |
thomasmorris | 11:7efb6e4759cc | 74 | _Wo_d2 = (_Wo_d * _Wo_d);//Omega-Dash-Naught-Squared |
mwthewsey | 2:f6e49378dd8a | 75 | _Com_Den = 1 + ((3-_k)*_Wo_d/_Q) + _Wo_d2; |
mwthewsey | 2:f6e49378dd8a | 76 | _NC0 = 1 + ((3+_k)*_Wo_d/_Q) + _Wo_d2; |
mwthewsey | 2:f6e49378dd8a | 77 | _NC1 = -2 + (2 * _Wo_d2); |
mwthewsey | 2:f6e49378dd8a | 78 | _NC2 = 1 - ((3+_k)*_Wo_d/_Q) + _Wo_d2; |
mwthewsey | 2:f6e49378dd8a | 79 | _DC1 = _NC1; |
mwthewsey | 2:f6e49378dd8a | 80 | _DC2 = 1 - ((3-_k)*_Wo_d/_Q) + _Wo_d2; |
mwthewsey | 2:f6e49378dd8a | 81 | |
mwthewsey | 2:f6e49378dd8a | 82 | //Calculate Coefficients |
mwthewsey | 2:f6e49378dd8a | 83 | |
mwthewsey | 2:f6e49378dd8a | 84 | _b0 = _NC0 / _Com_Den; |
mwthewsey | 2:f6e49378dd8a | 85 | _b1 = _NC1 / _Com_Den; |
mwthewsey | 2:f6e49378dd8a | 86 | _b2 = _NC2 / _Com_Den; |
mwthewsey | 2:f6e49378dd8a | 87 | _a0 = 1.0000; |
mwthewsey | 2:f6e49378dd8a | 88 | _a1 = _DC1 / _Com_Den; |
mwthewsey | 2:f6e49378dd8a | 89 | _a2 = _DC2 / _Com_Den; |
mwthewsey | 2:f6e49378dd8a | 90 | } |
mwthewsey | 2:f6e49378dd8a | 91 | |
thomasmorris | 11:7efb6e4759cc | 92 | void FILTER::Print_Filter()//Print Filter Coefficients |
chills | 6:a2737b51424c | 93 | { |
chills | 8:192b376a6da7 | 94 | printf("Num 1 = %f\t", _b0); |
chills | 8:192b376a6da7 | 95 | printf("Num 2 = %f\t", _b1); |
chills | 8:192b376a6da7 | 96 | printf("Num 3 = %f\t", _b2); |
chills | 8:192b376a6da7 | 97 | printf("Den 1 = %f\t", _a0); |
chills | 8:192b376a6da7 | 98 | printf("Den 2 = %f\t", _a1); |
chills | 6:a2737b51424c | 99 | printf("Den 3 = %f\n\r", _a2); |
chills | 7:6cb27cce4c50 | 100 | } |
chills | 7:6cb27cce4c50 | 101 | |
chills | 7:6cb27cce4c50 | 102 | void FILTER::Update_Fo(int Fo_New){_Fo = Fo_New;} |
chills | 7:6cb27cce4c50 | 103 | int FILTER::Get_Fo(){return _Fo;} |
chills | 7:6cb27cce4c50 | 104 | |
chills | 7:6cb27cce4c50 | 105 | void FILTER::Update_Q(int Q_New){_Q = Q_New;} |
chills | 7:6cb27cce4c50 | 106 | int FILTER::Get_Q(){return _Q;} |
chills | 7:6cb27cce4c50 | 107 | |
chills | 7:6cb27cce4c50 | 108 | void FILTER::Update_Boost(double Boost_New){_Boost = Boost_New;} |
chills | 7:6cb27cce4c50 | 109 | double FILTER::Get_Boost(){return _Boost;} |