Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_rms_q15.c Source File

arm_rms_q15.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_rms_q15.c
00004  * Description:  Root Mean Square of the elements of a Q15 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  * @addtogroup RMS
00033  * @{
00034  */
00035 
00036 /**
00037  * @brief Root Mean Square of the elements of a Q15 vector.
00038  * @param[in]       *pSrc points to the input vector
00039  * @param[in]       blockSize length of the input vector
00040  * @param[out]      *pResult rms value returned here
00041  * @return none.
00042  *
00043  * @details
00044  * <b>Scaling and Overflow Behavior:</b>
00045  *
00046  * \par
00047  * The function is implemented using a 64-bit internal accumulator.
00048  * The input is represented in 1.15 format.
00049  * Intermediate multiplication yields a 2.30 format, and this
00050  * result is added without saturation to a 64-bit accumulator in 34.30 format.
00051  * With 33 guard bits in the accumulator, there is no risk of overflow, and the
00052  * full precision of the intermediate multiplication is preserved.
00053  * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
00054  * 15 bits, and then saturated to yield a result in 1.15 format.
00055  *
00056  */
00057 
00058 void arm_rms_q15(
00059   q15_t * pSrc,
00060   uint32_t blockSize,
00061   q15_t * pResult)
00062 {
00063   q63_t sum = 0;                                 /* accumulator */
00064 
00065 #if defined (ARM_MATH_DSP)
00066   /* Run the below code for Cortex-M4 and Cortex-M3 */
00067 
00068   q31_t in;                                      /* temporary variable to store the input value */
00069   q15_t in1;                                     /* temporary variable to store the input value */
00070   uint32_t blkCnt;                               /* loop counter */
00071 
00072   /* loop Unrolling */
00073   blkCnt = blockSize >> 2U;
00074 
00075   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00076    ** a second loop below computes the remaining 1 to 3 samples. */
00077   while (blkCnt > 0U)
00078   {
00079     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00080     /* Compute sum of the squares and then store the results in a temporary variable, sum */
00081     in = *__SIMD32(pSrc)++;
00082     sum = __SMLALD(in, in, sum);
00083     in = *__SIMD32(pSrc)++;
00084     sum = __SMLALD(in, in, sum);
00085 
00086     /* Decrement the loop counter */
00087     blkCnt--;
00088   }
00089 
00090   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00091    ** No loop unrolling is used. */
00092   blkCnt = blockSize % 0x4U;
00093 
00094   while (blkCnt > 0U)
00095   {
00096     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00097     /* Compute sum of the squares and then store the results in a temporary variable, sum */
00098     in1 = *pSrc++;
00099     sum = __SMLALD(in1, in1, sum);
00100 
00101     /* Decrement the loop counter */
00102     blkCnt--;
00103   }
00104 
00105   /* Truncating and saturating the accumulator to 1.15 format */
00106   /* Store the result in the destination */
00107   arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult);
00108 
00109 #else
00110   /* Run the below code for Cortex-M0 */
00111 
00112   q15_t in;                                      /* temporary variable to store the input value */
00113   uint32_t blkCnt;                               /* loop counter */
00114 
00115   /* Loop over blockSize number of values */
00116   blkCnt = blockSize;
00117 
00118   while (blkCnt > 0U)
00119   {
00120     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00121     /* Compute sum of the squares and then store the results in a temporary variable, sum */
00122     in = *pSrc++;
00123     sum += ((q31_t) in * in);
00124 
00125     /* Decrement the loop counter */
00126     blkCnt--;
00127   }
00128 
00129   /* Truncating and saturating the accumulator to 1.15 format */
00130   /* Store the result in the destination */
00131   arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult);
00132 
00133 #endif /* #if defined (ARM_MATH_DSP) */
00134 
00135 }
00136 
00137 /**
00138  * @} end of RMS group
00139  */
00140