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 *
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *   - Redistributions of source code must retain the above copyright
18 *     notice, this list of conditions and the following disclaimer.
19 *   - Redistributions in binary form must reproduce the above copyright
20 *     notice, this list of conditions and the following disclaimer in
21 *     the documentation and/or other materials provided with the
22 *     distribution.
23 *   - Neither the name of ARM LIMITED nor the names of its contributors
24 *     may be used to endorse or promote products derived from this
25 *     software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 * -------------------------------------------------------------------- */
40 
41 #include "arm_math.h"
42 #include "arm_common_tables.h"
43 
44 /**
45  * @ingroup groupFastMath
46  */
47 
48 /**
49  * @defgroup sin Sine
50  *
51  * Computes the trigonometric sine function using a combination of table lookup
52  * and cubic interpolation.  There are separate functions for
53  * Q15, Q31, and floating-point data types.
54  * The input to the floating-point version is in radians while the
55  * fixed-point Q15 and Q31 have a scaled input with the range
56  * [0 +0.9999] mapping to [0 2*pi).  The fixed-point range is chosen so that a
57  * value of 2*pi wraps around to 0.
58  *
59  * The implementation is based on table lookup using 256 values together with cubic interpolation.
60  * The steps used are:
61  *  -# Calculation of the nearest integer table index
62  *  -# Fetch the four table values a, b, c, and d
63  *  -# Compute the fractional portion (fract) of the table index.
64  *  -# Calculation of wa, wb, wc, wd
65  *  -# The final result equals <code>a*wa + b*wb + c*wc + d*wd</code>
66  *
67  * where
68  * <pre>
69  *    a=Table[index-1];
70  *    b=Table[index+0];
71  *    c=Table[index+1];
72  *    d=Table[index+2];
73  * </pre>
74  * and
75  * <pre>
76  *    wa=-(1/6)*fract.^3 + (1/2)*fract.^2 - (1/3)*fract;
77  *    wb=(1/2)*fract.^3 - fract.^2 - (1/2)*fract + 1;
78  *    wc=-(1/2)*fract.^3+(1/2)*fract.^2+fract;
79  *    wd=(1/6)*fract.^3 - (1/6)*fract;
80  * </pre>
81  */
82 
83 /**
84  * @addtogroup sin
85  * @{
86  */
87 
88 /**
89  * @brief  Fast approximation to the trigonometric sine function for floating-point data.
90  * @param[in] x input value in radians.
91  * @return  sin(x).
92  */
93 
arm_sin_f32(float32_t x)94 float32_t arm_sin_f32(
95   float32_t x)
96 {
97   float32_t sinVal, fract, in;                           /* Temporary variables for input, output */
98   uint16_t index;                                        /* Index variable */
99   float32_t a, b;                                        /* Two nearest output values */
100   int32_t n;
101   float32_t findex;
102 
103   /* input x is in radians */
104   /* Scale the input to [0 1] range from [0 2*PI] , divide input by 2*pi */
105   in = x * 0.159154943092f;
106 
107   /* Calculation of floor value of input */
108   n = (int32_t) in;
109 
110   /* Make negative values towards -infinity */
111   if(x < 0.0f)
112   {
113     n--;
114   }
115 
116   /* Map input value to [0 1] */
117   in = in - (float32_t) n;
118 
119   /* Calculation of index of the table */
120   findex = (float32_t) FAST_MATH_TABLE_SIZE * in;
121   index = ((uint16_t)findex) & 0x1ff;
122 
123   /* fractional value calculation */
124   fract = findex - (float32_t) index;
125 
126   /* Read two nearest values of input value from the sin table */
127   a = sinTable_f32[index];
128   b = sinTable_f32[index+1];
129 
130   /* Linear interpolation process */
131   sinVal = (1.0f-fract)*a + fract*b;
132 
133   /* Return the output value */
134   return (sinVal);
135 }
136 
137 /**
138  * @} end of sin group
139  */
140