1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
10 * Description: Processing function for the Q15 LMS filter.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated
29 * Version 0.0.7 2010/06/10
30 * Misra-C changes done
31 * -------------------------------------------------------------------- */
35 * @ingroup groupFilters
44 * @brief Processing function for Q15 LMS filter.
45 * @param[in] *S points to an instance of the Q15 LMS filter structure.
46 * @param[in] *pSrc points to the block of input data.
47 * @param[in] *pRef points to the block of reference data.
48 * @param[out] *pOut points to the block of output data.
49 * @param[out] *pErr points to the block of error data.
50 * @param[in] blockSize number of samples to process.
53 * \par Scaling and Overflow Behavior:
54 * The function is implemented using a 64-bit internal accumulator.
55 * Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result.
56 * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
57 * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
58 * After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits.
59 * Lastly, the accumulator is saturated to yield a result in 1.15 format.
62 * In this filter, filter coefficients are updated for each sample and the updation of filter cofficients are saturted.
67 const arm_lms_instance_q15 * S,
74 q15_t *pState = S->pState; /* State pointer */
75 uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
76 q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
77 q15_t *pStateCurnt; /* Points to the current sample of the state */
78 q15_t mu = S->mu; /* Adaptive factor */
79 q15_t *px; /* Temporary pointer for state */
80 q15_t *pb; /* Temporary pointer for coefficient buffer */
81 uint32_t tapCnt, blkCnt; /* Loop counters */
82 q63_t acc; /* Accumulator */
83 q15_t e = 0; /* error of data sample */
84 q15_t alpha; /* Intermediate constant for taps update */
85 uint32_t shift = S->postShift + 1u; /* Shift to be applied to the output */
90 /* Run the below code for Cortex-M4 and Cortex-M3 */
92 q31_t coef; /* Teporary variable for coefficient */
94 /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
95 /* pStateCurnt points to the location where the new input data should be written */
96 pStateCurnt = &(S->pState[(numTaps - 1u)]);
98 /* Initializing blkCnt with blockSize */
103 /* Copy the new input sample into the state buffer */
104 *pStateCurnt++ = *pSrc++;
106 /* Initialize state pointer */
109 /* Initialize coefficient pointer */
112 /* Set the accumulator to zero */
115 /* Loop unrolling. Process 4 taps at a time. */
116 tapCnt = numTaps >> 2u;
120 /* acc += b[N] * x[n-N] + b[N-1] * x[n-N-1] */
121 /* Perform the multiply-accumulate */
122 acc = __SMLALD(*__SIMD32(px)++, (*__SIMD32(pb)++), acc);
123 acc = __SMLALD(*__SIMD32(px)++, (*__SIMD32(pb)++), acc);
125 /* Decrement the loop counter */
129 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
130 tapCnt = numTaps % 0x4u;
134 /* Perform the multiply-accumulate */
135 acc += (q63_t) (((q31_t) (*px++) * (*pb++)));
137 /* Decrement the loop counter */
141 /* Converting the result to 1.15 format and saturate the output */
142 acc = __SSAT((acc >> (16 - shift)), 16);
144 /* Store the result from accumulator into the destination buffer. */
145 *pOut++ = (q15_t) acc;
147 /* Compute and store error */
148 e = *pRef++ - (q15_t) acc;
152 /* Compute alpha i.e. intermediate constant for taps update */
153 alpha = (q15_t) (((q31_t) e * (mu)) >> 15);
155 /* Initialize state pointer */
156 /* Advance state pointer by 1 for the next sample */
159 /* Initialize coefficient pointer */
162 /* Loop unrolling. Process 4 taps at a time. */
163 tapCnt = numTaps >> 2u;
165 /* Update filter coefficients */
168 coef = (q31_t) * pb + (((q31_t) alpha * (*px++)) >> 15);
169 *pb++ = (q15_t) __SSAT((coef), 16);
170 coef = (q31_t) * pb + (((q31_t) alpha * (*px++)) >> 15);
171 *pb++ = (q15_t) __SSAT((coef), 16);
172 coef = (q31_t) * pb + (((q31_t) alpha * (*px++)) >> 15);
173 *pb++ = (q15_t) __SSAT((coef), 16);
174 coef = (q31_t) * pb + (((q31_t) alpha * (*px++)) >> 15);
175 *pb++ = (q15_t) __SSAT((coef), 16);
177 /* Decrement the loop counter */
181 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
182 tapCnt = numTaps % 0x4u;
186 /* Perform the multiply-accumulate */
187 coef = (q31_t) * pb + (((q31_t) alpha * (*px++)) >> 15);
188 *pb++ = (q15_t) __SSAT((coef), 16);
190 /* Decrement the loop counter */
194 /* Decrement the loop counter */
199 /* Processing is complete. Now copy the last numTaps - 1 samples to the
200 satrt of the state buffer. This prepares the state buffer for the
201 next function call. */
203 /* Points to the start of the pState buffer */
204 pStateCurnt = S->pState;
206 /* Calculation of count for copying integer writes */
207 tapCnt = (numTaps - 1u) >> 2;
212 *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
213 *__SIMD32(pStateCurnt)++ = *__SIMD32(pState)++;
219 /* Calculation of count for remaining q15_t data */
220 tapCnt = (numTaps - 1u) % 0x4u;
225 *pStateCurnt++ = *pState++;
227 /* Decrement the loop counter */
233 /* Run the below code for Cortex-M0 */
235 /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */
236 /* pStateCurnt points to the location where the new input data should be written */
237 pStateCurnt = &(S->pState[(numTaps - 1u)]);
239 /* Loop over blockSize number of values */
244 /* Copy the new input sample into the state buffer */
245 *pStateCurnt++ = *pSrc++;
247 /* Initialize pState pointer */
250 /* Initialize pCoeffs pointer */
253 /* Set the accumulator to zero */
256 /* Loop over numTaps number of values */
261 /* Perform the multiply-accumulate */
262 acc += (q63_t) ((q31_t) (*px++) * (*pb++));
264 /* Decrement the loop counter */
268 /* Converting the result to 1.15 format and saturate the output */
269 acc = __SSAT((acc >> (16 - shift)), 16);
271 /* Store the result from accumulator into the destination buffer. */
272 *pOut++ = (q15_t) acc;
274 /* Compute and store error */
275 e = *pRef++ - (q15_t) acc;
279 /* Compute alpha i.e. intermediate constant for taps update */
280 alpha = (q15_t) (((q31_t) e * (mu)) >> 15);
282 /* Initialize pState pointer */
283 /* Advance state pointer by 1 for the next sample */
286 /* Initialize pCoeffs pointer */
289 /* Loop over numTaps number of values */
294 /* Perform the multiply-accumulate */
295 *pb++ += (q15_t) (((q31_t) alpha * (*px++)) >> 15);
297 /* Decrement the loop counter */
301 /* Decrement the loop counter */
306 /* Processing is complete. Now copy the last numTaps - 1 samples to the
307 start of the state buffer. This prepares the state buffer for the
308 next function call. */
310 /* Points to the start of the pState buffer */
311 pStateCurnt = S->pState;
313 /* Copy (numTaps - 1u) samples */
314 tapCnt = (numTaps - 1u);
319 *pStateCurnt++ = *pState++;
321 /* Decrement the loop counter */
325 #endif /* #ifndef ARM_MATH_CM0 */
330 * @} end of LMS group