Last active
April 29, 2016 07:16
-
-
Save maartenl/6e934ef3f35c79d45148 to your computer and use it in GitHub Desktop.
js file for my blog at http://randomthoughtsonjavaprogramming.blogspot.nl/p/measurements.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function computeDiet(now) | |
{ | |
var beginning = new Date("March 1, 2014 11:13:00"); | |
// from milliseconds to days = / 1000 / 60 / 60 / 24 | |
// from milliseconds to days = / 3600000 / 24 | |
var period = now.getTime()-beginning.getTime(); | |
var days = period / 3600000 / 24; | |
var total_days = 365 * 2; | |
var begin_weight = 98.6; | |
var end_weight = 80.0; | |
var result = begin_weight - | |
(begin_weight-end_weight)*(days/total_days); | |
if (result < end_weight) {return end_weight;} | |
return result; | |
} | |
function computeMeasurements() | |
{ | |
var weights = measurements(); | |
var average_weight = 0; | |
var average_impedance = 0; | |
var steps = 10; // window for the moving average | |
for (var x in weights) | |
{ | |
weights[x].measurementTimestamp = new Date(weights[x].measurementTimestamp); | |
weights[x].registrationTimestamp = new Date(weights[x].registrationTimestamp); | |
weights[x].bmi = weights[x].weight / (1.96*1.96); | |
weights[x].dietWeight = computeDiet(weights[x].registrationTimestamp); | |
} | |
weights.sort(function(a,b){return a.registrationTimestamp.getTime()-b.registrationTimestamp.getTime()}); | |
if (average_weight === 0) | |
{ | |
average_weight = weights[0].weight; | |
} | |
if (average_impedance === 0) | |
{ | |
average_impedance = weights[0].impedance; | |
} | |
for (var x in weights) | |
{ | |
average_weight = (steps * average_weight + weights[x].weight - (x-steps < 0 ? average_weight : weights[x-steps].averageWeight)) / steps | |
weights[x].averageWeight = average_weight; | |
average_impedance = (steps * average_impedance + weights[x].impedance - (x-steps < 0 ? average_impedance : weights[x-steps].averageImpedance)) / steps | |
weights[x].averageImpedance = average_impedance; | |
} | |
return weights; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment