问题描述:

I have a time series x(t) that is a NumPy array. My assignment tells me that I need to find the integral of this data with time.

How am I supposed to do this? It's not a function that I need to integrate, it's a list of data.

网友答案:

It depends on the statement of the problem. A rude approach would be something like this

import numpy as np
import scipy as sp

t = np.linspace(-1, 1, 100)
x = t*t

delta = t[1] - t[0]
I = sum(delta*x)
网友答案:

You can use Simpson's Rule. A routine that does that for you is simps in spicy.integrate.

>>> help(scipy.integrate.simps)
Help on function simps in module scipy.integrate.quadrature:

simps(y, x=None, dx=1, axis=-1, even='avg')
    Integrate y(x) using samples along the given axis and the composite
    Simpson's rule.  If x is None, spacing of dx is assumed.

    If there are an even number of samples, N, then there are an odd
    number of intervals (N-1), but Simpson's rule requires an even number
    of intervals.  The parameter 'even' controls how this is handled.
相关阅读:
Top