arm_var_q15.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_var_q15.c
  4. * Description: Variance of an array of Q15 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 Q15 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 a 64-bit internal accumulator.
  47. * The input is represented in 1.15 format.
  48. * Intermediate multiplication yields a 2.30 format, and this
  49. * result is added without saturation to a 64-bit accumulator in 34.30 format.
  50. * With 33 guard bits in the accumulator, there is no risk of overflow, and the
  51. * full precision of the intermediate multiplication is preserved.
  52. * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
  53. * 15 bits, and then saturated to yield a result in 1.15 format.
  54. */
  55. void arm_var_q15(
  56. q15_t * pSrc,
  57. uint32_t blockSize,
  58. q15_t * pResult)
  59. {
  60. q31_t sum = 0; /* Accumulator */
  61. q31_t meanOfSquares, squareOfMean; /* square of mean and mean of square */
  62. uint32_t blkCnt; /* loop counter */
  63. q63_t sumOfSquares = 0; /* Accumulator */
  64. #if defined (ARM_MATH_DSP)
  65. q31_t in; /* input value */
  66. q15_t in1; /* input value */
  67. #else
  68. q15_t in; /* input value */
  69. #endif
  70. if (blockSize == 1U)
  71. {
  72. *pResult = 0;
  73. return;
  74. }
  75. #if defined (ARM_MATH_DSP)
  76. /* Run the below code for Cortex-M4 and Cortex-M3 */
  77. /*loop Unrolling */
  78. blkCnt = blockSize >> 2U;
  79. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  80. ** a second loop below computes the remaining 1 to 3 samples. */
  81. while (blkCnt > 0U)
  82. {
  83. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  84. /* Compute Sum of squares of the input samples
  85. * and then store the result in a temporary variable, sum. */
  86. in = *__SIMD32(pSrc)++;
  87. sum += ((in << 16U) >> 16U);
  88. sum += (in >> 16U);
  89. sumOfSquares = __SMLALD(in, in, sumOfSquares);
  90. in = *__SIMD32(pSrc)++;
  91. sum += ((in << 16U) >> 16U);
  92. sum += (in >> 16U);
  93. sumOfSquares = __SMLALD(in, in, sumOfSquares);
  94. /* Decrement the loop counter */
  95. blkCnt--;
  96. }
  97. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  98. ** No loop unrolling is used. */
  99. blkCnt = blockSize % 0x4U;
  100. while (blkCnt > 0U)
  101. {
  102. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  103. /* Compute Sum of squares of the input samples
  104. * and then store the result in a temporary variable, sum. */
  105. in1 = *pSrc++;
  106. sumOfSquares = __SMLALD(in1, in1, sumOfSquares);
  107. sum += in1;
  108. /* Decrement the loop counter */
  109. blkCnt--;
  110. }
  111. /* Compute Mean of squares of the input samples
  112. * and then store the result in a temporary variable, meanOfSquares. */
  113. meanOfSquares = (q31_t)(sumOfSquares / (q63_t)(blockSize - 1U));
  114. /* Compute square of mean */
  115. squareOfMean = (q31_t)((q63_t)sum * sum / (q63_t)(blockSize * (blockSize - 1U)));
  116. /* mean of the squares minus the square of the mean. */
  117. *pResult = (meanOfSquares - squareOfMean) >> 15U;
  118. #else
  119. /* Run the below code for Cortex-M0 */
  120. /* Loop over blockSize number of values */
  121. blkCnt = blockSize;
  122. while (blkCnt > 0U)
  123. {
  124. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  125. /* Compute Sum of squares of the input samples
  126. * and then store the result in a temporary variable, sumOfSquares. */
  127. in = *pSrc++;
  128. sumOfSquares += (in * in);
  129. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  130. /* Compute sum of all input values and then store the result in a temporary variable, sum. */
  131. sum += in;
  132. /* Decrement the loop counter */
  133. blkCnt--;
  134. }
  135. /* Compute Mean of squares of the input samples
  136. * and then store the result in a temporary variable, meanOfSquares. */
  137. meanOfSquares = (q31_t)(sumOfSquares / (q63_t)(blockSize - 1U));
  138. /* Compute square of mean */
  139. squareOfMean = (q31_t)((q63_t)sum * sum / (q63_t)(blockSize * (blockSize - 1U)));
  140. /* mean of the squares minus the square of the mean. */
  141. *pResult = (meanOfSquares - squareOfMean) >> 15;
  142. #endif /* #if defined (ARM_MATH_DSP) */
  143. }
  144. /**
  145. * @} end of variance group
  146. */