Aded CMSIS5 DSP and NN folder. Needs some work

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_std_f32.c Source File

arm_std_f32.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_std_f32.c
00004  * Description:  Standard deviation 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 STD Standard deviation
00037  *
00038  * Calculates the standard deviation of the elements in the input vector.
00039  * The underlying algorithm is used:
00040  *
00041  * <pre>
00042  *   Result = sqrt((sumOfSquares - sum<sup>2</sup> / blockSize) / (blockSize - 1))
00043  *
00044  *     where, sumOfSquares = pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]
00045  *
00046  *                     sum = pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1]
00047  * </pre>
00048  *
00049  * There are separate functions for floating point, Q31, and Q15 data types.
00050  */
00051 
00052 /**
00053  * @addtogroup STD
00054  * @{
00055  */
00056 
00057 
00058 /**
00059  * @brief Standard deviation of the elements of a floating-point vector.
00060  * @param[in]       *pSrc points to the input vector
00061  * @param[in]       blockSize length of the input vector
00062  * @param[out]      *pResult standard deviation value returned here
00063  * @return none.
00064  */
00065 
00066 void arm_std_f32(
00067   float32_t * pSrc,
00068   uint32_t blockSize,
00069   float32_t * pResult)
00070 {
00071   float32_t sum = 0.0f;                          /* Temporary result storage */
00072   float32_t sumOfSquares = 0.0f;                 /* Sum of squares */
00073   float32_t in;                                  /* input value */
00074   uint32_t blkCnt;                               /* loop counter */
00075 #if defined (ARM_MATH_DSP)
00076   float32_t meanOfSquares, mean, squareOfMean;   /* Temporary variables */
00077 #else
00078   float32_t squareOfSum;                         /* Square of Sum */
00079   float32_t var;                                 /* Temporary varaince storage */
00080 #endif
00081 
00082   if (blockSize == 1U)
00083   {
00084     *pResult = 0;
00085     return;
00086   }
00087 
00088 #if defined (ARM_MATH_DSP)
00089   /* Run the below code for Cortex-M4 and Cortex-M3 */
00090 
00091   /*loop Unrolling */
00092   blkCnt = blockSize >> 2U;
00093 
00094   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00095    ** a second loop below computes the remaining 1 to 3 samples. */
00096   while (blkCnt > 0U)
00097   {
00098     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1])  */
00099     /* Compute Sum of squares of the input samples
00100      * and then store the result in a temporary variable, sum. */
00101     in = *pSrc++;
00102     sum += in;
00103     sumOfSquares += in * in;
00104     in = *pSrc++;
00105     sum += in;
00106     sumOfSquares += in * in;
00107     in = *pSrc++;
00108     sum += in;
00109     sumOfSquares += in * in;
00110     in = *pSrc++;
00111     sum += in;
00112     sumOfSquares += in * in;
00113 
00114     /* Decrement the loop counter */
00115     blkCnt--;
00116   }
00117 
00118   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00119    ** No loop unrolling is used. */
00120   blkCnt = blockSize % 0x4U;
00121 
00122   while (blkCnt > 0U)
00123   {
00124     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00125     /* Compute Sum of squares of the input samples
00126      * and then store the result in a temporary variable, sum. */
00127     in = *pSrc++;
00128     sum += in;
00129     sumOfSquares += in * in;
00130 
00131     /* Decrement the loop counter */
00132     blkCnt--;
00133   }
00134 
00135   /* Compute Mean of squares of the input samples
00136    * and then store the result in a temporary variable, meanOfSquares. */
00137   meanOfSquares = sumOfSquares / ((float32_t) blockSize - 1.0f);
00138 
00139   /* Compute mean of all input values */
00140   mean = sum / (float32_t) blockSize;
00141 
00142   /* Compute square of mean */
00143   squareOfMean = (mean * mean) * (((float32_t) blockSize) /
00144                                   ((float32_t) blockSize - 1.0f));
00145 
00146   /* Compute standard deviation and then store the result to the destination */
00147   arm_sqrt_f32((meanOfSquares - squareOfMean), pResult);
00148 
00149 #else
00150   /* Run the below code for Cortex-M0 */
00151 
00152   /* Loop over blockSize number of values */
00153   blkCnt = blockSize;
00154 
00155   while (blkCnt > 0U)
00156   {
00157     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00158     /* Compute Sum of squares of the input samples
00159      * and then store the result in a temporary variable, sumOfSquares. */
00160     in = *pSrc++;
00161     sumOfSquares += in * in;
00162 
00163     /* C = (A[0] + A[1] + ... + A[blockSize-1]) */
00164     /* Compute Sum of the input samples
00165      * and then store the result in a temporary variable, sum. */
00166     sum += in;
00167 
00168     /* Decrement the loop counter */
00169     blkCnt--;
00170   }
00171 
00172   /* Compute the square of sum */
00173   squareOfSum = ((sum * sum) / (float32_t) blockSize);
00174 
00175   /* Compute the variance */
00176   var = ((sumOfSquares - squareOfSum) / (float32_t) (blockSize - 1.0f));
00177 
00178   /* Compute standard deviation and then store the result to the destination */
00179   arm_sqrt_f32(var, pResult);
00180 
00181 #endif /* #if defined (ARM_MATH_DSP) */
00182 }
00183 
00184 /**
00185  * @} end of STD group
00186  */
00187