1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
4 * Description: Fast cosine calculation for floating-point 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"
32 * @ingroup groupFastMath
36 * @defgroup cos Cosine
38 * Computes the trigonometric cosine function using a combination of table lookup
39 * and linear interpolation. There are separate functions for
40 * Q15, Q31, and floating-point data types.
41 * The input to the floating-point version is in radians and in the range [0 2*pi) while the
42 * fixed-point Q15 and Q31 have a scaled input with the range
43 * [0 +0.9999] mapping to [0 2*pi). The fixed-point range is chosen so that a
44 * value of 2*pi wraps around to 0.
46 * The implementation is based on table lookup using 256 values together with linear interpolation.
48 * -# Calculation of the nearest integer table index
49 * -# Compute the fractional portion (fract) of the table index.
50 * -# The final result equals <code>(1.0f-fract)*a + fract*b;</code>
65 * @brief Fast approximation to the trigonometric cosine function for floating-point data.
66 * @param[in] x input value in radians.
70 float32_t
arm_cos_f32(
73 float32_t cosVal
, fract
, in
; /* Temporary variables for input, output */
74 uint16_t index
; /* Index variable */
75 float32_t a
, b
; /* Two nearest output values */
79 /* input x is in radians */
80 /* Scale the input to [0 1] range from [0 2*PI] , divide input by 2*pi, add 0.25 (pi/2) to read sine table */
81 in
= x
* 0.159154943092f
+ 0.25f
;
83 /* Calculation of floor value of input */
86 /* Make negative values towards -infinity */
92 /* Map input value to [0 1] */
93 in
= in
- (float32_t
) n
;
95 /* Calculation of index of the table */
96 findex
= (float32_t
) FAST_MATH_TABLE_SIZE
* in
;
97 index
= ((uint16_t)findex
) & 0x1ff;
99 /* fractional value calculation */
100 fract
= findex
- (float32_t
) index
;
102 /* Read two nearest values of input value from the cos table */
103 a
= sinTable_f32
[index
];
104 b
= sinTable_f32
[index
+1];
106 /* Linear interpolation process */
107 cosVal
= (1.0f
-fract
)*a
+ fract
*b
;
109 /* Return the output value */
114 * @} end of cos group