arm_rms_f32.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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: 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 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. */
  51. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  52. void arm_rms_f32(
  53. const float32_t * pSrc,
  54. uint32_t blockSize,
  55. float32_t * pResult)
  56. {
  57. float32_t pow = 0.0f;
  58. arm_power_f32(pSrc, blockSize, &pow);
  59. /* Compute Rms and store the result in the destination */
  60. arm_sqrt_f32(pow / (float32_t) blockSize, pResult);
  61. }
  62. #else
  63. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  64. void arm_rms_f32(
  65. const float32_t * pSrc,
  66. uint32_t blockSize,
  67. float32_t * pResult)
  68. {
  69. float32_t sum = 0.0f; /* accumulator */
  70. float32_t in; /* Temporary variable to store input value */
  71. uint32_t blkCnt; /* loop counter */
  72. float32x4_t sumV = vdupq_n_f32(0.0f); /* Temporary result storage */
  73. float32x2_t sumV2;
  74. float32x4_t inV;
  75. blkCnt = blockSize >> 2U;
  76. /* Compute 4 outputs at a time.
  77. ** a second loop below computes the remaining 1 to 3 samples. */
  78. while (blkCnt > 0U)
  79. {
  80. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  81. /* Compute Power and then store the result in a temporary variable, sum. */
  82. inV = vld1q_f32(pSrc);
  83. sumV = vmlaq_f32(sumV, inV, inV);
  84. pSrc += 4;
  85. /* Decrement the loop counter */
  86. blkCnt--;
  87. }
  88. sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
  89. sum = vget_lane_f32(sumV2, 0) + vget_lane_f32(sumV2, 1);
  90. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  91. ** No loop unrolling is used. */
  92. blkCnt = blockSize % 0x4U;
  93. while (blkCnt > 0U)
  94. {
  95. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  96. /* compute power and then store the result in a temporary variable, sum. */
  97. in = *pSrc++;
  98. sum += in * in;
  99. /* Decrement the loop counter */
  100. blkCnt--;
  101. }
  102. /* Compute Rms and store the result in the destination */
  103. arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
  104. }
  105. #else
  106. void arm_rms_f32(
  107. const float32_t * pSrc,
  108. uint32_t blockSize,
  109. float32_t * pResult)
  110. {
  111. uint32_t blkCnt; /* Loop counter */
  112. float32_t sum = 0.0f; /* Temporary result storage */
  113. float32_t in; /* Temporary variable to store input value */
  114. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  115. /* Loop unrolling: Compute 4 outputs at a time */
  116. blkCnt = blockSize >> 2U;
  117. while (blkCnt > 0U)
  118. {
  119. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  120. in = *pSrc++;
  121. /* Compute sum of squares and store result in a temporary variable, sum. */
  122. sum += in * in;
  123. in = *pSrc++;
  124. sum += in * in;
  125. in = *pSrc++;
  126. sum += in * in;
  127. in = *pSrc++;
  128. sum += in * in;
  129. /* Decrement loop counter */
  130. blkCnt--;
  131. }
  132. /* Loop unrolling: Compute remaining outputs */
  133. blkCnt = blockSize % 0x4U;
  134. #else
  135. /* Initialize blkCnt with number of samples */
  136. blkCnt = blockSize;
  137. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  138. while (blkCnt > 0U)
  139. {
  140. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  141. in = *pSrc++;
  142. /* Compute sum of squares and store result in a temporary variable. */
  143. sum += ( in * in);
  144. /* Decrement loop counter */
  145. blkCnt--;
  146. }
  147. /* Compute Rms and store result in destination */
  148. arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
  149. }
  150. #endif /* #if defined(ARM_MATH_NEON) */
  151. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  152. /**
  153. @} end of RMS group
  154. */