1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_std_q31.c
4 * Description: Standard deviation of the elements of a Q31 vector
5 *
6 * $Date: 23 April 2021
7 * $Revision: V1.9.0
8 *
9 * Target Processor: Cortex-M and Cortex-A cores
10 * -------------------------------------------------------------------- */
11 /*
12 * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13 *
14 * SPDX-License-Identifier: Apache-2.0
15 *
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
19 *
20 * www.apache.org/licenses/LICENSE-2.0
21 *
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.
27 */
28
29 #include "dsp/statistics_functions.h"
30
31 /**
32 @ingroup groupStats
33 */
34
35 /**
36 @addtogroup STD
37 @{
38 */
39
40 /**
41 @brief Standard deviation of the elements of a Q31 vector.
42 @param[in] pSrc points to the input vector.
43 @param[in] blockSize number of samples in input vector.
44 @param[out] pResult standard deviation value returned here.
45
46 @par Scaling and Overflow Behavior
47 The function is implemented using an internal 64-bit accumulator.
48 The input is represented in 1.31 format, which is then downshifted by 8 bits
49 which yields 1.23, and intermediate multiplication yields a 2.46 format.
50 The accumulator maintains full precision of the intermediate multiplication results,
51 but provides only a 16 guard bits.
52 There is no saturation on intermediate additions.
53 If the accumulator overflows it wraps around and distorts the result.
54 In order to avoid overflows completely the input signal must be scaled down by
55 log2(blockSize)-8 bits, as a total of blockSize additions are performed internally.
56 After division, internal variables should be Q18.46
57 Finally, the 18.46 accumulator is right shifted by 15 bits to yield a 1.31 format value.
58 */
59 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_std_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)60 ARM_DSP_ATTRIBUTE void arm_std_q31(
61 const q31_t * pSrc,
62 uint32_t blockSize,
63 q31_t * pResult)
64 {
65 q31_t var=0;
66
67 arm_var_q31(pSrc, blockSize, &var);
68 arm_sqrt_q31(var, pResult);
69 }
70 #else
arm_std_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)71 ARM_DSP_ATTRIBUTE void arm_std_q31(
72 const q31_t * pSrc,
73 uint32_t blockSize,
74 q31_t * pResult)
75 {
76 uint32_t blkCnt; /* Loop counter */
77 q63_t sum = 0; /* Accumulator */
78 q63_t meanOfSquares, squareOfMean; /* Square of mean and mean of square */
79 q63_t sumOfSquares = 0; /* Sum of squares */
80 q31_t in; /* Temporary variable to store input value */
81
82 if (blockSize <= 1U)
83 {
84 *pResult = 0;
85 return;
86 }
87
88 #if defined (ARM_MATH_LOOPUNROLL)
89
90 /* Loop unrolling: Compute 4 outputs at a time */
91 blkCnt = blockSize >> 2U;
92
93 while (blkCnt > 0U)
94 {
95 /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
96 /* C = A[0] + A[1] + ... + A[blockSize-1] */
97
98 in = *pSrc++ >> 8U;
99 /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
100 sumOfSquares += ((q63_t) (in) * (in));
101 /* Compute sum and store result in a temporary variable, sum. */
102 sum += in;
103
104 in = *pSrc++ >> 8U;
105 sumOfSquares += ((q63_t) (in) * (in));
106 sum += in;
107
108 in = *pSrc++ >> 8U;
109 sumOfSquares += ((q63_t) (in) * (in));
110 sum += in;
111
112 in = *pSrc++ >> 8U;
113 sumOfSquares += ((q63_t) (in) * (in));
114 sum += in;
115
116 /* Decrement loop counter */
117 blkCnt--;
118 }
119
120 /* Loop unrolling: Compute remaining outputs */
121 blkCnt = blockSize % 0x4U;
122
123 #else
124
125 /* Initialize blkCnt with number of samples */
126 blkCnt = blockSize;
127
128 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
129
130 while (blkCnt > 0U)
131 {
132 /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
133 /* C = A[0] + A[1] + ... + A[blockSize-1] */
134
135 in = *pSrc++ >> 8U;
136 /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
137 sumOfSquares += ((q63_t) (in) * (in));
138 /* Compute sum and store result in a temporary variable, sum. */
139 sum += in;
140
141 /* Decrement loop counter */
142 blkCnt--;
143 }
144
145 /* Compute Mean of squares and store result in a temporary variable, meanOfSquares. */
146 meanOfSquares = (sumOfSquares / (q63_t)(blockSize - 1U));
147
148 /* Compute square of mean */
149 squareOfMean = ( sum * sum / (q63_t)(blockSize * (blockSize - 1U)));
150
151 /* Compute standard deviation and store result in destination */
152 arm_sqrt_q31((meanOfSquares - squareOfMean) >> 15U, pResult);
153 }
154 #endif /* defined(ARM_MATH_MVEI) */
155
156 /**
157 @} end of STD group
158 */
159