• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_rms_q31.c
4  * Description:  Root Mean Square of the elements of a Q31 vector
5  *
6  * $Date:        23 April 2021
7  * $Revision:    V1.9.0
8  *
9  * Target Processor: Cortex-M and Cortex-A cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2021 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 "dsp/statistics_functions.h"
30 
31 /**
32   @ingroup groupStats
33  */
34 
35 /**
36   @addtogroup RMS
37   @{
38  */
39 
40 /**
41   @brief         Root Mean Square of the elements of a Q31 vector.
42   @param[in]     pSrc       points to the input vector
43   @param[in]     blockSize  number of samples in input vector
44   @param[out]    pResult    root mean square value returned here
45   @return        none
46 
47   @par           Scaling and Overflow Behavior
48                    The function is implemented using an internal 64-bit accumulator.
49                    The input is represented in 1.31 format, and intermediate multiplication
50                    yields a 2.62 format.
51                    The accumulator maintains full precision of the intermediate multiplication results,
52                    but provides only a single guard bit.
53                    There is no saturation on intermediate additions.
54                    If the accumulator overflows, it wraps around and distorts the result.
55                    In order to avoid overflows completely, the input signal must be scaled down by
56                    log2(blockSize) bits, as a total of blockSize additions are performed internally.
57                    Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value.
58  */
59 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
60 
arm_rms_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)61 void arm_rms_q31(
62   const q31_t * pSrc,
63         uint32_t blockSize,
64         q31_t * pResult)
65 {
66     q63_t pow = 0.0f;
67     q31_t normalizedPower;
68     arm_power_q31(pSrc, blockSize, &pow);
69 
70     normalizedPower=clip_q63_to_q31((pow / (q63_t) blockSize) >> 17);
71     arm_sqrt_q31(normalizedPower, pResult);
72 
73 }
74 
75 #else
arm_rms_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)76 void arm_rms_q31(
77   const q31_t * pSrc,
78         uint32_t blockSize,
79         q31_t * pResult)
80 {
81         uint32_t blkCnt;                               /* Loop counter */
82         uint64_t sum = 0;                              /* Temporary result storage (can get never negative. changed type from q63 to uint64 */
83         q31_t in;                                      /* Temporary variable to store input value */
84 
85 #if defined (ARM_MATH_LOOPUNROLL)
86 
87   /* Loop unrolling: Compute 4 outputs at a time */
88   blkCnt = blockSize >> 2U;
89 
90   while (blkCnt > 0U)
91   {
92     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
93 
94     in = *pSrc++;
95     /* Compute sum of squares and store result in a temporary variable, sum. */
96     sum += ((q63_t) in * in);
97 
98     in = *pSrc++;
99     sum += ((q63_t) in * in);
100 
101     in = *pSrc++;
102     sum += ((q63_t) in * in);
103 
104     in = *pSrc++;
105     sum += ((q63_t) in * in);
106 
107     /* Decrement loop counter */
108     blkCnt--;
109   }
110 
111   /* Loop unrolling: Compute remaining outputs */
112   blkCnt = blockSize % 0x4U;
113 
114 #else
115 
116   /* Initialize blkCnt with number of samples */
117   blkCnt = blockSize;
118 
119 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
120 
121   while (blkCnt > 0U)
122   {
123     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
124 
125     in = *pSrc++;
126     /* Compute sum of squares and store result in a temporary variable. */
127     sum += ((q63_t) in * in);
128 
129     /* Decrement loop counter */
130     blkCnt--;
131   }
132 
133   /* Convert data in 2.62 to 1.31 by 31 right shifts and saturate */
134   /* Compute Rms and store result in destination vector */
135   arm_sqrt_q31(clip_q63_to_q31((sum / (q63_t) blockSize) >> 31), pResult);
136 }
137 #endif /* defined(ARM_MATH_MVEI) */
138 
139 /**
140   @} end of RMS group
141  */
142