Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 5 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* ---------------------------------------------------------------------- |
1 | /* ---------------------------------------------------------------------- |
2 | * Copyright (C) 2010-2014 ARM Limited. All rights reserved. |
2 | * Copyright (C) 2010-2014 ARM Limited. All rights reserved. |
3 | * |
3 | * |
4 | * $Date: 19. March 2015 |
4 | * $Date: 21. September 2015 |
5 | * $Revision: V.1.4.5 |
5 | * $Revision: V.1.4.5 a |
6 | * |
6 | * |
7 | * Project: CMSIS DSP Library |
7 | * Project: CMSIS DSP Library |
8 | * Title: arm_sin_f32.c |
8 | * Title: arm_sin_f32.c |
9 | * |
9 | * |
10 | * Description: Fast sine calculation for floating-point values. |
10 | * Description: Fast sine calculation for floating-point values. |
11 | * |
11 | * |
12 | * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 |
12 | * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 |
13 | * |
13 | * |
14 | * Redistribution and use in source and binary forms, with or without |
14 | * Redistribution and use in source and binary forms, with or without |
15 | * modification, are permitted provided that the following conditions |
15 | * modification, are permitted provided that the following conditions |
16 | * are met: |
16 | * are met: |
17 | * - Redistributions of source code must retain the above copyright |
17 | * - Redistributions of source code must retain the above copyright |
18 | * notice, this list of conditions and the following disclaimer. |
18 | * notice, this list of conditions and the following disclaimer. |
19 | * - Redistributions in binary form must reproduce the above copyright |
19 | * - Redistributions in binary form must reproduce the above copyright |
20 | * notice, this list of conditions and the following disclaimer in |
20 | * notice, this list of conditions and the following disclaimer in |
21 | * the documentation and/or other materials provided with the |
21 | * the documentation and/or other materials provided with the |
22 | * distribution. |
22 | * distribution. |
23 | * - Neither the name of ARM LIMITED nor the names of its contributors |
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 |
24 | * may be used to endorse or promote products derived from this |
25 | * software without specific prior written permission. |
25 | * software without specific prior written permission. |
26 | * |
26 | * |
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
30 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
30 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
31 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
31 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
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 |
37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | * POSSIBILITY OF SUCH DAMAGE. |
38 | * POSSIBILITY OF SUCH DAMAGE. |
39 | * -------------------------------------------------------------------- */ |
39 | * -------------------------------------------------------------------- */ |
40 | 40 | ||
41 | #include "arm_math.h" |
41 | #include "arm_math.h" |
42 | #include "arm_common_tables.h" |
42 | #include "arm_common_tables.h" |
- | 43 | #include <math.h> |
|
43 | 44 | ||
44 | /** |
45 | /** |
45 | * @ingroup groupFastMath |
46 | * @ingroup groupFastMath |
46 | */ |
47 | */ |
47 | 48 | ||
48 | /** |
49 | /** |
49 | * @defgroup sin Sine |
50 | * @defgroup sin Sine |
50 | * |
51 | * |
51 | * Computes the trigonometric sine function using a combination of table lookup |
52 | * Computes the trigonometric sine function using a combination of table lookup |
52 | * and cubic interpolation. There are separate functions for |
53 | * and linear interpolation. There are separate functions for |
53 | * Q15, Q31, and floating-point data types. |
54 | * Q15, Q31, and floating-point data types. |
54 | * The input to the floating-point version is in radians while the |
55 | * 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 | * 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 | * [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 | * value of 2*pi wraps around to 0. |
58 | * |
59 | * |
59 | * The implementation is based on table lookup using 256 values together with cubic interpolation. |
60 | * The implementation is based on table lookup using 256 values together with linear interpolation. |
60 | * The steps used are: |
61 | * The steps used are: |
61 | * -# Calculation of the nearest integer table index |
62 | * -# 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. |
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> |
64 | * -# The final result equals <code>(1.0f-fract)*a + fract*b;</code> |
66 | * |
65 | * |
67 | * where |
66 | * where |
68 | * <pre> |
67 | * <pre> |
69 | * a=Table[index-1]; |
- | |
70 | * b=Table[index+0]; |
68 | * b=Table[index+0]; |
71 | * c=Table[index+1]; |
69 | * c=Table[index+1]; |
72 | * d=Table[index+2]; |
- | |
73 | * </pre> |
70 | * </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 | */ |
71 | */ |
82 | 72 | ||
83 | /** |
73 | /** |
84 | * @addtogroup sin |
74 | * @addtogroup sin |
85 | * @{ |
75 | * @{ |
86 | */ |
76 | */ |
87 | 77 | ||
88 | /** |
78 | /** |
89 | * @brief Fast approximation to the trigonometric sine function for floating-point data. |
79 | * @brief Fast approximation to the trigonometric sine function for floating-point data. |
90 | * @param[in] x input value in radians. |
80 | * @param[in] x input value in radians. |
91 | * @return sin(x). |
81 | * @return sin(x). |
92 | */ |
82 | */ |
93 | 83 | ||
94 | float32_t arm_sin_f32( |
84 | float32_t arm_sin_f32( |
95 | float32_t x) |
85 | float32_t x) |
96 | { |
86 | { |
Line 116... | Line 106... | ||
116 | /* Map input value to [0 1] */ |
106 | /* Map input value to [0 1] */ |
117 | in = in - (float32_t) n; |
107 | in = in - (float32_t) n; |
118 | 108 | ||
119 | /* Calculation of index of the table */ |
109 | /* Calculation of index of the table */ |
120 | findex = (float32_t) FAST_MATH_TABLE_SIZE * in; |
110 | findex = (float32_t) FAST_MATH_TABLE_SIZE * in; |
- | 111 | if (findex >= 512.0f) { |
|
- | 112 | findex -= 512.0f; |
|
- | 113 | } |
|
- | 114 | ||
121 | index = ((uint16_t)findex) & 0x1ff; |
115 | index = ((uint16_t)findex) & 0x1ff; |
122 | 116 | ||
123 | /* fractional value calculation */ |
117 | /* fractional value calculation */ |
124 | fract = findex - (float32_t) index; |
118 | fract = findex - (float32_t) index; |
125 | 119 | ||
Line 132... | Line 126... | ||
132 | 126 | ||
133 | /* Return the output value */ |
127 | /* Return the output value */ |
134 | return (sinVal); |
128 | return (sinVal); |
135 | } |
129 | } |
136 | 130 | ||
137 | /** |
131 | /** |
138 | * @} end of sin group |
132 | * @} end of sin group |
139 | */ |
133 | */ |