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@54:f2a413d5dffd, 2017-09-27 (annotated)
- Committer:
- nikitateggi
- Date:
- Wed Sep 27 09:36:26 2017 +0000
- Revision:
- 54:f2a413d5dffd
- Parent:
- 51:1a4693774b60
- Child:
- 55:057f4af9d581
Modification in LPF and GLC
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" |
nikitateggi | 39:69b09bd87502 | 15 | #include "eeprom_pgm.h" |
nidhinvarghese | 14:f5c62d30c6fc | 16 | #include "struct.h" // Added on 31/5/17_Nidhin |
rashmivenkataramaiah | 43:85a7f399cb9d | 17 | #include "main.h" |
nikitateggi | 19:1650bbc923cc | 18 | //#include "test_type.h" //Added on 31/5/17_Nidhin |
avp2417 | 40:f7f351699288 | 19 | |
nikitateggi | 51:1a4693774b60 | 20 | void low_pass_filter(uint32_t pid); |
nikitateggi | 51:1a4693774b60 | 21 | void swap(uint32_t *ptr); |
nikitateggi | 51:1a4693774b60 | 22 | |
avp2417 | 40:f7f351699288 | 23 | #define MAX_HR_THRESHOLD 205 |
avp2417 | 40:f7f351699288 | 24 | #define MIN_HR_THRESHOLD 25 |
avp2417 | 40:f7f351699288 | 25 | |
avp2417 | 40:f7f351699288 | 26 | |
suhasini | 26:53ff13a164f3 | 27 | FILE *fpeecg1; // FILE pointer to ECG file |
nikitateggi | 3:9a06c2bed650 | 28 | Serial pc(USBTX,USBRX); |
nikitateggi | 1:8316c23ec6b9 | 29 | |
nikitateggi | 1:8316c23ec6b9 | 30 | |
rashmivenkataramaiah | 43:85a7f399cb9d | 31 | uint16_t ecg(uint32_t pid) |
nikitateggi | 22:ffa88619551d | 32 | //float ecg(int pid) |
nikitateggi | 3:9a06c2bed650 | 33 | { |
nikitateggi | 20:7c64e6ecad76 | 34 | |
nikitateggi | 20:7c64e6ecad76 | 35 | uint8_t lead_reg=0; // added on 14/06 to check lead -off |
nikitateggi | 51:1a4693774b60 | 36 | FILE *fpeecg1; |
nikitateggi | 51:1a4693774b60 | 37 | pc.baud(baud_rate); |
nidhinvarghese | 14:f5c62d30c6fc | 38 | //----------------------- Structure for Bluetooth Added Nidhin 1/6/2017-------------------// |
nidhinvarghese | 14:f5c62d30c6fc | 39 | |
nidhinvarghese | 14:f5c62d30c6fc | 40 | //BLUETOOTH STRUCTURE |
nidhinvarghese | 14:f5c62d30c6fc | 41 | |
nidhinvarghese | 14:f5c62d30c6fc | 42 | 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 | 43 | ptr_BLEMsg_info_ecg = &BLEMsg_info_ecg; // *ptr_BLEMsg_info_bp is the pointer to local copy; |
nidhinvarghese | 14:f5c62d30c6fc | 44 | |
nidhinvarghese | 14:f5c62d30c6fc | 45 | // Declaration of Date Structure |
nidhinvarghese | 14:f5c62d30c6fc | 46 | DateTime_info *ptr_DateTime_info_ecg, DateTime_info_ecg; // A copy of Master Structure "DateTime_info" created, |
nidhinvarghese | 14:f5c62d30c6fc | 47 | ptr_DateTime_info_ecg = &DateTime_info_ecg; // Structure pointer points to that copy. |
nidhinvarghese | 14:f5c62d30c6fc | 48 | |
nidhinvarghese | 14:f5c62d30c6fc | 49 | |
nidhinvarghese | 14:f5c62d30c6fc | 50 | // RTC operations |
nidhinvarghese | 14:f5c62d30c6fc | 51 | time_t epoch_time_ecg; //A copy of time_t by name epoch_time_bp is created |
nidhinvarghese | 14:f5c62d30c6fc | 52 | epoch_time_ecg = rtc_read(); // time is got from get epoch function. |
nidhinvarghese | 14:f5c62d30c6fc | 53 | |
nidhinvarghese | 14:f5c62d30c6fc | 54 | struct tm * ptr_time_info_ecg, time_info_ecg; // Sturucture copy of tm is created |
nidhinvarghese | 14:f5c62d30c6fc | 55 | ptr_time_info_ecg = localtime(&epoch_time_ecg); // Structure accepts the time in local format from "time_t" type. |
nidhinvarghese | 14:f5c62d30c6fc | 56 | |
nidhinvarghese | 14:f5c62d30c6fc | 57 | //BELOW LINE IS TO CHECK Date and TIME |
nidhinvarghese | 15:208b146151ba | 58 | //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 | 59 | //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 | 60 | |
nidhinvarghese | 14:f5c62d30c6fc | 61 | //Copying from one structure to the other using variables |
nidhinvarghese | 14:f5c62d30c6fc | 62 | DateTime_info_ecg.hour = (uint8_t)(*ptr_time_info_ecg).tm_hour; |
nidhinvarghese | 14:f5c62d30c6fc | 63 | DateTime_info_ecg.mins = (uint8_t)(*ptr_time_info_ecg).tm_min; |
nidhinvarghese | 14:f5c62d30c6fc | 64 | DateTime_info_ecg.sec = (uint8_t)(*ptr_time_info_ecg).tm_sec; |
nidhinvarghese | 14:f5c62d30c6fc | 65 | |
nidhinvarghese | 14:f5c62d30c6fc | 66 | DateTime_info_ecg.date = (uint8_t) (*ptr_time_info_ecg).tm_mday; |
nidhinvarghese | 14:f5c62d30c6fc | 67 | DateTime_info_ecg.month =(uint8_t)(*ptr_time_info_ecg).tm_mon+1; |
nidhinvarghese | 14:f5c62d30c6fc | 68 | DateTime_info_ecg.year = (uint8_t)(*ptr_time_info_ecg).tm_year-100; |
nidhinvarghese | 14:f5c62d30c6fc | 69 | |
nidhinvarghese | 14:f5c62d30c6fc | 70 | // Copying Time to Main structure |
nidhinvarghese | 14:f5c62d30c6fc | 71 | BLEMsg_info_ecg.date_time.hour = DateTime_info_ecg.hour; |
nidhinvarghese | 14:f5c62d30c6fc | 72 | BLEMsg_info_ecg.date_time.mins = DateTime_info_ecg.mins; |
nidhinvarghese | 14:f5c62d30c6fc | 73 | BLEMsg_info_ecg.date_time.sec = DateTime_info_ecg.sec; |
nidhinvarghese | 14:f5c62d30c6fc | 74 | |
nidhinvarghese | 14:f5c62d30c6fc | 75 | BLEMsg_info_ecg.date_time.date = DateTime_info_ecg.date ; |
nidhinvarghese | 14:f5c62d30c6fc | 76 | BLEMsg_info_ecg.date_time.month = DateTime_info_ecg.month ; |
nidhinvarghese | 14:f5c62d30c6fc | 77 | BLEMsg_info_ecg.date_time.year = DateTime_info_ecg.year ; |
nidhinvarghese | 14:f5c62d30c6fc | 78 | |
nidhinvarghese | 14:f5c62d30c6fc | 79 | |
nidhinvarghese | 14:f5c62d30c6fc | 80 | //Checking if the structure has these values |
nidhinvarghese | 15:208b146151ba | 81 | //pc.printf("Time 2 is %d:%d:%d\n", DateTime_info_ecg.hour, DateTime_info_ecg.mins, DateTime_info_ecg.sec); |
nidhinvarghese | 15:208b146151ba | 82 | //pc.printf("\t Date is %d:%d:%d\n",DateTime_info_ecg.date, DateTime_info_ecg.month, DateTime_info_ecg.year); |
nidhinvarghese | 14:f5c62d30c6fc | 83 | |
nidhinvarghese | 14:f5c62d30c6fc | 84 | |
nidhinvarghese | 14:f5c62d30c6fc | 85 | //Loading values to of Test type |
nidhinvarghese | 14:f5c62d30c6fc | 86 | test_type_info test_type_info_ecg; // copy of " test_type_info" created |
nidhinvarghese | 14:f5c62d30c6fc | 87 | test_type_info_ecg = ECG_Test; // Loaded value 00 to the test type |
nidhinvarghese | 14:f5c62d30c6fc | 88 | |
nidhinvarghese | 14:f5c62d30c6fc | 89 | BLEMsg_info_ecg.test_type = test_type_info_ecg; |
nidhinvarghese | 14:f5c62d30c6fc | 90 | //Check if 00 is getting printed |
nidhinvarghese | 15:208b146151ba | 91 | //pc.printf("Test Type is : %d\n", test_type_info_ecg); |
nidhinvarghese | 14:f5c62d30c6fc | 92 | |
nidhinvarghese | 14:f5c62d30c6fc | 93 | |
nidhinvarghese | 14:f5c62d30c6fc | 94 | // Loading values of Length , PID, DID, sampling frequency, number of samples, calculated data. |
nikitateggi | 39:69b09bd87502 | 95 | BLEMsg_info_ecg.device_id = eprom_read_8(12); // Device ID fixed |
nidhinvarghese | 14:f5c62d30c6fc | 96 | BLEMsg_info_ecg.patient_id = (uint32_t)pid; // Patient ID |
nidhinvarghese | 14:f5c62d30c6fc | 97 | BLEMsg_info_ecg.sampling_freq = 500; // sampling frrquency |
nikitateggi | 32:76892fdf5e4c | 98 | BLEMsg_info_ecg.length = 8022; //Total length of data in bytes 22 B+ 4000 data |
nidhinvarghese | 14:f5c62d30c6fc | 99 | |
nidhinvarghese | 14:f5c62d30c6fc | 100 | /* |
nidhinvarghese | 14:f5c62d30c6fc | 101 | //Loading number of samples |
nidhinvarghese | 14:f5c62d30c6fc | 102 | NumSamples_info NumSamples_info_bp; //Copy of structure NumSamples_info |
nidhinvarghese | 14:f5c62d30c6fc | 103 | NumSamples_info_bp.num_ppg_sample = 1664; // PPG & ECG Sample number loaded in structure copy |
nidhinvarghese | 14:f5c62d30c6fc | 104 | NumSamples_info_bp.num_ecg_sample = 1024; |
nidhinvarghese | 14:f5c62d30c6fc | 105 | */ |
nidhinvarghese | 14:f5c62d30c6fc | 106 | |
nidhinvarghese | 14:f5c62d30c6fc | 107 | BLEMsg_info_ecg.num_samples.num_sample_ppg_dummy = 0 ;// PPG number of samples copied to master struct |
nikitateggi | 32:76892fdf5e4c | 108 | BLEMsg_info_ecg.num_samples.num_sample_ecg_OTtyp = 2000 ; // ECG number of samples copied to master struct |
nidhinvarghese | 14:f5c62d30c6fc | 109 | |
nidhinvarghese | 14:f5c62d30c6fc | 110 | //----------------------------------------END Structure for Bluetooth - Added Nidhin 1/6/2017------- |
nidhinvarghese | 14:f5c62d30c6fc | 111 | |
suhasini | 26:53ff13a164f3 | 112 | uint32_t concatenate_value2 = 0; // ORG. "int concatenate_value2 = 0;" Nidhin 1/6/17 |
suhasini | 26:53ff13a164f3 | 113 | uint32_t *ecg_ptr; // Added 1/6/2017 Nidhin |
nidhinvarghese | 14:f5c62d30c6fc | 114 | ecg_ptr = &concatenate_value2; // Pointer to pass for ECG write into SD card Nidhin 1/6/2017 |
nidhinvarghese | 14:f5c62d30c6fc | 115 | |
suhasini | 26:53ff13a164f3 | 116 | uint16_t count = 0; |
suhasini | 26:53ff13a164f3 | 117 | uint16_t fs = 500; |
suhasini | 26:53ff13a164f3 | 118 | //uint32_t ecg_buf[N_ECG]; |
suhasini | 26:53ff13a164f3 | 119 | Timer t; |
suhasini | 13:5d3b478ea9c7 | 120 | |
nikitateggi | 51:1a4693774b60 | 121 | /********** Declaration for Peak value detection ***************/ |
nikitateggi | 51:1a4693774b60 | 122 | |
nikitateggi | 51:1a4693774b60 | 123 | 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 |
nikitateggi | 51:1a4693774b60 | 124 | int32_t ecg_samp2[1]; //changed from uint32_t to static double- by Suhasini- 5-8-17 |
nikitateggi | 51:1a4693774b60 | 125 | int32_t ecg_samp3[1]; // to Stores sample 1, 2 & 10th sample //changed from uint32_t to static double- by Suhasini- 5-8-17 |
nikitateggi | 51:1a4693774b60 | 126 | |
nikitateggi | 51:1a4693774b60 | 127 | |
nikitateggi | 51:1a4693774b60 | 128 | uint32_t samp_10 = 28; |
nikitateggi | 51:1a4693774b60 | 129 | static int32_t hi_val; //changed from uint32_t to static double- by Suhasini- 5-8-17 |
nikitateggi | 51:1a4693774b60 | 130 | int32_t pk_val[20]; //changed from uint32_t to double - Suhasini - 5-8-17 |
nikitateggi | 51:1a4693774b60 | 131 | 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 |
nikitateggi | 51:1a4693774b60 | 132 | uint16_t pk_pos[20]= {0}; // Variable to hold pointer positio |
nikitateggi | 51:1a4693774b60 | 133 | uint8_t j=0; // int count1 = N_ECG/fs, a_dif=0, fs1 = fs ,h=0; |
nikitateggi | 51:1a4693774b60 | 134 | uint32_t m = 400; |
suhasini | 26:53ff13a164f3 | 135 | |
suhasini | 13:5d3b478ea9c7 | 136 | // ------------------------- Declaration for Heart Rate calculation -------------------------- |
nikitateggi | 51:1a4693774b60 | 137 | uint8_t n=0; |
nikitateggi | 54:f2a413d5dffd | 138 | uint16_t pos_dif; // changed from float to uint16_t- Suhasini- 8-8-17, changed back to float- 17th aug,2017 |
nikitateggi | 51:1a4693774b60 | 139 | float HR[10],HR1,t_pos_dif; |
nikitateggi | 51:1a4693774b60 | 140 | uint8_t t_sec = 60; |
nikitateggi | 51:1a4693774b60 | 141 | float HR_sum = 0,HR_avg; |
nikitateggi | 51:1a4693774b60 | 142 | char buffer4[32] = {0}; |
nikitateggi | 51:1a4693774b60 | 143 | |
nikitateggi | 51:1a4693774b60 | 144 | |
nikitateggi | 51:1a4693774b60 | 145 | freqset(); // setting the frequency |
nikitateggi | 51:1a4693774b60 | 146 | setupfunc(); |
nikitateggi | 51:1a4693774b60 | 147 | lead_reg = ecgsetupfunc(); |
nikitateggi | 22:ffa88619551d | 148 | |
nikitateggi | 51:1a4693774b60 | 149 | |
nikitateggi | 51:1a4693774b60 | 150 | if (lead_reg==0) |
nikitateggi | 51:1a4693774b60 | 151 | { // checking for proper lead contact |
nikitateggi | 51:1a4693774b60 | 152 | |
nikitateggi | 51:1a4693774b60 | 153 | sd_open_ECGfilee(pid); //open temporary ecg.csv file to write 2000 samples of data |
nikitateggi | 51:1a4693774b60 | 154 | pc.printf( "Raw data is = \n"); |
nikitateggi | 51:1a4693774b60 | 155 | |
nikitateggi | 51:1a4693774b60 | 156 | for(int i=0; i<N_ECG; i++) { |
nikitateggi | 51:1a4693774b60 | 157 | concatenate_value2= readvalue(); |
nikitateggi | 51:1a4693774b60 | 158 | pc.printf( "%d\n", concatenate_value2); |
nikitateggi | 51:1a4693774b60 | 159 | |
nikitateggi | 51:1a4693774b60 | 160 | sd_ecgwrite(ecg_ptr); //writing data to temporary file ECG.csv |
nikitateggi | 51:1a4693774b60 | 161 | } |
nikitateggi | 51:1a4693774b60 | 162 | sd_close_ecg(); // closing the ECG file REPLACED Nidhin 1/6/2017 Nidhin |
nikitateggi | 51:1a4693774b60 | 163 | |
nikitateggi | 51:1a4693774b60 | 164 | low_pass_filter(pid); // low pass filter calculation function |
nikitateggi | 51:1a4693774b60 | 165 | |
nikitateggi | 22:ffa88619551d | 166 | |
suhasini | 13:5d3b478ea9c7 | 167 | //----------------------------- PEAK DETECTION AND HEART RATE CALCULATION --------------------------------------------------- |
suhasini | 26:53ff13a164f3 | 168 | |
suhasini | 13:5d3b478ea9c7 | 169 | // -------------------------------------- PEAK DETECTION ------------------------------------------------------------- |
suhasini | 26:53ff13a164f3 | 170 | // ----------------------------------------- Main loop -------------------------------------------------- |
suhasini | 26:53ff13a164f3 | 171 | |
nikitateggi | 51:1a4693774b60 | 172 | sprintf(buffer4, "/sd/%d_ECG_lpf.csv", pid); // For opening a specific file |
nikitateggi | 51:1a4693774b60 | 173 | fpeecg1 = fopen(buffer4, "r"); |
suhasini | 13:5d3b478ea9c7 | 174 | |
nikitateggi | 51:1a4693774b60 | 175 | for(uint16_t i=0;i<((N_ECG-4)-10);i++){ |
suhasini | 26:53ff13a164f3 | 176 | count++; |
suhasini | 26:53ff13a164f3 | 177 | rewind(fpeecg1); // Go to start of file each time |
suhasini | 26:53ff13a164f3 | 178 | 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 |
nikitateggi | 51:1a4693774b60 | 179 | fread(ecg_samp1, sizeof(int32_t), 1, fpeecg1); // Read sample 1 |
nikitateggi | 51:1a4693774b60 | 180 | fread(ecg_samp2, sizeof(int32_t), 1, fpeecg1); // Read Sample 2 |
suhasini | 26:53ff13a164f3 | 181 | fseek(fpeecg1, samp_10, SEEK_CUR); // Moving to tenth sample |
nikitateggi | 51:1a4693774b60 | 182 | fread(ecg_samp3, sizeof(int32_t), 1, fpeecg1); // Read 3rd sample |
suhasini | 26:53ff13a164f3 | 183 | |
suhasini | 26:53ff13a164f3 | 184 | if(ecg_samp1[0]>ecg_samp2[0]) |
suhasini | 13:5d3b478ea9c7 | 185 | { |
suhasini | 26:53ff13a164f3 | 186 | hi_val = ecg_samp1[0]; //To find the high value |
suhasini | 26:53ff13a164f3 | 187 | hi_dif = hi_val-ecg_samp3[0]; |
suhasini | 13:5d3b478ea9c7 | 188 | |
suhasini | 26:53ff13a164f3 | 189 | // ---------------------------- If hi_val is greater than next ten input values, then compare the hi_val with the tenth input value. |
nikitateggi | 51:1a4693774b60 | 190 | // If the diff is greater than 4000, then it is a valid peak (pls chk the below condition)------------------------------------- |
nikitateggi | 51:1a4693774b60 | 191 | if(hi_dif > 4000) |
suhasini | 13:5d3b478ea9c7 | 192 | { |
suhasini | 13:5d3b478ea9c7 | 193 | pk_val[j] = hi_val; //if condition satisfied, put the "pk" value into "pk_val" buffer |
nikitateggi | 22:ffa88619551d | 194 | pc.printf("peak value= %d\n",pk_val[j]); |
suhasini | 26:53ff13a164f3 | 195 | pk_pos[j]=i; // also save the peak's position |
nikitateggi | 22:ffa88619551d | 196 | pc.printf("peak position is = %d\n",pk_pos[j]); |
suhasini | 26:53ff13a164f3 | 197 | i = i+120; // once confirmed that this is the necessary peak, skip the next 120 input values |
suhasini | 13:5d3b478ea9c7 | 198 | n = j; // where n is the number of peaks detected |
suhasini | 13:5d3b478ea9c7 | 199 | j = j+1; |
suhasini | 26:53ff13a164f3 | 200 | 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 | 201 | |
avp2417 | 36:00d96aa14658 | 202 | } |
suhasini | 26:53ff13a164f3 | 203 | |
suhasini | 13:5d3b478ea9c7 | 204 | else |
suhasini | 13:5d3b478ea9c7 | 205 | { |
suhasini | 26:53ff13a164f3 | 206 | 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 | 207 | } |
avp2417 | 36:00d96aa14658 | 208 | } |
suhasini | 26:53ff13a164f3 | 209 | else |
suhasini | 26:53ff13a164f3 | 210 | { |
suhasini | 26:53ff13a164f3 | 211 | m = m+4; |
suhasini | 13:5d3b478ea9c7 | 212 | } |
suhasini | 26:53ff13a164f3 | 213 | //pc.printf("i=%d",i); |
suhasini | 13:5d3b478ea9c7 | 214 | } |
suhasini | 26:53ff13a164f3 | 215 | n=n+1; |
suhasini | 26:53ff13a164f3 | 216 | pc.printf("n=%d\n",n); |
suhasini | 26:53ff13a164f3 | 217 | |
nikitateggi | 51:1a4693774b60 | 218 | pc.printf("to enter HR loop\n"); |
suhasini | 13:5d3b478ea9c7 | 219 | // ----------------- HEART RATE LOGIC --------------------------- |
nikitateggi | 3:9a06c2bed650 | 220 | |
suhasini | 26:53ff13a164f3 | 221 | for(uint16_t i = 0;i < n-1;i++) |
suhasini | 13:5d3b478ea9c7 | 222 | { |
suhasini | 13:5d3b478ea9c7 | 223 | pos_dif = pk_pos[i+1] - pk_pos[i]; // difference between two consequtive peaks |
nikitateggi | 51:1a4693774b60 | 224 | pc.printf("peak position diff is = %d\n",pos_dif); |
nikitateggi | 54:f2a413d5dffd | 225 | t_pos_dif = (float)pos_dif/fs; // sample difference between peak positions divided by sampling frequency gives the difference value in terms of actual time |
suhasini | 13:5d3b478ea9c7 | 226 | pc.printf("time in seconds is = %f\n",t_pos_dif); |
suhasini | 13:5d3b478ea9c7 | 227 | HR[i] = t_sec/t_pos_dif; //HR calculation |
suhasini | 13:5d3b478ea9c7 | 228 | pc.printf("Heart Rate is = %f\n",HR[i]); |
suhasini | 13:5d3b478ea9c7 | 229 | HR1 = HR[0]; |
suhasini | 26:53ff13a164f3 | 230 | } |
suhasini | 13:5d3b478ea9c7 | 231 | |
suhasini | 26:53ff13a164f3 | 232 | |
suhasini | 13:5d3b478ea9c7 | 233 | // ---------------------- To average individual HRs for higher number of samples ----------------------- |
suhasini | 26:53ff13a164f3 | 234 | for(uint16_t i = 0;i < n-1;i++) |
suhasini | 13:5d3b478ea9c7 | 235 | { |
avp2417 | 40:f7f351699288 | 236 | HR_sum = HR[i] + HR_sum; |
suhasini | 13:5d3b478ea9c7 | 237 | } |
avp2417 | 40:f7f351699288 | 238 | HR_avg = (HR_sum / (n-1)); // To find average of all the individual HRs calculated |
suhasini | 13:5d3b478ea9c7 | 239 | printf("Heart Rate sum is = %f\n",HR_sum); |
suhasini | 26:53ff13a164f3 | 240 | printf("Heart Rate avg is = %f\n",HR_avg); |
nidhinvarghese | 14:f5c62d30c6fc | 241 | |
suhasini | 26:53ff13a164f3 | 242 | fclose(fpeecg1); |
suhasini | 26:53ff13a164f3 | 243 | pc.printf("temporary file closed\n"); |
nikitateggi | 31:0f67acfc6236 | 244 | |
avp2417 | 40:f7f351699288 | 245 | if(HR_avg > MAX_HR_THRESHOLD || HR_avg < MIN_HR_THRESHOLD) |
nikitateggi | 31:0f67acfc6236 | 246 | { |
nikitateggi | 39:69b09bd87502 | 247 | delete_subfiles(pid); // added on 11/8/17 nikita |
nikitateggi | 31:0f67acfc6236 | 248 | return 1; // out of range condition returning 1 //nikita//10/7 |
nikitateggi | 31:0f67acfc6236 | 249 | } |
nikitateggi | 31:0f67acfc6236 | 250 | else |
nikitateggi | 31:0f67acfc6236 | 251 | { |
nikitateggi | 31:0f67acfc6236 | 252 | |
nikitateggi | 31:0f67acfc6236 | 253 | BLEMsg_info_ecg.cal_data.cal_sbp_dummy = 0; |
rashmivenkataramaiah | 41:1141a75eacc4 | 254 | BLEMsg_info_ecg.cal_data.cal_dbp_OTtyp = HR_avg; //To be modified after HR code is added. |
nidhinvarghese | 14:f5c62d30c6fc | 255 | |
rashmivenkataramaiah | 41:1141a75eacc4 | 256 | structure_file(ptr_BLEMsg_info_ecg, pid); //copy the ECG structure to Main file //COMMENTED Nidhin 10/6/2017 |
rashmivenkataramaiah | 41:1141a75eacc4 | 257 | ecgfile_mainfile(pid); // copy raw data to the main file and ECG file is cleared. //COMMENTED Nidhin 10/6/2017 |
rashmivenkataramaiah | 43:85a7f399cb9d | 258 | if(get_filecreated_status() == false) //if file is in write mode |
rashmivenkataramaiah | 43:85a7f399cb9d | 259 | { |
rashmivenkataramaiah | 43:85a7f399cb9d | 260 | set_filecreated_status(); |
rashmivenkataramaiah | 43:85a7f399cb9d | 261 | increment_filepid (); |
rashmivenkataramaiah | 43:85a7f399cb9d | 262 | } |
nikitateggi | 31:0f67acfc6236 | 263 | pc.printf("Closed the main file\n"); |
nikitateggi | 31:0f67acfc6236 | 264 | return HR_avg; |
nikitateggi | 31:0f67acfc6236 | 265 | } |
nikitateggi | 22:ffa88619551d | 266 | } |
nikitateggi | 20:7c64e6ecad76 | 267 | else |
nikitateggi | 20:7c64e6ecad76 | 268 | { |
nikitateggi | 31:0f67acfc6236 | 269 | pc.printf("improper lead connection"); |
avp2417 | 36:00d96aa14658 | 270 | return 0; |
nikitateggi | 20:7c64e6ecad76 | 271 | } |
suhasini | 26:53ff13a164f3 | 272 | pc.printf("closing temporary file\n"); |
nikitateggi | 51:1a4693774b60 | 273 | } // End of main function |
nikitateggi | 51:1a4693774b60 | 274 | /************************* LOW PASS FILTER FUNCTION **************************/ |
nikitateggi | 51:1a4693774b60 | 275 | |
nikitateggi | 51:1a4693774b60 | 276 | void low_pass_filter(uint32_t pid) |
nikitateggi | 51:1a4693774b60 | 277 | { |
nikitateggi | 51:1a4693774b60 | 278 | FILE *fpecg1; // FILE pointer to read raw data ECG file added by Suhasini 5-8-17 |
nikitateggi | 51:1a4693774b60 | 279 | FILE *fpeecg_lpf; // FILE pointer to read and write into LPF output file added by Suhasini 5-8-17 |
nikitateggi | 51:1a4693774b60 | 280 | static uint32_t ecg_raw_temp[5]; // changed by rohit on 13 sep |
nikitateggi | 54:f2a413d5dffd | 281 | uint32_t ecg_raw[5]; // Buff of 5 samples created to Stores input samples for LPF added by Suhasini - 5-8-17 added by rohit |
nikitateggi | 54:f2a413d5dffd | 282 | 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 added by rohit |
nikitateggi | 51:1a4693774b60 | 283 | |
nikitateggi | 51:1a4693774b60 | 284 | uint32_t lpf_4samp = 0; // Variable to move 16 samples back from current in input file - changed by Suhasini- 5-8-17 |
nikitateggi | 51:1a4693774b60 | 285 | char buffer3[32]; |
nikitateggi | 51:1a4693774b60 | 286 | char buffer4[32]; // Added by Suhasini - 5-8-17 |
nikitateggi | 51:1a4693774b60 | 287 | 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 |
nikitateggi | 51:1a4693774b60 | 288 | 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 |
nikitateggi | 51:1a4693774b60 | 289 | |
nikitateggi | 51:1a4693774b60 | 290 | |
nikitateggi | 51:1a4693774b60 | 291 | |
nikitateggi | 51:1a4693774b60 | 292 | sprintf(buffer3, "/sd/%d_ECG.csv", pid); // opening raw data file to read |
nikitateggi | 51:1a4693774b60 | 293 | fpecg1 = fopen(buffer3, "r"); // to read data from the above opened temp file |
nikitateggi | 51:1a4693774b60 | 294 | pc.printf("entered raw data file\n"); |
nikitateggi | 51:1a4693774b60 | 295 | sprintf(buffer4, "/sd/%d_ECG_lpf.csv", pid); // opening empty file to write lpf data |
nikitateggi | 51:1a4693774b60 | 296 | fpeecg_lpf = fopen(buffer4, "a"); // to read data from and write data into the above opened file // changed from "a+" to "a" |
nikitateggi | 51:1a4693774b60 | 297 | pc.printf("entered LPF data file\n"); |
nikitateggi | 51:1a4693774b60 | 298 | |
nikitateggi | 51:1a4693774b60 | 299 | fwrite(&lpf_op, sizeof(int32_t), 4, fpeecg_lpf); |
nikitateggi | 51:1a4693774b60 | 300 | 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]); |
nikitateggi | 51:1a4693774b60 | 301 | |
nikitateggi | 51:1a4693774b60 | 302 | pc.printf("low pass filter output is= %d\n",lpf_op[4]); //message to display LPF output |
nikitateggi | 51:1a4693774b60 | 303 | |
nikitateggi | 51:1a4693774b60 | 304 | fread(&ecg_raw, sizeof(uint32_t), 5, fpecg1); |
nikitateggi | 51:1a4693774b60 | 305 | // 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]); |
nikitateggi | 51:1a4693774b60 | 306 | |
nikitateggi | 51:1a4693774b60 | 307 | for (uint16_t i = 1; i < (N_ECG-4); i++) { // reduced loop length to check i/p and o/p timing |
nikitateggi | 51:1a4693774b60 | 308 | //fseek(fpecg1, lpf_4samp, SEEK_CUR); // Update the count value according to move pointer |
nikitateggi | 51:1a4693774b60 | 309 | // fread(&ecg_raw, sizeof(uint32_t), 5, fpecg1); // Read sample 1 of i/p |
nikitateggi | 51:1a4693774b60 | 310 | |
nikitateggi | 51:1a4693774b60 | 311 | /**********rohit***************/ |
nikitateggi | 51:1a4693774b60 | 312 | //fread(&ecg_raw_temp, sizeof(uint32_t), 5, fpecg1); |
nikitateggi | 51:1a4693774b60 | 313 | //memcpy(ecg_raw, ecg_raw_temp,sizeof(ecg_raw)); |
nikitateggi | 51:1a4693774b60 | 314 | |
nikitateggi | 51:1a4693774b60 | 315 | // fread(ecg_raw, sizeof(uint32_t), 5, fpecg1); |
nikitateggi | 51:1a4693774b60 | 316 | |
nikitateggi | 51:1a4693774b60 | 317 | /*********** filter output for the ECG data for a low-pass butter-worth filter of order=4 is as follows******************/ |
nikitateggi | 51:1a4693774b60 | 318 | |
nikitateggi | 51:1a4693774b60 | 319 | |
nikitateggi | 51:1a4693774b60 | 320 | |
nikitateggi | 51:1a4693774b60 | 321 | (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]); |
nikitateggi | 51:1a4693774b60 | 322 | |
nikitateggi | 51:1a4693774b60 | 323 | |
nikitateggi | 51:1a4693774b60 | 324 | fwrite(&lpf_op[4], sizeof(int32_t), 1, fpeecg_lpf); |
nikitateggi | 51:1a4693774b60 | 325 | |
nikitateggi | 51:1a4693774b60 | 326 | //pc.printf("pointer to output value is= %ld\n",ftell(fpeecg_lpf)); |
nikitateggi | 51:1a4693774b60 | 327 | pc.printf("%d\n",lpf_op[4]); // to print LPF output on hyperterminal |
nikitateggi | 51:1a4693774b60 | 328 | swap(lpf_op); |
nikitateggi | 51:1a4693774b60 | 329 | /* |
nikitateggi | 51:1a4693774b60 | 330 | lpf_op[0] = lpf_op[1]; |
nikitateggi | 51:1a4693774b60 | 331 | lpf_op[1] = lpf_op[2]; |
nikitateggi | 51:1a4693774b60 | 332 | lpf_op[2] = lpf_op[3]; |
nikitateggi | 51:1a4693774b60 | 333 | lpf_op[3] = lpf_op[4]; |
nikitateggi | 51:1a4693774b60 | 334 | */ |
nikitateggi | 51:1a4693774b60 | 335 | // rewind(fpecg1); commented by rohit |
nikitateggi | 51:1a4693774b60 | 336 | // rewind(fpeecg_lpf); // commented by nikita 5/09/17 // no need to rewind as single array member result is stored in the file |
nikitateggi | 51:1a4693774b60 | 337 | // lpf_4samp = (i*4); // commented by rohit |
nikitateggi | 51:1a4693774b60 | 338 | |
nikitateggi | 51:1a4693774b60 | 339 | // lpfop_4samp = (i*4); |
nikitateggi | 51:1a4693774b60 | 340 | 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// |
nikitateggi | 51:1a4693774b60 | 341 | fread(&ecg_raw[4], sizeof(uint32_t), 1, fpecg1); |
nikitateggi | 51:1a4693774b60 | 342 | pc.printf("5th samples: %d\n",ecg_raw[4]); |
nikitateggi | 51:1a4693774b60 | 343 | |
nikitateggi | 51:1a4693774b60 | 344 | //fseek(fpecg1, -16 , SEEK_CUR); |
nikitateggi | 51:1a4693774b60 | 345 | // memset(ecg_raw,0,sizeof(ecg_raw)); |
nikitateggi | 51:1a4693774b60 | 346 | } |
nikitateggi | 51:1a4693774b60 | 347 | |
nikitateggi | 51:1a4693774b60 | 348 | |
nikitateggi | 51:1a4693774b60 | 349 | fclose(fpecg1); |
nikitateggi | 51:1a4693774b60 | 350 | fclose(fpeecg_lpf); |
nikitateggi | 51:1a4693774b60 | 351 | pc.printf("temporary file closed after LPF\n"); |
nikitateggi | 51:1a4693774b60 | 352 | |
nikitateggi | 51:1a4693774b60 | 353 | } |
nikitateggi | 51:1a4693774b60 | 354 | |
nikitateggi | 51:1a4693774b60 | 355 | void swap(uint32_t *ptr) |
nikitateggi | 51:1a4693774b60 | 356 | { |
nikitateggi | 51:1a4693774b60 | 357 | int i; |
nikitateggi | 51:1a4693774b60 | 358 | for(i=0;i<4;i++) |
nikitateggi | 51:1a4693774b60 | 359 | ptr[i] = ptr[i+1]; |
nikitateggi | 51:1a4693774b60 | 360 | } |