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