arm_std_q31.c
1 /* ---------------------------------------------------------------------- 2 * Project: CMSIS DSP Library 3 * Title: arm_std_q31.c 4 * Description: Standard deviation of the elements of a Q31 vector 5 * 6 * $Date: 23 April 2021 7 * $Revision: V1.9.0 8 * 9 * Target Processor: Cortex-M and Cortex-A cores 10 * -------------------------------------------------------------------- */ 11 /* 12 * Copyright (C) 2010-2021 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 29 #include "dsp/statistics_functions.h" 30 31 /** 32 @ingroup groupStats 33 */ 34 35 /** 36 @addtogroup STD 37 @{ 38 */ 39 40 /** 41 @brief Standard deviation of the elements of a Q31 vector. 42 @param[in] pSrc points to the input vector. 43 @param[in] blockSize number of samples in input vector. 44 @param[out] pResult standard deviation value returned here. 45 @return none 46 47 @par Scaling and Overflow Behavior 48 The function is implemented using an internal 64-bit accumulator. 49 The input is represented in 1.31 format, which is then downshifted by 8 bits 50 which yields 1.23, and intermediate multiplication yields a 2.46 format. 51 The accumulator maintains full precision of the intermediate multiplication results, 52 but provides only a 16 guard bits. 53 There is no saturation on intermediate additions. 54 If the accumulator overflows it wraps around and distorts the result. 55 In order to avoid overflows completely the input signal must be scaled down by 56 log2(blockSize)-8 bits, as a total of blockSize additions are performed internally. 57 After division, internal variables should be Q18.46 58 Finally, the 18.46 accumulator is right shifted by 15 bits to yield a 1.31 format value. 59 */ 60 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE) 61 void arm_std_q31( 62 const q31_t * pSrc, 63 uint32_t blockSize, 64 q31_t * pResult) 65 { 66 q31_t var=0; 67 68 arm_var_q31(pSrc, blockSize, &var); 69 arm_sqrt_q31(var, pResult); 70 } 71 #else 72 void arm_std_q31( 73 const q31_t * pSrc, 74 uint32_t blockSize, 75 q31_t * pResult) 76 { 77 uint32_t blkCnt; /* Loop counter */ 78 q63_t sum = 0; /* Accumulator */ 79 q63_t meanOfSquares, squareOfMean; /* Square of mean and mean of square */ 80 q63_t sumOfSquares = 0; /* Sum of squares */ 81 q31_t in; /* Temporary variable to store input value */ 82 83 if (blockSize <= 1U) 84 { 85 *pResult = 0; 86 return; 87 } 88 89 #if defined (ARM_MATH_LOOPUNROLL) 90 91 /* Loop unrolling: Compute 4 outputs at a time */ 92 blkCnt = blockSize >> 2U; 93 94 while (blkCnt > 0U) 95 { 96 /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ 97 /* C = A[0] + A[1] + ... + A[blockSize-1] */ 98 99 in = *pSrc++ >> 8U; 100 /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */ 101 sumOfSquares += ((q63_t) (in) * (in)); 102 /* Compute sum and store result in a temporary variable, sum. */ 103 sum += in; 104 105 in = *pSrc++ >> 8U; 106 sumOfSquares += ((q63_t) (in) * (in)); 107 sum += in; 108 109 in = *pSrc++ >> 8U; 110 sumOfSquares += ((q63_t) (in) * (in)); 111 sum += in; 112 113 in = *pSrc++ >> 8U; 114 sumOfSquares += ((q63_t) (in) * (in)); 115 sum += in; 116 117 /* Decrement loop counter */ 118 blkCnt--; 119 } 120 121 /* Loop unrolling: Compute remaining outputs */ 122 blkCnt = blockSize % 0x4U; 123 124 #else 125 126 /* Initialize blkCnt with number of samples */ 127 blkCnt = blockSize; 128 129 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ 130 131 while (blkCnt > 0U) 132 { 133 /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ 134 /* C = A[0] + A[1] + ... + A[blockSize-1] */ 135 136 in = *pSrc++ >> 8U; 137 /* Compute sum of squares and store result in a temporary variable, sumOfSquares. */ 138 sumOfSquares += ((q63_t) (in) * (in)); 139 /* Compute sum and store result in a temporary variable, sum. */ 140 sum += in; 141 142 /* Decrement loop counter */ 143 blkCnt--; 144 } 145 146 /* Compute Mean of squares and store result in a temporary variable, meanOfSquares. */ 147 meanOfSquares = (sumOfSquares / (q63_t)(blockSize - 1U)); 148 149 /* Compute square of mean */ 150 squareOfMean = ( sum * sum / (q63_t)(blockSize * (blockSize - 1U))); 151 152 /* Compute standard deviation and store result in destination */ 153 arm_sqrt_q31((meanOfSquares - squareOfMean) >> 15U, pResult); 154 } 155 #endif /* defined(ARM_MATH_MVEI) */ 156 157 /** 158 @} end of STD group 159 */