1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
8 * Title: arm_fir_sparse_q31.c
10 * Description: Q31 sparse FIR filter processing function.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
23 * - Neither the name of ARM LIMITED nor the names of its contributors
24 * may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 * ------------------------------------------------------------------- */
44 * @addtogroup FIR_Sparse
49 * @brief Processing function for the Q31 sparse FIR filter.
50 * @param[in] *S points to an instance of the Q31 sparse FIR structure.
51 * @param[in] *pSrc points to the block of input data.
52 * @param[out] *pDst points to the block of output data
53 * @param[in] *pScratchIn points to a temporary buffer of size blockSize.
54 * @param[in] blockSize number of input samples to process per call.
57 * <b>Scaling and Overflow Behavior:</b>
59 * The function is implemented using an internal 32-bit accumulator.
60 * The 1.31 x 1.31 multiplications are truncated to 2.30 format.
61 * This leads to loss of precision on the intermediate multiplications and provides only a single guard bit.
62 * If the accumulator result overflows, it wraps around rather than saturate.
63 * In order to avoid overflows the input signal or coefficients must be scaled down by log2(numTaps) bits.
66 void arm_fir_sparse_q31(
67 arm_fir_sparse_instance_q31
* S
,
74 q31_t
*pState
= S
->pState
; /* State pointer */
75 q31_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
76 q31_t
*px
; /* Scratch buffer pointer */
77 q31_t
*py
= pState
; /* Temporary pointers for state buffer */
78 q31_t
*pb
= pScratchIn
; /* Temporary pointers for scratch buffer */
79 q31_t
*pOut
; /* Destination pointer */
80 q63_t out
; /* Temporary output variable */
81 int32_t *pTapDelay
= S
->pTapDelay
; /* Pointer to the array containing offset of the non-zero tap values. */
82 uint32_t delaySize
= S
->maxDelay
+ blockSize
; /* state length */
83 uint16_t numTaps
= S
->numTaps
; /* Filter order */
84 int32_t readIndex
; /* Read index of the state buffer */
85 uint32_t tapCnt
, blkCnt
; /* loop counters */
86 q31_t coeff
= *pCoeffs
++; /* Read the first coefficient value */
90 /* BlockSize of Input samples are copied into the state buffer */
91 /* StateIndex points to the starting position to write in the state buffer */
92 arm_circularWrite_f32((int32_t *) py
, delaySize
, &S
->stateIndex
, 1,
93 (int32_t *) pSrc
, 1, blockSize
);
95 /* Read Index, from where the state buffer should be read, is calculated. */
96 readIndex
= (int32_t) (S
->stateIndex
- blockSize
) - *pTapDelay
++;
98 /* Wraparound of readIndex */
101 readIndex
+= (int32_t) delaySize
;
104 /* Working pointer for state buffer is updated */
107 /* blockSize samples are read from the state buffer */
108 arm_circularRead_f32((int32_t *) py
, delaySize
, &readIndex
, 1,
109 (int32_t *) pb
, (int32_t *) pb
, blockSize
, 1,
112 /* Working pointer for the scratch buffer of state values */
115 /* Working pointer for scratch buffer of output values */
119 #ifndef ARM_MATH_CM0_FAMILY
121 /* Run the below code for Cortex-M4 and Cortex-M3 */
123 /* Loop over the blockSize. Unroll by a factor of 4.
124 * Compute 4 Multiplications at a time. */
125 blkCnt
= blockSize
>> 2;
129 /* Perform Multiplications and store in the destination buffer */
130 *pOut
++ = (q31_t
) (((q63_t
) * px
++ * coeff
) >> 32);
131 *pOut
++ = (q31_t
) (((q63_t
) * px
++ * coeff
) >> 32);
132 *pOut
++ = (q31_t
) (((q63_t
) * px
++ * coeff
) >> 32);
133 *pOut
++ = (q31_t
) (((q63_t
) * px
++ * coeff
) >> 32);
135 /* Decrement the loop counter */
139 /* If the blockSize is not a multiple of 4,
140 * compute the remaining samples */
141 blkCnt
= blockSize
% 0x4u
;
145 /* Perform Multiplications and store in the destination buffer */
146 *pOut
++ = (q31_t
) (((q63_t
) * px
++ * coeff
) >> 32);
148 /* Decrement the loop counter */
152 /* Load the coefficient value and
153 * increment the coefficient buffer for the next set of state values */
156 /* Read Index, from where the state buffer should be read, is calculated. */
157 readIndex
= (int32_t) (S
->stateIndex
- blockSize
) - *pTapDelay
++;
159 /* Wraparound of readIndex */
162 readIndex
+= (int32_t) delaySize
;
165 /* Loop over the number of taps. */
166 tapCnt
= (uint32_t) numTaps
- 1u;
170 /* Working pointer for state buffer is updated */
173 /* blockSize samples are read from the state buffer */
174 arm_circularRead_f32((int32_t *) py
, delaySize
, &readIndex
, 1,
175 (int32_t *) pb
, (int32_t *) pb
, blockSize
, 1,
178 /* Working pointer for the scratch buffer of state values */
181 /* Working pointer for scratch buffer of output values */
184 /* Loop over the blockSize. Unroll by a factor of 4.
185 * Compute 4 MACS at a time. */
186 blkCnt
= blockSize
>> 2;
191 out
+= ((q63_t
) * px
++ * coeff
) >> 32;
192 *pOut
++ = (q31_t
) (out
);
195 out
+= ((q63_t
) * px
++ * coeff
) >> 32;
196 *pOut
++ = (q31_t
) (out
);
199 out
+= ((q63_t
) * px
++ * coeff
) >> 32;
200 *pOut
++ = (q31_t
) (out
);
203 out
+= ((q63_t
) * px
++ * coeff
) >> 32;
204 *pOut
++ = (q31_t
) (out
);
206 /* Decrement the loop counter */
210 /* If the blockSize is not a multiple of 4,
211 * compute the remaining samples */
212 blkCnt
= blockSize
% 0x4u
;
216 /* Perform Multiply-Accumulate */
218 out
+= ((q63_t
) * px
++ * coeff
) >> 32;
219 *pOut
++ = (q31_t
) (out
);
221 /* Decrement the loop counter */
225 /* Load the coefficient value and
226 * increment the coefficient buffer for the next set of state values */
229 /* Read Index, from where the state buffer should be read, is calculated. */
230 readIndex
= (int32_t) (S
->stateIndex
- blockSize
) - *pTapDelay
++;
232 /* Wraparound of readIndex */
235 readIndex
+= (int32_t) delaySize
;
238 /* Decrement the tap loop counter */
242 /* Working output pointer is updated */
245 /* Output is converted into 1.31 format. */
246 /* Loop over the blockSize. Unroll by a factor of 4.
247 * process 4 output samples at a time. */
248 blkCnt
= blockSize
>> 2;
261 /* Decrement the loop counter */
265 /* If the blockSize is not a multiple of 4,
266 * process the remaining output samples */
267 blkCnt
= blockSize
% 0x4u
;
274 /* Decrement the loop counter */
280 /* Run the below code for Cortex-M0 */
285 /* Perform Multiplications and store in the destination buffer */
286 *pOut
++ = (q31_t
) (((q63_t
) * px
++ * coeff
) >> 32);
288 /* Decrement the loop counter */
292 /* Load the coefficient value and
293 * increment the coefficient buffer for the next set of state values */
296 /* Read Index, from where the state buffer should be read, is calculated. */
297 readIndex
= (int32_t) (S
->stateIndex
- blockSize
) - *pTapDelay
++;
299 /* Wraparound of readIndex */
302 readIndex
+= (int32_t) delaySize
;
305 /* Loop over the number of taps. */
306 tapCnt
= (uint32_t) numTaps
- 1u;
310 /* Working pointer for state buffer is updated */
313 /* blockSize samples are read from the state buffer */
314 arm_circularRead_f32((int32_t *) py
, delaySize
, &readIndex
, 1,
315 (int32_t *) pb
, (int32_t *) pb
, blockSize
, 1,
318 /* Working pointer for the scratch buffer of state values */
321 /* Working pointer for scratch buffer of output values */
328 /* Perform Multiply-Accumulate */
330 out
+= ((q63_t
) * px
++ * coeff
) >> 32;
331 *pOut
++ = (q31_t
) (out
);
333 /* Decrement the loop counter */
337 /* Load the coefficient value and
338 * increment the coefficient buffer for the next set of state values */
341 /* Read Index, from where the state buffer should be read, is calculated. */
342 readIndex
= (int32_t) (S
->stateIndex
- blockSize
) - *pTapDelay
++;
344 /* Wraparound of readIndex */
347 readIndex
+= (int32_t) delaySize
;
350 /* Decrement the tap loop counter */
354 /* Working output pointer is updated */
357 /* Output is converted into 1.31 format. */
365 /* Decrement the loop counter */
369 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
374 * @} end of FIR_Sparse group