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:
emilmont
Date:
Wed Nov 28 12:30:09 2012 +0000
Revision:
1:fdd22bb7aa52
Child:
2:da51fb522205
DSP library code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:fdd22bb7aa52 1 /* ----------------------------------------------------------------------
emilmont 1:fdd22bb7aa52 2 * Copyright (C) 2010 ARM Limited. All rights reserved.
emilmont 1:fdd22bb7aa52 3 *
emilmont 1:fdd22bb7aa52 4 * $Date: 15. February 2012
emilmont 1:fdd22bb7aa52 5 * $Revision: V1.1.0
emilmont 1:fdd22bb7aa52 6 *
emilmont 1:fdd22bb7aa52 7 * Project: CMSIS DSP Library
emilmont 1:fdd22bb7aa52 8 * Title: arm_lms_norm_f32.c
emilmont 1:fdd22bb7aa52 9 *
emilmont 1:fdd22bb7aa52 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 *
emilmont 1:fdd22bb7aa52 14 * Version 1.1.0 2012/02/15
emilmont 1:fdd22bb7aa52 15 * Updated with more optimizations, bug fixes and minor API changes.
emilmont 1:fdd22bb7aa52 16 *
emilmont 1:fdd22bb7aa52 17 * Version 1.0.10 2011/7/15
emilmont 1:fdd22bb7aa52 18 * Big Endian support added and Merged M0 and M3/M4 Source code.
emilmont 1:fdd22bb7aa52 19 *
emilmont 1:fdd22bb7aa52 20 * Version 1.0.3 2010/11/29
emilmont 1:fdd22bb7aa52 21 * Re-organized the CMSIS folders and updated documentation.
emilmont 1:fdd22bb7aa52 22 *
emilmont 1:fdd22bb7aa52 23 * Version 1.0.2 2010/11/11
emilmont 1:fdd22bb7aa52 24 * Documentation updated.
emilmont 1:fdd22bb7aa52 25 *
emilmont 1:fdd22bb7aa52 26 * Version 1.0.1 2010/10/05
emilmont 1:fdd22bb7aa52 27 * Production release and review comments incorporated.
emilmont 1:fdd22bb7aa52 28 *
emilmont 1:fdd22bb7aa52 29 * Version 1.0.0 2010/09/20
emilmont 1:fdd22bb7aa52 30 * Production release and review comments incorporated
emilmont 1:fdd22bb7aa52 31 *
emilmont 1:fdd22bb7aa52 32 * Version 0.0.7 2010/06/10
emilmont 1:fdd22bb7aa52 33 * Misra-C changes done
emilmont 1:fdd22bb7aa52 34 * -------------------------------------------------------------------- */
emilmont 1:fdd22bb7aa52 35
emilmont 1:fdd22bb7aa52 36 #include "arm_math.h"
emilmont 1:fdd22bb7aa52 37
emilmont 1:fdd22bb7aa52 38 /**
emilmont 1:fdd22bb7aa52 39 * @ingroup groupFilters
emilmont 1:fdd22bb7aa52 40 */
emilmont 1:fdd22bb7aa52 41
emilmont 1:fdd22bb7aa52 42 /**
emilmont 1:fdd22bb7aa52 43 * @defgroup LMS_NORM Normalized LMS Filters
emilmont 1:fdd22bb7aa52 44 *
emilmont 1:fdd22bb7aa52 45 * This set of functions implements a commonly used adaptive filter.
emilmont 1:fdd22bb7aa52 46 * It is related to the Least Mean Square (LMS) adaptive filter and includes an additional normalization
emilmont 1:fdd22bb7aa52 47 * factor which increases the adaptation rate of the filter.
emilmont 1:fdd22bb7aa52 48 * The CMSIS DSP Library contains normalized LMS filter functions that operate on Q15, Q31, and floating-point data types.
emilmont 1:fdd22bb7aa52 49 *
emilmont 1:fdd22bb7aa52 50 * A normalized least mean square (NLMS) filter consists of two components as shown below.
emilmont 1:fdd22bb7aa52 51 * The first component is a standard transversal or FIR filter.
emilmont 1:fdd22bb7aa52 52 * The second component is a coefficient update mechanism.
emilmont 1:fdd22bb7aa52 53 * The NLMS filter has two input signals.
emilmont 1:fdd22bb7aa52 54 * The "input" feeds the FIR filter while the "reference input" corresponds to the desired output of the FIR filter.
emilmont 1:fdd22bb7aa52 55 * That is, the FIR filter coefficients are updated so that the output of the FIR filter matches the reference input.
emilmont 1:fdd22bb7aa52 56 * The filter coefficient update mechanism is based on the difference between the FIR filter output and the reference input.
emilmont 1:fdd22bb7aa52 57 * This "error signal" tends towards zero as the filter adapts.
emilmont 1:fdd22bb7aa52 58 * The NLMS processing functions accept the input and reference input signals and generate the filter output and error signal.
emilmont 1:fdd22bb7aa52 59 * \image html LMS.gif "Internal structure of the NLMS adaptive filter"
emilmont 1:fdd22bb7aa52 60 *
emilmont 1:fdd22bb7aa52 61 * The functions operate on blocks of data and each call to the function processes
emilmont 1:fdd22bb7aa52 62 * <code>blockSize</code> samples through the filter.
emilmont 1:fdd22bb7aa52 63 * <code>pSrc</code> points to input signal, <code>pRef</code> points to reference signal,
emilmont 1:fdd22bb7aa52 64 * <code>pOut</code> points to output signal and <code>pErr</code> points to error signal.
emilmont 1:fdd22bb7aa52 65 * All arrays contain <code>blockSize</code> values.
emilmont 1:fdd22bb7aa52 66 *
emilmont 1:fdd22bb7aa52 67 * The functions operate on a block-by-block basis.
emilmont 1:fdd22bb7aa52 68 * Internally, the filter coefficients <code>b[n]</code> are updated on a sample-by-sample basis.
emilmont 1:fdd22bb7aa52 69 * The convergence of the LMS filter is slower compared to the normalized LMS algorithm.
emilmont 1:fdd22bb7aa52 70 *
emilmont 1:fdd22bb7aa52 71 * \par Algorithm:
emilmont 1:fdd22bb7aa52 72 * The output signal <code>y[n]</code> is computed by a standard FIR filter:
emilmont 1:fdd22bb7aa52 73 * <pre>
emilmont 1:fdd22bb7aa52 74 * 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 75 * </pre>
emilmont 1:fdd22bb7aa52 76 *
emilmont 1:fdd22bb7aa52 77 * \par
emilmont 1:fdd22bb7aa52 78 * The error signal equals the difference between the reference signal <code>d[n]</code> and the filter output:
emilmont 1:fdd22bb7aa52 79 * <pre>
emilmont 1:fdd22bb7aa52 80 * e[n] = d[n] - y[n].
emilmont 1:fdd22bb7aa52 81 * </pre>
emilmont 1:fdd22bb7aa52 82 *
emilmont 1:fdd22bb7aa52 83 * \par
emilmont 1:fdd22bb7aa52 84 * After each sample of the error signal is computed the instanteous energy of the filter state variables is calculated:
emilmont 1:fdd22bb7aa52 85 * <pre>
emilmont 1:fdd22bb7aa52 86 * E = x[n]^2 + x[n-1]^2 + ... + x[n-numTaps+1]^2.
emilmont 1:fdd22bb7aa52 87 * </pre>
emilmont 1:fdd22bb7aa52 88 * The filter coefficients <code>b[k]</code> are then updated on a sample-by-sample basis:
emilmont 1:fdd22bb7aa52 89 * <pre>
emilmont 1:fdd22bb7aa52 90 * b[k] = b[k] + e[n] * (mu/E) * x[n-k], for k=0, 1, ..., numTaps-1
emilmont 1:fdd22bb7aa52 91 * </pre>
emilmont 1:fdd22bb7aa52 92 * where <code>mu</code> is the step size and controls the rate of coefficient convergence.
emilmont 1:fdd22bb7aa52 93 *\par
emilmont 1:fdd22bb7aa52 94 * In the APIs, <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>.
emilmont 1:fdd22bb7aa52 95 * Coefficients are stored in time reversed order.
emilmont 1:fdd22bb7aa52 96 * \par
emilmont 1:fdd22bb7aa52 97 * <pre>
emilmont 1:fdd22bb7aa52 98 * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
emilmont 1:fdd22bb7aa52 99 * </pre>
emilmont 1:fdd22bb7aa52 100 * \par
emilmont 1:fdd22bb7aa52 101 * <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>.
emilmont 1:fdd22bb7aa52 102 * Samples in the state buffer are stored in the order:
emilmont 1:fdd22bb7aa52 103 * \par
emilmont 1:fdd22bb7aa52 104 * <pre>
emilmont 1:fdd22bb7aa52 105 * {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 106 * </pre>
emilmont 1:fdd22bb7aa52 107 * \par
emilmont 1:fdd22bb7aa52 108 * Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code> samples.
emilmont 1:fdd22bb7aa52 109 * The increased state buffer length allows circular addressing, which is traditionally used in FIR filters,
emilmont 1:fdd22bb7aa52 110 * to be avoided and yields a significant speed improvement.
emilmont 1:fdd22bb7aa52 111 * The state variables are updated after each block of data is processed.
emilmont 1:fdd22bb7aa52 112 * \par Instance Structure
emilmont 1:fdd22bb7aa52 113 * The coefficients and state variables for a filter are stored together in an instance data structure.
emilmont 1:fdd22bb7aa52 114 * A separate instance structure must be defined for each filter and
emilmont 1:fdd22bb7aa52 115 * coefficient and state arrays cannot be shared among instances.
emilmont 1:fdd22bb7aa52 116 * There are separate instance structure declarations for each of the 3 supported data types.
emilmont 1:fdd22bb7aa52 117 *
emilmont 1:fdd22bb7aa52 118 * \par Initialization Functions
emilmont 1:fdd22bb7aa52 119 * There is also an associated initialization function for each data type.
emilmont 1:fdd22bb7aa52 120 * The initialization function performs the following operations:
emilmont 1:fdd22bb7aa52 121 * - Sets the values of the internal structure fields.
emilmont 1:fdd22bb7aa52 122 * - Zeros out the values in the state buffer.
emilmont 1:fdd22bb7aa52 123 * \par
emilmont 1:fdd22bb7aa52 124 * Instance structure cannot be placed into a const data section and it is recommended to use the initialization function.
emilmont 1:fdd22bb7aa52 125 * \par Fixed-Point Behavior:
emilmont 1:fdd22bb7aa52 126 * Care must be taken when using the Q15 and Q31 versions of the normalised LMS filter.
emilmont 1:fdd22bb7aa52 127 * The following issues must be considered:
emilmont 1:fdd22bb7aa52 128 * - Scaling of coefficients
emilmont 1:fdd22bb7aa52 129 * - Overflow and saturation
emilmont 1:fdd22bb7aa52 130 *
emilmont 1:fdd22bb7aa52 131 * \par Scaling of Coefficients:
emilmont 1:fdd22bb7aa52 132 * Filter coefficients are represented as fractional values and
emilmont 1:fdd22bb7aa52 133 * coefficients are restricted to lie in the range <code>[-1 +1)</code>.
emilmont 1:fdd22bb7aa52 134 * The fixed-point functions have an additional scaling parameter <code>postShift</code>.
emilmont 1:fdd22bb7aa52 135 * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits.
emilmont 1:fdd22bb7aa52 136 * This essentially scales the filter coefficients by <code>2^postShift</code> and
emilmont 1:fdd22bb7aa52 137 * allows the filter coefficients to exceed the range <code>[+1 -1)</code>.
emilmont 1:fdd22bb7aa52 138 * The value of <code>postShift</code> is set by the user based on the expected gain through the system being modeled.
emilmont 1:fdd22bb7aa52 139 *
emilmont 1:fdd22bb7aa52 140 * \par Overflow and Saturation:
emilmont 1:fdd22bb7aa52 141 * Overflow and saturation behavior of the fixed-point Q15 and Q31 versions are
emilmont 1:fdd22bb7aa52 142 * described separately as part of the function specific documentation below.
emilmont 1:fdd22bb7aa52 143 */
emilmont 1:fdd22bb7aa52 144
emilmont 1:fdd22bb7aa52 145
emilmont 1:fdd22bb7aa52 146 /**
emilmont 1:fdd22bb7aa52 147 * @addtogroup LMS_NORM
emilmont 1:fdd22bb7aa52 148 * @{
emilmont 1:fdd22bb7aa52 149 */
emilmont 1:fdd22bb7aa52 150
emilmont 1:fdd22bb7aa52 151
emilmont 1:fdd22bb7aa52 152 /**
emilmont 1:fdd22bb7aa52 153 * @brief Processing function for floating-point normalized LMS filter.
emilmont 1:fdd22bb7aa52 154 * @param[in] *S points to an instance of the floating-point normalized LMS filter structure.
emilmont 1:fdd22bb7aa52 155 * @param[in] *pSrc points to the block of input data.
emilmont 1:fdd22bb7aa52 156 * @param[in] *pRef points to the block of reference data.
emilmont 1:fdd22bb7aa52 157 * @param[out] *pOut points to the block of output data.
emilmont 1:fdd22bb7aa52 158 * @param[out] *pErr points to the block of error data.
emilmont 1:fdd22bb7aa52 159 * @param[in] blockSize number of samples to process.
emilmont 1:fdd22bb7aa52 160 * @return none.
emilmont 1:fdd22bb7aa52 161 */
emilmont 1:fdd22bb7aa52 162
emilmont 1:fdd22bb7aa52 163 void arm_lms_norm_f32(
emilmont 1:fdd22bb7aa52 164 arm_lms_norm_instance_f32 * S,
emilmont 1:fdd22bb7aa52 165 float32_t * pSrc,
emilmont 1:fdd22bb7aa52 166 float32_t * pRef,
emilmont 1:fdd22bb7aa52 167 float32_t * pOut,
emilmont 1:fdd22bb7aa52 168 float32_t * pErr,
emilmont 1:fdd22bb7aa52 169 uint32_t blockSize)
emilmont 1:fdd22bb7aa52 170 {
emilmont 1:fdd22bb7aa52 171 float32_t *pState = S->pState; /* State pointer */
emilmont 1:fdd22bb7aa52 172 float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
emilmont 1:fdd22bb7aa52 173 float32_t *pStateCurnt; /* Points to the current sample of the state */
emilmont 1:fdd22bb7aa52 174 float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */
emilmont 1:fdd22bb7aa52 175 float32_t mu = S->mu; /* Adaptive factor */
emilmont 1:fdd22bb7aa52 176 uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
emilmont 1:fdd22bb7aa52 177 uint32_t tapCnt, blkCnt; /* Loop counters */
emilmont 1:fdd22bb7aa52 178 float32_t energy; /* Energy of the input */
emilmont 1:fdd22bb7aa52 179 float32_t sum, e, d; /* accumulator, error, reference data sample */
emilmont 1:fdd22bb7aa52 180 float32_t w, x0, in; /* weight factor, temporary variable to hold input sample and state */
emilmont 1:fdd22bb7aa52 181
emilmont 1:fdd22bb7aa52 182 /* Initializations of error, difference, Coefficient update */
emilmont 1:fdd22bb7aa52 183 e = 0.0f;
emilmont 1:fdd22bb7aa52 184 d = 0.0f;
emilmont 1:fdd22bb7aa52 185 w = 0.0f;
emilmont 1:fdd22bb7aa52 186
emilmont 1:fdd22bb7aa52 187 energy = S->energy;
emilmont 1:fdd22bb7aa52 188 x0 = S->x0;
emilmont 1:fdd22bb7aa52 189
emilmont 1:fdd22bb7aa52 190 /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
emilmont 1:fdd22bb7aa52 191 /* pStateCurnt points to the location where the new input data should be written */
emilmont 1:fdd22bb7aa52 192 pStateCurnt = &(S->pState[(numTaps - 1u)]);
emilmont 1:fdd22bb7aa52 193
emilmont 1:fdd22bb7aa52 194 /* Loop over blockSize number of values */
emilmont 1:fdd22bb7aa52 195 blkCnt = blockSize;
emilmont 1:fdd22bb7aa52 196
emilmont 1:fdd22bb7aa52 197
emilmont 1:fdd22bb7aa52 198 #ifndef ARM_MATH_CM0
emilmont 1:fdd22bb7aa52 199
emilmont 1:fdd22bb7aa52 200 /* Run the below code for Cortex-M4 and Cortex-M3 */
emilmont 1:fdd22bb7aa52 201
emilmont 1:fdd22bb7aa52 202 while(blkCnt > 0u)
emilmont 1:fdd22bb7aa52 203 {
emilmont 1:fdd22bb7aa52 204 /* Copy the new input sample into the state buffer */
emilmont 1:fdd22bb7aa52 205 *pStateCurnt++ = *pSrc;
emilmont 1:fdd22bb7aa52 206
emilmont 1:fdd22bb7aa52 207 /* Initialize pState pointer */
emilmont 1:fdd22bb7aa52 208 px = pState;
emilmont 1:fdd22bb7aa52 209
emilmont 1:fdd22bb7aa52 210 /* Initialize coeff pointer */
emilmont 1:fdd22bb7aa52 211 pb = (pCoeffs);
emilmont 1:fdd22bb7aa52 212
emilmont 1:fdd22bb7aa52 213 /* Read the sample from input buffer */
emilmont 1:fdd22bb7aa52 214 in = *pSrc++;
emilmont 1:fdd22bb7aa52 215
emilmont 1:fdd22bb7aa52 216 /* Update the energy calculation */
emilmont 1:fdd22bb7aa52 217 energy -= x0 * x0;
emilmont 1:fdd22bb7aa52 218 energy += in * in;
emilmont 1:fdd22bb7aa52 219
emilmont 1:fdd22bb7aa52 220 /* Set the accumulator to zero */
emilmont 1:fdd22bb7aa52 221 sum = 0.0f;
emilmont 1:fdd22bb7aa52 222
emilmont 1:fdd22bb7aa52 223 /* Loop unrolling. Process 4 taps at a time. */
emilmont 1:fdd22bb7aa52 224 tapCnt = numTaps >> 2;
emilmont 1:fdd22bb7aa52 225
emilmont 1:fdd22bb7aa52 226 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 227 {
emilmont 1:fdd22bb7aa52 228 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 229 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 230 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 231 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 232 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 233
emilmont 1:fdd22bb7aa52 234 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 235 tapCnt--;
emilmont 1:fdd22bb7aa52 236 }
emilmont 1:fdd22bb7aa52 237
emilmont 1:fdd22bb7aa52 238 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
emilmont 1:fdd22bb7aa52 239 tapCnt = numTaps % 0x4u;
emilmont 1:fdd22bb7aa52 240
emilmont 1:fdd22bb7aa52 241 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 242 {
emilmont 1:fdd22bb7aa52 243 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 244 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 245
emilmont 1:fdd22bb7aa52 246 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 247 tapCnt--;
emilmont 1:fdd22bb7aa52 248 }
emilmont 1:fdd22bb7aa52 249
emilmont 1:fdd22bb7aa52 250 /* The result in the accumulator, store in the destination buffer. */
emilmont 1:fdd22bb7aa52 251 *pOut++ = sum;
emilmont 1:fdd22bb7aa52 252
emilmont 1:fdd22bb7aa52 253 /* Compute and store error */
emilmont 1:fdd22bb7aa52 254 d = (float32_t) (*pRef++);
emilmont 1:fdd22bb7aa52 255 e = d - sum;
emilmont 1:fdd22bb7aa52 256 *pErr++ = e;
emilmont 1:fdd22bb7aa52 257
emilmont 1:fdd22bb7aa52 258 /* Calculation of Weighting factor for updating filter coefficients */
emilmont 1:fdd22bb7aa52 259 /* epsilon value 0.000000119209289f */
emilmont 1:fdd22bb7aa52 260 w = (e * mu) / (energy + 0.000000119209289f);
emilmont 1:fdd22bb7aa52 261
emilmont 1:fdd22bb7aa52 262 /* Initialize pState pointer */
emilmont 1:fdd22bb7aa52 263 px = pState;
emilmont 1:fdd22bb7aa52 264
emilmont 1:fdd22bb7aa52 265 /* Initialize coeff pointer */
emilmont 1:fdd22bb7aa52 266 pb = (pCoeffs);
emilmont 1:fdd22bb7aa52 267
emilmont 1:fdd22bb7aa52 268 /* Loop unrolling. Process 4 taps at a time. */
emilmont 1:fdd22bb7aa52 269 tapCnt = numTaps >> 2;
emilmont 1:fdd22bb7aa52 270
emilmont 1:fdd22bb7aa52 271 /* Update filter coefficients */
emilmont 1:fdd22bb7aa52 272 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 273 {
emilmont 1:fdd22bb7aa52 274 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 275 *pb += w * (*px++);
emilmont 1:fdd22bb7aa52 276 pb++;
emilmont 1:fdd22bb7aa52 277
emilmont 1:fdd22bb7aa52 278 *pb += w * (*px++);
emilmont 1:fdd22bb7aa52 279 pb++;
emilmont 1:fdd22bb7aa52 280
emilmont 1:fdd22bb7aa52 281 *pb += w * (*px++);
emilmont 1:fdd22bb7aa52 282 pb++;
emilmont 1:fdd22bb7aa52 283
emilmont 1:fdd22bb7aa52 284 *pb += w * (*px++);
emilmont 1:fdd22bb7aa52 285 pb++;
emilmont 1:fdd22bb7aa52 286
emilmont 1:fdd22bb7aa52 287
emilmont 1:fdd22bb7aa52 288 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 289 tapCnt--;
emilmont 1:fdd22bb7aa52 290 }
emilmont 1:fdd22bb7aa52 291
emilmont 1:fdd22bb7aa52 292 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
emilmont 1:fdd22bb7aa52 293 tapCnt = numTaps % 0x4u;
emilmont 1:fdd22bb7aa52 294
emilmont 1:fdd22bb7aa52 295 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 296 {
emilmont 1:fdd22bb7aa52 297 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 298 *pb += w * (*px++);
emilmont 1:fdd22bb7aa52 299 pb++;
emilmont 1:fdd22bb7aa52 300
emilmont 1:fdd22bb7aa52 301 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 302 tapCnt--;
emilmont 1:fdd22bb7aa52 303 }
emilmont 1:fdd22bb7aa52 304
emilmont 1:fdd22bb7aa52 305 x0 = *pState;
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 S->energy = energy;
emilmont 1:fdd22bb7aa52 315 S->x0 = x0;
emilmont 1:fdd22bb7aa52 316
emilmont 1:fdd22bb7aa52 317 /* Processing is complete. Now copy the last numTaps - 1 samples to the
emilmont 1:fdd22bb7aa52 318 satrt of the state buffer. This prepares the state buffer for the
emilmont 1:fdd22bb7aa52 319 next function call. */
emilmont 1:fdd22bb7aa52 320
emilmont 1:fdd22bb7aa52 321 /* Points to the start of the pState buffer */
emilmont 1:fdd22bb7aa52 322 pStateCurnt = S->pState;
emilmont 1:fdd22bb7aa52 323
emilmont 1:fdd22bb7aa52 324 /* Loop unrolling for (numTaps - 1u)/4 samples copy */
emilmont 1:fdd22bb7aa52 325 tapCnt = (numTaps - 1u) >> 2u;
emilmont 1:fdd22bb7aa52 326
emilmont 1:fdd22bb7aa52 327 /* copy data */
emilmont 1:fdd22bb7aa52 328 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 329 {
emilmont 1:fdd22bb7aa52 330 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 331 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 332 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 333 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 334
emilmont 1:fdd22bb7aa52 335 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 336 tapCnt--;
emilmont 1:fdd22bb7aa52 337 }
emilmont 1:fdd22bb7aa52 338
emilmont 1:fdd22bb7aa52 339 /* Calculate remaining number of copies */
emilmont 1:fdd22bb7aa52 340 tapCnt = (numTaps - 1u) % 0x4u;
emilmont 1:fdd22bb7aa52 341
emilmont 1:fdd22bb7aa52 342 /* Copy the remaining q31_t data */
emilmont 1:fdd22bb7aa52 343 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 344 {
emilmont 1:fdd22bb7aa52 345 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 346
emilmont 1:fdd22bb7aa52 347 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 348 tapCnt--;
emilmont 1:fdd22bb7aa52 349 }
emilmont 1:fdd22bb7aa52 350
emilmont 1:fdd22bb7aa52 351 #else
emilmont 1:fdd22bb7aa52 352
emilmont 1:fdd22bb7aa52 353 /* Run the below code for Cortex-M0 */
emilmont 1:fdd22bb7aa52 354
emilmont 1:fdd22bb7aa52 355 while(blkCnt > 0u)
emilmont 1:fdd22bb7aa52 356 {
emilmont 1:fdd22bb7aa52 357 /* Copy the new input sample into the state buffer */
emilmont 1:fdd22bb7aa52 358 *pStateCurnt++ = *pSrc;
emilmont 1:fdd22bb7aa52 359
emilmont 1:fdd22bb7aa52 360 /* Initialize pState pointer */
emilmont 1:fdd22bb7aa52 361 px = pState;
emilmont 1:fdd22bb7aa52 362
emilmont 1:fdd22bb7aa52 363 /* Initialize pCoeffs pointer */
emilmont 1:fdd22bb7aa52 364 pb = pCoeffs;
emilmont 1:fdd22bb7aa52 365
emilmont 1:fdd22bb7aa52 366 /* Read the sample from input buffer */
emilmont 1:fdd22bb7aa52 367 in = *pSrc++;
emilmont 1:fdd22bb7aa52 368
emilmont 1:fdd22bb7aa52 369 /* Update the energy calculation */
emilmont 1:fdd22bb7aa52 370 energy -= x0 * x0;
emilmont 1:fdd22bb7aa52 371 energy += in * in;
emilmont 1:fdd22bb7aa52 372
emilmont 1:fdd22bb7aa52 373 /* Set the accumulator to zero */
emilmont 1:fdd22bb7aa52 374 sum = 0.0f;
emilmont 1:fdd22bb7aa52 375
emilmont 1:fdd22bb7aa52 376 /* Loop over numTaps number of values */
emilmont 1:fdd22bb7aa52 377 tapCnt = numTaps;
emilmont 1:fdd22bb7aa52 378
emilmont 1:fdd22bb7aa52 379 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 380 {
emilmont 1:fdd22bb7aa52 381 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 382 sum += (*px++) * (*pb++);
emilmont 1:fdd22bb7aa52 383
emilmont 1:fdd22bb7aa52 384 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 385 tapCnt--;
emilmont 1:fdd22bb7aa52 386 }
emilmont 1:fdd22bb7aa52 387
emilmont 1:fdd22bb7aa52 388 /* The result in the accumulator is stored in the destination buffer. */
emilmont 1:fdd22bb7aa52 389 *pOut++ = sum;
emilmont 1:fdd22bb7aa52 390
emilmont 1:fdd22bb7aa52 391 /* Compute and store error */
emilmont 1:fdd22bb7aa52 392 d = (float32_t) (*pRef++);
emilmont 1:fdd22bb7aa52 393 e = d - sum;
emilmont 1:fdd22bb7aa52 394 *pErr++ = e;
emilmont 1:fdd22bb7aa52 395
emilmont 1:fdd22bb7aa52 396 /* Calculation of Weighting factor for updating filter coefficients */
emilmont 1:fdd22bb7aa52 397 /* epsilon value 0.000000119209289f */
emilmont 1:fdd22bb7aa52 398 w = (e * mu) / (energy + 0.000000119209289f);
emilmont 1:fdd22bb7aa52 399
emilmont 1:fdd22bb7aa52 400 /* Initialize pState pointer */
emilmont 1:fdd22bb7aa52 401 px = pState;
emilmont 1:fdd22bb7aa52 402
emilmont 1:fdd22bb7aa52 403 /* Initialize pCcoeffs pointer */
emilmont 1:fdd22bb7aa52 404 pb = pCoeffs;
emilmont 1:fdd22bb7aa52 405
emilmont 1:fdd22bb7aa52 406 /* Loop over numTaps number of values */
emilmont 1:fdd22bb7aa52 407 tapCnt = numTaps;
emilmont 1:fdd22bb7aa52 408
emilmont 1:fdd22bb7aa52 409 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 410 {
emilmont 1:fdd22bb7aa52 411 /* Perform the multiply-accumulate */
emilmont 1:fdd22bb7aa52 412 *pb += w * (*px++);
emilmont 1:fdd22bb7aa52 413 pb++;
emilmont 1:fdd22bb7aa52 414
emilmont 1:fdd22bb7aa52 415 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 416 tapCnt--;
emilmont 1:fdd22bb7aa52 417 }
emilmont 1:fdd22bb7aa52 418
emilmont 1:fdd22bb7aa52 419 x0 = *pState;
emilmont 1:fdd22bb7aa52 420
emilmont 1:fdd22bb7aa52 421 /* Advance state pointer by 1 for the next sample */
emilmont 1:fdd22bb7aa52 422 pState = pState + 1;
emilmont 1:fdd22bb7aa52 423
emilmont 1:fdd22bb7aa52 424 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 425 blkCnt--;
emilmont 1:fdd22bb7aa52 426 }
emilmont 1:fdd22bb7aa52 427
emilmont 1:fdd22bb7aa52 428 S->energy = energy;
emilmont 1:fdd22bb7aa52 429 S->x0 = x0;
emilmont 1:fdd22bb7aa52 430
emilmont 1:fdd22bb7aa52 431 /* Processing is complete. Now copy the last numTaps - 1 samples to the
emilmont 1:fdd22bb7aa52 432 satrt of the state buffer. This prepares the state buffer for the
emilmont 1:fdd22bb7aa52 433 next function call. */
emilmont 1:fdd22bb7aa52 434
emilmont 1:fdd22bb7aa52 435 /* Points to the start of the pState buffer */
emilmont 1:fdd22bb7aa52 436 pStateCurnt = S->pState;
emilmont 1:fdd22bb7aa52 437
emilmont 1:fdd22bb7aa52 438 /* Copy (numTaps - 1u) samples */
emilmont 1:fdd22bb7aa52 439 tapCnt = (numTaps - 1u);
emilmont 1:fdd22bb7aa52 440
emilmont 1:fdd22bb7aa52 441 /* Copy the remaining q31_t data */
emilmont 1:fdd22bb7aa52 442 while(tapCnt > 0u)
emilmont 1:fdd22bb7aa52 443 {
emilmont 1:fdd22bb7aa52 444 *pStateCurnt++ = *pState++;
emilmont 1:fdd22bb7aa52 445
emilmont 1:fdd22bb7aa52 446 /* Decrement the loop counter */
emilmont 1:fdd22bb7aa52 447 tapCnt--;
emilmont 1:fdd22bb7aa52 448 }
emilmont 1:fdd22bb7aa52 449
emilmont 1:fdd22bb7aa52 450 #endif /* #ifndef ARM_MATH_CM0 */
emilmont 1:fdd22bb7aa52 451
emilmont 1:fdd22bb7aa52 452 }
emilmont 1:fdd22bb7aa52 453
emilmont 1:fdd22bb7aa52 454 /**
emilmont 1:fdd22bb7aa52 455 * @} end of LMS_NORM group
emilmont 1:fdd22bb7aa52 456 */