Home > Software design >  How can I subtract data of sensor_1 from sensor_2 using Numpy while sensor_1 has a different size an
How can I subtract data of sensor_1 from sensor_2 using Numpy while sensor_1 has a different size an

Time:12-05

How can I subtract data of sensor_1 from sensor_2 using Numpy while sensor_1 has a different size, different time distance and misses same timepoints?

x1=np.array([4.1, 5.2, 7.1])                       # time points
y1=np.array([6.1, 7.1, 7.9])                       # sensor1 data

x2=np.array([1.3, 2.3, 3.4, 4.7, 5.1,  6.3, 7.2])  # time points
y2=np.array([4.9, 5.1, 4.6, 6.1, 7.1, 17.9, 4.2])  # sensor2 data

Im looking in the range of x1 for the difference of y2-y1. The missing time point (~6) and the later start in x1:y1 makes it a little bit difficult.

The size of each data set is about 1MByte. The distance of the timepoints is unknown. It makes re-sampling difficult.

For legacy reasons a Py2.7 solution is preferred. Though asking for Numpy Pandas is fine.

CodePudding user response:

One thing you can do for y2 - y1 at the points in x1 is the linear interpolation:

np.interp(x1, x2, y2) - y1

CodePudding user response:

In response of answer @tueda.

Good point ...

>>> np.interp(x1, x2, y2)-y1
array([-0.69230769,  0.9       , -2.17777778])

Resampling seems to be needed to handle the missing point in x1 @ 6.3 to keep the spike added.

>>> np.interp(np.linspace(x1[0], x1[-1], 4), x1, y1)-np.interp(np.linspace(x1[0],x1[-1], 4), x2, y2)
array([ 0.69230769, -0.09090909, -8.62105263,  2.17777778])

Close enough for me as solution. Thank you @tueda.

  • Related