arm_std_f32.c 5.6 KB

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