arm_var_f32.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_var_f32.c
  4. * Description: Variance 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. #include "arm_math.h"
  29. /**
  30. * @ingroup groupStats
  31. */
  32. /**
  33. * @defgroup variance Variance
  34. *
  35. * Calculates the variance of the elements in the input vector.
  36. * The underlying algorithm used is the direct method sometimes referred to as the two-pass method:
  37. *
  38. * <pre>
  39. * Result = sum(element - meanOfElements)^2) / numElement - 1
  40. *
  41. * where, meanOfElements = ( pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] ) / blockSize
  42. *
  43. * </pre>
  44. *
  45. * There are separate functions for floating point, Q31, and Q15 data types.
  46. */
  47. /**
  48. * @addtogroup variance
  49. * @{
  50. */
  51. /**
  52. * @brief Variance of the elements of a floating-point vector.
  53. * @param[in] *pSrc points to the input vector
  54. * @param[in] blockSize length of the input vector
  55. * @param[out] *pResult variance value returned here
  56. * @return none.
  57. */
  58. void arm_var_f32(
  59. float32_t * pSrc,
  60. uint32_t blockSize,
  61. float32_t * pResult)
  62. {
  63. float32_t fMean, fValue;
  64. uint32_t blkCnt; /* loop counter */
  65. float32_t * pInput = pSrc;
  66. float32_t sum = 0.0f;
  67. float32_t fSum = 0.0f;
  68. #if defined(ARM_MATH_DSP)
  69. float32_t in1, in2, in3, in4;
  70. #endif
  71. if (blockSize <= 1U)
  72. {
  73. *pResult = 0;
  74. return;
  75. }
  76. #if defined(ARM_MATH_DSP)
  77. /* Run the below code for Cortex-M4 and Cortex-M7 */
  78. /*loop Unrolling */
  79. blkCnt = blockSize >> 2U;
  80. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  81. ** a second loop below computes the remaining 1 to 3 samples. */
  82. while (blkCnt > 0U)
  83. {
  84. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  85. in1 = *pInput++;
  86. in2 = *pInput++;
  87. in3 = *pInput++;
  88. in4 = *pInput++;
  89. sum += in1;
  90. sum += in2;
  91. sum += in3;
  92. sum += in4;
  93. /* Decrement the loop counter */
  94. blkCnt--;
  95. }
  96. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  97. ** No loop unrolling is used. */
  98. blkCnt = blockSize % 0x4U;
  99. #else
  100. /* Run the below code for Cortex-M0 or Cortex-M3 */
  101. /* Loop over blockSize number of values */
  102. blkCnt = blockSize;
  103. #endif
  104. while (blkCnt > 0U)
  105. {
  106. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  107. sum += *pInput++;
  108. /* Decrement the loop counter */
  109. blkCnt--;
  110. }
  111. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
  112. fMean = sum / (float32_t) blockSize;
  113. pInput = pSrc;
  114. #if defined(ARM_MATH_DSP)
  115. /*loop Unrolling */
  116. blkCnt = blockSize >> 2U;
  117. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  118. ** a second loop below computes the remaining 1 to 3 samples. */
  119. while (blkCnt > 0U)
  120. {
  121. fValue = *pInput++ - fMean;
  122. fSum += fValue * fValue;
  123. fValue = *pInput++ - fMean;
  124. fSum += fValue * fValue;
  125. fValue = *pInput++ - fMean;
  126. fSum += fValue * fValue;
  127. fValue = *pInput++ - fMean;
  128. fSum += fValue * fValue;
  129. /* Decrement the loop counter */
  130. blkCnt--;
  131. }
  132. blkCnt = blockSize % 0x4U;
  133. #else
  134. /* Run the below code for Cortex-M0 or Cortex-M3 */
  135. /* Loop over blockSize number of values */
  136. blkCnt = blockSize;
  137. #endif
  138. while (blkCnt > 0U)
  139. {
  140. fValue = *pInput++ - fMean;
  141. fSum += fValue * fValue;
  142. /* Decrement the loop counter */
  143. blkCnt--;
  144. }
  145. /* Variance */
  146. *pResult = fSum / (float32_t)(blockSize - 1.0f);
  147. }
  148. /**
  149. * @} end of variance group
  150. */