diff trafficintelligence/moving.py @ 1270:20a5e1292321

added smoothing functions and velocity generation to dltrack
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 10 Jun 2024 16:44:19 -0400
parents ef68d4ba7dae
children
line wrap: on
line diff
--- a/trafficintelligence/moving.py	Wed Jun 05 10:12:43 2024 -0400
+++ b/trafficintelligence/moving.py	Mon Jun 10 16:44:19 2024 -0400
@@ -1747,6 +1747,30 @@
         else:
             return speeds
 
+    def computeVelocities(self, halfWidth = None):
+        '''compute velocities, smoothed if halfwidth is not None '''        
+        if halfWidth is None:
+            self.velocities = self.getPositions().differentiate(True)
+        else:
+            self.velocities = self.getPositions().differentiate().filterMovingWindow(halfWidth)
+            self.velocities.addPosition(self.velocities[-1])
+        
+    def smoothPositions(self, halfWidth, replace = False):
+        'Returns the smoothed positions (or replaces them)'
+        smoothed = self.getPositions().filterMovingWindow(halfWidth)
+        if replace:
+            self.positions = smoothed
+        else:
+            return smoothed
+
+    def smoothVelocities(self, halfWidth, replace = False):
+        'Returns the smoothed velocities (or replaces them)'
+        smoothed = self.getVelocities().filterMovingWindow(halfWidth)
+        if replace:
+            self.velocities = smoothed
+        else:
+            return smoothed
+
     def getAccelerations(self, fromSpeeds = True, nInstantsIgnoredAtEnds = 0):
         '''Returns the scalar acceleration by differentiating the speeds (fromSpeeds = True) or by differentiating the velocity (vector) and taking the norm (frompSpeeds = False. In the second case, speed is always positive'''
         if fromSpeeds: