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 
46   @par           Scaling and Overflow Behavior
47                    The function is implemented using an internal 64-bit accumulator.
48                    The input is represented in 1.31 format, and intermediate multiplication
49                    yields a 2.62 format.
50                    The accumulator maintains full precision of the intermediate multiplication results,
51                    but provides only a single guard bit.
52                    There is no saturation on intermediate additions.
53                    If the accumulator overflows, it wraps around and distorts the result.
54                    In order to avoid overflows completely, the input signal must be scaled down by
55                    log2(blockSize) bits, as a total of blockSize additions are performed internally.
56                    Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value.
57  */
58 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
59 
arm_rms_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)60 ARM_DSP_ATTRIBUTE void arm_rms_q31(
61   const q31_t * pSrc,
62         uint32_t blockSize,
63         q31_t * pResult)
64 {
65     q63_t pow = 0.0f;
66     q31_t normalizedPower;
67     arm_power_q31(pSrc, blockSize, &pow);
68 
69     normalizedPower=clip_q63_to_q31((pow / (q63_t) blockSize) >> 17);
70     arm_sqrt_q31(normalizedPower, pResult);
71 
72 }
73 
74 #else
arm_rms_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)75 ARM_DSP_ATTRIBUTE void arm_rms_q31(
76   const q31_t * pSrc,
77         uint32_t blockSize,
78         q31_t * pResult)
79 {
80         uint32_t blkCnt;                               /* Loop counter */
81         uint64_t sum = 0;                              /* Temporary result storage (can get never negative. changed type from q63 to uint64 */
82         q31_t in;                                      /* Temporary variable to store input value */
83 
84 #if defined (ARM_MATH_LOOPUNROLL)
85 
86   /* Loop unrolling: Compute 4 outputs at a time */
87   blkCnt = blockSize >> 2U;
88 
89   while (blkCnt > 0U)
90   {
91     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
92 
93     in = *pSrc++;
94     /* Compute sum of squares and store result in a temporary variable, sum. */
95     sum += ((q63_t) in * in);
96 
97     in = *pSrc++;
98     sum += ((q63_t) in * in);
99 
100     in = *pSrc++;
101     sum += ((q63_t) in * in);
102 
103     in = *pSrc++;
104     sum += ((q63_t) in * in);
105 
106     /* Decrement loop counter */
107     blkCnt--;
108   }
109 
110   /* Loop unrolling: Compute remaining outputs */
111   blkCnt = blockSize % 0x4U;
112 
113 #else
114 
115   /* Initialize blkCnt with number of samples */
116   blkCnt = blockSize;
117 
118 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
119 
120   while (blkCnt > 0U)
121   {
122     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
123 
124     in = *pSrc++;
125     /* Compute sum of squares and store result in a temporary variable. */
126     sum += ((q63_t) in * in);
127 
128     /* Decrement loop counter */
129     blkCnt--;
130   }
131 
132   /* Convert data in 2.62 to 1.31 by 31 right shifts and saturate */
133   /* Compute Rms and store result in destination vector */
134   arm_sqrt_q31(clip_q63_to_q31((sum / (q63_t) blockSize) >> 31), pResult);
135 }
136 #endif /* defined(ARM_MATH_MVEI) */
137 
138 /**
139   @} end of RMS group
140  */
141