|
| 1 | +# CORDIC |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +Trigonometric functions, magnitudes, and vector rotations are fundamental operations in motor |
| 6 | +control, radar processing, and navigation. On microcontrollers without a floating-point unit or |
| 7 | +hardware multiplier, library implementations of `sin`, `cos`, `atan2`, and `hypot` require |
| 8 | +expensive software-emulated multiplications. This limits their use in hard real-time loops where |
| 9 | +deterministic execution time is mandatory. |
| 10 | + |
| 11 | +CORDIC (COordinate Rotation DIgital Computer) solves this by expressing any planar rotation as a |
| 12 | +sum of progressively smaller elementary rotations, each of which requires only a bit-shift and an |
| 13 | +addition. The result is a trig engine that runs on shift-add hardware with a fixed, data-independent |
| 14 | +cycle count — exactly what a deterministic real-time loop demands. |
| 15 | + |
| 16 | +## Mathematical Theory |
| 17 | + |
| 18 | +### Elementary Rotations |
| 19 | + |
| 20 | +A rotation by angle $\theta$ in two dimensions transforms a vector $(x, y)$ to |
| 21 | + |
| 22 | +$$x' = x\cos\theta - y\sin\theta, \quad y' = y\cos\theta + x\sin\theta.$$ |
| 23 | + |
| 24 | +Factoring out $\cos\theta$ gives |
| 25 | + |
| 26 | +$$x' = \cos\theta\,(x - y\tan\theta), \quad y' = \cos\theta\,(y + x\tan\theta).$$ |
| 27 | + |
| 28 | +When $\tan\theta_i = \pm 2^{-i}$, the multiplication by $\tan\theta_i$ becomes a right-shift by $i$ |
| 29 | +bits. The angles $\theta_i = \arctan(2^{-i})$ form the **CORDIC angle table**. |
| 30 | + |
| 31 | +### Rotation Mode (sin/cos) |
| 32 | + |
| 33 | +Starting from $(x_0, y_0, z_0) = (K, 0, \theta)$, each iteration steers the residual angle $z$ |
| 34 | +toward zero: |
| 35 | + |
| 36 | +$$x_{i+1} = x_i - \sigma_i \, 2^{-i} y_i$$ |
| 37 | +$$y_{i+1} = y_i + \sigma_i \, 2^{-i} x_i$$ |
| 38 | +$$z_{i+1} = z_i - \sigma_i \, \theta_i$$ |
| 39 | + |
| 40 | +where $\sigma_i = \text{sign}(z_i)$. After $N$ iterations, $x_N \approx \cos\theta$ and |
| 41 | +$y_N \approx \sin\theta$. |
| 42 | + |
| 43 | +### Vectoring Mode (atan2/magnitude) |
| 44 | + |
| 45 | +Starting from $(x_0, y_0, z_0) = (x, y, 0)$, each iteration steers $y$ toward zero: |
| 46 | + |
| 47 | +$$\sigma_i = -\text{sign}(y_i)$$ |
| 48 | + |
| 49 | +After $N$ iterations, $z_N \approx \arctan(y/x)$ and $x_N \approx \|(x, y)\| / K$. |
| 50 | + |
| 51 | +### CORDIC Gain |
| 52 | + |
| 53 | +Each elementary rotation stretches the vector length by $\sqrt{1 + 2^{-2i}}$. The accumulated |
| 54 | +gain over $N$ iterations is |
| 55 | + |
| 56 | +$$A_N = \prod_{i=0}^{N-1} \sqrt{1 + 2^{-2i}}.$$ |
| 57 | + |
| 58 | +The constant $K = 1/A_N \approx 0.6073$ compensates for this growth. In rotation mode the initial |
| 59 | +$x$ is pre-scaled by $K$; in vectoring mode the final $x$ is multiplied by $K$. |
| 60 | + |
| 61 | +### Convergence Domain |
| 62 | + |
| 63 | +The convergence domain is $|z| \leq \sum_{i=0}^{N-1} \arctan(2^{-i})$. For $N = 16$ this exceeds |
| 64 | +$\pi/2$, so inputs outside $[-\pi/2, \pi/2]$ must be range-reduced by shifting the angle by $\pm\pi$ |
| 65 | +and inverting the output signs. Vectoring mode uses quadrant detection on the signs of $x$ and $y$ |
| 66 | +to handle the full $[-\pi, \pi]$ range. |
| 67 | + |
| 68 | +## Complexity Analysis |
| 69 | + |
| 70 | +| Metric | Value | |
| 71 | +|-------------|-------------------------------------------------------| |
| 72 | +| Time | $O(N)$ — exactly $N$ shift-add steps per call | |
| 73 | +| Space | $O(N)$ — angle table in ROM; $O(1)$ working registers | |
| 74 | +| Cycle count | Fixed, data-independent — no branch on input value | |
| 75 | + |
| 76 | +One additional bit of precision is gained per iteration. $N = 16$ yields approximately 16-bit |
| 77 | +accuracy; $N = 20$ reaches the limits of single-precision float. |
| 78 | + |
| 79 | +## Step-by-Step Walkthrough |
| 80 | + |
| 81 | +Compute $\sin(\pi/6) = 0.5$ with $N = 4$ for brevity (gain $K_4 \approx 0.6352$). |
| 82 | + |
| 83 | +| $i$ | $\theta_i$ | $\sigma_i$ | $x_i$ | $y_i$ | $z_i$ | |
| 84 | +|-----|------------|------------|--------|--------|---------| |
| 85 | +| — | — | — | 0.6352 | 0.0000 | 0.5236 | |
| 86 | +| 0 | 0.7854 | +1 | 0.6352 | 0.6352 | −0.2618 | |
| 87 | +| 1 | 0.4636 | −1 | 0.7940 | 0.3176 | 0.2018 | |
| 88 | +| 2 | 0.2450 | +1 | 0.7147 | 0.5122 | −0.0432 | |
| 89 | +| 3 | 0.1244 | −1 | 0.8425 | 0.4248 | 0.0812 | |
| 90 | + |
| 91 | +After iteration 3: $y_4 \approx 0.43$, improving toward 0.5 as $N$ grows. |
| 92 | + |
| 93 | +## Pitfalls & Edge Cases |
| 94 | + |
| 95 | +The input to rotation mode must lie within the convergence domain after range reduction. Angles |
| 96 | +at exactly $\pm\pi/2$ sit at the edge of the domain and may accumulate an extra half-ulp error. |
| 97 | + |
| 98 | +In vectoring mode, $(x, y) = (0, 0)$ is degenerate; by convention the angle is returned as zero |
| 99 | +rather than causing a division-by-zero or NaN. |
| 100 | + |
| 101 | +The shift $2^{-i}$ eventually underflows in floating-point for large $i$; iterations beyond |
| 102 | +$\lfloor -\log_2(\epsilon) \rfloor$ contribute nothing and can be capped without loss of accuracy. |
| 103 | + |
| 104 | +## Variants & Generalizations |
| 105 | + |
| 106 | +**Hyperbolic CORDIC** replaces the elementary angle table with $\tanh^{-1}(2^{-i})$ and handles |
| 107 | +`sinh`, `cosh`, `exp`, and `ln`. |
| 108 | + |
| 109 | +**Linear CORDIC** uses shifts alone (no rotation) to implement multiply and divide. |
| 110 | + |
| 111 | +**Double-rotation trick** repeats certain iterations to extend the convergence domain to $(-\pi, \pi]$ |
| 112 | +without a range-reduction step. |
| 113 | + |
| 114 | +## Applications |
| 115 | + |
| 116 | +- Field-oriented motor control: Park/Clarke transforms require `sin`/`cos` at carrier frequency. |
| 117 | +- Radar and sonar: Cartesian-to-polar conversion of sample streams. |
| 118 | +- Navigation: continuous `atan2` for heading on heading-constrained MCUs. |
| 119 | +- Audio synthesis: wavetable-free sine generation on FPU-less targets. |
| 120 | + |
| 121 | +## Connections to Other Algorithms |
| 122 | + |
| 123 | +`TrigonometricFunctions` provides a table-lookup alternative with lower iteration count but higher |
| 124 | +ROM usage for the same precision. `Quaternion` consumes CORDIC-generated `sin`/`cos` for axis-angle |
| 125 | +conversions. On targets with an FPU the standard library usually outperforms CORDIC; the advantage |
| 126 | +is exclusive to multiply-poor hardware. |
| 127 | + |
| 128 | +## References & Further Reading |
| 129 | + |
| 130 | +- J. E. Volder, "The CORDIC Trigonometric Computing Technique," *IRE Transactions on Electronic Computers*, EC-8(3), pp. 330–334, 1959. |
| 131 | +- R. Andraka, "A survey of CORDIC algorithms for FPGA-based computers," *Proc. ACM/SIGDA FPGA*, 1998, pp. 191–200. |
| 132 | +- J. S. Walther, "A unified algorithm for elementary functions," *AFIPS Spring Joint Computer Conference*, 1971. |
0 commit comments