1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
3 *
4 * $Date:        12. March 2014
5 * $Revision:    V1.4.4
6 *
7 * Project:      CMSIS DSP Library
8 * Title:        arm_sin_f32.c
9 *
10 * Description:  Fast sine calculation for floating-point values.
11 *               Fast cosine calculation for floating-point values.
12 *
13 *
14 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 *   - Redistributions of source code must retain the above copyright
20 *     notice, this list of conditions and the following disclaimer.
21 *   - Redistributions in binary form must reproduce the above copyright
22 *     notice, this list of conditions and the following disclaimer in
23 *     the documentation and/or other materials provided with the
24 *     distribution.
25 *   - Neither the name of ARM LIMITED nor the names of its contributors
26 *     may be used to endorse or promote products derived from this
27 *     software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
33 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
35 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 * -------------------------------------------------------------------- */
42 
43 #include <stdint.h>
44 #include <nanohub_math.h>
45 
46 #define FAST_MATH_TABLE_SIZE  512
47 typedef float float32_t;
48 
49 /**
50  * \par
51  * Example code for the generation of the floating-point sine table:
52  * <pre>
53  * tableSize = 512;
54  * for(n = 0; n < (tableSize + 1); n++)
55  * {
56  *	sinTable[n]=sin(2*pi*n/tableSize);
57  * }</pre>
58  * \par
59  * where pi value is  3.14159265358979
60  */
61 
62 static const float32_t sinTable_f32[FAST_MATH_TABLE_SIZE + 1] = {
63    0.00000000f, 0.01227154f, 0.02454123f, 0.03680722f, 0.04906767f, 0.06132074f,
64    0.07356456f, 0.08579731f, 0.09801714f, 0.11022221f, 0.12241068f, 0.13458071f,
65    0.14673047f, 0.15885814f, 0.17096189f, 0.18303989f, 0.19509032f, 0.20711138f,
66    0.21910124f, 0.23105811f, 0.24298018f, 0.25486566f, 0.26671276f, 0.27851969f,
67    0.29028468f, 0.30200595f, 0.31368174f, 0.32531029f, 0.33688985f, 0.34841868f,
68    0.35989504f, 0.37131719f, 0.38268343f, 0.39399204f, 0.40524131f, 0.41642956f,
69    0.42755509f, 0.43861624f, 0.44961133f, 0.46053871f, 0.47139674f, 0.48218377f,
70    0.49289819f, 0.50353838f, 0.51410274f, 0.52458968f, 0.53499762f, 0.54532499f,
71    0.55557023f, 0.56573181f, 0.57580819f, 0.58579786f, 0.59569930f, 0.60551104f,
72    0.61523159f, 0.62485949f, 0.63439328f, 0.64383154f, 0.65317284f, 0.66241578f,
73    0.67155895f, 0.68060100f, 0.68954054f, 0.69837625f, 0.70710678f, 0.71573083f,
74    0.72424708f, 0.73265427f, 0.74095113f, 0.74913639f, 0.75720885f, 0.76516727f,
75    0.77301045f, 0.78073723f, 0.78834643f, 0.79583690f, 0.80320753f, 0.81045720f,
76    0.81758481f, 0.82458930f, 0.83146961f, 0.83822471f, 0.84485357f, 0.85135519f,
77    0.85772861f, 0.86397286f, 0.87008699f, 0.87607009f, 0.88192126f, 0.88763962f,
78    0.89322430f, 0.89867447f, 0.90398929f, 0.90916798f, 0.91420976f, 0.91911385f,
79    0.92387953f, 0.92850608f, 0.93299280f, 0.93733901f, 0.94154407f, 0.94560733f,
80    0.94952818f, 0.95330604f, 0.95694034f, 0.96043052f, 0.96377607f, 0.96697647f,
81    0.97003125f, 0.97293995f, 0.97570213f, 0.97831737f, 0.98078528f, 0.98310549f,
82    0.98527764f, 0.98730142f, 0.98917651f, 0.99090264f, 0.99247953f, 0.99390697f,
83    0.99518473f, 0.99631261f, 0.99729046f, 0.99811811f, 0.99879546f, 0.99932238f,
84    0.99969882f, 0.99992470f, 1.00000000f, 0.99992470f, 0.99969882f, 0.99932238f,
85    0.99879546f, 0.99811811f, 0.99729046f, 0.99631261f, 0.99518473f, 0.99390697f,
86    0.99247953f, 0.99090264f, 0.98917651f, 0.98730142f, 0.98527764f, 0.98310549f,
87    0.98078528f, 0.97831737f, 0.97570213f, 0.97293995f, 0.97003125f, 0.96697647f,
88    0.96377607f, 0.96043052f, 0.95694034f, 0.95330604f, 0.94952818f, 0.94560733f,
89    0.94154407f, 0.93733901f, 0.93299280f, 0.92850608f, 0.92387953f, 0.91911385f,
90    0.91420976f, 0.90916798f, 0.90398929f, 0.89867447f, 0.89322430f, 0.88763962f,
91    0.88192126f, 0.87607009f, 0.87008699f, 0.86397286f, 0.85772861f, 0.85135519f,
92    0.84485357f, 0.83822471f, 0.83146961f, 0.82458930f, 0.81758481f, 0.81045720f,
93    0.80320753f, 0.79583690f, 0.78834643f, 0.78073723f, 0.77301045f, 0.76516727f,
94    0.75720885f, 0.74913639f, 0.74095113f, 0.73265427f, 0.72424708f, 0.71573083f,
95    0.70710678f, 0.69837625f, 0.68954054f, 0.68060100f, 0.67155895f, 0.66241578f,
96    0.65317284f, 0.64383154f, 0.63439328f, 0.62485949f, 0.61523159f, 0.60551104f,
97    0.59569930f, 0.58579786f, 0.57580819f, 0.56573181f, 0.55557023f, 0.54532499f,
98    0.53499762f, 0.52458968f, 0.51410274f, 0.50353838f, 0.49289819f, 0.48218377f,
99    0.47139674f, 0.46053871f, 0.44961133f, 0.43861624f, 0.42755509f, 0.41642956f,
100    0.40524131f, 0.39399204f, 0.38268343f, 0.37131719f, 0.35989504f, 0.34841868f,
101    0.33688985f, 0.32531029f, 0.31368174f, 0.30200595f, 0.29028468f, 0.27851969f,
102    0.26671276f, 0.25486566f, 0.24298018f, 0.23105811f, 0.21910124f, 0.20711138f,
103    0.19509032f, 0.18303989f, 0.17096189f, 0.15885814f, 0.14673047f, 0.13458071f,
104    0.12241068f, 0.11022221f, 0.09801714f, 0.08579731f, 0.07356456f, 0.06132074f,
105    0.04906767f, 0.03680722f, 0.02454123f, 0.01227154f, 0.00000000f, -0.01227154f,
106    -0.02454123f, -0.03680722f, -0.04906767f, -0.06132074f, -0.07356456f,
107    -0.08579731f, -0.09801714f, -0.11022221f, -0.12241068f, -0.13458071f,
108    -0.14673047f, -0.15885814f, -0.17096189f, -0.18303989f, -0.19509032f,
109    -0.20711138f, -0.21910124f, -0.23105811f, -0.24298018f, -0.25486566f,
110    -0.26671276f, -0.27851969f, -0.29028468f, -0.30200595f, -0.31368174f,
111    -0.32531029f, -0.33688985f, -0.34841868f, -0.35989504f, -0.37131719f,
112    -0.38268343f, -0.39399204f, -0.40524131f, -0.41642956f, -0.42755509f,
113    -0.43861624f, -0.44961133f, -0.46053871f, -0.47139674f, -0.48218377f,
114    -0.49289819f, -0.50353838f, -0.51410274f, -0.52458968f, -0.53499762f,
115    -0.54532499f, -0.55557023f, -0.56573181f, -0.57580819f, -0.58579786f,
116    -0.59569930f, -0.60551104f, -0.61523159f, -0.62485949f, -0.63439328f,
117    -0.64383154f, -0.65317284f, -0.66241578f, -0.67155895f, -0.68060100f,
118    -0.68954054f, -0.69837625f, -0.70710678f, -0.71573083f, -0.72424708f,
119    -0.73265427f, -0.74095113f, -0.74913639f, -0.75720885f, -0.76516727f,
120    -0.77301045f, -0.78073723f, -0.78834643f, -0.79583690f, -0.80320753f,
121    -0.81045720f, -0.81758481f, -0.82458930f, -0.83146961f, -0.83822471f,
122    -0.84485357f, -0.85135519f, -0.85772861f, -0.86397286f, -0.87008699f,
123    -0.87607009f, -0.88192126f, -0.88763962f, -0.89322430f, -0.89867447f,
124    -0.90398929f, -0.90916798f, -0.91420976f, -0.91911385f, -0.92387953f,
125    -0.92850608f, -0.93299280f, -0.93733901f, -0.94154407f, -0.94560733f,
126    -0.94952818f, -0.95330604f, -0.95694034f, -0.96043052f, -0.96377607f,
127    -0.96697647f, -0.97003125f, -0.97293995f, -0.97570213f, -0.97831737f,
128    -0.98078528f, -0.98310549f, -0.98527764f, -0.98730142f, -0.98917651f,
129    -0.99090264f, -0.99247953f, -0.99390697f, -0.99518473f, -0.99631261f,
130    -0.99729046f, -0.99811811f, -0.99879546f, -0.99932238f, -0.99969882f,
131    -0.99992470f, -1.00000000f, -0.99992470f, -0.99969882f, -0.99932238f,
132    -0.99879546f, -0.99811811f, -0.99729046f, -0.99631261f, -0.99518473f,
133    -0.99390697f, -0.99247953f, -0.99090264f, -0.98917651f, -0.98730142f,
134    -0.98527764f, -0.98310549f, -0.98078528f, -0.97831737f, -0.97570213f,
135    -0.97293995f, -0.97003125f, -0.96697647f, -0.96377607f, -0.96043052f,
136    -0.95694034f, -0.95330604f, -0.94952818f, -0.94560733f, -0.94154407f,
137    -0.93733901f, -0.93299280f, -0.92850608f, -0.92387953f, -0.91911385f,
138    -0.91420976f, -0.90916798f, -0.90398929f, -0.89867447f, -0.89322430f,
139    -0.88763962f, -0.88192126f, -0.87607009f, -0.87008699f, -0.86397286f,
140    -0.85772861f, -0.85135519f, -0.84485357f, -0.83822471f, -0.83146961f,
141    -0.82458930f, -0.81758481f, -0.81045720f, -0.80320753f, -0.79583690f,
142    -0.78834643f, -0.78073723f, -0.77301045f, -0.76516727f, -0.75720885f,
143    -0.74913639f, -0.74095113f, -0.73265427f, -0.72424708f, -0.71573083f,
144    -0.70710678f, -0.69837625f, -0.68954054f, -0.68060100f, -0.67155895f,
145    -0.66241578f, -0.65317284f, -0.64383154f, -0.63439328f, -0.62485949f,
146    -0.61523159f, -0.60551104f, -0.59569930f, -0.58579786f, -0.57580819f,
147    -0.56573181f, -0.55557023f, -0.54532499f, -0.53499762f, -0.52458968f,
148    -0.51410274f, -0.50353838f, -0.49289819f, -0.48218377f, -0.47139674f,
149    -0.46053871f, -0.44961133f, -0.43861624f, -0.42755509f, -0.41642956f,
150    -0.40524131f, -0.39399204f, -0.38268343f, -0.37131719f, -0.35989504f,
151    -0.34841868f, -0.33688985f, -0.32531029f, -0.31368174f, -0.30200595f,
152    -0.29028468f, -0.27851969f, -0.26671276f, -0.25486566f, -0.24298018f,
153    -0.23105811f, -0.21910124f, -0.20711138f, -0.19509032f, -0.18303989f,
154    -0.17096189f, -0.15885814f, -0.14673047f, -0.13458071f, -0.12241068f,
155    -0.11022221f, -0.09801714f, -0.08579731f, -0.07356456f, -0.06132074f,
156    -0.04906767f, -0.03680722f, -0.02454123f, -0.01227154f, -0.00000000f
157 };
158 
159 /**
160  * @ingroup groupFastMath
161  */
162 
163 /**
164  * @defgroup sin Sine
165  *
166  * Computes the trigonometric sine function using a combination of table lookup
167  * and cubic interpolation.  There are separate functions for
168  * Q15, Q31, and floating-point data types.
169  * The input to the floating-point version is in radians while the
170  * fixed-point Q15 and Q31 have a scaled input with the range
171  * [0 +0.9999] mapping to [0 2*pi).  The fixed-point range is chosen so that a
172  * value of 2*pi wraps around to 0.
173  *
174  * The implementation is based on table lookup using 256 values together with cubic interpolation.
175  * The steps used are:
176  *  -# Calculation of the nearest integer table index
177  *  -# Fetch the four table values a, b, c, and d
178  *  -# Compute the fractional portion (fract) of the table index.
179  *  -# Calculation of wa, wb, wc, wd
180  *  -# The final result equals <code>a*wa + b*wb + c*wc + d*wd</code>
181  *
182  * where
183  * <pre>
184  *    a=Table[index-1];
185  *    b=Table[index+0];
186  *    c=Table[index+1];
187  *    d=Table[index+2];
188  * </pre>
189  * and
190  * <pre>
191  *    wa=-(1/6)*fract.^3 + (1/2)*fract.^2 - (1/3)*fract;
192  *    wb=(1/2)*fract.^3 - fract.^2 - (1/2)*fract + 1;
193  *    wc=-(1/2)*fract.^3+(1/2)*fract.^2+fract;
194  *    wd=(1/6)*fract.^3 - (1/6)*fract;
195  * </pre>
196  */
197 
198 /**
199  * @addtogroup sin
200  * @{
201  */
202 
203 /**
204  * @brief  Fast approximation to the trigonometric sine function for floating-point data.
205  * @param[in] x input value in radians.
206  * @return  sin(x).
207  */
208 
arm_sin_f32(float32_t x)209 float32_t arm_sin_f32(
210   float32_t x)
211 {
212   float32_t sinVal, fract, in;                           /* Temporary variables for input, output */
213   uint16_t index;                                        /* Index variable */
214   float32_t a, b;                                        /* Two nearest output values */
215   int32_t n;
216   float32_t findex;
217 
218   /* input x is in radians */
219   /* Scale the input to [0 1] range from [0 2*PI] , divide input by 2*pi */
220   in = x * 0.159154943092f;
221 
222   /* Calculation of floor value of input */
223   n = (int32_t) in;
224 
225   /* Make negative values towards -infinity */
226   if(x < 0.0f)
227   {
228     n--;
229   }
230 
231   /* Map input value to [0 1] */
232   in = in - (float32_t) n;
233 
234   /* Calculation of index of the table */
235   findex = (float32_t) FAST_MATH_TABLE_SIZE * in;
236   index = ((uint16_t)findex) & 0x1ff;
237 
238   /* fractional value calculation */
239   fract = findex - (float32_t) index;
240 
241   /* Read two nearest values of input value from the sin table */
242   a = sinTable_f32[index];
243   b = sinTable_f32[index+1];
244 
245   /* Linear interpolation process */
246   sinVal = (1.0f-fract)*a + fract*b;
247 
248   /* Return the output value */
249   return (sinVal);
250 }
251 
252 /**
253  * @defgroup cos Cosine
254  *
255  * Computes the trigonometric cosine function using a combination of table lookup
256  * and cubic interpolation.  There are separate functions for
257  * Q15, Q31, and floating-point data types.
258  * The input to the floating-point version is in radians while the
259  * fixed-point Q15 and Q31 have a scaled input with the range
260  * [0 +0.9999] mapping to [0 2*pi).  The fixed-point range is chosen so that a
261  * value of 2*pi wraps around to 0.
262  *
263  * The implementation is based on table lookup using 256 values together with cubic interpolation.
264  * The steps used are:
265  *  -# Calculation of the nearest integer table index
266  *  -# Fetch the four table values a, b, c, and d
267  *  -# Compute the fractional portion (fract) of the table index.
268  *  -# Calculation of wa, wb, wc, wd
269  *  -# The final result equals <code>a*wa + b*wb + c*wc + d*wd</code>
270  *
271  * where
272  * <pre>
273  *    a=Table[index-1];
274  *    b=Table[index+0];
275  *    c=Table[index+1];
276  *    d=Table[index+2];
277  * </pre>
278  * and
279  * <pre>
280  *    wa=-(1/6)*fract.^3 + (1/2)*fract.^2 - (1/3)*fract;
281  *    wb=(1/2)*fract.^3 - fract.^2 - (1/2)*fract + 1;
282  *    wc=-(1/2)*fract.^3+(1/2)*fract.^2+fract;
283  *    wd=(1/6)*fract.^3 - (1/6)*fract;
284  * </pre>
285  */
286 
287  /**
288  * @addtogroup cos
289  * @{
290  */
291 
292 /**
293  * @brief  Fast approximation to the trigonometric cosine function for floating-point data.
294  * @param[in] x input value in radians.
295  * @return cos(x).
296  */
297 
arm_cos_f32(float32_t x)298 float32_t arm_cos_f32(
299   float32_t x)
300 {
301   float32_t cosVal, fract, in;                   /* Temporary variables for input, output */
302   uint16_t index;                                /* Index variable */
303   float32_t a, b;                                /* Two nearest output values */
304   int32_t n;
305   float32_t findex;
306 
307   /* input x is in radians */
308   /* Scale the input to [0 1] range from [0 2*PI] , divide input by 2*pi, add 0.25 (pi/2) to read sine table */
309   in = x * 0.159154943092f + 0.25f;
310 
311   /* Calculation of floor value of input */
312   n = (int32_t) in;
313 
314   /* Make negative values towards -infinity */
315   if(in < 0.0f)
316   {
317     n--;
318   }
319 
320   /* Map input value to [0 1] */
321   in = in - (float32_t) n;
322 
323   /* Calculation of index of the table */
324   findex = (float32_t) FAST_MATH_TABLE_SIZE * in;
325   index = ((uint16_t)findex) & 0x1ff;
326 
327   /* fractional value calculation */
328   fract = findex - (float32_t) index;
329 
330   /* Read two nearest values of input value from the cos table */
331   a = sinTable_f32[index];
332   b = sinTable_f32[index+1];
333 
334   /* Linear interpolation process */
335   cosVal = (1.0f-fract)*a + fract*b;
336 
337   /* Return the output value */
338   return (cosVal);
339 }
340 
341 /**
342  * @} end of cos group
343  */
344