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 Sine and Cosine

This section shows how the above techniques are applied to optimize the computations for a Sin + Cos function. Sin + Cos functions are commonly used in the transforms for control algorithms where both sin and cos are needed at the same time.

The first step in using polynomial approximations is to range reduce the input so that the segment of the function that needs to be modeled is smaller. The most common range reduction for sin/cos computation is to reduce the input to the range.

Equation 1. - π 2 x π 2

Then, compute the approximation in this region and then adjust the sign depending on which quadrant the angle was in originally. Using the Chebyshev polynomials and the fact that sin(x) is an odd function and cos(x) is an even function, you will see the following:

Equation 2. s i n x C 1 X +   C 3 X 3 +   C 5 X 5 +  
Equation 3. c o s x C 0 +   C 2 X 2 +   C 4 X 4 +  
GUID-20220314-SS0I-GMMR-HST8-NVVQRGPGZSXT-low.png Figure 2-1 Plot of Sine and Cosine Over the Range

If you assume that the input to the functions are limited to [ 0 : 2 π ] , you need to map the input value to the range - π 2 x π 2 before implementing the approximation. This can be done with a couple simple comparisons:

Equation 4. i f   x > 3 π 2   ,   t h e n   x = x - 2 π
Equation 5. e l s e     i f   π 2 < x < 3 π 2 ,     t h e n   x = π - x ,   a n d   c o s ( x )   =   -   a p p r o x ( c o s ( x ) )

A further range reduction technique can be used to limit the input to - π 4 x π 4 using the trigonometric identities:

Equation 6. s i n ( π 2 - θ ) = c o s ( θ )  
Equation 7. c o s ( π 2 - θ ) = s i n ( θ )

Which yields the following mapping for sin(x) and a similar one for cos(x):

GUID-20220314-SS0I-PMMM-CVTC-VHWH5VT4L2VG-low.png Figure 2-2 Mapping of the Unit Circle for sin(x) for 0 x 2 π

Since you only have to model the sine cosine from - π 4 x π 4 , you need fewer coefficients to achieve higher accuracy.

GUID-20220314-SS0I-J46L-FCDF-WVMZZJRBG4SL-low.png Figure 2-3 Plot of Sine and Cosine Over the Range