Search code examples
signal-processingsensorsquaternionseuler-angles

Eliminating sign flips in quaternion data from sensors


I obtain sensor data from an AR headset, which outputs the user's head orientation as quaternions. When I inspected the raw data, I saw that there are several significant jumps in the consecutive quaternion samples, especially for qw and qy components as shown below.

enter image description here

Since I want to use some machine learning algorithms on this data, continuity is important. To get rid of the flips, I followed the advice in this answer and flipped the sign of all quaternion components, if qw < 0. This is valid because q and -q denote the same rotation, assuming q is a unit quaternion. With this approach, most of the flips are gone:

enter image description here

However, I noticed that this created another jump for qy at around t=25000 where the magnitude of qy is very close to 1.0. Checking the specific samples where the jump occurs, I converted the quaternions values to Euler angles (yaw, pitch, roll) to get a better understanding:

e1 = [175.84487617, 4.24149047, 170.7215615]
e2 = [175.0441748, -0.47157242, 169.98347392]

It is clear that the angles are very similar except for the zero-crossing in the pitch value which seems to cause the flip in qy. Do I have to live with these discontinuities that occur at the borders of the range or is there a way to make quaternions fully continuous?


Solution

  • Don't flip the signs based on one particular element of the quaternion (e.g., always looking at qw). This is not a good scheme. Instead, flip the signs based on whichever element is the greatest in magnitude at the time. That is, at each step recalculate which quaternion element is the greatest in magnitude and ensure that particular element's sign stays the same across the step. At one time you might be flipping the signs based on qx, and at another time you might be flipping the signs based on qw, etc.