Changes done in ECG and BT

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_20sept_2017_4th_oct_2017 by nikita teggi

Revision:
13:5d3b478ea9c7
Parent:
4:6bd81bb1790d
Child:
14:f5c62d30c6fc
--- a/ecgg.cpp	Mon May 08 05:19:22 2017 +0000
+++ b/ecgg.cpp	Sat May 27 05:47:55 2017 +0000
@@ -16,30 +16,125 @@
 
 
 
-int ecg(int pid) 
+float ecg(int pid) 
  {
 
     int concatenate_value2 = 0;
-    int32_t sample_sum; // static int32_t avg[1500];
-    int32_t count = 0; int32_t fs = 500; int32_t w=0,w1=0;
+ //   int32_t sample_sum; // static int32_t avg[1500];
+    int32_t count = 0; int32_t fs = 500; 
+    uint32_t ecg_buf[N_ECG];
     Timer t;
-    unsigned char chk = 1;
+    
+    //------------------ Declaration for Peak value detection ------------------------------------
+ 
+ uint32_t hi_val;uint32_t pk_val[20];unsigned int pk_pos[20]={0};unsigned int a; //uint32_t pk=0;
+ int hi_dif = 0;int j=0; // int count1 = N_ECG/fs, a_dif=0, fs1 = fs ,h=0;
+    // ------------------------- Declaration for Heart Rate calculation --------------------------
+ int n=0;    //int n=10; 
+ float pos_dif, HR[2], HR1,t_pos_dif;int t_sec = 60; float HR_sum = 0,HR_avg;
+    // -------------------------------------------------------------------------------------------
+    
+  //  unsigned char chk = 1;
     pc.baud(baud_rate);
     freqset();               // setting the frequency
     setupfunc();              
     ecgsetupfunc();
-    chk = 1;
+ //   chk = 1;
    sd_open_ECGfile(pid);        // opening the ecg file
-   
+  pc.printf( "Raw data is = \n"); 
  for(int i=0; i<N_ECG; i++)
    {
      concatenate_value2= readvalue();
      sd_write(concatenate_value2);               // writing into the sd card
+     ecg_buf[i] = concatenate_value2;
      pc.printf( "%d\n", concatenate_value2);
    } 
    
    sd_close();          // closing the file
-
+/*   
+ // ---------- reading back SD data for processing --------------------
+ sd_read_file(15);
+ printf("Reading back SD data\n");
+ for(int i=10;i<N_ECG;i++)
+ {
+ ecg_buf= sd_read();
+ //printf("%d\n",ecg_buf[i]);
+ }
+ 
+  sd_close();    */
+ 
+ //----------------------------- PEAK DETECTION AND HEART RATE CALCULATION ---------------------------------------------------
+ // -------------------------------------- PEAK DETECTION -------------------------------------------------------------
+ 
+  // ------------------ Main loop ---------------------------------
  
+for(int i=0;i<N_ECG-10;i++){
+if(ecg_buf[i]>ecg_buf[i+1])
+{
+hi_val = ecg_buf[i]; //To find the high value
+//printf("high value= %d\n",hi_val);
+a = i;
+//printf("a= %d\n",a);
+hi_dif = hi_val-ecg_buf[a+10];
+
+//a_dif = ecg_buf[a+2] - ecg_buf[a+1]; 
+//printf("The difference between high and low value is = %d\n",hi_dif);
+
+   // ---------------------------- If hi_val is greater than next ten input values, then compare the hi_val with the tenth input value. 
+    //     If the diff is greater than 10000, then it is a valid peak (pls chk the below condition)--------------------------------------------------
+ //   if((hi_dif > 10000) && ((a+10) < N_ECG))
+    if(hi_dif > 10000)
+    {
+   //  if(a_dif <= 0) ------------------------ add this condition if needed ----------------------
+   //  {
+     pk_val[j] = hi_val; //if condition satisfied, put the "pk" value into "pk_val" buffer
+     pc.printf("peak value= %d\n",pk_val[j]);
+     pk_pos[j]=a; // also save the peak's position
+     pc.printf("peak position is = %d\n",pk_pos[j]);
+     i = a+120;  // once confirmed that this is the necessary peak, skip the next 120 input values
+     n = j; // where n is the number of peaks detected
+     j = j+1;
+     
+  //   printf("j after peak detection is= %d\n",j);
+  //   }
+     }
+    else
+    {
+    //h = a+1;
+    i = a+1;
+  //  printf("i if peak not found is= %d\n",i);
+    }
+     // store the peak value position in "pk_pos" 
+     
+}
+}
+ // ----------------- HEART RATE LOGIC --------------------------- 
  
- }    // End of main function
+ for(int i = 0;i < n-1;i++)
+ {
+  pos_dif = pk_pos[i+1] - pk_pos[i];                 // difference between two consequtive peaks
+  pc.printf("peak position diff is = %f\n",pos_dif);
+  //printf("peak position i value is = %d\n",i);
+  t_pos_dif = pos_dif/fs;                           // sample difference between peak positions divided by sampling frequency gives the difference value in terms of actual time
+  pc.printf("time in seconds is = %f\n",t_pos_dif);
+  HR[i] = t_sec/t_pos_dif;                          //HR calculation
+  pc.printf("Heart Rate is = %f\n",HR[i]);
+ // n = i;
+  HR1 = HR[0];
+ 
+ }
+ // ---------------------- To average individual HRs for higher number of samples -----------------------
+ /* for(int i = 0;i < 5;i++)
+ {
+ HR_sum = HR[i]+HR_sum; 
+ }
+ HR_avg = HR_sum/5; // To find average of all the individual HRs calculated
+ printf("Heart Rate sum is = %f\n",HR_sum);
+ printf("Denominator = %d\n",n);
+ printf("Heart Rate = %f\n",HR_avg);
+ */
+  return HR1;
+  // return HR_avg; 
+  
+}          // End of main function
+