1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
4 * Description: Standard deviation of an array of Q15 vector
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
20 * www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
41 * @brief Standard deviation of the elements of a Q15 vector.
42 * @param[in] *pSrc points to the input vector
43 * @param[in] blockSize length of the input vector
44 * @param[out] *pResult standard deviation value returned here
47 * <b>Scaling and Overflow Behavior:</b>
50 * The function is implemented using a 64-bit internal accumulator.
51 * The input is represented in 1.15 format.
52 * Intermediate multiplication yields a 2.30 format, and this
53 * result is added without saturation to a 64-bit accumulator in 34.30 format.
54 * With 33 guard bits in the accumulator, there is no risk of overflow, and the
55 * full precision of the intermediate multiplication is preserved.
56 * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
57 * 15 bits, and then saturated to yield a result in 1.15 format.
65 q31_t sum
= 0; /* Accumulator */
66 q31_t meanOfSquares
, squareOfMean
; /* square of mean and mean of square */
67 uint32_t blkCnt
; /* loop counter */
68 q63_t sumOfSquares
= 0; /* Accumulator */
69 #if defined (ARM_MATH_DSP)
70 q31_t in
; /* input value */
71 q15_t in1
; /* input value */
73 q15_t in
; /* input value */
82 #if defined (ARM_MATH_DSP)
83 /* Run the below code for Cortex-M4 and Cortex-M3 */
86 blkCnt
= blockSize
>> 2U;
88 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
89 ** a second loop below computes the remaining 1 to 3 samples. */
92 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
93 /* Compute Sum of squares of the input samples
94 * and then store the result in a temporary variable, sum. */
95 in
= *__SIMD32(pSrc
)++;
96 sum
+= ((in
<< 16U) >> 16U);
98 sumOfSquares
= __SMLALD(in
, in
, sumOfSquares
);
99 in
= *__SIMD32(pSrc
)++;
100 sum
+= ((in
<< 16U) >> 16U);
102 sumOfSquares
= __SMLALD(in
, in
, sumOfSquares
);
104 /* Decrement the loop counter */
108 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
109 ** No loop unrolling is used. */
110 blkCnt
= blockSize
% 0x4U
;
114 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
115 /* Compute Sum of squares of the input samples
116 * and then store the result in a temporary variable, sum. */
118 sumOfSquares
= __SMLALD(in1
, in1
, sumOfSquares
);
121 /* Decrement the loop counter */
125 /* Compute Mean of squares of the input samples
126 * and then store the result in a temporary variable, meanOfSquares. */
127 meanOfSquares
= (q31_t
)(sumOfSquares
/ (q63_t
)(blockSize
- 1U));
129 /* Compute square of mean */
130 squareOfMean
= (q31_t
)((q63_t
)sum
* sum
/ (q63_t
)(blockSize
* (blockSize
- 1U)));
132 /* mean of the squares minus the square of the mean. */
133 /* Compute standard deviation and store the result to the destination */
134 arm_sqrt_q15(__SSAT((meanOfSquares
- squareOfMean
) >> 15U, 16U), pResult
);
137 /* Run the below code for Cortex-M0 */
139 /* Loop over blockSize number of values */
144 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
145 /* Compute Sum of squares of the input samples
146 * and then store the result in a temporary variable, sumOfSquares. */
148 sumOfSquares
+= (in
* in
);
150 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
151 /* Compute sum of all input values and then store the result in a temporary variable, sum. */
154 /* Decrement the loop counter */
158 /* Compute Mean of squares of the input samples
159 * and then store the result in a temporary variable, meanOfSquares. */
160 meanOfSquares
= (q31_t
)(sumOfSquares
/ (q63_t
)(blockSize
- 1U));
162 /* Compute square of mean */
163 squareOfMean
= (q31_t
)((q63_t
)sum
* sum
/ (q63_t
)(blockSize
* (blockSize
- 1U)));
165 /* mean of the squares minus the square of the mean. */
166 /* Compute standard deviation and store the result to the destination */
167 arm_sqrt_q15(__SSAT((meanOfSquares
- squareOfMean
) >> 15U, 16U), pResult
);
169 #endif /* #if defined (ARM_MATH_DSP) */
173 * @} end of STD group