1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_sin_cos_q31.c
4 * Description: Cosine & Sine calculation for Q31 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 @ingroup groupController
34 */
35
36 /**
37 @addtogroup SinCos
38 @{
39 */
40
41 /**
42 @brief Q31 sin_cos function.
43 @param[in] theta scaled input value in degrees
44 @param[out] pSinVal points to processed sine output
45 @param[out] pCosVal points to processed cosine output
46
47 The Q31 input value is in the range [-1 0.999999] and is mapped to a degree value in the range [-180 179].
48 */
49
arm_sin_cos_q31(q31_t theta,q31_t * pSinVal,q31_t * pCosVal)50 ARM_DSP_ATTRIBUTE void arm_sin_cos_q31(
51 q31_t theta,
52 q31_t * pSinVal,
53 q31_t * pCosVal)
54 {
55 q31_t fract; /* Temporary input, output variables */
56 uint16_t indexS, indexC; /* Index variable */
57 q31_t f1, f2, d1, d2; /* Two nearest output values */
58 q31_t Dn, Df;
59 q63_t temp;
60
61 /* Calculate the nearest index */
62 indexS = (uint32_t)theta >> CONTROLLER_Q31_SHIFT;
63 indexC = (indexS + 128) & 0x1ff;
64
65 /* Calculation of fractional value */
66 fract = (theta - (indexS << CONTROLLER_Q31_SHIFT)) << 8;
67
68 /* Read two nearest values of input value from the cos & sin tables */
69 f1 = sinTable_q31[indexC ];
70 f2 = sinTable_q31[indexC+1];
71 d1 = -sinTable_q31[indexS ];
72 d2 = -sinTable_q31[indexS+1];
73
74 Dn = 0x1921FB5; /* delta between the two points (fixed), in this case 2*pi/FAST_MATH_TABLE_SIZE */
75 Df = f2 - f1; /* delta between the values of the functions */
76
77 temp = Dn * ((q63_t)d1 + d2);
78 temp = temp - ((q63_t)Df << 32);
79 temp = (q63_t)fract * (temp >> 31);
80 temp = temp + ((3 * (q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1)) * Dn);
81 temp = (q63_t)fract * (temp >> 31);
82 temp = temp + (q63_t)d1 * Dn;
83 temp = (q63_t)fract * (temp >> 31);
84
85 /* Calculation of cosine value */
86 *pCosVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1);
87
88 /* Read two nearest values of input value from the cos & sin tables */
89 f1 = sinTable_q31[indexS ];
90 f2 = sinTable_q31[indexS+1];
91 d1 = sinTable_q31[indexC ];
92 d2 = sinTable_q31[indexC+1];
93
94 Df = f2 - f1; // delta between the values of the functions
95 temp = Dn * ((q63_t)d1 + d2);
96 temp = temp - ((q63_t)Df << 32);
97 temp = (q63_t)fract * (temp >> 31);
98 temp = temp + ((3 * (q63_t)Df << 31) - (d2 + ((q63_t)d1 << 1)) * Dn);
99 temp = (q63_t)fract * (temp >> 31);
100 temp = temp + (q63_t)d1 * Dn;
101 temp = (q63_t)fract * (temp >> 31);
102
103 /* Calculation of sine value */
104 *pSinVal = clip_q63_to_q31((temp >> 31) + (q63_t)f1);
105 }
106
107 /**
108 @} end of SinCos group
109 */
110