1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mean_q31.c
4 * Description: Mean value of a Q31 vector
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
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
49 * <b>Scaling and Overflow Behavior:</b>
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.
65 q63_t sum
= 0; /* Temporary result storage */
66 uint32_t blkCnt
; /* loop counter */
68 #if defined (ARM_MATH_DSP)
69 /* Run the below code for Cortex-M4 and Cortex-M3 */
71 q31_t in1
, in2
, in3
, in4
;
74 blkCnt
= blockSize
>> 2U;
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. */
80 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
91 /* Decrement the loop counter */
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
;
100 /* Run the below code for Cortex-M0 */
102 /* Loop over blockSize number of values */
105 #endif /* #if defined (ARM_MATH_DSP) */
109 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
112 /* Decrement the loop counter */
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
);
122 * @} end of mean group