Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /* ---------------------------------------------------------------------- |
| 2 | * Copyright (C) 2010-2012 ARM Limited. All rights reserved. |
||
| 3 | * |
||
| 4 | * $Date: 17. January 2013 |
||
| 5 | * $Revision: V1.4.0 |
||
| 6 | * |
||
| 7 | * Project: CMSIS DSP Library |
||
| 8 | * Title: arm_variance_example_f32.c |
||
| 9 | * |
||
| 10 | * Description: Example code demonstrating variance calculation of input sequence. |
||
| 11 | * |
||
| 12 | * Target Processor: Cortex-M4/Cortex-M3 |
||
| 13 | * |
||
| 14 | * Redistribution and use in source and binary forms, with or without |
||
| 15 | * modification, are permitted provided that the following conditions |
||
| 16 | * are met: |
||
| 17 | * - Redistributions of source code must retain the above copyright |
||
| 18 | * notice, this list of conditions and the following disclaimer. |
||
| 19 | * - Redistributions in binary form must reproduce the above copyright |
||
| 20 | * notice, this list of conditions and the following disclaimer in |
||
| 21 | * the documentation and/or other materials provided with the |
||
| 22 | * distribution. |
||
| 23 | * - Neither the name of ARM LIMITED nor the names of its contributors |
||
| 24 | * may be used to endorse or promote products derived from this |
||
| 25 | * software without specific prior written permission. |
||
| 26 | * |
||
| 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
| 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
| 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
||
| 30 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
||
| 31 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
||
| 33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
| 34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
| 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
| 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
||
| 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
| 38 | * POSSIBILITY OF SUCH DAMAGE. |
||
| 39 | * -------------------------------------------------------------------- */ |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @ingroup groupExamples |
||
| 43 | */ |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @defgroup VarianceExample Variance Example |
||
| 47 | * |
||
| 48 | * \par Description: |
||
| 49 | * \par |
||
| 50 | * Demonstrates the use of Basic Math and Support Functions to calculate the variance of an |
||
| 51 | * input sequence with N samples. Uniformly distributed white noise is taken as input. |
||
| 52 | * |
||
| 53 | * \par Algorithm: |
||
| 54 | * \par |
||
| 55 | * The variance of a sequence is the mean of the squared deviation of the sequence from its mean. |
||
| 56 | * \par |
||
| 57 | * This is denoted by the following equation: |
||
| 58 | * <pre> variance = ((x[0] - x') * (x[0] - x') + (x[1] - x') * (x[1] - x') + ... + * (x[n-1] - x') * (x[n-1] - x')) / (N-1)</pre> |
||
| 59 | * where, <code>x[n]</code> is the input sequence, <code>N</code> is the number of input samples, and |
||
| 60 | * <code>x'</code> is the mean value of the input sequence, <code>x[n]</code>. |
||
| 61 | * \par |
||
| 62 | * The mean value <code>x'</code> is defined as: |
||
| 63 | * <pre> x' = (x[0] + x[1] + ... + x[n-1]) / N</pre> |
||
| 64 | * |
||
| 65 | * \par Block Diagram: |
||
| 66 | * \par |
||
| 67 | * \image html Variance.gif |
||
| 68 | * |
||
| 69 | * |
||
| 70 | * \par Variables Description: |
||
| 71 | * \par |
||
| 72 | * \li \c testInput_f32 points to the input data |
||
| 73 | * \li \c wire1, \c wir2, \c wire3 temporary buffers |
||
| 74 | * \li \c blockSize number of samples processed at a time |
||
| 75 | * \li \c refVarianceOut reference variance value |
||
| 76 | * |
||
| 77 | * \par CMSIS DSP Software Library Functions Used: |
||
| 78 | * \par |
||
| 79 | * - arm_dot_prod_f32() |
||
| 80 | * - arm_mult_f32() |
||
| 81 | * - arm_sub_f32() |
||
| 82 | * - arm_fill_f32() |
||
| 83 | * - arm_copy_f32() |
||
| 84 | * |
||
| 85 | * <b> Refer </b> |
||
| 86 | * \link arm_variance_example_f32.c \endlink |
||
| 87 | * |
||
| 88 | */ |
||
| 89 | |||
| 90 | |||
| 91 | /** \example arm_variance_example_f32.c |
||
| 92 | */ |
||
| 93 | #include <math.h> |
||
| 94 | #include "arm_math.h" |
||
| 95 | |||
| 96 | /* ---------------------------------------------------------------------- |
||
| 97 | * Defines each of the tests performed |
||
| 98 | * ------------------------------------------------------------------- */ |
||
| 99 | #define MAX_BLOCKSIZE 32 |
||
| 100 | #define DELTA (0.000001f) |
||
| 101 | |||
| 102 | |||
| 103 | /* ---------------------------------------------------------------------- |
||
| 104 | * Declare I/O buffers |
||
| 105 | * ------------------------------------------------------------------- */ |
||
| 106 | float32_t wire1[MAX_BLOCKSIZE]; |
||
| 107 | float32_t wire2[MAX_BLOCKSIZE]; |
||
| 108 | float32_t wire3[MAX_BLOCKSIZE]; |
||
| 109 | |||
| 110 | /* ---------------------------------------------------------------------- |
||
| 111 | * Test input data for Floating point Variance example for 32-blockSize |
||
| 112 | * Generated by the MATLAB randn() function |
||
| 113 | * ------------------------------------------------------------------- */ |
||
| 114 | |||
| 115 | float32_t testInput_f32[32] = |
||
| 116 | { |
||
| 117 | -0.432564811528221, -1.665584378238097, 0.125332306474831, 0.287676420358549, |
||
| 118 | -1.146471350681464, 1.190915465642999, 1.189164201652103, -0.037633276593318, |
||
| 119 | 0.327292361408654, 0.174639142820925, -0.186708577681439, 0.725790548293303, |
||
| 120 | -0.588316543014189, 2.183185818197101, -0.136395883086596, 0.113931313520810, |
||
| 121 | 1.066768211359189, 0.059281460523605, -0.095648405483669, -0.832349463650022, |
||
| 122 | 0.294410816392640, -1.336181857937804, 0.714324551818952, 1.623562064446271, |
||
| 123 | -0.691775701702287, 0.857996672828263, 1.254001421602532, -1.593729576447477, |
||
| 124 | -1.440964431901020, 0.571147623658178, -0.399885577715363, 0.689997375464345 |
||
| 125 | |||
| 126 | }; |
||
| 127 | |||
| 128 | /* ---------------------------------------------------------------------- |
||
| 129 | * Declare Global variables |
||
| 130 | * ------------------------------------------------------------------- */ |
||
| 131 | uint32_t blockSize = 32; |
||
| 132 | float32_t refVarianceOut = 0.903941793931839; |
||
| 133 | |||
| 134 | /* ---------------------------------------------------------------------- |
||
| 135 | * Variance calculation test |
||
| 136 | * ------------------------------------------------------------------- */ |
||
| 137 | |||
| 138 | int32_t main(void) |
||
| 139 | { |
||
| 140 | arm_status status; |
||
| 141 | float32_t mean, oneByBlockSize; |
||
| 142 | float32_t variance; |
||
| 143 | float32_t diff; |
||
| 144 | |||
| 145 | status = ARM_MATH_SUCCESS; |
||
| 146 | |||
| 147 | /* Calculation of mean value of input */ |
||
| 148 | |||
| 149 | /* x' = 1/blockSize * (x(0)* 1 + x(1) * 1 + ... + x(n-1) * 1) */ |
||
| 150 | |||
| 151 | /* Fill wire1 buffer with 1.0 value */ |
||
| 152 | arm_fill_f32(1.0, wire1, blockSize); |
||
| 153 | |||
| 154 | /* Calculate the dot product of wire1 and wire2 */ |
||
| 155 | /* (x(0)* 1 + x(1) * 1 + ...+ x(n-1) * 1) */ |
||
| 156 | arm_dot_prod_f32(testInput_f32, wire1, blockSize, &mean); |
||
| 157 | |||
| 158 | /* Calculation of 1/blockSize */ |
||
| 159 | oneByBlockSize = 1.0 / (blockSize); |
||
| 160 | |||
| 161 | /* 1/blockSize * (x(0)* 1 + x(1) * 1 + ... + x(n-1) * 1) */ |
||
| 162 | arm_mult_f32(&mean, &oneByBlockSize, &mean, 1); |
||
| 163 | |||
| 164 | |||
| 165 | /* Calculation of variance value of input */ |
||
| 166 | |||
| 167 | /* (1/blockSize) * (x(0) - x') * (x(0) - x') + (x(1) - x') * (x(1) - x') + ... + (x(n-1) - x') * (x(n-1) - x') */ |
||
| 168 | |||
| 169 | /* Fill wire2 with mean value x' */ |
||
| 170 | arm_fill_f32(mean, wire2, blockSize); |
||
| 171 | |||
| 172 | /* wire3 contains (x-x') */ |
||
| 173 | arm_sub_f32(testInput_f32, wire2, wire3, blockSize); |
||
| 174 | |||
| 175 | /* wire2 contains (x-x') */ |
||
| 176 | arm_copy_f32(wire3, wire2, blockSize); |
||
| 177 | |||
| 178 | /* (x(0) - x') * (x(0) - x') + (x(1) - x') * (x(1) - x') + ... + (x(n-1) - x') * (x(n-1) - x') */ |
||
| 179 | arm_dot_prod_f32(wire2, wire3, blockSize, &variance); |
||
| 180 | |||
| 181 | /* Calculation of 1/blockSize */ |
||
| 182 | oneByBlockSize = 1.0 / (blockSize - 1); |
||
| 183 | |||
| 184 | /* Calculation of variance */ |
||
| 185 | arm_mult_f32(&variance, &oneByBlockSize, &variance, 1); |
||
| 186 | |||
| 187 | /* absolute value of difference between ref and test */ |
||
| 188 | diff = fabsf(refVarianceOut - variance); |
||
| 189 | |||
| 190 | /* Comparison of variance value with reference */ |
||
| 191 | if (diff > DELTA) |
||
| 192 | { |
||
| 193 | status = ARM_MATH_TEST_FAILURE; |
||
| 194 | } |
||
| 195 | |||
| 196 | if ( status != ARM_MATH_SUCCESS) |
||
| 197 | { |
||
| 198 | while (1); |
||
| 199 | } |
||
| 200 | |||
| 201 | while (1); /* main function does not return */ |
||
| 202 | } |
||
| 203 | |||
| 204 | /** \endlink */ |