Skip to content

Instantly share code, notes, and snippets.

@howtodowtle
Last active April 21, 2021 21:49
Show Gist options
  • Save howtodowtle/3fbeb55632e6cc80e9be460423a52f1a to your computer and use it in GitHub Desktop.
Save howtodowtle/3fbeb55632e6cc80e9be460423a52f1a to your computer and use it in GitHub Desktop.
Stryd calibration algorithm
import math
def calibrate_stryd(stryd_distance: float, real_distance: float,
current_calibration_factor: float = 100.,
max_deviation: float = 2., max_weight: float = 0.4444, verbose: bool = True):
"""Calculates the new Stryd calibration factor given a distance measured by Stryd vs in reality (in m).
All calibration factors and the deviation are given with baseline 100 (as in Garmin watches).
`max_weight` is in [0., 1.] and determines the maximum weight of the new data.
Rules:
1. Get the theoretical new calibration (straightforward).
2. Weigh it with `real_distance` / 1000., but not more than `max_weight`.
3. Calculate a weighted average of the current and new calibration.
4. Round to the next decimal (Garmin only allows 1 decimal).
5. Clip at 100. -/+ `max_deviation`.
"""
calibration_adjustment = real_distance / stryd_distance
theoretical_new_calibration = current_calibration_factor * calibration_adjustment
new_weight = min(real_distance / 1000., max_weight)
weighted_average = (1 - new_weight) * current_calibration_factor + new_weight * theoretical_new_calibration
rounded_wavg = math.floor(weighted_average * 10) / 10 if weighted_average > 100. else math.ceil(weighted_average * 10) / 10
clipped_rounded_wavg = min(max(100. - max_deviation, rounded_wavg), 100. + max_deviation)
if verbose:
print(f"Stryd is off by {(stryd_distance / real_distance - 1) * 100:.1f} % (+/-: too long/short)",
f"Theoretical new calibration: {theoretical_new_calibration:.2f}",
f"Weighting new data with {new_weight:.4f}",
f"Weighted average: {weighted_average:.1f}",
f"After rounding: {rounded_wavg:.1f}",
f"After clipping: {clipped_rounded_wavg:.1f}",
f"-" * 30,
f"Set your new calibration factor to {clipped_rounded_wavg:.1f}",
sep="\n")
return clipped_rounded_wavg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment