Search code examples
pythonnumerical-methodsnumerical-integration

Double integration of discrete data with respect to time


Let's say I have a set of data points called signal and I want to integrate it twice with respect to time (i.e., if signal was acceleration, I'd like to integrate it twice w.r.t. time to get the position). I can integrate it once using simps but the output here is a scalar. How can you numerically integrate a (random) data set twice? I'd imagine it would look something like this, but obviously the inputs are not compatible after the first integration.

n_samples = 5000
t_range = np.arange(float(n_samples))
signal = np.random.normal(0.,1.,n_samples)
signal_integration = simps(signal, t_range)
signal_integration_double = simps(simps(signal, t_range), t_range)

Any help would be appreciated.


Solution

  • Sorry I answered too fast. scipy.integrate.simps give the value of the integration over the range you give it, similar to np.sum(signal).

    What you want is the integration beween the start and each data point, which is what cumsum does. A better method could be scipy.integrate.cumtrapz. You can apply either method twice to get the result you want.

    See:
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.simps.html
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.cumtrapz.html

    Original answer:
    I think you want np.cumsum. Integration of discrete data is just a sum. You have to multiply the result by the step value to get the correct scale.

    See https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.cumsum.html