Zeeshan
Zeeshan

Reputation: 1278

Area under curve python

I have a set of data points which when visualized by line graph look like this enter image description here

The value on x-axis in time and value on Y axis is a number. My ultimate task is to find the integral of the graph between any input time frame.

I think I need to do two things-

1- Find a continuous function defining this line graph (orange line)

2- Then maybe use scipy.integrate library to compute the integration.

I have two questions-

1- Is this the right approach or there is any smarter way to do it?

2- How to find the function of line graph given a set of data points ? This is little different than curve fitting where we find the 'best' fit. Here I want a function defining the line graph visualized.

Any insight in the right direction would be helpful.

Thanks

Upvotes: 2

Views: 6476

Answers (2)

duffymo
duffymo

Reputation: 308733

I disagree about the continuous function. It's possible to do numerical integration for data like this using Simpson's rule or something like it.

Since it's a time series, you could do an FFT and integrate in the frequency domain, then transform the result.

Upvotes: 1

heltonbiker
heltonbiker

Reputation: 27575

You cannot find any continuous function for this kind of data, it's totally irregular as the plot shows.

What you want to do is to use the Trapezoidal Rule. It is available as a Numpy function (numpy.trapz).

Upvotes: 5

Related Questions