System.println((234.56789 * 100000).toString());
The NEW avg power is simply (234.56789 * 10,800,000 + 203 * 1,250) / 10,800,100 = 234.56424W
Are you saying that saving all 10.801 integers, the average would be more accurate? In fact, I think it would be less accurate, since you'd assume all 10,801 values were captured at the same time gap between samples.
When a lap starts, I have a lap running total and lap start time. Gets a little more complicated if you want to pull out zeros or pause time from those averages.
var N = 0;
var total = 0.0;
var ignoringZeros = false; // set to true to ignore zeros
// Lap or overall average
function cumulative_average[var latestSample]
{
// ignore pause time, nulls, and [optionally] zeros
if [timerIsRunning && latestSample != null && [latestSample != 0 || !ignoringZeros]]
{
total += latestSample;
N++;
}
if [N != 0]
{
return total / N;
}
return null;
}
I use this for my LAPSTATS data field. Hit LAP at any point during your ride, and it displays your current LAP avg power. So I just grab the starting overall avg power at the start of your LAP, and then report on the LAP's current avg power as the lap evolves. At any point, I just grab the current overall average power, and compare it to the overall avg power at the start of the lap.
Given a more precise FP average... Then I can calculate an accurate average power for any segment in the ride.... starting and ending at any point, of any duration. By only grabbing the FP overall average at the start and at the end of the segment. That might be a 10 min segment or a 2 hour segment. Starting 1 hour or 20 hours into the ride.
Hi him i have used this method for calculating 30 seconds average power
var aAvg = 30;
avgPower = ((avgPower * (aAvg-1) + rawPower) / aAvg).toNumber();
but after a lot of tests i recently noticed that avgPower is 29 down that it should be
i´m not using the
x++;
could it be the reason??
The problem is that you're truncating avgPower to the next lowest integer value every time you do this calculation.
Lets just assume for a second that avgPower is 150 and the new sample is 162... ((150 * 29 + 162) / 30) => ((4350 + 162) / 30) => (4512 / 30). If you do an integer divide, the result is 150. If you do a floating-point divide you keep the decimal and you get 150.4. In the next second you would be throwing away (0.4 * 29) => 11.6 from the power value.
To do this right, you need to do the arithmetic with floating point values, and then the presentation with an integer.
hidden var avgPower; hidden const aAvg = 30; function compute(info) { var rawPower = info.currentPower; if (avgPower == null) { // ensure that avgPower is a Float so we get fractional parts avgPower = rawPower.toFloat(); } if (rawPower != null) { // if avgPower is Float, all of these calculations will produce // a Float as well, and no precision will be lost avgPower = (avgPower * (aAvg - 1) + rawPower) / aAvg; } // without modifying our Float, get the average value as a whole // number. this will truncate to the nearest whole number below // the actual value. return avgPower.toNumber(); // this would do proper rounding... 150.3 => 150, but 150.7 => 151 // return Math.round(avgPower); }