I have a numpy array which has 8 fields and has 20 million samples.
The fields are [time,X,Y,Z,QW,QX,QY,QZ]
where x,y,z are spatial points and QW,QX,QY,QZ is a quaternion
There are 2 different kinds of samples, one which contains [time,X,Y,Z]
data, and one which contains [time,QW,QX,QY,QZ]
. Since the array is homogeneous, a sample of the first type will look like [time,X,Y,Z,nan,nan,nan,nan]
and a sample of the second type will look like [time,nan,nan,nan,QW,QX,QY,QZ]
. There are many more samples of type [time,X,Y,Z,nan,nan,nan,nan]
. There are only 20,000 samples of type [time,nan,nan,nan,QW,QX,QY,QZ]
in the array.
So the array would look something like:
[time,nan,nan,nan,QW,QX,QY,QZ]
[time,X,Y,Z,nan,nan,nan,nan]
[time,X,Y,Z,nan,nan,nan,nan]
[time,X,Y,Z,nan,nan,nan,nan]
...
[time,X,Y,Z,nan,nan,nan,nan]
[time,nan,nan,nan,QW,QX,QY,QZ]
[time,X,Y,Z,nan,nan,nan,nan]
...
[time,nan,nan,nan,QW,QX,QY,QZ]
my question is, how can I use SLERP (spherical linear interpolation) from pyquaternion to interpolate the QW,QX,QY,QZ values between observations?
The pyquaternion interpolate function takes arguments (quaternion1,quaternion2,ratio)
where ratio is (TimeOfPoint-TimeQuaternion1)/(TimeQuaternion2-TimeQuaternion1)
the issue is, for a given point time,x,y,z, how to quickly search for the nearest upper and lower quaternion observation.
For an example point 1000000 I have tried:
data.shape = (20000000,8)
quat1=Quaternion(data[np.where((data[1000000,0]>=data[:,0]) & (~np.isnan(data[:,4])))[0][-1],4:8])
quat2=Quaternion(data[np.where((data[1000000,0]<=data[:,0]) & (~np.isnan(data[:,4])))[0][0],4:8])
this works, but it takes 2 seconds per sample.
I am looking for a faster way to find the upper and lower quaternion observation for each x,y,z point in order to SLERP.