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_q15.c 00009 * 00010 * Description: Standard deviation of an array of Q15 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 00062 void arm_std_q15( 00063 q15_t * pSrc, 00064 uint32_t blockSize, 00065 q15_t * pResult) 00066 { 00067 q63_t sum = 0; /* Accumulator */ 00068 q31_t meanOfSquares, squareOfMean; /* square of mean and mean of square */ 00069 q15_t mean; /* mean */ 00070 uint32_t blkCnt; /* loop counter */ 00071 q15_t t; /* Temporary variable */ 00072 00073 #ifndef ARM_MATH_CM0 00074 00075 /* Run the below code for Cortex-M4 and Cortex-M3 */ 00076 00077 q15_t *pIn; /* Temporary pointer */ 00078 q31_t in; /* input value */ 00079 q15_t in1; /* input value */ 00080 00081 pIn = pSrc; 00082 00083 /*loop Unrolling */ 00084 blkCnt = blockSize >> 2u; 00085 00086 /* First part of the processing with loop unrolling. Compute 4 outputs at a time. 00087 ** a second loop below computes the remaining 1 to 3 samples. */ 00088 while(blkCnt > 0u) 00089 { 00090 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00091 /* Compute Sum of squares of the input samples 00092 * and then store the result in a temporary variable, sum. */ 00093 in = *__SIMD32(pSrc)++; 00094 sum = __SMLALD(in, in, sum); 00095 in = *__SIMD32(pSrc)++; 00096 sum = __SMLALD(in, in, sum); 00097 00098 /* Decrement the loop counter */ 00099 blkCnt--; 00100 } 00101 00102 /* If the blockSize is not a multiple of 4, compute any remaining output samples here. 00103 ** No loop unrolling is used. */ 00104 blkCnt = blockSize % 0x4u; 00105 00106 while(blkCnt > 0u) 00107 { 00108 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00109 /* Compute Sum of squares of the input samples 00110 * and then store the result in a temporary variable, sum. */ 00111 in1 = *pSrc++; 00112 sum = __SMLALD(in1, in1, sum); 00113 00114 /* Decrement the loop counter */ 00115 blkCnt--; 00116 } 00117 00118 /* Compute Mean of squares of the input samples 00119 * and then store the result in a temporary variable, meanOfSquares. */ 00120 t = (q15_t) ((1.0 / (blockSize - 1)) * 16384LL); 00121 sum = __SSAT((sum >> 15u), 16u); 00122 00123 meanOfSquares = (q31_t) ((sum * t) >> 14u); 00124 00125 /* Reset the accumulator */ 00126 sum = 0; 00127 00128 /*loop Unrolling */ 00129 blkCnt = blockSize >> 2u; 00130 00131 /* Reset the input working pointer */ 00132 pSrc = pIn; 00133 00134 /* First part of the processing with loop unrolling. Compute 4 outputs at a time. 00135 ** a second loop below computes the remaining 1 to 3 samples. */ 00136 while(blkCnt > 0u) 00137 { 00138 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ 00139 /* Compute sum of all input values and then store the result in a temporary variable, sum. */ 00140 sum += *pSrc++; 00141 sum += *pSrc++; 00142 sum += *pSrc++; 00143 sum += *pSrc++; 00144 00145 /* Decrement the loop counter */ 00146 blkCnt--; 00147 } 00148 00149 /* If the blockSize is not a multiple of 4, compute any remaining output samples here. 00150 ** No loop unrolling is used. */ 00151 blkCnt = blockSize % 0x4u; 00152 00153 while(blkCnt > 0u) 00154 { 00155 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ 00156 /* Compute sum of all input values and then store the result in a temporary variable, sum. */ 00157 sum += *pSrc++; 00158 00159 /* Decrement the loop counter */ 00160 blkCnt--; 00161 } 00162 /* Compute mean of all input values */ 00163 t = (q15_t) ((1.0 / (blockSize * (blockSize - 1))) * 32768LL); 00164 mean = (q15_t) __SSAT(sum, 16u); 00165 00166 /* Compute square of mean */ 00167 squareOfMean = ((q31_t) mean * mean) >> 15; 00168 squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 15); 00169 00170 /* mean of the squares minus the square of the mean. */ 00171 in1 = (q15_t) (meanOfSquares - squareOfMean); 00172 00173 /* Compute standard deviation and store the result to the destination */ 00174 arm_sqrt_q15(in1, pResult); 00175 00176 #else 00177 00178 /* Run the below code for Cortex-M0 */ 00179 00180 q63_t sumOfSquares = 0; /* Accumulator */ 00181 q15_t in; /* input value */ 00182 /* Loop over blockSize number of values */ 00183 blkCnt = blockSize; 00184 00185 while(blkCnt > 0u) 00186 { 00187 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00188 /* Compute Sum of squares of the input samples 00189 * and then store the result in a temporary variable, sumOfSquares. */ 00190 in = *pSrc++; 00191 sumOfSquares += (in * in); 00192 00193 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ 00194 /* Compute sum of all input values and then store the result in a temporary variable, sum. */ 00195 sum += in; 00196 00197 /* Decrement the loop counter */ 00198 blkCnt--; 00199 } 00200 00201 /* Compute Mean of squares of the input samples 00202 * and then store the result in a temporary variable, meanOfSquares. */ 00203 t = (q15_t) ((1.0 / (blockSize - 1)) * 16384LL); 00204 sumOfSquares = __SSAT((sumOfSquares >> 15u), 16u); 00205 meanOfSquares = (q31_t) ((sumOfSquares * t) >> 14u); 00206 00207 /* Compute mean of all input values */ 00208 mean = (q15_t) __SSAT(sum, 16u); 00209 00210 /* Compute square of mean of the input samples 00211 * and then store the result in a temporary variable, squareOfMean.*/ 00212 t = (q15_t) ((1.0 / (blockSize * (blockSize - 1))) * 32768LL); 00213 squareOfMean = ((q31_t) mean * mean) >> 15; 00214 squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 15); 00215 00216 /* mean of the squares minus the square of the mean. */ 00217 in = (q15_t) (meanOfSquares - squareOfMean); 00218 00219 /* Compute standard deviation and store the result to the destination */ 00220 arm_sqrt_q15(in, pResult); 00221 00222 #endif /* #ifndef ARM_MATH_CM0 */ 00223 00224 00225 } 00226