Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / StatisticsFunctions / arm_std_q31.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_q31.c   
9 *   
10 * Description:  Standard deviation of an array of Q31 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 /**   
43  * @brief Standard deviation of the elements of a Q31 vector.   
44  * @param[in]       *pSrc points to the input vector   
45  * @param[in]       blockSize length of the input vector   
46  * @param[out]      *pResult standard deviation value returned here   
47  * @return none.   
48  * @details   
49  * <b>Scaling and Overflow Behavior:</b>   
50  *   
51  *\par   
52  * The function is implemented using an internal 64-bit accumulator.   
53  * The input is represented in 1.31 format, and intermediate multiplication   
54  * yields a 2.62 format.   
55  * The accumulator maintains full precision of the intermediate multiplication results,    
56  * but provides only a single guard bit.   
57  * There is no saturation on intermediate additions.   
58  * If the accumulator overflows it wraps around and distorts the result.   
59  * In order to avoid overflows completely the input signal must be scaled down by    
60  * log2(blockSize) bits, as a total of blockSize additions are performed internally.    
61  * Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value.   
62  *   
63  */
64
65
66 void arm_std_q31(
67   q31_t * pSrc,
68   uint32_t blockSize,
69   q31_t * pResult)
70 {
71   q63_t sum = 0;                                 /* Accumulator */
72   q31_t meanOfSquares, squareOfMean;             /* square of mean and mean of square */
73   q31_t mean;                                    /* mean */
74   q31_t in;                                      /* input value */
75   q31_t t;                                       /* Temporary variable */
76   uint32_t blkCnt;                               /* loop counter */
77
78
79 #ifndef ARM_MATH_CM0
80
81   /* Run the below code for Cortex-M4 and Cortex-M3 */
82
83   q31_t *pIn;                                    /* Temporary pointer */
84
85   pIn = pSrc;
86
87   /*loop Unrolling */
88   blkCnt = blockSize >> 2u;
89
90   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
91    ** a second loop below computes the remaining 1 to 3 samples. */
92   while(blkCnt > 0u)
93   {
94     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1])  */
95     /* Compute Sum of squares of the input samples   
96      * and then store the result in a temporary variable, sum. */
97     in = *pSrc++;
98     sum += ((q63_t) (in) * (in));
99     in = *pSrc++;
100     sum += ((q63_t) (in) * (in));
101     in = *pSrc++;
102     sum += ((q63_t) (in) * (in));
103     in = *pSrc++;
104     sum += ((q63_t) (in) * (in));
105
106     /* Decrement the loop counter */
107     blkCnt--;
108   }
109
110   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
111    ** No loop unrolling is used. */
112   blkCnt = blockSize % 0x4u;
113
114   while(blkCnt > 0u)
115   {
116     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
117     /* Compute Sum of squares of the input samples   
118      * and then store the result in a temporary variable, sum. */
119     in = *pSrc++;
120     sum += ((q63_t) (in) * (in));
121
122     /* Decrement the loop counter */
123     blkCnt--;
124   }
125
126   t = (q31_t) ((1.0f / (float32_t) (blockSize - 1u)) * 1073741824.0f);
127
128   /* Compute Mean of squares of the input samples   
129    * and then store the result in a temporary variable, meanOfSquares. */
130   sum = (sum >> 31);
131   meanOfSquares = (q31_t) ((sum * t) >> 30);
132
133   /* Reset the accumulator */
134   sum = 0;
135
136   /*loop Unrolling */
137   blkCnt = blockSize >> 2u;
138
139   /* Reset the input working pointer */
140   pSrc = pIn;
141
142   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
143    ** a second loop below computes the remaining 1 to 3 samples. */
144   while(blkCnt > 0u)
145   {
146     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
147     /* Compute sum of all input values and then store the result in a temporary variable, sum. */
148     sum += *pSrc++;
149     sum += *pSrc++;
150     sum += *pSrc++;
151     sum += *pSrc++;
152
153     /* Decrement the loop counter */
154     blkCnt--;
155   }
156
157   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
158    ** No loop unrolling is used. */
159   blkCnt = blockSize % 0x4u;
160
161   while(blkCnt > 0u)
162   {
163     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
164     /* Compute sum of all input values and then store the result in a temporary variable, sum. */
165     sum += *pSrc++;
166
167     /* Decrement the loop counter */
168     blkCnt--;
169   }
170
171 #else
172
173   /* Run the below code for Cortex-M0 */
174
175   q63_t sumOfSquares = 0;                        /* Accumulator */
176   /* Loop over blockSize number of values */
177   blkCnt = blockSize;
178
179   while(blkCnt > 0u)
180   {
181     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
182     /* Compute Sum of squares of the input samples    
183      * and then store the result in a temporary variable, sumOfSquares. */
184     in = *pSrc++;
185     sumOfSquares += ((q63_t) (in) * (in));
186
187     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
188     /* Compute sum of all input values and then store the result in a temporary variable, sum. */
189     sum += in;
190
191     /* Decrement the loop counter */
192     blkCnt--;
193   }
194
195   /* Compute Mean of squares of the input samples    
196    * and then store the result in a temporary variable, meanOfSquares. */
197   t = (q31_t) ((1.0f / (float32_t) (blockSize - 1u)) * 1073741824.0f);
198   sumOfSquares = (sumOfSquares >> 31);
199   meanOfSquares = (q31_t) ((sumOfSquares * t) >> 30);
200
201 #endif /* #ifndef ARM_MATH_CM0 */
202
203   /* Compute mean of all input values */
204   t = (q31_t) ((1.0f / (blockSize * (blockSize - 1u))) * 2147483648.0f);
205   mean = (q31_t) (sum);
206
207   /* Compute square of mean */
208   squareOfMean = (q31_t) (((q63_t) mean * mean) >> 31);
209   squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 31);
210
211
212   /* Compute standard deviation and then store the result to the destination */
213   arm_sqrt_q31(meanOfSquares - squareOfMean, pResult);
214
215 }
216
217 /**   
218  * @} end of STD group   
219  */