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