1- import os , sys , copy , scipy
1+ import os , sys , copy
22import numpy as np
3+ from scipy .integrate import cumulative_trapezoid
4+ from scipy .optimize import minimize
35
46
57def hankel_matrix (x , num_delays , pad = False ): # fixed delay step of 1
@@ -95,16 +97,16 @@ def peakdet(x, delta, t=None):
9597
9698
9799# Trapazoidal integration, with 0 first value so that the lengths match. See #88.
98- def integrate_dxdt_hat (dxdt_hat , dt ):
99- """Wrapper for scipy.integrate.cumulative_trapezoid to integrate dxdt_hat that ensures
100- the integral has the same length
100+ def integrate_dxdt_hat (dxdt_hat , _t ):
101+ """Wrapper for scipy.integrate.cumulative_trapezoid
101102
102103 :param np.array[float] dxdt_hat: estimate derivative of timeseries
103- :param float dt: time step in seconds
104+ :param float _t: stepsize if given as a scalar or a vector of sample locations
104105
105106 :return: **x_hat** (np.array[float]) -- integral of dxdt_hat
106107 """
107- return np .hstack ((0 , scipy .integrate .cumulative_trapezoid (dxdt_hat )))* dt
108+ return cumulative_trapezoid (dxdt_hat , initial = 0 )* _t if np .isscalar (_t ) \
109+ else cumulative_trapezoid (dxdt_hat , x = _t , initial = 0 )
108110
109111
110112# Optimization routine to estimate the integration constant.
@@ -118,7 +120,7 @@ def estimate_integration_constant(x, x_hat):
118120
119121 :return: **integration constant** (float) -- initial condition that best aligns x_hat with x
120122 """
121- return scipy . optimize . minimize (lambda x0 , x , xhat : np .linalg .norm (x - (x_hat + x0 )), # fn to minimize in 1st argument
123+ return minimize (lambda x0 , x , xhat : np .linalg .norm (x - (x_hat + x0 )), # fn to minimize in 1st argument
122124 0 , args = (x , x_hat ), method = 'SLSQP' ).x [0 ] # result is a vector, even if initial guess is just a scalar
123125
124126
0 commit comments