Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interval average of 1D data

I have two 1D arrays, one for measured data and the other one for location. For example, the measured data could be temperature and the other array the heights of the measurement:

temp = np.asarray([10, 9.6, 9.3, ..., -20.3, -21.0])  # Temperature in celsius
height = np.asarray([129, 145, 167, ..., 5043, 5112]) # Height in meters

As you can see, the height of the measurements is not regularly spaced.

I want to compute the mean temperature in regularly spaced height intervals. This is some kind of moving average, but the window size is variable, because the data points inside the interval of interest is not always the same.

This could be done with a for loop in the following way:

regular_heights = np.arange(0, 6000, 100) # Regular heights every 100m
regular_temps = []

for i in range(len(regular_heights)-1):
    mask = np.logical_and(height > regular_heights[i], height < regular_heights[i+1])
    mean = np.mean(temp[mask])
    regular_temps.append(mean)

regular_temps = np.hstack((regular_temps))

I don't like this approach that much and I was wondering if there would be a more "numpy-style" solution.

like image 793
Iñigo Hernáez Corres Avatar asked May 24 '13 11:05

Iñigo Hernáez Corres


1 Answers

You are probably looking for UnivariateSpline. For example:

from scipy.interpolate import UnivariateSpline

temp = np.asarray([10, 9.6, 9.3, 9.0, 8.7])    # Temperature in celsius
height = np.asarray([129, 145, 167, 190, 213]) # Height in meters
f = UnivariateSpline(height, temp)

Now you can evaluate f wherever you want:

regular_heights = np.arange(120, 213, 5)       # Regular heights every 5m
plot(height, temp, 'o', regular_heights, f(regular_heights), 'x')

enter image description here

like image 189
elyase Avatar answered Nov 11 '22 09:11

elyase