Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_var_f32.c Source File

arm_var_f32.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_var_f32.c
00004  * Description:  Variance of the elements of a floating-point vector
00005  *
00006  * $Date:        27. January 2017
00007  * $Revision:    V.1.5.1
00008  *
00009  * Target Processor: Cortex-M cores
00010  * -------------------------------------------------------------------- */
00011 /*
00012  * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
00013  *
00014  * SPDX-License-Identifier: Apache-2.0
00015  *
00016  * Licensed under the Apache License, Version 2.0 (the License); you may
00017  * not use this file except in compliance with the License.
00018  * You may obtain a copy of the License at
00019  *
00020  * www.apache.org/licenses/LICENSE-2.0
00021  *
00022  * Unless required by applicable law or agreed to in writing, software
00023  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00024  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00025  * See the License for the specific language governing permissions and
00026  * limitations under the License.
00027  */
00028 
00029 #include "arm_math.h"
00030 
00031 /**
00032  * @ingroup groupStats
00033  */
00034 
00035 /**
00036  * @defgroup variance  Variance
00037  *
00038  * Calculates the variance of the elements in the input vector.
00039  * The underlying algorithm used is the direct method sometimes referred to as the two-pass method:
00040  *
00041  * <pre>
00042  *   Result = sum(element - meanOfElements)^2) / numElement - 1
00043  *
00044  *     where, meanOfElements = ( pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] ) / blockSize
00045  *
00046  * </pre>
00047  *
00048  * There are separate functions for floating point, Q31, and Q15 data types.
00049  */
00050 
00051 /**
00052  * @addtogroup variance
00053  * @{
00054  */
00055 
00056 
00057 /**
00058  * @brief Variance of the elements of a floating-point vector.
00059  * @param[in]       *pSrc points to the input vector
00060  * @param[in]       blockSize length of the input vector
00061  * @param[out]      *pResult variance value returned here
00062  * @return none.
00063  */
00064 
00065 void arm_var_f32(
00066                  float32_t * pSrc,
00067                  uint32_t blockSize,
00068                  float32_t * pResult)
00069 {
00070     float32_t fMean, fValue;
00071     uint32_t blkCnt;            /* loop counter */
00072     float32_t * pInput = pSrc;
00073     float32_t sum = 0.0f;
00074     float32_t fSum = 0.0f;
00075     #if defined(ARM_MATH_DSP)
00076     float32_t in1, in2, in3, in4;
00077     #endif
00078 
00079     if (blockSize <= 1U)
00080     {
00081         *pResult = 0;
00082         return;
00083     }
00084 
00085     #if defined(ARM_MATH_DSP)
00086         /* Run the below code for Cortex-M4 and Cortex-M7 */
00087 
00088         /*loop Unrolling */
00089         blkCnt = blockSize >> 2U;
00090 
00091         /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00092         ** a second loop below computes the remaining 1 to 3 samples. */
00093         while (blkCnt > 0U)
00094         {
00095             /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
00096             in1 = *pInput++;
00097             in2 = *pInput++;
00098             in3 = *pInput++;
00099             in4 = *pInput++;
00100 
00101             sum += in1;
00102             sum += in2;
00103             sum += in3;
00104             sum += in4;
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     #else
00115         /* Run the below code for Cortex-M0 or Cortex-M3 */
00116 
00117         /* Loop over blockSize number of values */
00118         blkCnt = blockSize;
00119 
00120     #endif
00121 
00122     while (blkCnt > 0U)
00123     {
00124         /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
00125         sum += *pInput++;
00126 
00127         /* Decrement the loop counter */
00128         blkCnt--;
00129     }
00130 
00131     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize  */
00132     fMean = sum / (float32_t) blockSize;
00133 
00134     pInput = pSrc;
00135 
00136     #if defined(ARM_MATH_DSP)
00137 
00138         /*loop Unrolling */
00139         blkCnt = blockSize >> 2U;
00140 
00141         /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00142         ** a second loop below computes the remaining 1 to 3 samples. */
00143         while (blkCnt > 0U)
00144         {
00145             fValue = *pInput++ - fMean;
00146             fSum += fValue * fValue;
00147             fValue = *pInput++ - fMean;
00148             fSum += fValue * fValue;
00149             fValue = *pInput++ - fMean;
00150             fSum += fValue * fValue;
00151             fValue = *pInput++ - fMean;
00152             fSum += fValue * fValue;
00153 
00154             /* Decrement the loop counter */
00155             blkCnt--;
00156         }
00157 
00158         blkCnt = blockSize % 0x4U;
00159     #else
00160         /* Run the below code for Cortex-M0 or Cortex-M3 */
00161 
00162         /* Loop over blockSize number of values */
00163         blkCnt = blockSize;
00164     #endif
00165 
00166     while (blkCnt > 0U)
00167     {
00168         fValue = *pInput++ - fMean;
00169         fSum += fValue * fValue;
00170 
00171         /* Decrement the loop counter */
00172         blkCnt--;
00173     }
00174 
00175     /* Variance */
00176     *pResult = fSum / (float32_t)(blockSize - 1.0f);
00177 }
00178 
00179 /**
00180  * @} end of variance group
00181  */
00182