2

I'm trying to filter positional and rotational data using an Exponential Moving Average (EMA) filter. This has worked without issues for positional data (3D vectors) but I can't figure it out for rotational data (Quaternions). I first tried the naive way, which is applying the EMA to each separate component, but this obviously doesn't work very well. How would I do it?

2 Answers2

3

Equations 12 and 13 in Markley et al.'s paper "Quaternion Averaging" (2007) define the weighted average of $n$ quaterions $\mathbf q_i$ with scalar weights $w_i$: $$\bar{\mathbf q}=\arg\,\max_{\mathbf q\in\mathbb S^3}\mathbf q^T\mathbf M\mathbf q$$ where $$\mathbf M=\sum_{i=1}^nw_i\mathbf q_i\mathbf q_i^T.$$ That is, interpret the quaternions as vectors in $\mathbb R^4$, form the matrix $\mathbf M$, and take its largest eigenvector.

Here, by the "average quaternion" we mean the one that minimizes the weighted sum of squared differences of the corresponding rotation matrices, measured in the Frobenius norm (equation 2 of the above paper).

  • We've been looking over this with a couple of people here, but neither of us are mathematicians. The second equation is quite obvious, but what are $\mathbf q^T$ and $\mathbf q$ in the first equation? – Simon Verbeke Mar 24 '15 at 15:05
  • $\mathbf q$ is a placeholder variable denoting the unknown quaternion that you're maximizing over. That is, the average quaternion $\bar{\mathbf q}$ is the value of $\mathbf q$ that maximizes $\mathbf q^T\mathbf M\mathbf q$. In practice, what you have to do is take $\mathbf M$ and find its eigenvector with the largest eigenvalue, and that'll be $\bar{\mathbf q}$. –  Mar 24 '15 at 15:32
  • You can see an implementation of this in my answer to a related question here: https://math.stackexchange.com/a/3435296/365886 – Luke Hutchison Aug 25 '20 at 10:59
  • When trying to unravel the iterative expression for EMA into the infinite series I was easily able to show that SLERP will not result in a sum of exponentially decaying historical measurements. Ie I came to conclude what you noted in your comment for Steven's answer. I also attempted it with the weighted average method you mentioned here. There I got stuck because I couldn't figure out how to unpack the inner argmax. Do you know for sure that it works? Do you have any references to working, or perhaps a hint as to the "trick" needed? – Alexander Soare Nov 23 '22 at 13:51
2

One of the standard techniques in using quaternions for graphics applications is the notion of 'slerping', Spherical Linear Interpolation, for interpolating between two quaternions. The technique is an extension of the standard form of linear interpolation to work along spherical arcs ('moving' at constant angular velocity): essentially, rather than computing $(1-\alpha)v_0+\alpha v_1$, you instead compute $\dfrac{\sin((1-\alpha)\theta)}{\sin\theta}\mathbf{q}_0+\dfrac{\sin(\alpha\theta)}{\sin\theta}\mathbf{q}_1$, where $\cos\theta=\mathbf{q}_0\cdot\mathbf{q}_1$ (i.e., $\theta$ is the angle between the two vectors). It's a nice exercise to prove that this produces a unit quaternion, and that it reduces to linear interpolation in the limit of small $\theta$.

Since every step of the EMA filter is an interpolation between the previous smoothed value and the new value, you can use slerping to perform those interpolations on your quaternion data. The operation is somewhat expensive for real-time calculation (it involves an inverse trigonometric function evaluation and multiple trig function evaluations), but it's so fundamental that many articles have been written on making faster approximate versions, and searching for 'optimized slerp' or 'fast slerp' should yield plenty of good results for you.

  • 1
    That's a good point. However, the reason the EMA filter works is that the exponentially weighted average of all the data is equivalent to a weighted average of the previous EMA output and the current value. That's no longer true for weighted averages of quaternions. –  Mar 24 '15 at 19:24
  • @Rahul I'm not sure what you mean by 'works' in this context. It's true that the equivalence between a weighted sum of all previous values and the interpolation between most recent value and most recent weighted sum is only true in the linear case, but both are really just approximate solutions to a differential equation, and I don't see any a priori reason why the full sum $f(n) = \sum_i w_ia_i$ is any more valid a formula than $f(n)=\mathop{lerp}(f(n-1),a_n)$... – Steven Stadnicki Mar 24 '15 at 19:31
  • 1
    Fair enough; it depends on whether one views EMA as a local filter that happens to have a moving-average interpretation, or as a particular moving average that happens to have a simple implementation as a local filter. I guess that's why we disagree on what the natural generalization is. –  Mar 24 '15 at 20:09
  • 1
    @Rahul I tend to think of it as an approximate solution to a differential equation that models a natural 'drag' factor being applied to a given signal, so the IIR/'feedback' interpretation tends to be the natural one for me; the characterization as a decaying moving average/FIR filter is then just something that falls out conveniently from the math. Agreed that it's largely a matter of interpretation, though! – Steven Stadnicki Mar 24 '15 at 21:04
  • @StevenStadnicki So if I understand this discussion correctly, I wouldn't notice any major differences between EMA and Slerp? – Simon Verbeke Mar 25 '15 at 09:08
  • @SimonVerbeke It would be better to say that the slerp-based approach is one way of implementing an EMA filter; it corresponds directly to the 'simplest form' formula on the Wikipedia page that you link. The core operation in that form is 'give me a value that's $\alpha$ of the way between this value and this value', and slerp is the direct quaternion analog of that. (Note that something similar comes up in generating Bezier curves, which can be thought of as a small tree of lerps (or slerps); see http://en.wikipedia.org/wiki/De_Casteljau's_algorithm and note the similarity. – Steven Stadnicki Mar 25 '15 at 15:19
  • @StevenStadnicki I did know about Slerp, but didn't think of applying it in a way similar to how an EMA filter works. Thanks! – Simon Verbeke Mar 25 '15 at 19:59