1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
4 * Description: Standard deviation of the elements of a floating-point 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.
36 * @defgroup STD Standard deviation
38 * Calculates the standard deviation of the elements in the input vector.
39 * The underlying algorithm is used:
42 * Result = sqrt((sumOfSquares - sum<sup>2</sup> / blockSize) / (blockSize - 1))
44 * where, sumOfSquares = pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]
46 * sum = pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1]
49 * There are separate functions for floating point, Q31, and Q15 data types.
59 * @brief Standard deviation of the elements of a floating-point vector.
60 * @param[in] *pSrc points to the input vector
61 * @param[in] blockSize length of the input vector
62 * @param[out] *pResult standard deviation value returned here
71 float32_t sum
= 0.0f
; /* Temporary result storage */
72 float32_t sumOfSquares
= 0.0f
; /* Sum of squares */
73 float32_t in
; /* input value */
74 uint32_t blkCnt
; /* loop counter */
75 #if defined (ARM_MATH_DSP)
76 float32_t meanOfSquares
, mean
, squareOfMean
; /* Temporary variables */
78 float32_t squareOfSum
; /* Square of Sum */
79 float32_t var
; /* Temporary varaince storage */
88 #if defined (ARM_MATH_DSP)
89 /* Run the below code for Cortex-M4 and Cortex-M3 */
92 blkCnt
= blockSize
>> 2U;
94 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
95 ** a second loop below computes the remaining 1 to 3 samples. */
98 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
99 /* Compute Sum of squares of the input samples
100 * and then store the result in a temporary variable, sum. */
103 sumOfSquares
+= in
* in
;
106 sumOfSquares
+= in
* in
;
109 sumOfSquares
+= in
* in
;
112 sumOfSquares
+= in
* in
;
114 /* Decrement the loop counter */
118 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
119 ** No loop unrolling is used. */
120 blkCnt
= blockSize
% 0x4U
;
124 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
125 /* Compute Sum of squares of the input samples
126 * and then store the result in a temporary variable, sum. */
129 sumOfSquares
+= in
* in
;
131 /* Decrement the loop counter */
135 /* Compute Mean of squares of the input samples
136 * and then store the result in a temporary variable, meanOfSquares. */
137 meanOfSquares
= sumOfSquares
/ ((float32_t
) blockSize
- 1.0f
);
139 /* Compute mean of all input values */
140 mean
= sum
/ (float32_t
) blockSize
;
142 /* Compute square of mean */
143 squareOfMean
= (mean
* mean
) * (((float32_t
) blockSize
) /
144 ((float32_t
) blockSize
- 1.0f
));
146 /* Compute standard deviation and then store the result to the destination */
147 arm_sqrt_f32((meanOfSquares
- squareOfMean
), pResult
);
150 /* Run the below code for Cortex-M0 */
152 /* Loop over blockSize number of values */
157 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
158 /* Compute Sum of squares of the input samples
159 * and then store the result in a temporary variable, sumOfSquares. */
161 sumOfSquares
+= in
* in
;
163 /* C = (A[0] + A[1] + ... + A[blockSize-1]) */
164 /* Compute Sum of the input samples
165 * and then store the result in a temporary variable, sum. */
168 /* Decrement the loop counter */
172 /* Compute the square of sum */
173 squareOfSum
= ((sum
* sum
) / (float32_t
) blockSize
);
175 /* Compute the variance */
176 var
= ((sumOfSquares
- squareOfSum
) / (float32_t
) (blockSize
- 1.0f
));
178 /* Compute standard deviation and then store the result to the destination */
179 arm_sqrt_f32(var
, pResult
);
181 #endif /* #if defined (ARM_MATH_DSP) */
185 * @} end of STD group