1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_sin_cos_f32.c
4 * Description: Sine and Cosine calculation for floating-point values
5 *
6 * $Date: 23 April 2021
7 * $Revision: V1.9.0
8 *
9 * Target Processor: Cortex-M and Cortex-A cores
10 * -------------------------------------------------------------------- */
11 /*
12 * Copyright (C) 2010-2021 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 "dsp/controller_functions.h"
30 #include "arm_common_tables.h"
31
32 /**
33 @addtogroup SinCos
34 @{
35 */
36
37 /**
38 @brief Floating-point sin_cos function.
39 @param[in] theta input value in degrees
40 @param[out] pSinVal points to processed sine output
41 @param[out] pCosVal points to processed cosine output
42 @return none
43 */
44
arm_sin_cos_f32(float32_t theta,float32_t * pSinVal,float32_t * pCosVal)45 void arm_sin_cos_f32(
46 float32_t theta,
47 float32_t * pSinVal,
48 float32_t * pCosVal)
49 {
50 float32_t fract, in; /* Temporary input, output variables */
51 uint16_t indexS, indexC; /* Index variable */
52 float32_t f1, f2, d1, d2; /* Two nearest output values */
53 float32_t Dn, Df;
54 float32_t temp, findex;
55
56 /* input x is in degrees */
57 /* Scale input, divide input by 360, for cosine add 0.25 (pi/2) to read sine table */
58 in = theta * 0.00277777777778f;
59
60 if (in < 0.0f)
61 {
62 in = -in;
63 }
64
65 in = in - (int32_t)in;
66
67 /* Calculate the nearest index */
68 findex = (float32_t)FAST_MATH_TABLE_SIZE * in;
69 indexS = ((uint16_t)findex) & 0x1ff;
70 indexC = (indexS + (FAST_MATH_TABLE_SIZE / 4)) & 0x1ff;
71
72 /* Calculation of fractional value */
73 fract = findex - (float32_t) indexS;
74
75 /* Read two nearest values of input value from the cos & sin tables */
76 f1 = sinTable_f32[indexC ];
77 f2 = sinTable_f32[indexC+1];
78 d1 = -sinTable_f32[indexS ];
79 d2 = -sinTable_f32[indexS+1];
80
81 Dn = 0.0122718463030f; /* delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE */
82 Df = f2 - f1; /* delta between the values of the functions */
83
84 temp = Dn * (d1 + d2) - 2 * Df;
85 temp = fract * temp + (3 * Df - (d2 + 2 * d1) * Dn);
86 temp = fract * temp + d1 * Dn;
87
88 /* Calculation of cosine value */
89 *pCosVal = fract * temp + f1;
90
91 /* Read two nearest values of input value from the cos & sin tables */
92 f1 = sinTable_f32[indexS ];
93 f2 = sinTable_f32[indexS+1];
94 d1 = sinTable_f32[indexC ];
95 d2 = sinTable_f32[indexC+1];
96
97
98 Df = f2 - f1; // delta between the values of the functions
99 temp = Dn * (d1 + d2) - 2 * Df;
100 temp = fract * temp + (3 * Df - (d2 + 2 * d1) * Dn);
101 temp = fract * temp + d1 * Dn;
102
103 /* Calculation of sine value */
104 *pSinVal = fract * temp + f1;
105
106 if (theta < 0.0f)
107 {
108 *pSinVal = -*pSinVal;
109 }
110 }
111
112 /**
113 @} end of SinCos group
114 */
115