changeset 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 d72e4bcc1e36
children d6f0e0cab07d
files python/moving.py
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/python/moving.py	Wed Nov 04 17:44:07 2015 -0500
+++ b/python/moving.py	Sat Nov 28 13:29:54 2015 -0500
@@ -758,6 +758,26 @@
             diff.addPosition(diff[-1])
         return diff
 
+    def differentiateSG(self, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0, removeBothEnds = 2):
+        '''Differentiates the trajectory using the Savitsky Golay filter
+
+        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.
+        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.
+        delta : The spacing of the samples to which the filter will be applied. This is only used if deriv > 0. Default is 1.0.
+        axis : The axis of the array x along which the filter is to be applied. Default is -1.
+        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.
+        cval : Value to fill past the edges of the input if mode is constant. Default is 0.0.'''
+        from scipy.signal import savgol_filter
+
+        if removeBothEnds >=1:
+            pos = [self.positions[0][removeBothEnds:-removeBothEnds],
+                   self.positions[1][removeBothEnds:-removeBothEnds]]
+        else:
+            pos = self.positions
+        filtered = savgol_filter(pos, window_length, polyorder, deriv, delta, axis, mode, cval)
+        return Trajectory(filtered)
+
     def norm(self):
         '''Returns the list of the norms at each instant'''
 #        def add(x, y): return x+y