CMSIS DSP library
Dependents: performance_timer Surfboard_ gps2rtty Capstone ... more
Legacy Warning
This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.
cmsis_dsp/FilteringFunctions/arm_lms_norm_f32.c@5:3762170b6d4d, 2015-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Nov 20 08:45:18 2015 +0000
- Revision:
- 5:3762170b6d4d
- Parent:
- 3:7a284390b0ce
Synchronized with git revision 2eb940b9a73af188d3004a2575fdfbb05febe62b
Full URL: https://github.com/mbedmicro/mbed/commit/2eb940b9a73af188d3004a2575fdfbb05febe62b/
Added option to build rpc library. closes #1426
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:fdd22bb7aa52 | 1 | /* ---------------------------------------------------------------------- |
mbed_official | 5:3762170b6d4d | 2 | * Copyright (C) 2010-2014 ARM Limited. All rights reserved. |
emilmont | 1:fdd22bb7aa52 | 3 | * |
mbed_official | 5:3762170b6d4d | 4 | * $Date: 19. March 2015 |
mbed_official | 5:3762170b6d4d | 5 | * $Revision: V.1.4.5 |
emilmont | 1:fdd22bb7aa52 | 6 | * |
emilmont | 2:da51fb522205 | 7 | * Project: CMSIS DSP Library |
emilmont | 2:da51fb522205 | 8 | * Title: arm_lms_norm_f32.c |
emilmont | 1:fdd22bb7aa52 | 9 | * |
emilmont | 2:da51fb522205 | 10 | * Description: Processing function for the floating-point Normalised LMS. |
emilmont | 1:fdd22bb7aa52 | 11 | * |
emilmont | 1:fdd22bb7aa52 | 12 | * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 |
emilmont | 1:fdd22bb7aa52 | 13 | * |
mbed_official | 3:7a284390b0ce | 14 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 3:7a284390b0ce | 15 | * modification, are permitted provided that the following conditions |
mbed_official | 3:7a284390b0ce | 16 | * are met: |
mbed_official | 3:7a284390b0ce | 17 | * - Redistributions of source code must retain the above copyright |
mbed_official | 3:7a284390b0ce | 18 | * notice, this list of conditions and the following disclaimer. |
mbed_official | 3:7a284390b0ce | 19 | * - Redistributions in binary form must reproduce the above copyright |
mbed_official | 3:7a284390b0ce | 20 | * notice, this list of conditions and the following disclaimer in |
mbed_official | 3:7a284390b0ce | 21 | * the documentation and/or other materials provided with the |
mbed_official | 3:7a284390b0ce | 22 | * distribution. |
mbed_official | 3:7a284390b0ce | 23 | * - Neither the name of ARM LIMITED nor the names of its contributors |
mbed_official | 3:7a284390b0ce | 24 | * may be used to endorse or promote products derived from this |
mbed_official | 3:7a284390b0ce | 25 | * software without specific prior written permission. |
mbed_official | 3:7a284390b0ce | 26 | * |
mbed_official | 3:7a284390b0ce | 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
mbed_official | 3:7a284390b0ce | 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
mbed_official | 3:7a284390b0ce | 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
mbed_official | 3:7a284390b0ce | 30 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
mbed_official | 3:7a284390b0ce | 31 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
mbed_official | 3:7a284390b0ce | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
mbed_official | 3:7a284390b0ce | 33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
mbed_official | 3:7a284390b0ce | 34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 3:7a284390b0ce | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
mbed_official | 3:7a284390b0ce | 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
mbed_official | 3:7a284390b0ce | 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 3:7a284390b0ce | 38 | * POSSIBILITY OF SUCH DAMAGE. |
emilmont | 1:fdd22bb7aa52 | 39 | * -------------------------------------------------------------------- */ |
emilmont | 1:fdd22bb7aa52 | 40 | |
emilmont | 1:fdd22bb7aa52 | 41 | #include "arm_math.h" |
emilmont | 1:fdd22bb7aa52 | 42 | |
emilmont | 1:fdd22bb7aa52 | 43 | /** |
emilmont | 1:fdd22bb7aa52 | 44 | * @ingroup groupFilters |
emilmont | 1:fdd22bb7aa52 | 45 | */ |
emilmont | 1:fdd22bb7aa52 | 46 | |
emilmont | 1:fdd22bb7aa52 | 47 | /** |
emilmont | 1:fdd22bb7aa52 | 48 | * @defgroup LMS_NORM Normalized LMS Filters |
emilmont | 1:fdd22bb7aa52 | 49 | * |
emilmont | 1:fdd22bb7aa52 | 50 | * This set of functions implements a commonly used adaptive filter. |
emilmont | 1:fdd22bb7aa52 | 51 | * It is related to the Least Mean Square (LMS) adaptive filter and includes an additional normalization |
emilmont | 1:fdd22bb7aa52 | 52 | * factor which increases the adaptation rate of the filter. |
emilmont | 1:fdd22bb7aa52 | 53 | * The CMSIS DSP Library contains normalized LMS filter functions that operate on Q15, Q31, and floating-point data types. |
emilmont | 1:fdd22bb7aa52 | 54 | * |
emilmont | 1:fdd22bb7aa52 | 55 | * A normalized least mean square (NLMS) filter consists of two components as shown below. |
emilmont | 1:fdd22bb7aa52 | 56 | * The first component is a standard transversal or FIR filter. |
emilmont | 1:fdd22bb7aa52 | 57 | * The second component is a coefficient update mechanism. |
emilmont | 1:fdd22bb7aa52 | 58 | * The NLMS filter has two input signals. |
emilmont | 1:fdd22bb7aa52 | 59 | * The "input" feeds the FIR filter while the "reference input" corresponds to the desired output of the FIR filter. |
emilmont | 1:fdd22bb7aa52 | 60 | * That is, the FIR filter coefficients are updated so that the output of the FIR filter matches the reference input. |
emilmont | 1:fdd22bb7aa52 | 61 | * The filter coefficient update mechanism is based on the difference between the FIR filter output and the reference input. |
emilmont | 1:fdd22bb7aa52 | 62 | * This "error signal" tends towards zero as the filter adapts. |
emilmont | 1:fdd22bb7aa52 | 63 | * The NLMS processing functions accept the input and reference input signals and generate the filter output and error signal. |
emilmont | 1:fdd22bb7aa52 | 64 | * \image html LMS.gif "Internal structure of the NLMS adaptive filter" |
emilmont | 1:fdd22bb7aa52 | 65 | * |
emilmont | 1:fdd22bb7aa52 | 66 | * The functions operate on blocks of data and each call to the function processes |
emilmont | 1:fdd22bb7aa52 | 67 | * <code>blockSize</code> samples through the filter. |
emilmont | 1:fdd22bb7aa52 | 68 | * <code>pSrc</code> points to input signal, <code>pRef</code> points to reference signal, |
emilmont | 1:fdd22bb7aa52 | 69 | * <code>pOut</code> points to output signal and <code>pErr</code> points to error signal. |
emilmont | 1:fdd22bb7aa52 | 70 | * All arrays contain <code>blockSize</code> values. |
emilmont | 1:fdd22bb7aa52 | 71 | * |
emilmont | 1:fdd22bb7aa52 | 72 | * The functions operate on a block-by-block basis. |
emilmont | 1:fdd22bb7aa52 | 73 | * Internally, the filter coefficients <code>b[n]</code> are updated on a sample-by-sample basis. |
emilmont | 1:fdd22bb7aa52 | 74 | * The convergence of the LMS filter is slower compared to the normalized LMS algorithm. |
emilmont | 1:fdd22bb7aa52 | 75 | * |
emilmont | 1:fdd22bb7aa52 | 76 | * \par Algorithm: |
emilmont | 1:fdd22bb7aa52 | 77 | * The output signal <code>y[n]</code> is computed by a standard FIR filter: |
emilmont | 1:fdd22bb7aa52 | 78 | * <pre> |
emilmont | 1:fdd22bb7aa52 | 79 | * y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1] |
emilmont | 1:fdd22bb7aa52 | 80 | * </pre> |
emilmont | 1:fdd22bb7aa52 | 81 | * |
emilmont | 1:fdd22bb7aa52 | 82 | * \par |
emilmont | 1:fdd22bb7aa52 | 83 | * The error signal equals the difference between the reference signal <code>d[n]</code> and the filter output: |
emilmont | 1:fdd22bb7aa52 | 84 | * <pre> |
emilmont | 1:fdd22bb7aa52 | 85 | * e[n] = d[n] - y[n]. |
emilmont | 1:fdd22bb7aa52 | 86 | * </pre> |
emilmont | 1:fdd22bb7aa52 | 87 | * |
emilmont | 1:fdd22bb7aa52 | 88 | * \par |
emilmont | 1:fdd22bb7aa52 | 89 | * After each sample of the error signal is computed the instanteous energy of the filter state variables is calculated: |
emilmont | 1:fdd22bb7aa52 | 90 | * <pre> |
emilmont | 1:fdd22bb7aa52 | 91 | * E = x[n]^2 + x[n-1]^2 + ... + x[n-numTaps+1]^2. |
emilmont | 1:fdd22bb7aa52 | 92 | * </pre> |
emilmont | 1:fdd22bb7aa52 | 93 | * The filter coefficients <code>b[k]</code> are then updated on a sample-by-sample basis: |
emilmont | 1:fdd22bb7aa52 | 94 | * <pre> |
emilmont | 1:fdd22bb7aa52 | 95 | * b[k] = b[k] + e[n] * (mu/E) * x[n-k], for k=0, 1, ..., numTaps-1 |
emilmont | 1:fdd22bb7aa52 | 96 | * </pre> |
emilmont | 1:fdd22bb7aa52 | 97 | * where <code>mu</code> is the step size and controls the rate of coefficient convergence. |
emilmont | 1:fdd22bb7aa52 | 98 | *\par |
emilmont | 1:fdd22bb7aa52 | 99 | * In the APIs, <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>. |
emilmont | 1:fdd22bb7aa52 | 100 | * Coefficients are stored in time reversed order. |
emilmont | 1:fdd22bb7aa52 | 101 | * \par |
emilmont | 1:fdd22bb7aa52 | 102 | * <pre> |
emilmont | 1:fdd22bb7aa52 | 103 | * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} |
emilmont | 1:fdd22bb7aa52 | 104 | * </pre> |
emilmont | 1:fdd22bb7aa52 | 105 | * \par |
emilmont | 1:fdd22bb7aa52 | 106 | * <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>. |
emilmont | 1:fdd22bb7aa52 | 107 | * Samples in the state buffer are stored in the order: |
emilmont | 1:fdd22bb7aa52 | 108 | * \par |
emilmont | 1:fdd22bb7aa52 | 109 | * <pre> |
emilmont | 1:fdd22bb7aa52 | 110 | * {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]} |
emilmont | 1:fdd22bb7aa52 | 111 | * </pre> |
emilmont | 1:fdd22bb7aa52 | 112 | * \par |
emilmont | 1:fdd22bb7aa52 | 113 | * Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code> samples. |
emilmont | 1:fdd22bb7aa52 | 114 | * The increased state buffer length allows circular addressing, which is traditionally used in FIR filters, |
emilmont | 1:fdd22bb7aa52 | 115 | * to be avoided and yields a significant speed improvement. |
emilmont | 1:fdd22bb7aa52 | 116 | * The state variables are updated after each block of data is processed. |
emilmont | 1:fdd22bb7aa52 | 117 | * \par Instance Structure |
emilmont | 1:fdd22bb7aa52 | 118 | * The coefficients and state variables for a filter are stored together in an instance data structure. |
emilmont | 1:fdd22bb7aa52 | 119 | * A separate instance structure must be defined for each filter and |
emilmont | 1:fdd22bb7aa52 | 120 | * coefficient and state arrays cannot be shared among instances. |
emilmont | 1:fdd22bb7aa52 | 121 | * There are separate instance structure declarations for each of the 3 supported data types. |
emilmont | 1:fdd22bb7aa52 | 122 | * |
emilmont | 1:fdd22bb7aa52 | 123 | * \par Initialization Functions |
emilmont | 1:fdd22bb7aa52 | 124 | * There is also an associated initialization function for each data type. |
emilmont | 1:fdd22bb7aa52 | 125 | * The initialization function performs the following operations: |
emilmont | 1:fdd22bb7aa52 | 126 | * - Sets the values of the internal structure fields. |
emilmont | 1:fdd22bb7aa52 | 127 | * - Zeros out the values in the state buffer. |
mbed_official | 3:7a284390b0ce | 128 | * To do this manually without calling the init function, assign the follow subfields of the instance structure: |
mbed_official | 3:7a284390b0ce | 129 | * numTaps, pCoeffs, mu, energy, x0, pState. Also set all of the values in pState to zero. |
mbed_official | 3:7a284390b0ce | 130 | * For Q7, Q15, and Q31 the following fields must also be initialized; |
mbed_official | 3:7a284390b0ce | 131 | * recipTable, postShift |
mbed_official | 3:7a284390b0ce | 132 | * |
emilmont | 1:fdd22bb7aa52 | 133 | * \par |
emilmont | 1:fdd22bb7aa52 | 134 | * Instance structure cannot be placed into a const data section and it is recommended to use the initialization function. |
emilmont | 1:fdd22bb7aa52 | 135 | * \par Fixed-Point Behavior: |
emilmont | 1:fdd22bb7aa52 | 136 | * Care must be taken when using the Q15 and Q31 versions of the normalised LMS filter. |
emilmont | 1:fdd22bb7aa52 | 137 | * The following issues must be considered: |
emilmont | 1:fdd22bb7aa52 | 138 | * - Scaling of coefficients |
emilmont | 1:fdd22bb7aa52 | 139 | * - Overflow and saturation |
emilmont | 1:fdd22bb7aa52 | 140 | * |
emilmont | 1:fdd22bb7aa52 | 141 | * \par Scaling of Coefficients: |
emilmont | 1:fdd22bb7aa52 | 142 | * Filter coefficients are represented as fractional values and |
emilmont | 1:fdd22bb7aa52 | 143 | * coefficients are restricted to lie in the range <code>[-1 +1)</code>. |
emilmont | 1:fdd22bb7aa52 | 144 | * The fixed-point functions have an additional scaling parameter <code>postShift</code>. |
emilmont | 1:fdd22bb7aa52 | 145 | * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits. |
emilmont | 1:fdd22bb7aa52 | 146 | * This essentially scales the filter coefficients by <code>2^postShift</code> and |
emilmont | 1:fdd22bb7aa52 | 147 | * allows the filter coefficients to exceed the range <code>[+1 -1)</code>. |
emilmont | 1:fdd22bb7aa52 | 148 | * The value of <code>postShift</code> is set by the user based on the expected gain through the system being modeled. |
emilmont | 1:fdd22bb7aa52 | 149 | * |
emilmont | 1:fdd22bb7aa52 | 150 | * \par Overflow and Saturation: |
emilmont | 1:fdd22bb7aa52 | 151 | * Overflow and saturation behavior of the fixed-point Q15 and Q31 versions are |
emilmont | 1:fdd22bb7aa52 | 152 | * described separately as part of the function specific documentation below. |
emilmont | 1:fdd22bb7aa52 | 153 | */ |
emilmont | 1:fdd22bb7aa52 | 154 | |
emilmont | 1:fdd22bb7aa52 | 155 | |
emilmont | 1:fdd22bb7aa52 | 156 | /** |
emilmont | 1:fdd22bb7aa52 | 157 | * @addtogroup LMS_NORM |
emilmont | 1:fdd22bb7aa52 | 158 | * @{ |
emilmont | 1:fdd22bb7aa52 | 159 | */ |
emilmont | 1:fdd22bb7aa52 | 160 | |
emilmont | 1:fdd22bb7aa52 | 161 | |
emilmont | 1:fdd22bb7aa52 | 162 | /** |
emilmont | 1:fdd22bb7aa52 | 163 | * @brief Processing function for floating-point normalized LMS filter. |
emilmont | 1:fdd22bb7aa52 | 164 | * @param[in] *S points to an instance of the floating-point normalized LMS filter structure. |
emilmont | 1:fdd22bb7aa52 | 165 | * @param[in] *pSrc points to the block of input data. |
emilmont | 1:fdd22bb7aa52 | 166 | * @param[in] *pRef points to the block of reference data. |
emilmont | 1:fdd22bb7aa52 | 167 | * @param[out] *pOut points to the block of output data. |
emilmont | 1:fdd22bb7aa52 | 168 | * @param[out] *pErr points to the block of error data. |
emilmont | 1:fdd22bb7aa52 | 169 | * @param[in] blockSize number of samples to process. |
emilmont | 1:fdd22bb7aa52 | 170 | * @return none. |
emilmont | 1:fdd22bb7aa52 | 171 | */ |
emilmont | 1:fdd22bb7aa52 | 172 | |
emilmont | 1:fdd22bb7aa52 | 173 | void arm_lms_norm_f32( |
emilmont | 1:fdd22bb7aa52 | 174 | arm_lms_norm_instance_f32 * S, |
emilmont | 1:fdd22bb7aa52 | 175 | float32_t * pSrc, |
emilmont | 1:fdd22bb7aa52 | 176 | float32_t * pRef, |
emilmont | 1:fdd22bb7aa52 | 177 | float32_t * pOut, |
emilmont | 1:fdd22bb7aa52 | 178 | float32_t * pErr, |
emilmont | 1:fdd22bb7aa52 | 179 | uint32_t blockSize) |
emilmont | 1:fdd22bb7aa52 | 180 | { |
emilmont | 1:fdd22bb7aa52 | 181 | float32_t *pState = S->pState; /* State pointer */ |
emilmont | 1:fdd22bb7aa52 | 182 | float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ |
emilmont | 1:fdd22bb7aa52 | 183 | float32_t *pStateCurnt; /* Points to the current sample of the state */ |
emilmont | 1:fdd22bb7aa52 | 184 | float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ |
emilmont | 1:fdd22bb7aa52 | 185 | float32_t mu = S->mu; /* Adaptive factor */ |
emilmont | 1:fdd22bb7aa52 | 186 | uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ |
emilmont | 1:fdd22bb7aa52 | 187 | uint32_t tapCnt, blkCnt; /* Loop counters */ |
emilmont | 1:fdd22bb7aa52 | 188 | float32_t energy; /* Energy of the input */ |
emilmont | 1:fdd22bb7aa52 | 189 | float32_t sum, e, d; /* accumulator, error, reference data sample */ |
emilmont | 1:fdd22bb7aa52 | 190 | float32_t w, x0, in; /* weight factor, temporary variable to hold input sample and state */ |
emilmont | 1:fdd22bb7aa52 | 191 | |
emilmont | 1:fdd22bb7aa52 | 192 | /* Initializations of error, difference, Coefficient update */ |
emilmont | 1:fdd22bb7aa52 | 193 | e = 0.0f; |
emilmont | 1:fdd22bb7aa52 | 194 | d = 0.0f; |
emilmont | 1:fdd22bb7aa52 | 195 | w = 0.0f; |
emilmont | 1:fdd22bb7aa52 | 196 | |
emilmont | 1:fdd22bb7aa52 | 197 | energy = S->energy; |
emilmont | 1:fdd22bb7aa52 | 198 | x0 = S->x0; |
emilmont | 1:fdd22bb7aa52 | 199 | |
emilmont | 1:fdd22bb7aa52 | 200 | /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */ |
emilmont | 1:fdd22bb7aa52 | 201 | /* pStateCurnt points to the location where the new input data should be written */ |
emilmont | 1:fdd22bb7aa52 | 202 | pStateCurnt = &(S->pState[(numTaps - 1u)]); |
emilmont | 1:fdd22bb7aa52 | 203 | |
emilmont | 1:fdd22bb7aa52 | 204 | /* Loop over blockSize number of values */ |
emilmont | 1:fdd22bb7aa52 | 205 | blkCnt = blockSize; |
emilmont | 1:fdd22bb7aa52 | 206 | |
emilmont | 1:fdd22bb7aa52 | 207 | |
mbed_official | 3:7a284390b0ce | 208 | #ifndef ARM_MATH_CM0_FAMILY |
emilmont | 1:fdd22bb7aa52 | 209 | |
emilmont | 1:fdd22bb7aa52 | 210 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
emilmont | 1:fdd22bb7aa52 | 211 | |
emilmont | 1:fdd22bb7aa52 | 212 | while(blkCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 213 | { |
emilmont | 1:fdd22bb7aa52 | 214 | /* Copy the new input sample into the state buffer */ |
emilmont | 1:fdd22bb7aa52 | 215 | *pStateCurnt++ = *pSrc; |
emilmont | 1:fdd22bb7aa52 | 216 | |
emilmont | 1:fdd22bb7aa52 | 217 | /* Initialize pState pointer */ |
emilmont | 1:fdd22bb7aa52 | 218 | px = pState; |
emilmont | 1:fdd22bb7aa52 | 219 | |
emilmont | 1:fdd22bb7aa52 | 220 | /* Initialize coeff pointer */ |
emilmont | 1:fdd22bb7aa52 | 221 | pb = (pCoeffs); |
emilmont | 1:fdd22bb7aa52 | 222 | |
emilmont | 1:fdd22bb7aa52 | 223 | /* Read the sample from input buffer */ |
emilmont | 1:fdd22bb7aa52 | 224 | in = *pSrc++; |
emilmont | 1:fdd22bb7aa52 | 225 | |
emilmont | 1:fdd22bb7aa52 | 226 | /* Update the energy calculation */ |
emilmont | 1:fdd22bb7aa52 | 227 | energy -= x0 * x0; |
emilmont | 1:fdd22bb7aa52 | 228 | energy += in * in; |
emilmont | 1:fdd22bb7aa52 | 229 | |
emilmont | 1:fdd22bb7aa52 | 230 | /* Set the accumulator to zero */ |
emilmont | 1:fdd22bb7aa52 | 231 | sum = 0.0f; |
emilmont | 1:fdd22bb7aa52 | 232 | |
emilmont | 1:fdd22bb7aa52 | 233 | /* Loop unrolling. Process 4 taps at a time. */ |
emilmont | 1:fdd22bb7aa52 | 234 | tapCnt = numTaps >> 2; |
emilmont | 1:fdd22bb7aa52 | 235 | |
emilmont | 1:fdd22bb7aa52 | 236 | while(tapCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 237 | { |
emilmont | 1:fdd22bb7aa52 | 238 | /* Perform the multiply-accumulate */ |
emilmont | 1:fdd22bb7aa52 | 239 | sum += (*px++) * (*pb++); |
emilmont | 1:fdd22bb7aa52 | 240 | sum += (*px++) * (*pb++); |
emilmont | 1:fdd22bb7aa52 | 241 | sum += (*px++) * (*pb++); |
emilmont | 1:fdd22bb7aa52 | 242 | sum += (*px++) * (*pb++); |
emilmont | 1:fdd22bb7aa52 | 243 | |
emilmont | 1:fdd22bb7aa52 | 244 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 245 | tapCnt--; |
emilmont | 1:fdd22bb7aa52 | 246 | } |
emilmont | 1:fdd22bb7aa52 | 247 | |
emilmont | 1:fdd22bb7aa52 | 248 | /* If the filter length is not a multiple of 4, compute the remaining filter taps */ |
emilmont | 1:fdd22bb7aa52 | 249 | tapCnt = numTaps % 0x4u; |
emilmont | 1:fdd22bb7aa52 | 250 | |
emilmont | 1:fdd22bb7aa52 | 251 | while(tapCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 252 | { |
emilmont | 1:fdd22bb7aa52 | 253 | /* Perform the multiply-accumulate */ |
emilmont | 1:fdd22bb7aa52 | 254 | sum += (*px++) * (*pb++); |
emilmont | 1:fdd22bb7aa52 | 255 | |
emilmont | 1:fdd22bb7aa52 | 256 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 257 | tapCnt--; |
emilmont | 1:fdd22bb7aa52 | 258 | } |
emilmont | 1:fdd22bb7aa52 | 259 | |
emilmont | 1:fdd22bb7aa52 | 260 | /* The result in the accumulator, store in the destination buffer. */ |
emilmont | 1:fdd22bb7aa52 | 261 | *pOut++ = sum; |
emilmont | 1:fdd22bb7aa52 | 262 | |
emilmont | 1:fdd22bb7aa52 | 263 | /* Compute and store error */ |
emilmont | 1:fdd22bb7aa52 | 264 | d = (float32_t) (*pRef++); |
emilmont | 1:fdd22bb7aa52 | 265 | e = d - sum; |
emilmont | 1:fdd22bb7aa52 | 266 | *pErr++ = e; |
emilmont | 1:fdd22bb7aa52 | 267 | |
emilmont | 1:fdd22bb7aa52 | 268 | /* Calculation of Weighting factor for updating filter coefficients */ |
emilmont | 1:fdd22bb7aa52 | 269 | /* epsilon value 0.000000119209289f */ |
emilmont | 1:fdd22bb7aa52 | 270 | w = (e * mu) / (energy + 0.000000119209289f); |
emilmont | 1:fdd22bb7aa52 | 271 | |
emilmont | 1:fdd22bb7aa52 | 272 | /* Initialize pState pointer */ |
emilmont | 1:fdd22bb7aa52 | 273 | px = pState; |
emilmont | 1:fdd22bb7aa52 | 274 | |
emilmont | 1:fdd22bb7aa52 | 275 | /* Initialize coeff pointer */ |
emilmont | 1:fdd22bb7aa52 | 276 | pb = (pCoeffs); |
emilmont | 1:fdd22bb7aa52 | 277 | |
emilmont | 1:fdd22bb7aa52 | 278 | /* Loop unrolling. Process 4 taps at a time. */ |
emilmont | 1:fdd22bb7aa52 | 279 | tapCnt = numTaps >> 2; |
emilmont | 1:fdd22bb7aa52 | 280 | |
emilmont | 1:fdd22bb7aa52 | 281 | /* Update filter coefficients */ |
emilmont | 1:fdd22bb7aa52 | 282 | while(tapCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 283 | { |
emilmont | 1:fdd22bb7aa52 | 284 | /* Perform the multiply-accumulate */ |
emilmont | 1:fdd22bb7aa52 | 285 | *pb += w * (*px++); |
emilmont | 1:fdd22bb7aa52 | 286 | pb++; |
emilmont | 1:fdd22bb7aa52 | 287 | |
emilmont | 1:fdd22bb7aa52 | 288 | *pb += w * (*px++); |
emilmont | 1:fdd22bb7aa52 | 289 | pb++; |
emilmont | 1:fdd22bb7aa52 | 290 | |
emilmont | 1:fdd22bb7aa52 | 291 | *pb += w * (*px++); |
emilmont | 1:fdd22bb7aa52 | 292 | pb++; |
emilmont | 1:fdd22bb7aa52 | 293 | |
emilmont | 1:fdd22bb7aa52 | 294 | *pb += w * (*px++); |
emilmont | 1:fdd22bb7aa52 | 295 | pb++; |
emilmont | 1:fdd22bb7aa52 | 296 | |
emilmont | 1:fdd22bb7aa52 | 297 | |
emilmont | 1:fdd22bb7aa52 | 298 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 299 | tapCnt--; |
emilmont | 1:fdd22bb7aa52 | 300 | } |
emilmont | 1:fdd22bb7aa52 | 301 | |
emilmont | 1:fdd22bb7aa52 | 302 | /* If the filter length is not a multiple of 4, compute the remaining filter taps */ |
emilmont | 1:fdd22bb7aa52 | 303 | tapCnt = numTaps % 0x4u; |
emilmont | 1:fdd22bb7aa52 | 304 | |
emilmont | 1:fdd22bb7aa52 | 305 | while(tapCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 306 | { |
emilmont | 1:fdd22bb7aa52 | 307 | /* Perform the multiply-accumulate */ |
emilmont | 1:fdd22bb7aa52 | 308 | *pb += w * (*px++); |
emilmont | 1:fdd22bb7aa52 | 309 | pb++; |
emilmont | 1:fdd22bb7aa52 | 310 | |
emilmont | 1:fdd22bb7aa52 | 311 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 312 | tapCnt--; |
emilmont | 1:fdd22bb7aa52 | 313 | } |
emilmont | 1:fdd22bb7aa52 | 314 | |
emilmont | 1:fdd22bb7aa52 | 315 | x0 = *pState; |
emilmont | 1:fdd22bb7aa52 | 316 | |
emilmont | 1:fdd22bb7aa52 | 317 | /* Advance state pointer by 1 for the next sample */ |
emilmont | 1:fdd22bb7aa52 | 318 | pState = pState + 1; |
emilmont | 1:fdd22bb7aa52 | 319 | |
emilmont | 1:fdd22bb7aa52 | 320 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 321 | blkCnt--; |
emilmont | 1:fdd22bb7aa52 | 322 | } |
emilmont | 1:fdd22bb7aa52 | 323 | |
emilmont | 1:fdd22bb7aa52 | 324 | S->energy = energy; |
emilmont | 1:fdd22bb7aa52 | 325 | S->x0 = x0; |
emilmont | 1:fdd22bb7aa52 | 326 | |
emilmont | 1:fdd22bb7aa52 | 327 | /* Processing is complete. Now copy the last numTaps - 1 samples to the |
emilmont | 1:fdd22bb7aa52 | 328 | satrt of the state buffer. This prepares the state buffer for the |
emilmont | 1:fdd22bb7aa52 | 329 | next function call. */ |
emilmont | 1:fdd22bb7aa52 | 330 | |
emilmont | 1:fdd22bb7aa52 | 331 | /* Points to the start of the pState buffer */ |
emilmont | 1:fdd22bb7aa52 | 332 | pStateCurnt = S->pState; |
emilmont | 1:fdd22bb7aa52 | 333 | |
emilmont | 1:fdd22bb7aa52 | 334 | /* Loop unrolling for (numTaps - 1u)/4 samples copy */ |
emilmont | 1:fdd22bb7aa52 | 335 | tapCnt = (numTaps - 1u) >> 2u; |
emilmont | 1:fdd22bb7aa52 | 336 | |
emilmont | 1:fdd22bb7aa52 | 337 | /* copy data */ |
emilmont | 1:fdd22bb7aa52 | 338 | while(tapCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 339 | { |
emilmont | 1:fdd22bb7aa52 | 340 | *pStateCurnt++ = *pState++; |
emilmont | 1:fdd22bb7aa52 | 341 | *pStateCurnt++ = *pState++; |
emilmont | 1:fdd22bb7aa52 | 342 | *pStateCurnt++ = *pState++; |
emilmont | 1:fdd22bb7aa52 | 343 | *pStateCurnt++ = *pState++; |
emilmont | 1:fdd22bb7aa52 | 344 | |
emilmont | 1:fdd22bb7aa52 | 345 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 346 | tapCnt--; |
emilmont | 1:fdd22bb7aa52 | 347 | } |
emilmont | 1:fdd22bb7aa52 | 348 | |
emilmont | 1:fdd22bb7aa52 | 349 | /* Calculate remaining number of copies */ |
emilmont | 1:fdd22bb7aa52 | 350 | tapCnt = (numTaps - 1u) % 0x4u; |
emilmont | 1:fdd22bb7aa52 | 351 | |
emilmont | 1:fdd22bb7aa52 | 352 | /* Copy the remaining q31_t data */ |
emilmont | 1:fdd22bb7aa52 | 353 | while(tapCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 354 | { |
emilmont | 1:fdd22bb7aa52 | 355 | *pStateCurnt++ = *pState++; |
emilmont | 1:fdd22bb7aa52 | 356 | |
emilmont | 1:fdd22bb7aa52 | 357 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 358 | tapCnt--; |
emilmont | 1:fdd22bb7aa52 | 359 | } |
emilmont | 1:fdd22bb7aa52 | 360 | |
emilmont | 1:fdd22bb7aa52 | 361 | #else |
emilmont | 1:fdd22bb7aa52 | 362 | |
emilmont | 1:fdd22bb7aa52 | 363 | /* Run the below code for Cortex-M0 */ |
emilmont | 1:fdd22bb7aa52 | 364 | |
emilmont | 1:fdd22bb7aa52 | 365 | while(blkCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 366 | { |
emilmont | 1:fdd22bb7aa52 | 367 | /* Copy the new input sample into the state buffer */ |
emilmont | 1:fdd22bb7aa52 | 368 | *pStateCurnt++ = *pSrc; |
emilmont | 1:fdd22bb7aa52 | 369 | |
emilmont | 1:fdd22bb7aa52 | 370 | /* Initialize pState pointer */ |
emilmont | 1:fdd22bb7aa52 | 371 | px = pState; |
emilmont | 1:fdd22bb7aa52 | 372 | |
emilmont | 1:fdd22bb7aa52 | 373 | /* Initialize pCoeffs pointer */ |
emilmont | 1:fdd22bb7aa52 | 374 | pb = pCoeffs; |
emilmont | 1:fdd22bb7aa52 | 375 | |
emilmont | 1:fdd22bb7aa52 | 376 | /* Read the sample from input buffer */ |
emilmont | 1:fdd22bb7aa52 | 377 | in = *pSrc++; |
emilmont | 1:fdd22bb7aa52 | 378 | |
emilmont | 1:fdd22bb7aa52 | 379 | /* Update the energy calculation */ |
emilmont | 1:fdd22bb7aa52 | 380 | energy -= x0 * x0; |
emilmont | 1:fdd22bb7aa52 | 381 | energy += in * in; |
emilmont | 1:fdd22bb7aa52 | 382 | |
emilmont | 1:fdd22bb7aa52 | 383 | /* Set the accumulator to zero */ |
emilmont | 1:fdd22bb7aa52 | 384 | sum = 0.0f; |
emilmont | 1:fdd22bb7aa52 | 385 | |
emilmont | 1:fdd22bb7aa52 | 386 | /* Loop over numTaps number of values */ |
emilmont | 1:fdd22bb7aa52 | 387 | tapCnt = numTaps; |
emilmont | 1:fdd22bb7aa52 | 388 | |
emilmont | 1:fdd22bb7aa52 | 389 | while(tapCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 390 | { |
emilmont | 1:fdd22bb7aa52 | 391 | /* Perform the multiply-accumulate */ |
emilmont | 1:fdd22bb7aa52 | 392 | sum += (*px++) * (*pb++); |
emilmont | 1:fdd22bb7aa52 | 393 | |
emilmont | 1:fdd22bb7aa52 | 394 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 395 | tapCnt--; |
emilmont | 1:fdd22bb7aa52 | 396 | } |
emilmont | 1:fdd22bb7aa52 | 397 | |
emilmont | 1:fdd22bb7aa52 | 398 | /* The result in the accumulator is stored in the destination buffer. */ |
emilmont | 1:fdd22bb7aa52 | 399 | *pOut++ = sum; |
emilmont | 1:fdd22bb7aa52 | 400 | |
emilmont | 1:fdd22bb7aa52 | 401 | /* Compute and store error */ |
emilmont | 1:fdd22bb7aa52 | 402 | d = (float32_t) (*pRef++); |
emilmont | 1:fdd22bb7aa52 | 403 | e = d - sum; |
emilmont | 1:fdd22bb7aa52 | 404 | *pErr++ = e; |
emilmont | 1:fdd22bb7aa52 | 405 | |
emilmont | 1:fdd22bb7aa52 | 406 | /* Calculation of Weighting factor for updating filter coefficients */ |
emilmont | 1:fdd22bb7aa52 | 407 | /* epsilon value 0.000000119209289f */ |
emilmont | 1:fdd22bb7aa52 | 408 | w = (e * mu) / (energy + 0.000000119209289f); |
emilmont | 1:fdd22bb7aa52 | 409 | |
emilmont | 1:fdd22bb7aa52 | 410 | /* Initialize pState pointer */ |
emilmont | 1:fdd22bb7aa52 | 411 | px = pState; |
emilmont | 1:fdd22bb7aa52 | 412 | |
emilmont | 1:fdd22bb7aa52 | 413 | /* Initialize pCcoeffs pointer */ |
emilmont | 1:fdd22bb7aa52 | 414 | pb = pCoeffs; |
emilmont | 1:fdd22bb7aa52 | 415 | |
emilmont | 1:fdd22bb7aa52 | 416 | /* Loop over numTaps number of values */ |
emilmont | 1:fdd22bb7aa52 | 417 | tapCnt = numTaps; |
emilmont | 1:fdd22bb7aa52 | 418 | |
emilmont | 1:fdd22bb7aa52 | 419 | while(tapCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 420 | { |
emilmont | 1:fdd22bb7aa52 | 421 | /* Perform the multiply-accumulate */ |
emilmont | 1:fdd22bb7aa52 | 422 | *pb += w * (*px++); |
emilmont | 1:fdd22bb7aa52 | 423 | pb++; |
emilmont | 1:fdd22bb7aa52 | 424 | |
emilmont | 1:fdd22bb7aa52 | 425 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 426 | tapCnt--; |
emilmont | 1:fdd22bb7aa52 | 427 | } |
emilmont | 1:fdd22bb7aa52 | 428 | |
emilmont | 1:fdd22bb7aa52 | 429 | x0 = *pState; |
emilmont | 1:fdd22bb7aa52 | 430 | |
emilmont | 1:fdd22bb7aa52 | 431 | /* Advance state pointer by 1 for the next sample */ |
emilmont | 1:fdd22bb7aa52 | 432 | pState = pState + 1; |
emilmont | 1:fdd22bb7aa52 | 433 | |
emilmont | 1:fdd22bb7aa52 | 434 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 435 | blkCnt--; |
emilmont | 1:fdd22bb7aa52 | 436 | } |
emilmont | 1:fdd22bb7aa52 | 437 | |
emilmont | 1:fdd22bb7aa52 | 438 | S->energy = energy; |
emilmont | 1:fdd22bb7aa52 | 439 | S->x0 = x0; |
emilmont | 1:fdd22bb7aa52 | 440 | |
emilmont | 1:fdd22bb7aa52 | 441 | /* Processing is complete. Now copy the last numTaps - 1 samples to the |
emilmont | 1:fdd22bb7aa52 | 442 | satrt of the state buffer. This prepares the state buffer for the |
emilmont | 1:fdd22bb7aa52 | 443 | next function call. */ |
emilmont | 1:fdd22bb7aa52 | 444 | |
emilmont | 1:fdd22bb7aa52 | 445 | /* Points to the start of the pState buffer */ |
emilmont | 1:fdd22bb7aa52 | 446 | pStateCurnt = S->pState; |
emilmont | 1:fdd22bb7aa52 | 447 | |
emilmont | 1:fdd22bb7aa52 | 448 | /* Copy (numTaps - 1u) samples */ |
emilmont | 1:fdd22bb7aa52 | 449 | tapCnt = (numTaps - 1u); |
emilmont | 1:fdd22bb7aa52 | 450 | |
emilmont | 1:fdd22bb7aa52 | 451 | /* Copy the remaining q31_t data */ |
emilmont | 1:fdd22bb7aa52 | 452 | while(tapCnt > 0u) |
emilmont | 1:fdd22bb7aa52 | 453 | { |
emilmont | 1:fdd22bb7aa52 | 454 | *pStateCurnt++ = *pState++; |
emilmont | 1:fdd22bb7aa52 | 455 | |
emilmont | 1:fdd22bb7aa52 | 456 | /* Decrement the loop counter */ |
emilmont | 1:fdd22bb7aa52 | 457 | tapCnt--; |
emilmont | 1:fdd22bb7aa52 | 458 | } |
emilmont | 1:fdd22bb7aa52 | 459 | |
mbed_official | 3:7a284390b0ce | 460 | #endif /* #ifndef ARM_MATH_CM0_FAMILY */ |
emilmont | 1:fdd22bb7aa52 | 461 | |
emilmont | 1:fdd22bb7aa52 | 462 | } |
emilmont | 1:fdd22bb7aa52 | 463 | |
emilmont | 1:fdd22bb7aa52 | 464 | /** |
emilmont | 1:fdd22bb7aa52 | 465 | * @} end of LMS_NORM group |
emilmont | 1:fdd22bb7aa52 | 466 | */ |