]> git.gag.com Git - fw/stlink/blob - exampleF4/CMSIS/DSP_Lib/Source/StatisticsFunctions/arm_std_q15.c
Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / StatisticsFunctions / arm_std_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_std_q15.c   
9 *   
10 * Description:  Standard deviation of an array of Q15 type.   
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 groupStats   
34  */
35
36 /**   
37  * @addtogroup STD   
38  * @{   
39  */
40
41 /**   
42  * @brief Standard deviation of the elements of a Q15 vector.   
43  * @param[in]       *pSrc points to the input vector   
44  * @param[in]       blockSize length of the input vector   
45  * @param[out]      *pResult standard deviation value returned here   
46  * @return none.   
47  *   
48  * @details   
49  * <b>Scaling and Overflow Behavior:</b>   
50  *   
51  * \par   
52  * The function is implemented using a 64-bit internal accumulator.   
53  * The input is represented in 1.15 format.  
54  * Intermediate multiplication yields a 2.30 format, and this   
55  * result is added without saturation to a 64-bit accumulator in 34.30 format.   
56  * With 33 guard bits in the accumulator, there is no risk of overflow, and the   
57  * full precision of the intermediate multiplication is preserved.   
58  * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower    
59  * 15 bits, and then saturated to yield a result in 1.15 format.   
60  */
61
62 void arm_std_q15(
63   q15_t * pSrc,
64   uint32_t blockSize,
65   q15_t * pResult)
66 {
67   q63_t sum = 0;                                 /* Accumulator */
68   q31_t meanOfSquares, squareOfMean;             /* square of mean and mean of square */
69   q15_t mean;                                    /* mean */
70   uint32_t blkCnt;                               /* loop counter */
71   q15_t t;                                       /* Temporary variable */
72
73 #ifndef ARM_MATH_CM0
74
75   /* Run the below code for Cortex-M4 and Cortex-M3 */
76
77   q15_t *pIn;                                    /* Temporary pointer */
78   q31_t in;                                      /* input value */
79   q15_t in1;                                     /* input value */
80
81   pIn = pSrc;
82
83   /*loop Unrolling */
84   blkCnt = blockSize >> 2u;
85
86   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
87    ** a second loop below computes the remaining 1 to 3 samples. */
88   while(blkCnt > 0u)
89   {
90     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1])  */
91     /* Compute Sum of squares of the input samples   
92      * and then store the result in a temporary variable, sum. */
93     in = *__SIMD32(pSrc)++;
94     sum = __SMLALD(in, in, sum);
95     in = *__SIMD32(pSrc)++;
96     sum = __SMLALD(in, in, sum);
97
98     /* Decrement the loop counter */
99     blkCnt--;
100   }
101
102   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
103    ** No loop unrolling is used. */
104   blkCnt = blockSize % 0x4u;
105
106   while(blkCnt > 0u)
107   {
108     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
109     /* Compute Sum of squares of the input samples   
110      * and then store the result in a temporary variable, sum. */
111     in1 = *pSrc++;
112     sum = __SMLALD(in1, in1, sum);
113
114     /* Decrement the loop counter */
115     blkCnt--;
116   }
117
118   /* Compute Mean of squares of the input samples   
119    * and then store the result in a temporary variable, meanOfSquares. */
120   t = (q15_t) ((1.0 / (blockSize - 1)) * 16384LL);
121   sum = __SSAT((sum >> 15u), 16u);
122
123   meanOfSquares = (q31_t) ((sum * t) >> 14u);
124
125   /* Reset the accumulator */
126   sum = 0;
127
128   /*loop Unrolling */
129   blkCnt = blockSize >> 2u;
130
131   /* Reset the input working pointer */
132   pSrc = pIn;
133
134   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
135    ** a second loop below computes the remaining 1 to 3 samples. */
136   while(blkCnt > 0u)
137   {
138     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
139     /* Compute sum of all input values and then store the result in a temporary variable, sum. */
140     sum += *pSrc++;
141     sum += *pSrc++;
142     sum += *pSrc++;
143     sum += *pSrc++;
144
145     /* Decrement the loop counter */
146     blkCnt--;
147   }
148
149   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
150    ** No loop unrolling is used. */
151   blkCnt = blockSize % 0x4u;
152
153   while(blkCnt > 0u)
154   {
155     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
156     /* Compute sum of all input values and then store the result in a temporary variable, sum. */
157     sum += *pSrc++;
158
159     /* Decrement the loop counter */
160     blkCnt--;
161   }
162   /* Compute mean of all input values */
163   t = (q15_t) ((1.0 / (blockSize * (blockSize - 1))) * 32768LL);
164   mean = (q15_t) __SSAT(sum, 16u);
165
166   /* Compute square of mean */
167   squareOfMean = ((q31_t) mean * mean) >> 15;
168   squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 15);
169
170   /* mean of the squares minus the square of the mean. */
171   in1 = (q15_t) (meanOfSquares - squareOfMean);
172
173   /* Compute standard deviation and store the result to the destination */
174   arm_sqrt_q15(in1, pResult);
175
176 #else
177
178   /* Run the below code for Cortex-M0 */
179
180   q63_t sumOfSquares = 0;                        /* Accumulator */
181   q15_t in;                                      /* input value */
182   /* Loop over blockSize number of values */
183   blkCnt = blockSize;
184
185   while(blkCnt > 0u)
186   {
187     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
188     /* Compute Sum of squares of the input samples    
189      * and then store the result in a temporary variable, sumOfSquares. */
190     in = *pSrc++;
191     sumOfSquares += (in * in);
192
193     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
194     /* Compute sum of all input values and then store the result in a temporary variable, sum. */
195     sum += in;
196
197     /* Decrement the loop counter */
198     blkCnt--;
199   }
200
201   /* Compute Mean of squares of the input samples    
202    * and then store the result in a temporary variable, meanOfSquares. */
203   t = (q15_t) ((1.0 / (blockSize - 1)) * 16384LL);
204   sumOfSquares = __SSAT((sumOfSquares >> 15u), 16u);
205   meanOfSquares = (q31_t) ((sumOfSquares * t) >> 14u);
206
207   /* Compute mean of all input values */
208   mean = (q15_t) __SSAT(sum, 16u);
209
210   /* Compute square of mean of the input samples  
211    * and then store the result in a temporary variable, squareOfMean.*/
212   t = (q15_t) ((1.0 / (blockSize * (blockSize - 1))) * 32768LL);
213   squareOfMean = ((q31_t) mean * mean) >> 15;
214   squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 15);
215
216   /* mean of the squares minus the square of the mean. */
217   in = (q15_t) (meanOfSquares - squareOfMean);
218
219   /* Compute standard deviation and store the result to the destination */
220   arm_sqrt_q15(in, pResult);
221
222 #endif /* #ifndef ARM_MATH_CM0 */
223
224
225 }
226
227 /**   
228  * @} end of STD group   
229  */