# HG changeset patch # User Nicolas Saunier # Date 1656605102 -7200 # Node ID d324305c1240179c547880dd2fa75e9f864b515f # Parent 606817bc31e8208aa2180b7d4e86489d11496e20 solving issues with extreme values when filtering using savitsky golay diff -r 606817bc31e8 -r d324305c1240 trafficintelligence/moving.py --- a/trafficintelligence/moving.py Wed Jun 29 23:02:47 2022 +0200 +++ b/trafficintelligence/moving.py Thu Jun 30 18:05:02 2022 +0200 @@ -860,13 +860,10 @@ cval : Value to fill past the edges of the input if mode is constant. Default is 0.0. https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.savgol_filter.html#scipy.signal.savgol_filter''' + filtered = savgol_filter(self.positions, window_length, polyorder, deriv, delta, axis, mode, cval) if nInstantsIgnoredAtEnds >=1: - pos = [self.positions[0][nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds], - self.positions[1][nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds]] - else: - pos = self.positions - filtered = savgol_filter(pos, window_length, polyorder, deriv, delta, axis, mode, cval) - return Trajectory(filtered) + filtered = filtered[:,nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds] + return Trajectory(filtered.tolist()) def norm(self): '''Returns the list of the norms at each instant''' @@ -1567,14 +1564,18 @@ def getAccelerations(self, window_length, polyorder, delta=1.0, axis=-1, mode='interp', cval=0.0, nInstantsIgnoredAtEnds = 0): '''Returns the 1-D acceleration from the 1-D speeds Caution about previously filtered data''' - speeds = self.getSpeeds(nInstantsIgnoredAtEnds) + speeds = self.getSpeeds() if window_length > len(speeds): wlength = min(window_length, len(speeds)) if wlength % 2 == 0: wlength -=1 else: wlength = window_length - return savgol_filter(speeds, wlength, min(wlength-1, polyorder), 1, delta, axis, mode, cval) + filtered = savgol_filter(speeds, wlength, min(wlength-1, polyorder), 1, delta, axis, mode, cval) + if nInstantsIgnoredAtEnds >= 1: + return filtered[nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds] + else: + return filtered def getSpeedIndicator(self): from indicators import SeverityIndicator