Changes done in ECG and BT
Dependencies: SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217
Fork of merged_code2_20sept_2017_4th_oct_2017 by
Diff: ecgg.cpp
- Revision:
- 26:53ff13a164f3
- Parent:
- 24:d992ee8369f4
- Child:
- 31:0f67acfc6236
--- a/ecgg.cpp Sun Jul 02 19:22:34 2017 +0000 +++ b/ecgg.cpp Wed Jul 05 15:36:58 2017 +0000 @@ -15,6 +15,7 @@ #include "struct.h" // Added on 31/5/17_Nidhin //#include "test_type.h" //Added on 31/5/17_Nidhin +FILE *fpeecg1; // FILE pointer to ECG file Serial pc(USBTX,USBRX); @@ -97,26 +98,34 @@ //----------------------------------------END Structure for Bluetooth - Added Nidhin 1/6/2017------- - int32_t concatenate_value2 = 0; // ORG. "int concatenate_value2 = 0;" Nidhin 1/6/17 - int32_t *ecg_ptr; // Added 1/6/2017 Nidhin + uint32_t concatenate_value2 = 0; // ORG. "int concatenate_value2 = 0;" Nidhin 1/6/17 + uint32_t *ecg_ptr; // Added 1/6/2017 Nidhin ecg_ptr = &concatenate_value2; // Pointer to pass for ECG write into SD card Nidhin 1/6/2017 - // int32_t sample_sum; // static int32_t avg[1500]; - int32_t count = 0; - //int32_t fs = 500; //COMMENTED NIDHIN 17/6/17 - //uint32_t ecg_buf[N_ECG]; //COMMENTED NIDHIN 17/6/17 - //Timer t; //COMMENTED NIDHIN 17/6/17 + uint16_t count = 0; + uint16_t fs = 500; + //uint32_t ecg_buf[N_ECG]; + Timer t; //------------------ 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; + uint32_t ecg_samp1[1] ; uint32_t ecg_samp2[1]; uint32_t ecg_samp3[1]; // Buff 12 &3 Stores sample 1, 2 & 10th sample + uint32_t fppos; // Variable to hold pointer position + uint32_t hi_val; + uint32_t pk_val[20]; + uint16_t pk_pos[20]={0}; + // unsigned int a; //uint32_t pk=0; + int32_t hi_dif = 0; //diff between high value and it's consecutive value + uint16_t j=0; // int count1 = N_ECG/fs, a_dif=0, fs1 = fs ,h=0; + int32_t m =0; // Variable to move the file pointer in fseek fun + int32_t samp_10 = 28; // Variable to move to 10th sampple from current + char buffer3[32]; + // ------------------------- 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; - */ //WHOLE SECTION COMMENTED NIDHIN 17/6/17 + uint8_t n=0; + float pos_dif, HR[10], HR1,t_pos_dif; + uint8_t t_sec = 60; + float HR_sum = 0,HR_avg; // ------------------------------------------------------------------------------------------- - // unsigned char chk = 1; pc.baud(baud_rate); freqset(); // setting the frequency @@ -132,16 +141,14 @@ sd_open_ECGfilee(pid); // REPLACED Nidhin 1/6/2017 Nidhin pc.printf( "Raw data is = \n"); + for(int i=0; i<N_ECG; i++) { concatenate_value2= readvalue(); pc.printf( "%d\n", concatenate_value2); //ADDED Nidhin 21/6/2017 - //ORIGINAL sd_write(concatenate_value2); // writing into the sd card COMMENTED Nidhin 1/6/2017 sd_ecgwrite(ecg_ptr); // REPLACED Nidhin 1/6/2017 - //pc.printf( "%d\n", *ecg_ptr); - //ecg_buf[i] = concatenate_value2; //COMMENTED Nidhin 10/6/2017 - //pc.printf( "%d\n", concatenate_value2); //COMMENTED Nidhin 10/6/2017 + } //sd_close(); // closing the file COMMENTED Nidhin 1/6/2017 @@ -160,54 +167,72 @@ sd_close(); */ + //----------------------------- PEAK DETECTION AND HEART RATE CALCULATION --------------------------------------------------- + // -------------------------------------- PEAK DETECTION ------------------------------------------------------------- + // ----------------------------------------- Main loop -------------------------------------------------- + + sprintf(buffer3, "/sd/%d_ECG.csv", pid); // For opening a specific file + fpeecg1 = fopen(buffer3, "r"); - // ------------------ Main loop --------------------------------- -/* -for(int i=0;i<N_ECG-10;i++){ -if(ecg_buf[i]>ecg_buf[i+1]) +for(uint16_t i=0;i<(N_ECG-10);i++){ + count++; +rewind(fpeecg1); // Go to start of file each time +fseek(fpeecg1, m, SEEK_CUR); // Update the count value according to move pointer //// after every calc. the pointer moves to 0th position, as we have used fseek, hence to make it jump to the respective position by "m" bytes this command is used +fread(ecg_samp1, sizeof(uint32_t), 1, fpeecg1); // Read sample 1 +fread(ecg_samp2, sizeof(uint32_t), 1, fpeecg1); // Read Sample 2 +fseek(fpeecg1, samp_10, SEEK_CUR); // Moving to tenth sample +fread(ecg_samp3, sizeof(uint32_t), 1, fpeecg1); // Read 3rd sample +//pc.printf("ecg_samp1 = %d , ecg_samp2 = %d, ecg_samp3 = %d\n",ecg_samp1[0],ecg_samp2[0],ecg_samp3[0] ); //Test Value held by buffer each round + + +if(ecg_samp1[0]>ecg_samp2[0]) { -hi_val = ecg_buf[i]; //To find the high value +hi_val = ecg_samp1[0]; //To find the high value //printf("high value= %d\n",hi_val); -a = i; +//a = i; //printf("a= %d\n",a); -hi_dif = hi_val-ecg_buf[a+10]; +hi_dif = hi_val-ecg_samp3[0]; -//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_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 + pk_pos[j]=i; // 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 + i = i+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; + m = m + 480; //similar reason to considering 28, but to skip 120 samples. this cond. is satisfied only when we hit a peak - suhasini_26thjune17 // 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); + m = m+4; // this is when we do not hit a peak and have to continue searching thru, hence move to the next sample and not skip 120 samples- - suhasini_26thjune17 } // store the peak value position in "pk_pos" - +} +else +{ +m = m+4; } +//pc.printf("i=%d",i); } +n=n+1; +pc.printf("n=%d\n",n); + // ----------------- HEART RATE LOGIC --------------------------- - for(int i = 0;i < n-1;i++) + for(uint16_t 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); @@ -218,26 +243,21 @@ 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++) + for(uint16_t i = 0;i < n-1;i++) { HR_sum = HR[i]+HR_sum; } - HR_avg = HR_sum/5; // To find average of all the individual HRs calculated + HR_avg = HR_sum/(n-1); // 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); - */ - //HR1 = 72; // Added Nidhin 10June17; - //ADDED Nidhin 1/6/2017 + //printf("Denominator = %d\n",n); + printf("Heart Rate avg is = %f\n",HR_avg); - uint16_t HR1; - -//HR1 = 72; - HR1 = heart_cal(pid); - + fclose(fpeecg1); + pc.printf("temporary file closed\n"); BLEMsg_info_ecg.cal_data.cal_sbp_dummy = 0; BLEMsg_info_ecg.cal_data.cal_dbp_OTtyp = HR1; //To be modified after HR code is added. @@ -248,8 +268,8 @@ - return HR1; - // return HR_avg; + //return HR1; + return HR_avg; } else @@ -257,5 +277,7 @@ pc.printf("no - leadoff"); return 0; } - +pc.printf("closing temporary file\n"); + // fclose(fpeecg1); +// pc.printf("temporary file closed\n"); } // End of main function