mirror of
https://github.com/sstent/GarminSync.git
synced 2026-01-25 16:42:20 +00:00
working - moved to compose
This commit is contained in:
73
garminsync/fit_processor/gear_analyzer.py
Normal file
73
garminsync/fit_processor/gear_analyzer.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import numpy as np
|
||||
|
||||
class SinglespeedAnalyzer:
|
||||
def __init__(self):
|
||||
self.chainring_options = [38, 46] # teeth
|
||||
self.common_cogs = list(range(11, 28)) # 11t to 27t rear cogs
|
||||
self.wheel_circumference_m = 2.096 # 700x25c tire
|
||||
|
||||
def analyze_gear_ratio(self, speed_data, cadence_data, gradient_data):
|
||||
"""Determine most likely singlespeed gear ratio"""
|
||||
# Validate input parameters
|
||||
if not speed_data or not cadence_data or not gradient_data:
|
||||
raise ValueError("Input data cannot be empty")
|
||||
if len(speed_data) != len(cadence_data) or len(speed_data) != len(gradient_data):
|
||||
raise ValueError("Input data arrays must be of equal length")
|
||||
|
||||
# Filter for flat terrain segments (gradient < 3%)
|
||||
flat_indices = [i for i, grad in enumerate(gradient_data) if abs(grad) < 3.0]
|
||||
flat_speeds = [speed_data[i] for i in flat_indices]
|
||||
flat_cadences = [cadence_data[i] for i in flat_indices]
|
||||
|
||||
# Only consider data points with sufficient speed (15 km/h) and cadence
|
||||
valid_indices = [i for i in range(len(flat_speeds))
|
||||
if flat_speeds[i] > 4.17 and flat_cadences[i] > 0] # 15 km/h threshold
|
||||
|
||||
if not valid_indices:
|
||||
return None # Not enough data
|
||||
|
||||
valid_speeds = [flat_speeds[i] for i in valid_indices]
|
||||
valid_cadences = [flat_cadences[i] for i in valid_indices]
|
||||
|
||||
# Calculate gear ratios from speed and cadence
|
||||
gear_ratios = []
|
||||
for speed, cadence in zip(valid_speeds, valid_cadences):
|
||||
# Gear ratio = (speed in m/s * 60 seconds/minute) / (cadence in rpm * wheel circumference in meters)
|
||||
gr = (speed * 60) / (cadence * self.wheel_circumference_m)
|
||||
gear_ratios.append(gr)
|
||||
|
||||
# Calculate average gear ratio
|
||||
avg_gear_ratio = sum(gear_ratios) / len(gear_ratios)
|
||||
|
||||
# Find best matching chainring and cog combination
|
||||
best_fit = None
|
||||
min_diff = float('inf')
|
||||
for chainring in self.chainring_options:
|
||||
for cog in self.common_cogs:
|
||||
theoretical_ratio = chainring / cog
|
||||
diff = abs(theoretical_ratio - avg_gear_ratio)
|
||||
if diff < min_diff:
|
||||
min_diff = diff
|
||||
best_fit = (chainring, cog, theoretical_ratio)
|
||||
|
||||
if not best_fit:
|
||||
return None
|
||||
|
||||
chainring, cog, ratio = best_fit
|
||||
|
||||
# Calculate gear metrics
|
||||
wheel_diameter_inches = 27.0 # 700c wheel diameter
|
||||
gear_inches = ratio * wheel_diameter_inches
|
||||
development_meters = ratio * self.wheel_circumference_m
|
||||
|
||||
# Calculate confidence score (1 - relative error)
|
||||
confidence = max(0, 1 - (min_diff / ratio)) if ratio > 0 else 0
|
||||
|
||||
return {
|
||||
'estimated_chainring_teeth': chainring,
|
||||
'estimated_cassette_teeth': cog,
|
||||
'gear_ratio': ratio,
|
||||
'gear_inches': gear_inches,
|
||||
'development_meters': development_meters,
|
||||
'confidence_score': confidence
|
||||
}
|
||||
44
garminsync/fit_processor/power_estimator.py
Normal file
44
garminsync/fit_processor/power_estimator.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import numpy as np
|
||||
|
||||
class PowerEstimator:
|
||||
def __init__(self):
|
||||
self.bike_weight_kg = 10.0 # 22 lbs
|
||||
self.rider_weight_kg = 75.0 # Default assumption
|
||||
self.drag_coefficient = 0.88 # Road bike
|
||||
self.frontal_area_m2 = 0.4 # Typical road cycling position
|
||||
self.rolling_resistance = 0.004 # Road tires
|
||||
self.drivetrain_efficiency = 0.97
|
||||
self.air_density = 1.225 # kg/m³ at sea level, 20°C
|
||||
|
||||
def calculate_power(self, speed_ms, gradient_percent,
|
||||
air_temp_c=20, altitude_m=0):
|
||||
"""Calculate estimated power using physics model"""
|
||||
# Validate input parameters
|
||||
if not isinstance(speed_ms, (int, float)) or speed_ms < 0:
|
||||
raise ValueError("Speed must be a non-negative number")
|
||||
if not isinstance(gradient_percent, (int, float)):
|
||||
raise ValueError("Gradient must be a number")
|
||||
|
||||
# Calculate air density based on temperature and altitude
|
||||
temp_k = air_temp_c + 273.15
|
||||
pressure = 101325 * (1 - 0.0000225577 * altitude_m) ** 5.25588
|
||||
air_density = pressure / (287.05 * temp_k)
|
||||
|
||||
# Convert gradient to angle
|
||||
gradient_rad = np.arctan(gradient_percent / 100.0)
|
||||
|
||||
# Total mass
|
||||
total_mass = self.bike_weight_kg + self.rider_weight_kg
|
||||
|
||||
# Power components
|
||||
P_roll = self.rolling_resistance * total_mass * 9.81 * np.cos(gradient_rad) * speed_ms
|
||||
P_grav = total_mass * 9.81 * np.sin(gradient_rad) * speed_ms
|
||||
P_aero = 0.5 * air_density * self.drag_coefficient * self.frontal_area_m2 * speed_ms ** 3
|
||||
|
||||
# Power = (Rolling + Gravity + Aerodynamic) / Drivetrain efficiency
|
||||
return (P_roll + P_grav + P_aero) / self.drivetrain_efficiency
|
||||
|
||||
def estimate_peak_power(self, power_values, durations):
|
||||
"""Calculate peak power for various durations"""
|
||||
# This will be implemented in Phase 3
|
||||
return {}
|
||||
Reference in New Issue
Block a user