CMSIS DSP library

Dependents:   KL25Z_FFT_Demo Hat_Board_v5_1 KL25Z_FFT_Demo_tony KL25Z_FFT_Demo_tony ... more

Fork of mbed-dsp by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_var_f32.c Source File

arm_var_f32.c

00001 /* ----------------------------------------------------------------------    
00002 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.    
00003 *    
00004 * $Date:        17. January 2013
00005 * $Revision:    V1.4.1  
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   /*loop Unrolling */
00098   blkCnt = blockSize >> 2u;
00099 
00100   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.    
00101    ** a second loop below computes the remaining 1 to 3 samples. */
00102   while(blkCnt > 0u)
00103   {
00104     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1])  */
00105     /* Compute Sum of squares of the input samples    
00106      * and then store the result in a temporary variable, sum. */
00107     in = *pSrc++;
00108     sum += in;
00109     sumOfSquares += in * in;
00110     in = *pSrc++;
00111     sum += in;
00112     sumOfSquares += in * in;
00113     in = *pSrc++;
00114     sum += in;
00115     sumOfSquares += in * in;
00116     in = *pSrc++;
00117     sum += in;
00118     sumOfSquares += in * in;
00119 
00120     /* Decrement the loop counter */
00121     blkCnt--;
00122   }
00123 
00124   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.    
00125    ** No loop unrolling is used. */
00126   blkCnt = blockSize % 0x4u;
00127 
00128   while(blkCnt > 0u)
00129   {
00130     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00131     /* Compute Sum of squares of the input samples    
00132      * and then store the result in a temporary variable, sum. */
00133     in = *pSrc++;
00134     sum += in;
00135     sumOfSquares += in * in;
00136 
00137     /* Decrement the loop counter */
00138     blkCnt--;
00139   }
00140 
00141   /* Compute Mean of squares of the input samples    
00142    * and then store the result in a temporary variable, meanOfSquares. */
00143   meanOfSquares = sumOfSquares / ((float32_t) blockSize - 1.0f);
00144 
00145   /* Compute mean of all input values */
00146   mean = sum / (float32_t) blockSize;
00147 
00148   /* Compute square of mean */
00149   squareOfMean = (mean * mean) * (((float32_t) blockSize) /
00150                                   ((float32_t) blockSize - 1.0f));
00151 
00152   /* Compute variance and then store the result to the destination */
00153   *pResult = meanOfSquares - squareOfMean;
00154 
00155 #else
00156 
00157   /* Run the below code for Cortex-M0 */
00158   float32_t squareOfSum;                         /* Square of Sum */
00159 
00160   /* Loop over blockSize number of values */
00161   blkCnt = blockSize;
00162 
00163   while(blkCnt > 0u)
00164   {
00165     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00166     /* Compute Sum of squares of the input samples     
00167      * and then store the result in a temporary variable, sumOfSquares. */
00168     in = *pSrc++;
00169     sumOfSquares += in * in;
00170 
00171     /* C = (A[0] + A[1] + ... + A[blockSize-1]) */
00172     /* Compute Sum of the input samples     
00173      * and then store the result in a temporary variable, sum. */
00174     sum += in;
00175 
00176     /* Decrement the loop counter */
00177     blkCnt--;
00178   }
00179 
00180   /* Compute the square of sum */
00181   squareOfSum = ((sum * sum) / (float32_t) blockSize);
00182 
00183   /* Compute the variance */
00184   *pResult = ((sumOfSquares - squareOfSum) / (float32_t) (blockSize - 1.0f));
00185 
00186 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
00187 
00188 }
00189 
00190 /**    
00191  * @} end of variance group    
00192  */