00001 /* ---------------------------------------------------------------------- 00002 * Copyright (C) 2010 ARM Limited. All rights reserved. 00003 * 00004 * $Date: 15. July 2011 00005 * $Revision: V1.0.10 00006 * 00007 * Project: CMSIS DSP Library 00008 * Title: arm_std_q31.c 00009 * 00010 * Description: Standard deviation of an array of Q31 type. 00011 * 00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 00013 * 00014 * Version 1.0.10 2011/7/15 00015 * Big Endian support added and Merged M0 and M3/M4 Source code. 00016 * 00017 * Version 1.0.3 2010/11/29 00018 * Re-organized the CMSIS folders and updated documentation. 00019 * 00020 * Version 1.0.2 2010/11/11 00021 * Documentation updated. 00022 * 00023 * Version 1.0.1 2010/10/05 00024 * Production release and review comments incorporated. 00025 * 00026 * Version 1.0.0 2010/09/20 00027 * Production release and review comments incorporated. 00028 * -------------------------------------------------------------------- */ 00029 00030 #include "arm_math.h" 00031 00066 void arm_std_q31( 00067 q31_t * pSrc, 00068 uint32_t blockSize, 00069 q31_t * pResult) 00070 { 00071 q63_t sum = 0; /* Accumulator */ 00072 q31_t meanOfSquares, squareOfMean; /* square of mean and mean of square */ 00073 q31_t mean; /* mean */ 00074 q31_t in; /* input value */ 00075 q31_t t; /* Temporary variable */ 00076 uint32_t blkCnt; /* loop counter */ 00077 00078 00079 #ifndef ARM_MATH_CM0 00080 00081 /* Run the below code for Cortex-M4 and Cortex-M3 */ 00082 00083 q31_t *pIn; /* Temporary pointer */ 00084 00085 pIn = pSrc; 00086 00087 /*loop Unrolling */ 00088 blkCnt = blockSize >> 2u; 00089 00090 /* First part of the processing with loop unrolling. Compute 4 outputs at a time. 00091 ** a second loop below computes the remaining 1 to 3 samples. */ 00092 while(blkCnt > 0u) 00093 { 00094 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00095 /* Compute Sum of squares of the input samples 00096 * and then store the result in a temporary variable, sum. */ 00097 in = *pSrc++; 00098 sum += ((q63_t) (in) * (in)); 00099 in = *pSrc++; 00100 sum += ((q63_t) (in) * (in)); 00101 in = *pSrc++; 00102 sum += ((q63_t) (in) * (in)); 00103 in = *pSrc++; 00104 sum += ((q63_t) (in) * (in)); 00105 00106 /* Decrement the loop counter */ 00107 blkCnt--; 00108 } 00109 00110 /* If the blockSize is not a multiple of 4, compute any remaining output samples here. 00111 ** No loop unrolling is used. */ 00112 blkCnt = blockSize % 0x4u; 00113 00114 while(blkCnt > 0u) 00115 { 00116 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00117 /* Compute Sum of squares of the input samples 00118 * and then store the result in a temporary variable, sum. */ 00119 in = *pSrc++; 00120 sum += ((q63_t) (in) * (in)); 00121 00122 /* Decrement the loop counter */ 00123 blkCnt--; 00124 } 00125 00126 t = (q31_t) ((1.0f / (float32_t) (blockSize - 1u)) * 1073741824.0f); 00127 00128 /* Compute Mean of squares of the input samples 00129 * and then store the result in a temporary variable, meanOfSquares. */ 00130 sum = (sum >> 31); 00131 meanOfSquares = (q31_t) ((sum * t) >> 30); 00132 00133 /* Reset the accumulator */ 00134 sum = 0; 00135 00136 /*loop Unrolling */ 00137 blkCnt = blockSize >> 2u; 00138 00139 /* Reset the input working pointer */ 00140 pSrc = pIn; 00141 00142 /* First part of the processing with loop unrolling. Compute 4 outputs at a time. 00143 ** a second loop below computes the remaining 1 to 3 samples. */ 00144 while(blkCnt > 0u) 00145 { 00146 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ 00147 /* Compute sum of all input values and then store the result in a temporary variable, sum. */ 00148 sum += *pSrc++; 00149 sum += *pSrc++; 00150 sum += *pSrc++; 00151 sum += *pSrc++; 00152 00153 /* Decrement the loop counter */ 00154 blkCnt--; 00155 } 00156 00157 /* If the blockSize is not a multiple of 4, compute any remaining output samples here. 00158 ** No loop unrolling is used. */ 00159 blkCnt = blockSize % 0x4u; 00160 00161 while(blkCnt > 0u) 00162 { 00163 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ 00164 /* Compute sum of all input values and then store the result in a temporary variable, sum. */ 00165 sum += *pSrc++; 00166 00167 /* Decrement the loop counter */ 00168 blkCnt--; 00169 } 00170 00171 #else 00172 00173 /* Run the below code for Cortex-M0 */ 00174 00175 q63_t sumOfSquares = 0; /* Accumulator */ 00176 /* Loop over blockSize number of values */ 00177 blkCnt = blockSize; 00178 00179 while(blkCnt > 0u) 00180 { 00181 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00182 /* Compute Sum of squares of the input samples 00183 * and then store the result in a temporary variable, sumOfSquares. */ 00184 in = *pSrc++; 00185 sumOfSquares += ((q63_t) (in) * (in)); 00186 00187 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ 00188 /* Compute sum of all input values and then store the result in a temporary variable, sum. */ 00189 sum += in; 00190 00191 /* Decrement the loop counter */ 00192 blkCnt--; 00193 } 00194 00195 /* Compute Mean of squares of the input samples 00196 * and then store the result in a temporary variable, meanOfSquares. */ 00197 t = (q31_t) ((1.0f / (float32_t) (blockSize - 1u)) * 1073741824.0f); 00198 sumOfSquares = (sumOfSquares >> 31); 00199 meanOfSquares = (q31_t) ((sumOfSquares * t) >> 30); 00200 00201 #endif /* #ifndef ARM_MATH_CM0 */ 00202 00203 /* Compute mean of all input values */ 00204 t = (q31_t) ((1.0f / (blockSize * (blockSize - 1u))) * 2147483648.0f); 00205 mean = (q31_t) (sum); 00206 00207 /* Compute square of mean */ 00208 squareOfMean = (q31_t) (((q63_t) mean * mean) >> 31); 00209 squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 31); 00210 00211 00212 /* Compute standard deviation and then store the result to the destination */ 00213 arm_sqrt_q31(meanOfSquares - squareOfMean, pResult); 00214 00215 } 00216