Changes done in ECG and BT
Dependencies: SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217
Fork of merged_code2_20sept_2017_4th_oct_2017 by
ecgg.cpp@31:0f67acfc6236, 2017-07-15 (annotated)
- Committer:
- nikitateggi
- Date:
- Sat Jul 15 05:39:45 2017 +0000
- Revision:
- 31:0f67acfc6236
- Parent:
- 26:53ff13a164f3
- Child:
- 32:76892fdf5e4c
new display code 15/07/17
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikitateggi | 1:8316c23ec6b9 | 1 | /** ECG ADS1291 Test program. |
nikitateggi | 1:8316c23ec6b9 | 2 | ADS1291 is a single channel ECG chip |
nikitateggi | 1:8316c23ec6b9 | 3 | with a 24 bit Sigma-Delta ADC |
nikitateggi | 1:8316c23ec6b9 | 4 | */ |
nikitateggi | 1:8316c23ec6b9 | 5 | |
nikitateggi | 1:8316c23ec6b9 | 6 | #include "mbed.h" |
nikitateggi | 1:8316c23ec6b9 | 7 | #include <string.h> |
nikitateggi | 1:8316c23ec6b9 | 8 | #include <stdio.h> |
nikitateggi | 1:8316c23ec6b9 | 9 | #include "ds3231.h" |
nikitateggi | 1:8316c23ec6b9 | 10 | #include "SDFileSystem.h" |
nikitateggi | 1:8316c23ec6b9 | 11 | #include "ecg_dec.h" |
nikitateggi | 1:8316c23ec6b9 | 12 | #include "rtc.h" |
nikitateggi | 1:8316c23ec6b9 | 13 | #include "sdcard.h" |
nikitateggi | 3:9a06c2bed650 | 14 | #include "ec_bp.h" |
nidhinvarghese | 14:f5c62d30c6fc | 15 | |
nidhinvarghese | 14:f5c62d30c6fc | 16 | #include "struct.h" // Added on 31/5/17_Nidhin |
nikitateggi | 19:1650bbc923cc | 17 | //#include "test_type.h" //Added on 31/5/17_Nidhin |
suhasini | 26:53ff13a164f3 | 18 | FILE *fpeecg1; // FILE pointer to ECG file |
nikitateggi | 3:9a06c2bed650 | 19 | Serial pc(USBTX,USBRX); |
nikitateggi | 1:8316c23ec6b9 | 20 | |
nikitateggi | 1:8316c23ec6b9 | 21 | |
nikitateggi | 22:ffa88619551d | 22 | uint16_t ecg(int32_t pid) |
nikitateggi | 22:ffa88619551d | 23 | //float ecg(int pid) |
nikitateggi | 3:9a06c2bed650 | 24 | { |
nikitateggi | 20:7c64e6ecad76 | 25 | |
nikitateggi | 20:7c64e6ecad76 | 26 | uint8_t lead_reg=0; // added on 14/06 to check lead -off |
nidhinvarghese | 14:f5c62d30c6fc | 27 | //----------------------- Structure for Bluetooth Added Nidhin 1/6/2017-------------------// |
nidhinvarghese | 14:f5c62d30c6fc | 28 | |
nidhinvarghese | 14:f5c62d30c6fc | 29 | //BLUETOOTH STRUCTURE |
nidhinvarghese | 14:f5c62d30c6fc | 30 | |
nidhinvarghese | 14:f5c62d30c6fc | 31 | BLEMsg_info *ptr_BLEMsg_info_ecg, BLEMsg_info_ecg; // A copy of master strcuture [ "BLEMsg_info" ] by name "BLEMsg_info_ecg" is created |
nidhinvarghese | 14:f5c62d30c6fc | 32 | ptr_BLEMsg_info_ecg = &BLEMsg_info_ecg; // *ptr_BLEMsg_info_bp is the pointer to local copy; |
nidhinvarghese | 14:f5c62d30c6fc | 33 | |
nidhinvarghese | 14:f5c62d30c6fc | 34 | // Declaration of Date Structure |
nidhinvarghese | 14:f5c62d30c6fc | 35 | DateTime_info *ptr_DateTime_info_ecg, DateTime_info_ecg; // A copy of Master Structure "DateTime_info" created, |
nidhinvarghese | 14:f5c62d30c6fc | 36 | ptr_DateTime_info_ecg = &DateTime_info_ecg; // Structure pointer points to that copy. |
nidhinvarghese | 14:f5c62d30c6fc | 37 | |
nidhinvarghese | 14:f5c62d30c6fc | 38 | |
nidhinvarghese | 14:f5c62d30c6fc | 39 | // RTC operations |
nidhinvarghese | 14:f5c62d30c6fc | 40 | time_t epoch_time_ecg; //A copy of time_t by name epoch_time_bp is created |
nidhinvarghese | 14:f5c62d30c6fc | 41 | epoch_time_ecg = rtc_read(); // time is got from get epoch function. |
nidhinvarghese | 14:f5c62d30c6fc | 42 | |
nidhinvarghese | 14:f5c62d30c6fc | 43 | struct tm * ptr_time_info_ecg, time_info_ecg; // Sturucture copy of tm is created |
nidhinvarghese | 14:f5c62d30c6fc | 44 | ptr_time_info_ecg = localtime(&epoch_time_ecg); // Structure accepts the time in local format from "time_t" type. |
nidhinvarghese | 14:f5c62d30c6fc | 45 | |
nidhinvarghese | 14:f5c62d30c6fc | 46 | //BELOW LINE IS TO CHECK Date and TIME |
nidhinvarghese | 15:208b146151ba | 47 | //pc.printf("Time is %d: %d: %d\n", (*ptr_time_info_ecg).tm_hour, (*ptr_time_info_ecg).tm_min, (*ptr_time_info_ecg).tm_sec); |
nidhinvarghese | 15:208b146151ba | 48 | //pc.printf("Date is %d:%d:%d\n", (*ptr_time_info_ecg).tm_mday, (*ptr_time_info_ecg).tm_mon+1, (*ptr_time_info_ecg).tm_year-100); |
nidhinvarghese | 14:f5c62d30c6fc | 49 | |
nidhinvarghese | 14:f5c62d30c6fc | 50 | //Copying from one structure to the other using variables |
nidhinvarghese | 14:f5c62d30c6fc | 51 | DateTime_info_ecg.hour = (uint8_t)(*ptr_time_info_ecg).tm_hour; |
nidhinvarghese | 14:f5c62d30c6fc | 52 | DateTime_info_ecg.mins = (uint8_t)(*ptr_time_info_ecg).tm_min; |
nidhinvarghese | 14:f5c62d30c6fc | 53 | DateTime_info_ecg.sec = (uint8_t)(*ptr_time_info_ecg).tm_sec; |
nidhinvarghese | 14:f5c62d30c6fc | 54 | |
nidhinvarghese | 14:f5c62d30c6fc | 55 | DateTime_info_ecg.date = (uint8_t) (*ptr_time_info_ecg).tm_mday; |
nidhinvarghese | 14:f5c62d30c6fc | 56 | DateTime_info_ecg.month =(uint8_t)(*ptr_time_info_ecg).tm_mon+1; |
nidhinvarghese | 14:f5c62d30c6fc | 57 | DateTime_info_ecg.year = (uint8_t)(*ptr_time_info_ecg).tm_year-100; |
nidhinvarghese | 14:f5c62d30c6fc | 58 | |
nidhinvarghese | 14:f5c62d30c6fc | 59 | // Copying Time to Main structure |
nidhinvarghese | 14:f5c62d30c6fc | 60 | BLEMsg_info_ecg.date_time.hour = DateTime_info_ecg.hour; |
nidhinvarghese | 14:f5c62d30c6fc | 61 | BLEMsg_info_ecg.date_time.mins = DateTime_info_ecg.mins; |
nidhinvarghese | 14:f5c62d30c6fc | 62 | BLEMsg_info_ecg.date_time.sec = DateTime_info_ecg.sec; |
nidhinvarghese | 14:f5c62d30c6fc | 63 | |
nidhinvarghese | 14:f5c62d30c6fc | 64 | BLEMsg_info_ecg.date_time.date = DateTime_info_ecg.date ; |
nidhinvarghese | 14:f5c62d30c6fc | 65 | BLEMsg_info_ecg.date_time.month = DateTime_info_ecg.month ; |
nidhinvarghese | 14:f5c62d30c6fc | 66 | BLEMsg_info_ecg.date_time.year = DateTime_info_ecg.year ; |
nidhinvarghese | 14:f5c62d30c6fc | 67 | |
nidhinvarghese | 14:f5c62d30c6fc | 68 | |
nidhinvarghese | 14:f5c62d30c6fc | 69 | //Checking if the structure has these values |
nidhinvarghese | 15:208b146151ba | 70 | //pc.printf("Time 2 is %d:%d:%d\n", DateTime_info_ecg.hour, DateTime_info_ecg.mins, DateTime_info_ecg.sec); |
nidhinvarghese | 15:208b146151ba | 71 | //pc.printf("\t Date is %d:%d:%d\n",DateTime_info_ecg.date, DateTime_info_ecg.month, DateTime_info_ecg.year); |
nidhinvarghese | 14:f5c62d30c6fc | 72 | |
nidhinvarghese | 14:f5c62d30c6fc | 73 | |
nidhinvarghese | 14:f5c62d30c6fc | 74 | //Loading values to of Test type |
nidhinvarghese | 14:f5c62d30c6fc | 75 | test_type_info test_type_info_ecg; // copy of " test_type_info" created |
nidhinvarghese | 14:f5c62d30c6fc | 76 | test_type_info_ecg = ECG_Test; // Loaded value 00 to the test type |
nidhinvarghese | 14:f5c62d30c6fc | 77 | |
nidhinvarghese | 14:f5c62d30c6fc | 78 | BLEMsg_info_ecg.test_type = test_type_info_ecg; |
nidhinvarghese | 14:f5c62d30c6fc | 79 | //Check if 00 is getting printed |
nidhinvarghese | 15:208b146151ba | 80 | //pc.printf("Test Type is : %d\n", test_type_info_ecg); |
nidhinvarghese | 14:f5c62d30c6fc | 81 | |
nidhinvarghese | 14:f5c62d30c6fc | 82 | |
nidhinvarghese | 14:f5c62d30c6fc | 83 | // Loading values of Length , PID, DID, sampling frequency, number of samples, calculated data. |
nidhinvarghese | 14:f5c62d30c6fc | 84 | BLEMsg_info_ecg.device_id = 01; // Device ID fixed |
nidhinvarghese | 14:f5c62d30c6fc | 85 | BLEMsg_info_ecg.patient_id = (uint32_t)pid; // Patient ID |
nidhinvarghese | 14:f5c62d30c6fc | 86 | BLEMsg_info_ecg.sampling_freq = 500; // sampling frrquency |
nikitateggi | 22:ffa88619551d | 87 | BLEMsg_info_ecg.length = 4022; //Total length of data in bytes 22 B+ 4000 data |
nidhinvarghese | 14:f5c62d30c6fc | 88 | |
nidhinvarghese | 14:f5c62d30c6fc | 89 | /* |
nidhinvarghese | 14:f5c62d30c6fc | 90 | //Loading number of samples |
nidhinvarghese | 14:f5c62d30c6fc | 91 | NumSamples_info NumSamples_info_bp; //Copy of structure NumSamples_info |
nidhinvarghese | 14:f5c62d30c6fc | 92 | NumSamples_info_bp.num_ppg_sample = 1664; // PPG & ECG Sample number loaded in structure copy |
nidhinvarghese | 14:f5c62d30c6fc | 93 | NumSamples_info_bp.num_ecg_sample = 1024; |
nidhinvarghese | 14:f5c62d30c6fc | 94 | */ |
nidhinvarghese | 14:f5c62d30c6fc | 95 | |
nidhinvarghese | 14:f5c62d30c6fc | 96 | BLEMsg_info_ecg.num_samples.num_sample_ppg_dummy = 0 ;// PPG number of samples copied to master struct |
nidhinvarghese | 14:f5c62d30c6fc | 97 | BLEMsg_info_ecg.num_samples.num_sample_ecg_OTtyp = 1000 ; // ECG number of samples copied to master struct |
nidhinvarghese | 14:f5c62d30c6fc | 98 | |
nidhinvarghese | 14:f5c62d30c6fc | 99 | //----------------------------------------END Structure for Bluetooth - Added Nidhin 1/6/2017------- |
nidhinvarghese | 14:f5c62d30c6fc | 100 | |
suhasini | 26:53ff13a164f3 | 101 | uint32_t concatenate_value2 = 0; // ORG. "int concatenate_value2 = 0;" Nidhin 1/6/17 |
suhasini | 26:53ff13a164f3 | 102 | uint32_t *ecg_ptr; // Added 1/6/2017 Nidhin |
nidhinvarghese | 14:f5c62d30c6fc | 103 | ecg_ptr = &concatenate_value2; // Pointer to pass for ECG write into SD card Nidhin 1/6/2017 |
nidhinvarghese | 14:f5c62d30c6fc | 104 | |
suhasini | 26:53ff13a164f3 | 105 | uint16_t count = 0; |
suhasini | 26:53ff13a164f3 | 106 | uint16_t fs = 500; |
suhasini | 26:53ff13a164f3 | 107 | //uint32_t ecg_buf[N_ECG]; |
suhasini | 26:53ff13a164f3 | 108 | Timer t; |
suhasini | 13:5d3b478ea9c7 | 109 | |
suhasini | 13:5d3b478ea9c7 | 110 | //------------------ Declaration for Peak value detection ------------------------------------ |
suhasini | 26:53ff13a164f3 | 111 | uint32_t ecg_samp1[1] ; uint32_t ecg_samp2[1]; uint32_t ecg_samp3[1]; // Buff 12 &3 Stores sample 1, 2 & 10th sample |
suhasini | 26:53ff13a164f3 | 112 | uint32_t fppos; // Variable to hold pointer position |
suhasini | 26:53ff13a164f3 | 113 | uint32_t hi_val; |
suhasini | 26:53ff13a164f3 | 114 | uint32_t pk_val[20]; |
suhasini | 26:53ff13a164f3 | 115 | uint16_t pk_pos[20]={0}; |
suhasini | 26:53ff13a164f3 | 116 | // unsigned int a; //uint32_t pk=0; |
suhasini | 26:53ff13a164f3 | 117 | int32_t hi_dif = 0; //diff between high value and it's consecutive value |
suhasini | 26:53ff13a164f3 | 118 | uint16_t j=0; // int count1 = N_ECG/fs, a_dif=0, fs1 = fs ,h=0; |
suhasini | 26:53ff13a164f3 | 119 | int32_t m =0; // Variable to move the file pointer in fseek fun |
suhasini | 26:53ff13a164f3 | 120 | int32_t samp_10 = 28; // Variable to move to 10th sampple from current |
suhasini | 26:53ff13a164f3 | 121 | char buffer3[32]; |
suhasini | 26:53ff13a164f3 | 122 | |
suhasini | 13:5d3b478ea9c7 | 123 | // ------------------------- Declaration for Heart Rate calculation -------------------------- |
suhasini | 26:53ff13a164f3 | 124 | uint8_t n=0; |
suhasini | 26:53ff13a164f3 | 125 | float pos_dif, HR[10], HR1,t_pos_dif; |
suhasini | 26:53ff13a164f3 | 126 | uint8_t t_sec = 60; |
suhasini | 26:53ff13a164f3 | 127 | float HR_sum = 0,HR_avg; |
suhasini | 13:5d3b478ea9c7 | 128 | // ------------------------------------------------------------------------------------------- |
suhasini | 13:5d3b478ea9c7 | 129 | // unsigned char chk = 1; |
nikitateggi | 3:9a06c2bed650 | 130 | pc.baud(baud_rate); |
nikitateggi | 4:6bd81bb1790d | 131 | freqset(); // setting the frequency |
nikitateggi | 22:ffa88619551d | 132 | setupfunc(); |
nikitateggi | 22:ffa88619551d | 133 | //ecgtestsetupfunc(); // For test set up of 1Hz square wave signal |
nikitateggi | 22:ffa88619551d | 134 | lead_reg= ecgsetupfunc(); |
suhasini | 13:5d3b478ea9c7 | 135 | // chk = 1; |
nikitateggi | 22:ffa88619551d | 136 | |
nikitateggi | 22:ffa88619551d | 137 | |
nikitateggi | 22:ffa88619551d | 138 | //ORIGINAL sd_open_ECGfile(pid); // opening the ecg file COMMENTED Nidhin 1/6/2017 |
nikitateggi | 20:7c64e6ecad76 | 139 | if (lead_reg==0) // checking for proper lead contact// 14/06 |
nikitateggi | 22:ffa88619551d | 140 | { |
nidhinvarghese | 14:f5c62d30c6fc | 141 | sd_open_ECGfilee(pid); // REPLACED Nidhin 1/6/2017 Nidhin |
nidhinvarghese | 14:f5c62d30c6fc | 142 | |
suhasini | 13:5d3b478ea9c7 | 143 | pc.printf( "Raw data is = \n"); |
suhasini | 26:53ff13a164f3 | 144 | |
nikitateggi | 3:9a06c2bed650 | 145 | for(int i=0; i<N_ECG; i++) |
nikitateggi | 3:9a06c2bed650 | 146 | { |
nikitateggi | 3:9a06c2bed650 | 147 | concatenate_value2= readvalue(); |
nikitateggi | 22:ffa88619551d | 148 | pc.printf( "%d\n", concatenate_value2); //ADDED Nidhin 21/6/2017 |
nidhinvarghese | 14:f5c62d30c6fc | 149 | |
nidhinvarghese | 14:f5c62d30c6fc | 150 | sd_ecgwrite(ecg_ptr); // REPLACED Nidhin 1/6/2017 |
suhasini | 26:53ff13a164f3 | 151 | |
nikitateggi | 3:9a06c2bed650 | 152 | } |
nidhinvarghese | 14:f5c62d30c6fc | 153 | |
nidhinvarghese | 14:f5c62d30c6fc | 154 | //sd_close(); // closing the file COMMENTED Nidhin 1/6/2017 |
nidhinvarghese | 14:f5c62d30c6fc | 155 | sd_close_ecg(); // closing the ECG file REPLACED Nidhin 1/6/2017 Nidhin |
nikitateggi | 22:ffa88619551d | 156 | |
nikitateggi | 22:ffa88619551d | 157 | |
suhasini | 13:5d3b478ea9c7 | 158 | /* |
suhasini | 13:5d3b478ea9c7 | 159 | // ---------- reading back SD data for processing -------------------- |
suhasini | 13:5d3b478ea9c7 | 160 | sd_read_file(15); |
suhasini | 13:5d3b478ea9c7 | 161 | printf("Reading back SD data\n"); |
suhasini | 13:5d3b478ea9c7 | 162 | for(int i=10;i<N_ECG;i++) |
suhasini | 13:5d3b478ea9c7 | 163 | { |
suhasini | 13:5d3b478ea9c7 | 164 | ecg_buf= sd_read(); |
suhasini | 13:5d3b478ea9c7 | 165 | //printf("%d\n",ecg_buf[i]); |
suhasini | 13:5d3b478ea9c7 | 166 | } |
suhasini | 13:5d3b478ea9c7 | 167 | |
suhasini | 13:5d3b478ea9c7 | 168 | sd_close(); */ |
suhasini | 13:5d3b478ea9c7 | 169 | |
suhasini | 26:53ff13a164f3 | 170 | |
suhasini | 13:5d3b478ea9c7 | 171 | //----------------------------- PEAK DETECTION AND HEART RATE CALCULATION --------------------------------------------------- |
suhasini | 26:53ff13a164f3 | 172 | |
suhasini | 13:5d3b478ea9c7 | 173 | // -------------------------------------- PEAK DETECTION ------------------------------------------------------------- |
suhasini | 26:53ff13a164f3 | 174 | // ----------------------------------------- Main loop -------------------------------------------------- |
suhasini | 26:53ff13a164f3 | 175 | |
suhasini | 26:53ff13a164f3 | 176 | sprintf(buffer3, "/sd/%d_ECG.csv", pid); // For opening a specific file |
suhasini | 26:53ff13a164f3 | 177 | fpeecg1 = fopen(buffer3, "r"); |
suhasini | 13:5d3b478ea9c7 | 178 | |
suhasini | 26:53ff13a164f3 | 179 | for(uint16_t i=0;i<(N_ECG-10);i++){ |
suhasini | 26:53ff13a164f3 | 180 | count++; |
suhasini | 26:53ff13a164f3 | 181 | rewind(fpeecg1); // Go to start of file each time |
suhasini | 26:53ff13a164f3 | 182 | 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 |
suhasini | 26:53ff13a164f3 | 183 | fread(ecg_samp1, sizeof(uint32_t), 1, fpeecg1); // Read sample 1 |
suhasini | 26:53ff13a164f3 | 184 | fread(ecg_samp2, sizeof(uint32_t), 1, fpeecg1); // Read Sample 2 |
suhasini | 26:53ff13a164f3 | 185 | fseek(fpeecg1, samp_10, SEEK_CUR); // Moving to tenth sample |
suhasini | 26:53ff13a164f3 | 186 | fread(ecg_samp3, sizeof(uint32_t), 1, fpeecg1); // Read 3rd sample |
suhasini | 26:53ff13a164f3 | 187 | //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 |
suhasini | 26:53ff13a164f3 | 188 | |
suhasini | 26:53ff13a164f3 | 189 | |
suhasini | 26:53ff13a164f3 | 190 | if(ecg_samp1[0]>ecg_samp2[0]) |
suhasini | 13:5d3b478ea9c7 | 191 | { |
suhasini | 26:53ff13a164f3 | 192 | hi_val = ecg_samp1[0]; //To find the high value |
suhasini | 13:5d3b478ea9c7 | 193 | //printf("high value= %d\n",hi_val); |
suhasini | 26:53ff13a164f3 | 194 | //a = i; |
suhasini | 13:5d3b478ea9c7 | 195 | //printf("a= %d\n",a); |
suhasini | 26:53ff13a164f3 | 196 | hi_dif = hi_val-ecg_samp3[0]; |
suhasini | 13:5d3b478ea9c7 | 197 | |
suhasini | 26:53ff13a164f3 | 198 | // ---------------------------- If hi_val is greater than next ten input values, then compare the hi_val with the tenth input value. |
suhasini | 26:53ff13a164f3 | 199 | // If the diff is greater than 10000, then it is a valid peak (pls chk the below condition)------------------------------------- |
suhasini | 26:53ff13a164f3 | 200 | // if((hi_dif > 10000) && ((a+10) < N_ECG)) |
suhasini | 13:5d3b478ea9c7 | 201 | if(hi_dif > 10000) |
suhasini | 13:5d3b478ea9c7 | 202 | { |
suhasini | 13:5d3b478ea9c7 | 203 | // if(a_dif <= 0) ------------------------ add this condition if needed ---------------------- |
suhasini | 13:5d3b478ea9c7 | 204 | // { |
suhasini | 13:5d3b478ea9c7 | 205 | pk_val[j] = hi_val; //if condition satisfied, put the "pk" value into "pk_val" buffer |
nikitateggi | 22:ffa88619551d | 206 | pc.printf("peak value= %d\n",pk_val[j]); |
suhasini | 26:53ff13a164f3 | 207 | pk_pos[j]=i; // also save the peak's position |
nikitateggi | 22:ffa88619551d | 208 | pc.printf("peak position is = %d\n",pk_pos[j]); |
suhasini | 26:53ff13a164f3 | 209 | i = i+120; // once confirmed that this is the necessary peak, skip the next 120 input values |
suhasini | 13:5d3b478ea9c7 | 210 | n = j; // where n is the number of peaks detected |
suhasini | 13:5d3b478ea9c7 | 211 | j = j+1; |
suhasini | 26:53ff13a164f3 | 212 | 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 |
suhasini | 13:5d3b478ea9c7 | 213 | |
suhasini | 13:5d3b478ea9c7 | 214 | // printf("j after peak detection is= %d\n",j); |
suhasini | 13:5d3b478ea9c7 | 215 | // } |
suhasini | 13:5d3b478ea9c7 | 216 | } |
suhasini | 26:53ff13a164f3 | 217 | |
suhasini | 13:5d3b478ea9c7 | 218 | else |
suhasini | 13:5d3b478ea9c7 | 219 | { |
suhasini | 26:53ff13a164f3 | 220 | 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 |
suhasini | 13:5d3b478ea9c7 | 221 | } |
suhasini | 13:5d3b478ea9c7 | 222 | // store the peak value position in "pk_pos" |
suhasini | 26:53ff13a164f3 | 223 | } |
suhasini | 26:53ff13a164f3 | 224 | else |
suhasini | 26:53ff13a164f3 | 225 | { |
suhasini | 26:53ff13a164f3 | 226 | m = m+4; |
suhasini | 13:5d3b478ea9c7 | 227 | } |
suhasini | 26:53ff13a164f3 | 228 | //pc.printf("i=%d",i); |
suhasini | 13:5d3b478ea9c7 | 229 | } |
suhasini | 26:53ff13a164f3 | 230 | n=n+1; |
suhasini | 26:53ff13a164f3 | 231 | pc.printf("n=%d\n",n); |
suhasini | 26:53ff13a164f3 | 232 | |
suhasini | 13:5d3b478ea9c7 | 233 | // ----------------- HEART RATE LOGIC --------------------------- |
nikitateggi | 3:9a06c2bed650 | 234 | |
suhasini | 26:53ff13a164f3 | 235 | for(uint16_t i = 0;i < n-1;i++) |
suhasini | 13:5d3b478ea9c7 | 236 | { |
suhasini | 13:5d3b478ea9c7 | 237 | pos_dif = pk_pos[i+1] - pk_pos[i]; // difference between two consequtive peaks |
suhasini | 13:5d3b478ea9c7 | 238 | pc.printf("peak position diff is = %f\n",pos_dif); |
suhasini | 13:5d3b478ea9c7 | 239 | //printf("peak position i value is = %d\n",i); |
suhasini | 13:5d3b478ea9c7 | 240 | t_pos_dif = pos_dif/fs; // sample difference between peak positions divided by sampling frequency gives the difference value in terms of actual time |
suhasini | 13:5d3b478ea9c7 | 241 | pc.printf("time in seconds is = %f\n",t_pos_dif); |
suhasini | 13:5d3b478ea9c7 | 242 | HR[i] = t_sec/t_pos_dif; //HR calculation |
suhasini | 13:5d3b478ea9c7 | 243 | pc.printf("Heart Rate is = %f\n",HR[i]); |
suhasini | 13:5d3b478ea9c7 | 244 | // n = i; |
suhasini | 13:5d3b478ea9c7 | 245 | HR1 = HR[0]; |
suhasini | 26:53ff13a164f3 | 246 | } |
suhasini | 13:5d3b478ea9c7 | 247 | |
suhasini | 26:53ff13a164f3 | 248 | |
suhasini | 13:5d3b478ea9c7 | 249 | // ---------------------- To average individual HRs for higher number of samples ----------------------- |
suhasini | 26:53ff13a164f3 | 250 | for(uint16_t i = 0;i < n-1;i++) |
suhasini | 13:5d3b478ea9c7 | 251 | { |
suhasini | 13:5d3b478ea9c7 | 252 | HR_sum = HR[i]+HR_sum; |
suhasini | 13:5d3b478ea9c7 | 253 | } |
suhasini | 26:53ff13a164f3 | 254 | HR_avg = HR_sum/(n-1); // To find average of all the individual HRs calculated |
suhasini | 13:5d3b478ea9c7 | 255 | printf("Heart Rate sum is = %f\n",HR_sum); |
suhasini | 26:53ff13a164f3 | 256 | //printf("Denominator = %d\n",n); |
suhasini | 26:53ff13a164f3 | 257 | printf("Heart Rate avg is = %f\n",HR_avg); |
nidhinvarghese | 14:f5c62d30c6fc | 258 | |
suhasini | 26:53ff13a164f3 | 259 | fclose(fpeecg1); |
suhasini | 26:53ff13a164f3 | 260 | pc.printf("temporary file closed\n"); |
nikitateggi | 31:0f67acfc6236 | 261 | |
nikitateggi | 31:0f67acfc6236 | 262 | if(HR_avg>100 || HR_avg<40) |
nikitateggi | 31:0f67acfc6236 | 263 | { |
nikitateggi | 31:0f67acfc6236 | 264 | |
nikitateggi | 31:0f67acfc6236 | 265 | return 1; // out of range condition returning 1 //nikita//10/7 |
nikitateggi | 31:0f67acfc6236 | 266 | |
nikitateggi | 31:0f67acfc6236 | 267 | } |
nikitateggi | 31:0f67acfc6236 | 268 | else |
nikitateggi | 31:0f67acfc6236 | 269 | { |
nikitateggi | 31:0f67acfc6236 | 270 | |
nikitateggi | 31:0f67acfc6236 | 271 | BLEMsg_info_ecg.cal_data.cal_sbp_dummy = 0; |
nikitateggi | 31:0f67acfc6236 | 272 | BLEMsg_info_ecg.cal_data.cal_dbp_OTtyp = HR1; //To be modified after HR code is added. |
nidhinvarghese | 14:f5c62d30c6fc | 273 | |
nikitateggi | 31:0f67acfc6236 | 274 | structure_file(ptr_BLEMsg_info_ecg, pid); //copy the ECG structure to Main file //COMMENTED Nidhin 10/6/2017 |
nikitateggi | 31:0f67acfc6236 | 275 | ecgfile_mainfile(pid); // copy raw data to the main file and ECG file is cleared. //COMMENTED Nidhin 10/6/2017 |
nikitateggi | 22:ffa88619551d | 276 | |
nikitateggi | 31:0f67acfc6236 | 277 | pc.printf("Closed the main file\n"); |
nidhinvarghese | 14:f5c62d30c6fc | 278 | |
nidhinvarghese | 14:f5c62d30c6fc | 279 | |
nidhinvarghese | 14:f5c62d30c6fc | 280 | |
suhasini | 26:53ff13a164f3 | 281 | //return HR1; |
nikitateggi | 31:0f67acfc6236 | 282 | return HR_avg; |
nikitateggi | 31:0f67acfc6236 | 283 | } |
nikitateggi | 22:ffa88619551d | 284 | } |
nikitateggi | 20:7c64e6ecad76 | 285 | else |
nikitateggi | 20:7c64e6ecad76 | 286 | { |
nikitateggi | 31:0f67acfc6236 | 287 | pc.printf("improper lead connection"); |
nikitateggi | 20:7c64e6ecad76 | 288 | return 0; |
nikitateggi | 20:7c64e6ecad76 | 289 | } |
suhasini | 26:53ff13a164f3 | 290 | pc.printf("closing temporary file\n"); |
suhasini | 26:53ff13a164f3 | 291 | // fclose(fpeecg1); |
suhasini | 26:53ff13a164f3 | 292 | // pc.printf("temporary file closed\n"); |
nikitateggi | 22:ffa88619551d | 293 | } // End of main function |