Skip to content

Improving readme #58

@maminian

Description

@maminian

Greetings,

What are the maintainers' thoughts about (a) adding noise to the demo; (b) adding a visual illustration to the demo? For example this is what I threw together based on what's there.


from derivative import dxdt
import numpy as np

t = np.linspace(0,2*np.pi,50)
x = np.sin(t) + 0.05*np.random.normal(0,1,t.shape)


# 1. Finite differences with central differencing using 3 points.
result1 = dxdt(x, t, kind="finite_difference", k=1)

# 2. Savitzky-Golay using cubic polynomials to fit in a centered window of length 1
result2 = dxdt(x, t, kind="savitzky_golay", left=.5, right=.5, order=3)

# 3. Spectral derivative
result3 = dxdt(x, t, kind="spectral")

# 4. Spline derivative with smoothing set to 0.01
result4 = dxdt(x, t, kind="spline", s=1e-2)

# 5. Total variational derivative with regularization set to 0.01
result5 = dxdt(x, t, kind="trend_filtered", order=0, alpha=1e-2)

# 6. Kalman derivative with smoothing set to 1
result6 = dxdt(x, t, kind="kalman", alpha=1)

# 7. Kernel derivative with smoothing set to 1
result7 = dxdt(x, t, kind="kernel", sigma=1, lmbd=.1, kernel="rbf")


# Visualize
all_derivs = [result1,result2,result3,result4,result5,result6,result7]
method_names = ["finite_difference", "savitzky_golay", "spectral", "spline", "trend_filtered", "kalman", "kernel"]

from matplotlib import pyplot as plt
fig,ax = plt.subplots(2,4, figsize=(8,4), constrained_layout=True, sharex=True, sharey=True)

# x(t)
ax[0,0].plot(t,x,c='r')
ax[0,0].text(0,1,'x(t)', ha='left', va='top', transform=ax[0,0].transAxes)

# derivatives
for k,r,title in zip(range(1,8), all_derivs, method_names):
    i,j=k//4,k%4
    ax[i,j].plot(t,r)
    ax[i,j].plot(t,np.cos(t), zorder=-10, alpha=0.5, c='k')
    ax[i,j].text(0,1,title, ha='left', va='top', transform=ax[i,j].transAxes)

ax[0,0].set(xlim=[0,2*np.pi], ylim=[-2.5,2.5])
#fig.savefig('comparison.png', bbox_inches='tight')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions