arm_var_q31.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_var_q31.c
  4. * Description: Variance of an array of Q31 type
  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. * @ingroup groupStats
  31. */
  32. /**
  33. * @addtogroup variance
  34. * @{
  35. */
  36. /**
  37. * @brief Variance of the elements of a Q31 vector.
  38. * @param[in] *pSrc points to the input vector
  39. * @param[in] blockSize length of the input vector
  40. * @param[out] *pResult variance value returned here
  41. * @return none.
  42. * @details
  43. * <b>Scaling and Overflow Behavior:</b>
  44. *
  45. *\par
  46. * The function is implemented using an internal 64-bit accumulator.
  47. * The input is represented in 1.31 format, which is then downshifted by 8 bits
  48. * which yields 1.23, and intermediate multiplication yields a 2.46 format.
  49. * The accumulator maintains full precision of the intermediate multiplication results,
  50. * but provides only a 16 guard bits.
  51. * There is no saturation on intermediate additions.
  52. * If the accumulator overflows it wraps around and distorts the result.
  53. * In order to avoid overflows completely the input signal must be scaled down by
  54. * log2(blockSize)-8 bits, as a total of blockSize additions are performed internally.
  55. * After division, internal variables should be Q18.46
  56. * Finally, the 18.46 accumulator is right shifted by 15 bits to yield a 1.31 format value.
  57. *
  58. */
  59. void arm_var_q31(
  60. q31_t * pSrc,
  61. uint32_t blockSize,
  62. q31_t * pResult)
  63. {
  64. q63_t sum = 0; /* Accumulator */
  65. q63_t meanOfSquares, squareOfMean; /* square of mean and mean of square */
  66. q31_t in; /* input value */
  67. uint32_t blkCnt; /* loop counter */
  68. q63_t sumOfSquares = 0; /* Accumulator */
  69. if (blockSize == 1U)
  70. {
  71. *pResult = 0;
  72. return;
  73. }
  74. #if defined (ARM_MATH_DSP)
  75. /* Run the below code for Cortex-M4 and Cortex-M3 */
  76. /*loop Unrolling */
  77. blkCnt = blockSize >> 2U;
  78. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  79. ** a second loop below computes the remaining 1 to 3 samples. */
  80. while (blkCnt > 0U)
  81. {
  82. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  83. /* Compute Sum of squares of the input samples
  84. * and then store the result in a temporary variable, sum. */
  85. in = *pSrc++ >> 8U;
  86. sum += in;
  87. sumOfSquares += ((q63_t) (in) * (in));
  88. in = *pSrc++ >> 8U;
  89. sum += in;
  90. sumOfSquares += ((q63_t) (in) * (in));
  91. in = *pSrc++ >> 8U;
  92. sum += in;
  93. sumOfSquares += ((q63_t) (in) * (in));
  94. in = *pSrc++ >> 8U;
  95. sum += in;
  96. sumOfSquares += ((q63_t) (in) * (in));
  97. /* Decrement the loop counter */
  98. blkCnt--;
  99. }
  100. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  101. ** No loop unrolling is used. */
  102. blkCnt = blockSize % 0x4U;
  103. while (blkCnt > 0U)
  104. {
  105. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  106. /* Compute Sum of squares of the input samples
  107. * and then store the result in a temporary variable, sum. */
  108. in = *pSrc++ >> 8U;
  109. sum += in;
  110. sumOfSquares += ((q63_t) (in) * (in));
  111. /* Decrement the loop counter */
  112. blkCnt--;
  113. }
  114. /* Compute Mean of squares of the input samples
  115. * and then store the result in a temporary variable, meanOfSquares. */
  116. meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1U);
  117. #else
  118. /* Run the below code for Cortex-M0 */
  119. /* Loop over blockSize number of values */
  120. blkCnt = blockSize;
  121. while (blkCnt > 0U)
  122. {
  123. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  124. /* Compute Sum of squares of the input samples
  125. * and then store the result in a temporary variable, sumOfSquares. */
  126. in = *pSrc++ >> 8U;
  127. sumOfSquares += ((q63_t) (in) * (in));
  128. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  129. /* Compute sum of all input values and then store the result in a temporary variable, sum. */
  130. sum += in;
  131. /* Decrement the loop counter */
  132. blkCnt--;
  133. }
  134. /* Compute Mean of squares of the input samples
  135. * and then store the result in a temporary variable, meanOfSquares. */
  136. meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1U);
  137. #endif /* #if defined (ARM_MATH_DSP) */
  138. /* Compute square of mean */
  139. squareOfMean = sum * sum / (q63_t)(blockSize * (blockSize - 1U));
  140. /* Compute standard deviation and then store the result to the destination */
  141. *pResult = (meanOfSquares - squareOfMean) >> 15U;
  142. }
  143. /**
  144. * @} end of variance group
  145. */