arm_rms_q15.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rms_q15.c
  4. * Description: Root Mean Square of the elements of a Q15 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. @addtogroup RMS
  34. @{
  35. */
  36. /**
  37. @brief Root Mean Square of the elements of a Q15 vector.
  38. @param[in] pSrc points to the input vector
  39. @param[in] blockSize number of samples in input vector
  40. @param[out] pResult root mean square value returned here
  41. @return none
  42. @par Scaling and Overflow Behavior
  43. The function is implemented using a 64-bit internal accumulator.
  44. The input is represented in 1.15 format.
  45. Intermediate multiplication yields a 2.30 format, and this
  46. result is added without saturation to a 64-bit accumulator in 34.30 format.
  47. With 33 guard bits in the accumulator, there is no risk of overflow, and the
  48. full precision of the intermediate multiplication is preserved.
  49. Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
  50. 15 bits, and then saturated to yield a result in 1.15 format.
  51. */
  52. #if defined(ARM_MATH_MVEI)
  53. void arm_rms_q15(
  54. const q15_t * pSrc,
  55. uint32_t blockSize,
  56. q15_t * pResult)
  57. {
  58. q63_t pow = 0.0f;
  59. q15_t normalizedPower;
  60. arm_power_q15(pSrc, blockSize, &pow);
  61. normalizedPower=__SSAT((pow / (q63_t) blockSize) >> 15,16);
  62. arm_sqrt_q15(normalizedPower, pResult);
  63. }
  64. #else
  65. void arm_rms_q15(
  66. const q15_t * pSrc,
  67. uint32_t blockSize,
  68. q15_t * pResult)
  69. {
  70. uint32_t blkCnt; /* Loop counter */
  71. q63_t sum = 0; /* Temporary result storage */
  72. q15_t in; /* Temporary variable to store input value */
  73. #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
  74. q31_t in32; /* Temporary variable to store input value */
  75. #endif
  76. #if defined (ARM_MATH_LOOPUNROLL)
  77. /* Loop unrolling: Compute 4 outputs at a time */
  78. blkCnt = blockSize >> 2U;
  79. while (blkCnt > 0U)
  80. {
  81. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  82. /* Compute sum of squares and store result in a temporary variable. */
  83. #if defined (ARM_MATH_DSP)
  84. in32 = read_q15x2_ia ((q15_t **) &pSrc);
  85. sum = __SMLALD(in32, in32, sum);
  86. in32 = read_q15x2_ia ((q15_t **) &pSrc);
  87. sum = __SMLALD(in32, in32, sum);
  88. #else
  89. in = *pSrc++;
  90. sum += ((q31_t) in * in);
  91. in = *pSrc++;
  92. sum += ((q31_t) in * in);
  93. in = *pSrc++;
  94. sum += ((q31_t) in * in);
  95. in = *pSrc++;
  96. sum += ((q31_t) in * in);
  97. #endif /* #if defined (ARM_MATH_DSP) */
  98. /* Decrement loop counter */
  99. blkCnt--;
  100. }
  101. /* Loop unrolling: Compute remaining outputs */
  102. blkCnt = blockSize % 0x4U;
  103. #else
  104. /* Initialize blkCnt with number of samples */
  105. blkCnt = blockSize;
  106. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  107. while (blkCnt > 0U)
  108. {
  109. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  110. in = *pSrc++;
  111. /* Compute sum of squares and store result in a temporary variable. */
  112. sum += ((q31_t) in * in);
  113. /* Decrement loop counter */
  114. blkCnt--;
  115. }
  116. /* Truncating and saturating the accumulator to 1.15 format */
  117. /* Store result in destination */
  118. arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult);
  119. }
  120. #endif /* defined(ARM_MATH_MVEI) */
  121. /**
  122. @} end of RMS group
  123. */