The CMSIS DSP 5 library

Dependents:   Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more

Committer:
xorjoep
Date:
Thu Jun 21 11:56:27 2018 +0000
Revision:
3:4098b9d3d571
Parent:
1:24714b45cd1b
headers is a folder not a library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xorjoep 1:24714b45cd1b 1 /* ----------------------------------------------------------------------
xorjoep 1:24714b45cd1b 2 * Project: CMSIS DSP Library
xorjoep 1:24714b45cd1b 3 * Title: arm_std_f32.c
xorjoep 1:24714b45cd1b 4 * Description: Standard deviation of the elements of a floating-point vector
xorjoep 1:24714b45cd1b 5 *
xorjoep 1:24714b45cd1b 6 * $Date: 27. January 2017
xorjoep 1:24714b45cd1b 7 * $Revision: V.1.5.1
xorjoep 1:24714b45cd1b 8 *
xorjoep 1:24714b45cd1b 9 * Target Processor: Cortex-M cores
xorjoep 1:24714b45cd1b 10 * -------------------------------------------------------------------- */
xorjoep 1:24714b45cd1b 11 /*
xorjoep 1:24714b45cd1b 12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
xorjoep 1:24714b45cd1b 13 *
xorjoep 1:24714b45cd1b 14 * SPDX-License-Identifier: Apache-2.0
xorjoep 1:24714b45cd1b 15 *
xorjoep 1:24714b45cd1b 16 * Licensed under the Apache License, Version 2.0 (the License); you may
xorjoep 1:24714b45cd1b 17 * not use this file except in compliance with the License.
xorjoep 1:24714b45cd1b 18 * You may obtain a copy of the License at
xorjoep 1:24714b45cd1b 19 *
xorjoep 1:24714b45cd1b 20 * www.apache.org/licenses/LICENSE-2.0
xorjoep 1:24714b45cd1b 21 *
xorjoep 1:24714b45cd1b 22 * Unless required by applicable law or agreed to in writing, software
xorjoep 1:24714b45cd1b 23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
xorjoep 1:24714b45cd1b 24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
xorjoep 1:24714b45cd1b 25 * See the License for the specific language governing permissions and
xorjoep 1:24714b45cd1b 26 * limitations under the License.
xorjoep 1:24714b45cd1b 27 */
xorjoep 1:24714b45cd1b 28
xorjoep 1:24714b45cd1b 29 #include "arm_math.h"
xorjoep 1:24714b45cd1b 30
xorjoep 1:24714b45cd1b 31 /**
xorjoep 1:24714b45cd1b 32 * @ingroup groupStats
xorjoep 1:24714b45cd1b 33 */
xorjoep 1:24714b45cd1b 34
xorjoep 1:24714b45cd1b 35 /**
xorjoep 1:24714b45cd1b 36 * @defgroup STD Standard deviation
xorjoep 1:24714b45cd1b 37 *
xorjoep 1:24714b45cd1b 38 * Calculates the standard deviation of the elements in the input vector.
xorjoep 1:24714b45cd1b 39 * The underlying algorithm is used:
xorjoep 1:24714b45cd1b 40 *
xorjoep 1:24714b45cd1b 41 * <pre>
xorjoep 1:24714b45cd1b 42 * Result = sqrt((sumOfSquares - sum<sup>2</sup> / blockSize) / (blockSize - 1))
xorjoep 1:24714b45cd1b 43 *
xorjoep 1:24714b45cd1b 44 * where, sumOfSquares = pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]
xorjoep 1:24714b45cd1b 45 *
xorjoep 1:24714b45cd1b 46 * sum = pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1]
xorjoep 1:24714b45cd1b 47 * </pre>
xorjoep 1:24714b45cd1b 48 *
xorjoep 1:24714b45cd1b 49 * There are separate functions for floating point, Q31, and Q15 data types.
xorjoep 1:24714b45cd1b 50 */
xorjoep 1:24714b45cd1b 51
xorjoep 1:24714b45cd1b 52 /**
xorjoep 1:24714b45cd1b 53 * @addtogroup STD
xorjoep 1:24714b45cd1b 54 * @{
xorjoep 1:24714b45cd1b 55 */
xorjoep 1:24714b45cd1b 56
xorjoep 1:24714b45cd1b 57
xorjoep 1:24714b45cd1b 58 /**
xorjoep 1:24714b45cd1b 59 * @brief Standard deviation of the elements of a floating-point vector.
xorjoep 1:24714b45cd1b 60 * @param[in] *pSrc points to the input vector
xorjoep 1:24714b45cd1b 61 * @param[in] blockSize length of the input vector
xorjoep 1:24714b45cd1b 62 * @param[out] *pResult standard deviation value returned here
xorjoep 1:24714b45cd1b 63 * @return none.
xorjoep 1:24714b45cd1b 64 */
xorjoep 1:24714b45cd1b 65
xorjoep 1:24714b45cd1b 66 void arm_std_f32(
xorjoep 1:24714b45cd1b 67 float32_t * pSrc,
xorjoep 1:24714b45cd1b 68 uint32_t blockSize,
xorjoep 1:24714b45cd1b 69 float32_t * pResult)
xorjoep 1:24714b45cd1b 70 {
xorjoep 1:24714b45cd1b 71 float32_t sum = 0.0f; /* Temporary result storage */
xorjoep 1:24714b45cd1b 72 float32_t sumOfSquares = 0.0f; /* Sum of squares */
xorjoep 1:24714b45cd1b 73 float32_t in; /* input value */
xorjoep 1:24714b45cd1b 74 uint32_t blkCnt; /* loop counter */
xorjoep 1:24714b45cd1b 75 #if defined (ARM_MATH_DSP)
xorjoep 1:24714b45cd1b 76 float32_t meanOfSquares, mean, squareOfMean; /* Temporary variables */
xorjoep 1:24714b45cd1b 77 #else
xorjoep 1:24714b45cd1b 78 float32_t squareOfSum; /* Square of Sum */
xorjoep 1:24714b45cd1b 79 float32_t var; /* Temporary varaince storage */
xorjoep 1:24714b45cd1b 80 #endif
xorjoep 1:24714b45cd1b 81
xorjoep 1:24714b45cd1b 82 if (blockSize == 1U)
xorjoep 1:24714b45cd1b 83 {
xorjoep 1:24714b45cd1b 84 *pResult = 0;
xorjoep 1:24714b45cd1b 85 return;
xorjoep 1:24714b45cd1b 86 }
xorjoep 1:24714b45cd1b 87
xorjoep 1:24714b45cd1b 88 #if defined (ARM_MATH_DSP)
xorjoep 1:24714b45cd1b 89 /* Run the below code for Cortex-M4 and Cortex-M3 */
xorjoep 1:24714b45cd1b 90
xorjoep 1:24714b45cd1b 91 /*loop Unrolling */
xorjoep 1:24714b45cd1b 92 blkCnt = blockSize >> 2U;
xorjoep 1:24714b45cd1b 93
xorjoep 1:24714b45cd1b 94 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
xorjoep 1:24714b45cd1b 95 ** a second loop below computes the remaining 1 to 3 samples. */
xorjoep 1:24714b45cd1b 96 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 97 {
xorjoep 1:24714b45cd1b 98 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
xorjoep 1:24714b45cd1b 99 /* Compute Sum of squares of the input samples
xorjoep 1:24714b45cd1b 100 * and then store the result in a temporary variable, sum. */
xorjoep 1:24714b45cd1b 101 in = *pSrc++;
xorjoep 1:24714b45cd1b 102 sum += in;
xorjoep 1:24714b45cd1b 103 sumOfSquares += in * in;
xorjoep 1:24714b45cd1b 104 in = *pSrc++;
xorjoep 1:24714b45cd1b 105 sum += in;
xorjoep 1:24714b45cd1b 106 sumOfSquares += in * in;
xorjoep 1:24714b45cd1b 107 in = *pSrc++;
xorjoep 1:24714b45cd1b 108 sum += in;
xorjoep 1:24714b45cd1b 109 sumOfSquares += in * in;
xorjoep 1:24714b45cd1b 110 in = *pSrc++;
xorjoep 1:24714b45cd1b 111 sum += in;
xorjoep 1:24714b45cd1b 112 sumOfSquares += in * in;
xorjoep 1:24714b45cd1b 113
xorjoep 1:24714b45cd1b 114 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 115 blkCnt--;
xorjoep 1:24714b45cd1b 116 }
xorjoep 1:24714b45cd1b 117
xorjoep 1:24714b45cd1b 118 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
xorjoep 1:24714b45cd1b 119 ** No loop unrolling is used. */
xorjoep 1:24714b45cd1b 120 blkCnt = blockSize % 0x4U;
xorjoep 1:24714b45cd1b 121
xorjoep 1:24714b45cd1b 122 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 123 {
xorjoep 1:24714b45cd1b 124 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
xorjoep 1:24714b45cd1b 125 /* Compute Sum of squares of the input samples
xorjoep 1:24714b45cd1b 126 * and then store the result in a temporary variable, sum. */
xorjoep 1:24714b45cd1b 127 in = *pSrc++;
xorjoep 1:24714b45cd1b 128 sum += in;
xorjoep 1:24714b45cd1b 129 sumOfSquares += in * in;
xorjoep 1:24714b45cd1b 130
xorjoep 1:24714b45cd1b 131 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 132 blkCnt--;
xorjoep 1:24714b45cd1b 133 }
xorjoep 1:24714b45cd1b 134
xorjoep 1:24714b45cd1b 135 /* Compute Mean of squares of the input samples
xorjoep 1:24714b45cd1b 136 * and then store the result in a temporary variable, meanOfSquares. */
xorjoep 1:24714b45cd1b 137 meanOfSquares = sumOfSquares / ((float32_t) blockSize - 1.0f);
xorjoep 1:24714b45cd1b 138
xorjoep 1:24714b45cd1b 139 /* Compute mean of all input values */
xorjoep 1:24714b45cd1b 140 mean = sum / (float32_t) blockSize;
xorjoep 1:24714b45cd1b 141
xorjoep 1:24714b45cd1b 142 /* Compute square of mean */
xorjoep 1:24714b45cd1b 143 squareOfMean = (mean * mean) * (((float32_t) blockSize) /
xorjoep 1:24714b45cd1b 144 ((float32_t) blockSize - 1.0f));
xorjoep 1:24714b45cd1b 145
xorjoep 1:24714b45cd1b 146 /* Compute standard deviation and then store the result to the destination */
xorjoep 1:24714b45cd1b 147 arm_sqrt_f32((meanOfSquares - squareOfMean), pResult);
xorjoep 1:24714b45cd1b 148
xorjoep 1:24714b45cd1b 149 #else
xorjoep 1:24714b45cd1b 150 /* Run the below code for Cortex-M0 */
xorjoep 1:24714b45cd1b 151
xorjoep 1:24714b45cd1b 152 /* Loop over blockSize number of values */
xorjoep 1:24714b45cd1b 153 blkCnt = blockSize;
xorjoep 1:24714b45cd1b 154
xorjoep 1:24714b45cd1b 155 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 156 {
xorjoep 1:24714b45cd1b 157 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
xorjoep 1:24714b45cd1b 158 /* Compute Sum of squares of the input samples
xorjoep 1:24714b45cd1b 159 * and then store the result in a temporary variable, sumOfSquares. */
xorjoep 1:24714b45cd1b 160 in = *pSrc++;
xorjoep 1:24714b45cd1b 161 sumOfSquares += in * in;
xorjoep 1:24714b45cd1b 162
xorjoep 1:24714b45cd1b 163 /* C = (A[0] + A[1] + ... + A[blockSize-1]) */
xorjoep 1:24714b45cd1b 164 /* Compute Sum of the input samples
xorjoep 1:24714b45cd1b 165 * and then store the result in a temporary variable, sum. */
xorjoep 1:24714b45cd1b 166 sum += in;
xorjoep 1:24714b45cd1b 167
xorjoep 1:24714b45cd1b 168 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 169 blkCnt--;
xorjoep 1:24714b45cd1b 170 }
xorjoep 1:24714b45cd1b 171
xorjoep 1:24714b45cd1b 172 /* Compute the square of sum */
xorjoep 1:24714b45cd1b 173 squareOfSum = ((sum * sum) / (float32_t) blockSize);
xorjoep 1:24714b45cd1b 174
xorjoep 1:24714b45cd1b 175 /* Compute the variance */
xorjoep 1:24714b45cd1b 176 var = ((sumOfSquares - squareOfSum) / (float32_t) (blockSize - 1.0f));
xorjoep 1:24714b45cd1b 177
xorjoep 1:24714b45cd1b 178 /* Compute standard deviation and then store the result to the destination */
xorjoep 1:24714b45cd1b 179 arm_sqrt_f32(var, pResult);
xorjoep 1:24714b45cd1b 180
xorjoep 1:24714b45cd1b 181 #endif /* #if defined (ARM_MATH_DSP) */
xorjoep 1:24714b45cd1b 182 }
xorjoep 1:24714b45cd1b 183
xorjoep 1:24714b45cd1b 184 /**
xorjoep 1:24714b45cd1b 185 * @} end of STD group
xorjoep 1:24714b45cd1b 186 */