Matthew Thewsey / Mbed 2 deprecated ELEC347_CourseWork

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
mwthewsey
Date:
Tue Nov 28 09:51:30 2017 +0000
Parent:
1:b088b771a591
Commit message:
Working Working version;

Changed in this revision

Filter.cpp Show annotated file Show diff for this revision Revisions of this file
Filter.hpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Filter.cpp	Tue Nov 28 09:41:07 2017 +0000
+++ b/Filter.cpp	Tue Nov 28 09:51:30 2017 +0000
@@ -1,64 +1,54 @@
 #include "Filter.hpp"
+#include <math.h>
 
-FILTER::FILTER(int Fs, int Fo, int Boost, int Q) //Constuctor
+#define PI 3.14159
+
+FILTER::FILTER(int Fs, int Fo, double Boost, int Q) //Constuctor
 {
-    _Fs = Fs;
+    this-> _Fs = Fs;
     this-> _Fo = Fo;
     this-> _Boost = Boost;
     this-> _Q = Q;
 
-    // B - Upnominator coefficients
-    _b0 = 0.0201;
-    _b1 = 0.0;
-    _b2 = -0.0402;
-    _b3 = 0.0;
-    _b4 = 0.0201;
-
-    // A - Denominator coefficients
-    _a0 = 1.0000;
-    _a1 = -2.5494;
-    _a2 = 3.2024;
-    _a3 = -2.0359;
-    _a4 = 0.6414;
 
     _centreTap = 1;
     _G = 0;
     _k = 0;
     _Wo = 0;
+    _Wo_d = 0;
+    _Wo_d2 = 0;
+    _Com_Den = 0;
+    _NC0 = 0;
+    _NC1 = 0;
+    _NC2 = 0;
+    _DC1 = 0;
+    _DC2 = 0;
 
-    _xn = 0;     //this value is the input value
+    _xn = 0;     //This value is the input value
     _xnm1 = 0;
     _xnm2 = 0;
-    _xnm3 = 0;
-    _xnm4 = 0;
 
     _yn = 0;
     _ynm1 = 0;
     _ynm2 = 0;
-    _ynm3 = 0;
-    _ynm4 = 0;
 }
 
 FILTER::~FILTER() {}
 
-void FILTER::setvalue(int RAW_input)
+void FILTER::setvalue(float RAW_input)
 {
             
     _xn = RAW_input;
-    _centreTap = _xn*_b0 + _xnm1*_b1 + _xnm2*_b2 + _xnm3*_b3 + _xnm4*_b4;  //IIR Filter
-    _yn = _centreTap*_a0 - _a1*_ynm1 - _a2*_ynm2  - _a3*_ynm3 - _a4*_ynm4; //Result in yn
+    _centreTap = _xn*_b0 + _xnm1*_b1 + _xnm2*_b2; //IIR Filter
+    _yn = _centreTap*_a0 - _a1*_ynm1 - _a2*_ynm2; //Result in yn
 
-    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
+    _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
 
     //THESE NEED TO BE LOADED IN THIS ORDER OTHERWISE ALL xnm VALUES WILL BECOME THE SAME AS xn
-    _xnm4 = _xnm3;
-    _xnm3 = _xnm2;
     _xnm2 = _xnm1;
     _xnm1 = _xn;
 
     //THESE NEED TO BE LOADED IN THIS ORDER OTHERWISE ALL ynm VALUES WILL BECOME THE SAME AS yn
-    _ynm4 = _ynm3;
-    _ynm3 = _ynm2;
     _ynm2 = _ynm1;
     _ynm1 = _yn;
 
@@ -66,7 +56,30 @@
 
 float FILTER::getvalue(void)
 {
-    return FilterOutput;  
+    return _FilterOutput;  
 }
 
-https://os.mbed.com/users/thomasmorris/code/ELEC347_CourseWork/
\ No newline at end of file
+void FILTER::Define_Filter()//Define the filter coefficients
+{
+    _G = pow(10,_Boost/20.0);
+    _k = 3 * ((_G - 1) / (_G+1));
+    _Wo = 2 * PI * _Fo;
+    _Wo_d = tan(_Wo / (2*_Fs));
+    _Wo_d2 = (_Wo_d * _Wo_d);                   //Omega-Dash-Naught-Squared
+    _Com_Den = 1 + ((3-_k)*_Wo_d/_Q) + _Wo_d2;
+    _NC0 = 1 + ((3+_k)*_Wo_d/_Q) + _Wo_d2;
+    _NC1 = -2 + (2 * _Wo_d2);
+    _NC2 = 1 - ((3+_k)*_Wo_d/_Q) + _Wo_d2;
+    _DC1 = _NC1;
+    _DC2 = 1 - ((3-_k)*_Wo_d/_Q) + _Wo_d2;
+    
+    //Calculate Coefficients
+    
+    _b0 = _NC0 / _Com_Den;
+    _b1 = _NC1 / _Com_Den;
+    _b2 = _NC2 / _Com_Den;
+    _a0 = 1.0000;
+    _a1 = _DC1 / _Com_Den;
+    _a2 = _DC2 / _Com_Den;
+}
+
--- a/Filter.hpp	Tue Nov 28 09:41:07 2017 +0000
+++ b/Filter.hpp	Tue Nov 28 09:51:30 2017 +0000
@@ -9,10 +9,18 @@
     //Can be interrupt driven
     int _Fs;
     int _Fo;
-    int _Boost;
+    double _Boost;
     int _Q;
-
-    float FilterOutput;
+    double _Wo_d;
+    double _Wo_d2;
+    float _Com_Den;
+    float _NC0;
+    float _NC1;
+    float _NC2;
+    float _DC1;
+    float _DC2;
+    
+    float _FilterOutput;
     float _centreTap; //internal node of filter
     float _b0;
     float _b1;
@@ -45,16 +53,18 @@
 
 
 public:
-    FILTER(int Fs, int Fo, int Boost, int Q);  //Constuctor
+    FILTER(int Fs, int Fo, double Boost, int Q);  //Constuctor
     ~FILTER(); //deConstuctor
 
-    //setters
-    void setvalue(int RAW_input);  //xn sample input
+    //Constuct filter
+    
+    void Define_Filter(); //Calculates the coefficeints of the filter
+    
+    //Setters
+    void setvalue(float RAW_input);  //xn sample input
 
-    //getters
+    //Getters
     float getvalue(void);          //returns yn
-
 };
 
 
-https://os.mbed.com/users/thomasmorris/code/ELEC347_CourseWork/
\ No newline at end of file
--- a/main.cpp	Tue Nov 28 09:41:07 2017 +0000
+++ b/main.cpp	Tue Nov 28 09:51:30 2017 +0000
@@ -14,7 +14,9 @@
 Thread T1;
 Thread T2;
 
-FILTER BP_filter(35000,4000,2,2);  //Create object of type Filter(Fs,Fo,Boost,Q)
+float input = 0.0;
+FILTER BP_filter(48000,10,16,1600);  //Create object of type Filter(Fs,Fo,Boost,Q)
+
 
 void sampler(void);
 
@@ -22,14 +24,12 @@
 int main()
 {
     
-    
+    BP_filter.Define_Filter();
     float sample_rate = (1.0/35000) ;
-    sample_timer.attach(&sampler,sample_rate);
-    
-    
+    sample_timer.attach(&sampler,sample_rate);;
 
     while(1) {
-        sleep();
+
     }
 }
 
@@ -39,7 +39,7 @@
 
     SampLED = 1;                        //LED Indicates start of sampling
 
-    float input = Ain;
+    input = Ain;
     BP_filter.setvalue(input);       //Input ADC. N.B. ADC in MBED is 0.0 to 1.0 float!!!!!!
     Aout = BP_filter.getvalue();
     
@@ -47,4 +47,3 @@
     SampLED = 0;      //LED Indicates end of sampling
 }
 
-https://os.mbed.com/users/thomasmorris/code/ELEC347_CourseWork/
\ No newline at end of file