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

Files at this revision

API Documentation at this revision

Comitter:
SSR
Date:
Fri Dec 15 09:41:06 2017 +0000
Parent:
56:89eb9a977641
Commit message:
added function for 4 point or 8 point averaging on raw data.

Changed in this revision

bp.cpp Show annotated file Show diff for this revision Revisions of this file
sdcard.cpp Show annotated file Show diff for this revision Revisions of this file
--- 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);
                              
--- a/sdcard.cpp	Fri Nov 03 07:16:44 2017 +0000
+++ b/sdcard.cpp	Fri Dec 15 09:41:06 2017 +0000
@@ -335,6 +335,28 @@
         snd.printf("\nPPG file delete : Successful");   // Checks for SUCCESS
     else 
         snd.printf("\nPPG file delete : UnSuccessful");
+     
+     //Added by rohit ////////////   
+    status = 1 ;
+    sprintf(buffer3, "/sd/%d_BP_ECG_avg.csv",pid);
+    status = remove(buffer3);                           //Removes PPG file of BP and updates status with 0 if successful
+    
+    if( status == 0 )
+        snd.printf("\nECG_AVG file delete : Successful");   // Checks for SUCCESS
+    else 
+        snd.printf("\nECG_AVG file delete : UnSuccessful");   
+        
+        
+    status = 1 ;
+    sprintf(buffer3, "/sd/%d_BP_PPG_avg.csv",pid);
+    status = remove(buffer3);                           //Removes PPG file of BP and updates status with 0 if successful
+    
+    if( status == 0 )
+        snd.printf("\nPPG_AVG file delete : Successful");   // Checks for SUCCESS
+    else 
+        snd.printf("\nPPG_AVG file delete : UnSuccessful");   
+            
+    ////////////////////////////////////////////////////////////////    
        
 }
 
@@ -472,15 +494,31 @@
 void delete_subfiles(uint32_t pid)
 {
     char buffer3[32];  
-    int success1 , success2, success3;
+    int success1 , success2, success3,success4;
     success1 = 1 ;
     sprintf(buffer3, "/sd/%d_BP.csv", pid);
     success1 = remove(buffer3);                           //Removes PPG file of BP and updates status with 0 if successful
-    
     if( success1 == 0 )
         snd.printf("\nBP file delete : Successful");   // Checks for SUCCESS
     else 
         snd.printf("\nBP file delete : UnSuccessful");
+///////////////// Added by rohit /////////////////
+     success4 = 1;
+    sprintf(buffer3, "/sd/%d_BP_ECG_avg.csv", pid);
+    success4 = remove(buffer3);
+    if (success4 == 0)
+    snd.printf("\nBP_ECG_AVG file delete : Successful");
+    else
+    snd.printf("\nBP_ECG_AVG file delete : UnSuccessful");
+    
+    success4 = 1;
+    sprintf(buffer3, "/sd/%d_BP_PPG_avg.csv", pid);
+    success4 = remove(buffer3);
+    if (success4 == 0)
+    snd.printf("\nBP_PPG_AVG file delete : Successful");
+    else
+    snd.printf("\nBP_PPG_AVG file delete : UnSuccessful");
+   ///////////////////////////// 
 
     success2 = 1 ;
     sprintf(buffer3, "/sd/%d_GLC.csv", pid);
@@ -650,7 +688,7 @@
         for(uint16_t file_start=0;file_start<sample;file_start++)                                       //reading the raw data
                         {     
                             fread(buf,sizeof(uint32_t),1,fp_read_sdcard_file);
-                            snd.printf("%08x\n", buf[0]);
+                            snd.printf("%d\n", buf[0]);
                         }
       /* switch(sample)
             {