# Derivation of fixed-point $\tt atan2$ with self-normalization

I'm trying to understand the maths behind this Fast fixed-point $\tt atan2$ calculation with self-normalization. In particular, equation $(2)$ for theta1 appears to provide a first-degree expansion of some series. Similarly, equation $(2a)$ under "QUICK NOTE" seems to expand this series.

• What is the series that's being expanded?
• How exactly is equation $(2)$ derived?

I had the exact question, nearly a decade later - and think I figured out the cool fixed-points tricks thanks to and edaboard thread and helpful write up in the IEEE Signal Processing Magazine.

First, some trig identities are used to transform the problem into one that is self-regularizing:

\begin{align} \tan(\pi/4 - \theta) &= \frac{\tan(\pi/4)+\tan(-\theta)}{1-\tan(\pi/4)\tan(-\theta)}\\ &= \frac{1-\tan(\theta)}{1+\tan(\theta)} \end{align}

Here $$\tan(\theta) = y/x$$, therefore

\begin{align} \tan(\pi/4 - \theta) &= \frac{1-y/x}{1+y/x} = \frac{x-y}{x+y}\\ \pi/4 - \theta &= \arctan \left( \frac{x-y}{x+y} \right) \\ \theta &= \pi/4 - \arctan \left( r \right) \\ \end{align}

where $$r = \frac{x-y}{x+y}$$. That's a great self-normalizing trick with zero approximations so far. Since we want $$\theta$$, we can solve something that is a function of $$r$$, which is already guaranteed to be $$-1 < r < 1$$ (in the first quadrant). Now we just need an easy expression for $$\arctan(r)$$ between $$-1 < r < 1$$. There's a bunch of techniques to do that, but the simplest is to just fit a line that goes through $$r=\{-1,0,1\}$$, the corresponding $$\arctan(r)$$ is $$\{-\pi/4,0,\pi/4\}$$. So a quick a dirty approximation gives us:

$$\arctan(r) \approx \frac{\pi}{4}r$$

put it all together (for the first quadrant):

$$\theta \approx \frac{\pi}{4} - \frac{\pi}{4}r$$

To get more accurate, you can get better polynomials to approximate $$\arctan(r)$$. Things like the Remez algorithm can help find those, and the IEEE Signal Processing Magazine also gives a method and compares polynomials.

it's an approximation.

a better approximation is in this answer:

$$\arctan(u) \approx \frac{u}{f(u^2)} \quad \quad -1 \le u \le 1$$

where

\begin{align} f(u^2)& = \sum\limits_{n=0}^{4} \ a_n \ u^{2n} \\ a_0 & = 1.0 \\ a_1 & = 0.33288950512027 \\ a_2 & = -0.08467922817644 \\ a_3 & = 0.03252232640125 \\ a_4 & = -0.00749305860992 \\ \end{align}

not as fast, but quite accurate. not bit-perfect accurate, but very accurate.

for atan2(), you need to put it in the correct tilted quadrant:

$$\operatorname{atan2}(y,x) = \arg\{ x + j\,y \} = \begin{cases} \arctan\left(\frac{y}{x}\right) &\text{if } 0 < |y| \le x, \\ \frac{\pi}{2} - \arctan\left(\frac{x}{y}\right) &\text{if } 0 < |x| \le y, \\ -\frac{\pi}{2} - \arctan\left(\frac{x}{y}\right) &\text{if } y \le -|x| < 0, \\ \arctan\left(\frac{y}{x}\right) \pm \pi &\text{if } x \le -|y| < 0, \\ \text{undefined} &\text{if } x = 0 \text{ and } y = 0. \end{cases}$$

this will require two fixed-point divisions, but you can arrange it so that the result is always no greater than 1 in magnitude (the numerator is always smaller in magnitude than the denominator).