arm_var_q31.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 12. March 2014
  5. * $Revision: V1.4.4
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_var_q31.c
  9. *
  10. * Description: Variance of an array of Q31 type.
  11. *
  12. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * - Neither the name of ARM LIMITED nor the names of its contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. * -------------------------------------------------------------------- */
  40. #include "arm_math.h"
  41. /**
  42. * @ingroup groupStats
  43. */
  44. /**
  45. * @addtogroup variance
  46. * @{
  47. */
  48. /**
  49. * @brief Variance of the elements of a Q31 vector.
  50. * @param[in] *pSrc points to the input vector
  51. * @param[in] blockSize length of the input vector
  52. * @param[out] *pResult variance value returned here
  53. * @return none.
  54. *
  55. * @details
  56. * <b>Scaling and Overflow Behavior:</b>
  57. *
  58. *\par
  59. * The function is implemented using an internal 64-bit accumulator.
  60. * The input is represented in 1.31 format, which is then downshifted by 8 bits
  61. * which yields 1.23, and intermediate multiplication yields a 2.46 format.
  62. * The accumulator maintains full precision of the intermediate multiplication results,
  63. * but provides only a 16 guard bits.
  64. * There is no saturation on intermediate additions.
  65. * If the accumulator overflows it wraps around and distorts the result.
  66. * In order to avoid overflows completely the input signal must be scaled down by
  67. * log2(blockSize)-8 bits, as a total of blockSize additions are performed internally.
  68. * After division, internal variables should be Q18.46
  69. * Finally, the 18.46 accumulator is right shifted by 15 bits to yield a 1.31 format value.
  70. *
  71. */
  72. void arm_var_q31(
  73. q31_t * pSrc,
  74. uint32_t blockSize,
  75. q31_t * pResult)
  76. {
  77. q63_t sum = 0; /* Accumulator */
  78. q63_t meanOfSquares, squareOfMean; /* square of mean and mean of square */
  79. q31_t in; /* input value */
  80. uint32_t blkCnt; /* loop counter */
  81. q63_t sumOfSquares = 0; /* Accumulator */
  82. if(blockSize == 1)
  83. {
  84. *pResult = 0;
  85. return;
  86. }
  87. #ifndef ARM_MATH_CM0_FAMILY
  88. /* Run the below code for Cortex-M4 and Cortex-M3 */
  89. /*loop Unrolling */
  90. blkCnt = blockSize >> 2u;
  91. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  92. ** a second loop below computes the remaining 1 to 3 samples. */
  93. while(blkCnt > 0u)
  94. {
  95. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  96. /* Compute Sum of squares of the input samples
  97. * and then store the result in a temporary variable, sum. */
  98. in = *pSrc++ >> 8;
  99. sum += in;
  100. sumOfSquares += ((q63_t) (in) * (in));
  101. in = *pSrc++ >> 8;
  102. sum += in;
  103. sumOfSquares += ((q63_t) (in) * (in));
  104. in = *pSrc++ >> 8;
  105. sum += in;
  106. sumOfSquares += ((q63_t) (in) * (in));
  107. in = *pSrc++ >> 8;
  108. sum += in;
  109. sumOfSquares += ((q63_t) (in) * (in));
  110. /* Decrement the loop counter */
  111. blkCnt--;
  112. }
  113. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  114. ** No loop unrolling is used. */
  115. blkCnt = blockSize % 0x4u;
  116. while(blkCnt > 0u)
  117. {
  118. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  119. /* Compute Sum of squares of the input samples
  120. * and then store the result in a temporary variable, sum. */
  121. in = *pSrc++ >> 8;
  122. sum += in;
  123. sumOfSquares += ((q63_t) (in) * (in));
  124. /* Decrement the loop counter */
  125. blkCnt--;
  126. }
  127. /* Compute Mean of squares of the input samples
  128. * and then store the result in a temporary variable, meanOfSquares. */
  129. meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1);
  130. #else
  131. /* Run the below code for Cortex-M0 */
  132. /* Loop over blockSize number of values */
  133. blkCnt = blockSize;
  134. while(blkCnt > 0u)
  135. {
  136. /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  137. /* Compute Sum of squares of the input samples
  138. * and then store the result in a temporary variable, sumOfSquares. */
  139. in = *pSrc++ >> 8;
  140. sumOfSquares += ((q63_t) (in) * (in));
  141. /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
  142. /* Compute sum of all input values and then store the result in a temporary variable, sum. */
  143. sum += in;
  144. /* Decrement the loop counter */
  145. blkCnt--;
  146. }
  147. /* Compute Mean of squares of the input samples
  148. * and then store the result in a temporary variable, meanOfSquares. */
  149. meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1);
  150. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  151. /* Compute square of mean */
  152. squareOfMean = sum * sum / (q63_t)(blockSize * (blockSize - 1u));
  153. /* Compute standard deviation and then store the result to the destination */
  154. *pResult = (meanOfSquares - squareOfMean) >> 15;
  155. }
  156. /**
  157. * @} end of variance group
  158. */