BioRobotics Group 3 / Mbed 2 deprecated Moving-average

Dependencies:   HIDScope MODSERIAL mbed

Fork of Signal Filter by BioRobotics Group 3

Revision:
6:c20a8c0f90ed
Parent:
5:ba1679ff9951
Child:
7:7493a0c5c6aa
diff -r ba1679ff9951 -r c20a8c0f90ed main.cpp
--- a/main.cpp	Mon Sep 21 10:49:48 2015 +0000
+++ b/main.cpp	Sun Sep 27 14:56:16 2015 +0000
@@ -13,13 +13,16 @@
 // Define the HIDScope and Ticker objects
 HIDScope    scope(1);
 Ticker scopeTimer;
-// Define the storage variables and filter coeicients for two filters
 
+// Define moving average variables
+const int N = 150;
+double a;  
 
  // Define the storage variables and filter coeicients for two filters
  double f1_v1 = 0, f1_v2 = 0, f2_v1 = 0, f2_v2 = 0;
- const double f1_a1 =  0.25071442433, f1_a2 = 0.21711875780, f1_b0 = 1.0, f1_b1 = -1.62432585007, f1_b2 = 1.0;
- const double f2_a1 = -1.77682226139, f2_a2 = 0.80213897411, f2_b0 = 1.0, f2_b1 =  -1.62432585007, f2_b2 = 1.0;
+const double f1_a1 =  -1.998222847291842, f1_a2 = 0.998224425026401, f1_b0 = 0.999111818079561, f1_b1 = -1.998223636159122, f1_b2 = 0.999111818079561;
+// const double f1_a1 =  0.25071442433, f1_a2 = 0.21711875780, f1_b0 = 1.0, f1_b1 = -1.62432585007, f1_b2 = 1.0;
+// const double f2_a1 = -1.77682226139, f2_a2 = 0.80213897411, f2_b0 = 1.0, f2_b1 =  -1.62432585007, f2_b2 = 1.0;
 
  double biquad( double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2 )
  {
@@ -29,6 +32,31 @@
  return y;
  }
 
+double sliding_average(double u,const int f_N)
+{
+    double f_x[f_N];
+    double f_sum=0;
+    f_x[1]=abs(u);
+    for (int i=f_N; i>=1; i--){
+        f_x[i]=f_x[i-1];
+        }
+        
+      
+    for (int i=f_N; i>=0; i--){
+        f_sum=f_x[i]+f_sum;
+       }
+        a=f_sum/(double(f_N)*0.002);
+        f_sum=0;
+    return a;
+}
+
+        
+    
+    
+        
+        
+
+
  // This is your controller, call it using a Ticker
 // void myController() {
  //double u1 = EMG, u2 = y1 ;
@@ -40,17 +68,19 @@
 
 
 void scopeSend(){
- double u1 = EMG  ; //filter 1 input
+ double u1 = EMG.read()  ; //filter 1 input
+
  double y1 = biquad( u1, f1_v1, f1_v2, f1_a1, f1_a2, f1_b0, f1_b1, f1_b2 );
- double u2 = y1; //filter 2 input
- double y2 = biquad( u2, f2_v1, f2_v2, f2_a1, f2_a2, f2_b0, f2_b1, f2_b2 );   
-scope.set(0,y2);
+ //double u2 = y1; //filter 2 input
+ //double y2 = biquad( u2, f2_v1, f2_v2, f2_a1, f2_a2, f2_b0, f2_b1, f2_b2 );
+ double z1 = sliding_average(y1, N);
+  scope.set(0,z1);
 scope.send();
  }
  
 int main()
 {
-    scopeTimer.attach_us(&scopeSend,1e4);
+    scopeTimer.attach(&scopeSend,0.002);
      while(1){}
         
 }
\ No newline at end of file