arm_rms_q31.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rms_q31.c
  4. * Description: Root Mean Square of the elements of a Q31 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 Q31 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 an internal 64-bit accumulator.
  44. The input is represented in 1.31 format, and intermediate multiplication
  45. yields a 2.62 format.
  46. The accumulator maintains full precision of the intermediate multiplication results,
  47. but provides only a single guard bit.
  48. There is no saturation on intermediate additions.
  49. If the accumulator overflows, it wraps around and distorts the result.
  50. In order to avoid overflows completely, the input signal must be scaled down by
  51. log2(blockSize) bits, as a total of blockSize additions are performed internally.
  52. Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value.
  53. */
  54. #if defined(ARM_MATH_MVEI)
  55. void arm_rms_q31(
  56. const q31_t * pSrc,
  57. uint32_t blockSize,
  58. q31_t * pResult)
  59. {
  60. q63_t pow = 0.0f;
  61. q31_t normalizedPower;
  62. arm_power_q31(pSrc, blockSize, &pow);
  63. normalizedPower=clip_q63_to_q31((pow / (q63_t) blockSize) >> 17);
  64. arm_sqrt_q31(normalizedPower, pResult);
  65. }
  66. #else
  67. void arm_rms_q31(
  68. const q31_t * pSrc,
  69. uint32_t blockSize,
  70. q31_t * pResult)
  71. {
  72. uint32_t blkCnt; /* Loop counter */
  73. uint64_t sum = 0; /* Temporary result storage (can get never negative. changed type from q63 to uint64 */
  74. q31_t in; /* Temporary variable to store input value */
  75. #if defined (ARM_MATH_LOOPUNROLL)
  76. /* Loop unrolling: Compute 4 outputs at a time */
  77. blkCnt = blockSize >> 2U;
  78. while (blkCnt > 0U)
  79. {
  80. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  81. in = *pSrc++;
  82. /* Compute sum of squares and store result in a temporary variable, sum. */
  83. sum += ((q63_t) in * in);
  84. in = *pSrc++;
  85. sum += ((q63_t) in * in);
  86. in = *pSrc++;
  87. sum += ((q63_t) in * in);
  88. in = *pSrc++;
  89. sum += ((q63_t) in * in);
  90. /* Decrement loop counter */
  91. blkCnt--;
  92. }
  93. /* Loop unrolling: Compute remaining outputs */
  94. blkCnt = blockSize % 0x4U;
  95. #else
  96. /* Initialize blkCnt with number of samples */
  97. blkCnt = blockSize;
  98. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  99. while (blkCnt > 0U)
  100. {
  101. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  102. in = *pSrc++;
  103. /* Compute sum of squares and store result in a temporary variable. */
  104. sum += ((q63_t) in * in);
  105. /* Decrement loop counter */
  106. blkCnt--;
  107. }
  108. /* Convert data in 2.62 to 1.31 by 31 right shifts and saturate */
  109. /* Compute Rms and store result in destination vector */
  110. arm_sqrt_q31(clip_q63_to_q31((sum / (q63_t) blockSize) >> 31), pResult);
  111. }
  112. #endif /* defined(ARM_MATH_MVEI) */
  113. /**
  114. @} end of RMS group
  115. */