IIR Sample Code

Dependencies:   mbed

Committer:
martinsimpson
Date:
Fri Oct 13 09:40:21 2017 +0000
Revision:
0:cf938939cb77
First Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martinsimpson 0:cf938939cb77 1 #include "mbed.h"
martinsimpson 0:cf938939cb77 2 #include "main.h"
martinsimpson 0:cf938939cb77 3
martinsimpson 0:cf938939cb77 4 #include "BP_4KHz_Fc_35KHz_Fs_5N.h"
martinsimpson 0:cf938939cb77 5
martinsimpson 0:cf938939cb77 6 //MBED Class Instances follows
martinsimpson 0:cf938939cb77 7 DigitalOut SampLED(LED1); //Digital Output (GREEN LED is PB_3, D13 You can use an Oscilloscope on This pin to confirm sample rate)
martinsimpson 0:cf938939cb77 8
martinsimpson 0:cf938939cb77 9 AnalogIn Ain(PA_1); //Analog Input (Signal Input 0 to +3 Volts)
martinsimpson 0:cf938939cb77 10 AnalogOut Aout(PA_4); //Analog Output (Signal Input 0 to +3 Volts)
martinsimpson 0:cf938939cb77 11
martinsimpson 0:cf938939cb77 12 Ticker sample_timer;
martinsimpson 0:cf938939cb77 13
martinsimpson 0:cf938939cb77 14 //Ticker routine PROTOTYPE to service samples from Analogue IP and port to Analogue Output
martinsimpson 0:cf938939cb77 15 void sampler(void);
martinsimpson 0:cf938939cb77 16
martinsimpson 0:cf938939cb77 17 int main() {
martinsimpson 0:cf938939cb77 18
martinsimpson 0:cf938939cb77 19 float sample_rate=(1.0/Fs)*1000000.0; //Calculate the number of uS required for a Frequency Sampling Rate
martinsimpson 0:cf938939cb77 20 //Fs held in init.h
martinsimpson 0:cf938939cb77 21 sample_timer.attach_us(&sampler,(int)sample_rate);
martinsimpson 0:cf938939cb77 22 //Ticker Instance serviced by routine at a repeat rate in microseconds
martinsimpson 0:cf938939cb77 23
martinsimpson 0:cf938939cb77 24 while(1) {
martinsimpson 0:cf938939cb77 25 sleep();
martinsimpson 0:cf938939cb77 26 }
martinsimpson 0:cf938939cb77 27 }
martinsimpson 0:cf938939cb77 28
martinsimpson 0:cf938939cb77 29 //Ticker routine to service samples from Analogue IP and port to Analogue Output
martinsimpson 0:cf938939cb77 30 void sampler(void)
martinsimpson 0:cf938939cb77 31 {
martinsimpson 0:cf938939cb77 32 SampLED = ON; //LED Indicates start of sampling
martinsimpson 0:cf938939cb77 33
martinsimpson 0:cf938939cb77 34 xn=Ain; //Input ADC. N.B. ADC in MBED is 0.0 to 1.0 float!!!!!!
martinsimpson 0:cf938939cb77 35
martinsimpson 0:cf938939cb77 36 centreTap = xn*b0 + xnm1*b1 + xnm2*b2 + xnm3*b3 + xnm4*b4; //IIR Filter
martinsimpson 0:cf938939cb77 37 yn = centreTap*a0 - a1*ynm1 - a2*ynm2 - a3*ynm3 - a4*ynm4; //Result in yn
martinsimpson 0:cf938939cb77 38
martinsimpson 0:cf938939cb77 39 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
martinsimpson 0:cf938939cb77 40
martinsimpson 0:cf938939cb77 41 //THESE NEED TO BE LOADED IN THIS ORDER OTHERWISE ALL xnm VALUES WILL BECOME THE SAME AS xn
martinsimpson 0:cf938939cb77 42 xnm4 = xnm3;
martinsimpson 0:cf938939cb77 43 xnm3 = xnm2;
martinsimpson 0:cf938939cb77 44 xnm2 = xnm1;
martinsimpson 0:cf938939cb77 45 xnm1 = xn;
martinsimpson 0:cf938939cb77 46
martinsimpson 0:cf938939cb77 47 //THESE NEED TO BE LOADED IN THIS ORDER OTHERWISE ALL ynm VALUES WILL BECOME THE SAME AS yn
martinsimpson 0:cf938939cb77 48 ynm4 = ynm3;
martinsimpson 0:cf938939cb77 49 ynm3 = ynm2;
martinsimpson 0:cf938939cb77 50 ynm2 = ynm1;
martinsimpson 0:cf938939cb77 51 ynm1 = yn;
martinsimpson 0:cf938939cb77 52
martinsimpson 0:cf938939cb77 53 SampLED = OFF; //LED Indicates end of sampling
martinsimpson 0:cf938939cb77 54 }
martinsimpson 0:cf938939cb77 55