arm_var_f32.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  54. #include "arm_helium_utils.h"
  55. void arm_var_f32(
  56. const float32_t * pSrc,
  57. uint32_t blockSize,
  58. float32_t * pResult)
  59. {
  60. uint32_t blkCnt; /* loop counters */
  61. f32x4_t vecSrc;
  62. f32x4_t sumVec = vdupq_n_f32(0.0f);
  63. float32_t fMean;
  64. float32_t sum = 0.0f; /* accumulator */
  65. float32_t in; /* Temporary variable to store input value */
  66. if (blockSize <= 1U) {
  67. *pResult = 0;
  68. return;
  69. }
  70. arm_mean_f32(pSrc, blockSize, &fMean);
  71. /* Compute 4 outputs at a time */
  72. blkCnt = blockSize >> 2U;
  73. while (blkCnt > 0U)
  74. {
  75. vecSrc = vldrwq_f32(pSrc);
  76. /*
  77. * sum lanes
  78. */
  79. vecSrc = vsubq(vecSrc, fMean);
  80. sumVec = vfmaq(sumVec, vecSrc, vecSrc);
  81. blkCnt --;
  82. pSrc += 4;
  83. }
  84. sum = vecAddAcrossF32Mve(sumVec);
  85. /*
  86. * tail
  87. */
  88. blkCnt = blockSize & 0x3;
  89. while (blkCnt > 0U)
  90. {
  91. in = *pSrc++ - fMean;
  92. sum += in * in;
  93. /* Decrement loop counter */
  94. blkCnt--;
  95. }
  96. /* Variance */
  97. *pResult = sum / (float32_t) (blockSize - 1);
  98. }
  99. #else
  100. #if defined(ARM_MATH_NEON_EXPERIMENTAL) && !defined(ARM_MATH_AUTOVECTORIZE)
  101. void arm_var_f32(
  102. const float32_t * pSrc,
  103. uint32_t blockSize,
  104. float32_t * pResult)
  105. {
  106. float32_t mean;
  107. float32_t sum = 0.0f; /* accumulator */
  108. float32_t in; /* Temporary variable to store input value */
  109. uint32_t blkCnt; /* loop counter */
  110. float32x4_t sumV = vdupq_n_f32(0.0f); /* Temporary result storage */
  111. float32x2_t sumV2;
  112. float32x4_t inV;
  113. float32x4_t avg;
  114. arm_mean_f32(pSrc,blockSize,&mean);
  115. avg = vdupq_n_f32(mean);
  116. blkCnt = blockSize >> 2U;
  117. /* Compute 4 outputs at a time.
  118. ** a second loop below computes the remaining 1 to 3 samples. */
  119. while (blkCnt > 0U)
  120. {
  121. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  122. /* Compute Power and then store the result in a temporary variable, sum. */
  123. inV = vld1q_f32(pSrc);
  124. inV = vsubq_f32(inV, avg);
  125. sumV = vmlaq_f32(sumV, inV, inV);
  126. pSrc += 4;
  127. /* Decrement the loop counter */
  128. blkCnt--;
  129. }
  130. sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
  131. sum = vget_lane_f32(sumV2, 0) + vget_lane_f32(sumV2, 1);
  132. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  133. ** No loop unrolling is used. */
  134. blkCnt = blockSize % 0x4U;
  135. while (blkCnt > 0U)
  136. {
  137. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  138. /* compute power and then store the result in a temporary variable, sum. */
  139. in = *pSrc++;
  140. in = in - mean;
  141. sum += in * in;
  142. /* Decrement the loop counter */
  143. blkCnt--;
  144. }
  145. /* Variance */
  146. *pResult = sum / (float32_t)(blockSize - 1.0f);
  147. }
  148. #else
  149. void arm_var_f32(
  150. const float32_t * pSrc,
  151. uint32_t blockSize,
  152. float32_t * pResult)
  153. {
  154. uint32_t blkCnt; /* Loop counter */
  155. float32_t sum = 0.0f; /* Temporary result storage */
  156. float32_t fSum = 0.0f;
  157. float32_t fMean, fValue;
  158. const float32_t * pInput = pSrc;
  159. if (blockSize <= 1U)
  160. {
  161. *pResult = 0;
  162. return;
  163. }
  164. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  165. /* Loop unrolling: Compute 4 outputs at a time */
  166. blkCnt = blockSize >> 2U;
  167. while (blkCnt > 0U)
  168. {
  169. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  170. sum += *pInput++;
  171. sum += *pInput++;
  172. sum += *pInput++;
  173. sum += *pInput++;
  174. /* Decrement loop counter */
  175. blkCnt--;
  176. }
  177. /* Loop unrolling: Compute remaining outputs */
  178. blkCnt = blockSize % 0x4U;
  179. #else
  180. /* Initialize blkCnt with number of samples */
  181. blkCnt = blockSize;
  182. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  183. while (blkCnt > 0U)
  184. {
  185. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  186. sum += *pInput++;
  187. /* Decrement loop counter */
  188. blkCnt--;
  189. }
  190. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
  191. fMean = sum / (float32_t) blockSize;
  192. pInput = pSrc;
  193. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  194. /* Loop unrolling: Compute 4 outputs at a time */
  195. blkCnt = blockSize >> 2U;
  196. while (blkCnt > 0U)
  197. {
  198. fValue = *pInput++ - fMean;
  199. fSum += fValue * fValue;
  200. fValue = *pInput++ - fMean;
  201. fSum += fValue * fValue;
  202. fValue = *pInput++ - fMean;
  203. fSum += fValue * fValue;
  204. fValue = *pInput++ - fMean;
  205. fSum += fValue * fValue;
  206. /* Decrement loop counter */
  207. blkCnt--;
  208. }
  209. /* Loop unrolling: Compute remaining outputs */
  210. blkCnt = blockSize % 0x4U;
  211. #else
  212. /* Initialize blkCnt with number of samples */
  213. blkCnt = blockSize;
  214. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  215. while (blkCnt > 0U)
  216. {
  217. fValue = *pInput++ - fMean;
  218. fSum += fValue * fValue;
  219. /* Decrement loop counter */
  220. blkCnt--;
  221. }
  222. /* Variance */
  223. *pResult = fSum / (float32_t)(blockSize - 1.0f);
  224. }
  225. #endif /* #if defined(ARM_MATH_NEON) */
  226. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  227. /**
  228. @} end of variance group
  229. */