[Python] `np.gradient` has high variance for non-uniform spacing

Discussão em 'Python' iniciado por Stack, Fevereiro 1, 2025.

  1. Stack

    Stack Membro Participativo

    I encountered high variance in np.gradient for relatively smooth data.

    Suppose we want to calculate dx/dt:

    import numpy as np
    import matplotlib.pyplot as plt
    x = np.array([13.11149679, 13.2141427 , 13.37743691, 13.3934357 , 13.56163066,
    13.60207566, 13.69304133])
    t = np.array([0.73065159, 0.74012055, 0.75911018, 0.7607452 , 0.77811468,
    0.78031837, 0.79046324])
    x_grad = np.gradient(x, t)

    plt.plot(t[1:], np.diff(x) / np.diff(t), 'xb', label="findiff")
    plt.plot(t, x_grad, 'or', label = 'np.gradient')
    plt.plot(t, x, '+g', label = "x")
    plt.xlabel('t')
    plt.ylabel('x')
    plt.legend()
    plt.show()


    [​IMG]

    The noise in data seems to be amplified by a lot. How should we deal with this?

    Continue reading...

Compartilhe esta Página