CMSIS DSP library
Dependents: performance_timer Surfboard_ gps2rtty Capstone ... more
arm_var_f32.c
00001 /* ---------------------------------------------------------------------- 00002 * Copyright (C) 2010-2014 ARM Limited. All rights reserved. 00003 * 00004 * $Date: 19. March 2015 00005 * $Revision: V.1.4.5 00006 * 00007 * Project: CMSIS DSP Library 00008 * Title: arm_var_f32.c 00009 * 00010 * Description: Variance of the elements of a floating-point vector. 00011 * 00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions 00016 * are met: 00017 * - Redistributions of source code must retain the above copyright 00018 * notice, this list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright 00020 * notice, this list of conditions and the following disclaimer in 00021 * the documentation and/or other materials provided with the 00022 * distribution. 00023 * - Neither the name of ARM LIMITED nor the names of its contributors 00024 * may be used to endorse or promote products derived from this 00025 * software without specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00028 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00029 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00030 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00031 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00033 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00034 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00036 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00037 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00038 * POSSIBILITY OF SUCH DAMAGE. 00039 * ---------------------------------------------------------------------------- */ 00040 00041 #include "arm_math.h" 00042 00043 /** 00044 * @ingroup groupStats 00045 */ 00046 00047 /** 00048 * @defgroup variance Variance 00049 * 00050 * Calculates the variance of the elements in the input vector. 00051 * The underlying algorithm is used: 00052 * 00053 * <pre> 00054 * Result = (sumOfSquares - sum<sup>2</sup> / blockSize) / (blockSize - 1) 00055 * 00056 * where, sumOfSquares = pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1] 00057 * 00058 * sum = pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1] 00059 * </pre> 00060 * 00061 * There are separate functions for floating point, Q31, and Q15 data types. 00062 */ 00063 00064 /** 00065 * @addtogroup variance 00066 * @{ 00067 */ 00068 00069 00070 /** 00071 * @brief Variance of the elements of a floating-point vector. 00072 * @param[in] *pSrc points to the input vector 00073 * @param[in] blockSize length of the input vector 00074 * @param[out] *pResult variance value returned here 00075 * @return none. 00076 * 00077 */ 00078 00079 00080 void arm_var_f32( 00081 float32_t * pSrc, 00082 uint32_t blockSize, 00083 float32_t * pResult) 00084 { 00085 00086 float32_t sum = 0.0f; /* Temporary result storage */ 00087 float32_t sumOfSquares = 0.0f; /* Sum of squares */ 00088 float32_t in; /* input value */ 00089 uint32_t blkCnt; /* loop counter */ 00090 00091 #ifndef ARM_MATH_CM0_FAMILY 00092 00093 /* Run the below code for Cortex-M4 and Cortex-M3 */ 00094 00095 float32_t meanOfSquares, mean, squareOfMean; /* Temporary variables */ 00096 00097 if(blockSize == 1) 00098 { 00099 *pResult = 0; 00100 return; 00101 } 00102 00103 /*loop Unrolling */ 00104 blkCnt = blockSize >> 2u; 00105 00106 /* First part of the processing with loop unrolling. Compute 4 outputs at a time. 00107 ** a second loop below computes the remaining 1 to 3 samples. */ 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 in = *pSrc++; 00114 sum += in; 00115 sumOfSquares += in * in; 00116 in = *pSrc++; 00117 sum += in; 00118 sumOfSquares += in * in; 00119 in = *pSrc++; 00120 sum += in; 00121 sumOfSquares += in * in; 00122 in = *pSrc++; 00123 sum += in; 00124 sumOfSquares += in * in; 00125 00126 /* Decrement the loop counter */ 00127 blkCnt--; 00128 } 00129 00130 /* If the blockSize is not a multiple of 4, compute any remaining output samples here. 00131 ** No loop unrolling is used. */ 00132 blkCnt = blockSize % 0x4u; 00133 00134 while(blkCnt > 0u) 00135 { 00136 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00137 /* Compute Sum of squares of the input samples 00138 * and then store the result in a temporary variable, sum. */ 00139 in = *pSrc++; 00140 sum += in; 00141 sumOfSquares += in * in; 00142 00143 /* Decrement the loop counter */ 00144 blkCnt--; 00145 } 00146 00147 /* Compute Mean of squares of the input samples 00148 * and then store the result in a temporary variable, meanOfSquares. */ 00149 meanOfSquares = sumOfSquares / ((float32_t) blockSize - 1.0f); 00150 00151 /* Compute mean of all input values */ 00152 mean = sum / (float32_t) blockSize; 00153 00154 /* Compute square of mean */ 00155 squareOfMean = (mean * mean) * (((float32_t) blockSize) / 00156 ((float32_t) blockSize - 1.0f)); 00157 00158 /* Compute variance and then store the result to the destination */ 00159 *pResult = meanOfSquares - squareOfMean; 00160 00161 #else 00162 00163 /* Run the below code for Cortex-M0 */ 00164 float32_t squareOfSum; /* Square of Sum */ 00165 00166 if(blockSize == 1) 00167 { 00168 *pResult = 0; 00169 return; 00170 } 00171 00172 /* Loop over blockSize number of values */ 00173 blkCnt = blockSize; 00174 00175 while(blkCnt > 0u) 00176 { 00177 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00178 /* Compute Sum of squares of the input samples 00179 * and then store the result in a temporary variable, sumOfSquares. */ 00180 in = *pSrc++; 00181 sumOfSquares += in * in; 00182 00183 /* C = (A[0] + A[1] + ... + A[blockSize-1]) */ 00184 /* Compute Sum of the input samples 00185 * and then store the result in a temporary variable, sum. */ 00186 sum += in; 00187 00188 /* Decrement the loop counter */ 00189 blkCnt--; 00190 } 00191 00192 /* Compute the square of sum */ 00193 squareOfSum = ((sum * sum) / (float32_t) blockSize); 00194 00195 /* Compute the variance */ 00196 *pResult = ((sumOfSquares - squareOfSum) / (float32_t) (blockSize - 1.0f)); 00197 00198 #endif /* #ifndef ARM_MATH_CM0_FAMILY */ 00199 00200 } 00201 00202 /** 00203 * @} end of variance group 00204 */
Generated on Tue Jul 12 2022 11:59:19 by 1.7.2