Skip to content

Commit 7ec39c5

Browse files
committed
fixing sliding jerk and test case
1 parent 58f6677 commit 7ec39c5

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

pynumdiff/tests/test_diff_methods.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def iterated_first_order(*args, **kwargs): return first_order(*args, **kwargs)
1414
dt = 0.1
1515
t = np.arange(0, 3+dt, dt) # sample locations, including the endpoint
1616
tt = np.linspace(0, 3) # full domain, for visualizing denser plots
17-
ttt = np.linspace(0, 3, 1001) # for testing jerk_sliding, which requires > 1000 points
17+
ttt = np.linspace(0, 3, 101) # for testing jerk_sliding, which requires > 1001 points
1818
np.random.seed(7) # for repeatability of the test, so we don't get random failures
1919
noise = 0.05*np.random.randn(*t.shape)
2020

@@ -205,6 +205,7 @@ def test_diff_method(diff_method_and_params, test_func_and_deriv, request): # re
205205
except: warn(f"Cannot import cvxpy, skipping {diff_method} test."); return
206206
if diff_method == jerk_sliding:
207207
t = ttt
208+
dt = 0.03
208209
noise = 0.05*np.random.randn(*t.shape)
209210

210211
# sample the true function and make noisy samples, and sample true derivative

pynumdiff/total_variation_regularization/_total_variation_regularization.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def _total_variation_regularized_derivative(x, dt, order, gamma, solver=None):
9797
dxdt_hat = y/dt # y only holds the dx values; to get deriv scale by dt
9898
x_hat = np.cumsum(y) + integration_constants.value[order-1] # smoothed data
9999

100-
dxdt_hat = (dxdt_hat[0:-1] + dxdt_hat[1:])/2 # take first order FD to smooth a touch
100+
dxdt_hat = (dxdt_hat[:-1] + dxdt_hat[1:])/2 # take first order FD to smooth a touch
101101
ddxdt_hat_f = dxdt_hat[-1] - dxdt_hat[-2]
102102
dxdt_hat_f = dxdt_hat[-1] + ddxdt_hat_f # What is this doing? Could we use a 2nd order FD above natively?
103103
dxdt_hat = np.hstack((dxdt_hat, dxdt_hat_f))
@@ -106,7 +106,7 @@ def _total_variation_regularized_derivative(x, dt, order, gamma, solver=None):
106106
d = dxdt_hat[2] - dxdt_hat[1]
107107
dxdt_hat[0] = dxdt_hat[1] - d
108108

109-
return x_hat*std+mean, dxdt_hat*std
109+
return x_hat*std+mean, dxdt_hat*std # derivative is linear, so scale derivative by std
110110

111111

112112
def velocity(x, dt, params=None, options=None, gamma=None, solver=None):
@@ -253,8 +253,9 @@ def jerk_sliding(x, dt, params=None, options=None, gamma=None, solver=None):
253253
elif gamma == None:
254254
raise ValueError("`gamma` must be given.")
255255

256-
if len(x) <= 1000:
256+
if len(x) <= 100:
257+
warn("len(x) <= 1000, calling standard jerk() without sliding")
257258
return _total_variation_regularized_derivative(x, dt, 3, gamma, solver=solver)
258259

259-
kernel = np.hstack((np.arange(1, 201)/200, np.ones(600), (np.arange(0, 201)/200)[::-1]))
260-
return utility.slide_function(jerk_sliding, x, dt, kernel, gamma, stride=200)
260+
kernel = np.hstack((np.arange(1, 21)/20, np.ones(60), (np.arange(0, 21)/20)[::-1]))
261+
return utility.slide_function(_total_variation_regularized_derivative, x, dt, kernel, 3, gamma, stride=20)

0 commit comments

Comments
 (0)