Aded CMSIS5 DSP and NN folder. Needs some work

Committer:
robert_lp
Date:
Thu Apr 12 01:31:58 2018 +0000
Revision:
0:eedb7d567a5d
CMSIS5 Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robert_lp 0:eedb7d567a5d 1 /* ----------------------------------------------------------------------
robert_lp 0:eedb7d567a5d 2 * Project: CMSIS DSP Library
robert_lp 0:eedb7d567a5d 3 * Title: arm_std_q31.c
robert_lp 0:eedb7d567a5d 4 * Description: Standard deviation of an array of Q31 type.
robert_lp 0:eedb7d567a5d 5 *
robert_lp 0:eedb7d567a5d 6 * $Date: 27. January 2017
robert_lp 0:eedb7d567a5d 7 * $Revision: V.1.5.1
robert_lp 0:eedb7d567a5d 8 *
robert_lp 0:eedb7d567a5d 9 * Target Processor: Cortex-M cores
robert_lp 0:eedb7d567a5d 10 * -------------------------------------------------------------------- */
robert_lp 0:eedb7d567a5d 11 /*
robert_lp 0:eedb7d567a5d 12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
robert_lp 0:eedb7d567a5d 13 *
robert_lp 0:eedb7d567a5d 14 * SPDX-License-Identifier: Apache-2.0
robert_lp 0:eedb7d567a5d 15 *
robert_lp 0:eedb7d567a5d 16 * Licensed under the Apache License, Version 2.0 (the License); you may
robert_lp 0:eedb7d567a5d 17 * not use this file except in compliance with the License.
robert_lp 0:eedb7d567a5d 18 * You may obtain a copy of the License at
robert_lp 0:eedb7d567a5d 19 *
robert_lp 0:eedb7d567a5d 20 * www.apache.org/licenses/LICENSE-2.0
robert_lp 0:eedb7d567a5d 21 *
robert_lp 0:eedb7d567a5d 22 * Unless required by applicable law or agreed to in writing, software
robert_lp 0:eedb7d567a5d 23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
robert_lp 0:eedb7d567a5d 24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
robert_lp 0:eedb7d567a5d 25 * See the License for the specific language governing permissions and
robert_lp 0:eedb7d567a5d 26 * limitations under the License.
robert_lp 0:eedb7d567a5d 27 */
robert_lp 0:eedb7d567a5d 28
robert_lp 0:eedb7d567a5d 29 #include "arm_math.h"
robert_lp 0:eedb7d567a5d 30
robert_lp 0:eedb7d567a5d 31 /**
robert_lp 0:eedb7d567a5d 32 * @ingroup groupStats
robert_lp 0:eedb7d567a5d 33 */
robert_lp 0:eedb7d567a5d 34
robert_lp 0:eedb7d567a5d 35 /**
robert_lp 0:eedb7d567a5d 36 * @addtogroup STD
robert_lp 0:eedb7d567a5d 37 * @{
robert_lp 0:eedb7d567a5d 38 */
robert_lp 0:eedb7d567a5d 39
robert_lp 0:eedb7d567a5d 40 /**
robert_lp 0:eedb7d567a5d 41 * @brief Standard deviation of the elements of a Q31 vector.
robert_lp 0:eedb7d567a5d 42 * @param[in] *pSrc points to the input vector
robert_lp 0:eedb7d567a5d 43 * @param[in] blockSize length of the input vector
robert_lp 0:eedb7d567a5d 44 * @param[out] *pResult standard deviation value returned here
robert_lp 0:eedb7d567a5d 45 * @return none.
robert_lp 0:eedb7d567a5d 46 * @details
robert_lp 0:eedb7d567a5d 47 * <b>Scaling and Overflow Behavior:</b>
robert_lp 0:eedb7d567a5d 48 *
robert_lp 0:eedb7d567a5d 49 *\par
robert_lp 0:eedb7d567a5d 50 * The function is implemented using an internal 64-bit accumulator.
robert_lp 0:eedb7d567a5d 51 * The input is represented in 1.31 format, which is then downshifted by 8 bits
robert_lp 0:eedb7d567a5d 52 * which yields 1.23, and intermediate multiplication yields a 2.46 format.
robert_lp 0:eedb7d567a5d 53 * The accumulator maintains full precision of the intermediate multiplication results,
robert_lp 0:eedb7d567a5d 54 * but provides only a 16 guard bits.
robert_lp 0:eedb7d567a5d 55 * There is no saturation on intermediate additions.
robert_lp 0:eedb7d567a5d 56 * If the accumulator overflows it wraps around and distorts the result.
robert_lp 0:eedb7d567a5d 57 * In order to avoid overflows completely the input signal must be scaled down by
robert_lp 0:eedb7d567a5d 58 * log2(blockSize)-8 bits, as a total of blockSize additions are performed internally.
robert_lp 0:eedb7d567a5d 59 * After division, internal variables should be Q18.46
robert_lp 0:eedb7d567a5d 60 * Finally, the 18.46 accumulator is right shifted by 15 bits to yield a 1.31 format value.
robert_lp 0:eedb7d567a5d 61 *
robert_lp 0:eedb7d567a5d 62 */
robert_lp 0:eedb7d567a5d 63
robert_lp 0:eedb7d567a5d 64 void arm_std_q31(
robert_lp 0:eedb7d567a5d 65 q31_t * pSrc,
robert_lp 0:eedb7d567a5d 66 uint32_t blockSize,
robert_lp 0:eedb7d567a5d 67 q31_t * pResult)
robert_lp 0:eedb7d567a5d 68 {
robert_lp 0:eedb7d567a5d 69 q63_t sum = 0; /* Accumulator */
robert_lp 0:eedb7d567a5d 70 q63_t meanOfSquares, squareOfMean; /* square of mean and mean of square */
robert_lp 0:eedb7d567a5d 71 q31_t in; /* input value */
robert_lp 0:eedb7d567a5d 72 uint32_t blkCnt; /* loop counter */
robert_lp 0:eedb7d567a5d 73 q63_t sumOfSquares = 0; /* Accumulator */
robert_lp 0:eedb7d567a5d 74
robert_lp 0:eedb7d567a5d 75 if (blockSize == 1U)
robert_lp 0:eedb7d567a5d 76 {
robert_lp 0:eedb7d567a5d 77 *pResult = 0;
robert_lp 0:eedb7d567a5d 78 return;
robert_lp 0:eedb7d567a5d 79 }
robert_lp 0:eedb7d567a5d 80
robert_lp 0:eedb7d567a5d 81 #if defined (ARM_MATH_DSP)
robert_lp 0:eedb7d567a5d 82 /* Run the below code for Cortex-M4 and Cortex-M3 */
robert_lp 0:eedb7d567a5d 83
robert_lp 0:eedb7d567a5d 84 /*loop Unrolling */
robert_lp 0:eedb7d567a5d 85 blkCnt = blockSize >> 2U;
robert_lp 0:eedb7d567a5d 86
robert_lp 0:eedb7d567a5d 87 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
robert_lp 0:eedb7d567a5d 88 ** a second loop below computes the remaining 1 to 3 samples. */
robert_lp 0:eedb7d567a5d 89 while (blkCnt > 0U)
robert_lp 0:eedb7d567a5d 90 {
robert_lp 0:eedb7d567a5d 91 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
robert_lp 0:eedb7d567a5d 92 /* Compute Sum of squares of the input samples
robert_lp 0:eedb7d567a5d 93 * and then store the result in a temporary variable, sum. */
robert_lp 0:eedb7d567a5d 94 in = *pSrc++ >> 8U;
robert_lp 0:eedb7d567a5d 95 sum += in;
robert_lp 0:eedb7d567a5d 96 sumOfSquares += ((q63_t) (in) * (in));
robert_lp 0:eedb7d567a5d 97 in = *pSrc++ >> 8U;
robert_lp 0:eedb7d567a5d 98 sum += in;
robert_lp 0:eedb7d567a5d 99 sumOfSquares += ((q63_t) (in) * (in));
robert_lp 0:eedb7d567a5d 100 in = *pSrc++ >> 8U;
robert_lp 0:eedb7d567a5d 101 sum += in;
robert_lp 0:eedb7d567a5d 102 sumOfSquares += ((q63_t) (in) * (in));
robert_lp 0:eedb7d567a5d 103 in = *pSrc++ >> 8U;
robert_lp 0:eedb7d567a5d 104 sum += in;
robert_lp 0:eedb7d567a5d 105 sumOfSquares += ((q63_t) (in) * (in));
robert_lp 0:eedb7d567a5d 106
robert_lp 0:eedb7d567a5d 107 /* Decrement the loop counter */
robert_lp 0:eedb7d567a5d 108 blkCnt--;
robert_lp 0:eedb7d567a5d 109 }
robert_lp 0:eedb7d567a5d 110
robert_lp 0:eedb7d567a5d 111 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
robert_lp 0:eedb7d567a5d 112 ** No loop unrolling is used. */
robert_lp 0:eedb7d567a5d 113 blkCnt = blockSize % 0x4U;
robert_lp 0:eedb7d567a5d 114
robert_lp 0:eedb7d567a5d 115 while (blkCnt > 0U)
robert_lp 0:eedb7d567a5d 116 {
robert_lp 0:eedb7d567a5d 117 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
robert_lp 0:eedb7d567a5d 118 /* Compute Sum of squares of the input samples
robert_lp 0:eedb7d567a5d 119 * and then store the result in a temporary variable, sum. */
robert_lp 0:eedb7d567a5d 120 in = *pSrc++ >> 8U;
robert_lp 0:eedb7d567a5d 121 sum += in;
robert_lp 0:eedb7d567a5d 122 sumOfSquares += ((q63_t) (in) * (in));
robert_lp 0:eedb7d567a5d 123
robert_lp 0:eedb7d567a5d 124 /* Decrement the loop counter */
robert_lp 0:eedb7d567a5d 125 blkCnt--;
robert_lp 0:eedb7d567a5d 126 }
robert_lp 0:eedb7d567a5d 127
robert_lp 0:eedb7d567a5d 128 /* Compute Mean of squares of the input samples
robert_lp 0:eedb7d567a5d 129 * and then store the result in a temporary variable, meanOfSquares. */
robert_lp 0:eedb7d567a5d 130 meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1U);
robert_lp 0:eedb7d567a5d 131
robert_lp 0:eedb7d567a5d 132 #else
robert_lp 0:eedb7d567a5d 133 /* Run the below code for Cortex-M0 */
robert_lp 0:eedb7d567a5d 134
robert_lp 0:eedb7d567a5d 135 /* Loop over blockSize number of values */
robert_lp 0:eedb7d567a5d 136 blkCnt = blockSize;
robert_lp 0:eedb7d567a5d 137
robert_lp 0:eedb7d567a5d 138 while (blkCnt > 0U)
robert_lp 0:eedb7d567a5d 139 {
robert_lp 0:eedb7d567a5d 140 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
robert_lp 0:eedb7d567a5d 141 /* Compute Sum of squares of the input samples
robert_lp 0:eedb7d567a5d 142 * and then store the result in a temporary variable, sumOfSquares. */
robert_lp 0:eedb7d567a5d 143 in = *pSrc++ >> 8U;
robert_lp 0:eedb7d567a5d 144 sumOfSquares += ((q63_t) (in) * (in));
robert_lp 0:eedb7d567a5d 145
robert_lp 0:eedb7d567a5d 146 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
robert_lp 0:eedb7d567a5d 147 /* Compute sum of all input values and then store the result in a temporary variable, sum. */
robert_lp 0:eedb7d567a5d 148 sum += in;
robert_lp 0:eedb7d567a5d 149
robert_lp 0:eedb7d567a5d 150 /* Decrement the loop counter */
robert_lp 0:eedb7d567a5d 151 blkCnt--;
robert_lp 0:eedb7d567a5d 152 }
robert_lp 0:eedb7d567a5d 153
robert_lp 0:eedb7d567a5d 154 /* Compute Mean of squares of the input samples
robert_lp 0:eedb7d567a5d 155 * and then store the result in a temporary variable, meanOfSquares. */
robert_lp 0:eedb7d567a5d 156 meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1U);
robert_lp 0:eedb7d567a5d 157
robert_lp 0:eedb7d567a5d 158 #endif /* #if defined (ARM_MATH_DSP) */
robert_lp 0:eedb7d567a5d 159
robert_lp 0:eedb7d567a5d 160 /* Compute square of mean */
robert_lp 0:eedb7d567a5d 161 squareOfMean = sum * sum / (q63_t)(blockSize * (blockSize - 1U));
robert_lp 0:eedb7d567a5d 162
robert_lp 0:eedb7d567a5d 163 /* Compute standard deviation and then store the result to the destination */
robert_lp 0:eedb7d567a5d 164 arm_sqrt_q31((meanOfSquares - squareOfMean) >> 15U, pResult);
robert_lp 0:eedb7d567a5d 165 }
robert_lp 0:eedb7d567a5d 166
robert_lp 0:eedb7d567a5d 167 /**
robert_lp 0:eedb7d567a5d 168 * @} end of STD group
robert_lp 0:eedb7d567a5d 169 */
robert_lp 0:eedb7d567a5d 170