问题描述
我正在尝试从labview移植到python.
I am trying to port from labview to python.
在Labview中,有一个函数"Integral x(t)VI",该函数将一组样本作为输入,对样本进行离散积分,并根据Simpsons规则返回值列表(曲线下的面积)
In labview there is a function "Integral x(t) VI" that takes a set of samples as input, performs a discrete integration of the samples and returns a list of values (the areas under the curve) according to Simpsons rule.
我试图在scipy中找到一个等效的功能,例如scipy.integrate.simps,但是这些函数在整个样本集中以浮点数形式返回求和积分.
I tried to find an equivalent function in scipy, e.g. scipy.integrate.simps, but those functions return the summed integral across the set of samples, as a float.
如何获取积分值列表而不是积分值之和?
How do I get the list of integrated values as opposed to the sum of the integrated values?
我只是在错误地看问题吗?
Am I just looking at the problem the wrong way around?
推荐答案
我认为您可能正在使用 scipy.integrate.simps 略有错误. scipy.integrate.simps
返回的面积是y
(传递的第一个参数)下的总面积.第二个参数是可选的,并且是x轴的样本值(每个y值的实际x值).即:
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
这篇关于使用scipy执行样品的离散积分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!