implementation of the signed 16-bit lookup table style arctan function in C, take sin and cos as arguments, where 32767 represents 1
时间: 2024-02-03 22:02:06 浏览: 25
Here is an implementation of the signed 16-bit lookup table style arctan function in C:
```c
#include <stdint.h>
#define ATAN_LUT_SIZE 256
const int16_t atan_lut[ATAN_LUT_SIZE] = {
0, 101, 202, 303, 404, 505, 606, 707,
808, 909, 1010, 1111, 1212, 1313, 1414, 1515,
1616, 1717, 1818, 1919, 2020, 2121, 2222, 2323,
2424, 2525, 2626, 2727, 2828, 2929, 3030, 3131,
3232, 3333, 3434, 3535, 3636, 3737, 3838, 3939,
4040, 4141, 4242, 4343, 4444, 4545, 4646, 4747,
4848, 4949, 5050, 5151, 5252, 5353, 5454, 5555,
5656, 5757, 5858, 5959, 6060, 6161, 6262, 6363,
6464, 6565, 6666, 6767, 6868, 6969, 7070, 7171,
7272, 7373, 7474, 7575, 7676, 7777, 7878, 7979,
8080, 8181, 8282, 8383, 8484, 8585, 8686, 8787,
8888, 8989, 9090, 9191, 9292, 9393, 9494, 9595,
9696, 9797, 9898, 9999, 10100, 10202, 10303, 10404,
10505, 10606, 10707, 10808, 10909, 11010, 11111, 11212,
11313, 11414, 11515, 11616, 11717, 11818, 11919, 12020,
12121, 12222, 12323, 12424, 12525, 12626, 12727, 12828,
12929, 13030, 13131, 13232, 13333, 13434, 13535, 13636,
13737, 13838, 13939, 14040, 14141, 14242, 14343, 14444,
14545, 14646, 14747, 14848, 14949, 15050, 15151, 15252,
15353, 15454, 15555, 15656, 15757, 15858, 15959, 16060,
16161, 16262, 16363, 16464, 16565, 16666, 16767, 16868,
16969, 17070, 17171, 17272, 17373, 17474, 17575, 17676,
17777, 17878, 17979, 18080, 18181, 18282, 18383, 18484,
18585, 18686, 18787, 18888, 18989, 19090, 19191, 19292,
19393, 19494, 19595, 19696, 19797, 19898, 19999, 20100
};
int16_t atan2(int16_t sinval, int16_t cosval) {
int16_t angle;
if (cosval == 0) {
if (sinval > 0) {
angle = 90;
} else if (sinval < 0) {
angle = -90;
} else {
angle = 0;
}
} else {
int16_t tanval = sinval / cosval;
if (cosval > 0) {
if (sinval >= 0) {
angle = atan_lut[tanval];
} else {
angle = -atan_lut[-tanval];
}
} else {
angle = 180;
if (sinval >= 0) {
angle -= atan_lut[-tanval];
} else {
angle += atan_lut[tanval];
}
}
}
return angle;
}
```
The `atan_lut` array is a lookup table containing the arctan values for angles between -1 and 1 (inclusive), with a resolution of 0.0078 degrees (i.e., 256 values per 180 degrees). The `atan2` function takes the sine and cosine values of an angle (where 32767 represents 1) and returns the angle in degrees (-180 to 180). If the cosine value is zero, the function returns either 90 or -90 depending on the sign of the sine value. If the cosine value is positive, the arctan value is looked up in the `atan_lut` array; if the sine value is negative, the negated arctan value is used instead. If the cosine value is negative, the angle is adjusted to be in the second or third quadrant before looking up the arctan value.