# Intro

Computing sin(), and cos() function are heavily needed in signal processing systems, FFT, DCT. Usually, we relly on the math.h package in C for the floating-point computation.

However, the full-precesion calculation for such functions (in math.h) could be very time consuming. Two methods are compared here, mainly for the complexity vs. error.

# CORDIC and Polynomical

• CORDIC is believed to be superior when there’s no hardware multipliers available, because it only need international shift and add.
• Polynomial Approximation uses the Taylor series to calculate the sin() and cos().

A quick note on CORDIC can be found in this Wikipeadia page on CORDIC. It is an iterative approach that each iteration reduces the error.

Using Taylor series (that expand at 0), the sin() and cos() can be represented as:
$$cos(x)=1-\frac{x^2}{2!}+\frac{x^4}{4!}-\frac{x^6}{6!}+ …$$
$$sin(x)=x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+…$$
The result becomes more accurate along with the increase of orders. For the example, cos(x) use 6 orders, while sin(x) uses 7 orders.

For sin() and cos(), the error can be quantified as by the number of binary bits (nb), which represent the dynamical range:

$$nb = log_2\frac{range}{resolution}=log_2\frac{max-min}{2*error}=log_2\frac{1-(-1)}{2*error}$$

• The “CORRECT” number is given by the matlab function “sin()”, (possiblility using the .math.h C function) which might not be so accurate in iteself. But it is believed to be accurate enough to calculate the error of our scheme (since it’s much more accurate than these 2 approaches mentioned before).

• When calculating the value of sin(), we are using the format of (64-bit) double floating point in matlab. This might be very different if you are implemented that in a FPGA or ASIC, when you might be restricted to use a fixed-point ALU. Since normally the calculation precision is less than in this test, your error will be larger in reality.

• the “order” in the polynomial approximation means the maximum number of order in the Taylor series. A n-order polynomial results only adds up n/2 items($\frac{x^n}{n!}$).

# result

The dynamical ranges of those two approaches are listed below, where the input ranges form $-\pi/2$ to $\pi/2$.  For CORDIC, the error is more uniform across inputs range. However, for Taylor series, the more distant form the expanding root (0), the larger the error. To reduce the error around $\pi/2$, larger inputs are flipped.

Rule of thumb results is that: 30 CORDIC iterations results to 30-bit resolutions, which is about 10 taylor iterations.

The complexity for 30-bits is compared below. Each CORDIC iteration need 2 additions and a comparison with 0. The terms for Taylor series ($\frac{1}{n!}$) are pre-stored, so for each order, we need 2 multiplications, one for $x^n$ (generated form $x^(n-2)$), one for $\frac{1}{n!}$.

add. compare mul.
CORDIC 60 (30x2) 30 0
Taylor polynominal 5 (10/2) 0 10 (10/2x2)

# Sum

The error plot can be used to understand the usage of CORDIC and Taylor approximation.
So generally:

• CORDIC is prefered if no FPU is presented.
• Taylor can be faster if FPU is presented, since for FPU, the latency for mul. and add. are more or less equal.
• For low-power, CORDIC is prefered, since add, especially fixed-point add is much more cheaper than FPU. But it takes longer latencies, and if the control power dominates (no execution power), then Taylor should be used.

# Code

Compare error script:

CORDIC function (reference: jacoby, or wikipedia )

taylor approximation function: