changeset 1196:d5566af60a69

correcting bug with savgol filter, renaming the functions to proper names filterSG and getAccelerationsSG
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 14 Jul 2022 11:29:13 +0200
parents 27a6a7f9b972
children 0475b4cd0cfb
files trafficintelligence/moving.py
diffstat 1 files changed, 8 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/trafficintelligence/moving.py	Fri Jul 08 14:41:36 2022 +0200
+++ b/trafficintelligence/moving.py	Thu Jul 14 11:29:13 2022 +0200
@@ -848,8 +848,10 @@
             diff.addPosition(diff[-1])
         return diff
 
-    def differentiateSG(self, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='nearest', cval=0.0, nInstantsIgnoredAtEnds = 2):
-        '''Differentiates the trajectory using the Savitsky Golay filter
+    def filterSG(self, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='nearest', cval=0.0, nInstantsIgnoredAtEnds = 2):
+        '''Filters the trajectory using the Savitsky Golay filter
+        if deriv = 1, the method differentiates
+        Warning: high order polynomials yield artefacts
 
         window_length : The length of the filter window (i.e. the number of coefficients). window_length must be a positive odd integer.
         polyorder : The order of the polynomial used to fit the samples. polyorder must be less than window_length.
@@ -861,9 +863,10 @@
 
         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)
+        length = self.length()
         if nInstantsIgnoredAtEnds >=1:
-            if nInstantsIgnoredAtEnds >= len(speeds)/2:
-                n = int(round(len(speeds)/2))-1
+            if nInstantsIgnoredAtEnds >= length/2:
+                n = int(round(length/2))-1
             else:
                 n = nInstantsIgnoredAtEnds
             filtered = filtered[:,n:-n]
@@ -1565,7 +1568,7 @@
         else:
             return speeds
 
-    def getAccelerations(self, window_length, polyorder, delta=1.0, axis=-1, mode='nearest', cval=0.0, nInstantsIgnoredAtEnds = 0):
+    def getAccelerationsSG(self, window_length, polyorder, delta=1.0, axis=-1, mode='nearest', cval=0.0, nInstantsIgnoredAtEnds = 0):
         '''Returns the 1-D acceleration from the 1-D speeds
         Caution about previously filtered data'''
         speeds = self.getSpeeds()