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