1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
10 * Description: Variance of the elements of a floating-point vector.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
28 * ---------------------------------------------------------------------------- */
37 * @defgroup variance Variance
39 * Calculates the variance of the elements in the input vector.
40 * The underlying algorithm is used:
43 * Result = (sumOfSquares - sum<sup>2</sup> / blockSize) / (blockSize - 1)
45 * where, sumOfSquares = pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]
47 * sum = pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1]
50 * There are separate functions for floating point, Q31, and Q15 data types.
54 * @addtogroup variance
60 * @brief Variance of the elements of a floating-point vector.
61 * @param[in] *pSrc points to the input vector
62 * @param[in] blockSize length of the input vector
63 * @param[out] *pResult variance value returned here
77 /* Run the below code for Cortex-M4 and Cortex-M3 */
79 float32_t sum = (float32_t) 0.0; /* Accumulator */
80 float32_t meanOfSquares, mean, in, squareOfMean; /* Temporary variables */
81 uint32_t blkCnt; /* loop counter */
82 float32_t *pIn; /* Temporary pointer */
84 /* updating temporary pointer */
88 blkCnt = blockSize >> 2u;
90 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
91 ** a second loop below computes the remaining 1 to 3 samples. */
94 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
95 /* Compute Sum of squares of the input samples
96 * and then store the result in a temporary variable, sum. */
106 /* Decrement the loop counter */
110 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
111 ** No loop unrolling is used. */
112 blkCnt = blockSize % 0x4u;
116 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
117 /* Compute Sum of squares of the input samples
118 * and then store the result in a temporary variable, sum. */
122 /* Decrement the loop counter */
126 /* Compute Mean of squares of the input samples
127 * and then store the result in a temporary variable, meanOfSquares. */
128 meanOfSquares = sum / ((float32_t) blockSize - 1.0f);
130 /* Reset the accumulator */
134 blkCnt = blockSize >> 2u;
136 /* Reset the input working pointer */
139 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
140 ** a second loop below computes the remaining 1 to 3 samples. */
143 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
144 /* Compute sum of all input values and then store the result in a temporary variable, sum. */
150 /* Decrement the loop counter */
154 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
155 ** No loop unrolling is used. */
156 blkCnt = blockSize % 0x4u;
160 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
161 /* Compute sum of all input values and then store the result in a temporary variable, sum. */
164 /* Decrement the loop counter */
167 /* Compute mean of all input values */
168 mean = sum / (float32_t) blockSize;
170 /* Compute square of mean */
171 squareOfMean = (mean * mean) * (((float32_t) blockSize) /
172 ((float32_t) blockSize - 1.0f));
174 /* Compute variance and then store the result to the destination */
175 *pResult = meanOfSquares - squareOfMean;
179 /* Run the below code for Cortex-M0 */
181 float32_t sum = 0.0f; /* Temporary result storage */
182 float32_t sumOfSquares = 0.0f; /* Sum of squares */
183 float32_t squareOfSum; /* Square of Sum */
184 float32_t in; /* input value */
185 uint32_t blkCnt; /* loop counter */
187 /* Loop over blockSize number of values */
192 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
193 /* Compute Sum of squares of the input samples
194 * and then store the result in a temporary variable, sumOfSquares. */
196 sumOfSquares += in * in;
198 /* C = (A[0] + A[1] + ... + A[blockSize-1]) */
199 /* Compute Sum of the input samples
200 * and then store the result in a temporary variable, sum. */
203 /* Decrement the loop counter */
207 /* Compute the square of sum */
208 squareOfSum = ((sum * sum) / (float32_t) blockSize);
210 /* Compute the variance */
211 *pResult = ((sumOfSquares - squareOfSum) / (float32_t) (blockSize - 1.0f));
213 #endif /* #ifndef ARM_MATH_CM0 */
218 * @} end of variance group