comparison trafficintelligence/moving.py @ 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 d24d57e4de24
children 27a6a7f9b972
comparison
equal deleted inserted replaced
1192:606817bc31e8 1193:d324305c1240
858 axis : The axis of the array x along which the filter is to be applied. Default is -1. 858 axis : The axis of the array x along which the filter is to be applied. Default is -1.
859 mode : Must be mirror, constant, nearest, wrap or interp. This determines the type of extension to use for the padded signal to which the filter is applied. When mode is constant, the padding value is given by cval. See the Notes for more details on mirror, constant, wrap, and nearest. When the interp mode is selected (the default), no extension is used. Instead, a degree polyorder polynomial is fit to the last window_length values of the edges, and this polynomial is used to evaluate the last window_length // 2 output values. 859 mode : Must be mirror, constant, nearest, wrap or interp. This determines the type of extension to use for the padded signal to which the filter is applied. When mode is constant, the padding value is given by cval. See the Notes for more details on mirror, constant, wrap, and nearest. When the interp mode is selected (the default), no extension is used. Instead, a degree polyorder polynomial is fit to the last window_length values of the edges, and this polynomial is used to evaluate the last window_length // 2 output values.
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 if nInstantsIgnoredAtEnds >=1: 864 if nInstantsIgnoredAtEnds >=1:
864 pos = [self.positions[0][nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds], 865 filtered = filtered[:,nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds]
865 self.positions[1][nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds]] 866 return Trajectory(filtered.tolist())
866 else:
867 pos = self.positions
868 filtered = savgol_filter(pos, window_length, polyorder, deriv, delta, axis, mode, cval)
869 return Trajectory(filtered)
870 867
871 def norm(self): 868 def norm(self):
872 '''Returns the list of the norms at each instant''' 869 '''Returns the list of the norms at each instant'''
873 return hypot(self.positions[0], self.positions[1]) 870 return hypot(self.positions[0], self.positions[1])
874 871
1565 return speeds 1562 return speeds
1566 1563
1567 def getAccelerations(self, window_length, polyorder, delta=1.0, axis=-1, mode='interp', cval=0.0, nInstantsIgnoredAtEnds = 0): 1564 def getAccelerations(self, window_length, polyorder, delta=1.0, axis=-1, mode='interp', cval=0.0, nInstantsIgnoredAtEnds = 0):
1568 '''Returns the 1-D acceleration from the 1-D speeds 1565 '''Returns the 1-D acceleration from the 1-D speeds
1569 Caution about previously filtered data''' 1566 Caution about previously filtered data'''
1570 speeds = self.getSpeeds(nInstantsIgnoredAtEnds) 1567 speeds = self.getSpeeds()
1571 if window_length > len(speeds): 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
1575 else: 1572 else:
1576 wlength = window_length 1573 wlength = window_length
1577 return savgol_filter(speeds, wlength, min(wlength-1, polyorder), 1, delta, axis, mode, cval) 1574 filtered = savgol_filter(speeds, wlength, min(wlength-1, polyorder), 1, delta, axis, mode, cval)
1575 if nInstantsIgnoredAtEnds >= 1:
1576 return filtered[nInstantsIgnoredAtEnds:-nInstantsIgnoredAtEnds]
1577 else:
1578 return filtered
1578 1579
1579 def getSpeedIndicator(self): 1580 def getSpeedIndicator(self):
1580 from indicators import SeverityIndicator 1581 from indicators import SeverityIndicator
1581 return SeverityIndicator('Speed', {t:self.getVelocityAtInstant(t).norm2() for t in self.getTimeInterval()}) 1582 return SeverityIndicator('Speed', {t:self.getVelocityAtInstant(t).norm2() for t in self.getTimeInterval()})
1582 1583