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