arm_var_q15.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_var_q15.c
  4. * Description: Variance of an array of Q15 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 Q15 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 a 64-bit internal accumulator.
  44. The input is represented in 1.15 format.
  45. Intermediate multiplication yields a 2.30 format, and this
  46. result is added without saturation to a 64-bit accumulator in 34.30 format.
  47. With 33 guard bits in the accumulator, there is no risk of overflow, and the
  48. full precision of the intermediate multiplication is preserved.
  49. Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
  50. 15 bits, and then saturated to yield a result in 1.15 format.
  51. */
  52. #if defined(ARM_MATH_MVEI)
  53. void arm_var_q15(
  54. const q15_t * pSrc,
  55. uint32_t blockSize,
  56. q15_t * pResult)
  57. {
  58. uint32_t blkCnt; /* loop counters */
  59. q15x8_t vecSrc;
  60. q63_t sumOfSquares = 0LL;
  61. q63_t meanOfSquares, squareOfMean; /* square of mean and mean of square */
  62. q63_t sum = 0LL;
  63. q15_t in;
  64. if (blockSize <= 1U) {
  65. *pResult = 0;
  66. return;
  67. }
  68. blkCnt = blockSize >> 3;
  69. while (blkCnt > 0U)
  70. {
  71. vecSrc = vldrhq_s16(pSrc);
  72. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  73. /* Compute Sum of squares of the input samples
  74. * and then store the result in a temporary variable, sumOfSquares. */
  75. sumOfSquares = vmlaldavaq(sumOfSquares, vecSrc, vecSrc);
  76. sum = vaddvaq(sum, vecSrc);
  77. blkCnt --;
  78. pSrc += 8;
  79. }
  80. /* Tail */
  81. blkCnt = blockSize & 7;
  82. while (blkCnt > 0U)
  83. {
  84. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  85. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  86. in = *pSrc++;
  87. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  88. #if defined (ARM_MATH_DSP)
  89. sumOfSquares = __SMLALD(in, in, sumOfSquares);
  90. #else
  91. sumOfSquares += (in * in);
  92. #endif /* #if defined (ARM_MATH_DSP) */
  93. /* Compute sum and store result in a temporary variable, sum. */
  94. sum += in;
  95. /* Decrement loop counter */
  96. blkCnt--;
  97. }
  98. /* Compute Mean of squares of the input samples
  99. * and then store the result in a temporary variable, meanOfSquares. */
  100. meanOfSquares = arm_div_q63_to_q31(sumOfSquares, (blockSize - 1U));
  101. /* Compute square of mean */
  102. squareOfMean = arm_div_q63_to_q31((q63_t)sum * sum, (q31_t)(blockSize * (blockSize - 1U)));
  103. /* mean of the squares minus the square of the mean. */
  104. *pResult = (meanOfSquares - squareOfMean) >> 15;
  105. }
  106. #else
  107. void arm_var_q15(
  108. const q15_t * pSrc,
  109. uint32_t blockSize,
  110. q15_t * pResult)
  111. {
  112. uint32_t blkCnt; /* Loop counter */
  113. q31_t sum = 0; /* Accumulator */
  114. q31_t meanOfSquares, squareOfMean; /* Square of mean and mean of square */
  115. q63_t sumOfSquares = 0; /* Sum of squares */
  116. q15_t in; /* Temporary variable to store input value */
  117. #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
  118. q31_t in32; /* Temporary variable to store input value */
  119. #endif
  120. if (blockSize <= 1U)
  121. {
  122. *pResult = 0;
  123. return;
  124. }
  125. #if defined (ARM_MATH_LOOPUNROLL)
  126. /* Loop unrolling: Compute 4 outputs at a time */
  127. blkCnt = blockSize >> 2U;
  128. while (blkCnt > 0U)
  129. {
  130. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  131. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  132. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  133. /* Compute sum and store result in a temporary variable, sum. */
  134. #if defined (ARM_MATH_DSP)
  135. in32 = read_q15x2_ia ((q15_t **) &pSrc);
  136. sumOfSquares = __SMLALD(in32, in32, sumOfSquares);
  137. sum += ((in32 << 16U) >> 16U);
  138. sum += (in32 >> 16U);
  139. in32 = read_q15x2_ia ((q15_t **) &pSrc);
  140. sumOfSquares = __SMLALD(in32, in32, sumOfSquares);
  141. sum += ((in32 << 16U) >> 16U);
  142. sum += (in32 >> 16U);
  143. #else
  144. in = *pSrc++;
  145. sumOfSquares += (in * in);
  146. sum += in;
  147. in = *pSrc++;
  148. sumOfSquares += (in * in);
  149. sum += in;
  150. in = *pSrc++;
  151. sumOfSquares += (in * in);
  152. sum += in;
  153. in = *pSrc++;
  154. sumOfSquares += (in * in);
  155. sum += in;
  156. #endif /* #if defined (ARM_MATH_DSP) */
  157. /* Decrement loop counter */
  158. blkCnt--;
  159. }
  160. /* Loop unrolling: Compute remaining outputs */
  161. blkCnt = blockSize % 0x4U;
  162. #else
  163. /* Initialize blkCnt with number of samples */
  164. blkCnt = blockSize;
  165. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  166. while (blkCnt > 0U)
  167. {
  168. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  169. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  170. in = *pSrc++;
  171. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  172. #if defined (ARM_MATH_DSP)
  173. sumOfSquares = __SMLALD(in, in, sumOfSquares);
  174. #else
  175. sumOfSquares += (in * in);
  176. #endif /* #if defined (ARM_MATH_DSP) */
  177. /* Compute sum and store result in a temporary variable, sum. */
  178. sum += in;
  179. /* Decrement loop counter */
  180. blkCnt--;
  181. }
  182. /* Compute Mean of squares and store result in a temporary variable, meanOfSquares. */
  183. meanOfSquares = (q31_t) (sumOfSquares / (q63_t)(blockSize - 1U));
  184. /* Compute square of mean */
  185. squareOfMean = (q31_t) ((q63_t) sum * sum / (q63_t)(blockSize * (blockSize - 1U)));
  186. /* mean of squares minus the square of mean. */
  187. *pResult = (meanOfSquares - squareOfMean) >> 15U;
  188. }
  189. #endif /* defined(ARM_MATH_MVEI) */
  190. /**
  191. @} end of variance group
  192. */