IIR Sample Code

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
martinsimpson
Date:
Fri Oct 13 09:40:21 2017 +0000
Commit message:
First Commit

Changed in this revision

BP_4KHz_Fc_35KHz_Fs_5N.h 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
main.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r cf938939cb77 BP_4KHz_Fc_35KHz_Fs_5N.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BP_4KHz_Fc_35KHz_Fs_5N.h	Fri Oct 13 09:40:21 2017 +0000
@@ -0,0 +1,16 @@
+#define Fs  35000       //Frequency of Sample Rate in Hz
+#define N       5           //Number of Nodes N
+
+// B - Numerator coefficients
+float b0 = 0.0201;
+float b1 = 0.0;
+float b2 = -0.0402;
+float b3 = 0.0;
+float b4 = 0.0201;
+
+// A - Denominator coefficients
+float a0 =    1.0000; 
+float a1 =      -2.5494; 
+float a2 =      3.2024; 
+float a3 =      -2.0359; 
+float a4 =      0.6414;
diff -r 000000000000 -r cf938939cb77 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Oct 13 09:40:21 2017 +0000
@@ -0,0 +1,55 @@
+#include "mbed.h"
+#include "main.h"
+
+#include "BP_4KHz_Fc_35KHz_Fs_5N.h"
+
+//MBED Class Instances follows
+DigitalOut SampLED(LED1);              //Digital Output  (GREEN LED is PB_3, D13 You can use an Oscilloscope on This pin to confirm sample rate)
+
+AnalogIn  Ain(PA_1);                    //Analog Input (Signal Input 0 to +3 Volts)
+AnalogOut Aout(PA_4);                   //Analog Output (Signal Input 0 to +3 Volts)
+
+Ticker sample_timer;
+
+//Ticker routine PROTOTYPE to service samples from Analogue IP and port to Analogue Output
+void sampler(void);
+
+int main() {
+   
+    float sample_rate=(1.0/Fs)*1000000.0; //Calculate the number of uS required for a Frequency Sampling Rate
+                                          //Fs held in init.h
+    sample_timer.attach_us(&sampler,(int)sample_rate);
+                                          //Ticker Instance serviced by routine at a repeat rate in microseconds
+  
+    while(1) {
+          sleep();
+    }
+}
+
+//Ticker routine to service samples from Analogue IP and port to Analogue Output
+void sampler(void)
+{
+    SampLED = ON;       //LED Indicates start of sampling
+
+    xn=Ain;               //Input ADC. N.B. ADC in MBED is 0.0 to 1.0 float!!!!!!
+
+    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
+  
+    Aout=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;
+
+    SampLED = OFF;      //LED Indicates end of sampling
+}
+
diff -r 000000000000 -r cf938939cb77 main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Fri Oct 13 09:40:21 2017 +0000
@@ -0,0 +1,19 @@
+#define ON  1
+#define OFF 0
+
+unsigned short ADC_DATA;
+float centreTap = 0.0;
+
+//ALL unsigned short VALUES HAVE BEEN CHANGED TO FLOATS OTHERWISE THE COEFFICIENT MULTIPLICATIONS WILL BE ROUNDED TO THE NEAREST INTEGER WHICH IN SOME CASES IS ZERO.
+float xn   = 0.0;       
+float xnm1 = 0.0;
+float xnm2 = 0.0;
+float xnm3 = 0.0;
+float xnm4 = 0.0;
+
+
+float yn   = 0.0;
+float ynm1 = 0.0;
+float ynm2 = 0.0;
+float ynm3 = 0.0;
+float ynm4 = 0.0;
diff -r 000000000000 -r cf938939cb77 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Oct 13 09:40:21 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/b484a57bc302
\ No newline at end of file