CMSIS DSP Library from CMSIS 2.0. See http://www.onarm.com/cmsis/ for full details
Dependents: K22F_DSP_Matrix_least_square BNO055-ELEC3810 1BNO055 ECE4180Project--Slave2 ... more
src/Cortex-M4-M3/StatisticsFunctions/arm_std_q15.c@0:1014af42efd9, 2011-03-10 (annotated)
- Committer:
- simon
- Date:
- Thu Mar 10 15:07:50 2011 +0000
- Revision:
- 0:1014af42efd9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:1014af42efd9 | 1 | /* ---------------------------------------------------------------------- |
simon | 0:1014af42efd9 | 2 | * Copyright (C) 2010 ARM Limited. All rights reserved. |
simon | 0:1014af42efd9 | 3 | * |
simon | 0:1014af42efd9 | 4 | * $Date: 29. November 2010 |
simon | 0:1014af42efd9 | 5 | * $Revision: V1.0.3 |
simon | 0:1014af42efd9 | 6 | * |
simon | 0:1014af42efd9 | 7 | * Project: CMSIS DSP Library |
simon | 0:1014af42efd9 | 8 | * Title: arm_std_q15.c |
simon | 0:1014af42efd9 | 9 | * |
simon | 0:1014af42efd9 | 10 | * Description: Standard deviation of an array of Q15 type. |
simon | 0:1014af42efd9 | 11 | * |
simon | 0:1014af42efd9 | 12 | * Target Processor: Cortex-M4/Cortex-M3 |
simon | 0:1014af42efd9 | 13 | * |
simon | 0:1014af42efd9 | 14 | * Version 1.0.3 2010/11/29 |
simon | 0:1014af42efd9 | 15 | * Re-organized the CMSIS folders and updated documentation. |
simon | 0:1014af42efd9 | 16 | * |
simon | 0:1014af42efd9 | 17 | * Version 1.0.2 2010/11/11 |
simon | 0:1014af42efd9 | 18 | * Documentation updated. |
simon | 0:1014af42efd9 | 19 | * |
simon | 0:1014af42efd9 | 20 | * Version 1.0.1 2010/10/05 |
simon | 0:1014af42efd9 | 21 | * Production release and review comments incorporated. |
simon | 0:1014af42efd9 | 22 | * |
simon | 0:1014af42efd9 | 23 | * Version 1.0.0 2010/09/20 |
simon | 0:1014af42efd9 | 24 | * Production release and review comments incorporated. |
simon | 0:1014af42efd9 | 25 | * -------------------------------------------------------------------- */ |
simon | 0:1014af42efd9 | 26 | |
simon | 0:1014af42efd9 | 27 | #include "arm_math.h" |
simon | 0:1014af42efd9 | 28 | |
simon | 0:1014af42efd9 | 29 | /** |
simon | 0:1014af42efd9 | 30 | * @ingroup groupStats |
simon | 0:1014af42efd9 | 31 | */ |
simon | 0:1014af42efd9 | 32 | |
simon | 0:1014af42efd9 | 33 | /** |
simon | 0:1014af42efd9 | 34 | * @addtogroup STD |
simon | 0:1014af42efd9 | 35 | * @{ |
simon | 0:1014af42efd9 | 36 | */ |
simon | 0:1014af42efd9 | 37 | |
simon | 0:1014af42efd9 | 38 | /** |
simon | 0:1014af42efd9 | 39 | * @brief Standard deviation of the elements of a Q15 vector. |
simon | 0:1014af42efd9 | 40 | * @param[in] *pSrc points to the input vector |
simon | 0:1014af42efd9 | 41 | * @param[in] blockSize length of the input vector |
simon | 0:1014af42efd9 | 42 | * @param[out] *pResult standard deviation value returned here |
simon | 0:1014af42efd9 | 43 | * @return none. |
simon | 0:1014af42efd9 | 44 | * |
simon | 0:1014af42efd9 | 45 | * @details |
simon | 0:1014af42efd9 | 46 | * <b>Scaling and Overflow Behavior:</b> |
simon | 0:1014af42efd9 | 47 | * |
simon | 0:1014af42efd9 | 48 | * \par |
simon | 0:1014af42efd9 | 49 | * The function is implemented using a 64-bit internal accumulator. |
simon | 0:1014af42efd9 | 50 | * The input is represented in 1.15 format. |
simon | 0:1014af42efd9 | 51 | * Intermediate multiplication yields a 2.30 format, and this |
simon | 0:1014af42efd9 | 52 | * result is added without saturation to a 64-bit accumulator in 34.30 format. |
simon | 0:1014af42efd9 | 53 | * With 33 guard bits in the accumulator, there is no risk of overflow, and the |
simon | 0:1014af42efd9 | 54 | * full precision of the intermediate multiplication is preserved. |
simon | 0:1014af42efd9 | 55 | * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower |
simon | 0:1014af42efd9 | 56 | * 15 bits, and then saturated to yield a result in 1.15 format. |
simon | 0:1014af42efd9 | 57 | */ |
simon | 0:1014af42efd9 | 58 | |
simon | 0:1014af42efd9 | 59 | void arm_std_q15( |
simon | 0:1014af42efd9 | 60 | q15_t * pSrc, |
simon | 0:1014af42efd9 | 61 | uint32_t blockSize, |
simon | 0:1014af42efd9 | 62 | q15_t * pResult) |
simon | 0:1014af42efd9 | 63 | { |
simon | 0:1014af42efd9 | 64 | q63_t sum = 0; /* Accumulator */ |
simon | 0:1014af42efd9 | 65 | q31_t meanOfSquares, squareOfMean; /* square of mean and mean of square */ |
simon | 0:1014af42efd9 | 66 | q15_t mean; /* mean */ |
simon | 0:1014af42efd9 | 67 | q31_t in; /* input value */ |
simon | 0:1014af42efd9 | 68 | q15_t in1; /* input value */ |
simon | 0:1014af42efd9 | 69 | uint32_t blkCnt; /* loop counter */ |
simon | 0:1014af42efd9 | 70 | q15_t t; /* Temporary variable */ |
simon | 0:1014af42efd9 | 71 | q15_t *pIn; /* Temporary pointer */ |
simon | 0:1014af42efd9 | 72 | |
simon | 0:1014af42efd9 | 73 | pIn = pSrc; |
simon | 0:1014af42efd9 | 74 | |
simon | 0:1014af42efd9 | 75 | /*loop Unrolling */ |
simon | 0:1014af42efd9 | 76 | blkCnt = blockSize >> 2u; |
simon | 0:1014af42efd9 | 77 | |
simon | 0:1014af42efd9 | 78 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
simon | 0:1014af42efd9 | 79 | ** a second loop below computes the remaining 1 to 3 samples. */ |
simon | 0:1014af42efd9 | 80 | while(blkCnt > 0u) |
simon | 0:1014af42efd9 | 81 | { |
simon | 0:1014af42efd9 | 82 | /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ |
simon | 0:1014af42efd9 | 83 | /* Compute Sum of squares of the input samples |
simon | 0:1014af42efd9 | 84 | * and then store the result in a temporary variable, sum. */ |
simon | 0:1014af42efd9 | 85 | in = *__SIMD32(pSrc)++; |
simon | 0:1014af42efd9 | 86 | sum = __SMLALD(in, in, sum); |
simon | 0:1014af42efd9 | 87 | in = *__SIMD32(pSrc)++; |
simon | 0:1014af42efd9 | 88 | sum = __SMLALD(in, in, sum); |
simon | 0:1014af42efd9 | 89 | |
simon | 0:1014af42efd9 | 90 | /* Decrement the loop counter */ |
simon | 0:1014af42efd9 | 91 | blkCnt--; |
simon | 0:1014af42efd9 | 92 | } |
simon | 0:1014af42efd9 | 93 | |
simon | 0:1014af42efd9 | 94 | /* If the blockSize is not a multiple of 4, compute any remaining output samples here. |
simon | 0:1014af42efd9 | 95 | ** No loop unrolling is used. */ |
simon | 0:1014af42efd9 | 96 | blkCnt = blockSize % 0x4u; |
simon | 0:1014af42efd9 | 97 | |
simon | 0:1014af42efd9 | 98 | while(blkCnt > 0u) |
simon | 0:1014af42efd9 | 99 | { |
simon | 0:1014af42efd9 | 100 | /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ |
simon | 0:1014af42efd9 | 101 | /* Compute Sum of squares of the input samples |
simon | 0:1014af42efd9 | 102 | * and then store the result in a temporary variable, sum. */ |
simon | 0:1014af42efd9 | 103 | in1 = *pSrc++; |
simon | 0:1014af42efd9 | 104 | sum = __SMLALD(in1, in1, sum); |
simon | 0:1014af42efd9 | 105 | |
simon | 0:1014af42efd9 | 106 | /* Decrement the loop counter */ |
simon | 0:1014af42efd9 | 107 | blkCnt--; |
simon | 0:1014af42efd9 | 108 | } |
simon | 0:1014af42efd9 | 109 | |
simon | 0:1014af42efd9 | 110 | /* Compute Mean of squares of the input samples |
simon | 0:1014af42efd9 | 111 | * and then store the result in a temporary variable, meanOfSquares. */ |
simon | 0:1014af42efd9 | 112 | t = (q15_t) ((1.0 / (blockSize - 1)) * 16384LL); |
simon | 0:1014af42efd9 | 113 | sum = __SSAT((sum >> 15u), 16u); |
simon | 0:1014af42efd9 | 114 | |
simon | 0:1014af42efd9 | 115 | meanOfSquares = (q31_t) ((sum * t) >> 14u); |
simon | 0:1014af42efd9 | 116 | |
simon | 0:1014af42efd9 | 117 | /* Reset the accumulator */ |
simon | 0:1014af42efd9 | 118 | sum = 0; |
simon | 0:1014af42efd9 | 119 | |
simon | 0:1014af42efd9 | 120 | /*loop Unrolling */ |
simon | 0:1014af42efd9 | 121 | blkCnt = blockSize >> 2u; |
simon | 0:1014af42efd9 | 122 | |
simon | 0:1014af42efd9 | 123 | /* Reset the input working pointer */ |
simon | 0:1014af42efd9 | 124 | pSrc = pIn; |
simon | 0:1014af42efd9 | 125 | |
simon | 0:1014af42efd9 | 126 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
simon | 0:1014af42efd9 | 127 | ** a second loop below computes the remaining 1 to 3 samples. */ |
simon | 0:1014af42efd9 | 128 | while(blkCnt > 0u) |
simon | 0:1014af42efd9 | 129 | { |
simon | 0:1014af42efd9 | 130 | /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ |
simon | 0:1014af42efd9 | 131 | /* Compute sum of all input values and then store the result in a temporary variable, sum. */ |
simon | 0:1014af42efd9 | 132 | sum += *pSrc++; |
simon | 0:1014af42efd9 | 133 | sum += *pSrc++; |
simon | 0:1014af42efd9 | 134 | sum += *pSrc++; |
simon | 0:1014af42efd9 | 135 | sum += *pSrc++; |
simon | 0:1014af42efd9 | 136 | |
simon | 0:1014af42efd9 | 137 | /* Decrement the loop counter */ |
simon | 0:1014af42efd9 | 138 | blkCnt--; |
simon | 0:1014af42efd9 | 139 | } |
simon | 0:1014af42efd9 | 140 | |
simon | 0:1014af42efd9 | 141 | /* If the blockSize is not a multiple of 4, compute any remaining output samples here. |
simon | 0:1014af42efd9 | 142 | ** No loop unrolling is used. */ |
simon | 0:1014af42efd9 | 143 | blkCnt = blockSize % 0x4u; |
simon | 0:1014af42efd9 | 144 | |
simon | 0:1014af42efd9 | 145 | while(blkCnt > 0u) |
simon | 0:1014af42efd9 | 146 | { |
simon | 0:1014af42efd9 | 147 | /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ |
simon | 0:1014af42efd9 | 148 | /* Compute sum of all input values and then store the result in a temporary variable, sum. */ |
simon | 0:1014af42efd9 | 149 | sum += *pSrc++; |
simon | 0:1014af42efd9 | 150 | |
simon | 0:1014af42efd9 | 151 | /* Decrement the loop counter */ |
simon | 0:1014af42efd9 | 152 | blkCnt--; |
simon | 0:1014af42efd9 | 153 | } |
simon | 0:1014af42efd9 | 154 | /* Compute mean of all input values */ |
simon | 0:1014af42efd9 | 155 | t = (q15_t) ((1.0 / (blockSize * (blockSize - 1))) * 32768LL); |
simon | 0:1014af42efd9 | 156 | mean = (q15_t) __SSAT(sum, 16u); |
simon | 0:1014af42efd9 | 157 | |
simon | 0:1014af42efd9 | 158 | /* Compute square of mean */ |
simon | 0:1014af42efd9 | 159 | squareOfMean = ((q31_t) mean * mean) >> 15; |
simon | 0:1014af42efd9 | 160 | squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 15); |
simon | 0:1014af42efd9 | 161 | |
simon | 0:1014af42efd9 | 162 | /* mean of the squares minus the square of the mean. */ |
simon | 0:1014af42efd9 | 163 | in1 = (q15_t) (meanOfSquares - squareOfMean); |
simon | 0:1014af42efd9 | 164 | |
simon | 0:1014af42efd9 | 165 | /* Compute standard deviation and store the result to the destination */ |
simon | 0:1014af42efd9 | 166 | arm_sqrt_q15(in1, pResult); |
simon | 0:1014af42efd9 | 167 | } |
simon | 0:1014af42efd9 | 168 | |
simon | 0:1014af42efd9 | 169 | /** |
simon | 0:1014af42efd9 | 170 | * @} end of STD group |
simon | 0:1014af42efd9 | 171 | */ |