arm_std_f32.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_std_f32.c
  4. * Description: Standard deviation 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 STD Standard deviation
  34. Calculates the standard deviation of the elements in the input vector.
  35. The underlying algorithm is used:
  36. <pre>
  37. Result = sqrt((sumOfSquares - sum<sup>2</sup> / blockSize) / (blockSize - 1))
  38. sumOfSquares = pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]
  39. sum = pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1]
  40. </pre>
  41. There are separate functions for floating point, Q31, and Q15 data types.
  42. */
  43. /**
  44. @addtogroup STD
  45. @{
  46. */
  47. /**
  48. @brief Standard deviation of the elements of a floating-point vector.
  49. @param[in] pSrc points to the input vector
  50. @param[in] blockSize number of samples in input vector
  51. @param[out] pResult standard deviation value returned here
  52. @return none
  53. */
  54. #if defined(ARM_MATH_NEON_EXPERIMENTAL)
  55. void arm_std_f32(
  56. const float32_t * pSrc,
  57. uint32_t blockSize,
  58. float32_t * pResult)
  59. {
  60. float32_t var;
  61. arm_var_f32(pSrc,blockSize,&var);
  62. arm_sqrt_f32(var, pResult);
  63. }
  64. #else
  65. void arm_std_f32(
  66. const float32_t * pSrc,
  67. uint32_t blockSize,
  68. float32_t * pResult)
  69. {
  70. uint32_t blkCnt; /* Loop counter */
  71. float32_t sum = 0.0f; /* Temporary result storage */
  72. float32_t sumOfSquares = 0.0f; /* Sum of squares */
  73. float32_t in; /* Temporary variable to store input value */
  74. #ifndef ARM_MATH_CM0_FAMILY
  75. float32_t meanOfSquares, mean, squareOfMean; /* Temporary variables */
  76. #else
  77. float32_t squareOfSum; /* Square of Sum */
  78. float32_t var; /* Temporary varaince storage */
  79. #endif
  80. if (blockSize <= 1U)
  81. {
  82. *pResult = 0;
  83. return;
  84. }
  85. #if defined (ARM_MATH_LOOPUNROLL)
  86. /* Loop unrolling: Compute 4 outputs at a time */
  87. blkCnt = blockSize >> 2U;
  88. while (blkCnt > 0U)
  89. {
  90. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  91. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  92. in = *pSrc++;
  93. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  94. sumOfSquares += in * in;
  95. /* Compute sum and store result in a temporary variable, sum. */
  96. sum += in;
  97. in = *pSrc++;
  98. sumOfSquares += in * in;
  99. sum += in;
  100. in = *pSrc++;
  101. sumOfSquares += in * in;
  102. sum += in;
  103. in = *pSrc++;
  104. sumOfSquares += in * in;
  105. sum += in;
  106. /* Decrement loop counter */
  107. blkCnt--;
  108. }
  109. /* Loop unrolling: Compute remaining outputs */
  110. blkCnt = blockSize % 0x4U;
  111. #else
  112. /* Initialize blkCnt with number of samples */
  113. blkCnt = blockSize;
  114. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  115. while (blkCnt > 0U)
  116. {
  117. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  118. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  119. in = *pSrc++;
  120. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  121. sumOfSquares += ( in * in);
  122. /* Compute sum and store result in a temporary variable, sum. */
  123. sum += in;
  124. /* Decrement loop counter */
  125. blkCnt--;
  126. }
  127. #ifndef ARM_MATH_CM0_FAMILY
  128. /* Compute Mean of squares and store result in a temporary variable, meanOfSquares. */
  129. meanOfSquares = sumOfSquares / ((float32_t) blockSize - 1.0f);
  130. /* Compute mean of all input values */
  131. mean = sum / (float32_t) blockSize;
  132. /* Compute square of mean */
  133. squareOfMean = (mean * mean) * (((float32_t) blockSize) /
  134. ((float32_t) blockSize - 1.0f));
  135. /* Compute standard deviation and store result to destination */
  136. arm_sqrt_f32((meanOfSquares - squareOfMean), pResult);
  137. #else
  138. /* Run the below code for Cortex-M0 */
  139. /* Compute square of sum */
  140. squareOfSum = ((sum * sum) / (float32_t) blockSize);
  141. /* Compute variance */
  142. var = ((sumOfSquares - squareOfSum) / (float32_t) (blockSize - 1.0f));
  143. /* Compute standard deviation and store result in destination */
  144. arm_sqrt_f32(var, pResult);
  145. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  146. }
  147. #endif /* #if defined(ARM_MATH_NEON) */
  148. /**
  149. @} end of STD group
  150. */