1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
4 * Description: Fast cosine calculation for Q31 values
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
30 #include "arm_common_tables.h"
33 * @ingroup groupFastMath
42 * @brief Fast approximation to the trigonometric cosine function for Q31 data.
43 * @param[in] x Scaled input value in radians.
46 * The Q31 input value is in the range [0 +0.9999] and is mapped to a radian
47 * value in the range [0 2*pi).
53 q31_t cosVal
; /* Temporary variables for input, output */
54 int32_t index
; /* Index variables */
55 q31_t a
, b
; /* Four nearest output values */
56 q31_t fract
; /* Temporary values for fractional values */
58 /* add 0.25 (pi/2) to read sine table */
59 x
= (uint32_t)x
+ 0x20000000;
61 { /* convert negative numbers to corresponding positive ones */
62 x
= (uint32_t)x
+ 0x80000000;
65 /* Calculate the nearest index */
66 index
= (uint32_t)x
>> FAST_MATH_Q31_SHIFT
;
68 /* Calculation of fractional value */
69 fract
= (x
- (index
<< FAST_MATH_Q31_SHIFT
)) << 9;
71 /* Read two nearest values of input value from the sin table */
72 a
= sinTable_q31
[index
];
73 b
= sinTable_q31
[index
+1];
75 /* Linear interpolation process */
76 cosVal
= (q63_t
)(0x80000000-fract
)*a
>> 32;
77 cosVal
= (q31_t
)((((q63_t
)cosVal
<< 32) + ((q63_t
)fract
*b
)) >> 32);