source: S-port/trunk/Drivers/CMSIS/DSP/Source/StatisticsFunctions/arm_std_f32.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 5.6 KB
Line 
1/* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_std_f32.c
4 * Description: Standard deviation of the elements of a floating-point vector
5 *
6 * $Date: 27. January 2017
7 * $Revision: V.1.5.1
8 *
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
11/*
12 * Copyright (C) 2010-2017 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 "arm_math.h"
30
31/**
32 * @ingroup groupStats
33 */
34
35/**
36 * @defgroup STD Standard deviation
37 *
38 * Calculates the standard deviation of the elements in the input vector.
39 * The underlying algorithm is used:
40 *
41 * <pre>
42 * Result = sqrt((sumOfSquares - sum<sup>2</sup> / blockSize) / (blockSize - 1))
43 *
44 * where, sumOfSquares = pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]
45 *
46 * sum = pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1]
47 * </pre>
48 *
49 * There are separate functions for floating point, Q31, and Q15 data types.
50 */
51
52/**
53 * @addtogroup STD
54 * @{
55 */
56
57
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.
64 */
65
66void arm_std_f32(
67 float32_t * pSrc,
68 uint32_t blockSize,
69 float32_t * pResult)
70{
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
81
82 if (blockSize == 1U)
83 {
84 *pResult = 0;
85 return;
86 }
87
88#if defined (ARM_MATH_DSP)
89 /* Run the below code for Cortex-M4 and Cortex-M3 */
90
91 /*loop Unrolling */
92 blkCnt = blockSize >> 2U;
93
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)
97 {
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;
113
114 /* Decrement the loop counter */
115 blkCnt--;
116 }
117
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;
121
122 while (blkCnt > 0U)
123 {
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;
130
131 /* Decrement the loop counter */
132 blkCnt--;
133 }
134
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);
138
139 /* Compute mean of all input values */
140 mean = sum / (float32_t) blockSize;
141
142 /* Compute square of mean */
143 squareOfMean = (mean * mean) * (((float32_t) blockSize) /
144 ((float32_t) blockSize - 1.0f));
145
146 /* Compute standard deviation and then store the result to the destination */
147 arm_sqrt_f32((meanOfSquares - squareOfMean), pResult);
148
149#else
150 /* Run the below code for Cortex-M0 */
151
152 /* Loop over blockSize number of values */
153 blkCnt = blockSize;
154
155 while (blkCnt > 0U)
156 {
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;
162
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;
167
168 /* Decrement the loop counter */
169 blkCnt--;
170 }
171
172 /* Compute the square of sum */
173 squareOfSum = ((sum * sum) / (float32_t) blockSize);
174
175 /* Compute the variance */
176 var = ((sumOfSquares - squareOfSum) / (float32_t) (blockSize - 1.0f));
177
178 /* Compute standard deviation and then store the result to the destination */
179 arm_sqrt_f32(var, pResult);
180
181#endif /* #if defined (ARM_MATH_DSP) */
182}
183
184/**
185 * @} end of STD group
186 */
Note: See TracBrowser for help on using the repository browser.