Mercurial Hosting > traffic-intelligence
comparison trafficintelligence/moving.py @ 1195:27a6a7f9b972
adjusting savgol use
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 08 Jul 2022 14:41:36 +0200 |
parents | d324305c1240 |
children | d5566af60a69 |
comparison
equal
deleted
inserted
replaced
1194:756bc885a573 | 1195:27a6a7f9b972 |
---|---|
846 diff.addPosition(self[i]-self[i-1]) | 846 diff.addPosition(self[i]-self[i-1]) |
847 if doubleLastPosition: | 847 if doubleLastPosition: |
848 diff.addPosition(diff[-1]) | 848 diff.addPosition(diff[-1]) |
849 return diff | 849 return diff |
850 | 850 |
851 def differentiateSG(self, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0, nInstantsIgnoredAtEnds = 2): | 851 def differentiateSG(self, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='nearest', cval=0.0, nInstantsIgnoredAtEnds = 2): |
852 '''Differentiates the trajectory using the Savitsky Golay filter | 852 '''Differentiates the trajectory using the Savitsky Golay filter |
853 | 853 |
854 window_length : The length of the filter window (i.e. the number of coefficients). window_length must be a positive odd integer. | 854 window_length : The length of the filter window (i.e. the number of coefficients). window_length must be a positive odd integer. |
855 polyorder : The order of the polynomial used to fit the samples. polyorder must be less than window_length. | 855 polyorder : The order of the polynomial used to fit the samples. polyorder must be less than window_length. |
856 deriv : The order of the derivative to compute. This must be a nonnegative integer. The default is 0, which means to filter the data without differentiating. | 856 deriv : The order of the derivative to compute. This must be a nonnegative integer. The default is 0, which means to filter the data without differentiating. |
860 cval : Value to fill past the edges of the input if mode is constant. Default is 0.0. | 860 cval : Value to fill past the edges of the input if mode is constant. Default is 0.0. |
861 | 861 |
862 https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.savgol_filter.html#scipy.signal.savgol_filter''' | 862 https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.savgol_filter.html#scipy.signal.savgol_filter''' |
863 filtered = savgol_filter(self.positions, window_length, polyorder, deriv, delta, axis, mode, cval) | 863 filtered = savgol_filter(self.positions, window_length, polyorder, deriv, delta, axis, mode, cval) |
864 if nInstantsIgnoredAtEnds >=1: | 864 if nInstantsIgnoredAtEnds >=1: |
865 filtered = filtered[:,nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds] | 865 if nInstantsIgnoredAtEnds >= len(speeds)/2: |
866 n = int(round(len(speeds)/2))-1 | |
867 else: | |
868 n = nInstantsIgnoredAtEnds | |
869 filtered = filtered[:,n:-n] | |
866 return Trajectory(filtered.tolist()) | 870 return Trajectory(filtered.tolist()) |
867 | 871 |
868 def norm(self): | 872 def norm(self): |
869 '''Returns the list of the norms at each instant''' | 873 '''Returns the list of the norms at each instant''' |
870 return hypot(self.positions[0], self.positions[1]) | 874 return hypot(self.positions[0], self.positions[1]) |
1559 n = min(nInstantsIgnoredAtEnds, int(floor(self.length()/2.))) | 1563 n = min(nInstantsIgnoredAtEnds, int(floor(self.length()/2.))) |
1560 return speeds[n:-n] | 1564 return speeds[n:-n] |
1561 else: | 1565 else: |
1562 return speeds | 1566 return speeds |
1563 | 1567 |
1564 def getAccelerations(self, window_length, polyorder, delta=1.0, axis=-1, mode='interp', cval=0.0, nInstantsIgnoredAtEnds = 0): | 1568 def getAccelerations(self, window_length, polyorder, delta=1.0, axis=-1, mode='nearest', cval=0.0, nInstantsIgnoredAtEnds = 0): |
1565 '''Returns the 1-D acceleration from the 1-D speeds | 1569 '''Returns the 1-D acceleration from the 1-D speeds |
1566 Caution about previously filtered data''' | 1570 Caution about previously filtered data''' |
1567 speeds = self.getSpeeds() | 1571 speeds = self.getSpeeds() |
1568 if window_length > len(speeds): | 1572 wlength = min(window_length, len(speeds)) |
1569 wlength = min(window_length, len(speeds)) | 1573 if wlength % 2 == 0: |
1570 if wlength % 2 == 0: | 1574 wlength -=1 |
1571 wlength -=1 | |
1572 else: | |
1573 wlength = window_length | |
1574 filtered = savgol_filter(speeds, wlength, min(wlength-1, polyorder), 1, delta, axis, mode, cval) | 1575 filtered = savgol_filter(speeds, wlength, min(wlength-1, polyorder), 1, delta, axis, mode, cval) |
1575 if nInstantsIgnoredAtEnds >= 1: | 1576 if nInstantsIgnoredAtEnds >= 1: |
1576 return filtered[nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds] | 1577 if nInstantsIgnoredAtEnds >= len(speeds)/2: |
1578 n = int(round(len(speeds)/2))-1 | |
1579 else: | |
1580 n = nInstantsIgnoredAtEnds | |
1581 return filtered[n:-n] | |
1577 else: | 1582 else: |
1578 return filtered | 1583 return filtered |
1579 | 1584 |
1580 def getSpeedIndicator(self): | 1585 def getSpeedIndicator(self): |
1581 from indicators import SeverityIndicator | 1586 from indicators import SeverityIndicator |