arm_rms_f32.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rms_f32.c
  4. * Description: Root mean square value 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 RMS Root mean square (RMS)
  34. Calculates the Root Mean Square of the elements in the input vector.
  35. The underlying algorithm is used:
  36. <pre>
  37. Result = sqrt(((pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]) / blockSize));
  38. </pre>
  39. There are separate functions for floating point, Q31, and Q15 data types.
  40. */
  41. /**
  42. @addtogroup RMS
  43. @{
  44. */
  45. /**
  46. @brief Root Mean Square of the elements of a floating-point vector.
  47. @param[in] pSrc points to the input vector
  48. @param[in] blockSize number of samples in input vector
  49. @param[out] pResult root mean square value returned here
  50. @return none
  51. */
  52. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  53. void arm_rms_f32(
  54. const float32_t * pSrc,
  55. uint32_t blockSize,
  56. float32_t * pResult)
  57. {
  58. float32_t pow = 0.0f;
  59. arm_power_f32(pSrc, blockSize, &pow);
  60. /* Compute Rms and store the result in the destination */
  61. arm_sqrt_f32(pow / (float32_t) blockSize, pResult);
  62. }
  63. #else
  64. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  65. void arm_rms_f32(
  66. const float32_t * pSrc,
  67. uint32_t blockSize,
  68. float32_t * pResult)
  69. {
  70. float32_t sum = 0.0f; /* accumulator */
  71. float32_t in; /* Temporary variable to store input value */
  72. uint32_t blkCnt; /* loop counter */
  73. float32x4_t sumV = vdupq_n_f32(0.0f); /* Temporary result storage */
  74. float32x2_t sumV2;
  75. float32x4_t inV;
  76. blkCnt = blockSize >> 2U;
  77. /* Compute 4 outputs at a time.
  78. ** a second loop below computes the remaining 1 to 3 samples. */
  79. while (blkCnt > 0U)
  80. {
  81. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  82. /* Compute Power and then store the result in a temporary variable, sum. */
  83. inV = vld1q_f32(pSrc);
  84. sumV = vmlaq_f32(sumV, inV, inV);
  85. pSrc += 4;
  86. /* Decrement the loop counter */
  87. blkCnt--;
  88. }
  89. sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
  90. sum = vget_lane_f32(sumV2, 0) + vget_lane_f32(sumV2, 1);
  91. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  92. ** No loop unrolling is used. */
  93. blkCnt = blockSize % 0x4U;
  94. while (blkCnt > 0U)
  95. {
  96. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  97. /* compute power and then store the result in a temporary variable, sum. */
  98. in = *pSrc++;
  99. sum += in * in;
  100. /* Decrement the loop counter */
  101. blkCnt--;
  102. }
  103. /* Compute Rms and store the result in the destination */
  104. arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
  105. }
  106. #else
  107. void arm_rms_f32(
  108. const float32_t * pSrc,
  109. uint32_t blockSize,
  110. float32_t * pResult)
  111. {
  112. uint32_t blkCnt; /* Loop counter */
  113. float32_t sum = 0.0f; /* Temporary result storage */
  114. float32_t in; /* Temporary variable to store input value */
  115. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  116. /* Loop unrolling: Compute 4 outputs at a time */
  117. blkCnt = blockSize >> 2U;
  118. while (blkCnt > 0U)
  119. {
  120. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  121. in = *pSrc++;
  122. /* Compute sum of squares and store result in a temporary variable, sum. */
  123. sum += in * in;
  124. in = *pSrc++;
  125. sum += in * in;
  126. in = *pSrc++;
  127. sum += in * in;
  128. in = *pSrc++;
  129. sum += in * in;
  130. /* Decrement loop counter */
  131. blkCnt--;
  132. }
  133. /* Loop unrolling: Compute remaining outputs */
  134. blkCnt = blockSize % 0x4U;
  135. #else
  136. /* Initialize blkCnt with number of samples */
  137. blkCnt = blockSize;
  138. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  139. while (blkCnt > 0U)
  140. {
  141. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  142. in = *pSrc++;
  143. /* Compute sum of squares and store result in a temporary variable. */
  144. sum += ( in * in);
  145. /* Decrement loop counter */
  146. blkCnt--;
  147. }
  148. /* Compute Rms and store result in destination */
  149. arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
  150. }
  151. #endif /* #if defined(ARM_MATH_NEON) */
  152. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  153. /**
  154. @} end of RMS group
  155. */