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.

Committer:
mbed_official
Date:
Fri Nov 08 13:45:10 2013 +0000
Revision:
3:7a284390b0ce
Parent:
2:da51fb522205
Child:
5:3762170b6d4d
Synchronized with git revision e69956aba2f68a2a26ac26b051f8d349deaa1ce8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:fdd22bb7aa52 1 /* ----------------------------------------------------------------------
mbed_official 3:7a284390b0ce 2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
emilmont 1:fdd22bb7aa52 3 *
mbed_official 3:7a284390b0ce 4 * $Date: 17. January 2013
mbed_official 3:7a284390b0ce 5 * $Revision: V1.4.1
emilmont 1:fdd22bb7aa52 6 *
emilmont 2:da51fb522205 7 * Project: CMSIS DSP Library
emilmont 2:da51fb522205 8 * Title: arm_lms_f32.c
emilmont 1:fdd22bb7aa52 9 *
emilmont 2:da51fb522205 10 * Description: Processing function for the floating-point LMS filter.
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 Least Mean Square (LMS) Filters
emilmont 1:fdd22bb7aa52 49 *
emilmont 1:fdd22bb7aa52 50 * LMS filters are a class of adaptive filters that are able to "learn" an unknown transfer functions.
emilmont 1:fdd22bb7aa52 51 * LMS filters use a gradient descent method in which the filter coefficients are updated based on the instantaneous error signal.
emilmont 1:fdd22bb7aa52 52 * Adaptive filters are often used in communication systems, equalizers, and noise removal.
emilmont 1:fdd22bb7aa52 53 * The CMSIS DSP Library contains LMS filter functions that operate on Q15, Q31, and floating-point data types.
emilmont 1:fdd22bb7aa52 54 * The library also contains normalized LMS filters in which the filter coefficient adaptation is indepedent of the level of the input signal.
emilmont 1:fdd22bb7aa52 55 *
emilmont 1:fdd22bb7aa52 56 * An LMS filter consists of two components as shown below.
emilmont 1:fdd22bb7aa52 57 * The first component is a standard transversal or FIR filter.
emilmont 1:fdd22bb7aa52 58 * The second component is a coefficient update mechanism.
emilmont 1:fdd22bb7aa52 59 * The LMS filter has two input signals.
emilmont 1:fdd22bb7aa52 60 * The "input" feeds the FIR filter while the "reference input" corresponds to the desired output of the FIR filter.
emilmont 1:fdd22bb7aa52 61 * That is, the FIR filter coefficients are updated so that the output of the FIR filter matches the reference input.
emilmont 1:fdd22bb7aa52 62 * The filter coefficient update mechanism is based on the difference between the FIR filter output and the reference input.
emilmont 1:fdd22bb7aa52 63 * This "error signal" tends towards zero as the filter adapts.
emilmont 1:fdd22bb7aa52 64 * The LMS processing functions accept the input and reference input signals and generate the filter output and error signal.
emilmont 1:fdd22bb7aa52 65 * \image html LMS.gif "Internal structure of the Least Mean Square filter"
emilmont 1:fdd22bb7aa52 66 *
emilmont 1:fdd22bb7aa52 67 * The functions operate on blocks of data and each call to the function processes
emilmont 1:fdd22bb7aa52 68 * <code>blockSize</code> samples through the filter.
emilmont 1:fdd22bb7aa52 69 * <code>pSrc</code> points to input signal, <code>pRef</code> points to reference signal,
emilmont 1:fdd22bb7aa52 70 * <code>pOut</code> points to output signal and <code>pErr</code> points to error signal.
emilmont 1:fdd22bb7aa52 71 * All arrays contain <code>blockSize</code> values.
emilmont 1:fdd22bb7aa52 72 *
emilmont 1:fdd22bb7aa52 73 * The functions operate on a block-by-block basis.
emilmont 1:fdd22bb7aa52 74 * Internally, the filter coefficients <code>b[n]</code> are updated on a sample-by-sample basis.
emilmont 1:fdd22bb7aa52 75 * The convergence of the LMS filter is slower compared to the normalized LMS algorithm.
emilmont 1:fdd22bb7aa52 76 *
emilmont 1:fdd22bb7aa52 77 * \par Algorithm:
emilmont 1:fdd22bb7aa52 78 * The output signal <code>y[n]</code> is computed by a standard FIR filter:
emilmont 1:fdd22bb7aa52 79 * <pre>
emilmont 1:fdd22bb7aa52 80 * 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 81 * </pre>
emilmont 1:fdd22bb7aa52 82 *
emilmont 1:fdd22bb7aa52 83 * \par
emilmont 1:fdd22bb7aa52 84 * The error signal equals the difference between the reference signal <code>d[n]</code> and the filter output:
emilmont 1:fdd22bb7aa52 85 * <pre>
emilmont 1:fdd22bb7aa52 86 * e[n] = d[n] - y[n].
emilmont 1:fdd22bb7aa52 87 * </pre>
emilmont 1:fdd22bb7aa52 88 *
emilmont 1:fdd22bb7aa52 89 * \par
emilmont 1:fdd22bb7aa52 90 * After each sample of the error signal is computed, the filter coefficients <code>b[k]</code> are updated on a sample-by-sample basis:
emilmont 1:fdd22bb7aa52 91 * <pre>
emilmont 1:fdd22bb7aa52 92 * b[k] = b[k] + e[n] * mu * x[n-k], for k=0, 1, ..., numTaps-1
emilmont 1:fdd22bb7aa52 93 * </pre>
emilmont 1:fdd22bb7aa52 94 * where <code>mu</code> is the step size and controls the rate of coefficient convergence.
emilmont 1:fdd22bb7aa52 95 *\par
emilmont 1:fdd22bb7aa52 96 * In the APIs, <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>.
emilmont 1:fdd22bb7aa52 97 * Coefficients are stored in time reversed order.
emilmont 1:fdd22bb7aa52 98 * \par
emilmont 1:fdd22bb7aa52 99 * <pre>
emilmont 1:fdd22bb7aa52 100 * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
emilmont 1:fdd22bb7aa52 101 * </pre>
emilmont 1:fdd22bb7aa52 102 * \par
emilmont 1:fdd22bb7aa52 103 * <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>.
emilmont 1:fdd22bb7aa52 104 * Samples in the state buffer are stored in the order:
emilmont 1:fdd22bb7aa52 105 * \par
emilmont 1:fdd22bb7aa52 106 * <pre>
emilmont 1:fdd22bb7aa52 107 * {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 108 * </pre>
emilmont 1:fdd22bb7aa52 109 * \par
emilmont 1:fdd22bb7aa52 110 * Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code> samples.
emilmont 1:fdd22bb7aa52 111 * The increased state buffer length allows circular addressing, which is traditionally used in FIR filters,
emilmont 1:fdd22bb7aa52 112 * to be avoided and yields a significant speed improvement.
emilmont 1:fdd22bb7aa52 113 * The state variables are updated after each block of data is processed.
emilmont 1:fdd22bb7aa52 114 * \par Instance Structure
emilmont 1:fdd22bb7aa52 115 * The coefficients and state variables for a filter are stored together in an instance data structure.
emilmont 1:fdd22bb7aa52 116 * A separate instance structure must be defined for each filter and
emilmont 1:fdd22bb7aa52 117 * coefficient and state arrays cannot be shared among instances.
emilmont 1:fdd22bb7aa52 118 * There are separate instance structure declarations for each of the 3 supported data types.
emilmont 1:fdd22bb7aa52 119 *
emilmont 1:fdd22bb7aa52 120 * \par Initialization Functions
emilmont 1:fdd22bb7aa52 121 * There is also an associated initialization function for each data type.
emilmont 1:fdd22bb7aa52 122 * The initialization function performs the following operations:
emilmont 1:fdd22bb7aa52 123 * - Sets the values of the internal structure fields.
emilmont 1:fdd22bb7aa52 124 * - Zeros out the values in the state buffer.
mbed_official 3:7a284390b0ce 125 * To do this manually without calling the init function, assign the follow subfields of the instance structure:
mbed_official 3:7a284390b0ce 126 * numTaps, pCoeffs, mu, postShift (not for f32), pState. Also set all of the values in pState to zero.
mbed_official 3:7a284390b0ce 127 *
emilmont 1:fdd22bb7aa52 128 * \par
emilmont 1:fdd22bb7aa52 129 * Use of the initialization function is optional.
emilmont 1:fdd22bb7aa52 130 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
emilmont 1:fdd22bb7aa52 131 * To place an instance structure into a const data section, the instance structure must be manually initialized.
emilmont 1:fdd22bb7aa52 132 * Set the values in the state buffer to zeros before static initialization.
emilmont 1:fdd22bb7aa52 133 * The code below statically initializes each of the 3 different data type filter instance structures
emilmont 1:fdd22bb7aa52 134 * <pre>
emilmont 1:fdd22bb7aa52 135 * arm_lms_instance_f32 S = {numTaps, pState, pCoeffs, mu};
emilmont 1:fdd22bb7aa52 136 * arm_lms_instance_q31 S = {numTaps, pState, pCoeffs, mu, postShift};
emilmont 1:fdd22bb7aa52 137 * arm_lms_instance_q15 S = {numTaps, pState, pCoeffs, mu, postShift};
emilmont 1:fdd22bb7aa52 138 * </pre>
emilmont 1:fdd22bb7aa52 139 * where <code>numTaps</code> is the number of filter coefficients in the filter; <code>pState</code> is the address of the state buffer;
emilmont 1:fdd22bb7aa52 140 * <code>pCoeffs</code> is the address of the coefficient buffer; <code>mu</code> is the step size parameter; and <code>postShift</code> is the shift applied to coefficients.
emilmont 1:fdd22bb7aa52 141 *
emilmont 1:fdd22bb7aa52 142 * \par Fixed-Point Behavior:
emilmont 1:fdd22bb7aa52 143 * Care must be taken when using the Q15 and Q31 versions of the LMS filter.
emilmont 1:fdd22bb7aa52 144 * The following issues must be considered:
emilmont 1:fdd22bb7aa52 145 * - Scaling of coefficients
emilmont 1:fdd22bb7aa52 146 * - Overflow and saturation
emilmont 1:fdd22bb7aa52 147 *
emilmont 1:fdd22bb7aa52 148 * \par Scaling of Coefficients:
emilmont 1:fdd22bb7aa52 149 * Filter coefficients are represented as fractional values and
emilmont 1:fdd22bb7aa52 150 * coefficients are restricted to lie in the range <code>[-1 +1)</code>.
emilmont 1:fdd22bb7aa52 151 * The fixed-point functions have an additional scaling parameter <code>postShift</code>.
emilmont 1:fdd22bb7aa52 152 * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits.
emilmont 1:fdd22bb7aa52 153 * This essentially scales the filter coefficients by <code>2^postShift</code> and
emilmont 1:fdd22bb7aa52 154 * allows the filter coefficients to exceed the range <code>[+1 -1)</code>.
emilmont 1:fdd22bb7aa52 155 * The value of <code>postShift</code> is set by the user based on the expected gain through the system being modeled.
emilmont 1:fdd22bb7aa52 156 *
emilmont 1:fdd22bb7aa52 157 * \par Overflow and Saturation:
emilmont 1:fdd22bb7aa52 158 * Overflow and saturation behavior of the fixed-point Q15 and Q31 versions are
emilmont 1:fdd22bb7aa52 159 * described separately as part of the function specific documentation below.
emilmont 1:fdd22bb7aa52 160 */
emilmont 1:fdd22bb7aa52 161
emilmont 1:fdd22bb7aa52 162 /**
emilmont 1:fdd22bb7aa52 163 * @addtogroup LMS
emilmont 1:fdd22bb7aa52 164 * @{
emilmont 1:fdd22bb7aa52 165 */
emilmont 1:fdd22bb7aa52 166
emilmont 1:fdd22bb7aa52 167 /**
emilmont 1:fdd22bb7aa52 168 * @details
emilmont 1:fdd22bb7aa52 169 * This function operates on floating-point data types.
emilmont 1:fdd22bb7aa52 170 *
emilmont 1:fdd22bb7aa52 171 * @brief Processing function for floating-point LMS filter.
emilmont 1:fdd22bb7aa52 172 * @param[in] *S points to an instance of the floating-point LMS filter structure.
emilmont 1:fdd22bb7aa52 173 * @param[in] *pSrc points to the block of input data.
emilmont 1:fdd22bb7aa52 174 * @param[in] *pRef points to the block of reference data.
emilmont 1:fdd22bb7aa52 175 * @param[out] *pOut points to the block of output data.
emilmont 1:fdd22bb7aa52 176 * @param[out] *pErr points to the block of error data.
emilmont 1:fdd22bb7aa52 177 * @param[in] blockSize number of samples to process.
emilmont 1:fdd22bb7aa52 178 * @return none.
emilmont 1:fdd22bb7aa52 179 */
emilmont 1:fdd22bb7aa52 180
emilmont 1:fdd22bb7aa52 181 void arm_lms_f32(
emilmont 1:fdd22bb7aa52 182 const arm_lms_instance_f32 * S,
emilmont 1:fdd22bb7aa52 183 float32_t * pSrc,
emilmont 1:fdd22bb7aa52 184 float32_t * pRef,
emilmont 1:fdd22bb7aa52 185 float32_t * pOut,
emilmont 1:fdd22bb7aa52 186 float32_t * pErr,
emilmont 1:fdd22bb7aa52 187 uint32_t blockSize)
emilmont 1:fdd22bb7aa52 188 {
emilmont 1:fdd22bb7aa52 189 float32_t *pState = S->pState; /* State pointer */
emilmont 1:fdd22bb7aa52 190 float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
emilmont 1:fdd22bb7aa52 191 float32_t *pStateCurnt; /* Points to the current sample of the state */
emilmont 1:fdd22bb7aa52 192 float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */
emilmont 1:fdd22bb7aa52 193 float32_t mu = S->mu; /* Adaptive factor */
emilmont 1:fdd22bb7aa52 194 uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
emilmont 1:fdd22bb7aa52 195 uint32_t tapCnt, blkCnt; /* Loop counters */
emilmont 1:fdd22bb7aa52 196 float32_t sum, e, d; /* accumulator, error, reference data sample */
emilmont 1:fdd22bb7aa52 197 float32_t w = 0.0f; /* weight factor */
emilmont 1:fdd22bb7aa52 198
emilmont 1:fdd22bb7aa52 199 e = 0.0f;
emilmont 1:fdd22bb7aa52 200 d = 0.0f;
emilmont 1:fdd22bb7aa52 201
emilmont 1:fdd22bb7aa52 202 /* S->pState points to state array which contains previous frame (numTaps - 1) samples */
emilmont 1:fdd22bb7aa52 203 /* pStateCurnt points to the location where the new input data should be written */
emilmont 1:fdd22bb7aa52 204 pStateCurnt = &(S->pState[(numTaps - 1u)]);
emilmont 1:fdd22bb7aa52 205
emilmont 1:fdd22bb7aa52 206 blkCnt = blockSize;
emilmont 1:fdd22bb7aa52 207
emilmont 1:fdd22bb7aa52 208
mbed_official 3:7a284390b0ce 209 #ifndef ARM_MATH_CM0_FAMILY
emilmont 1:fdd22bb7aa52 210
emilmont 1:fdd22bb7aa52 211 /* Run the below code for Cortex-M4 and Cortex-M3 */
emilmont 1:fdd22bb7aa52 212
emilmont 1:fdd22bb7aa52 213 while(blkCnt > 0u)
emilmont 1:fdd22bb7aa52 214 {
emilmont 1:fdd22bb7aa52 215 /* Copy the new input sample into the state buffer */
emilmont 1:fdd22bb7aa52 216 *pStateCurnt++ = *pSrc++;
emilmont 1:fdd22bb7aa52 217
emilmont 1:fdd22bb7aa52 218 /* Initialize pState pointer */
emilmont 1:fdd22bb7aa52 219 px = pState;
emilmont 1:fdd22bb7aa52 220
emilmont 1:fdd22bb7aa52 221 /* Initialize coeff pointer */
emilmont 1:fdd22bb7aa52 222 pb = (pCoeffs);
emilmont 1:fdd22bb7aa52 223
emilmont 1:fdd22bb7aa52 224 /* Set the accumulator to zero */
emilmont 1:fdd22bb7aa52 225 sum = 0.0f;
emilmont 1:fdd22bb7aa52 226
emilmont 1:fdd22bb7aa52 227 /* Loop unrolling. Process 4 taps at a time. */
emilmont 1:fdd22bb7aa52 228 tapCnt = numTaps >> 2;
emilmont 1:fdd22bb7aa52 229
emilmont 1:fdd22bb7aa52 230 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 231 {
emilmont 1:fdd22bb7aa52 232 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 233 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 234 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 235 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 236 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 237
emilmont 1:fdd22bb7aa52 238 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 239 tapCnt--;
emilmont 1:fdd22bb7aa52 240 }
emilmont 1:fdd22bb7aa52 241
emilmont 1:fdd22bb7aa52 242 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
emilmont 1:fdd22bb7aa52 243 tapCnt = numTaps % 0x4u;
emilmont 1:fdd22bb7aa52 244
emilmont 1:fdd22bb7aa52 245 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 246 {
emilmont 1:fdd22bb7aa52 247 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 248 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 249
emilmont 1:fdd22bb7aa52 250 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 251 tapCnt--;
emilmont 1:fdd22bb7aa52 252 }
emilmont 1:fdd22bb7aa52 253
emilmont 1:fdd22bb7aa52 254 /* The result in the accumulator, store in the destination buffer. */
emilmont 1:fdd22bb7aa52 255 *pOut++ = sum;
emilmont 1:fdd22bb7aa52 256
emilmont 1:fdd22bb7aa52 257 /* Compute and store error */
emilmont 1:fdd22bb7aa52 258 d = (float32_t) (*pRef++);
emilmont 1:fdd22bb7aa52 259 e = d - sum;
emilmont 1:fdd22bb7aa52 260 *pErr++ = e;
emilmont 1:fdd22bb7aa52 261
emilmont 1:fdd22bb7aa52 262 /* Calculation of Weighting factor for the updating filter coefficients */
emilmont 1:fdd22bb7aa52 263 w = e * mu;
emilmont 1:fdd22bb7aa52 264
emilmont 1:fdd22bb7aa52 265 /* Initialize pState pointer */
emilmont 1:fdd22bb7aa52 266 px = pState;
emilmont 1:fdd22bb7aa52 267
emilmont 1:fdd22bb7aa52 268 /* Initialize coeff pointer */
emilmont 1:fdd22bb7aa52 269 pb = (pCoeffs);
emilmont 1:fdd22bb7aa52 270
emilmont 1:fdd22bb7aa52 271 /* Loop unrolling. Process 4 taps at a time. */
emilmont 1:fdd22bb7aa52 272 tapCnt = numTaps >> 2;
emilmont 1:fdd22bb7aa52 273
emilmont 1:fdd22bb7aa52 274 /* Update filter coefficients */
emilmont 1:fdd22bb7aa52 275 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 276 {
emilmont 1:fdd22bb7aa52 277 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 278 *pb = *pb + (w * (*px++));
emilmont 1:fdd22bb7aa52 279 pb++;
emilmont 1:fdd22bb7aa52 280
emilmont 1:fdd22bb7aa52 281 *pb = *pb + (w * (*px++));
emilmont 1:fdd22bb7aa52 282 pb++;
emilmont 1:fdd22bb7aa52 283
emilmont 1:fdd22bb7aa52 284 *pb = *pb + (w * (*px++));
emilmont 1:fdd22bb7aa52 285 pb++;
emilmont 1:fdd22bb7aa52 286
emilmont 1:fdd22bb7aa52 287 *pb = *pb + (w * (*px++));
emilmont 1:fdd22bb7aa52 288 pb++;
emilmont 1:fdd22bb7aa52 289
emilmont 1:fdd22bb7aa52 290 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 291 tapCnt--;
emilmont 1:fdd22bb7aa52 292 }
emilmont 1:fdd22bb7aa52 293
emilmont 1:fdd22bb7aa52 294 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
emilmont 1:fdd22bb7aa52 295 tapCnt = numTaps % 0x4u;
emilmont 1:fdd22bb7aa52 296
emilmont 1:fdd22bb7aa52 297 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 298 {
emilmont 1:fdd22bb7aa52 299 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 300 *pb = *pb + (w * (*px++));
emilmont 1:fdd22bb7aa52 301 pb++;
emilmont 1:fdd22bb7aa52 302
emilmont 1:fdd22bb7aa52 303 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 304 tapCnt--;
emilmont 1:fdd22bb7aa52 305 }
emilmont 1:fdd22bb7aa52 306
emilmont 1:fdd22bb7aa52 307 /* Advance state pointer by 1 for the next sample */
emilmont 1:fdd22bb7aa52 308 pState = pState + 1;
emilmont 1:fdd22bb7aa52 309
emilmont 1:fdd22bb7aa52 310 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 311 blkCnt--;
emilmont 1:fdd22bb7aa52 312 }
emilmont 1:fdd22bb7aa52 313
emilmont 1:fdd22bb7aa52 314
emilmont 1:fdd22bb7aa52 315 /* Processing is complete. Now copy the last numTaps - 1 samples to the
emilmont 1:fdd22bb7aa52 316 satrt of the state buffer. This prepares the state buffer for the
emilmont 1:fdd22bb7aa52 317 next function call. */
emilmont 1:fdd22bb7aa52 318
emilmont 1:fdd22bb7aa52 319 /* Points to the start of the pState buffer */
emilmont 1:fdd22bb7aa52 320 pStateCurnt = S->pState;
emilmont 1:fdd22bb7aa52 321
emilmont 1:fdd22bb7aa52 322 /* Loop unrolling for (numTaps - 1u) samples copy */
emilmont 1:fdd22bb7aa52 323 tapCnt = (numTaps - 1u) >> 2u;
emilmont 1:fdd22bb7aa52 324
emilmont 1:fdd22bb7aa52 325 /* copy data */
emilmont 1:fdd22bb7aa52 326 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 327 {
emilmont 1:fdd22bb7aa52 328 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 329 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 330 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 331 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 332
emilmont 1:fdd22bb7aa52 333 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 334 tapCnt--;
emilmont 1:fdd22bb7aa52 335 }
emilmont 1:fdd22bb7aa52 336
emilmont 1:fdd22bb7aa52 337 /* Calculate remaining number of copies */
emilmont 1:fdd22bb7aa52 338 tapCnt = (numTaps - 1u) % 0x4u;
emilmont 1:fdd22bb7aa52 339
emilmont 1:fdd22bb7aa52 340 /* Copy the remaining q31_t data */
emilmont 1:fdd22bb7aa52 341 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 342 {
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 #else
emilmont 1:fdd22bb7aa52 350
emilmont 1:fdd22bb7aa52 351 /* Run the below code for Cortex-M0 */
emilmont 1:fdd22bb7aa52 352
emilmont 1:fdd22bb7aa52 353 while(blkCnt > 0u)
emilmont 1:fdd22bb7aa52 354 {
emilmont 1:fdd22bb7aa52 355 /* Copy the new input sample into the state buffer */
emilmont 1:fdd22bb7aa52 356 *pStateCurnt++ = *pSrc++;
emilmont 1:fdd22bb7aa52 357
emilmont 1:fdd22bb7aa52 358 /* Initialize pState pointer */
emilmont 1:fdd22bb7aa52 359 px = pState;
emilmont 1:fdd22bb7aa52 360
emilmont 1:fdd22bb7aa52 361 /* Initialize pCoeffs pointer */
emilmont 1:fdd22bb7aa52 362 pb = pCoeffs;
emilmont 1:fdd22bb7aa52 363
emilmont 1:fdd22bb7aa52 364 /* Set the accumulator to zero */
emilmont 1:fdd22bb7aa52 365 sum = 0.0f;
emilmont 1:fdd22bb7aa52 366
emilmont 1:fdd22bb7aa52 367 /* Loop over numTaps number of values */
emilmont 1:fdd22bb7aa52 368 tapCnt = numTaps;
emilmont 1:fdd22bb7aa52 369
emilmont 1:fdd22bb7aa52 370 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 371 {
emilmont 1:fdd22bb7aa52 372 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 373 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 374
emilmont 1:fdd22bb7aa52 375 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 376 tapCnt--;
emilmont 1:fdd22bb7aa52 377 }
emilmont 1:fdd22bb7aa52 378
emilmont 1:fdd22bb7aa52 379 /* The result is stored in the destination buffer. */
emilmont 1:fdd22bb7aa52 380 *pOut++ = sum;
emilmont 1:fdd22bb7aa52 381
emilmont 1:fdd22bb7aa52 382 /* Compute and store error */
emilmont 1:fdd22bb7aa52 383 d = (float32_t) (*pRef++);
emilmont 1:fdd22bb7aa52 384 e = d - sum;
emilmont 1:fdd22bb7aa52 385 *pErr++ = e;
emilmont 1:fdd22bb7aa52 386
emilmont 1:fdd22bb7aa52 387 /* Weighting factor for the LMS version */
emilmont 1:fdd22bb7aa52 388 w = e * mu;
emilmont 1:fdd22bb7aa52 389
emilmont 1:fdd22bb7aa52 390 /* Initialize pState pointer */
emilmont 1:fdd22bb7aa52 391 px = pState;
emilmont 1:fdd22bb7aa52 392
emilmont 1:fdd22bb7aa52 393 /* Initialize pCoeffs pointer */
emilmont 1:fdd22bb7aa52 394 pb = pCoeffs;
emilmont 1:fdd22bb7aa52 395
emilmont 1:fdd22bb7aa52 396 /* Loop over numTaps number of values */
emilmont 1:fdd22bb7aa52 397 tapCnt = numTaps;
emilmont 1:fdd22bb7aa52 398
emilmont 1:fdd22bb7aa52 399 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 400 {
emilmont 1:fdd22bb7aa52 401 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 402 *pb = *pb + (w * (*px++));
emilmont 1:fdd22bb7aa52 403 pb++;
emilmont 1:fdd22bb7aa52 404
emilmont 1:fdd22bb7aa52 405 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 406 tapCnt--;
emilmont 1:fdd22bb7aa52 407 }
emilmont 1:fdd22bb7aa52 408
emilmont 1:fdd22bb7aa52 409 /* Advance state pointer by 1 for the next sample */
emilmont 1:fdd22bb7aa52 410 pState = pState + 1;
emilmont 1:fdd22bb7aa52 411
emilmont 1:fdd22bb7aa52 412 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 413 blkCnt--;
emilmont 1:fdd22bb7aa52 414 }
emilmont 1:fdd22bb7aa52 415
emilmont 1:fdd22bb7aa52 416
emilmont 1:fdd22bb7aa52 417 /* Processing is complete. Now copy the last numTaps - 1 samples to the
emilmont 1:fdd22bb7aa52 418 * start of the state buffer. This prepares the state buffer for the
emilmont 1:fdd22bb7aa52 419 * next function call. */
emilmont 1:fdd22bb7aa52 420
emilmont 1:fdd22bb7aa52 421 /* Points to the start of the pState buffer */
emilmont 1:fdd22bb7aa52 422 pStateCurnt = S->pState;
emilmont 1:fdd22bb7aa52 423
emilmont 1:fdd22bb7aa52 424 /* Copy (numTaps - 1u) samples */
emilmont 1:fdd22bb7aa52 425 tapCnt = (numTaps - 1u);
emilmont 1:fdd22bb7aa52 426
emilmont 1:fdd22bb7aa52 427 /* Copy the data */
emilmont 1:fdd22bb7aa52 428 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 429 {
emilmont 1:fdd22bb7aa52 430 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 431
emilmont 1:fdd22bb7aa52 432 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 433 tapCnt--;
emilmont 1:fdd22bb7aa52 434 }
emilmont 1:fdd22bb7aa52 435
mbed_official 3:7a284390b0ce 436 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
emilmont 1:fdd22bb7aa52 437
emilmont 1:fdd22bb7aa52 438 }
emilmont 1:fdd22bb7aa52 439
emilmont 1:fdd22bb7aa52 440 /**
emilmont 1:fdd22bb7aa52 441 * @} end of LMS group
emilmont 1:fdd22bb7aa52 442 */