source: S-port/trunk/Drivers/CMSIS/DSP/Source/StatisticsFunctions/arm_mean_q31.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 3.3 KB
Line 
1/* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mean_q31.c
4 * Description: Mean value of a Q31 vector
5 *
6 * $Date: 27. January 2017
7 * $Revision: V.1.5.1
8 *
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
11/*
12 * Copyright (C) 2010-2017 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 "arm_math.h"
30
31/**
32 * @ingroup groupStats
33 */
34
35/**
36 * @addtogroup mean
37 * @{
38 */
39
40
41/**
42 * @brief Mean value of a Q31 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 64-bit internal accumulator.
52 * The input is represented in 1.31 format and is accumulated in a 64-bit
53 * accumulator in 33.31 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 truncated to yield a result of 1.31 format.
57 *
58 */
59
60void arm_mean_q31(
61 q31_t * pSrc,
62 uint32_t blockSize,
63 q31_t * pResult)
64{
65 q63_t sum = 0; /* Temporary result storage */
66 uint32_t blkCnt; /* loop counter */
67
68#if defined (ARM_MATH_DSP)
69 /* Run the below code for Cortex-M4 and Cortex-M3 */
70
71 q31_t in1, in2, in3, in4;
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 in1 = *pSrc++;
82 in2 = *pSrc++;
83 in3 = *pSrc++;
84 in4 = *pSrc++;
85
86 sum += in1;
87 sum += in2;
88 sum += in3;
89 sum += in4;
90
91 /* Decrement the loop counter */
92 blkCnt--;
93 }
94
95 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
96 ** No loop unrolling is used. */
97 blkCnt = blockSize % 0x4U;
98
99#else
100 /* Run the below code for Cortex-M0 */
101
102 /* Loop over blockSize number of values */
103 blkCnt = blockSize;
104
105#endif /* #if defined (ARM_MATH_DSP) */
106
107 while (blkCnt > 0U)
108 {
109 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
110 sum += *pSrc++;
111
112 /* Decrement the loop counter */
113 blkCnt--;
114 }
115
116 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
117 /* Store the result to the destination */
118 *pResult = (q31_t) (sum / (int32_t) blockSize);
119}
120
121/**
122 * @} end of mean group
123 */
Note: See TracBrowser for help on using the repository browser.