1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
10 * Description: Standard deviation of an array of Q31 type.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
28 * -------------------------------------------------------------------- */
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
49 * <b>Scaling and Overflow Behavior:</b>
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.
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 */
81 /* Run the below code for Cortex-M4 and Cortex-M3 */
83 q31_t *pIn; /* Temporary pointer */
88 blkCnt = blockSize >> 2u;
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. */
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. */
98 sum += ((q63_t) (in) * (in));
100 sum += ((q63_t) (in) * (in));
102 sum += ((q63_t) (in) * (in));
104 sum += ((q63_t) (in) * (in));
106 /* Decrement the loop counter */
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;
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. */
120 sum += ((q63_t) (in) * (in));
122 /* Decrement the loop counter */
126 t = (q31_t) ((1.0f / (float32_t) (blockSize - 1u)) * 1073741824.0f);
128 /* Compute Mean of squares of the input samples
129 * and then store the result in a temporary variable, meanOfSquares. */
131 meanOfSquares = (q31_t) ((sum * t) >> 30);
133 /* Reset the accumulator */
137 blkCnt = blockSize >> 2u;
139 /* Reset the input working pointer */
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. */
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. */
153 /* Decrement the loop counter */
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;
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. */
167 /* Decrement the loop counter */
173 /* Run the below code for Cortex-M0 */
175 q63_t sumOfSquares = 0; /* Accumulator */
176 /* Loop over blockSize number of values */
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. */
185 sumOfSquares += ((q63_t) (in) * (in));
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. */
191 /* Decrement the loop counter */
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);
201 #endif /* #ifndef ARM_MATH_CM0 */
203 /* Compute mean of all input values */
204 t = (q31_t) ((1.0f / (blockSize * (blockSize - 1u))) * 2147483648.0f);
205 mean = (q31_t) (sum);
207 /* Compute square of mean */
208 squareOfMean = (q31_t) (((q63_t) mean * mean) >> 31);
209 squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 31);
212 /* Compute standard deviation and then store the result to the destination */
213 arm_sqrt_q31(meanOfSquares - squareOfMean, pResult);
218 * @} end of STD group