1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
10 * Description: Standard deviation of an array of Q15 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 * -------------------------------------------------------------------- */
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
49 * <b>Scaling and Overflow Behavior:</b>
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.
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 */
75 /* Run the below code for Cortex-M4 and Cortex-M3 */
77 q15_t *pIn; /* Temporary pointer */
78 q31_t in; /* input value */
79 q15_t in1; /* input value */
84 blkCnt = blockSize >> 2u;
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. */
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);
98 /* Decrement the loop counter */
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;
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. */
112 sum = __SMLALD(in1, in1, sum);
114 /* Decrement the loop counter */
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);
123 meanOfSquares = (q31_t) ((sum * t) >> 14u);
125 /* Reset the accumulator */
129 blkCnt = blockSize >> 2u;
131 /* Reset the input working pointer */
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. */
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. */
145 /* Decrement the loop counter */
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;
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. */
159 /* Decrement the loop counter */
162 /* Compute mean of all input values */
163 t = (q15_t) ((1.0 / (blockSize * (blockSize - 1))) * 32768LL);
164 mean = (q15_t) __SSAT(sum, 16u);
166 /* Compute square of mean */
167 squareOfMean = ((q31_t) mean * mean) >> 15;
168 squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 15);
170 /* mean of the squares minus the square of the mean. */
171 in1 = (q15_t) (meanOfSquares - squareOfMean);
173 /* Compute standard deviation and store the result to the destination */
174 arm_sqrt_q15(in1, pResult);
178 /* Run the below code for Cortex-M0 */
180 q63_t sumOfSquares = 0; /* Accumulator */
181 q15_t in; /* input value */
182 /* Loop over blockSize number of values */
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. */
191 sumOfSquares += (in * in);
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. */
197 /* Decrement the loop counter */
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);
207 /* Compute mean of all input values */
208 mean = (q15_t) __SSAT(sum, 16u);
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);
216 /* mean of the squares minus the square of the mean. */
217 in = (q15_t) (meanOfSquares - squareOfMean);
219 /* Compute standard deviation and store the result to the destination */
220 arm_sqrt_q15(in, pResult);
222 #endif /* #ifndef ARM_MATH_CM0 */
228 * @} end of STD group