Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_rms_f32.c Source File

arm_rms_f32.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_rms_f32.c
00004  * Description:  Root mean square value of an array of F32 type
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 RMS Root mean square (RMS)
00037  *
00038  *
00039  * Calculates the Root Mean Sqaure of the elements in the input vector.
00040  * The underlying algorithm is used:
00041  *
00042  * <pre>
00043  *  Result = sqrt(((pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]) / blockSize));
00044  * </pre>
00045  *
00046  * There are separate functions for floating point, Q31, and Q15 data types.
00047  */
00048 
00049 /**
00050  * @addtogroup RMS
00051  * @{
00052  */
00053 
00054 
00055 /**
00056  * @brief Root Mean Square of the elements of a floating-point vector.
00057  * @param[in]       *pSrc points to the input vector
00058  * @param[in]       blockSize length of the input vector
00059  * @param[out]      *pResult rms value returned here
00060  * @return none.
00061  *
00062  */
00063 
00064 void arm_rms_f32(
00065   float32_t * pSrc,
00066   uint32_t blockSize,
00067   float32_t * pResult)
00068 {
00069   float32_t sum = 0.0f;                          /* Accumulator */
00070   float32_t in;                                  /* Tempoprary variable to store input value */
00071   uint32_t blkCnt;                               /* loop counter */
00072 
00073 #if defined (ARM_MATH_DSP)
00074   /* Run the below code for Cortex-M4 and Cortex-M3 */
00075 
00076   /* loop Unrolling */
00077   blkCnt = blockSize >> 2U;
00078 
00079   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00080    ** a second loop below computes the remaining 1 to 3 samples. */
00081   while (blkCnt > 0U)
00082   {
00083     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
00084     /* Compute sum of the squares and then store the result in a temporary variable, sum  */
00085     in = *pSrc++;
00086     sum += in * in;
00087     in = *pSrc++;
00088     sum += in * in;
00089     in = *pSrc++;
00090     sum += in * in;
00091     in = *pSrc++;
00092     sum += in * in;
00093 
00094     /* Decrement the loop counter */
00095     blkCnt--;
00096   }
00097 
00098   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00099    ** No loop unrolling is used. */
00100   blkCnt = blockSize % 0x4U;
00101 
00102 #else
00103   /* Run the below code for Cortex-M0 */
00104 
00105   /* Loop over blockSize number of values */
00106   blkCnt = blockSize;
00107 
00108 #endif /* #if defined (ARM_MATH_DSP) */
00109 
00110   while (blkCnt > 0U)
00111   {
00112     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
00113     /* Compute sum of the squares and then store the results in a temporary variable, sum  */
00114     in = *pSrc++;
00115     sum += in * in;
00116 
00117     /* Decrement the loop counter */
00118     blkCnt--;
00119   }
00120 
00121   /* Compute Rms and store the result in the destination */
00122   arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
00123 }
00124 
00125 /**
00126  * @} end of RMS group
00127  */
00128