Using scipy to perform discrete integration of the sample

后端 未结 2 957
我寻月下人不归
我寻月下人不归 2021-01-12 17:50

I am trying to port from labview to python.

In labview there is a function \"Integral x(t) VI\" that takes a set of samples as input, performs a discrete integratio

相关标签:
2条回答
  • 2021-01-12 17:57

    There is only one method in SciPy that does cumulative integration which is scipy.integrate.cumtrapz() which does what you want as long as you don't specifically need to use the Simpson rule or another method. For that, you can as suggested always write the loop on your own.

    0 讨论(0)
  • 2021-01-12 18:24

    I think you may be using scipy.integrate.simps slightly incorrectly. The area returned by scipy.integrate.simps is the total area under y (the first parameter passed). The second parameter is optional, and are sample values for the x-axis (the actual x values for each of the y values). ie:

    >>> import numpy as np
    >>> import scipy
    >>> a=np.array([1,1,1,1,1])
    >>> scipy.integrate.simps(a)
    4.0
    >>> scipy.integrate.simps(a,np.array([0,10,20,30,40]))
    40.0
    

    I think you want to return the areas under the same curve between different limits? To do that you pass the part of the curve you want, like this:

    >>> a=np.array([0,1,1,1,1,10,10,10,10,0])
    >>> scipy.integrate.simps(a)
    44.916666666666671
    >>> scipy.integrate.simps(a[:5])
    3.6666666666666665
    >>> scipy.integrate.simps(a[5:])
    36.666666666666664
    
    0 讨论(0)
提交回复
热议问题