arm_rms_q15.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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: 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. * @addtogroup RMS
  31. * @{
  32. */
  33. /**
  34. * @brief Root Mean Square of the elements of a Q15 vector.
  35. * @param[in] *pSrc points to the input vector
  36. * @param[in] blockSize length of the input vector
  37. * @param[out] *pResult rms value returned here
  38. * @return none.
  39. *
  40. * @details
  41. * <b>Scaling and Overflow Behavior:</b>
  42. *
  43. * \par
  44. * The function is implemented using a 64-bit internal accumulator.
  45. * The input is represented in 1.15 format.
  46. * Intermediate multiplication yields a 2.30 format, and this
  47. * result is added without saturation to a 64-bit accumulator in 34.30 format.
  48. * With 33 guard bits in the accumulator, there is no risk of overflow, and the
  49. * full precision of the intermediate multiplication is preserved.
  50. * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
  51. * 15 bits, and then saturated to yield a result in 1.15 format.
  52. *
  53. */
  54. void arm_rms_q15(
  55. q15_t * pSrc,
  56. uint32_t blockSize,
  57. q15_t * pResult)
  58. {
  59. q63_t sum = 0; /* accumulator */
  60. #if defined (ARM_MATH_DSP)
  61. /* Run the below code for Cortex-M4 and Cortex-M3 */
  62. q31_t in; /* temporary variable to store the input value */
  63. q15_t in1; /* temporary variable to store the input value */
  64. uint32_t blkCnt; /* loop counter */
  65. /* loop Unrolling */
  66. blkCnt = blockSize >> 2U;
  67. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  68. ** a second loop below computes the remaining 1 to 3 samples. */
  69. while (blkCnt > 0U)
  70. {
  71. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  72. /* Compute sum of the squares and then store the results in a temporary variable, sum */
  73. in = *__SIMD32(pSrc)++;
  74. sum = __SMLALD(in, in, sum);
  75. in = *__SIMD32(pSrc)++;
  76. sum = __SMLALD(in, in, sum);
  77. /* Decrement the loop counter */
  78. blkCnt--;
  79. }
  80. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  81. ** No loop unrolling is used. */
  82. blkCnt = blockSize % 0x4U;
  83. while (blkCnt > 0U)
  84. {
  85. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  86. /* Compute sum of the squares and then store the results in a temporary variable, sum */
  87. in1 = *pSrc++;
  88. sum = __SMLALD(in1, in1, sum);
  89. /* Decrement the loop counter */
  90. blkCnt--;
  91. }
  92. /* Truncating and saturating the accumulator to 1.15 format */
  93. /* Store the result in the destination */
  94. arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult);
  95. #else
  96. /* Run the below code for Cortex-M0 */
  97. q15_t in; /* temporary variable to store the input value */
  98. uint32_t blkCnt; /* loop counter */
  99. /* Loop over blockSize number of values */
  100. blkCnt = blockSize;
  101. while (blkCnt > 0U)
  102. {
  103. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  104. /* Compute sum of the squares and then store the results in a temporary variable, sum */
  105. in = *pSrc++;
  106. sum += ((q31_t) in * in);
  107. /* Decrement the loop counter */
  108. blkCnt--;
  109. }
  110. /* Truncating and saturating the accumulator to 1.15 format */
  111. /* Store the result in the destination */
  112. arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult);
  113. #endif /* #if defined (ARM_MATH_DSP) */
  114. }
  115. /**
  116. * @} end of RMS group
  117. */