00001 /* ---------------------------------------------------------------------- 00002 * Copyright (C) 2011 ARM Limited. All rights reserved. 00003 * 00004 * $Date: 15. December 2011 00005 * $Revision: V2.0.0 00006 * 00007 * Project: Cortex-R DSP Library 00008 * Title: arm_std_q31.c 00009 * 00010 * Description: Standard deviation of an array of Q31 type. 00011 * 00012 * Target Processor: Cortex-R4/R5 00013 * 00014 * Version 1.0.0 2011/03/08 00015 * Alpha release. 00016 * 00017 * Version 1.0.1 2011/09/30 00018 * Beta release. 00019 * 00020 * Version 2.0.0 2011/12/15 00021 * Final release. 00022 * 00023 * -------------------------------------------------------------------- */ 00024 #include "arm_math.h" 00025 00060 void arm_std_q31( 00061 q31_t * pSrc, 00062 uint32_t blockSize, 00063 q31_t * pResult) 00064 { 00065 q63_t sum = 0, sumSquare = 0; /* Accumulator */ 00066 q31_t meanOfSquares, squareOfMean; /* square of mean and mean of square */ 00067 q31_t mean; /* mean */ 00068 q31_t in; /* input value */ 00069 q31_t t; /* Temporary variable */ 00070 uint32_t blkCnt; /* loop counter */ 00071 q31_t in1, in2, in3, in4; /* Temporary input variables */ 00072 q63_t sumSquare1 = 0; 00073 00074 /*loop Unrolling */ 00075 blkCnt = blockSize >> 3u; 00076 00077 /* First part of the processing with loop unrolling. Compute 8 outputs at a time. 00078 ** a second loop below computes the remaining 1 to 7 samples. */ 00079 while(blkCnt > 0u) 00080 { 00081 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00082 /* Compute Sum of squares of the input samples 00083 * and then store the result in a temporary variable, sum. */ 00084 /* read input from source buffer */ 00085 in1 = pSrc[0]; 00086 in2 = pSrc[1]; 00087 00088 /* calculate sum of inputs */ 00089 sum += in1; 00090 /* calculate power of input and accumulate to accumulator */ 00091 sumSquare += ((q63_t) (in1) * (in1)); 00092 00093 /* read input from source buffer */ 00094 in3 = pSrc[2]; 00095 00096 /* calculate sum of inputs */ 00097 sum += in2; 00098 /* calculate power of input and accumulate to accumulator */ 00099 sumSquare1 += ((q63_t) (in2) * (in2)); 00100 00101 /* read input from source buffer */ 00102 in4 = pSrc[3]; 00103 00104 /* calculate sum of inputs */ 00105 sum += in3; 00106 /* calculate power of input and accumulate to accumulator */ 00107 sumSquare += ((q63_t) (in3) * (in3)); 00108 00109 /* read input from source buffer */ 00110 in1 = pSrc[4]; 00111 00112 /* calculate sum of inputs */ 00113 sum += in4; 00114 /* calculate power of input and accumulate to accumulator */ 00115 sumSquare1 += ((q63_t) (in4) * (in4)); 00116 00117 /* read input from source buffer */ 00118 in2 = pSrc[5]; 00119 00120 /* calculate sum of inputs */ 00121 sum += in1; 00122 /* calculate power of input and accumulate to accumulator */ 00123 sumSquare += ((q63_t) (in1) * (in1)); 00124 00125 /* read input from source buffer */ 00126 in3 = pSrc[6]; 00127 00128 /* calculate sum of inputs */ 00129 sum += in2; 00130 /* calculate power of input and accumulate to accumulator */ 00131 sumSquare1 += ((q63_t) (in2) * (in2)); 00132 00133 /* read input from source buffer */ 00134 in4 = pSrc[7]; 00135 00136 /* calculate sum of inputs */ 00137 sum += in3; 00138 /* calculate power of input and accumulate to accumulator */ 00139 sumSquare += ((q63_t) (in3) * (in3)); 00140 00141 /* calculate sum of inputs */ 00142 sum += in4; 00143 /* calculate power of input and accumulate to accumulator */ 00144 sumSquare1 += ((q63_t) (in4) * (in4)); 00145 00146 /* update source buffer pointer to process next samples */ 00147 pSrc += 8u; 00148 00149 /* Decrement the loop counter */ 00150 blkCnt--; 00151 } 00152 00153 /* add sum of square accumulators */ 00154 sumSquare = sumSquare + sumSquare1; 00155 00156 /* If the blockSize is not a multiple of 8, compute any remaining output samples here. 00157 ** No loop unrolling is used. */ 00158 blkCnt = blockSize % 0x8u; 00159 00160 while(blkCnt > 0u) 00161 { 00162 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00163 /* Compute Sum of squares of the input samples 00164 * and then store the result in a temporary variable, sum. */ 00165 in = *pSrc++; 00166 sumSquare += ((q63_t) (in) * (in)); 00167 sum += in; 00168 00169 /* Decrement the loop counter */ 00170 blkCnt--; 00171 } 00172 00173 t = (q31_t) ((1.0f / (float32_t) (blockSize - 1u)) * 1073741824.0f); 00174 00175 /* Compute Mean of squares of the input samples 00176 * and then store the result in a temporary variable, meanOfSquares. */ 00177 sumSquare = (sumSquare >> 31); 00178 meanOfSquares = (q31_t) ((sumSquare * t) >> 30); 00179 00180 /* Compute mean of all input values */ 00181 t = (q31_t) ((1.0f / (blockSize * (blockSize - 1u))) * 2147483648.0f); 00182 mean = (q31_t) (sum); 00183 00184 /* Compute square of mean */ 00185 squareOfMean = (q31_t) (((q63_t) mean * mean) >> 31); 00186 squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 31); 00187 00188 /* Compute standard deviation and then store the result to the destination */ 00189 arm_sqrt_q31(meanOfSquares - squareOfMean, pResult); 00190 00191 } 00192