Merge pull request #11270 from haslinghuis/rename_attr
[betaflight.git] / lib / main / CMSIS / DSP / Source / StatisticsFunctions / arm_std_f32.c
blob9750b88b8606cfa988a9838295a86125573dc682
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_std_f32.c
4 * Description: Standard deviation of the elements of a floating-point vector
6 * $Date: 27. January 2017
7 * $Revision: V.1.5.1
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.
29 #include "arm_math.h"
31 /**
32 * @ingroup groupStats
35 /**
36 * @defgroup STD Standard deviation
38 * Calculates the standard deviation of the elements in the input vector.
39 * The underlying algorithm is used:
41 * <pre>
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]
47 * </pre>
49 * There are separate functions for floating point, Q31, and Q15 data types.
52 /**
53 * @addtogroup STD
54 * @{
58 /**
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
63 * @return none.
66 void arm_std_f32(
67 float32_t * pSrc,
68 uint32_t blockSize,
69 float32_t * pResult)
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 */
77 #else
78 float32_t squareOfSum; /* Square of Sum */
79 float32_t var; /* Temporary varaince storage */
80 #endif
82 if (blockSize == 1U)
84 *pResult = 0;
85 return;
88 #if defined (ARM_MATH_DSP)
89 /* Run the below code for Cortex-M4 and Cortex-M3 */
91 /*loop Unrolling */
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. */
96 while (blkCnt > 0U)
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. */
101 in = *pSrc++;
102 sum += in;
103 sumOfSquares += in * in;
104 in = *pSrc++;
105 sum += in;
106 sumOfSquares += in * in;
107 in = *pSrc++;
108 sum += in;
109 sumOfSquares += in * in;
110 in = *pSrc++;
111 sum += in;
112 sumOfSquares += in * in;
114 /* Decrement the loop counter */
115 blkCnt--;
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;
122 while (blkCnt > 0U)
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. */
127 in = *pSrc++;
128 sum += in;
129 sumOfSquares += in * in;
131 /* Decrement the loop counter */
132 blkCnt--;
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);
149 #else
150 /* Run the below code for Cortex-M0 */
152 /* Loop over blockSize number of values */
153 blkCnt = blockSize;
155 while (blkCnt > 0U)
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. */
160 in = *pSrc++;
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. */
166 sum += in;
168 /* Decrement the loop counter */
169 blkCnt--;
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