Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os by
arm_lms_norm_f32.c
00001 /* ---------------------------------------------------------------------- 00002 * Copyright (C) 2010-2014 ARM Limited. All rights reserved. 00003 * 00004 * $Date: 19. March 2015 00005 * $Revision: V.1.4.5 00006 * 00007 * Project: CMSIS DSP Library 00008 * Title: arm_lms_norm_f32.c 00009 * 00010 * Description: Processing function for the floating-point Normalised LMS. 00011 * 00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions 00016 * are met: 00017 * - Redistributions of source code must retain the above copyright 00018 * notice, this list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright 00020 * notice, this list of conditions and the following disclaimer in 00021 * the documentation and/or other materials provided with the 00022 * distribution. 00023 * - Neither the name of ARM LIMITED nor the names of its contributors 00024 * may be used to endorse or promote products derived from this 00025 * software without specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00028 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00029 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00030 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00031 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00033 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00034 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00036 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00037 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00038 * POSSIBILITY OF SUCH DAMAGE. 00039 * -------------------------------------------------------------------- */ 00040 00041 #include "arm_math.h" 00042 00043 /** 00044 * @ingroup groupFilters 00045 */ 00046 00047 /** 00048 * @defgroup LMS_NORM Normalized LMS Filters 00049 * 00050 * This set of functions implements a commonly used adaptive filter. 00051 * It is related to the Least Mean Square (LMS) adaptive filter and includes an additional normalization 00052 * factor which increases the adaptation rate of the filter. 00053 * The CMSIS DSP Library contains normalized LMS filter functions that operate on Q15, Q31, and floating-point data types. 00054 * 00055 * A normalized least mean square (NLMS) filter consists of two components as shown below. 00056 * The first component is a standard transversal or FIR filter. 00057 * The second component is a coefficient update mechanism. 00058 * The NLMS filter has two input signals. 00059 * The "input" feeds the FIR filter while the "reference input" corresponds to the desired output of the FIR filter. 00060 * That is, the FIR filter coefficients are updated so that the output of the FIR filter matches the reference input. 00061 * The filter coefficient update mechanism is based on the difference between the FIR filter output and the reference input. 00062 * This "error signal" tends towards zero as the filter adapts. 00063 * The NLMS processing functions accept the input and reference input signals and generate the filter output and error signal. 00064 * \image html LMS.gif "Internal structure of the NLMS adaptive filter" 00065 * 00066 * The functions operate on blocks of data and each call to the function processes 00067 * <code>blockSize</code> samples through the filter. 00068 * <code>pSrc</code> points to input signal, <code>pRef</code> points to reference signal, 00069 * <code>pOut</code> points to output signal and <code>pErr</code> points to error signal. 00070 * All arrays contain <code>blockSize</code> values. 00071 * 00072 * The functions operate on a block-by-block basis. 00073 * Internally, the filter coefficients <code>b[n]</code> are updated on a sample-by-sample basis. 00074 * The convergence of the LMS filter is slower compared to the normalized LMS algorithm. 00075 * 00076 * \par Algorithm: 00077 * The output signal <code>y[n]</code> is computed by a standard FIR filter: 00078 * <pre> 00079 * y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1] 00080 * </pre> 00081 * 00082 * \par 00083 * The error signal equals the difference between the reference signal <code>d[n]</code> and the filter output: 00084 * <pre> 00085 * e[n] = d[n] - y[n]. 00086 * </pre> 00087 * 00088 * \par 00089 * After each sample of the error signal is computed the instanteous energy of the filter state variables is calculated: 00090 * <pre> 00091 * E = x[n]^2 + x[n-1]^2 + ... + x[n-numTaps+1]^2. 00092 * </pre> 00093 * The filter coefficients <code>b[k]</code> are then updated on a sample-by-sample basis: 00094 * <pre> 00095 * b[k] = b[k] + e[n] * (mu/E) * x[n-k], for k=0, 1, ..., numTaps-1 00096 * </pre> 00097 * where <code>mu</code> is the step size and controls the rate of coefficient convergence. 00098 *\par 00099 * In the APIs, <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>. 00100 * Coefficients are stored in time reversed order. 00101 * \par 00102 * <pre> 00103 * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} 00104 * </pre> 00105 * \par 00106 * <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>. 00107 * Samples in the state buffer are stored in the order: 00108 * \par 00109 * <pre> 00110 * {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]} 00111 * </pre> 00112 * \par 00113 * Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code> samples. 00114 * The increased state buffer length allows circular addressing, which is traditionally used in FIR filters, 00115 * to be avoided and yields a significant speed improvement. 00116 * The state variables are updated after each block of data is processed. 00117 * \par Instance Structure 00118 * The coefficients and state variables for a filter are stored together in an instance data structure. 00119 * A separate instance structure must be defined for each filter and 00120 * coefficient and state arrays cannot be shared among instances. 00121 * There are separate instance structure declarations for each of the 3 supported data types. 00122 * 00123 * \par Initialization Functions 00124 * There is also an associated initialization function for each data type. 00125 * The initialization function performs the following operations: 00126 * - Sets the values of the internal structure fields. 00127 * - Zeros out the values in the state buffer. 00128 * To do this manually without calling the init function, assign the follow subfields of the instance structure: 00129 * numTaps, pCoeffs, mu, energy, x0, pState. Also set all of the values in pState to zero. 00130 * For Q7, Q15, and Q31 the following fields must also be initialized; 00131 * recipTable, postShift 00132 * 00133 * \par 00134 * Instance structure cannot be placed into a const data section and it is recommended to use the initialization function. 00135 * \par Fixed-Point Behavior: 00136 * Care must be taken when using the Q15 and Q31 versions of the normalised LMS filter. 00137 * The following issues must be considered: 00138 * - Scaling of coefficients 00139 * - Overflow and saturation 00140 * 00141 * \par Scaling of Coefficients: 00142 * Filter coefficients are represented as fractional values and 00143 * coefficients are restricted to lie in the range <code>[-1 +1)</code>. 00144 * The fixed-point functions have an additional scaling parameter <code>postShift</code>. 00145 * At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits. 00146 * This essentially scales the filter coefficients by <code>2^postShift</code> and 00147 * allows the filter coefficients to exceed the range <code>[+1 -1)</code>. 00148 * The value of <code>postShift</code> is set by the user based on the expected gain through the system being modeled. 00149 * 00150 * \par Overflow and Saturation: 00151 * Overflow and saturation behavior of the fixed-point Q15 and Q31 versions are 00152 * described separately as part of the function specific documentation below. 00153 */ 00154 00155 00156 /** 00157 * @addtogroup LMS_NORM 00158 * @{ 00159 */ 00160 00161 00162 /** 00163 * @brief Processing function for floating-point normalized LMS filter. 00164 * @param[in] *S points to an instance of the floating-point normalized LMS filter structure. 00165 * @param[in] *pSrc points to the block of input data. 00166 * @param[in] *pRef points to the block of reference data. 00167 * @param[out] *pOut points to the block of output data. 00168 * @param[out] *pErr points to the block of error data. 00169 * @param[in] blockSize number of samples to process. 00170 * @return none. 00171 */ 00172 00173 void arm_lms_norm_f32( 00174 arm_lms_norm_instance_f32 * S, 00175 float32_t * pSrc, 00176 float32_t * pRef, 00177 float32_t * pOut, 00178 float32_t * pErr, 00179 uint32_t blockSize) 00180 { 00181 float32_t *pState = S->pState; /* State pointer */ 00182 float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ 00183 float32_t *pStateCurnt; /* Points to the current sample of the state */ 00184 float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ 00185 float32_t mu = S->mu; /* Adaptive factor */ 00186 uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ 00187 uint32_t tapCnt, blkCnt; /* Loop counters */ 00188 float32_t energy; /* Energy of the input */ 00189 float32_t sum, e, d; /* accumulator, error, reference data sample */ 00190 float32_t w, x0, in; /* weight factor, temporary variable to hold input sample and state */ 00191 00192 /* Initializations of error, difference, Coefficient update */ 00193 e = 0.0f; 00194 d = 0.0f; 00195 w = 0.0f; 00196 00197 energy = S->energy; 00198 x0 = S->x0; 00199 00200 /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */ 00201 /* pStateCurnt points to the location where the new input data should be written */ 00202 pStateCurnt = &(S->pState[(numTaps - 1u)]); 00203 00204 /* Loop over blockSize number of values */ 00205 blkCnt = blockSize; 00206 00207 00208 #ifndef ARM_MATH_CM0_FAMILY 00209 00210 /* Run the below code for Cortex-M4 and Cortex-M3 */ 00211 00212 while(blkCnt > 0u) 00213 { 00214 /* Copy the new input sample into the state buffer */ 00215 *pStateCurnt++ = *pSrc; 00216 00217 /* Initialize pState pointer */ 00218 px = pState; 00219 00220 /* Initialize coeff pointer */ 00221 pb = (pCoeffs); 00222 00223 /* Read the sample from input buffer */ 00224 in = *pSrc++; 00225 00226 /* Update the energy calculation */ 00227 energy -= x0 * x0; 00228 energy += in * in; 00229 00230 /* Set the accumulator to zero */ 00231 sum = 0.0f; 00232 00233 /* Loop unrolling. Process 4 taps at a time. */ 00234 tapCnt = numTaps >> 2; 00235 00236 while(tapCnt > 0u) 00237 { 00238 /* Perform the multiply-accumulate */ 00239 sum += (*px++) * (*pb++); 00240 sum += (*px++) * (*pb++); 00241 sum += (*px++) * (*pb++); 00242 sum += (*px++) * (*pb++); 00243 00244 /* Decrement the loop counter */ 00245 tapCnt--; 00246 } 00247 00248 /* If the filter length is not a multiple of 4, compute the remaining filter taps */ 00249 tapCnt = numTaps % 0x4u; 00250 00251 while(tapCnt > 0u) 00252 { 00253 /* Perform the multiply-accumulate */ 00254 sum += (*px++) * (*pb++); 00255 00256 /* Decrement the loop counter */ 00257 tapCnt--; 00258 } 00259 00260 /* The result in the accumulator, store in the destination buffer. */ 00261 *pOut++ = sum; 00262 00263 /* Compute and store error */ 00264 d = (float32_t) (*pRef++); 00265 e = d - sum; 00266 *pErr++ = e; 00267 00268 /* Calculation of Weighting factor for updating filter coefficients */ 00269 /* epsilon value 0.000000119209289f */ 00270 w = (e * mu) / (energy + 0.000000119209289f); 00271 00272 /* Initialize pState pointer */ 00273 px = pState; 00274 00275 /* Initialize coeff pointer */ 00276 pb = (pCoeffs); 00277 00278 /* Loop unrolling. Process 4 taps at a time. */ 00279 tapCnt = numTaps >> 2; 00280 00281 /* Update filter coefficients */ 00282 while(tapCnt > 0u) 00283 { 00284 /* Perform the multiply-accumulate */ 00285 *pb += w * (*px++); 00286 pb++; 00287 00288 *pb += w * (*px++); 00289 pb++; 00290 00291 *pb += w * (*px++); 00292 pb++; 00293 00294 *pb += w * (*px++); 00295 pb++; 00296 00297 00298 /* Decrement the loop counter */ 00299 tapCnt--; 00300 } 00301 00302 /* If the filter length is not a multiple of 4, compute the remaining filter taps */ 00303 tapCnt = numTaps % 0x4u; 00304 00305 while(tapCnt > 0u) 00306 { 00307 /* Perform the multiply-accumulate */ 00308 *pb += w * (*px++); 00309 pb++; 00310 00311 /* Decrement the loop counter */ 00312 tapCnt--; 00313 } 00314 00315 x0 = *pState; 00316 00317 /* Advance state pointer by 1 for the next sample */ 00318 pState = pState + 1; 00319 00320 /* Decrement the loop counter */ 00321 blkCnt--; 00322 } 00323 00324 S->energy = energy; 00325 S->x0 = x0; 00326 00327 /* Processing is complete. Now copy the last numTaps - 1 samples to the 00328 satrt of the state buffer. This prepares the state buffer for the 00329 next function call. */ 00330 00331 /* Points to the start of the pState buffer */ 00332 pStateCurnt = S->pState; 00333 00334 /* Loop unrolling for (numTaps - 1u)/4 samples copy */ 00335 tapCnt = (numTaps - 1u) >> 2u; 00336 00337 /* copy data */ 00338 while(tapCnt > 0u) 00339 { 00340 *pStateCurnt++ = *pState++; 00341 *pStateCurnt++ = *pState++; 00342 *pStateCurnt++ = *pState++; 00343 *pStateCurnt++ = *pState++; 00344 00345 /* Decrement the loop counter */ 00346 tapCnt--; 00347 } 00348 00349 /* Calculate remaining number of copies */ 00350 tapCnt = (numTaps - 1u) % 0x4u; 00351 00352 /* Copy the remaining q31_t data */ 00353 while(tapCnt > 0u) 00354 { 00355 *pStateCurnt++ = *pState++; 00356 00357 /* Decrement the loop counter */ 00358 tapCnt--; 00359 } 00360 00361 #else 00362 00363 /* Run the below code for Cortex-M0 */ 00364 00365 while(blkCnt > 0u) 00366 { 00367 /* Copy the new input sample into the state buffer */ 00368 *pStateCurnt++ = *pSrc; 00369 00370 /* Initialize pState pointer */ 00371 px = pState; 00372 00373 /* Initialize pCoeffs pointer */ 00374 pb = pCoeffs; 00375 00376 /* Read the sample from input buffer */ 00377 in = *pSrc++; 00378 00379 /* Update the energy calculation */ 00380 energy -= x0 * x0; 00381 energy += in * in; 00382 00383 /* Set the accumulator to zero */ 00384 sum = 0.0f; 00385 00386 /* Loop over numTaps number of values */ 00387 tapCnt = numTaps; 00388 00389 while(tapCnt > 0u) 00390 { 00391 /* Perform the multiply-accumulate */ 00392 sum += (*px++) * (*pb++); 00393 00394 /* Decrement the loop counter */ 00395 tapCnt--; 00396 } 00397 00398 /* The result in the accumulator is stored in the destination buffer. */ 00399 *pOut++ = sum; 00400 00401 /* Compute and store error */ 00402 d = (float32_t) (*pRef++); 00403 e = d - sum; 00404 *pErr++ = e; 00405 00406 /* Calculation of Weighting factor for updating filter coefficients */ 00407 /* epsilon value 0.000000119209289f */ 00408 w = (e * mu) / (energy + 0.000000119209289f); 00409 00410 /* Initialize pState pointer */ 00411 px = pState; 00412 00413 /* Initialize pCcoeffs pointer */ 00414 pb = pCoeffs; 00415 00416 /* Loop over numTaps number of values */ 00417 tapCnt = numTaps; 00418 00419 while(tapCnt > 0u) 00420 { 00421 /* Perform the multiply-accumulate */ 00422 *pb += w * (*px++); 00423 pb++; 00424 00425 /* Decrement the loop counter */ 00426 tapCnt--; 00427 } 00428 00429 x0 = *pState; 00430 00431 /* Advance state pointer by 1 for the next sample */ 00432 pState = pState + 1; 00433 00434 /* Decrement the loop counter */ 00435 blkCnt--; 00436 } 00437 00438 S->energy = energy; 00439 S->x0 = x0; 00440 00441 /* Processing is complete. Now copy the last numTaps - 1 samples to the 00442 satrt of the state buffer. This prepares the state buffer for the 00443 next function call. */ 00444 00445 /* Points to the start of the pState buffer */ 00446 pStateCurnt = S->pState; 00447 00448 /* Copy (numTaps - 1u) samples */ 00449 tapCnt = (numTaps - 1u); 00450 00451 /* Copy the remaining q31_t data */ 00452 while(tapCnt > 0u) 00453 { 00454 *pStateCurnt++ = *pState++; 00455 00456 /* Decrement the loop counter */ 00457 tapCnt--; 00458 } 00459 00460 #endif /* #ifndef ARM_MATH_CM0_FAMILY */ 00461 00462 } 00463 00464 /** 00465 * @} end of LMS_NORM group 00466 */
Generated on Tue Jul 12 2022 13:15:25 by
