annotate f103c8/Drivers/CMSIS/DSP_Lib/Source/FastMathFunctions/arm_cos_q15.c @ 4:ca4f5b55b391

working on pwm
author cin
date Wed, 18 Jan 2017 03:27:00 +0300
parents 0c59e7a7782a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
1 /* ----------------------------------------------------------------------
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
3 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
4 * $Date: 07. September 2015
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
5 * $Revision: V.1.4.5 a
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
6 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
7 * Project: CMSIS DSP Library
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
8 * Title: arm_cos_q15.c
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
9 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
10 * Description: Fast cosine calculation for Q15 values.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
11 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
13 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
14 * Redistribution and use in source and binary forms, with or without
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
15 * modification, are permitted provided that the following conditions
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
16 * are met:
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
17 * - Redistributions of source code must retain the above copyright
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
18 * notice, this list of conditions and the following disclaimer.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
19 * - Redistributions in binary form must reproduce the above copyright
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
20 * notice, this list of conditions and the following disclaimer in
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
21 * the documentation and/or other materials provided with the
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
22 * distribution.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
23 * - Neither the name of ARM LIMITED nor the names of its contributors
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
24 * may be used to endorse or promote products derived from this
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
25 * software without specific prior written permission.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
26 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
38 * POSSIBILITY OF SUCH DAMAGE.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
39 * -------------------------------------------------------------------- */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
40
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
41 #include "arm_math.h"
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
42 #include "arm_common_tables.h"
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
43
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
44 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
45 * @ingroup groupFastMath
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
46 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
47
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
48 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
49 * @addtogroup cos
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
50 * @{
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
51 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
52
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
53 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
54 * @brief Fast approximation to the trigonometric cosine function for Q15 data.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
55 * @param[in] x Scaled input value in radians.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
56 * @return cos(x).
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
57 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
58 * The Q15 input value is in the range [0 +0.9999] and is mapped to a radian
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
59 * value in the range [0 2*pi).
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
60 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
61
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
62 q15_t arm_cos_q15(
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
63 q15_t x)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
64 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
65 q15_t cosVal; /* Temporary variables for input, output */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
66 int32_t index; /* Index variables */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
67 q15_t a, b; /* Four nearest output values */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
68 q15_t fract; /* Temporary values for fractional values */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
69
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
70 /* add 0.25 (pi/2) to read sine table */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
71 x = (uint16_t)x + 0x2000;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
72 if(x < 0)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
73 { /* convert negative numbers to corresponding positive ones */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
74 x = (uint16_t)x + 0x8000;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
75 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
76
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
77 /* Calculate the nearest index */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
78 index = (uint32_t)x >> FAST_MATH_Q15_SHIFT;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
79
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
80 /* Calculation of fractional value */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
81 fract = (x - (index << FAST_MATH_Q15_SHIFT)) << 9;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
82
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
83 /* Read two nearest values of input value from the sin table */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
84 a = sinTable_q15[index];
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
85 b = sinTable_q15[index+1];
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
86
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
87 /* Linear interpolation process */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
88 cosVal = (q31_t)(0x8000-fract)*a >> 16;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
89 cosVal = (q15_t)((((q31_t)cosVal << 16) + ((q31_t)fract*b)) >> 16);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
90
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
91 return cosVal << 1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
92 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
93
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
94 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
95 * @} end of cos group
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
96 */