Subversion Repositories dashGPS

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* ----------------------------------------------------------------------
  2.  * Project:      CMSIS DSP Library
  3.  * Title:        arm_rms_q15.c
  4.  * Description:  Root Mean Square of the elements of a Q15 vector
  5.  *
  6.  * $Date:        27. January 2017
  7.  * $Revision:    V.1.5.1
  8.  *
  9.  * Target Processor: Cortex-M cores
  10.  * -------------------------------------------------------------------- */
  11. /*
  12.  * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
  13.  *
  14.  * SPDX-License-Identifier: Apache-2.0
  15.  *
  16.  * Licensed under the Apache License, Version 2.0 (the License); you may
  17.  * not use this file except in compliance with the License.
  18.  * You may obtain a copy of the License at
  19.  *
  20.  * www.apache.org/licenses/LICENSE-2.0
  21.  *
  22.  * Unless required by applicable law or agreed to in writing, software
  23.  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25.  * See the License for the specific language governing permissions and
  26.  * limitations under the License.
  27.  */
  28.  
  29. #include "arm_math.h"
  30.  
  31. /**
  32.  * @addtogroup RMS
  33.  * @{
  34.  */
  35.  
  36. /**
  37.  * @brief Root Mean Square of the elements of a Q15 vector.
  38.  * @param[in]       *pSrc points to the input vector
  39.  * @param[in]       blockSize length of the input vector
  40.  * @param[out]      *pResult rms value returned here
  41.  * @return none.
  42.  *
  43.  * @details
  44.  * <b>Scaling and Overflow Behavior:</b>
  45.  *
  46.  * \par
  47.  * The function is implemented using a 64-bit internal accumulator.
  48.  * The input is represented in 1.15 format.
  49.  * Intermediate multiplication yields a 2.30 format, and this
  50.  * result is added without saturation to a 64-bit accumulator in 34.30 format.
  51.  * With 33 guard bits in the accumulator, there is no risk of overflow, and the
  52.  * full precision of the intermediate multiplication is preserved.
  53.  * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
  54.  * 15 bits, and then saturated to yield a result in 1.15 format.
  55.  *
  56.  */
  57.  
  58. void arm_rms_q15(
  59.   q15_t * pSrc,
  60.   uint32_t blockSize,
  61.   q15_t * pResult)
  62. {
  63.   q63_t sum = 0;                                 /* accumulator */
  64.  
  65. #if defined (ARM_MATH_DSP)
  66.   /* Run the below code for Cortex-M4 and Cortex-M3 */
  67.  
  68.   q31_t in;                                      /* temporary variable to store the input value */
  69.   q15_t in1;                                     /* temporary variable to store the input value */
  70.   uint32_t blkCnt;                               /* loop counter */
  71.  
  72.   /* loop Unrolling */
  73.   blkCnt = blockSize >> 2U;
  74.  
  75.   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  76.    ** a second loop below computes the remaining 1 to 3 samples. */
  77.   while (blkCnt > 0U)
  78.   {
  79.     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  80.     /* Compute sum of the squares and then store the results in a temporary variable, sum */
  81.     in = *__SIMD32(pSrc)++;
  82.     sum = __SMLALD(in, in, sum);
  83.     in = *__SIMD32(pSrc)++;
  84.     sum = __SMLALD(in, in, sum);
  85.  
  86.     /* Decrement the loop counter */
  87.     blkCnt--;
  88.   }
  89.  
  90.   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  91.    ** No loop unrolling is used. */
  92.   blkCnt = blockSize % 0x4U;
  93.  
  94.   while (blkCnt > 0U)
  95.   {
  96.     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  97.     /* Compute sum of the squares and then store the results in a temporary variable, sum */
  98.     in1 = *pSrc++;
  99.     sum = __SMLALD(in1, in1, sum);
  100.  
  101.     /* Decrement the loop counter */
  102.     blkCnt--;
  103.   }
  104.  
  105.   /* Truncating and saturating the accumulator to 1.15 format */
  106.   /* Store the result in the destination */
  107.   arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult);
  108.  
  109. #else
  110.   /* Run the below code for Cortex-M0 */
  111.  
  112.   q15_t in;                                      /* temporary variable to store the input value */
  113.   uint32_t blkCnt;                               /* loop counter */
  114.  
  115.   /* Loop over blockSize number of values */
  116.   blkCnt = blockSize;
  117.  
  118.   while (blkCnt > 0U)
  119.   {
  120.     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
  121.     /* Compute sum of the squares and then store the results in a temporary variable, sum */
  122.     in = *pSrc++;
  123.     sum += ((q31_t) in * in);
  124.  
  125.     /* Decrement the loop counter */
  126.     blkCnt--;
  127.   }
  128.  
  129.   /* Truncating and saturating the accumulator to 1.15 format */
  130.   /* Store the result in the destination */
  131.   arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult);
  132.  
  133. #endif /* #if defined (ARM_MATH_DSP) */
  134.  
  135. }
  136.  
  137. /**
  138.  * @} end of RMS group
  139.  */
  140.