arm_var_f32.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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. Calculates the variance of the elements in the input vector.
  35. The underlying algorithm used is the direct method sometimes referred to as the two-pass method:
  36. <pre>
  37. Result = sum(element - meanOfElements)^2) / numElement - 1
  38. meanOfElements = ( pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] ) / blockSize
  39. </pre>
  40. There are separate functions for floating point, Q31, and Q15 data types.
  41. */
  42. /**
  43. @addtogroup variance
  44. @{
  45. */
  46. /**
  47. @brief Variance of the elements of a floating-point vector.
  48. @param[in] pSrc points to the input vector
  49. @param[in] blockSize number of samples in input vector
  50. @param[out] pResult variance value returned here
  51. @return none
  52. */
  53. #if defined(ARM_MATH_NEON_EXPERIMENTAL)
  54. void arm_var_f32(
  55. const float32_t * pSrc,
  56. uint32_t blockSize,
  57. float32_t * pResult)
  58. {
  59. float32_t mean;
  60. float32_t sum = 0.0f; /* accumulator */
  61. float32_t in; /* Temporary variable to store input value */
  62. uint32_t blkCnt; /* loop counter */
  63. float32x4_t sumV = vdupq_n_f32(0.0f); /* Temporary result storage */
  64. float32x2_t sumV2;
  65. float32x4_t inV;
  66. float32x4_t avg;
  67. arm_mean_f32(pSrc,blockSize,&mean);
  68. avg = vdupq_n_f32(mean);
  69. blkCnt = blockSize >> 2U;
  70. /* Compute 4 outputs at a time.
  71. ** a second loop below computes the remaining 1 to 3 samples. */
  72. while (blkCnt > 0U)
  73. {
  74. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  75. /* Compute Power and then store the result in a temporary variable, sum. */
  76. inV = vld1q_f32(pSrc);
  77. inV = vsubq_f32(inV, avg);
  78. sumV = vmlaq_f32(sumV, inV, inV);
  79. pSrc += 4;
  80. /* Decrement the loop counter */
  81. blkCnt--;
  82. }
  83. sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
  84. sum = sumV2[0] + sumV2[1];
  85. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  86. ** No loop unrolling is used. */
  87. blkCnt = blockSize % 0x4U;
  88. while (blkCnt > 0U)
  89. {
  90. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  91. /* compute power and then store the result in a temporary variable, sum. */
  92. in = *pSrc++;
  93. in = in - mean;
  94. sum += in * in;
  95. /* Decrement the loop counter */
  96. blkCnt--;
  97. }
  98. /* Variance */
  99. *pResult = sum / (float32_t)(blockSize - 1.0f);
  100. }
  101. #else
  102. void arm_var_f32(
  103. const float32_t * pSrc,
  104. uint32_t blockSize,
  105. float32_t * pResult)
  106. {
  107. uint32_t blkCnt; /* Loop counter */
  108. float32_t sum = 0.0f; /* Temporary result storage */
  109. float32_t fSum = 0.0f;
  110. float32_t fMean, fValue;
  111. const float32_t * pInput = pSrc;
  112. if (blockSize <= 1U)
  113. {
  114. *pResult = 0;
  115. return;
  116. }
  117. #if defined (ARM_MATH_LOOPUNROLL)
  118. /* Loop unrolling: Compute 4 outputs at a time */
  119. blkCnt = blockSize >> 2U;
  120. while (blkCnt > 0U)
  121. {
  122. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  123. sum += *pInput++;
  124. sum += *pInput++;
  125. sum += *pInput++;
  126. sum += *pInput++;
  127. /* Decrement loop counter */
  128. blkCnt--;
  129. }
  130. /* Loop unrolling: Compute remaining outputs */
  131. blkCnt = blockSize % 0x4U;
  132. #else
  133. /* Initialize blkCnt with number of samples */
  134. blkCnt = blockSize;
  135. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  136. while (blkCnt > 0U)
  137. {
  138. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  139. sum += *pInput++;
  140. /* Decrement loop counter */
  141. blkCnt--;
  142. }
  143. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
  144. fMean = sum / (float32_t) blockSize;
  145. pInput = pSrc;
  146. #if defined (ARM_MATH_LOOPUNROLL)
  147. /* Loop unrolling: Compute 4 outputs at a time */
  148. blkCnt = blockSize >> 2U;
  149. while (blkCnt > 0U)
  150. {
  151. fValue = *pInput++ - fMean;
  152. fSum += fValue * fValue;
  153. fValue = *pInput++ - fMean;
  154. fSum += fValue * fValue;
  155. fValue = *pInput++ - fMean;
  156. fSum += fValue * fValue;
  157. fValue = *pInput++ - fMean;
  158. fSum += fValue * fValue;
  159. /* Decrement loop counter */
  160. blkCnt--;
  161. }
  162. /* Loop unrolling: Compute remaining outputs */
  163. blkCnt = blockSize % 0x4U;
  164. #else
  165. /* Initialize blkCnt with number of samples */
  166. blkCnt = blockSize;
  167. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  168. while (blkCnt > 0U)
  169. {
  170. fValue = *pInput++ - fMean;
  171. fSum += fValue * fValue;
  172. /* Decrement loop counter */
  173. blkCnt--;
  174. }
  175. /* Variance */
  176. *pResult = fSum / (float32_t)(blockSize - 1.0f);
  177. }
  178. #endif /* #if defined(ARM_MATH_NEON) */
  179. /**
  180. @} end of variance group
  181. */