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_q15.c
10 * Description: Q15 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 * ------------------------------------------------------------------- */
43 * @addtogroup FIR_Sparse
48 * @brief Processing function for the Q15 sparse FIR filter.
49 * @param[in] *S points to an instance of the Q15 sparse FIR structure.
50 * @param[in] *pSrc points to the block of input data.
51 * @param[out] *pDst points to the block of output data
52 * @param[in] *pScratchIn points to a temporary buffer of size blockSize.
53 * @param[in] *pScratchOut 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.15 x 1.15 multiplications yield a 2.30 result and these are added to a 2.30 accumulator.
61 * Thus the full precision of the multiplications is maintained but there is only a single guard bit in the accumulator.
62 * If the accumulator result overflows it will wrap around rather than saturate.
63 * After all multiply-accumulates are performed, the 2.30 accumulator is truncated to 2.15 format and then saturated to 1.15 format.
64 * In order to avoid overflows the input signal or coefficients must be scaled down by log2(numTaps) bits.
68 void arm_fir_sparse_q15(
69 arm_fir_sparse_instance_q15
* S
,
77 q15_t
*pState
= S
->pState
; /* State pointer */
78 q15_t
*pIn
= pSrc
; /* Working pointer for input */
79 q15_t
*pOut
= pDst
; /* Working pointer for output */
80 q15_t
*pCoeffs
= S
->pCoeffs
; /* Coefficient pointer */
81 q15_t
*px
; /* Temporary pointers for scratch buffer */
82 q15_t
*pb
= pScratchIn
; /* Temporary pointers for scratch buffer */
83 q15_t
*py
= pState
; /* Temporary pointers for state buffer */
84 int32_t *pTapDelay
= S
->pTapDelay
; /* Pointer to the array containing offset of the non-zero tap values. */
85 uint32_t delaySize
= S
->maxDelay
+ blockSize
; /* state length */
86 uint16_t numTaps
= S
->numTaps
; /* Filter order */
87 int32_t readIndex
; /* Read index of the state buffer */
88 uint32_t tapCnt
, blkCnt
; /* loop counters */
89 q15_t coeff
= *pCoeffs
++; /* Read the first coefficient value */
90 q31_t
*pScr2
= pScratchOut
; /* Working pointer for pScratchOut */
93 #ifndef ARM_MATH_CM0_FAMILY
95 /* Run the below code for Cortex-M4 and Cortex-M3 */
97 q31_t in1
, in2
; /* Temporary variables */
100 /* BlockSize of Input samples are copied into the state buffer */
101 /* StateIndex points to the starting position to write in the state buffer */
102 arm_circularWrite_q15(py
, delaySize
, &S
->stateIndex
, 1, pIn
, 1, blockSize
);
104 /* Loop over the number of taps. */
107 /* Read Index, from where the state buffer should be read, is calculated. */
108 readIndex
= (S
->stateIndex
- blockSize
) - *pTapDelay
++;
110 /* Wraparound of readIndex */
113 readIndex
+= (int32_t) delaySize
;
116 /* Working pointer for state buffer is updated */
119 /* blockSize samples are read from the state buffer */
120 arm_circularRead_q15(py
, delaySize
, &readIndex
, 1,
121 pb
, pb
, blockSize
, 1, blockSize
);
123 /* Working pointer for the scratch buffer of state values */
126 /* Working pointer for scratch buffer of output values */
129 /* Loop over the blockSize. Unroll by a factor of 4.
130 * Compute 4 multiplications at a time. */
131 blkCnt
= blockSize
>> 2;
135 /* Perform multiplication and store in the scratch buffer */
136 *pScratchOut
++ = ((q31_t
) * px
++ * coeff
);
137 *pScratchOut
++ = ((q31_t
) * px
++ * coeff
);
138 *pScratchOut
++ = ((q31_t
) * px
++ * coeff
);
139 *pScratchOut
++ = ((q31_t
) * px
++ * coeff
);
141 /* Decrement the loop counter */
145 /* If the blockSize is not a multiple of 4,
146 * compute the remaining samples */
147 blkCnt
= blockSize
% 0x4u
;
151 /* Perform multiplication and store in the scratch buffer */
152 *pScratchOut
++ = ((q31_t
) * px
++ * coeff
);
154 /* Decrement the loop counter */
158 /* Load the coefficient value and
159 * increment the coefficient buffer for the next set of state values */
162 /* Read Index, from where the state buffer should be read, is calculated. */
163 readIndex
= (S
->stateIndex
- blockSize
) - *pTapDelay
++;
165 /* Wraparound of readIndex */
168 readIndex
+= (int32_t) delaySize
;
171 /* Loop over the number of taps. */
172 tapCnt
= (uint32_t) numTaps
- 1u;
176 /* Working pointer for state buffer is updated */
179 /* blockSize samples are read from the state buffer */
180 arm_circularRead_q15(py
, delaySize
, &readIndex
, 1,
181 pb
, pb
, blockSize
, 1, blockSize
);
183 /* Working pointer for the scratch buffer of state values */
186 /* Working pointer for scratch buffer of output values */
189 /* Loop over the blockSize. Unroll by a factor of 4.
190 * Compute 4 MACS at a time. */
191 blkCnt
= blockSize
>> 2;
195 /* Perform Multiply-Accumulate */
196 *pScratchOut
++ += (q31_t
) * px
++ * coeff
;
197 *pScratchOut
++ += (q31_t
) * px
++ * coeff
;
198 *pScratchOut
++ += (q31_t
) * px
++ * coeff
;
199 *pScratchOut
++ += (q31_t
) * px
++ * coeff
;
201 /* Decrement the loop counter */
205 /* If the blockSize is not a multiple of 4,
206 * compute the remaining samples */
207 blkCnt
= blockSize
% 0x4u
;
211 /* Perform Multiply-Accumulate */
212 *pScratchOut
++ += (q31_t
) * px
++ * coeff
;
214 /* Decrement the loop counter */
218 /* Load the coefficient value and
219 * increment the coefficient buffer for the next set of state values */
222 /* Read Index, from where the state buffer should be read, is calculated. */
223 readIndex
= (S
->stateIndex
- blockSize
) - *pTapDelay
++;
225 /* Wraparound of readIndex */
228 readIndex
+= (int32_t) delaySize
;
231 /* Decrement the tap loop counter */
235 /* All the output values are in pScratchOut buffer.
236 Convert them into 1.15 format, saturate and store in the destination buffer. */
237 /* Loop over the blockSize. */
238 blkCnt
= blockSize
>> 2;
245 #ifndef ARM_MATH_BIG_ENDIAN
248 __PKHBT((q15_t
) __SSAT(in1
>> 15, 16), (q15_t
) __SSAT(in2
>> 15, 16),
253 __PKHBT((q15_t
) __SSAT(in2
>> 15, 16), (q15_t
) __SSAT(in1
>> 15, 16),
256 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
262 #ifndef ARM_MATH_BIG_ENDIAN
265 __PKHBT((q15_t
) __SSAT(in1
>> 15, 16), (q15_t
) __SSAT(in2
>> 15, 16),
271 __PKHBT((q15_t
) __SSAT(in2
>> 15, 16), (q15_t
) __SSAT(in1
>> 15, 16),
274 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
281 /* If the blockSize is not a multiple of 4,
282 remaining samples are processed in the below loop */
283 blkCnt
= blockSize
% 0x4u
;
287 *pOut
++ = (q15_t
) __SSAT(*pScr2
++ >> 15, 16);
293 /* Run the below code for Cortex-M0 */
295 /* BlockSize of Input samples are copied into the state buffer */
296 /* StateIndex points to the starting position to write in the state buffer */
297 arm_circularWrite_q15(py
, delaySize
, &S
->stateIndex
, 1, pIn
, 1, blockSize
);
299 /* Loop over the number of taps. */
302 /* Read Index, from where the state buffer should be read, is calculated. */
303 readIndex
= (S
->stateIndex
- blockSize
) - *pTapDelay
++;
305 /* Wraparound of readIndex */
308 readIndex
+= (int32_t) delaySize
;
311 /* Working pointer for state buffer is updated */
314 /* blockSize samples are read from the state buffer */
315 arm_circularRead_q15(py
, delaySize
, &readIndex
, 1,
316 pb
, pb
, blockSize
, 1, blockSize
);
318 /* Working pointer for the scratch buffer of state values */
321 /* Working pointer for scratch buffer of output values */
328 /* Perform multiplication and store in the scratch buffer */
329 *pScratchOut
++ = ((q31_t
) * px
++ * coeff
);
331 /* Decrement the loop counter */
335 /* Load the coefficient value and
336 * increment the coefficient buffer for the next set of state values */
339 /* Read Index, from where the state buffer should be read, is calculated. */
340 readIndex
= (S
->stateIndex
- blockSize
) - *pTapDelay
++;
342 /* Wraparound of readIndex */
345 readIndex
+= (int32_t) delaySize
;
348 /* Loop over the number of taps. */
349 tapCnt
= (uint32_t) numTaps
- 1u;
353 /* Working pointer for state buffer is updated */
356 /* blockSize samples are read from the state buffer */
357 arm_circularRead_q15(py
, delaySize
, &readIndex
, 1,
358 pb
, pb
, blockSize
, 1, blockSize
);
360 /* Working pointer for the scratch buffer of state values */
363 /* Working pointer for scratch buffer of output values */
370 /* Perform Multiply-Accumulate */
371 *pScratchOut
++ += (q31_t
) * px
++ * coeff
;
373 /* Decrement the loop counter */
377 /* Load the coefficient value and
378 * increment the coefficient buffer for the next set of state values */
381 /* Read Index, from where the state buffer should be read, is calculated. */
382 readIndex
= (S
->stateIndex
- blockSize
) - *pTapDelay
++;
384 /* Wraparound of readIndex */
387 readIndex
+= (int32_t) delaySize
;
390 /* Decrement the tap loop counter */
394 /* All the output values are in pScratchOut buffer.
395 Convert them into 1.15 format, saturate and store in the destination buffer. */
396 /* Loop over the blockSize. */
401 *pOut
++ = (q15_t
) __SSAT(*pScr2
++ >> 15, 16);
405 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
410 * @} end of FIR_Sparse group