arm_rms_f16.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rms_f16.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_f16.h"
  29. #if defined(ARM_FLOAT16_SUPPORTED)
  30. /**
  31. @ingroup groupStats
  32. */
  33. /**
  34. @defgroup RMS Root mean square (RMS)
  35. Calculates the Root Mean Square of the elements in the input vector.
  36. The underlying algorithm is used:
  37. <pre>
  38. Result = sqrt(((pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]) / blockSize));
  39. </pre>
  40. There are separate functions for floating point, Q31, and Q15 data types.
  41. */
  42. /**
  43. @addtogroup RMS
  44. @{
  45. */
  46. /**
  47. @brief Root Mean Square 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 root mean square value returned here
  51. @return none
  52. */
  53. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  54. void arm_rms_f16(
  55. const float16_t * pSrc,
  56. uint32_t blockSize,
  57. float16_t * pResult)
  58. {
  59. float16_t pow = 0.0f;
  60. arm_power_f16(pSrc, blockSize, &pow);
  61. /* Compute Rms and store the result in the destination */
  62. arm_sqrt_f16((_Float16)pow / (_Float16) blockSize, pResult);
  63. }
  64. #else
  65. void arm_rms_f16(
  66. const float16_t * pSrc,
  67. uint32_t blockSize,
  68. float16_t * pResult)
  69. {
  70. uint32_t blkCnt; /* Loop counter */
  71. _Float16 sum = 0.0f16; /* Temporary result storage */
  72. _Float16 in; /* Temporary variable to store input value */
  73. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  74. /* Loop unrolling: Compute 4 outputs at a time */
  75. blkCnt = blockSize >> 2U;
  76. while (blkCnt > 0U)
  77. {
  78. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  79. in = *pSrc++;
  80. /* Compute sum of squares and store result in a temporary variable, sum. */
  81. sum += in * in;
  82. in = *pSrc++;
  83. sum += in * in;
  84. in = *pSrc++;
  85. sum += in * in;
  86. in = *pSrc++;
  87. sum += in * in;
  88. /* Decrement loop counter */
  89. blkCnt--;
  90. }
  91. /* Loop unrolling: Compute remaining outputs */
  92. blkCnt = blockSize % 0x4U;
  93. #else
  94. /* Initialize blkCnt with number of samples */
  95. blkCnt = blockSize;
  96. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  97. while (blkCnt > 0U)
  98. {
  99. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  100. in = *pSrc++;
  101. /* Compute sum of squares and store result in a temporary variable. */
  102. sum += ( in * in);
  103. /* Decrement loop counter */
  104. blkCnt--;
  105. }
  106. /* Compute Rms and store result in destination */
  107. arm_sqrt_f16((_Float16)sum / (_Float16) blockSize, pResult);
  108. }
  109. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  110. /**
  111. @} end of RMS group
  112. */
  113. #endif /* #if defined(ARM_FLOAT16_SUPPORTED) */