![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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:
- 51:1a4693774b60
- Parent:
- 43:85a7f399cb9d
- Child:
- 54:f2a413d5dffd
--- a/ecgg.cpp Sat Sep 16 06:54:42 2017 +0000 +++ b/ecgg.cpp Wed Sep 20 07:48:55 2017 +0000 @@ -17,6 +17,9 @@ #include "main.h" //#include "test_type.h" //Added on 31/5/17_Nidhin +void low_pass_filter(uint32_t pid); +void swap(uint32_t *ptr); + #define MAX_HR_THRESHOLD 205 #define MIN_HR_THRESHOLD 25 @@ -30,6 +33,8 @@ { uint8_t lead_reg=0; // added on 14/06 to check lead -off + FILE *fpeecg1; + pc.baud(baud_rate); //----------------------- Structure for Bluetooth Added Nidhin 1/6/2017-------------------// //BLUETOOTH STRUCTURE @@ -113,67 +118,68 @@ //uint32_t ecg_buf[N_ECG]; Timer t; - //------------------ Declaration for Peak value detection ------------------------------------ - uint32_t ecg_samp1[1] ; - uint32_t ecg_samp2[1]; - uint32_t ecg_samp3[1]; // to 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}; - 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 Peak value detection ***************/ + + int32_t ecg_samp1[1] ; //changed from uint32_t to static double, because LPF output is of static double type - by Suhasini- 5-8-17 + int32_t ecg_samp2[1]; //changed from uint32_t to static double- by Suhasini- 5-8-17 + int32_t ecg_samp3[1]; // to Stores sample 1, 2 & 10th sample //changed from uint32_t to static double- by Suhasini- 5-8-17 + + + uint32_t samp_10 = 28; + static int32_t hi_val; //changed from uint32_t to static double- by Suhasini- 5-8-17 + int32_t pk_val[20]; //changed from uint32_t to double - Suhasini - 5-8-17 + int32_t hi_dif = 0; //diff between high value and it's consecutive value //changed from uint32_t to static double- by Suhasini- 5-8-17 + uint16_t pk_pos[20]= {0}; // Variable to hold pointer positio + uint8_t j=0; // int count1 = N_ECG/fs, a_dif=0, fs1 = fs ,h=0; + uint32_t m = 400; // ------------------------- Declaration for Heart Rate calculation -------------------------- - uint8_t n=0; - float pos_dif, HR[10], HR1,t_pos_dif; - uint8_t t_sec = 60; - float HR_sum = 0,HR_avg; - // ------------------------------------------------------------------------------------------- - pc.baud(baud_rate); - freqset(); // setting the frequency - setupfunc(); - //ecgtestsetupfunc(); // For test set up of 1Hz square wave signal - lead_reg= ecgsetupfunc(); - - //ORIGINAL sd_open_ECGfile(pid); // opening the ecg file COMMENTED Nidhin 1/6/2017 - if (lead_reg==0) // checking for proper lead contact// 14/06 - { - 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 - - sd_ecgwrite(ecg_ptr); // REPLACED Nidhin 1/6/2017 - - } - - sd_close_ecg(); // closing the ECG file REPLACED Nidhin 1/6/2017 Nidhin + uint8_t n=0; + float pos_dif; // changed from float to uint16_t- Suhasini- 8-8-17, changed back to float- 17th aug,2017 + float HR[10],HR1,t_pos_dif; + uint8_t t_sec = 60; + float HR_sum = 0,HR_avg; + char buffer4[32] = {0}; + + + freqset(); // setting the frequency + setupfunc(); + lead_reg = ecgsetupfunc(); + + if (lead_reg==0) + { // checking for proper lead contact + + sd_open_ECGfilee(pid); //open temporary ecg.csv file to write 2000 samples of data + pc.printf( "Raw data is = \n"); + + for(int i=0; i<N_ECG; i++) { + concatenate_value2= readvalue(); + pc.printf( "%d\n", concatenate_value2); + + sd_ecgwrite(ecg_ptr); //writing data to temporary file ECG.csv + } + sd_close_ecg(); // closing the ECG file REPLACED Nidhin 1/6/2017 Nidhin + + low_pass_filter(pid); // low pass filter calculation function + //----------------------------- 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"); + sprintf(buffer4, "/sd/%d_ECG_lpf.csv", pid); // For opening a specific file + fpeecg1 = fopen(buffer4, "r"); -for(uint16_t i=0;i<(N_ECG-10);i++){ +for(uint16_t i=0;i<((N_ECG-4)-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 +fread(ecg_samp1, sizeof(int32_t), 1, fpeecg1); // Read sample 1 +fread(ecg_samp2, sizeof(int32_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 +fread(ecg_samp3, sizeof(int32_t), 1, fpeecg1); // Read 3rd sample if(ecg_samp1[0]>ecg_samp2[0]) { @@ -181,8 +187,8 @@ hi_dif = hi_val-ecg_samp3[0]; // ---------------------------- 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) + // If the diff is greater than 4000, then it is a valid peak (pls chk the below condition)------------------------------------- + if(hi_dif > 4000) { 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]); @@ -209,12 +215,13 @@ n=n+1; pc.printf("n=%d\n",n); +pc.printf("to enter HR loop\n"); // ----------------- HEART RATE LOGIC --------------------------- 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); + pc.printf("peak position diff is = %d\n",pos_dif); 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 @@ -263,4 +270,91 @@ return 0; } pc.printf("closing temporary file\n"); -} // End of main function \ No newline at end of file +} // End of main function +/************************* LOW PASS FILTER FUNCTION **************************/ + +void low_pass_filter(uint32_t pid) +{ + FILE *fpecg1; // FILE pointer to read raw data ECG file added by Suhasini 5-8-17 + FILE *fpeecg_lpf; // FILE pointer to read and write into LPF output file added by Suhasini 5-8-17 + static uint32_t ecg_raw_temp[5]; // changed by rohit on 13 sep + static uint32_t ecg_raw[5]; // Buff of 5 samples created to Stores input samples for LPF added by Suhasini - 5-8-17 + static uint32_t lpf_op[5]= {0.0}; // for LPF output added by Suhasini - 5-8-17 // changed from double to int32_t- 16-8-17 + + uint32_t lpf_4samp = 0; // Variable to move 16 samples back from current in input file - changed by Suhasini- 5-8-17 + char buffer3[32]; + char buffer4[32]; // Added by Suhasini - 5-8-17 + const float b[5]= {0.0000624,0.0002495,0.0003743,0.0002495,0.0000624}; //Numerator co-efficients of low-pass butter-worth filter of order=4 + const float a[5]= {1.0000,-3.5078,4.6409,-2.7427,0.6105}; //Denominator co-efficients of low-pass butter-worth filter of order=4 + + + + sprintf(buffer3, "/sd/%d_ECG.csv", pid); // opening raw data file to read + fpecg1 = fopen(buffer3, "r"); // to read data from the above opened temp file + pc.printf("entered raw data file\n"); + sprintf(buffer4, "/sd/%d_ECG_lpf.csv", pid); // opening empty file to write lpf data + fpeecg_lpf = fopen(buffer4, "a"); // to read data from and write data into the above opened file // changed from "a+" to "a" + pc.printf("entered LPF data file\n"); + + fwrite(&lpf_op, sizeof(int32_t), 4, fpeecg_lpf); + pc.printf("first 4 LPF data %d\n %d\n %d\n %d\n", lpf_op[0],lpf_op[1],lpf_op[2],lpf_op[3]); + + pc.printf("low pass filter output is= %d\n",lpf_op[4]); //message to display LPF output + + fread(&ecg_raw, sizeof(uint32_t), 5, fpecg1); + // pc.printf("first 4 RAW data %d\n %d\n %d\n %d\n %d\n", ecg_raw[0],ecg_raw[1],ecg_raw[2],ecg_raw[3],ecg_raw[4]); + + for (uint16_t i = 1; i < (N_ECG-4); i++) { // reduced loop length to check i/p and o/p timing + //fseek(fpecg1, lpf_4samp, SEEK_CUR); // Update the count value according to move pointer + // fread(&ecg_raw, sizeof(uint32_t), 5, fpecg1); // Read sample 1 of i/p + + /**********rohit***************/ + //fread(&ecg_raw_temp, sizeof(uint32_t), 5, fpecg1); + //memcpy(ecg_raw, ecg_raw_temp,sizeof(ecg_raw)); + + // fread(ecg_raw, sizeof(uint32_t), 5, fpecg1); + + /*********** filter output for the ECG data for a low-pass butter-worth filter of order=4 is as follows******************/ + + + + (lpf_op[4]) = (int32_t)(b[0]*((ecg_raw[4]))) + (int32_t)(b[1]*((ecg_raw[3]))) + (int32_t)(b[2]*((ecg_raw[2]))) + (int32_t)(b[3]*((ecg_raw[1]))) + (int32_t)(b[4]*((ecg_raw[0]))) - (int32_t)(a[1]*lpf_op[3]) - (int32_t)(a[2]*lpf_op[2]) - (int32_t)(a[3]*lpf_op[1]) - (int32_t)(a[4]*lpf_op[0]); + + + fwrite(&lpf_op[4], sizeof(int32_t), 1, fpeecg_lpf); + + //pc.printf("pointer to output value is= %ld\n",ftell(fpeecg_lpf)); + pc.printf("%d\n",lpf_op[4]); // to print LPF output on hyperterminal + swap(lpf_op); + /* + lpf_op[0] = lpf_op[1]; + lpf_op[1] = lpf_op[2]; + lpf_op[2] = lpf_op[3]; + lpf_op[3] = lpf_op[4]; + */ + // rewind(fpecg1); commented by rohit + // rewind(fpeecg_lpf); // commented by nikita 5/09/17 // no need to rewind as single array member result is stored in the file + // lpf_4samp = (i*4); // commented by rohit + + // lpfop_4samp = (i*4); + swap(ecg_raw); // 8 changed to 4, because datatype has been changed from double to int32_t- suhasini- 16/8/17 /// commented by nikita 5/09/17 // not used anywhere// + fread(&ecg_raw[4], sizeof(uint32_t), 1, fpecg1); + pc.printf("5th samples: %d\n",ecg_raw[4]); + + //fseek(fpecg1, -16 , SEEK_CUR); + // memset(ecg_raw,0,sizeof(ecg_raw)); + } + + + fclose(fpecg1); + fclose(fpeecg_lpf); + pc.printf("temporary file closed after LPF\n"); + +} + +void swap(uint32_t *ptr) +{ + int i; + for(i=0;i<4;i++) + ptr[i] = ptr[i+1]; +} \ No newline at end of file