Post · 4 min read· pp. 320–322

Sarabandi’s Method

ahrs.common.orientation.sarabandi(dcm: ndarray, eta: float = 0.0) → ndarray : Quaternion from a Direction Cosine Matrix using Sarabandi’s method [ST19].

code
A rotation matrix R\mathbf{R} can be expressed as:

R=[r11r12r13r21r22r23r31r32r33]\begin{split}\mathbf{R} = \begin{bmatrix} r_{11} & r_{12} & r_{13} \\ r_{21} & r_{22} & r_{23} \\ r_{31} & r_{32} & r_{33} \end{bmatrix}\end{split}
A quaternion q\mathbf{q} describing the same rotation can be expressed as:
q=[qw qx qy qz]=[cos⁡(θ2)nxsin⁡(θ2)nysin⁡(θ2)nzsin⁡(θ2)]\begin{split}\mathbf{q} = \begin{bmatrix} q_w \ q_x \ q_y \ q_z \end{bmatrix} = \begin{bmatrix} \cos (\frac{\theta}{2}) \\ n_x \sin (\frac{\theta}{2}) \\ n_y \sin (\frac{\theta}{2}) \\ n_z \sin (\frac{\theta}{2}) \end{bmatrix}\end{split}
where θ\theta is the rotation angle, and n\mathbf{n} is the unitary rotation axis. The quaternion is also unitary, i.e., ∥q∥=1\|\mathbf{q}\| = 1. The rotation matrix R\mathbf{R} can be expressed in terms of the quaternion q\mathbf{q} as:
R=[1−2qy2−2qz22(qxqy−qzqw)2(qxqz+qyqw)2(qxqy+qzqw)1−2qx2−2qz22(qyqz−qxqw)2(qxqz−qyqw)2(qyqz+qxqw)1−2qx2−2qy2]\begin{split}\mathbf{R} = \begin{bmatrix} 1 - 2q_y^2 - 2q_z^2 & 2(q_xq_y - q_zq_w) & 2(q_xq_z + q_yq_w) \\ 2(q_xq_y + q_zq_w) & 1 - 2q_x^2 - 2q_z^2 & 2(q_yq_z - q_xq_w) \\ 2(q_xq_z - q_yq_w) & 2(q_yq_z + q_xq_w) & 1 - 2q_x^2 - 2q_y^2 \end{bmatrix}\end{split}
As with [Shepperd’s method](https://ahrs.readthedocs.io/en/latest/special/shepperd.html), we build a system of linear equations with qwq_w, qxq_x, qyq_y and qzq_z:
4qw2=1+r11+r22+r334qx2=1+r11−r22−r334qy2=1−r11+r22−r334qz2=1−r11−r22+r334qyqz=r23+r324qxqz=r31+r134qxqy=r12+r214qwqx=r32−r234qwqy=r13−r314qwqz=r21−r12\begin{split}\begin{array}{rcl} 4q_w^2 &=& 1 + r_{11} + r_{22} + r_{33} \\ 4q_x^2 &=& 1 + r_{11} - r_{22} - r_{33} \\ 4q_y^2 &=& 1 - r_{11} + r_{22} - r_{33} \\ 4q_z^2 &=& 1 - r_{11} - r_{22} + r_{33} \\ 4q_yq_z &=& r_{23} + r_{32} \\ 4q_xq_z &=& r_{31} + r_{13} \\ 4q_xq_y &=& r_{12} + r_{21} \\ 4q_wq_x &=& r_{32} - r_{23} \\ 4q_wq_y &=& r_{13} - r_{31} \\ 4q_wq_z &=& r_{21} - r_{12} \end{array}\end{split}
Clearing for qwq_w, we get:
qw=121+r11+r22+r33q_w = \frac{1}{2}\sqrt{1 + r_{11} + r_{22} + r_{33}}
We see that trace(R)=r11+r22+r33=2cos⁡θ+1\mathrm{trace}(\mathbf{R}) = r_{11}+r_{22}+r_{33} = 2\cos\theta + 1. This becomes ill-conditioned when θ→π\theta \rightarrow \pi, and qwq_w can even become negative due to rounding errors, which is not allowed for our unit quaternion. To obtain a more robust solution, we involve the off-diagonal elements of the rotation matrix. Using the system of linear equations to substitute qwq_w, qxq_x, qyq_y and qzq_z in qw2+qx2+qy2+qz2=1q_w^2 + q_x^2 + q_y^2 + q_z^2 = 1 we get:
1+r11+r22+r334+(r32−r234qw)2+(r13−r314qw)2+(r21−r124qw)2=1\frac{1+r_{11}+r_{22}+r_{33}}{4} + \Bigg(\frac{r_{32}-r_{23}}{4q_w}\Bigg)^2 + \Bigg(\frac{r_{13}-r_{31}}{4q_w}\Bigg)^2 + \Bigg(\frac{r_{21}-r_{12}}{4q_w}\Bigg)^2 = 1
Solving for qwq_w:
qw=12(r32−r23)2+(r13−r31)2+(r21−r12)23−r11−r22−r33q_w = \frac{1}{2}\sqrt{\frac{(r_{32}-r_{23})^2 + (r_{13}-r_{31})^2 + (r_{21}-r_{12})^2}{3 - r_{11} - r_{22} - r_{33}}}
This new definition of qwq_w is now ill-conditioned when θ→0\theta \rightarrow 0. Thus, both definitions can be seen as complementary. Now we simply establish a threshold for the trace of the rotation matrix. This threshold is easily defined from the terms inside the square root.
qw={121+r11+r22+r33if  r11+r22+r33>η12(r32−r23)2+(r13−r31)2+(r21−r12)23−r11−r22−r33otherwise\begin{split}q_w = \left\{ \begin{array}{lc} \frac{1}{2}\sqrt{1+r_{11}+r_{22}+r_{33}} & \mathrm{if}\; r_{11}+r_{22}+r_{33} > \eta \\ \frac{1}{2}\sqrt{\frac{(r_{32}-r_{23})^2 + (r_{13}-r_{31})^2 + (r_{21}-r_{12})^2}{3-r_{11}-r_{22}-r_{33}}} & \mathrm{otherwise} \end{array} \right.\end{split}
Repeating the same process for the other elements of the quaternion:
qx={121+r11−r22−r33if  r11−r22−r33>η12(r32−r23)2+(r12+r21)2+(r31+r13)23−r11+r22+r33otherwiseqy={121−r11+r22−r33if  −r11+r22−r33>η12(r13−r31)2+(r12+r21)2+(r23+r32)23+r11−r22+r33otherwiseqz={121−r11−r22+r33if  −r11−r22+r33>η12(r21−r12)2+(r31+r13)2+(r23+r32)23+r11+r22−r33otherwise\begin{split}\begin{array}{rcl} q_x &=& \left\{ \begin{array}{lc} \frac{1}{2}\sqrt{1+r_{11}-r_{22}-r_{33}} & \mathrm{if}\; r_{11}-r_{22}-r_{33} > \eta \\ \frac{1}{2}\sqrt{\frac{(r_{32}-r_{23})^2 + (r_{12}+r_{21})^2 + (r_{31}+r_{13})^2}{3-r_{11}+r_{22}+r_{33}}} & \mathrm{otherwise} \end{array} \right.\\\\ q_y &=& \left\{ \begin{array}{lc} \frac{1}{2}\sqrt{1-r_{11}+r_{22}-r_{33}} & \mathrm{if}\; -r_{11}+r_{22}-r_{33} > \eta \\ \frac{1}{2}\sqrt{\frac{(r_{13}-r_{31})^2 + (r_{12}+r_{21})^2 + (r_{23}+r_{32})^2}{3+r_{11}-r_{22}+r_{33}}} & \mathrm{otherwise} \end{array} \right.\\\\ q_z &=& \left\{ \begin{array}{lc} \frac{1}{2}\sqrt{1-r_{11}-r_{22}+r_{33}} & \mathrm{if}\; -r_{11}-r_{22}+r_{33} > \eta \\ \frac{1}{2}\sqrt{\frac{(r_{21}-r_{12})^2 + (r_{31}+r_{13})^2 + (r_{23}+r_{32})^2}{3+r_{11}+r_{22}-r_{33}}} & \mathrm{otherwise} \end{array} \right. \end{array}\end{split}
Finally, if qwq_w is positive, we redefine the sign of the quaternion elements, with the signs of r32−r23r_{32}-r_{23}, r13−r31r_{13}-r_{31}, and r21−r12r_{21}-r_{12} assigned to qxq_x, qyq_y, and qzq_z, respectively. Parameters: : - **dcm** (*numpy.ndarray*) – 3-by-3 Direction Cosine Matrix. - **eta** (*float,d efault: 0.0*) – Threshold. Returns: : **q** – Quaternion. Return type: : numpy.ndarray