arm_rms_q31.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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: 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 Q31 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 an internal 64-bit accumulator.
  45. * The input is represented in 1.31 format, and intermediate multiplication
  46. * yields a 2.62 format.
  47. * The accumulator maintains full precision of the intermediate multiplication results,
  48. * but provides only a single guard bit.
  49. * There is no saturation on intermediate additions.
  50. * If the accumulator overflows, it wraps around and distorts the result.
  51. * In order to avoid overflows completely, the input signal must be scaled down by
  52. * log2(blockSize) bits, as a total of blockSize additions are performed internally.
  53. * Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value.
  54. *
  55. */
  56. void arm_rms_q31(
  57. q31_t * pSrc,
  58. uint32_t blockSize,
  59. q31_t * pResult)
  60. {
  61. q63_t sum = 0; /* accumulator */
  62. q31_t in; /* Temporary variable to store the input */
  63. uint32_t blkCnt; /* loop counter */
  64. #if defined (ARM_MATH_DSP)
  65. /* Run the below code for Cortex-M4 and Cortex-M3 */
  66. q31_t in1, in2, in3, in4; /* Temporary input variables */
  67. /*loop Unrolling */
  68. blkCnt = blockSize >> 2U;
  69. /* First part of the processing with loop unrolling. Compute 8 outputs at a time.
  70. ** a second loop below computes the remaining 1 to 7 samples. */
  71. while (blkCnt > 0U)
  72. {
  73. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  74. /* Compute sum of the squares and then store the result in a temporary variable, sum */
  75. /* read two samples from source buffer */
  76. in1 = pSrc[0];
  77. in2 = pSrc[1];
  78. /* calculate power and accumulate to accumulator */
  79. sum += (q63_t) in1 *in1;
  80. sum += (q63_t) in2 *in2;
  81. /* read two samples from source buffer */
  82. in3 = pSrc[2];
  83. in4 = pSrc[3];
  84. /* calculate power and accumulate to accumulator */
  85. sum += (q63_t) in3 *in3;
  86. sum += (q63_t) in4 *in4;
  87. /* update source buffer to process next samples */
  88. pSrc += 4U;
  89. /* Decrement the loop counter */
  90. blkCnt--;
  91. }
  92. /* If the blockSize is not a multiple of 8, compute any remaining output samples here.
  93. ** No loop unrolling is used. */
  94. blkCnt = blockSize % 0x4U;
  95. #else
  96. /* Run the below code for Cortex-M0 */
  97. blkCnt = blockSize;
  98. #endif /* #if defined (ARM_MATH_DSP) */
  99. while (blkCnt > 0U)
  100. {
  101. /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
  102. /* Compute sum of the squares and then store the results in a temporary variable, sum */
  103. in = *pSrc++;
  104. sum += (q63_t) in *in;
  105. /* Decrement the 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 the result in the destination vector */
  110. arm_sqrt_q31(clip_q63_to_q31((sum / (q63_t) blockSize) >> 31), pResult);
  111. }
  112. /**
  113. * @} end of RMS group
  114. */