arm_var_q31.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_var_q31.c
  4. * Description: Variance of an array of Q31 type
  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 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 number of samples in input vector
  40. @param[out] pResult variance 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, which is then downshifted by 8 bits
  45. which yields 1.23, and intermediate multiplication yields a 2.46 format.
  46. The accumulator maintains full precision of the intermediate multiplication results,
  47. and as a consequence has only 16 guard bits.
  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)-8 bits, as a total of blockSize additions are performed internally.
  52. After division, internal variables should be Q18.46
  53. Finally, the 18.46 accumulator is right shifted by 15 bits to yield a 1.31 format value.
  54. */
  55. #if defined(ARM_MATH_MVEI)
  56. void arm_var_q31(
  57. const q31_t * pSrc,
  58. uint32_t blockSize,
  59. q31_t * pResult)
  60. {
  61. uint32_t blkCnt; /* loop counters */
  62. q31x4_t vecSrc;
  63. q63_t sumOfSquares = 0LL;
  64. q63_t meanOfSquares, squareOfMean; /* square of mean and mean of square */
  65. q63_t sum = 0LL;
  66. q31_t in;
  67. if (blockSize <= 1U) {
  68. *pResult = 0;
  69. return;
  70. }
  71. /* Compute 4 outputs at a time */
  72. blkCnt = blockSize >> 2U;
  73. while (blkCnt > 0U)
  74. {
  75. vecSrc = vldrwq_s32(pSrc);
  76. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  77. /* Compute Sum of squares of the input samples
  78. * and then store the result in a temporary variable, sumOfSquares. */
  79. /* downscale */
  80. vecSrc = vshrq(vecSrc, 8);
  81. sumOfSquares = vmlaldavaq(sumOfSquares, vecSrc, vecSrc);
  82. sum = vaddlvaq(sum, vecSrc);
  83. blkCnt --;
  84. pSrc += 4;
  85. }
  86. /*
  87. * tail
  88. */
  89. blkCnt = blockSize & 0x3;
  90. while (blkCnt > 0U)
  91. {
  92. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  93. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  94. in = *pSrc++ >> 8U;
  95. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  96. sumOfSquares += ((q63_t) (in) * (in));
  97. /* Compute sum and store result in a temporary variable, sum. */
  98. sum += in;
  99. /* Decrement loop counter */
  100. blkCnt--;
  101. }
  102. /* Compute Mean of squares of the input samples
  103. * and then store the result in a temporary variable, meanOfSquares. */
  104. meanOfSquares = sumOfSquares / (q63_t) (blockSize - 1U);
  105. /* Compute square of mean */
  106. squareOfMean = sum * sum / (q63_t) (blockSize * (blockSize - 1U));
  107. /* Compute standard deviation and then store the result to the destination */
  108. *pResult = asrl(meanOfSquares - squareOfMean, 15U);
  109. }
  110. #else
  111. void arm_var_q31(
  112. const q31_t * pSrc,
  113. uint32_t blockSize,
  114. q31_t * pResult)
  115. {
  116. uint32_t blkCnt; /* Loop counter */
  117. q63_t sum = 0; /* Temporary result storage */
  118. q63_t meanOfSquares, squareOfMean; /* Square of mean and mean of square */
  119. q63_t sumOfSquares = 0; /* Sum of squares */
  120. q31_t in; /* Temporary variable to store input value */
  121. if (blockSize <= 1U)
  122. {
  123. *pResult = 0;
  124. return;
  125. }
  126. #if defined (ARM_MATH_LOOPUNROLL)
  127. /* Loop unrolling: Compute 4 outputs at a time */
  128. blkCnt = blockSize >> 2U;
  129. while (blkCnt > 0U)
  130. {
  131. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  132. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  133. in = *pSrc++ >> 8U;
  134. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  135. sumOfSquares += ((q63_t) (in) * (in));
  136. /* Compute sum and store result in a temporary variable, sum. */
  137. sum += in;
  138. in = *pSrc++ >> 8U;
  139. sumOfSquares += ((q63_t) (in) * (in));
  140. sum += in;
  141. in = *pSrc++ >> 8U;
  142. sumOfSquares += ((q63_t) (in) * (in));
  143. sum += in;
  144. in = *pSrc++ >> 8U;
  145. sumOfSquares += ((q63_t) (in) * (in));
  146. sum += in;
  147. /* Decrement loop counter */
  148. blkCnt--;
  149. }
  150. /* Loop unrolling: Compute remaining outputs */
  151. blkCnt = blockSize % 0x4U;
  152. #else
  153. /* Initialize blkCnt with number of samples */
  154. blkCnt = blockSize;
  155. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  156. while (blkCnt > 0U)
  157. {
  158. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  159. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  160. in = *pSrc++ >> 8U;
  161. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  162. sumOfSquares += ((q63_t) (in) * (in));
  163. /* Compute sum and store result in a temporary variable, sum. */
  164. sum += in;
  165. /* Decrement loop counter */
  166. blkCnt--;
  167. }
  168. /* Compute Mean of squares and store result in a temporary variable, meanOfSquares. */
  169. meanOfSquares = (sumOfSquares / (q63_t)(blockSize - 1U));
  170. /* Compute square of mean */
  171. squareOfMean = ( sum * sum / (q63_t)(blockSize * (blockSize - 1U)));
  172. /* Compute variance and store result in destination */
  173. *pResult = (meanOfSquares - squareOfMean) >> 15U;
  174. }
  175. #endif
  176. /**
  177. @} end of variance group
  178. */