arm_var_f32.c 7.2 KB

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