Merge branch 'jnosky/master'
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / StatisticsFunctions / arm_rms_f32.c
1 /* ----------------------------------------------------------------------   
2 * Copyright (C) 2010 ARM Limited. All rights reserved.   
3 *   
4 * $Date:        15. July 2011  
5 * $Revision:    V1.0.10  
6 *   
7 * Project:          CMSIS DSP Library   
8 * Title:                arm_rms_f32.c   
9 *   
10 * Description:  Root mean square value of an array of F32 type   
11 *   
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *  
14 * Version 1.0.10 2011/7/15 
15 *    Big Endian support added and Merged M0 and M3/M4 Source code.  
16 *   
17 * Version 1.0.3 2010/11/29  
18 *    Re-organized the CMSIS folders and updated documentation.   
19 *    
20 * Version 1.0.2 2010/11/11   
21 *    Documentation updated.    
22 *   
23 * Version 1.0.1 2010/10/05    
24 *    Production release and review comments incorporated.   
25 *   
26 * Version 1.0.0 2010/09/20    
27 *    Production release and review comments incorporated.   
28 * ---------------------------------------------------------------------------- */
29
30 #include "arm_math.h"
31
32 /**   
33  * @ingroup groupStats   
34  */
35
36 /**   
37  * @defgroup RMS Root mean square (RMS)   
38  *   
39  *    
40  * Calculates the Root Mean Sqaure of the elements in the input vector.   
41  * The underlying algorithm is used:   
42  *   
43  * <pre>   
44  *      Result = sqrt(((pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]) / blockSize));   
45  * </pre>   
46  *  
47  * There are separate functions for floating point, Q31, and Q15 data types.    
48  */
49
50 /**   
51  * @addtogroup RMS   
52  * @{   
53  */
54
55
56 /**   
57  * @brief Root Mean Square of the elements of a floating-point vector.   
58  * @param[in]       *pSrc points to the input vector   
59  * @param[in]       blockSize length of the input vector   
60  * @param[out]      *pResult rms value returned here   
61  * @return none.   
62  *   
63  */
64
65 void arm_rms_f32(
66   float32_t * pSrc,
67   uint32_t blockSize,
68   float32_t * pResult)
69 {
70   float32_t sum = 0.0f;                          /* Accumulator */
71   float32_t in;                                  /* Tempoprary variable to store input value */
72   uint32_t blkCnt;                               /* loop counter */
73
74 #ifndef ARM_MATH_CM0
75
76   /* Run the below code for Cortex-M4 and Cortex-M3 */
77
78   /* loop Unrolling */
79   blkCnt = blockSize >> 2u;
80
81   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
82    ** a second loop below computes the remaining 1 to 3 samples. */
83   while(blkCnt > 0u)
84   {
85     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
86     /* Compute sum of the squares and then store the result in a temporary variable, sum  */
87     in = *pSrc++;
88     sum += in * in;
89     in = *pSrc++;
90     sum += in * in;
91     in = *pSrc++;
92     sum += in * in;
93     in = *pSrc++;
94     sum += in * in;
95
96     /* Decrement the loop counter */
97     blkCnt--;
98   }
99
100   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
101    ** No loop unrolling is used. */
102   blkCnt = blockSize % 0x4u;
103
104 #else
105
106   /* Run the below code for Cortex-M0 */
107
108   /* Loop over blockSize number of values */
109   blkCnt = blockSize;
110
111 #endif /* #ifndef ARM_MATH_CM0 */
112
113   while(blkCnt > 0u)
114   {
115     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
116     /* Compute sum of the squares and then store the results in a temporary variable, sum  */
117     in = *pSrc++;
118     sum += in * in;
119
120     /* Decrement the loop counter */
121     blkCnt--;
122   }
123
124   /* Compute Rms and store the result in the destination */
125   arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
126 }
127
128 /**   
129  * @} end of RMS group   
130  */