Rev 308 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
256 | killagreg | 1 | #include "91x_lib.h" |
2 | #include "mymath.h" |
||
3 | |||
4 | // discrete mathematics |
||
5 | |||
6 | // sinus with argument in degree at an angular resolution of 1 degree and a discretisation of 13 bit. |
||
7 | const s16 sinlookup[91] = {0, 143, 286, 429, 571, 714, 856, 998, 1140, 1282, 1423, 1563, 1703, 1843, 1982, 2120, 2258, 2395, 2531, 2667, 2802, 2936, 3069, 3201, 3332, 3462, 3591, 3719, 3846, 3972, 4096, 4219, 4341, 4462, 4581, 4699, 4815, 4930, 5043, 5155, 5266, 5374, 5482, 5587, 5691, 5793, 5893, 5991, 6088, 6183, 6275, 6366, 6455, 6542, 6627, 6710, 6791, 6870, 6947, 7022, 7094, 7165, 7233, 7299, 7363, 7424, 7484, 7541, 7595, 7648, 7698, 7746, 7791, 7834, 7875, 7913, 7949, 7982, 8013, 8041, 8068, 8091, 8112, 8131, 8147, 8161, 8172, 8181, 8187, 8191, 8192}; |
||
8 | |||
9 | s16 c_sin_8192(s16 angle) |
||
10 | { |
||
11 | s8 m,n; |
||
12 | s16 sinus; |
||
13 | |||
14 | // avoid negative angles |
||
15 | if (angle < 0) |
||
16 | { |
||
17 | m = -1; |
||
18 | angle = -angle; |
||
19 | } |
||
20 | else m = +1; |
||
21 | |||
22 | // fold angle to interval 0 to 359 |
||
23 | angle %= 360; |
||
24 | |||
25 | // check quadrant |
||
26 | if (angle <= 90) n = 1; // first quadrant |
||
27 | else if ((angle > 90) && (angle <= 180)) {angle = 180 - angle; n = 1;} // second quadrant |
||
28 | else if ((angle > 180) && (angle <= 270)) {angle = angle - 180; n = -1;} // third quadrant |
||
29 | else {angle = 360 - angle; n = -1;} //fourth quadrant |
||
30 | // get lookup value |
||
31 | sinus = sinlookup[angle]; |
||
32 | // calculate sinus value |
||
33 | return (sinus * m * n); |
||
34 | } |
||
35 | |||
36 | // cosinus with argument in degree at an angular resolution of 1 degree and a discretisation of 13 bit. |
||
37 | s16 c_cos_8192(s16 angle) |
||
38 | { |
||
39 | return (c_sin_8192(90 - angle)); |
||
40 | } |
||
41 | |||
293 | killagreg | 42 | // higher resolution angle in deg is arg/div |
43 | s16 c_sin_8192_res(s16 arg, s16 div) |
||
44 | { |
||
45 | s16 angle, rest; |
||
46 | s32 tmp; |
||
47 | |||
48 | angle = arg/div; |
||
49 | rest = arg%div; |
||
50 | |||
51 | if(rest>0) |
||
52 | { |
||
53 | tmp = (div-rest)*(s32)c_sin_8192(angle); |
||
54 | tmp += rest * (s32)c_sin_8192(angle+1); |
||
55 | tmp /= div; |
||
56 | return(tmp); |
||
57 | } |
||
58 | else if(rest<0) |
||
59 | { |
||
60 | tmp = (div+rest)*(s32)c_sin_8192(angle); |
||
61 | tmp -= rest * (s32)c_sin_8192(angle-1); |
||
62 | tmp /= div; |
||
63 | return(tmp); |
||
64 | } |
||
65 | else |
||
66 | { |
||
67 | return(c_sin_8192(angle)); |
||
68 | } |
||
69 | |||
70 | } |
||
71 | |||
72 | s16 c_cos_8192_res(s16 arg, s16 div) |
||
73 | { |
||
74 | return(c_sin_8192_res(90*div - arg, div)); |
||
75 | } |
||
76 | |||
77 | |||
256 | killagreg | 78 | // integer based atan2 that returns angle in counts of 1/546.13° |
293 | killagreg | 79 | s32 c_atan2_546(s32 y, s32 x) |
256 | killagreg | 80 | { |
81 | s32 qx, qy, q; |
||
82 | |||
83 | if( x < 0) qx = -x; |
||
84 | else qx = x; |
||
85 | if( y < 0) qy = -y; |
||
86 | else qy = y; |
||
87 | if(qy <= qx) |
||
88 | { // scale down to avoid overflow in quadratic interpolation |
||
89 | while(qy > (1<<15)) |
||
90 | { |
||
91 | qy/=2; |
||
92 | qx/=2; |
||
93 | } |
||
94 | // calculate the quatratic interpolation |
||
95 | q = (((qy<<13)/qx)*qy)/qx; |
||
96 | q = (qy<<15)/qx-q; |
||
97 | } |
||
98 | else |
||
99 | { // scale down to avoid overflow in quadratic interpolation |
||
100 | while(qx>(1<<15)) |
||
101 | { |
||
102 | qy/=2; |
||
103 | qx/=2; |
||
104 | } |
||
105 | // calculate the quatratic interpolation |
||
106 | q = (((qx<<13)/qy)*qx)/qy; |
||
107 | q = (qx<<15)/qy-q; |
||
108 | q = 2*((1<<15)-(1<<13)) - q; |
||
109 | } |
||
110 | if(y < 0) |
||
111 | { |
||
112 | if(x < 0) q = q - 4*((1<<15)-(1<<13)); |
||
113 | else q = -q; |
||
114 | } |
||
115 | else if( x < 0) q = 4*((1<<15)-(1<<13)) - q; |
||
116 | return(q); |
||
117 | } |