#include <stdio.h> #include <time.h> #include <stdlib.h> #include <math.h> //declare variables double bpm, ewma, ewma_prev, average , raw, alpha, stored_bpm, D1, D2, read_pin, ewma_comp; int t_int; clock_t time1,time2; double t_old, t_new; //////SIMULATED SIGNAL READING DATA///// double signal_read[] = {1107,1263,1304,1286,2928,2042,1256,1603,1243,1152,1240,1246,1259,2676,2853,1256,1486,1339,1141,1219,1241,1250,2008,2927,1351,1415,1466,1181,1164,1224,1225,2021,2667,1211,1313,1328,1078,1105,1261,1294,2839,2190,1232,1553,1297,1119,1208,1275,1902,2926,1373,1428,1482,1145,1180,1256,1280,2927,1909,1131,1428,1129,985,1162,1233,1778,2669,1355,1362,1500,1181,1192,1295,1860,2925,1345,1421,1531,1163,1216,1298,1330,2927,1998,1246,1514,1190,1077,1221,1266,2223,2925,1293,1412,1357,1035,1096,1187,1204,2929,1701,1096,1501,1216,1124,1326,1355,2913,2004,1084,1563,1321,1139,1225,1286,2753,2790,1159,1504,1307,1068,1189,1295,1739,2939,2934,2944,2938,2938,2938 }; //////SIMULATED TIME READING DATA///// double time_read[] = {0,100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000,3100,3200,3300,3400,3500,3600,3700,3800,3900,4000,4100,4200,4300,4400,4500,4600,4700,4800,4900,5000,5100,5200,5300,5400,5500,5600,5700,5800,5900,6000,6100,6200,6300,6400,6500,6600,6700,6800,6900,7000,7100,7200,7300,7400,7500,7600,7700,7800,7900,8000,8100,8200,8300,8400,8500,8600,8700,8800,8900,9000,9100,9200,9300,9400,9500,9600,9700,9800,9900,10000,10100,10200,10300,10400,10500,10600,10700,10800,10900,11000,11100,11200,11300,11400,11500,11600,11700,11800,11900,12000,12100,12200,12300,12400,12500,12600,12700,12800,12900}; int main() { //initialize variables value bpm =0; ewma = 1200; average = 0; raw = 0; alpha=1.5; // X 10% D1 = 0; D2 =0; t_old =0; ewma_prev = 1200; stored_bpm =0; //////////////// t_int = 0; while (t_int<= 130) // loop begins // set how many loops or iterations are desired // //POINT OF INTEREST { ewma_prev = ewma; //store previous EWMA //read analog pin //////ANALOG READING SIMULATION read_pin = signal_read[t_int]; ////////////////////////////////// raw = read_pin; /// POINT OF INTEREST D1 = raw; ///calculate EWMA ewma = (alpha*D1 + (10-alpha)*ewma)/10; ewma_comp = ((ewma-1000)/(ewma_prev-1000))*100; //raw_array[0] = D2; D2 = D1; if (ewma_comp>136) /// detect signal peaks, comparing an EWMA with another //EWMA in previous iteration (101/100) { t_new = time_read[t_int];//set a timestamp once a peak is detected if (ewma>1000 && ewma<2400) // prevent ewma misread or data misread (200,700) { ////get latest running time in ms //t_new = clock(); ////// CAUTION ///calculate bpm //bpm = 1*60000/(t_new-t_old); ///measured time in milliseconds //bpm = 1*60000/ (((double) (t_new - t_old)) / CLOCKS_PER_SEC);//measured time in milliseconds bpm = 1000*60/ (t_new-t_old); ///measured time in seconds(*1) or milliseconds(x1000) t_old = t_new;///duplicate the t_new once its only use was finished in this iteration if (bpm > 0 && bpm < 5600) ///prevent systematic bpm miscalculation(20,220) { stored_bpm = bpm; /// update bpm } else { bpm = stored_bpm; /// bpm remains the same printf("Measured BPM is outside acceptable range \n"); } } } printf("T_peak : %.2f | signal : %.2f |ewma = %.0f| ewma comparison =%.2f percent | BPM = %.2f \n",t_new, D1, ewma,ewma_comp, stored_bpm ); t_int++; } return 0; }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more