arm_std_q15.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_std_q15.c
  4. * Description: Standard deviation of an array of Q15 vector
  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 STD
  34. @{
  35. */
  36. /**
  37. @brief Standard deviation 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 standard deviation 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_std_q15(
  54. const q15_t * pSrc,
  55. uint32_t blockSize,
  56. q15_t * pResult)
  57. {
  58. q15_t var=0;
  59. arm_var_q15(pSrc, blockSize, &var);
  60. arm_sqrt_q15(var,pResult);
  61. }
  62. #else
  63. void arm_std_q15(
  64. const q15_t * pSrc,
  65. uint32_t blockSize,
  66. q15_t * pResult)
  67. {
  68. uint32_t blkCnt; /* Loop counter */
  69. q31_t sum = 0; /* Accumulator */
  70. q31_t meanOfSquares, squareOfMean; /* Square of mean and mean of square */
  71. q63_t sumOfSquares = 0; /* Sum of squares */
  72. q15_t in; /* Temporary variable to store input value */
  73. #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
  74. q31_t in32; /* Temporary variable to store input value */
  75. #endif
  76. if (blockSize <= 1U)
  77. {
  78. *pResult = 0;
  79. return;
  80. }
  81. #if defined (ARM_MATH_LOOPUNROLL)
  82. /* Loop unrolling: Compute 4 outputs at a time */
  83. blkCnt = blockSize >> 2U;
  84. while (blkCnt > 0U)
  85. {
  86. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  87. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  88. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  89. /* Compute sum and store result in a temporary variable, sum. */
  90. #if defined (ARM_MATH_DSP)
  91. in32 = read_q15x2_ia ((q15_t **) &pSrc);
  92. sumOfSquares = __SMLALD(in32, in32, sumOfSquares);
  93. sum += ((in32 << 16U) >> 16U);
  94. sum += (in32 >> 16U);
  95. in32 = read_q15x2_ia ((q15_t **) &pSrc);
  96. sumOfSquares = __SMLALD(in32, in32, sumOfSquares);
  97. sum += ((in32 << 16U) >> 16U);
  98. sum += (in32 >> 16U);
  99. #else
  100. in = *pSrc++;
  101. sumOfSquares += (in * in);
  102. sum += in;
  103. in = *pSrc++;
  104. sumOfSquares += (in * in);
  105. sum += in;
  106. in = *pSrc++;
  107. sumOfSquares += (in * in);
  108. sum += in;
  109. in = *pSrc++;
  110. sumOfSquares += (in * in);
  111. sum += in;
  112. #endif /* #if defined (ARM_MATH_DSP) */
  113. /* Decrement loop counter */
  114. blkCnt--;
  115. }
  116. /* Loop unrolling: Compute remaining outputs */
  117. blkCnt = blockSize % 0x4U;
  118. #else
  119. /* Initialize blkCnt with number of samples */
  120. blkCnt = blockSize;
  121. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  122. while (blkCnt > 0U)
  123. {
  124. /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
  125. /* C = A[0] + A[1] + ... + A[blockSize-1] */
  126. in = *pSrc++;
  127. /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */
  128. sumOfSquares += (in * in);
  129. /* Compute sum and store result in a temporary variable, sum. */
  130. sum += in;
  131. /* Decrement loop counter */
  132. blkCnt--;
  133. }
  134. /* Compute Mean of squares and store result in a temporary variable, meanOfSquares. */
  135. meanOfSquares = (q31_t) (sumOfSquares / (q63_t)(blockSize - 1U));
  136. /* Compute square of mean */
  137. squareOfMean = (q31_t) ((q63_t) sum * sum / (q63_t)(blockSize * (blockSize - 1U)));
  138. /* mean of squares minus the square of mean. */
  139. /* Compute standard deviation and store result in destination */
  140. arm_sqrt_q15(__SSAT((meanOfSquares - squareOfMean) >> 15U, 16U), pResult);
  141. }
  142. #endif /* defined(ARM_MATH_MVEI) */
  143. /**
  144. @} end of STD group
  145. */