This repository provides a function to compute sine and cosine for data encoded as fix point.
This algorithm is an implementation of the CORDIC algorithm.
To find the sine and cosine of an angle
The principle of the algorithm is to converge a vector
At each rotation, the coordinates of the vector
Let’s assume that a set of
The vector
So vector
with
The relation between
The angles
The relation between
So the rotation can be computed using division by a power of two which can be implemented as a bit shift.
The values
So they can be ignored during each rotation and factorized into a multiplier
coefficient that depends on
This part gives explaination about the content of the source files.
The input of the function is an angle encoded on a 16 bits unsigned integer.
- It resolution is
$2^{-13}$ radian. - The minimum value is
$0$ . - The maximum value
$2\pi$ is encoded with the integer$51472$ ($51472\times2^{-13} = 6,28320$ ).
The outputs of the function are the sine and the cosine of the angle encoded on a 16 bits signed integer.
- Their resolution is
$2^{-14}$ . - The minimum value
$-1$ is encoded with the integer$-16384$ . - The maximum value
$1$ is encoded with the integer$16384$ .
The first step of the algorithm is to reduce the range of the input angle
If
$cos(\alpha) = cos(2\pi-\alpha)$ $sin(\alpha) = -sin(2\pi-\alpha)$
If
$cos(\alpha) = -cos(\alpha-\pi)$ $sin(\alpha) = -sin(\alpha-\pi)$
If
$cos(\alpha) = -cos(\pi-\alpha)$ $sin(\alpha) = sin(\pi-\alpha)$
To increase the precision of the algorithm, the rotations are computed on
32 bits variables.
Its is done by multiplicating by 65536 the implicated variables :
- the angle
$\alpha$ , - the angles
$\gamma_{i}$ - the sum of the rotation
- the coordinates of the vector
$\overrightarrow{v}$
With this choice, and knowing that the resolution of an angle is
The list of the
i | Integer | |
---|---|---|
0 | 0,785398163397448 | 421657428 |
1 | 0,463647609000806 | 248918914 |
2 | 0,244978663126864 | 131521918 |
3 | 0,124354994546761 | 66762579 |
4 | 0,062418809995957 | 33510843 |
5 | 0,031239833430268 | 16771758 |
6 | 0,015623728620477 | 8387925 |
7 | 0,007812341060101 | 4194219 |
8 | 0,003906230131967 | 2097141 |
9 | 0,001953122516479 | 1048575 |
10 | 0,000976562189559 | 524288 |
11 | 0,000488281211195 | 262144 |
12 | 0,000244140620149 | 131072 |
13 | 0,000122070311894 | 65536 |
14 | 0,000061035156174 | 32767 |
15 | 0,000030517578115 | 16384 |
16 | 0,000015258789061 | 8192 |
17 | 0,000007629394531 | 4096 |
18 | 0,000003814697265 | 2048 |
19 | 0,000001907348632 | 1024 |
20 | 0,000000953674316 | 512 |
21 | 0,000000476837158 | 256 |
22 | 0,000000238418579 | 128 |
23 | 0,000000119209289 | 64 |
24 | 0,000000059604644 | 32 |
25 | 0,000000029802322 | 16 |
26 | 0,000000014901161 | 8 |
27 | 0,000000007450580 | 4 |
28 | 0,000000003725290 | 2 |
29 | 0,000000001862645 | 1 |
The
For
To earn a rotation step, the first rotation angle
Its coordinates are :
To earn a multiplication, the multiplier coefficient is applied at the
initialization of these variables.
As the first rotation angle is not
So the integer software values of the modified coordinates of
The initial value of the sum of the rotations is :