comparison python/moving.py @ 761:15ddc8715236 dev

added savitzky golay for differentiation
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sat, 28 Nov 2015 13:29:54 -0500
parents c6d4ea05a2d0
children d6f0e0cab07d
comparison
equal deleted inserted replaced
760:d72e4bcc1e36 761:15ddc8715236
755 for i in xrange(1, self.length()): 755 for i in xrange(1, self.length()):
756 diff.addPosition(self[i]-self[i-1]) 756 diff.addPosition(self[i]-self[i-1])
757 if doubleLastPosition: 757 if doubleLastPosition:
758 diff.addPosition(diff[-1]) 758 diff.addPosition(diff[-1])
759 return diff 759 return diff
760
761 def differentiateSG(self, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0, removeBothEnds = 2):
762 '''Differentiates the trajectory using the Savitsky Golay filter
763
764 window_length : The length of the filter window (i.e. the number of coefficients). window_length must be a positive odd integer.
765 polyorder : The order of the polynomial used to fit the samples. polyorder must be less than window_length.
766 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.
767 delta : The spacing of the samples to which the filter will be applied. This is only used if deriv > 0. Default is 1.0.
768 axis : The axis of the array x along which the filter is to be applied. Default is -1.
769 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.
770 cval : Value to fill past the edges of the input if mode is constant. Default is 0.0.'''
771 from scipy.signal import savgol_filter
772
773 if removeBothEnds >=1:
774 pos = [self.positions[0][removeBothEnds:-removeBothEnds],
775 self.positions[1][removeBothEnds:-removeBothEnds]]
776 else:
777 pos = self.positions
778 filtered = savgol_filter(pos, window_length, polyorder, deriv, delta, axis, mode, cval)
779 return Trajectory(filtered)
760 780
761 def norm(self): 781 def norm(self):
762 '''Returns the list of the norms at each instant''' 782 '''Returns the list of the norms at each instant'''
763 # def add(x, y): return x+y 783 # def add(x, y): return x+y
764 # sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]]) 784 # sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]])