Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Source / StatisticsFunctions / arm_mean_q15.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_mean_q15.c   
9 *   
10 * Description:  Mean value of a Q15 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  * @ingroup groupStats   
34  */
35
36 /**   
37  * @addtogroup mean   
38  * @{   
39  */
40
41 /**   
42  * @brief Mean value of a Q15 vector.   
43  * @param[in]       *pSrc points to the input vector   
44  * @param[in]       blockSize length of the input vector   
45  * @param[out]      *pResult mean value returned here   
46  * @return none.   
47  *   
48  * @details   
49  * <b>Scaling and Overflow Behavior:</b>   
50  * \par   
51  * The function is implemented using a 32-bit internal accumulator.   
52  * The input is represented in 1.15 format and is accumulated in a 32-bit    
53  * accumulator in 17.15 format.    
54  * There is no risk of internal overflow with this approach, and the    
55  * full precision of intermediate result is preserved.    
56  * Finally, the accumulator is saturated and truncated to yield a result of 1.15 format.   
57  *   
58  */
59
60
61 void arm_mean_q15(
62   q15_t * pSrc,
63   uint32_t blockSize,
64   q15_t * pResult)
65 {
66   q31_t sum = 0;                                 /* Temporary result storage */
67   uint32_t blkCnt;                               /* loop counter */
68
69 #ifndef ARM_MATH_CM0
70
71   /* Run the below code for Cortex-M4 and Cortex-M3 */
72
73   /*loop Unrolling */
74   blkCnt = blockSize >> 2u;
75
76   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.   
77    ** a second loop below computes the remaining 1 to 3 samples. */
78   while(blkCnt > 0u)
79   {
80     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
81     sum += *pSrc++;
82     sum += *pSrc++;
83     sum += *pSrc++;
84     sum += *pSrc++;
85
86     /* Decrement the loop counter */
87     blkCnt--;
88   }
89
90   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.   
91    ** No loop unrolling is used. */
92   blkCnt = blockSize % 0x4u;
93
94 #else
95
96   /* Run the below code for Cortex-M0 */
97
98   /* Loop over blockSize number of values */
99   blkCnt = blockSize;
100
101 #endif /* #ifndef ARM_MATH_CM0 */
102
103   while(blkCnt > 0u)
104   {
105     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
106     sum += *pSrc++;
107
108     /* Decrement the loop counter */
109     blkCnt--;
110   }
111
112   /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize  */
113   /* Store the result to the destination */
114   *pResult = (q15_t) (sum / blockSize);
115 }
116
117 /**   
118  * @} end of mean group   
119  */