Search code examples
pythonnumpyscikit-learnscipyintegral

area under the curve between two troughs rather than integrating between the two points


I have a list of X(x) and Y(signal) data using which I am calculating the troughs:

from scipy.signal import find_peaks
troughs, _= find_peaks(-signal)

Now I want to calculate the integral between the first trough and the last trough

from scipy.interpolate import InterpolatedUnivariateSpline   
x = range(1,len(signal)+1)
f = InterpolatedUnivariateSpline(x, y)
f.integral(peaks[1], peaks[-1])
which gives me: -1.477

Given that this value is negative is consistent with the fact that it is calculating the integral. But what about Area Under the Curve? We know that they are not equal to each other as integral could be negative yet AUC cannot. Is there any easy way to calculate AUC between two different points? One way I could think of is divide Y into negative and non negative groups and do the integral for each group separately.


Solution

  • You could use the abs value for the function. Is the same of

    divide Y into negative and non negative groups and do the integral for each group separately

    but more fastly.

    Mathematically it should work ... but i don't know if this is whath you want.

    Hope it's helpful. :)