@sh.add_function(dsp, outputs=['gears'])
def identify_gears(
times, velocities, accelerations, gear_box_speeds_in,
velocity_speed_ratios, stop_velocity, plateau_acceleration,
change_gear_window_width, idle_engine_speed=(0, 0)):
"""
Identifies gear time series [-].
:param times:
Time vector [s].
:type times: numpy.array
:param velocities:
Velocity vector [km/h].
:type velocities: numpy.array
:param accelerations:
Acceleration vector [m/s2].
:type accelerations: numpy.array
:param gear_box_speeds_in:
Gear box speed [RPM].
:type gear_box_speeds_in: numpy.array
:param velocity_speed_ratios:
Constant velocity speed ratios of the gear box [km/(h*RPM)].
:type velocity_speed_ratios: dict[int | float]
:param stop_velocity:
Maximum velocity to consider the vehicle stopped [km/h].
:type stop_velocity: float
:param plateau_acceleration:
Maximum acceleration to be at constant velocity [m/s2].
:type plateau_acceleration: float
:param change_gear_window_width:
Time window used to apply gear change filters [s].
:type change_gear_window_width: float
:param idle_engine_speed:
Engine speed idle median and std [RPM].
:type idle_engine_speed: (float, float)
:return:
Gear vector identified [-].
:rtype: numpy.array
"""
with np.errstate(divide='ignore', invalid='ignore'):
r = velocities / gear_box_speeds_in
idle = (idle_engine_speed[0] - idle_engine_speed[1],
idle_engine_speed[0] + idle_engine_speed[1])
r[gear_box_speeds_in <= idle[0]] = 0
vsr = velocity_speed_ratios
g, vsr = np.array([(k, v) for k, v in sorted(vsr.items()) if k != 0]).T
dr = np.abs(vsr[:, None] - r)
i, j = np.argmin(dr, 0), np.arange(times.shape[0])
b = velocities <= vsr[i] * idle[0]
if idle[1]:
b |= np.abs(velocities / idle[1] - r) < dr[i, j]
b = (velocities <= stop_velocity) | (b & (accelerations < 0))
gear = np.where(b, 0, g[i])
b = (velocities > stop_velocity) & (accelerations > 0)
b |= accelerations > plateau_acceleration
gear[(gear == 0) & b] = 1
gear = co2_utl.median_filter(times, gear, change_gear_window_width)
gear = _correct_gear_shifts(times, r, gear, velocity_speed_ratios)
gear = co2_utl.clear_fluctuations(times, gear, change_gear_window_width)
return gear.astype(int)