diff options
Diffstat (limited to 'apps/plugins/lib')
-rw-r--r-- | apps/plugins/lib/fixedpoint.c | 66 | ||||
-rw-r--r-- | apps/plugins/lib/fixedpoint.h | 3 |
2 files changed, 68 insertions, 1 deletions
diff --git a/apps/plugins/lib/fixedpoint.c b/apps/plugins/lib/fixedpoint.c index 56597c1364..cf69d9c31b 100644 --- a/apps/plugins/lib/fixedpoint.c +++ b/apps/plugins/lib/fixedpoint.c | |||
@@ -60,6 +60,21 @@ static const unsigned long atan_table[] = { | |||
60 | 0x00000000, /* +0.000000000 */ | 60 | 0x00000000, /* +0.000000000 */ |
61 | }; | 61 | }; |
62 | 62 | ||
63 | /* Precalculated sine and cosine * 16384 (2^14) (fixed point 18.14) */ | ||
64 | static const short sin_table[91] = | ||
65 | { | ||
66 | 0, 285, 571, 857, 1142, 1427, 1712, 1996, 2280, 2563, | ||
67 | 2845, 3126, 3406, 3685, 3963, 4240, 4516, 4790, 5062, 5334, | ||
68 | 5603, 5871, 6137, 6401, 6663, 6924, 7182, 7438, 7691, 7943, | ||
69 | 8191, 8438, 8682, 8923, 9161, 9397, 9630, 9860, 10086, 10310, | ||
70 | 10531, 10748, 10963, 11173, 11381, 11585, 11785, 11982, 12175, 12365, | ||
71 | 12550, 12732, 12910, 13084, 13254, 13420, 13582, 13740, 13894, 14043, | ||
72 | 14188, 14329, 14466, 14598, 14725, 14848, 14967, 15081, 15190, 15295, | ||
73 | 15395, 15491, 15582, 15668, 15749, 15825, 15897, 15964, 16025, 16082, | ||
74 | 16135, 16182, 16224, 16261, 16294, 16321, 16344, 16361, 16374, 16381, | ||
75 | 16384 | ||
76 | }; | ||
77 | |||
63 | /** | 78 | /** |
64 | * Implements sin and cos using CORDIC rotation. | 79 | * Implements sin and cos using CORDIC rotation. |
65 | * | 80 | * |
@@ -136,3 +151,54 @@ long fsqrt(long a, unsigned int fracbits) | |||
136 | return b; | 151 | return b; |
137 | } | 152 | } |
138 | 153 | ||
154 | /** | ||
155 | * Fixed point sinus using a lookup table | ||
156 | * don't forget to divide the result by 16384 to get the actual sinus value | ||
157 | * @param val sinus argument in degree | ||
158 | * @return sin(val)*16384 | ||
159 | */ | ||
160 | long sin_int(int val) | ||
161 | { | ||
162 | val = (val+360)%360; | ||
163 | if (val < 181) | ||
164 | { | ||
165 | if (val < 91)/* phase 0-90 degree */ | ||
166 | return (long)sin_table[val]; | ||
167 | else/* phase 91-180 degree */ | ||
168 | return (long)sin_table[180-val]; | ||
169 | } | ||
170 | else | ||
171 | { | ||
172 | if (val < 271)/* phase 181-270 degree */ | ||
173 | return -(long)sin_table[val-180]; | ||
174 | else/* phase 270-359 degree */ | ||
175 | return -(long)sin_table[360-val]; | ||
176 | } | ||
177 | return 0; | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * Fixed point cosinus using a lookup table | ||
182 | * don't forget to divide the result by 16384 to get the actual cosinus value | ||
183 | * @param val sinus argument in degree | ||
184 | * @return cos(val)*16384 | ||
185 | */ | ||
186 | long cos_int(int val) | ||
187 | { | ||
188 | val = (val+360)%360; | ||
189 | if (val < 181) | ||
190 | { | ||
191 | if (val < 91)/* phase 0-90 degree */ | ||
192 | return (long)sin_table[90-val]; | ||
193 | else/* phase 91-180 degree */ | ||
194 | return -(long)sin_table[val-90]; | ||
195 | } | ||
196 | else | ||
197 | { | ||
198 | if (val < 271)/* phase 181-270 degree */ | ||
199 | return -(long)sin_table[270-val]; | ||
200 | else/* phase 270-359 degree */ | ||
201 | return (long)sin_table[val-270]; | ||
202 | } | ||
203 | return 0; | ||
204 | } | ||
diff --git a/apps/plugins/lib/fixedpoint.h b/apps/plugins/lib/fixedpoint.h index ff773cb0c5..c58798930a 100644 --- a/apps/plugins/lib/fixedpoint.h +++ b/apps/plugins/lib/fixedpoint.h | |||
@@ -21,4 +21,5 @@ | |||
21 | 21 | ||
22 | long fsincos(unsigned long phase, long *cos); | 22 | long fsincos(unsigned long phase, long *cos); |
23 | long fsqrt(long a, unsigned int fracbits); | 23 | long fsqrt(long a, unsigned int fracbits); |
24 | 24 | long cos_int(int val); | |
25 | long sin_int(int val); | ||