Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / StatisticsFunctions / arm_rms_q31.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_q31.c   
9 *   
10 * Description:  Root Mean Square of the elements of a Q31 vector.   
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  * @addtogroup RMS   
34  * @{   
35  */
36
37
38 /**   
39  * @brief Root Mean Square of the elements of a Q31 vector.   
40  * @param[in]       *pSrc points to the input vector   
41  * @param[in]       blockSize length of the input vector   
42  * @param[out]      *pResult rms value returned here   
43  * @return none.   
44  *   
45  * @details   
46  * <b>Scaling and Overflow Behavior:</b>   
47  *   
48  *\par   
49  * The function is implemented using an internal 64-bit accumulator.   
50  * The input is represented in 1.31 format, and intermediate multiplication   
51  * yields a 2.62 format.   
52  * The accumulator maintains full precision of the intermediate multiplication results,    
53  * but provides only a single guard bit.   
54  * There is no saturation on intermediate additions.   
55  * If the accumulator overflows, it wraps around and distorts the result.    
56  * In order to avoid overflows completely, the input signal must be scaled down by    
57  * log2(blockSize) bits, as a total of blockSize additions are performed internally.    
58  * Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value.   
59  *   
60  */
61
62 void arm_rms_q31(
63   q31_t * pSrc,
64   uint32_t blockSize,
65   q31_t * pResult)
66 {
67   q63_t sum = 0;                                 /* accumulator */
68   q31_t in;                                      /* Temporary variable to store the input */
69   uint32_t blkCnt;                               /* loop counter */
70
71 #ifndef ARM_MATH_CM0
72
73   /* Run the below code for Cortex-M4 and Cortex-M3 */
74
75   q31_t *pIn1 = pSrc;                            /* SrcA pointer */
76
77   /*loop Unrolling */
78   blkCnt = blockSize >> 2u;
79
80   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
81    ** a second loop below computes the remaining 1 to 3 samples. */
82   while(blkCnt > 0u)
83   {
84     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
85     /* Compute sum of the squares and then store the result in a temporary variable, sum */
86     in = *pIn1++;
87     sum += (q63_t) in *in;
88     in = *pIn1++;
89     sum += (q63_t) in *in;
90     in = *pIn1++;
91     sum += (q63_t) in *in;
92     in = *pIn1++;
93     sum += (q63_t) in *in;
94
95     /* Decrement the loop counter */
96     blkCnt--;
97   }
98
99   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
100    ** No loop unrolling is used. */
101   blkCnt = blockSize % 0x4u;
102
103   while(blkCnt > 0u)
104   {
105     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
106     /* Compute sum of the squares and then store the results in a temporary variable, sum */
107     in = *pIn1++;
108     sum += (q63_t) in *in;
109
110     /* Decrement the loop counter */
111     blkCnt--;
112   }
113
114 #else
115
116   /* Run the below code for Cortex-M0 */
117
118   /* Loop over blockSize number of values */
119   blkCnt = blockSize;
120
121   while(blkCnt > 0u)
122   {
123     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
124     /* Compute sum of the squares and then store the results in a temporary variable, sum */
125     in = *pSrc++;
126     sum += (q63_t) in *in;
127
128     /* Decrement the loop counter */
129     blkCnt--;
130   }
131
132 #endif /* #ifndef ARM_MATH_CM0 */
133
134   /* Convert data in 2.62 to 1.31 by 31 right shifts */
135   sum = sum >> 31;
136
137   /* Compute Rms and store the result in the destination vector */
138   arm_sqrt_q31((q31_t) (sum / (int32_t) blockSize), pResult);
139 }
140
141 /**   
142  * @} end of RMS group   
143  */