1
0
Fork 0

Update app.js

master
Ben Jabituya 2021-01-02 21:11:34 +00:00 committed by GitHub
parent aab2939011
commit a590a2e7d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 18 deletions

View File

@ -2,7 +2,6 @@ var option = null;
//debugging or analysis files //debugging or analysis files
var logfile = require("Storage").open("HRV_log.csv", "w"); var logfile = require("Storage").open("HRV_log.csv", "w");
logfile.write(""); //reset HRV log
logfile = require("Storage").open("HRV_log.csv", "a"); logfile = require("Storage").open("HRV_log.csv", "a");
@ -10,7 +9,10 @@ var csv = [
"time", "time",
"sample count", "sample count",
"HR", "HR",
"HRV" "SDNN",
"RMSSD",
"Temp",
"movement"
]; ];
logfile.write(csv.join(",")+"\n"); logfile.write(csv.join(",")+"\n");
@ -28,6 +30,7 @@ var sample_frequency = 51.6;
var gap_threshold = 0.15; var gap_threshold = 0.15;
var hr_min = 40; var hr_min = 40;
var hr_max = 160; var hr_max = 160;
var movement = 0;
function storeMyData(data, file_type) { function storeMyData(data, file_type) {
log = raw_HR_array; log = raw_HR_array;
@ -46,16 +49,19 @@ function average(samples) {
return avg; return avg;
} }
function StandardDeviation(data) { function StandardDeviation (array) {
var m = average(data); const n = array.length;
return Math.sqrt(data.reduce(function (sq, n) { const mean = array.reduce((a, b) => a + b) / n;
return sq + Math.pow(n - m, 2); return Math.sqrt(array.map(x => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n);
}, 0) / (data.length - 1));
} }
function turn_off() { function turn_off() {
Bangle.setHRMPower(0); Bangle.setHRMPower(0);
var accel = setInterval(function () {
movement = movement + Bangle.getAccel().diff;
}, 1000);
g.clear(); g.clear();
g.drawString("processing 1/5", 120, 120); g.drawString("processing 1/5", 120, 120);
@ -151,6 +157,21 @@ function find_peaks() {
} }
} }
function RMSSD(data){
var sum = 0;
var square = 0;
for (let i = 0; i < data.length; i++) {
square = data[i] * data[i];
Math.round(square);
sum += square;
}
//sum = parseInt(sum);
var meansquare = sum/data.length;
var RMS = Math.sqrt(meansquare);
RMS = parseInt(RMS);
return RMS;
}
function calculate_HRV() { function calculate_HRV() {
var gap_average = average(pulse_array); var gap_average = average(pulse_array);
var temp_array = []; var temp_array = [];
@ -166,8 +187,11 @@ function calculate_HRV() {
g.flip(); g.flip();
g.clear(); g.clear();
//var display_stdv = StandardDeviation(pulse_array).toFixed(1); //var display_stdv = StandardDeviation(pulse_array).toFixed(1);
var HRV = (StandardDeviation(temp_array) * (1 / (sample_frequency * 2) * 1000)).toFixed(0); var SDNN = (StandardDeviation(temp_array) * (1 / (sample_frequency * 2) * 1000)).toFixed(0);
g.drawString("HRV:" + HRV + "\nHR:" + calculatedHR.toFixed(0) var RMS_SD = RMSSD(temp_array);
g.drawString("SDNN:" + SDNN
+"\nRMSSD:" + RMS_SD
+ "\nHR:" + calculatedHR.toFixed(0)
+"\nSample Count:" + temp_array.length, 120, 120); +"\nSample Count:" + temp_array.length, 120, 120);
if(option == 0){ if(option == 0){
@ -180,18 +204,23 @@ function calculate_HRV() {
0|getTime(), 0|getTime(),
temp_array.length, temp_array.length,
calculatedHR.toFixed(0), calculatedHR.toFixed(0),
HRV SDNN,
RMS_SD,
E.getTemperature(),
movement.toFixed(5)
]; ];
logfile.write(csv.join(",")+"\n"); logfile.write(csv.join(",")+"\n");
for (let i = 0; i < raw_HR_array.length; i++) { movement = 0;
raw_HR_array[i] = null; // for (let i = 0; i < raw_HR_array.length; i++) {
} // raw_HR_array[i] = null;
//}
} }
} }
function btn1Pressed() { function btn1Pressed() {
if(option === null){ if(option === null){
clearInterval(accel);
g.clear(); g.clear();
g.drawString("one-off assessment", 120, 120); g.drawString("one-off assessment", 120, 120);
option = 0; option = 0;
@ -201,6 +230,8 @@ function btn1Pressed() {
function btn3Pressed() { function btn3Pressed() {
if(option === null){ if(option === null){
logfile.write(""); //reset HRV log
clearInterval(accel);
g.clear(); g.clear();
g.drawString("continuous mode", 120, 120); g.drawString("continuous mode", 120, 120);
option = 1; option = 1;
@ -209,13 +240,18 @@ function btn3Pressed() {
} }
var routine = setInterval(function () { var routine = setInterval(function () {
clearInterval(accel);
first_signals = 0; // ignore the first several signals first_signals = 0; // ignore the first several signals
pulsecount = 0; pulsecount = 0;
BPM_array = []; BPM_array = [];
heartrate = []; heartrate = [];
pulse_array = []; pulse_array = [];
Bangle.setHRMPower(1); Bangle.setHRMPower(1);
}, 210000); }, 180000);
var accel = setInterval(function () {
movement = movement + Bangle.getAccel().diff;
}, 1000);
g.clear(); g.clear();
g.setColor("#00ff7f"); g.setColor("#00ff7f");