Search code examples
pythonscipyareanumerical-integration

Integrating a data array unevenly sampled to get the area between the 'curve' and 0


I have 1D data arrays with about 100 strictly positive elements. Some will have more, some will have less. Most of the elements will be small, but this signal can have spikes, usually in the 4 to 5 range.

Example:

 data = [0.44217042317282634, 0.5371139455855275,
         0.44094305007577467, 0.5703620638886562, 
         0.5442900940823339, 0.650659771810529,
         ...,
         0.7121380290819317, 0.6901401693275381]

If I plot the signal, I get something like:

signal example

On the plot above, there are 111 points unevenly spaced. The sampling rate is not constant at all. How can I compute the area of the blue shade above?

One very important aspect is that if I have a similar signal with more samples, producing the same plot, I want the area to be unchanged (obvious, but still).

I am not at all familiar with integration techniques on signals with uneven sampling rate. Some guidance would be appreciated. Thanks!


Solution

  • Assuming you know the sampling times, you can use scipy for this:

    import numpy as np
    from scipy import interpolate
    
    data = np.array([0.44217042317282634, 0.5371139455855275,
             0.44094305007577467, 0.5703620638886562,
             0.5442900940823339, 0.650659771810529])
    
    discrete_time = np.array([0.11,0.34,0.55,0.61,0.8,0.967])
    area = interpolate.InterpolatedUnivariateSpline(discrete_time, data, k=1).integral(0,1)
    print(area)