Added 4 point or 8 point averaging function .

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_3rd_nov_2017 by nikita teggi

Revision:
57:a366af1ddd7b
Parent:
43:85a7f399cb9d
--- a/bp.cpp	Fri Nov 03 07:16:44 2017 +0000
+++ b/bp.cpp	Fri Dec 15 09:41:06 2017 +0000
@@ -23,12 +23,18 @@
 #define MIN_SBP_THRESHOLD 40
 #define MIN_DBP_THRESHOLD 40
 
-
+#define AVG_TYPE 8
+#define SAMPLE_NO_ECG 1016
+#define SAMPLE_NO_PPG 1656
+ 
 FILE *fpecg_bp; // file pointer in place of fpecg- Suhasini- 21/7/17
 FILE *fpppg1;
 
 #include "struct.h"
 
+void average_4_8(char file_name[],char file_name1[], uint32_t sampleno);
+
+
 using namespace std;
 
 Serial bpr(USBTX,USBRX);
@@ -49,6 +55,11 @@
     uint8_t lead_reg=0;
     uint32_t concatenate_value2 = 0;    //int to uint32_t-Suhasini-21stJuly2017
     uint8_t chk = 1;    //unsigned char to uint8_t-Suhasini-21stJuly2017
+  char file_name[32]={0};
+  char file_name1[32]={0};
+  //uint8_t avg_type
+  // uint32_t sampleno;
+  
   
     //Declaration of  Master structure
     
@@ -190,6 +201,23 @@
      
     maxim_max30102_reset();
     
+///////////// Averaging on Raw Data ////////////////////////////
+    
+    sprintf(file_name,"/sd/%d_BP_ECG.csv",pid);
+    sprintf(file_name1,"/sd/%d_BP_ECG_avg.csv",pid);
+    
+    average_4_8(file_name,file_name1,1024);
+    
+   // file_name[]={0};
+   // file_name[]=(0};
+    
+    sprintf(file_name,"/sd/%d_BP_PPG.csv",pid);
+    sprintf(file_name1,"/sd/%d_BP_PPG_avg.csv",pid);
+    
+    average_4_8(file_name,file_name1,1664);
+    
+    
+    
     uint16_t eloc = 0;  //int to uint16_t-Suhasini-21stJuly2017
     uint16_t ploc = 0;  //int to uint16_t-Suhasini-21stJuly2017
    
@@ -245,6 +273,88 @@
      screen_ecg_bp() ; 
 } 
 
+////// AVERAGE FUNCTION///////////////////
+
+void average_4_8( char file_name[],char file_name1[], uint32_t sampleno)
+{
+    
+uint32_t ecg_raw[4];
+uint32_t ecg_raw1[8];
+uint32_t average=0;
+
+uint16_t i;
+FILE *fpecg =NULL;
+FILE *fpecg_avg = NULL;
+
+
+//sprintf(file_name,"/sd/%d_BP_ECG.csv",pid);
+//sprintf(file_name1,"/sd/%d_BP_ECG_avg.csv",pid);
+
+    fpecg = fopen(file_name,"r");
+    
+        if(fpecg == NULL)
+        {
+            printf("error in opening raw data file file");
+            exit(1);
+        }
+    
+    fpecg_avg= fopen(file_name1,"w");  //a
+    
+        if(fpecg_avg == NULL)
+        {
+            printf("error in opening average file");
+            exit(1);
+        }
+     bpr.printf("open_avg_file\n");
+    if (  AVG_TYPE == 4)
+    {
+        fread(ecg_raw,sizeof(uint32_t),3,fpecg);
+        
+        bpr.printf("four point average data are\n");
+        for(i=0;i<(sampleno-4);i++)
+        {
+            fread(&ecg_raw[3], sizeof(uint32_t),1, fpecg);
+            average = (ecg_raw[0] + ecg_raw[1] + ecg_raw[2] + ecg_raw[3])/4;
+             fwrite(&average,sizeof(uint32_t),1,fpecg_avg);
+             bpr.printf("%d\n",average);
+
+            ecg_raw[0] = ecg_raw[1];
+            ecg_raw[1] = ecg_raw[2];
+            ecg_raw[2] = ecg_raw[3];    
+        }
+    }
+    
+    else
+    {
+        fread(ecg_raw1,sizeof(uint32_t),7,fpecg);
+        bpr.printf("eight point average data are\n");
+        
+        for( i= 0 ;i<(sampleno-8);i++)
+        {
+            fread(&ecg_raw1[7], sizeof(uint32_t),1, fpecg);
+            
+            average = ( ecg_raw1[0] + ecg_raw1[1] + ecg_raw1[2] + ecg_raw1[3] + ecg_raw1[4] + ecg_raw1[5] + ecg_raw1[6] + ecg_raw1[7])/8;
+            
+            fwrite(&average,sizeof(uint32_t),1,fpecg_avg);
+            bpr.printf("%d\n",average);
+            
+            ecg_raw1[0] = ecg_raw1[1];
+            ecg_raw1[1] = ecg_raw1[2];
+            ecg_raw1[2] = ecg_raw1[3];
+            ecg_raw1[3] = ecg_raw1[4];
+            ecg_raw1[4] = ecg_raw1[5];
+            ecg_raw1[5] = ecg_raw1[6];
+            ecg_raw1[6] = ecg_raw1[7];
+            
+        }
+        
+    }
+    
+    fclose(fpecg);
+    fclose(fpecg_avg);
+     bpr.printf("close_avg_file\n");
+}
+
 
  //------------------------------------------  
  //REOPEN FUNCTIONs for reading data Nidhin
@@ -256,7 +366,8 @@
   uint16_t samplesecg =0;   // changed from int to uint32_t - suhasini - 21/7/17  
   char buffer1[32];
    
-  sprintf(buffer1,"/sd/%d_BP_ECG.csv",pid);
+ // sprintf(buffer1,"/sd/%d_BP_ECG.csv",pid);
+ sprintf(buffer1,"/sd/%d_BP_ECG_avg.csv",pid);
   fpecg_bp = fopen(buffer1,"r"); 
   if(fpecg_bp == NULL)
   {
@@ -265,7 +376,7 @@
   
   else
   {
-    for(ebp=0; ebp<1024; ebp++)
+    for(ebp=0; ebp<1016; ebp++)
     {
 
     
@@ -292,7 +403,8 @@
   uint16_t samplesppg = 0;      //int to uint16_t-Suhasini-21stJuly2017
   char buffer1[32];
    
-  sprintf(buffer1,"/sd/%d_BP_PPG.csv",pid);
+ // sprintf(buffer1,"/sd/%d_BP_PPG.csv",pid);
+ sprintf(buffer1,"/sd/%d_BP_PPG_avg.csv",pid);
   fpppg1 = fopen(buffer1,"r"); 
   if(fpppg1 == NULL)
   {
@@ -301,7 +413,7 @@
   
   else
   {
-    for(pbp=0; pbp<1664; pbp++)
+    for(pbp=0; pbp<1656 ; pbp++)
             {
                 fread(b, sizeof(uint32_t),1, fpppg1);