Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / FastMathFunctions / arm_cos_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_cos_q15.c   
9 *   
10 * Description:  Fast cosine 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 cos   
38  * @{   
39  */
40
41 /**  
42 * \par   
43 * Table Values are in Q15(1.15 Fixed point format) and generation is done in three steps   
44 * \par   
45 * First Generate cos values in floating point:   
46 * tableSize = 256;    
47 * <pre>for(n = -1; n < (tableSize + 1); n++)   
48 * {   
49 *       cosTable[n+1]= cos(2*pi*n/tableSize);   
50 * }</pre>  
51 * where pi value is  3.14159265358979   
52 * \par   
53 * Secondly Convert Floating point to Q15(Fixed point):   
54 *       (cosTable[i] * pow(2, 15))   
55 * \par   
56 * Finally Rounding to nearest integer is done   
57 *       cosTable[i] += (cosTable[i] > 0 ? 0.5 :-0.5);   
58 */
59
60 static const q15_t cosTableQ15[259] = {
61   0x7ff6, 0x7fff, 0x7ff6, 0x7fd9, 0x7fa7, 0x7f62, 0x7f0a, 0x7e9d,
62   0x7e1e, 0x7d8a, 0x7ce4, 0x7c2a, 0x7b5d, 0x7a7d, 0x798a, 0x7885,
63   0x776c, 0x7642, 0x7505, 0x73b6, 0x7255, 0x70e3, 0x6f5f, 0x6dca,
64   0x6c24, 0x6a6e, 0x68a7, 0x66d0, 0x64e9, 0x62f2, 0x60ec, 0x5ed7,
65   0x5cb4, 0x5a82, 0x5843, 0x55f6, 0x539b, 0x5134, 0x4ec0, 0x4c40,
66   0x49b4, 0x471d, 0x447b, 0x41ce, 0x3f17, 0x3c57, 0x398d, 0x36ba,
67   0x33df, 0x30fc, 0x2e11, 0x2b1f, 0x2827, 0x2528, 0x2224, 0x1f1a,
68   0x1c0c, 0x18f9, 0x15e2, 0x12c8, 0xfab, 0xc8c, 0x96b, 0x648,
69   0x324, 0x0, 0xfcdc, 0xf9b8, 0xf695, 0xf374, 0xf055, 0xed38,
70   0xea1e, 0xe707, 0xe3f4, 0xe0e6, 0xdddc, 0xdad8, 0xd7d9, 0xd4e1,
71   0xd1ef, 0xcf04, 0xcc21, 0xc946, 0xc673, 0xc3a9, 0xc0e9, 0xbe32,
72   0xbb85, 0xb8e3, 0xb64c, 0xb3c0, 0xb140, 0xaecc, 0xac65, 0xaa0a,
73   0xa7bd, 0xa57e, 0xa34c, 0xa129, 0x9f14, 0x9d0e, 0x9b17, 0x9930,
74   0x9759, 0x9592, 0x93dc, 0x9236, 0x90a1, 0x8f1d, 0x8dab, 0x8c4a,
75   0x8afb, 0x89be, 0x8894, 0x877b, 0x8676, 0x8583, 0x84a3, 0x83d6,
76   0x831c, 0x8276, 0x81e2, 0x8163, 0x80f6, 0x809e, 0x8059, 0x8027,
77   0x800a, 0x8000, 0x800a, 0x8027, 0x8059, 0x809e, 0x80f6, 0x8163,
78   0x81e2, 0x8276, 0x831c, 0x83d6, 0x84a3, 0x8583, 0x8676, 0x877b,
79   0x8894, 0x89be, 0x8afb, 0x8c4a, 0x8dab, 0x8f1d, 0x90a1, 0x9236,
80   0x93dc, 0x9592, 0x9759, 0x9930, 0x9b17, 0x9d0e, 0x9f14, 0xa129,
81   0xa34c, 0xa57e, 0xa7bd, 0xaa0a, 0xac65, 0xaecc, 0xb140, 0xb3c0,
82   0xb64c, 0xb8e3, 0xbb85, 0xbe32, 0xc0e9, 0xc3a9, 0xc673, 0xc946,
83   0xcc21, 0xcf04, 0xd1ef, 0xd4e1, 0xd7d9, 0xdad8, 0xdddc, 0xe0e6,
84   0xe3f4, 0xe707, 0xea1e, 0xed38, 0xf055, 0xf374, 0xf695, 0xf9b8,
85   0xfcdc, 0x0, 0x324, 0x648, 0x96b, 0xc8c, 0xfab, 0x12c8,
86   0x15e2, 0x18f9, 0x1c0c, 0x1f1a, 0x2224, 0x2528, 0x2827, 0x2b1f,
87   0x2e11, 0x30fc, 0x33df, 0x36ba, 0x398d, 0x3c57, 0x3f17, 0x41ce,
88   0x447b, 0x471d, 0x49b4, 0x4c40, 0x4ec0, 0x5134, 0x539b, 0x55f6,
89   0x5843, 0x5a82, 0x5cb4, 0x5ed7, 0x60ec, 0x62f2, 0x64e9, 0x66d0,
90   0x68a7, 0x6a6e, 0x6c24, 0x6dca, 0x6f5f, 0x70e3, 0x7255, 0x73b6,
91   0x7505, 0x7642, 0x776c, 0x7885, 0x798a, 0x7a7d, 0x7b5d, 0x7c2a,
92   0x7ce4, 0x7d8a, 0x7e1e, 0x7e9d, 0x7f0a, 0x7f62, 0x7fa7, 0x7fd9,
93   0x7ff6, 0x7fff, 0x7ff6
94 };
95
96
97 /**  
98  * @brief Fast approximation to the trigonometric cosine function for Q15 data.  
99  * @param[in] x Scaled input value in radians.  
100  * @return  cos(x).  
101  *  
102  * The Q15 input value is in the range [0 +1) and is mapped to a radian value in the range [0 2*pi).  
103  */
104
105 q15_t arm_cos_q15(
106   q15_t x)
107 {
108   q31_t cosVal;                                  /* Temporary variable for output */
109   q15_t *tablePtr;                               /* Pointer to table */
110   q15_t in, in2;                                 /* Temporary variables for input */
111   q31_t wa, wb, wc, wd;                          /* Cubic interpolation coefficients */
112   q15_t a, b, c, d;                              /* Four nearest output values */
113   q15_t fract, fractCube, fractSquare;           /* Variables for fractional value */
114   q15_t oneBy6 = 0x1555;                         /* Fixed point value of 1/6 */
115   q15_t tableSpacing = TABLE_SPACING_Q15;        /* Table spacing */
116   int32_t index;                                 /* Index variable */
117
118   in = x;
119
120   /* Calculate the nearest index */
121   index = (int32_t) in / tableSpacing;
122
123   /* Calculate the nearest value of input */
124   in2 = (q15_t) index *tableSpacing;
125
126   /* Calculation of fractional value */
127   fract = (in - in2) << 8;
128
129   /* fractSquare = fract * fract */
130   fractSquare = (q15_t) ((fract * fract) >> 15);
131
132   /* fractCube = fract * fract * fract */
133   fractCube = (q15_t) ((fractSquare * fract) >> 15);
134
135   /* Initialise table pointer */
136   tablePtr = (q15_t *) & cosTableQ15[index];
137
138   /* Cubic interpolation process */
139   /* Calculation of wa */
140   /* wa = -(oneBy6)*fractCube + (fractSquare >> 1u) - (0x2AAA)*fract; */
141   wa = (q31_t) oneBy6 *fractCube;
142   wa += (q31_t) 0x2AAA *fract;
143   wa = -(wa >> 15);
144   wa += (fractSquare >> 1u);
145
146   /* Read first nearest value of output from the cos table */
147   a = *tablePtr++;
148
149   /* cosVal = a * wa */
150   cosVal = a * wa;
151
152   /* Calculation of wb */
153   wb = (((fractCube >> 1u) - fractSquare) - (fract >> 1u)) + 0x7FFF;
154
155   /* Read second nearest value of output from the cos table */
156   b = *tablePtr++;
157
158   /*      cosVal += b*wb */
159   cosVal += b * wb;
160
161   /* Calculation of wc */
162   wc = -(q31_t) fractCube + fractSquare;
163   wc = (wc >> 1u) + fract;
164
165   /* Read third nearest value of output from the cos table */
166   c = *tablePtr++;
167
168   /*      cosVal += c*wc */
169   cosVal += c * wc;
170
171   /* Calculation of wd */
172   /* wd = (oneBy6)*fractCube - (oneBy6)*fract; */
173   fractCube = fractCube - fract;
174   wd = ((q15_t) (((q31_t) oneBy6 * fractCube) >> 15));
175
176   /* Read fourth nearest value of output from the cos table */
177   d = *tablePtr++;
178
179   /* cosVal += d*wd; */
180   cosVal += d * wd;
181
182   /* Return the output value in 1.15(q15) format */
183   return ((q15_t) (cosVal >> 15u));
184
185 }
186
187 /**   
188  * @} end of cos group   
189  */