Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
56 | mjames | 1 | /* ---------------------------------------------------------------------- |
2 | * Project: CMSIS DSP Library |
||
3 | * Title: arm_sin_f32.c |
||
4 | * Description: Fast sine calculation for floating-point values |
||
5 | * |
||
6 | * $Date: 27. January 2017 |
||
7 | * $Revision: V.1.5.1 |
||
8 | * |
||
9 | * Target Processor: Cortex-M cores |
||
10 | * -------------------------------------------------------------------- */ |
||
11 | /* |
||
12 | * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved. |
||
13 | * |
||
14 | * SPDX-License-Identifier: Apache-2.0 |
||
15 | * |
||
16 | * Licensed under the Apache License, Version 2.0 (the License); you may |
||
17 | * not use this file except in compliance with the License. |
||
18 | * You may obtain a copy of the License at |
||
19 | * |
||
20 | * www.apache.org/licenses/LICENSE-2.0 |
||
21 | * |
||
22 | * Unless required by applicable law or agreed to in writing, software |
||
23 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
||
24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
25 | * See the License for the specific language governing permissions and |
||
26 | * limitations under the License. |
||
27 | */ |
||
28 | |||
29 | #include "arm_math.h" |
||
30 | #include "arm_common_tables.h" |
||
31 | #include <math.h> |
||
32 | |||
33 | /** |
||
34 | * @ingroup groupFastMath |
||
35 | */ |
||
36 | |||
37 | /** |
||
38 | * @defgroup sin Sine |
||
39 | * |
||
40 | * Computes the trigonometric sine function using a combination of table lookup |
||
41 | * and linear interpolation. There are separate functions for |
||
42 | * Q15, Q31, and floating-point data types. |
||
43 | * The input to the floating-point version is in radians and in the range [0 2*pi) while the |
||
44 | * fixed-point Q15 and Q31 have a scaled input with the range |
||
45 | * [0 +0.9999] mapping to [0 2*pi). The fixed-point range is chosen so that a |
||
46 | * value of 2*pi wraps around to 0. |
||
47 | * |
||
48 | * The implementation is based on table lookup using 256 values together with linear interpolation. |
||
49 | * The steps used are: |
||
50 | * -# Calculation of the nearest integer table index |
||
51 | * -# Compute the fractional portion (fract) of the table index. |
||
52 | * -# The final result equals <code>(1.0f-fract)*a + fract*b;</code> |
||
53 | * |
||
54 | * where |
||
55 | * <pre> |
||
56 | * b=Table[index+0]; |
||
57 | * c=Table[index+1]; |
||
58 | * </pre> |
||
59 | */ |
||
60 | |||
61 | /** |
||
62 | * @addtogroup sin |
||
63 | * @{ |
||
64 | */ |
||
65 | |||
66 | /** |
||
67 | * @brief Fast approximation to the trigonometric sine function for floating-point data. |
||
68 | * @param[in] x input value in radians. |
||
69 | * @return sin(x). |
||
70 | */ |
||
71 | |||
72 | float32_t arm_sin_f32( |
||
73 | float32_t x) |
||
74 | { |
||
75 | float32_t sinVal, fract, in; /* Temporary variables for input, output */ |
||
76 | uint16_t index; /* Index variable */ |
||
77 | float32_t a, b; /* Two nearest output values */ |
||
78 | int32_t n; |
||
79 | float32_t findex; |
||
80 | |||
81 | /* Special case for small negative inputs */ |
||
82 | if ((x < 0.0f) && (x >= -1.9e-7f)) { |
||
83 | return x; |
||
84 | } |
||
85 | |||
86 | /* input x is in radians */ |
||
87 | /* Scale the input to [0 1] range from [0 2*PI] , divide input by 2*pi */ |
||
88 | in = x * 0.159154943092f; |
||
89 | |||
90 | /* Calculation of floor value of input */ |
||
91 | n = (int32_t) in; |
||
92 | |||
93 | /* Make negative values towards -infinity */ |
||
94 | if (x < 0.0f) |
||
95 | { |
||
96 | n--; |
||
97 | } |
||
98 | |||
99 | /* Map input value to [0 1] */ |
||
100 | in = in - (float32_t) n; |
||
101 | |||
102 | /* Calculation of index of the table */ |
||
103 | findex = (float32_t) FAST_MATH_TABLE_SIZE * in; |
||
104 | |||
105 | index = ((uint16_t)findex) & 0x1ff; |
||
106 | |||
107 | /* fractional value calculation */ |
||
108 | fract = findex - (float32_t) index; |
||
109 | |||
110 | /* Read two nearest values of input value from the sin table */ |
||
111 | a = sinTable_f32[index]; |
||
112 | b = sinTable_f32[index+1]; |
||
113 | |||
114 | /* Linear interpolation process */ |
||
115 | sinVal = (1.0f-fract)*a + fract*b; |
||
116 | |||
117 | /* Return the output value */ |
||
118 | return (sinVal); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @} end of sin group |
||
123 | */ |