1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
4 * Description: Variance of an array of Q31 type
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 * @addtogroup variance
41 * @brief Variance of the elements of a Q31 vector.
42 * @param[in] *pSrc points to the input vector
43 * @param[in] blockSize length of the input vector
44 * @param[out] *pResult variance value returned here
47 * <b>Scaling and Overflow Behavior:</b>
50 * The function is implemented using an internal 64-bit accumulator.
51 * The input is represented in 1.31 format, which is then downshifted by 8 bits
52 * which yields 1.23, and intermediate multiplication yields a 2.46 format.
53 * The accumulator maintains full precision of the intermediate multiplication results,
54 * but provides only a 16 guard bits.
55 * There is no saturation on intermediate additions.
56 * If the accumulator overflows it wraps around and distorts the result.
57 * In order to avoid overflows completely the input signal must be scaled down by
58 * log2(blockSize)-8 bits, as a total of blockSize additions are performed internally.
59 * After division, internal variables should be Q18.46
60 * Finally, the 18.46 accumulator is right shifted by 15 bits to yield a 1.31 format value.
69 q63_t sum
= 0; /* Accumulator */
70 q63_t meanOfSquares
, squareOfMean
; /* square of mean and mean of square */
71 q31_t in
; /* input value */
72 uint32_t blkCnt
; /* loop counter */
73 q63_t sumOfSquares
= 0; /* Accumulator */
81 #if defined (ARM_MATH_DSP)
82 /* Run the below code for Cortex-M4 and Cortex-M3 */
85 blkCnt
= blockSize
>> 2U;
87 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
88 ** a second loop below computes the remaining 1 to 3 samples. */
91 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
92 /* Compute Sum of squares of the input samples
93 * and then store the result in a temporary variable, sum. */
96 sumOfSquares
+= ((q63_t
) (in
) * (in
));
99 sumOfSquares
+= ((q63_t
) (in
) * (in
));
102 sumOfSquares
+= ((q63_t
) (in
) * (in
));
105 sumOfSquares
+= ((q63_t
) (in
) * (in
));
107 /* Decrement the loop counter */
111 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
112 ** No loop unrolling is used. */
113 blkCnt
= blockSize
% 0x4U
;
117 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
118 /* Compute Sum of squares of the input samples
119 * and then store the result in a temporary variable, sum. */
122 sumOfSquares
+= ((q63_t
) (in
) * (in
));
124 /* Decrement the loop counter */
128 /* Compute Mean of squares of the input samples
129 * and then store the result in a temporary variable, meanOfSquares. */
130 meanOfSquares
= sumOfSquares
/ (q63_t
)(blockSize
- 1U);
133 /* Run the below code for Cortex-M0 */
135 /* Loop over blockSize number of values */
140 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
141 /* Compute Sum of squares of the input samples
142 * and then store the result in a temporary variable, sumOfSquares. */
144 sumOfSquares
+= ((q63_t
) (in
) * (in
));
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. */
150 /* Decrement the loop counter */
154 /* Compute Mean of squares of the input samples
155 * and then store the result in a temporary variable, meanOfSquares. */
156 meanOfSquares
= sumOfSquares
/ (q63_t
)(blockSize
- 1U);
158 #endif /* #if defined (ARM_MATH_DSP) */
160 /* Compute square of mean */
161 squareOfMean
= sum
* sum
/ (q63_t
)(blockSize
* (blockSize
- 1U));
163 /* Compute standard deviation and then store the result to the destination */
164 *pResult
= (meanOfSquares
- squareOfMean
) >> 15U;
168 * @} end of variance group