Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / FastMathFunctions / arm_sin_q15.c
1 /* ----------------------------------------------------------------------   
2 * Copyright (C) 2010 ARM Limited. All rights reserved.   
3 *   
4 * $Date:        15. July 2011  
5 * $Revision:    V1.0.10  
6 *   
7 * Project:          CMSIS DSP Library   
8 * Title:                arm_sin_q15.c   
9 *   
10 * Description:  Fast sine calculation for Q15 values.  
11 *   
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *  
14 * Version 1.0.10 2011/7/15 
15 *    Big Endian support added and Merged M0 and M3/M4 Source code.  
16 *   
17 * Version 1.0.3 2010/11/29  
18 *    Re-organized the CMSIS folders and updated documentation.   
19 *    
20 * Version 1.0.2 2010/11/11   
21 *    Documentation updated.    
22 *   
23 * Version 1.0.1 2010/10/05    
24 *    Production release and review comments incorporated.   
25 *   
26 * Version 1.0.0 2010/09/20    
27 *    Production release and review comments incorporated.   
28 * -------------------------------------------------------------------- */
29
30 #include "arm_math.h"
31
32 /**   
33  * @ingroup groupFastMath   
34  */
35
36  /**   
37  * @addtogroup sin   
38  * @{   
39  */
40
41
42 /**   
43  * \par   
44  * Example code for Generation of Q15 Sin Table:  
45  * \par   
46  * <pre>tableSize = 256;   
47  * for(n = -1; n < (tableSize + 1); n++)   
48  * {   
49  *      sinTable[n+1]=sin(2*pi*n/tableSize);   
50  * } </pre>   
51  * where pi value is  3.14159265358979   
52  * \par   
53  * Convert Floating point to Q15(Fixed point):   
54  *      (sinTable[i] * pow(2, 15))   
55  * \par   
56  * rounding to nearest integer is done   
57  *      sinTable[i] += (sinTable[i] > 0 ? 0.5 :-0.5);   
58  */
59
60
61 static const q15_t sinTableQ15[259] = {
62   0xfcdc, 0x0, 0x324, 0x648, 0x96b, 0xc8c, 0xfab, 0x12c8,
63   0x15e2, 0x18f9, 0x1c0c, 0x1f1a, 0x2224, 0x2528, 0x2827, 0x2b1f,
64   0x2e11, 0x30fc, 0x33df, 0x36ba, 0x398d, 0x3c57, 0x3f17, 0x41ce,
65   0x447b, 0x471d, 0x49b4, 0x4c40, 0x4ec0, 0x5134, 0x539b, 0x55f6,
66   0x5843, 0x5a82, 0x5cb4, 0x5ed7, 0x60ec, 0x62f2, 0x64e9, 0x66d0,
67   0x68a7, 0x6a6e, 0x6c24, 0x6dca, 0x6f5f, 0x70e3, 0x7255, 0x73b6,
68   0x7505, 0x7642, 0x776c, 0x7885, 0x798a, 0x7a7d, 0x7b5d, 0x7c2a,
69   0x7ce4, 0x7d8a, 0x7e1e, 0x7e9d, 0x7f0a, 0x7f62, 0x7fa7, 0x7fd9,
70   0x7ff6, 0x7fff, 0x7ff6, 0x7fd9, 0x7fa7, 0x7f62, 0x7f0a, 0x7e9d,
71   0x7e1e, 0x7d8a, 0x7ce4, 0x7c2a, 0x7b5d, 0x7a7d, 0x798a, 0x7885,
72   0x776c, 0x7642, 0x7505, 0x73b6, 0x7255, 0x70e3, 0x6f5f, 0x6dca,
73   0x6c24, 0x6a6e, 0x68a7, 0x66d0, 0x64e9, 0x62f2, 0x60ec, 0x5ed7,
74   0x5cb4, 0x5a82, 0x5843, 0x55f6, 0x539b, 0x5134, 0x4ec0, 0x4c40,
75   0x49b4, 0x471d, 0x447b, 0x41ce, 0x3f17, 0x3c57, 0x398d, 0x36ba,
76   0x33df, 0x30fc, 0x2e11, 0x2b1f, 0x2827, 0x2528, 0x2224, 0x1f1a,
77   0x1c0c, 0x18f9, 0x15e2, 0x12c8, 0xfab, 0xc8c, 0x96b, 0x648,
78   0x324, 0x0, 0xfcdc, 0xf9b8, 0xf695, 0xf374, 0xf055, 0xed38,
79   0xea1e, 0xe707, 0xe3f4, 0xe0e6, 0xdddc, 0xdad8, 0xd7d9, 0xd4e1,
80   0xd1ef, 0xcf04, 0xcc21, 0xc946, 0xc673, 0xc3a9, 0xc0e9, 0xbe32,
81   0xbb85, 0xb8e3, 0xb64c, 0xb3c0, 0xb140, 0xaecc, 0xac65, 0xaa0a,
82   0xa7bd, 0xa57e, 0xa34c, 0xa129, 0x9f14, 0x9d0e, 0x9b17, 0x9930,
83   0x9759, 0x9592, 0x93dc, 0x9236, 0x90a1, 0x8f1d, 0x8dab, 0x8c4a,
84   0x8afb, 0x89be, 0x8894, 0x877b, 0x8676, 0x8583, 0x84a3, 0x83d6,
85   0x831c, 0x8276, 0x81e2, 0x8163, 0x80f6, 0x809e, 0x8059, 0x8027,
86   0x800a, 0x8000, 0x800a, 0x8027, 0x8059, 0x809e, 0x80f6, 0x8163,
87   0x81e2, 0x8276, 0x831c, 0x83d6, 0x84a3, 0x8583, 0x8676, 0x877b,
88   0x8894, 0x89be, 0x8afb, 0x8c4a, 0x8dab, 0x8f1d, 0x90a1, 0x9236,
89   0x93dc, 0x9592, 0x9759, 0x9930, 0x9b17, 0x9d0e, 0x9f14, 0xa129,
90   0xa34c, 0xa57e, 0xa7bd, 0xaa0a, 0xac65, 0xaecc, 0xb140, 0xb3c0,
91   0xb64c, 0xb8e3, 0xbb85, 0xbe32, 0xc0e9, 0xc3a9, 0xc673, 0xc946,
92   0xcc21, 0xcf04, 0xd1ef, 0xd4e1, 0xd7d9, 0xdad8, 0xdddc, 0xe0e6,
93   0xe3f4, 0xe707, 0xea1e, 0xed38, 0xf055, 0xf374, 0xf695, 0xf9b8,
94   0xfcdc, 0x0, 0x324
95 };
96
97
98 /**  
99  * @brief Fast approximation to the trigonometric sine function for Q15 data.  
100  * @param[in] x Scaled input value in radians.  
101  * @return  sin(x).  
102  *  
103  * The Q15 input value is in the range [0 +1) and is mapped to a radian value in the range [0 2*pi).  
104  */
105
106 q15_t arm_sin_q15(
107   q15_t x)
108 {
109   q31_t sinVal;                                  /* Temporary variables output */
110   q15_t *tablePtr;                               /* Pointer to table */
111   q15_t fract, in, in2;                          /* Temporary variables for input, output */
112   q31_t wa, wb, wc, wd;                          /* Cubic interpolation coefficients */
113   q15_t a, b, c, d;                              /* Four nearest output values */
114   q15_t fractCube, fractSquare;                  /* Temporary values for fractional value */
115   q15_t oneBy6 = 0x1555;                         /* Fixed point value of 1/6 */
116   q15_t tableSpacing = TABLE_SPACING_Q15;        /* Table spacing */
117   int32_t index;                                 /* Index variable */
118
119   in = x;
120
121   /* Calculate the nearest index */
122   index = (int32_t) in / tableSpacing;
123
124   /* Calculate the nearest value of input */
125   in2 = (q15_t) ((index) * tableSpacing);
126
127   /* Calculation of fractional value */
128   fract = (in - in2) << 8;
129
130   /* fractSquare = fract * fract */
131   fractSquare = (q15_t) ((fract * fract) >> 15);
132
133   /* fractCube = fract * fract * fract */
134   fractCube = (q15_t) ((fractSquare * fract) >> 15);
135
136   /* Initialise table pointer */
137   tablePtr = (q15_t *) & sinTableQ15[index];
138
139   /* Cubic interpolation process */
140   /* Calculation of wa */
141   /* wa = -(oneBy6)*fractCube + (fractSquare >> 1u) - (0x2AAA)*fract; */
142   wa = (q31_t) oneBy6 *fractCube;
143   wa += (q31_t) 0x2AAA *fract;
144   wa = -(wa >> 15);
145   wa += ((q31_t) fractSquare >> 1u);
146
147   /* Read first nearest value of output from the sin table */
148   a = *tablePtr++;
149
150   /* sinVal = a * wa */
151   sinVal = a * wa;
152
153   /* Calculation of wb */
154   wb = (((q31_t) fractCube >> 1u) - (q31_t) fractSquare) -
155     (((q31_t) fract >> 1u) - 0x7FFF);
156
157   /* Read second nearest value of output from the sin table */
158   b = *tablePtr++;
159
160   /*      sinVal += b*wb */
161   sinVal += b * wb;
162
163
164   /* Calculation of wc */
165   wc = -(q31_t) fractCube + fractSquare;
166   wc = (wc >> 1u) + fract;
167
168   /* Read third nearest value of output from the sin table */
169   c = *tablePtr++;
170
171   /*      sinVal += c*wc */
172   sinVal += c * wc;
173
174   /* Calculation of wd */
175   /* wd = (oneBy6)*fractCube - (oneBy6)*fract; */
176   fractCube = fractCube - fract;
177   wd = ((q15_t) (((q31_t) oneBy6 * fractCube) >> 15));
178
179   /* Read fourth nearest value of output from the sin table */
180   d = *tablePtr++;
181
182   /* sinVal += d*wd; */
183   sinVal += d * wd;
184
185   /* Return the output value in 1.15(q15) format */
186   return ((q15_t) (sinVal >> 15u));
187
188 }
189
190 /**   
191  * @} end of sin group   
192  */