SPRAD27A July   2022  – August 2022 AM2431 , AM2432 , AM2434 , AM2631 , AM2631-Q1 , AM2632 , AM2632-Q1 , AM2634 , AM2634-Q1 , AM2732 , AM2732-Q1 , AM6411 , AM6412 , AM6421 , AM6422 , AM6441 , AM6442

 

  1.   Abstract
  2.   Trademarks
  3. 1Introduction
  4. 2Trigonometric Optimizations
    1. 2.1 Lookup Table-Based Approximation
    2. 2.2 Polynomial Approximation
      1. 2.2.1 Optimizing Sine and Cosine
        1. 2.2.1.1 Sine Cosine Polynomials From Sollya
      2. 2.2.2 Optimizing Arctangent and Arctangent2
        1. 2.2.2.1 Arctangent Polynomials
  5. 3Trig Library Benchmarks
    1. 3.1 C Math.h Library
    2. 3.2 Arm “Fast Math Functions” in CMSIS
    3. 3.3 TI Arm Trig Library
    4. 3.4 Table of Results
  6. 4Optimizations
    1. 4.1 Branch Prediction
    2. 4.2 Floating-Point Single-Precision Instructions
    3. 4.3 Memory Placement
    4. 4.4 Compiler
  7.   Revision History

Optimizing Arctangent and Arctangent2

The arctangent function, and in particular, the arctangent2 function is a critical function in control applications. For example, in motor control, there may be sensors used to get the x and y position of a motor. Then, the application needs to translate that into an angular value. A standard arctan function would be called as arctan(y/x), but this loses the quadrant information as Q1 and Q3 are both positive and Q2 and Q4 are both negative. The arctan2 function accepts both the x and the y input to return a value in the full −π to π range versus the arctan function that returns values only in Q1 or Q4, −π/2 to π/2.

GUID-3D72F46E-4E8B-4555-8B9C-20B78AC22555-low.png Figure 2-4 Plot of arctan2(y,x)

As the input to arctan(z) can be any number from −∞to ∞, use trig identities to reduce the range and get an approximation function that is relatively low complexity.

Start with:

  • arctan(x) = π/2- arctan(1/x) (1a)
  • arctan(x)=−arctan(x) (2a)

These identities allow you to restrict the approximation range to abs(x) <=1.

The next step is to find a simple polynomial that approximates arctan in the range 0-1, or -1 to 1. Using sollya, try some different polynomial lengths to get the approximation error. This is shown in Table 2-3. You can see that the error is not falling that fast as a function of the number of terms in the polynomial, so even at 6 terms you are still at 3.4e-6 in the range (-1,1). Also note that arctan is an odd function where all the terms are odd powers.