Skip to content

Commit 9c15c09

Browse files
authored
Update lib.js to save averaged HR instead of last HR
Previous version saved the last sent HR reading when saving to CSV. Updated to maintain an average of all HR readings during each recording interval (only readings with confidence > 80), and save this instead. If no readings pass confidence threshold, the reading with the highest confidence is saved.
1 parent 4f48439 commit 9c15c09

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

apps/recorder/lib.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,45 @@ exports.getRecorders = function() {
6161
};
6262
},
6363
hrm:function() {
64-
var bpm = "", bpmConfidence = "", src="";
64+
const CONFIDENCE_THRESHOLD = 80;
65+
var avgBpm = 0, avgBpmConfidence = 0, src="", nAvgReadings=0, lowConfBpm=null, lowConfBpmConfidence=-1;
66+
6567
function onHRM(h) {
66-
bpmConfidence = h.confidence;
67-
bpm = h.bpm;
68+
let newBpmConfidence = h.confidence;
69+
let newBpm = h.bpm;
6870
src = h.src;
71+
72+
if (newBpmConfidence >= CONFIDENCE_THRESHOLD){
73+
nAvgReadings++;
74+
avgBpm = avgBpm + (newBpm-avgBpm)/nAvgReadings;
75+
avgBpmConfidence = avgBpmConfidence + (newBpmConfidence-avgBpmConfidence)/nAvgReadings;
76+
77+
} else if (newBpmConfidence > lowConfBpmConfidence) {
78+
lowConfBpmConfidence = newBpmConfidence;
79+
lowConfBpm = newBpm;
80+
}
6981
}
82+
7083
return {
7184
name : "HR",
7285
fields : ["Heartrate", "Confidence", "Source"],
7386
getValues : () => {
74-
var r = [bpm,bpmConfidence,src];
75-
bpm = ""; bpmConfidence = ""; src="";
87+
let r;
88+
89+
if (nAvgReadings === 0) {
90+
if (lowConfBpm === null) r = ["","",""];
91+
else r = [lowConfBpm,lowConfBpmConfidence,src];
92+
} else {
93+
r = [Math.round(avgBpm),Math.round(avgBpmConfidence),src];
94+
}
95+
96+
avgBpm = 0;
97+
avgBpmConfidence = 0;
98+
nAvgReadings=0;
99+
lowConfBpm=null;
100+
lowConfBpmConfidence=-1;
101+
src="";
102+
76103
return r;
77104
},
78105
start : () => {
@@ -384,4 +411,4 @@ exports.isRecording = function() {
384411
return !!writeSetup;
385412
};
386413

387-
exports.reload({noUpdateWidget:true});
414+
exports.reload({noUpdateWidget:true});

0 commit comments

Comments
 (0)