annotate f103c8/Drivers/CMSIS/DSP_Lib/Source/FastMathFunctions/arm_sin_f32.c @ 2:0c59e7a7782a

Working on GPIO and RCC
author cin
date Mon, 16 Jan 2017 11:04:47 +0300
parents
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: 21. 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_sin_f32.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 sine calculation for floating-point 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 #include <math.h>
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
44
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
45 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
46 * @ingroup groupFastMath
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 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
50 * @defgroup sin Sine
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
51 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
52 * Computes the trigonometric sine function using a combination of table lookup
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
53 * and linear interpolation. There are separate functions for
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
54 * Q15, Q31, and floating-point data types.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
55 * The input to the floating-point version is in radians while the
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
56 * fixed-point Q15 and Q31 have a scaled input with the range
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
57 * [0 +0.9999] mapping to [0 2*pi). The fixed-point range is chosen so that a
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
58 * value of 2*pi wraps around to 0.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
59 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
60 * The implementation is based on table lookup using 256 values together with linear interpolation.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
61 * The steps used are:
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
62 * -# Calculation of the nearest integer table index
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
63 * -# Compute the fractional portion (fract) of the table index.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
64 * -# The final result equals <code>(1.0f-fract)*a + fract*b;</code>
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
65 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
66 * where
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
67 * <pre>
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
68 * b=Table[index+0];
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
69 * c=Table[index+1];
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
70 * </pre>
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
71 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
72
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
73 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
74 * @addtogroup sin
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
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
78 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
79 * @brief Fast approximation to the trigonometric sine function for floating-point data.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
80 * @param[in] x input value in radians.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
81 * @return sin(x).
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
82 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
83
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
84 float32_t arm_sin_f32(
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
85 float32_t x)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
86 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
87 float32_t sinVal, fract, in; /* Temporary variables for input, output */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
88 uint16_t index; /* Index variable */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
89 float32_t a, b; /* Two nearest output values */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
90 int32_t n;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
91 float32_t findex;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
92
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
93 /* input x is in radians */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
94 /* Scale the input to [0 1] range from [0 2*PI] , divide input by 2*pi */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
95 in = x * 0.159154943092f;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
96
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
97 /* Calculation of floor value of input */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
98 n = (int32_t) in;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
99
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
100 /* Make negative values towards -infinity */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
101 if(x < 0.0f)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
102 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
103 n--;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
104 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
105
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
106 /* Map input value to [0 1] */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
107 in = in - (float32_t) n;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
108
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
109 /* Calculation of index of the table */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
110 findex = (float32_t) FAST_MATH_TABLE_SIZE * in;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
111 if (findex >= 512.0f) {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
112 findex -= 512.0f;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
113 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
114
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
115 index = ((uint16_t)findex) & 0x1ff;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
116
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
117 /* fractional value calculation */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
118 fract = findex - (float32_t) index;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
119
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
120 /* Read two nearest values of input value from the sin table */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
121 a = sinTable_f32[index];
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
122 b = sinTable_f32[index+1];
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
123
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
124 /* Linear interpolation process */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
125 sinVal = (1.0f-fract)*a + fract*b;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
126
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
127 /* Return the output value */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
128 return (sinVal);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
129 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
130
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
131 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
132 * @} end of sin group
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
133 */