1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_rms_f32.c
4  * Description:  Root mean square value of the elements of a floating-point 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   @defgroup RMS Root mean square (RMS)
37 
38   Calculates the Root Mean Square of the elements in the input vector.
39   The underlying algorithm is used:
40 
41   <pre>
42       Result = sqrt(((pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]) / blockSize));
43   </pre>
44 
45   There are separate functions for floating point, Q31, and Q15 data types.
46  */
47 
48 /**
49   @addtogroup RMS
50   @{
51  */
52 
53 /**
54   @brief         Root Mean Square of the elements of a floating-point vector.
55   @param[in]     pSrc       points to the input vector
56   @param[in]     blockSize  number of samples in input vector
57   @param[out]    pResult    root mean square value returned here
58   @return        none
59  */
60 
61 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_rms_f32(const float32_t * pSrc,uint32_t blockSize,float32_t * pResult)62 void arm_rms_f32(
63   const float32_t * pSrc,
64   uint32_t blockSize,
65   float32_t * pResult)
66 {
67     float32_t pow = 0.0f;
68 
69     arm_power_f32(pSrc, blockSize, &pow);
70 
71     /* Compute Rms and store the result in the destination */
72     arm_sqrt_f32(pow / (float32_t) blockSize, pResult);
73 }
74 #else
75 #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_rms_f32(const float32_t * pSrc,uint32_t blockSize,float32_t * pResult)76 void arm_rms_f32(
77   const float32_t * pSrc,
78   uint32_t blockSize,
79   float32_t * pResult)
80 {
81   float32_t sum = 0.0f;                          /* accumulator */
82   float32_t in;                                  /* Temporary variable to store input value */
83   uint32_t blkCnt;                               /* loop counter */
84 
85   float32x4_t sumV = vdupq_n_f32(0.0f);                          /* Temporary result storage */
86   float32x2_t sumV2;
87   float32x4_t inV;
88 
89   blkCnt = blockSize >> 2U;
90 
91   /* Compute 4 outputs at a time.
92    ** a second loop below computes the remaining 1 to 3 samples. */
93   while (blkCnt > 0U)
94   {
95     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
96     /* Compute Power and then store the result in a temporary variable, sum. */
97     inV = vld1q_f32(pSrc);
98     sumV = vmlaq_f32(sumV, inV, inV);
99     pSrc += 4;
100 
101     /* Decrement the loop counter */
102     blkCnt--;
103   }
104 
105   sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
106   sum = vget_lane_f32(sumV2, 0) + vget_lane_f32(sumV2, 1);
107 
108   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
109    ** No loop unrolling is used. */
110   blkCnt = blockSize % 0x4U;
111 
112   while (blkCnt > 0U)
113   {
114     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
115     /* compute power and then store the result in a temporary variable, sum. */
116     in = *pSrc++;
117     sum += in * in;
118 
119     /* Decrement the loop counter */
120     blkCnt--;
121   }
122 
123   /* Compute Rms and store the result in the destination */
124   arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
125 }
126 #else
arm_rms_f32(const float32_t * pSrc,uint32_t blockSize,float32_t * pResult)127 void arm_rms_f32(
128   const float32_t * pSrc,
129         uint32_t blockSize,
130         float32_t * pResult)
131 {
132         uint32_t blkCnt;                               /* Loop counter */
133         float32_t sum = 0.0f;                          /* Temporary result storage */
134         float32_t in;                                  /* Temporary variable to store input value */
135 
136 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
137 
138   /* Loop unrolling: Compute 4 outputs at a time */
139   blkCnt = blockSize >> 2U;
140 
141   while (blkCnt > 0U)
142   {
143     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
144 
145     in = *pSrc++;
146     /* Compute sum of squares and store result in a temporary variable, sum. */
147     sum += in * in;
148 
149     in = *pSrc++;
150     sum += in * in;
151 
152     in = *pSrc++;
153     sum += in * in;
154 
155     in = *pSrc++;
156     sum += in * in;
157 
158     /* Decrement loop counter */
159     blkCnt--;
160   }
161 
162   /* Loop unrolling: Compute remaining outputs */
163   blkCnt = blockSize % 0x4U;
164 
165 #else
166 
167   /* Initialize blkCnt with number of samples */
168   blkCnt = blockSize;
169 
170 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
171 
172   while (blkCnt > 0U)
173   {
174     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
175 
176     in = *pSrc++;
177     /* Compute sum of squares and store result in a temporary variable. */
178     sum += ( in * in);
179 
180     /* Decrement loop counter */
181     blkCnt--;
182   }
183 
184   /* Compute Rms and store result in destination */
185   arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
186 }
187 #endif /* #if defined(ARM_MATH_NEON) */
188 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
189 
190 /**
191   @} end of RMS group
192  */
193