changeset 1193:d324305c1240

solving issues with extreme values when filtering using savitsky golay
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 30 Jun 2022 18:05:02 +0200
parents 606817bc31e8
children 756bc885a573
files trafficintelligence/moving.py
diffstat 1 files changed, 9 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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