Better contact point detection now handles some initial vibrations.
[hooke.git] / hooke / plugin / vclamp.py
index 441f1fea930214597d0998144ca69eca74e9beda..69025dd52a50ca2e929aeb07bcaf994edb30eac2 100644 (file)
@@ -166,8 +166,21 @@ Minimum `fit-contact-slope/guessed-contact-slope` ratio for a "good" fit.
         middle_deflection = (self.info['min deflection']
                              + self.info['deflection range']/2.)
         kink_position = 2*(self._data > middle_deflection).argmax()
-        left_slope = ((self._data[-1] - self.info['min deflection'])
-                      /kink_position)
+        if kink_position == 0:
+            # jump vibration at the start of the retraction?
+            start = int(min(max(20, 0.01 * len(self._data)), 0.5*len(self._data)))
+            std = self._data[:start].std()
+            left_offset = self._data[start].mean()
+            stop = start
+            while abs(self._data[stop] - left_offset) < 3*std:
+                stop += 1
+            left_slope = (self._data[stop-start:stop].mean()
+                          - left_offset) / (stop-start)
+            left_offset -= left_slope * start/2
+            kink_position = (self._data[-1] - left_offset)/left_slope
+        else:
+            left_slope = (self._data[-1] - self.info['min deflection']
+                          )/kink_position
         right_slope = 0
         self.info['guessed contact slope'] = left_slope
         params = [left_offset, left_slope, kink_position, right_slope]