code
stringlengths
1
1.05M
repo_name
stringlengths
6
83
path
stringlengths
3
242
language
stringclasses
222 values
license
stringclasses
20 values
size
int64
1
1.05M
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_decimate_init_q15.c * Description: Initialization function for the Q15 FIR Decimator * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_decimate @{ */ /** @brief Initialization function for the Q15 FIR decimator. @param[in,out] S points to an instance of the Q15 FIR decimator structure @param[in] numTaps number of coefficients in the filter @param[in] M decimation factor @param[in] pCoeffs points to the filter coefficients @param[in] pState points to the state buffer @param[in] blockSize number of input samples to process @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_LENGTH_ERROR : <code>blockSize</code> is not a multiple of <code>M</code> @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> @par <code>pState</code> points to the array of state variables. <code>pState</code> is of length <code>numTaps+blockSize-1</code> words where <code>blockSize</code> is the number of input samples to the call <code>arm_fir_decimate_q15()</code>. <code>M</code> is the decimation factor. */ arm_status arm_fir_decimate_init_q15( arm_fir_decimate_instance_q15 * S, uint16_t numTaps, uint8_t M, const q15_t * pCoeffs, q15_t * pState, uint32_t blockSize) { arm_status status; /* The size of the input block must be a multiple of the decimation factor */ if ((blockSize % M) != 0U) { /* Set status as ARM_MATH_LENGTH_ERROR */ status = ARM_MATH_LENGTH_ERROR; } else { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear the state buffer. The size is always (blockSize + numTaps - 1) */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q15_t)); /* Assign state pointer */ S->pState = pState; /* Assign Decimation Factor */ S->M = M; status = ARM_MATH_SUCCESS; } return (status); } /** @} end of FIR_decimate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_decimate_init_q15.c
C
apache-2.0
3,323
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_decimate_init_q31.c * Description: Initialization function for Q31 FIR Decimation filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_decimate @{ */ /** @brief Initialization function for the Q31 FIR decimator. @param[in,out] S points to an instance of the Q31 FIR decimator structure @param[in] numTaps number of coefficients in the filter @param[in] M decimation factor @param[in] pCoeffs points to the filter coefficients @param[in] pState points to the state buffer @param[in] blockSize number of input samples to process @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_LENGTH_ERROR : <code>blockSize</code> is not a multiple of <code>M</code> @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> @par <code>pState</code> points to the array of state variables. <code>pState</code> is of length <code>numTaps+blockSize-1</code> words where <code>blockSize</code> is the number of input samples passed to <code>arm_fir_decimate_q31()</code>. <code>M</code> is the decimation factor. */ arm_status arm_fir_decimate_init_q31( arm_fir_decimate_instance_q31 * S, uint16_t numTaps, uint8_t M, const q31_t * pCoeffs, q31_t * pState, uint32_t blockSize) { arm_status status; /* The size of the input block must be a multiple of the decimation factor */ if ((blockSize % M) != 0U) { /* Set status as ARM_MATH_LENGTH_ERROR */ status = ARM_MATH_LENGTH_ERROR; } else { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear the state buffer. The size is always (blockSize + numTaps - 1) */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q31_t)); /* Assign state pointer */ S->pState = pState; /* Assign Decimation Factor */ S->M = M; status = ARM_MATH_SUCCESS; } return (status); } /** @} end of FIR_decimate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_decimate_init_q31.c
C
apache-2.0
3,306
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_decimate_q15.c * Description: Q15 FIR Decimator * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_decimate @{ */ /** @brief Processing function for the Q15 FIR decimator. @param[in] S points to an instance of the Q15 FIR decimator structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of input samples to process per call @return none @par Scaling and Overflow Behavior The function is implemented using a 64-bit internal accumulator. Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits. Lastly, the accumulator is saturated to yield a result in 1.15 format. @remark Refer to \ref arm_fir_decimate_fast_q15() for a faster but less precise implementation of this function. */ #if defined (ARM_MATH_DSP) void arm_fir_decimate_q15( const arm_fir_decimate_instance_q15 * S, const q15_t * pSrc, q15_t * pDst, uint32_t blockSize) { q15_t *pState = S->pState; /* State pointer */ const q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *pStateCur; /* Points to the current sample of the state */ q15_t *px; /* Temporary pointer for state buffer */ const q15_t *pb; /* Temporary pointer for coefficient buffer */ q31_t x0, x1, c0; /* Temporary variables to hold state and coefficient values */ q63_t sum0; /* Accumulators */ q63_t acc0, acc1; q15_t *px0, *px1; uint32_t blkCntN3; uint32_t numTaps = S->numTaps; /* Number of taps */ uint32_t i, blkCnt, tapCnt, outBlockSize = blockSize / S->M; /* Loop counters */ #if defined (ARM_MATH_LOOPUNROLL) q31_t c1; /* Temporary variables to hold state and coefficient values */ #endif /* S->pState buffer contains previous frame (numTaps - 1) samples */ /* pStateCur points to the location where the new input data should be written */ pStateCur = S->pState + (numTaps - 1U); /* Total number of output samples to be computed */ blkCnt = outBlockSize / 2; blkCntN3 = outBlockSize - (2 * blkCnt); while (blkCnt > 0U) { /* Copy 2 * decimation factor number of new input samples into the state buffer */ i = S->M * 2; do { *pStateCur++ = *pSrc++; } while (--i); /* Set accumulator to zero */ acc0 = 0; acc1 = 0; /* Initialize state pointer for all the samples */ px0 = pState; px1 = pState + S->M; /* Initialize coeff pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Read the b[numTaps-1] and b[numTaps-2] coefficients */ c0 = read_q15x2_ia ((q15_t **) &pb); /* Read x[n-numTaps-1] and x[n-numTaps-2]sample */ x0 = read_q15x2_ia (&px0); x1 = read_q15x2_ia (&px1); /* Perform the multiply-accumulate */ acc0 = __SMLALD(x0, c0, acc0); acc1 = __SMLALD(x1, c0, acc1); /* Read the b[numTaps-3] and b[numTaps-4] coefficient */ c0 = read_q15x2_ia ((q15_t **) &pb); /* Read x[n-numTaps-2] and x[n-numTaps-3] sample */ x0 = read_q15x2_ia (&px0); x1 = read_q15x2_ia (&px1); /* Perform the multiply-accumulate */ acc0 = __SMLALD(x0, c0, acc0); acc1 = __SMLALD(x1, c0, acc1); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Read coefficients */ c0 = *pb++; /* Fetch state variables for acc0, acc1 */ x0 = *px0++; x1 = *px1++; /* Perform the multiply-accumulate */ acc0 = __SMLALD(x0, c0, acc0); acc1 = __SMLALD(x1, c0, acc1); /* Decrement loop counter */ tapCnt--; } /* Advance the state pointer by the decimation factor * to process the next group of decimation factor number samples */ pState = pState + S->M * 2; /* Store filter output, smlad returns the values in 2.14 format */ /* so downsacle by 15 to get output in 1.15 */ *pDst++ = (q15_t) (__SSAT((acc0 >> 15), 16)); *pDst++ = (q15_t) (__SSAT((acc1 >> 15), 16)); /* Decrement loop counter */ blkCnt--; } while (blkCntN3 > 0U) { /* Copy decimation factor number of new input samples into the state buffer */ i = S->M; do { *pStateCur++ = *pSrc++; } while (--i); /* Set accumulator to zero */ sum0 = 0; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Read the b[numTaps-1] and b[numTaps-2] coefficients */ c0 = read_q15x2_ia ((q15_t **) &pb); /* Read x[n-numTaps-1] and x[n-numTaps-2] sample */ x0 = read_q15x2_ia (&px); /* Read the b[numTaps-3] and b[numTaps-4] coefficients */ c1 = read_q15x2_ia ((q15_t **) &pb); /* Perform the multiply-accumulate */ sum0 = __SMLALD(x0, c0, sum0); /* Read x[n-numTaps-2] and x[n-numTaps-3] sample */ x0 = read_q15x2_ia (&px); /* Perform the multiply-accumulate */ sum0 = __SMLALD(x0, c1, sum0); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Read coefficients */ c0 = *pb++; /* Fetch 1 state variable */ x0 = *px++; /* Perform the multiply-accumulate */ sum0 = __SMLALD(x0, c0, sum0); /* Decrement loop counter */ tapCnt--; } /* Advance the state pointer by the decimation factor * to process the next group of decimation factor number samples */ pState = pState + S->M; /* Store filter output, smlad returns the values in 2.14 format */ /* so downsacle by 15 to get output in 1.15 */ *pDst++ = (q15_t) (__SSAT((sum0 >> 15), 16)); /* Decrement loop counter */ blkCntN3--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the satrt of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCur = S->pState; i = (numTaps - 1U) >> 2U; /* copy data */ while (i > 0U) { write_q15x2_ia (&pStateCur, read_q15x2_ia (&pState)); write_q15x2_ia (&pStateCur, read_q15x2_ia (&pState)); /* Decrement loop counter */ i--; } i = (numTaps - 1U) % 0x04U; /* Copy data */ while (i > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ i--; } } #else /* #if defined (ARM_MATH_DSP) */ void arm_fir_decimate_q15( const arm_fir_decimate_instance_q15 * S, const q15_t * pSrc, q15_t * pDst, uint32_t blockSize) { q15_t *pState = S->pState; /* State pointer */ const q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *pStateCur; /* Points to the current sample of the state */ q15_t *px; /* Temporary pointer for state buffer */ const q15_t *pb; /* Temporary pointer for coefficient buffer */ q15_t x0, x1, c0; /* Temporary variables to hold state and coefficient values */ q63_t sum0; /* Accumulators */ q63_t acc0, acc1; q15_t *px0, *px1; uint32_t blkCntN3; uint32_t numTaps = S->numTaps; /* Number of taps */ uint32_t i, blkCnt, tapCnt, outBlockSize = blockSize / S->M; /* Loop counters */ /* S->pState buffer contains previous frame (numTaps - 1) samples */ /* pStateCur points to the location where the new input data should be written */ pStateCur = S->pState + (numTaps - 1U); /* Total number of output samples to be computed */ blkCnt = outBlockSize / 2; blkCntN3 = outBlockSize - (2 * blkCnt); while (blkCnt > 0U) { /* Copy 2 * decimation factor number of new input samples into the state buffer */ i = S->M * 2; do { *pStateCur++ = *pSrc++; } while (--i); /* Set accumulator to zero */ acc0 = 0; acc1 = 0; /* Initialize state pointer */ px0 = pState; px1 = pState + S->M; /* Initialize coeff pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Read the Read b[numTaps-1] coefficients */ c0 = *pb++; /* Read x[n-numTaps-1] for sample 0 and for sample 1 */ x0 = *px0++; x1 = *px1++; /* Perform the multiply-accumulate */ acc0 += x0 * c0; acc1 += x1 * c0; /* Read the b[numTaps-2] coefficient */ c0 = *pb++; /* Read x[n-numTaps-2] for sample 0 and sample 1 */ x0 = *px0++; x1 = *px1++; /* Perform the multiply-accumulate */ acc0 += x0 * c0; acc1 += x1 * c0; /* Read the b[numTaps-3] coefficients */ c0 = *pb++; /* Read x[n-numTaps-3] for sample 0 and sample 1 */ x0 = *px0++; x1 = *px1++; /* Perform the multiply-accumulate */ acc0 += x0 * c0; acc1 += x1 * c0; /* Read the b[numTaps-4] coefficient */ c0 = *pb++; /* Read x[n-numTaps-4] for sample 0 and sample 1 */ x0 = *px0++; x1 = *px1++; /* Perform the multiply-accumulate */ acc0 += x0 * c0; acc1 += x1 * c0; /* Decrement the loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Read coefficients */ c0 = *pb++; /* Fetch 1 state variable */ x0 = *px0++; x1 = *px1++; /* Perform the multiply-accumulate */ acc0 += x0 * c0; acc1 += x1 * c0; /* Decrement the loop counter */ tapCnt--; } /* Advance the state pointer by the decimation factor * to process the next group of decimation factor number samples */ pState = pState + S->M * 2; /* Store filter output, smlad returns the values in 2.14 format */ /* so downsacle by 15 to get output in 1.15 */ *pDst++ = (q15_t) (__SSAT((acc0 >> 15), 16)); *pDst++ = (q15_t) (__SSAT((acc1 >> 15), 16)); /* Decrement loop counter */ blkCnt--; } while (blkCntN3 > 0U) { /* Copy decimation factor number of new input samples into the state buffer */ i = S->M; do { *pStateCur++ = *pSrc++; } while (--i); /* Set accumulator to zero */ sum0 = 0; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Read the b[numTaps-1] coefficient */ c0 = *pb++; /* Read x[n-numTaps-1] sample */ x0 = *px++; /* Perform the multiply-accumulate */ sum0 += x0 * c0; /* Read the b[numTaps-2] coefficient */ c0 = *pb++; /* Read x[n-numTaps-2] sample */ x0 = *px++; /* Perform the multiply-accumulate */ sum0 += x0 * c0; /* Read the b[numTaps-3] coefficient */ c0 = *pb++; /* Read x[n-numTaps-3] sample */ x0 = *px++; /* Perform the multiply-accumulate */ sum0 += x0 * c0; /* Read the b[numTaps-4] coefficient */ c0 = *pb++; /* Read x[n-numTaps-4] sample */ x0 = *px++; /* Perform the multiply-accumulate */ sum0 += x0 * c0; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Read coefficients */ c0 = *pb++; /* Fetch 1 state variable */ x0 = *px++; /* Perform the multiply-accumulate */ sum0 += x0 * c0; /* Decrement the loop counter */ tapCnt--; } /* Advance the state pointer by the decimation factor * to process the next group of decimation factor number samples */ pState = pState + S->M; /* Store filter output, smlad returns the values in 2.14 format */ /* so downsacle by 15 to get output in 1.15 */ *pDst++ = (q15_t) (__SSAT((sum0 >> 15), 16)); /* Decrement loop counter */ blkCntN3--; } /* Processing is complete. ** Now copy the last numTaps - 1 samples to the satrt of the state buffer. ** This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCur = S->pState; i = (numTaps - 1U) >> 2U; /* copy data */ while (i > 0U) { *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; /* Decrement loop counter */ i--; } i = (numTaps - 1U) % 0x04U; /* copy data */ while (i > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ i--; } } #endif /* #if defined (ARM_MATH_DSP) */ /** @} end of FIR_decimate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_decimate_q15.c
C
apache-2.0
15,691
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_decimate_q31.c * Description: Q31 FIR Decimator * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_decimate @{ */ /** @brief Processing function for the Q31 FIR decimator. @param[in] S points to an instance of the Q31 FIR decimator structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. Thus, if the accumulator result overflows it wraps around rather than clip. In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits (where log2 is read as log to the base 2). After all multiply-accumulates are performed, the 2.62 accumulator is truncated to 1.32 format and then saturated to 1.31 format. @remark Refer to \ref arm_fir_decimate_fast_q31() for a faster but less precise implementation of this function. */ void arm_fir_decimate_q31( const arm_fir_decimate_instance_q31 * S, const q31_t * pSrc, q31_t * pDst, uint32_t blockSize) { q31_t *pState = S->pState; /* State pointer */ const q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q31_t *pStateCur; /* Points to the current sample of the state */ q31_t *px0; /* Temporary pointer for state buffer */ const q31_t *pb; /* Temporary pointer for coefficient buffer */ q31_t x0, c0; /* Temporary variables to hold state and coefficient values */ q63_t acc0; /* Accumulator */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t i, tapCnt, blkCnt, outBlockSize = blockSize / S->M; /* Loop counters */ #if defined (ARM_MATH_LOOPUNROLL) q31_t *px1, *px2, *px3; q31_t x1, x2, x3; q63_t acc1, acc2, acc3; #endif /* S->pState buffer contains previous frame (numTaps - 1) samples */ /* pStateCur points to the location where the new input data should be written */ pStateCur = S->pState + (numTaps - 1U); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 samples at a time */ blkCnt = outBlockSize >> 2U; /* Samples loop unrolled by 4 */ while (blkCnt > 0U) { /* Copy 4 * decimation factor number of new input samples into the state buffer */ i = S->M * 4; do { *pStateCur++ = *pSrc++; } while (--i); /* Set accumulators to zero */ acc0 = 0; acc1 = 0; acc2 = 0; acc3 = 0; /* Initialize state pointer for all the samples */ px0 = pState; px1 = pState + S->M; px2 = pState + 2 * S->M; px3 = pState + 3 * S->M; /* Initialize coeff pointer */ pb = pCoeffs; /* Loop unrolling: Compute 4 taps at a time */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Read the b[numTaps-1] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-1] sample for acc0 */ x0 = *(px0++); /* Read x[n-numTaps-1] sample for acc1 */ x1 = *(px1++); /* Read x[n-numTaps-1] sample for acc2 */ x2 = *(px2++); /* Read x[n-numTaps-1] sample for acc3 */ x3 = *(px3++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; acc1 += (q63_t) x1 * c0; acc2 += (q63_t) x2 * c0; acc3 += (q63_t) x3 * c0; /* Read the b[numTaps-2] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-2] sample for acc0, acc1, acc2, acc3 */ x0 = *(px0++); x1 = *(px1++); x2 = *(px2++); x3 = *(px3++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; acc1 += (q63_t) x1 * c0; acc2 += (q63_t) x2 * c0; acc3 += (q63_t) x3 * c0; /* Read the b[numTaps-3] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-3] sample acc0, acc1, acc2, acc3 */ x0 = *(px0++); x1 = *(px1++); x2 = *(px2++); x3 = *(px3++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; acc1 += (q63_t) x1 * c0; acc2 += (q63_t) x2 * c0; acc3 += (q63_t) x3 * c0; /* Read the b[numTaps-4] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-4] sample acc0, acc1, acc2, acc3 */ x0 = *(px0++); x1 = *(px1++); x2 = *(px2++); x3 = *(px3++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; acc1 += (q63_t) x1 * c0; acc2 += (q63_t) x2 * c0; acc3 += (q63_t) x3 * c0; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; while (tapCnt > 0U) { /* Read coefficients */ c0 = *(pb++); /* Fetch state variables for acc0, acc1, acc2, acc3 */ x0 = *(px0++); x1 = *(px1++); x2 = *(px2++); x3 = *(px3++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; acc1 += (q63_t) x1 * c0; acc2 += (q63_t) x2 * c0; acc3 += (q63_t) x3 * c0; /* Decrement loop counter */ tapCnt--; } /* Advance the state pointer by the decimation factor * to process the next group of decimation factor number samples */ pState = pState + S->M * 4; /* The result is in the accumulator, store in the destination buffer. */ *pDst++ = (q31_t) (acc0 >> 31); *pDst++ = (q31_t) (acc1 >> 31); *pDst++ = (q31_t) (acc2 >> 31); *pDst++ = (q31_t) (acc3 >> 31); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining samples */ blkCnt = outBlockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = outBlockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy decimation factor number of new input samples into the state buffer */ i = S->M; do { *pStateCur++ = *pSrc++; } while (--i); /* Set accumulator to zero */ acc0 = 0; /* Initialize state pointer */ px0 = pState; /* Initialize coeff pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Read the b[numTaps-1] coefficient */ c0 = *pb++; /* Read x[n-numTaps-1] sample */ x0 = *px0++; /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; /* Read the b[numTaps-2] coefficient */ c0 = *pb++; /* Read x[n-numTaps-2] sample */ x0 = *px0++; /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; /* Read the b[numTaps-3] coefficient */ c0 = *pb++; /* Read x[n-numTaps-3] sample */ x0 = *px0++; /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; /* Read the b[numTaps-4] coefficient */ c0 = *pb++; /* Read x[n-numTaps-4] sample */ x0 = *px0++; /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Read coefficients */ c0 = *pb++; /* Fetch 1 state variable */ x0 = *px0++; /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; /* Decrement loop counter */ tapCnt--; } /* Advance the state pointer by the decimation factor * to process the next group of decimation factor number samples */ pState = pState + S->M; /* The result is in the accumulator, store in the destination buffer. */ *pDst++ = (q31_t) (acc0 >> 31); /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the satrt of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCur = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = (numTaps - 1U) >> 2U; /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = (numTaps - 1U) % 0x04U; #else /* Initialize tapCnt with number of taps */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } } /** @} end of FIR_decimate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_decimate_q31.c
C
apache-2.0
10,422
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_f32.c * Description: Floating-point FIR filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @defgroup FIR Finite Impulse Response (FIR) Filters This set of functions implements Finite Impulse Response (FIR) filters for Q7, Q15, Q31, and floating-point data types. Fast versions of Q15 and Q31 are also provided. The functions operate on blocks of input and output data and each call to the function processes <code>blockSize</code> samples through the filter. <code>pSrc</code> and <code>pDst</code> points to input and output arrays containing <code>blockSize</code> values. @par Algorithm The FIR filter algorithm is based upon a sequence of multiply-accumulate (MAC) operations. Each filter coefficient <code>b[n]</code> is multiplied by a state variable which equals a previous input sample <code>x[n]</code>. <pre> y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1] </pre> @par \image html FIR.GIF "Finite Impulse Response filter" @par <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>. Coefficients are stored in time reversed order. @par <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> @par <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>. Samples in the state buffer are stored in the following order. @par <pre> {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]} </pre> @par Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code>. The increased state buffer length allows circular addressing, which is traditionally used in the FIR filters, to be avoided and yields a significant speed improvement. The state variables are updated after each block of data is processed; the coefficients are untouched. @par Instance Structure The coefficients and state variables for a filter are stored together in an instance data structure. A separate instance structure must be defined for each filter. Coefficient arrays may be shared among several instances while state variable arrays cannot be shared. There are separate instance structure declarations for each of the 4 supported data types. @par Initialization Functions There is also an associated initialization function for each data type. The initialization function performs the following operations: - Sets the values of the internal structure fields. - Zeros out the values in the state buffer. To do this manually without calling the init function, assign the follow subfields of the instance structure: numTaps, pCoeffs, pState. Also set all of the values in pState to zero. @par Use of the initialization function is optional. However, if the initialization function is used, then the instance structure cannot be placed into a const data section. To place an instance structure into a const data section, the instance structure must be manually initialized. Set the values in the state buffer to zeros before static initialization. The code below statically initializes each of the 4 different data type filter instance structures <pre> arm_fir_instance_f32 S = {numTaps, pState, pCoeffs}; arm_fir_instance_q31 S = {numTaps, pState, pCoeffs}; arm_fir_instance_q15 S = {numTaps, pState, pCoeffs}; arm_fir_instance_q7 S = {numTaps, pState, pCoeffs}; </pre> where <code>numTaps</code> is the number of filter coefficients in the filter; <code>pState</code> is the address of the state buffer; <code>pCoeffs</code> is the address of the coefficient buffer. @par Fixed-Point Behavior Care must be taken when using the fixed-point versions of the FIR filter functions. In particular, the overflow and saturation behavior of the accumulator used in each function must be considered. Refer to the function specific documentation below for usage guidelines. */ /** @addtogroup FIR @{ */ /** @brief Processing function for floating-point FIR filter. @param[in] S points to an instance of the floating-point FIR filter structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none */ #if defined(ARM_MATH_NEON) void arm_fir_f32( const arm_fir_instance_f32 * S, const float32_t * pSrc, float32_t * pDst, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ const float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *pStateCurnt; /* Points to the current sample of the state */ float32_t *px; /* Temporary pointers for state buffer */ const float32_t *pb; /* Temporary pointers for coefficient buffer */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t i, tapCnt, blkCnt; /* Loop counters */ float32x4_t accv0,accv1,samples0,samples1,x0,x1,x2,xa,xb,x,b,accv; uint32x4_t x0_u,x1_u,x2_u,xa_u,xb_u; float32_t acc; /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); /* Loop unrolling */ blkCnt = blockSize >> 3; while (blkCnt > 0U) { /* Copy 8 samples at a time into state buffers */ samples0 = vld1q_f32(pSrc); vst1q_f32(pStateCurnt,samples0); pStateCurnt += 4; pSrc += 4 ; samples1 = vld1q_f32(pSrc); vst1q_f32(pStateCurnt,samples1); pStateCurnt += 4; pSrc += 4 ; /* Set the accumulators to zero */ accv0 = vdupq_n_f32(0); accv1 = vdupq_n_f32(0); /* Initialize state pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Loop unroling */ i = numTaps >> 2; /* Perform the multiply-accumulates */ x0 = vld1q_f32(px); x1 = vld1q_f32(px + 4); while(i > 0) { /* acc = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] */ x2 = vld1q_f32(px + 8); b = vld1q_f32(pb); xa = x0; xb = x1; accv0 = vmlaq_n_f32(accv0,xa,b[0]); accv1 = vmlaq_n_f32(accv1,xb,b[0]); xa = vextq_f32(x0,x1,1); xb = vextq_f32(x1,x2,1); accv0 = vmlaq_n_f32(accv0,xa,b[1]); accv1 = vmlaq_n_f32(accv1,xb,b[1]); xa = vextq_f32(x0,x1,2); xb = vextq_f32(x1,x2,2); accv0 = vmlaq_n_f32(accv0,xa,b[2]); accv1 = vmlaq_n_f32(accv1,xb,b[2]); xa = vextq_f32(x0,x1,3); xb = vextq_f32(x1,x2,3); accv0 = vmlaq_n_f32(accv0,xa,b[3]); accv1 = vmlaq_n_f32(accv1,xb,b[3]); pb += 4; x0 = x1; x1 = x2; px += 4; i--; } /* Tail */ i = numTaps & 3; x2 = vld1q_f32(px + 8); /* Perform the multiply-accumulates */ switch(i) { case 3: { accv0 = vmlaq_n_f32(accv0,x0,*pb); accv1 = vmlaq_n_f32(accv1,x1,*pb); pb++; xa = vextq_f32(x0,x1,1); xb = vextq_f32(x1,x2,1); accv0 = vmlaq_n_f32(accv0,xa,*pb); accv1 = vmlaq_n_f32(accv1,xb,*pb); pb++; xa = vextq_f32(x0,x1,2); xb = vextq_f32(x1,x2,2); accv0 = vmlaq_n_f32(accv0,xa,*pb); accv1 = vmlaq_n_f32(accv1,xb,*pb); } break; case 2: { accv0 = vmlaq_n_f32(accv0,x0,*pb); accv1 = vmlaq_n_f32(accv1,x1,*pb); pb++; xa = vextq_f32(x0,x1,1); xb = vextq_f32(x1,x2,1); accv0 = vmlaq_n_f32(accv0,xa,*pb); accv1 = vmlaq_n_f32(accv1,xb,*pb); } break; case 1: { accv0 = vmlaq_n_f32(accv0,x0,*pb); accv1 = vmlaq_n_f32(accv1,x1,*pb); } break; default: break; } /* The result is stored in the destination buffer. */ vst1q_f32(pDst,accv0); pDst += 4; vst1q_f32(pDst,accv1); pDst += 4; /* Advance state pointer by 8 for the next 8 samples */ pState = pState + 8; blkCnt--; } /* Tail */ blkCnt = blockSize & 0x7; while (blkCnt > 0U) { /* Copy one sample at a time into state buffer */ *pStateCurnt++ = *pSrc++; /* Set the accumulator to zero */ acc = 0.0f; /* Initialize state pointer */ px = pState; /* Initialize Coefficient pointer */ pb = pCoeffs; i = numTaps; /* Perform the multiply-accumulates */ do { /* acc = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] */ acc += *px++ * *pb++; i--; } while (i > 0U); /* The result is stored in the destination buffer. */ *pDst++ = acc; /* Advance state pointer by 1 for the next sample */ pState = pState + 1; blkCnt--; } /* Processing is complete. ** Now copy the last numTaps - 1 samples to the starting of the state buffer. ** This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCurnt = S->pState; /* Copy numTaps number of values */ tapCnt = numTaps - 1U; /* Copy data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement the loop counter */ tapCnt--; } } #else void arm_fir_f32( const arm_fir_instance_f32 * S, const float32_t * pSrc, float32_t * pDst, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ const float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *pStateCurnt; /* Points to the current sample of the state */ float32_t *px; /* Temporary pointer for state buffer */ const float32_t *pb; /* Temporary pointer for coefficient buffer */ float32_t acc0; /* Accumulator */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t i, tapCnt, blkCnt; /* Loop counters */ #if defined (ARM_MATH_LOOPUNROLL) float32_t acc1, acc2, acc3, acc4, acc5, acc6, acc7; /* Accumulators */ float32_t x0, x1, x2, x3, x4, x5, x6, x7; /* Temporary variables to hold state values */ float32_t c0; /* Temporary variable to hold coefficient value */ #endif /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 8 output values simultaneously. * The variables acc0 ... acc7 hold output values that are being computed: * * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1] * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2] * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3] */ blkCnt = blockSize >> 3U; while (blkCnt > 0U) { /* Copy 4 new input samples into the state buffer. */ *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; /* Set all accumulators to zero */ acc0 = 0.0f; acc1 = 0.0f; acc2 = 0.0f; acc3 = 0.0f; acc4 = 0.0f; acc5 = 0.0f; acc6 = 0.0f; acc7 = 0.0f; /* Initialize state pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* This is separated from the others to avoid * a call to __aeabi_memmove which would be slower */ *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; /* Read the first 7 samples from the state buffer: x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */ x0 = *px++; x1 = *px++; x2 = *px++; x3 = *px++; x4 = *px++; x5 = *px++; x6 = *px++; /* Loop unrolling: process 8 taps at a time. */ tapCnt = numTaps >> 3U; while (tapCnt > 0U) { /* Read the b[numTaps-1] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-3] sample */ x7 = *(px++); /* acc0 += b[numTaps-1] * x[n-numTaps] */ acc0 += x0 * c0; /* acc1 += b[numTaps-1] * x[n-numTaps-1] */ acc1 += x1 * c0; /* acc2 += b[numTaps-1] * x[n-numTaps-2] */ acc2 += x2 * c0; /* acc3 += b[numTaps-1] * x[n-numTaps-3] */ acc3 += x3 * c0; /* acc4 += b[numTaps-1] * x[n-numTaps-4] */ acc4 += x4 * c0; /* acc1 += b[numTaps-1] * x[n-numTaps-5] */ acc5 += x5 * c0; /* acc2 += b[numTaps-1] * x[n-numTaps-6] */ acc6 += x6 * c0; /* acc3 += b[numTaps-1] * x[n-numTaps-7] */ acc7 += x7 * c0; /* Read the b[numTaps-2] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-4] sample */ x0 = *(px++); /* Perform the multiply-accumulate */ acc0 += x1 * c0; acc1 += x2 * c0; acc2 += x3 * c0; acc3 += x4 * c0; acc4 += x5 * c0; acc5 += x6 * c0; acc6 += x7 * c0; acc7 += x0 * c0; /* Read the b[numTaps-3] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-5] sample */ x1 = *(px++); /* Perform the multiply-accumulates */ acc0 += x2 * c0; acc1 += x3 * c0; acc2 += x4 * c0; acc3 += x5 * c0; acc4 += x6 * c0; acc5 += x7 * c0; acc6 += x0 * c0; acc7 += x1 * c0; /* Read the b[numTaps-4] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-6] sample */ x2 = *(px++); /* Perform the multiply-accumulates */ acc0 += x3 * c0; acc1 += x4 * c0; acc2 += x5 * c0; acc3 += x6 * c0; acc4 += x7 * c0; acc5 += x0 * c0; acc6 += x1 * c0; acc7 += x2 * c0; /* Read the b[numTaps-4] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-6] sample */ x3 = *(px++); /* Perform the multiply-accumulates */ acc0 += x4 * c0; acc1 += x5 * c0; acc2 += x6 * c0; acc3 += x7 * c0; acc4 += x0 * c0; acc5 += x1 * c0; acc6 += x2 * c0; acc7 += x3 * c0; /* Read the b[numTaps-4] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-6] sample */ x4 = *(px++); /* Perform the multiply-accumulates */ acc0 += x5 * c0; acc1 += x6 * c0; acc2 += x7 * c0; acc3 += x0 * c0; acc4 += x1 * c0; acc5 += x2 * c0; acc6 += x3 * c0; acc7 += x4 * c0; /* Read the b[numTaps-4] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-6] sample */ x5 = *(px++); /* Perform the multiply-accumulates */ acc0 += x6 * c0; acc1 += x7 * c0; acc2 += x0 * c0; acc3 += x1 * c0; acc4 += x2 * c0; acc5 += x3 * c0; acc6 += x4 * c0; acc7 += x5 * c0; /* Read the b[numTaps-4] coefficient */ c0 = *(pb++); /* Read x[n-numTaps-6] sample */ x6 = *(px++); /* Perform the multiply-accumulates */ acc0 += x7 * c0; acc1 += x0 * c0; acc2 += x1 * c0; acc3 += x2 * c0; acc4 += x3 * c0; acc5 += x4 * c0; acc6 += x5 * c0; acc7 += x6 * c0; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining outputs */ tapCnt = numTaps % 0x8U; while (tapCnt > 0U) { /* Read coefficients */ c0 = *(pb++); /* Fetch 1 state variable */ x7 = *(px++); /* Perform the multiply-accumulates */ acc0 += x0 * c0; acc1 += x1 * c0; acc2 += x2 * c0; acc3 += x3 * c0; acc4 += x4 * c0; acc5 += x5 * c0; acc6 += x6 * c0; acc7 += x7 * c0; /* Reuse the present sample states for next sample */ x0 = x1; x1 = x2; x2 = x3; x3 = x4; x4 = x5; x5 = x6; x6 = x7; /* Decrement loop counter */ tapCnt--; } /* Advance the state pointer by 8 to process the next group of 8 samples */ pState = pState + 8; /* The results in the 8 accumulators, store in the destination buffer. */ *pDst++ = acc0; *pDst++ = acc1; *pDst++ = acc2; *pDst++ = acc3; *pDst++ = acc4; *pDst++ = acc5; *pDst++ = acc6; *pDst++ = acc7; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining output samples */ blkCnt = blockSize % 0x8U; #else /* Initialize blkCnt with number of taps */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy one sample at a time into state buffer */ *pStateCurnt++ = *pSrc++; /* Set the accumulator to zero */ acc0 = 0.0f; /* Initialize state pointer */ px = pState; /* Initialize Coefficient pointer */ pb = pCoeffs; i = numTaps; /* Perform the multiply-accumulates */ do { /* acc = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] */ acc0 += *px++ * *pb++; i--; } while (i > 0U); /* Store result in destination buffer. */ *pDst++ = acc0; /* Advance state pointer by 1 for the next sample */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCurnt = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = (numTaps - 1U) >> 2U; /* Copy data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Calculate remaining number of copies */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy remaining data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } #endif /* #if defined(ARM_MATH_NEON) */ /** * @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_f32.c
C
apache-2.0
21,033
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_fast_q15.c * Description: Q15 Fast FIR filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR @{ */ /** @brief Processing function for the Q15 FIR filter (fast version). @param[in] S points to an instance of the Q15 FIR filter structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior This fast version uses a 32-bit accumulator with 2.30 format. The accumulator maintains full precision of the intermediate multiplication results but provides only a single guard bit. Thus, if the accumulator result overflows it wraps around and distorts the result. In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits. The 2.30 accumulator is then truncated to 2.15 format and saturated to yield the 1.15 result. @remark Refer to \ref arm_fir_q15() for a slower implementation of this function which uses 64-bit accumulation to avoid wrap around distortion. Both the slow and the fast versions use the same instance structure. Use function \ref arm_fir_init_q15() to initialize the filter structure. */ void arm_fir_fast_q15( const arm_fir_instance_q15 * S, const q15_t * pSrc, q15_t * pDst, uint32_t blockSize) { q15_t *pState = S->pState; /* State pointer */ const q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *pStateCurnt; /* Points to the current sample of the state */ q15_t *px; /* Temporary pointer for state buffer */ const q15_t *pb; /* Temporary pointer for coefficient buffer */ q31_t acc0; /* Accumulators */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ #if defined (ARM_MATH_LOOPUNROLL) q31_t acc1, acc2, acc3; /* Accumulators */ q31_t x0, x1, x2, c0; /* Temporary variables to hold state and coefficient values */ #endif /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 output values simultaneously. * The variables acc0 ... acc3 hold output values that are being computed: * * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1] * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2] * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3] */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Copy 4 new input samples into the state buffer. */ *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; /* Set all accumulators to zero */ acc0 = 0; acc1 = 0; acc2 = 0; acc3 = 0; /* Typecast q15_t pointer to q31_t pointer for state reading in q31_t */ px = pState; /* Typecast q15_t pointer to q31_t pointer for coefficient reading in q31_t */ pb = pCoeffs; /* Read the first two samples from the state buffer: x[n-N], x[n-N-1] */ x0 = read_q15x2_ia (&px); /* Read the third and forth samples from the state buffer: x[n-N-2], x[n-N-3] */ x2 = read_q15x2_ia (&px); /* Loop over the number of taps. Unroll by a factor of 4. Repeat until we've computed numTaps-(numTaps%4) coefficients. */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Read the first two coefficients using SIMD: b[N] and b[N-1] coefficients */ c0 = read_q15x2_ia ((q15_t **) &pb); /* acc0 += b[N] * x[n-N] + b[N-1] * x[n-N-1] */ acc0 = __SMLAD(x0, c0, acc0); /* acc2 += b[N] * x[n-N-2] + b[N-1] * x[n-N-3] */ acc2 = __SMLAD(x2, c0, acc2); /* pack x[n-N-1] and x[n-N-2] */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x2, x0, 0); #else x1 = __PKHBT(x0, x2, 0); #endif /* Read state x[n-N-4], x[n-N-5] */ x0 = read_q15x2_ia (&px); /* acc1 += b[N] * x[n-N-1] + b[N-1] * x[n-N-2] */ acc1 = __SMLADX(x1, c0, acc1); /* pack x[n-N-3] and x[n-N-4] */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x0, x2, 0); #else x1 = __PKHBT(x2, x0, 0); #endif /* acc3 += b[N] * x[n-N-3] + b[N-1] * x[n-N-4] */ acc3 = __SMLADX(x1, c0, acc3); /* Read coefficients b[N-2], b[N-3] */ c0 = read_q15x2_ia ((q15_t **) &pb); /* acc0 += b[N-2] * x[n-N-2] + b[N-3] * x[n-N-3] */ acc0 = __SMLAD(x2, c0, acc0); /* Read state x[n-N-6], x[n-N-7] with offset */ x2 = read_q15x2_ia (&px); /* acc2 += b[N-2] * x[n-N-4] + b[N-3] * x[n-N-5] */ acc2 = __SMLAD(x0, c0, acc2); /* acc1 += b[N-2] * x[n-N-3] + b[N-3] * x[n-N-4] */ acc1 = __SMLADX(x1, c0, acc1); /* pack x[n-N-5] and x[n-N-6] */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x2, x0, 0); #else x1 = __PKHBT(x0, x2, 0); #endif /* acc3 += b[N-2] * x[n-N-5] + b[N-3] * x[n-N-6] */ acc3 = __SMLADX(x1, c0, acc3); /* Decrement tap count */ tapCnt--; } /* If the filter length is not a multiple of 4, compute the remaining filter taps. This is always be 2 taps since the filter length is even. */ if ((numTaps & 0x3U) != 0U) { /* Read last two coefficients */ c0 = read_q15x2_ia ((q15_t **) &pb); /* Perform the multiply-accumulates */ acc0 = __SMLAD(x0, c0, acc0); acc2 = __SMLAD(x2, c0, acc2); /* pack state variables */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x2, x0, 0); #else x1 = __PKHBT(x0, x2, 0); #endif /* Read last state variables */ x0 = read_q15x2 (px); /* Perform the multiply-accumulates */ acc1 = __SMLADX(x1, c0, acc1); /* pack state variables */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x0, x2, 0); #else x1 = __PKHBT(x2, x0, 0); #endif /* Perform the multiply-accumulates */ acc3 = __SMLADX(x1, c0, acc3); } /* The results in the 4 accumulators are in 2.30 format. Convert to 1.15 with saturation. Then store the 4 outputs in the destination buffer. */ #ifndef ARM_MATH_BIG_ENDIAN write_q15x2_ia (&pDst, __PKHBT(__SSAT((acc0 >> 15), 16), __SSAT((acc1 >> 15), 16), 16)); write_q15x2_ia (&pDst, __PKHBT(__SSAT((acc2 >> 15), 16), __SSAT((acc3 >> 15), 16), 16)); #else write_q15x2_ia (&pDst, __PKHBT(__SSAT((acc1 >> 15), 16), __SSAT((acc0 >> 15), 16), 16)); write_q15x2_ia (&pDst, __PKHBT(__SSAT((acc3 >> 15), 16), __SSAT((acc2 >> 15), 16), 16)); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Advance the state pointer by 4 to process the next group of 4 samples */ pState = pState + 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining output samples */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of taps */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy two samples into state buffer */ *pStateCurnt++ = *pSrc++; /* Set the accumulator to zero */ acc0 = 0; /* Use SIMD to hold states and coefficients */ px = pState; pb = pCoeffs; tapCnt = numTaps >> 1U; do { acc0 += (q31_t) *px++ * *pb++; acc0 += (q31_t) *px++ * *pb++; tapCnt--; } while (tapCnt > 0U); /* The result is in 2.30 format. Convert to 1.15 with saturation. Then store the output in the destination buffer. */ *pDst++ = (q15_t) (__SSAT((acc0 >> 15), 16)); /* Advance state pointer by 1 for the next sample */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCurnt = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = (numTaps - 1U) >> 2U; /* Copy data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Calculate remaining number of copies */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy remaining data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } /** @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_fast_q15.c
C
apache-2.0
10,587
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_fast_q31.c * Description: Processing function for the Q31 Fast FIR filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR @{ */ /** @brief Processing function for the Q31 FIR filter (fast version). @param[in] S points to an instance of the Q31 structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior This function is optimized for speed at the expense of fixed-point precision and overflow protection. The result of each 1.31 x 1.31 multiplication is truncated to 2.30 format. These intermediate results are added to a 2.30 accumulator. Finally, the accumulator is saturated and converted to a 1.31 result. The fast version has the same overflow behavior as the standard version and provides less precision since it discards the low 32 bits of each multiplication result. In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits. @remark Refer to \ref arm_fir_q31() for a slower implementation of this function which uses a 64-bit accumulator to provide higher precision. Both the slow and the fast versions use the same instance structure. Use function \ref arm_fir_init_q31() to initialize the filter structure. */ IAR_ONLY_LOW_OPTIMIZATION_ENTER void arm_fir_fast_q31( const arm_fir_instance_q31 * S, const q31_t * pSrc, q31_t * pDst, uint32_t blockSize) { q31_t *pState = S->pState; /* State pointer */ const q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q31_t *pStateCurnt; /* Points to the current sample of the state */ q31_t *px; /* Temporary pointer for state buffer */ const q31_t *pb; /* Temporary pointer for coefficient buffer */ q31_t acc0; /* Accumulators */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t i, tapCnt, blkCnt; /* Loop counters */ #if defined (ARM_MATH_LOOPUNROLL) q31_t acc1, acc2, acc3; /* Accumulators */ q31_t x0, x1, x2, x3, c0; /* Temporary variables to hold state and coefficient values */ #endif /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 output values simultaneously. * The variables acc0 ... acc3 hold output values that are being computed: * * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1] * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2] * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3] */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Copy 4 new input samples into the state buffer. */ *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; /* Set all accumulators to zero */ acc0 = 0; acc1 = 0; acc2 = 0; acc3 = 0; /* Initialize state pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Read the first 3 samples from the state buffer: * x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */ x0 = *px++; x1 = *px++; x2 = *px++; /* Loop unrolling. Process 4 taps at a time. */ tapCnt = numTaps >> 2U; /* Loop over the number of taps. Unroll by a factor of 4. Repeat until we've computed numTaps-4 coefficients. */ while (tapCnt > 0U) { /* Read the b[numTaps] coefficient */ c0 = *pb; /* Read x[n-numTaps-3] sample */ x3 = *px; /* acc0 += b[numTaps] * x[n-numTaps] */ multAcc_32x32_keep32_R(acc0, x0, c0); /* acc1 += b[numTaps] * x[n-numTaps-1] */ multAcc_32x32_keep32_R(acc1, x1, c0); /* acc2 += b[numTaps] * x[n-numTaps-2] */ multAcc_32x32_keep32_R(acc2, x2, c0); /* acc3 += b[numTaps] * x[n-numTaps-3] */ multAcc_32x32_keep32_R(acc3, x3, c0); /* Read the b[numTaps-1] coefficient */ c0 = *(pb + 1U); /* Read x[n-numTaps-4] sample */ x0 = *(px + 1U); /* Perform the multiply-accumulates */ multAcc_32x32_keep32_R(acc0, x1, c0); multAcc_32x32_keep32_R(acc1, x2, c0); multAcc_32x32_keep32_R(acc2, x3, c0); multAcc_32x32_keep32_R(acc3, x0, c0); /* Read the b[numTaps-2] coefficient */ c0 = *(pb + 2U); /* Read x[n-numTaps-5] sample */ x1 = *(px + 2U); /* Perform the multiply-accumulates */ multAcc_32x32_keep32_R(acc0, x2, c0); multAcc_32x32_keep32_R(acc1, x3, c0); multAcc_32x32_keep32_R(acc2, x0, c0); multAcc_32x32_keep32_R(acc3, x1, c0); /* Read the b[numTaps-3] coefficients */ c0 = *(pb + 3U); /* Read x[n-numTaps-6] sample */ x2 = *(px + 3U); /* Perform the multiply-accumulates */ multAcc_32x32_keep32_R(acc0, x3, c0); multAcc_32x32_keep32_R(acc1, x0, c0); multAcc_32x32_keep32_R(acc2, x1, c0); multAcc_32x32_keep32_R(acc3, x2, c0); /* update coefficient pointer */ pb += 4U; px += 4U; /* Decrement loop counter */ tapCnt--; } /* If the filter length is not a multiple of 4, compute the remaining filter taps */ tapCnt = numTaps % 0x4U; while (tapCnt > 0U) { /* Read coefficients */ c0 = *(pb++); /* Fetch 1 state variable */ x3 = *(px++); /* Perform the multiply-accumulates */ multAcc_32x32_keep32_R(acc0, x0, c0); multAcc_32x32_keep32_R(acc1, x1, c0); multAcc_32x32_keep32_R(acc2, x2, c0); multAcc_32x32_keep32_R(acc3, x3, c0); /* Reuse the present sample states for next sample */ x0 = x1; x1 = x2; x2 = x3; /* Decrement loop counter */ tapCnt--; } /* The results in the 4 accumulators are in 2.30 format. Convert to 1.31 Then store the 4 outputs in the destination buffer. */ *pDst++ = (q31_t) (acc0 << 1); *pDst++ = (q31_t) (acc1 << 1); *pDst++ = (q31_t) (acc2 << 1); *pDst++ = (q31_t) (acc3 << 1); /* Advance the state pointer by 4 to process the next group of 4 samples */ pState = pState + 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining output samples */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of taps */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy one sample at a time into state buffer */ *pStateCurnt++ = *pSrc++; /* Set the accumulator to zero */ acc0 = 0; /* Initialize state pointer */ px = pState; /* Initialize Coefficient pointer */ pb = pCoeffs; i = numTaps; /* Perform the multiply-accumulates */ do { multAcc_32x32_keep32_R(acc0, (*px++), (*pb++)); i--; } while (i > 0U); /* The result is in 2.30 format. Convert to 1.31 Then store the output in the destination buffer. */ *pDst++ = (q31_t) (acc0 << 1); /* Advance state pointer by 1 for the next sample */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCurnt = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = (numTaps - 1U) >> 2U; /* Copy data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Calculate remaining number of copies */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy remaining data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement the loop counter */ tapCnt--; } } IAR_ONLY_LOW_OPTIMIZATION_EXIT /** @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_fast_q31.c
C
apache-2.0
10,140
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_init_f32.c * Description: Floating-point FIR filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR @{ */ /** @brief Initialization function for the floating-point FIR filter. @param[in,out] S points to an instance of the floating-point FIR filter structure @param[in] numTaps number of filter coefficients in the filter @param[in] pCoeffs points to the filter coefficients buffer @param[in] pState points to the state buffer @param[in] blockSize number of samples processed per call @return none @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> @par <code>pState</code> points to the array of state variables. <code>pState</code> is of length <code>numTaps+blockSize-1</code> samples, where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_fir_f32()</code>. */ void arm_fir_init_f32( arm_fir_instance_f32 * S, uint16_t numTaps, const float32_t * pCoeffs, float32_t * pState, uint32_t blockSize) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer. The size is always (blockSize + numTaps - 1) */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(float32_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_init_f32.c
C
apache-2.0
2,626
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_init_q15.c * Description: Q15 FIR filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR @{ */ /** @brief Initialization function for the Q15 FIR filter. @param[in,out] S points to an instance of the Q15 FIR filter structure. @param[in] numTaps number of filter coefficients in the filter. Must be even and greater than or equal to 4. @param[in] pCoeffs points to the filter coefficients buffer. @param[in] pState points to the state buffer. @param[in] blockSize number of samples processed per call. @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_ARGUMENT_ERROR : <code>numTaps</code> is not greater than or equal to 4 and even @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> Note that <code>numTaps</code> must be even and greater than or equal to 4. To implement an odd length filter simply increase <code>numTaps</code> by 1 and set the last coefficient to zero. For example, to implement a filter with <code>numTaps=3</code> and coefficients <pre> {0.3, -0.8, 0.3} </pre> set <code>numTaps=4</code> and use the coefficients: <pre> {0.3, -0.8, 0.3, 0}. </pre> Similarly, to implement a two point filter <pre> {0.3, -0.3} </pre> set <code>numTaps=4</code> and use the coefficients: <pre> {0.3, -0.3, 0, 0}. </pre> <code>pState</code> points to the array of state variables. <code>pState</code> is of length <code>numTaps+blockSize</code>, when running on Cortex-M4 and Cortex-M3 and is of length <code>numTaps+blockSize-1</code>, when running on Cortex-M0 where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_fir_q15()</code>. */ arm_status arm_fir_init_q15( arm_fir_instance_q15 * S, uint16_t numTaps, const q15_t * pCoeffs, q15_t * pState, uint32_t blockSize) { arm_status status; #if defined (ARM_MATH_DSP) /* The Number of filter coefficients in the filter must be even and at least 4 */ if (numTaps & 0x1U) { status = ARM_MATH_ARGUMENT_ERROR; } else { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear the state buffer. The size is always (blockSize + numTaps ) */ memset(pState, 0, (numTaps + (blockSize)) * sizeof(q15_t)); /* Assign state pointer */ S->pState = pState; status = ARM_MATH_SUCCESS; } return (status); #else /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer. The size is always (blockSize + numTaps - 1) */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q15_t)); /* Assign state pointer */ S->pState = pState; status = ARM_MATH_SUCCESS; return (status); #endif /* #if defined (ARM_MATH_DSP) */ } /** @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_init_q15.c
C
apache-2.0
4,319
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_init_q31.c * Description: Q31 FIR filter initialization function. * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR @{ */ /** @brief Initialization function for the Q31 FIR filter. @param[in,out] S points to an instance of the Q31 FIR filter structure @param[in] numTaps number of filter coefficients in the filter @param[in] pCoeffs points to the filter coefficients buffer @param[in] pState points to the state buffer @param[in] blockSize number of samples processed @return none @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> <code>pState</code> points to the array of state variables. <code>pState</code> is of length <code>numTaps+blockSize-1</code> samples, where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_fir_q31()</code>. */ void arm_fir_init_q31( arm_fir_instance_q31 * S, uint16_t numTaps, const q31_t * pCoeffs, q31_t * pState, uint32_t blockSize) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer. The size is always (blockSize + numTaps - 1) */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q31_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_init_q31.c
C
apache-2.0
2,566
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_init_q7.c * Description: Q7 FIR filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR @{ */ /** @brief Initialization function for the Q7 FIR filter. @param[in,out] S points to an instance of the Q7 FIR filter structure @param[in] numTaps number of filter coefficients in the filter @param[in] pCoeffs points to the filter coefficients buffer @param[in] pState points to the state buffer @param[in] blockSize number of samples processed @return none @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> @par <code>pState</code> points to the array of state variables. <code>pState</code> is of length <code>numTaps+blockSize-1</code> samples, where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_fir_q7()</code>. */ void arm_fir_init_q7( arm_fir_instance_q7 * S, uint16_t numTaps, const q7_t * pCoeffs, q7_t * pState, uint32_t blockSize) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer. The size is always (blockSize + numTaps - 1) */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q7_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_init_q7.c
C
apache-2.0
2,562
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_interpolate_f32.c * Description: Floating-point FIR interpolation sequences * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @defgroup FIR_Interpolate Finite Impulse Response (FIR) Interpolator These functions combine an upsampler (zero stuffer) and an FIR filter. They are used in multirate systems for increasing the sample rate of a signal without introducing high frequency images. Conceptually, the functions are equivalent to the block diagram below: \image html FIRInterpolator.gif "Components included in the FIR Interpolator functions" After upsampling by a factor of <code>L</code>, the signal should be filtered by a lowpass filter with a normalized cutoff frequency of <code>1/L</code> in order to eliminate high frequency copies of the spectrum. The user of the function is responsible for providing the filter coefficients. The FIR interpolator functions provided in the CMSIS DSP Library combine the upsampler and FIR filter in an efficient manner. The upsampler inserts <code>L-1</code> zeros between each sample. Instead of multiplying by these zero values, the FIR filter is designed to skip them. This leads to an efficient implementation without any wasted effort. The functions operate on blocks of input and output data. <code>pSrc</code> points to an array of <code>blockSize</code> input values and <code>pDst</code> points to an array of <code>blockSize*L</code> output values. The library provides separate functions for Q15, Q31, and floating-point data types. @par Algorithm The functions use a polyphase filter structure: <pre> y[n] = b[0] * x[n] + b[L] * x[n-1] + ... + b[L*(phaseLength-1)] * x[n-phaseLength+1] y[n+1] = b[1] * x[n] + b[L+1] * x[n-1] + ... + b[L*(phaseLength-1)+1] * x[n-phaseLength+1] ... y[n+(L-1)] = b[L-1] * x[n] + b[2*L-1] * x[n-1] + ....+ b[L*(phaseLength-1)+(L-1)] * x[n-phaseLength+1] </pre> This approach is more efficient than straightforward upsample-then-filter algorithms. With this method the computation is reduced by a factor of <code>1/L</code> when compared to using a standard FIR filter. @par <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>. <code>numTaps</code> must be a multiple of the interpolation factor <code>L</code> and this is checked by the initialization functions. Internally, the function divides the FIR filter's impulse response into shorter filters of length <code>phaseLength=numTaps/L</code>. Coefficients are stored in time reversed order. <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> @par <code>pState</code> points to a state array of size <code>blockSize + phaseLength - 1</code>. Samples in the state buffer are stored in the order: <pre> {x[n-phaseLength+1], x[n-phaseLength], x[n-phaseLength-1], x[n-phaseLength-2]....x[0], x[1], ..., x[blockSize-1]} </pre> @par The state variables are updated after each block of data is processed, the coefficients are untouched. @par Instance Structure The coefficients and state variables for a filter are stored together in an instance data structure. A separate instance structure must be defined for each filter. Coefficient arrays may be shared among several instances while state variable array should be allocated separately. There are separate instance structure declarations for each of the 3 supported data types. @par Initialization Functions There is also an associated initialization function for each data type. The initialization function performs the following operations: - Sets the values of the internal structure fields. - Zeros out the values in the state buffer. - Checks to make sure that the length of the filter is a multiple of the interpolation factor. To do this manually without calling the init function, assign the follow subfields of the instance structure: L (interpolation factor), pCoeffs, phaseLength (numTaps / L), pState. Also set all of the values in pState to zero. @par Use of the initialization function is optional. However, if the initialization function is used, then the instance structure cannot be placed into a const data section. To place an instance structure into a const data section, the instance structure must be manually initialized. The code below statically initializes each of the 3 different data type filter instance structures <pre> arm_fir_interpolate_instance_f32 S = {L, phaseLength, pCoeffs, pState}; arm_fir_interpolate_instance_q31 S = {L, phaseLength, pCoeffs, pState}; arm_fir_interpolate_instance_q15 S = {L, phaseLength, pCoeffs, pState}; </pre> @par where <code>L</code> is the interpolation factor; <code>phaseLength=numTaps/L</code> is the length of each of the shorter FIR filters used internally, <code>pCoeffs</code> is the address of the coefficient buffer; <code>pState</code> is the address of the state buffer. Be sure to set the values in the state buffer to zeros when doing static initialization. @par Fixed-Point Behavior Care must be taken when using the fixed-point versions of the FIR interpolate filter functions. In particular, the overflow and saturation behavior of the accumulator used in each function must be considered. Refer to the function specific documentation below for usage guidelines. */ /** @addtogroup FIR_Interpolate @{ */ /** @brief Processing function for floating-point FIR interpolator. @param[in] S points to an instance of the floating-point FIR interpolator structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none */ #if defined(ARM_MATH_NEON) void arm_fir_interpolate_f32( const arm_fir_interpolate_instance_f32 * S, const float32_t * pSrc, float32_t * pDst, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ const float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *pStateCurnt; /* Points to the current sample of the state */ float32_t *ptr1; /* Temporary pointers for state buffer */ const float32_t *ptr2; /* Temporary pointers for coefficient buffer */ float32_t sum0; /* Accumulators */ float32_t x0, c0; /* Temporary variables to hold state and coefficient values */ uint32_t i, blkCnt, j; /* Loop counters */ uint16_t phaseLen = S->phaseLength, tapCnt; /* Length of each polyphase filter component */ float32_t acc0, acc1, acc2, acc3; float32_t x1, x2, x3; uint32_t blkCntN4; float32_t c1, c2, c3; float32x4_t sum0v; float32x4_t accV,accV0,accV1; float32x4_t x0v,x1v,x2v,xa,xb; uint32x4_t x0v_u,x1v_u,x2v_u,xa_u,xb_u; float32x2_t tempV; /* S->pState buffer contains previous frame (phaseLen - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = S->pState + (phaseLen - 1U); /* Initialise blkCnt */ blkCnt = blockSize >> 3; blkCntN4 = blockSize & 7; /* Loop unrolling */ while (blkCnt > 0U) { /* Copy new input samples into the state buffer */ sum0v = vld1q_f32(pSrc); vst1q_f32(pStateCurnt,sum0v); pSrc += 4; pStateCurnt += 4; sum0v = vld1q_f32(pSrc); vst1q_f32(pStateCurnt,sum0v); pSrc += 4; pStateCurnt += 4; /* Address modifier index of coefficient buffer */ j = 1U; /* Loop over the Interpolation factor. */ i = (S->L); while (i > 0U) { /* Set accumulator to zero */ accV0 = vdupq_n_f32(0.0); accV1 = vdupq_n_f32(0.0); /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (S->L - j); /* Loop over the polyPhase length. Unroll by a factor of 4. ** Repeat until we've computed numTaps-(4*S->L) coefficients. */ tapCnt = phaseLen >> 2U; x0v = vld1q_f32(ptr1); x1v = vld1q_f32(ptr1 + 4); while (tapCnt > 0U) { /* Read the input samples */ x2v = vld1q_f32(ptr1 + 8); /* Read the coefficients */ c0 = *(ptr2); /* Perform the multiply-accumulate */ accV0 = vmlaq_n_f32(accV0,x0v,c0); accV1 = vmlaq_n_f32(accV1,x1v,c0); /* Read the coefficients, inputs and perform multiply-accumulate */ c1 = *(ptr2 + S->L); xa = vextq_f32(x0v,x1v,1); xb = vextq_f32(x1v,x2v,1); accV0 = vmlaq_n_f32(accV0,xa,c1); accV1 = vmlaq_n_f32(accV1,xb,c1); /* Read the coefficients, inputs and perform multiply-accumulate */ c2 = *(ptr2 + S->L * 2); xa = vextq_f32(x0v,x1v,2); xb = vextq_f32(x1v,x2v,2); accV0 = vmlaq_n_f32(accV0,xa,c2); accV1 = vmlaq_n_f32(accV1,xb,c2); /* Read the coefficients, inputs and perform multiply-accumulate */ c3 = *(ptr2 + S->L * 3); xa = vextq_f32(x0v,x1v,3); xb = vextq_f32(x1v,x2v,3); accV0 = vmlaq_n_f32(accV0,xa,c3); accV1 = vmlaq_n_f32(accV1,xb,c3); /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += 4 * S->L; ptr1 += 4; x0v = x1v; x1v = x2v; /* Decrement the loop counter */ tapCnt--; } /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */ tapCnt = phaseLen % 0x4U; x2v = vld1q_f32(ptr1 + 8); switch (tapCnt) { case 3: c0 = *(ptr2); accV0 = vmlaq_n_f32(accV0,x0v,c0); accV1 = vmlaq_n_f32(accV1,x1v,c0); ptr2 += S->L; c0 = *(ptr2); xa = vextq_f32(x0v,x1v,1); xb = vextq_f32(x1v,x2v,1); accV0 = vmlaq_n_f32(accV0,xa,c0); accV1 = vmlaq_n_f32(accV1,xb,c0); ptr2 += S->L; c0 = *(ptr2); xa = vextq_f32(x0v,x1v,2); xb = vextq_f32(x1v,x2v,2); accV0 = vmlaq_n_f32(accV0,xa,c0); accV1 = vmlaq_n_f32(accV1,xb,c0); ptr2 += S->L; break; case 2: c0 = *(ptr2); accV0 = vmlaq_n_f32(accV0,x0v,c0); accV1 = vmlaq_n_f32(accV1,x1v,c0); ptr2 += S->L; c0 = *(ptr2); xa = vextq_f32(x0v,x1v,1); xb = vextq_f32(x1v,x2v,1); accV0 = vmlaq_n_f32(accV0,xa,c0); accV1 = vmlaq_n_f32(accV1,xb,c0); ptr2 += S->L; break; case 1: c0 = *(ptr2); accV0 = vmlaq_n_f32(accV0,x0v,c0); accV1 = vmlaq_n_f32(accV1,x1v,c0); ptr2 += S->L; break; default: break; } /* The result is in the accumulator, store in the destination buffer. */ *pDst = accV0[0]; *(pDst + S->L) = accV0[1]; *(pDst + 2 * S->L) = accV0[2]; *(pDst + 3 * S->L) = accV0[3]; *(pDst + 4 * S->L) = accV1[0]; *(pDst + 5 * S->L) = accV1[1]; *(pDst + 6 * S->L) = accV1[2]; *(pDst + 7 * S->L) = accV1[3]; pDst++; /* Increment the address modifier index of coefficient buffer */ j++; /* Decrement the loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 8; pDst += S->L * 7; /* Decrement the loop counter */ blkCnt--; } /* If the blockSize is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ while (blkCntN4 > 0U) { /* Copy new input sample into the state buffer */ *pStateCurnt++ = *pSrc++; /* Address modifier index of coefficient buffer */ j = 1U; /* Loop over the Interpolation factor. */ i = S->L; while (i > 0U) { /* Set accumulator to zero */ sum0v = vdupq_n_f32(0.0); /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (S->L - j); /* Loop over the polyPhase length. Unroll by a factor of 4. ** Repeat until we've computed numTaps-(4*S->L) coefficients. */ tapCnt = phaseLen >> 2U; while (tapCnt > 0U) { /* Read the coefficient */ x1v[0] = *(ptr2); /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Read the input sample */ x0v = vld1q_f32(ptr1); ptr1 += 4; /* Read the coefficient */ x1v[1] = *(ptr2); /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Read the coefficient */ x1v[2] = *(ptr2); /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Read the coefficient */ x1v[3] = *(ptr2); /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; sum0v = vmlaq_f32(sum0v,x0v,x1v); /* Decrement the loop counter */ tapCnt--; } tempV = vpadd_f32(vget_low_f32(sum0v),vget_high_f32(sum0v)); sum0 = tempV[0] + tempV[1]; /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */ tapCnt = phaseLen % 0x4U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += *(ptr1++) * (*ptr2); /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Decrement the loop counter */ tapCnt--; } /* The result is in the accumulator, store in the destination buffer. */ *pDst++ = sum0; /* Increment the address modifier index of coefficient buffer */ j++; /* Decrement the loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 1; /* Decrement the loop counter */ blkCntN4--; } /* Processing is complete. ** Now copy the last phaseLen - 1 samples to the satrt of the state buffer. ** This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCurnt = S->pState; tapCnt = (phaseLen - 1U) >> 2U; /* Copy data */ while (tapCnt > 0U) { sum0v = vld1q_f32(pState); vst1q_f32(pStateCurnt,sum0v); pState += 4; pStateCurnt += 4; /* Decrement the loop counter */ tapCnt--; } tapCnt = (phaseLen - 1U) % 0x04U; /* copy data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement the loop counter */ tapCnt--; } } #else void arm_fir_interpolate_f32( const arm_fir_interpolate_instance_f32 * S, const float32_t * pSrc, float32_t * pDst, uint32_t blockSize) { #if (1) //#if !defined(ARM_MATH_CM0_FAMILY) float32_t *pState = S->pState; /* State pointer */ const float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *pStateCur; /* Points to the current sample of the state */ float32_t *ptr1; /* Temporary pointer for state buffer */ const float32_t *ptr2; /* Temporary pointer for coefficient buffer */ float32_t sum0; /* Accumulators */ uint32_t i, blkCnt, tapCnt; /* Loop counters */ uint32_t phaseLen = S->phaseLength; /* Length of each polyphase filter component */ uint32_t j; #if defined (ARM_MATH_LOOPUNROLL) float32_t acc0, acc1, acc2, acc3; float32_t x0, x1, x2, x3; float32_t c0, c1, c2, c3; #endif /* S->pState buffer contains previous frame (phaseLen - 1) samples */ /* pStateCur points to the location where the new input data should be written */ pStateCur = S->pState + (phaseLen - 1U); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Copy new input sample into the state buffer */ *pStateCur++ = *pSrc++; *pStateCur++ = *pSrc++; *pStateCur++ = *pSrc++; *pStateCur++ = *pSrc++; /* Address modifier index of coefficient buffer */ j = 1U; /* Loop over the Interpolation factor. */ i = (S->L); while (i > 0U) { /* Set accumulator to zero */ acc0 = 0.0f; acc1 = 0.0f; acc2 = 0.0f; acc3 = 0.0f; /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (S->L - j); /* Loop over the polyPhase length. Unroll by a factor of 4. Repeat until we've computed numTaps-(4*S->L) coefficients. */ tapCnt = phaseLen >> 2U; x0 = *(ptr1++); x1 = *(ptr1++); x2 = *(ptr1++); while (tapCnt > 0U) { /* Read the input sample */ x3 = *(ptr1++); /* Read the coefficient */ c0 = *(ptr2); /* Perform the multiply-accumulate */ acc0 += x0 * c0; acc1 += x1 * c0; acc2 += x2 * c0; acc3 += x3 * c0; /* Read the coefficient */ c1 = *(ptr2 + S->L); /* Read the input sample */ x0 = *(ptr1++); /* Perform the multiply-accumulate */ acc0 += x1 * c1; acc1 += x2 * c1; acc2 += x3 * c1; acc3 += x0 * c1; /* Read the coefficient */ c2 = *(ptr2 + S->L * 2); /* Read the input sample */ x1 = *(ptr1++); /* Perform the multiply-accumulate */ acc0 += x2 * c2; acc1 += x3 * c2; acc2 += x0 * c2; acc3 += x1 * c2; /* Read the coefficient */ c3 = *(ptr2 + S->L * 3); /* Read the input sample */ x2 = *(ptr1++); /* Perform the multiply-accumulate */ acc0 += x3 * c3; acc1 += x0 * c3; acc2 += x1 * c3; acc3 += x2 * c3; /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += 4 * S->L; /* Decrement loop counter */ tapCnt--; } /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */ tapCnt = phaseLen % 0x4U; while (tapCnt > 0U) { /* Read the input sample */ x3 = *(ptr1++); /* Read the coefficient */ c0 = *(ptr2); /* Perform the multiply-accumulate */ acc0 += x0 * c0; acc1 += x1 * c0; acc2 += x2 * c0; acc3 += x3 * c0; /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* update states for next sample processing */ x0 = x1; x1 = x2; x2 = x3; /* Decrement loop counter */ tapCnt--; } /* The result is in the accumulator, store in the destination buffer. */ *(pDst ) = acc0; *(pDst + S->L) = acc1; *(pDst + 2 * S->L) = acc2; *(pDst + 3 * S->L) = acc3; pDst++; /* Increment the address modifier index of coefficient buffer */ j++; /* Decrement loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 4; pDst += S->L * 3; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy new input sample into the state buffer */ *pStateCur++ = *pSrc++; /* Address modifier index of coefficient buffer */ j = 1U; /* Loop over the Interpolation factor. */ i = S->L; while (i > 0U) { /* Set accumulator to zero */ sum0 = 0.0f; /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (S->L - j); /* Loop over the polyPhase length. Repeat until we've computed numTaps-(4*S->L) coefficients. */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ tapCnt = phaseLen >> 2U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += *ptr1++ * *ptr2; /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; sum0 += *ptr1++ * *ptr2; ptr2 += S->L; sum0 += *ptr1++ * *ptr2; ptr2 += S->L; sum0 += *ptr1++ * *ptr2; ptr2 += S->L; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining outputs */ tapCnt = phaseLen % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = phaseLen; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += *ptr1++ * *ptr2; /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Decrement loop counter */ tapCnt--; } /* The result is in the accumulator, store in the destination buffer. */ *pDst++ = sum0; /* Increment the address modifier index of coefficient buffer */ j++; /* Decrement the loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 1; /* Decrement the loop counter */ blkCnt--; } /* Processing is complete. Now copy the last phaseLen - 1 samples to the satrt of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCur = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ tapCnt = (phaseLen - 1U) >> 2U; /* copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining outputs */ tapCnt = (phaseLen - 1U) % 0x04U; #else /* Initialize tapCnt with number of samples */ tapCnt = (phaseLen - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #else /* alternate version for CM0_FAMILY */ float32_t *pState = S->pState; /* State pointer */ const float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *pStateCur; /* Points to the current sample of the state */ float32_t *ptr1; /* Temporary pointer for state buffer */ const float32_t *ptr2; /* Temporary pointer for coefficient buffer */ float32_t sum0; /* Accumulators */ uint32_t i, blkCnt, tapCnt; /* Loop counters */ uint32_t phaseLen = S->phaseLength; /* Length of each polyphase filter component */ /* S->pState buffer contains previous frame (phaseLen - 1) samples */ /* pStateCur points to the location where the new input data should be written */ pStateCur = S->pState + (phaseLen - 1U); /* Total number of intput samples */ blkCnt = blockSize; /* Loop over the blockSize. */ while (blkCnt > 0U) { /* Copy new input sample into the state buffer */ *pStateCur++ = *pSrc++; /* Loop over the Interpolation factor. */ i = S->L; while (i > 0U) { /* Set accumulator to zero */ sum0 = 0.0f; /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (i - 1U); /* Loop over the polyPhase length */ tapCnt = phaseLen; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += *ptr1++ * *ptr2; /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Decrement the loop counter */ tapCnt--; } /* The result is in the accumulator, store in the destination buffer. */ *pDst++ = sum0; /* Decrement loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 1; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. ** Now copy the last phaseLen - 1 samples to the start of the state buffer. ** This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCur = S->pState; tapCnt = phaseLen - 1U; /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #endif /* #if !defined(ARM_MATH_CM0_FAMILY) */ } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of FIR_Interpolate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_interpolate_f32.c
C
apache-2.0
28,073
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_interpolate_init_f32.c * Description: Floating-point FIR interpolator initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Interpolate @{ */ /** @brief Initialization function for the floating-point FIR interpolator. @param[in,out] S points to an instance of the floating-point FIR interpolator structure @param[in] L upsample factor @param[in] numTaps number of filter coefficients in the filter @param[in] pCoeffs points to the filter coefficient buffer @param[in] pState points to the state buffer @param[in] blockSize number of input samples to process per call @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_ARGUMENT_ERROR : filter length <code>numTaps</code> is not a multiple of the interpolation factor <code>L</code> @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[numTaps-2], ..., b[1], b[0]} </pre> @par The length of the filter <code>numTaps</code> must be a multiple of the interpolation factor <code>L</code>. @par <code>pState</code> points to the array of state variables. <code>pState</code> is of length <code>(numTaps/L)+blockSize-1</code> words where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_fir_interpolate_f32()</code>. */ arm_status arm_fir_interpolate_init_f32( arm_fir_interpolate_instance_f32 * S, uint8_t L, uint16_t numTaps, const float32_t * pCoeffs, float32_t * pState, uint32_t blockSize) { arm_status status; /* The filter length must be a multiple of the interpolation factor */ if ((numTaps % L) != 0U) { /* Set status as ARM_MATH_LENGTH_ERROR */ status = ARM_MATH_LENGTH_ERROR; } else { /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Assign Interpolation factor */ S->L = L; /* Assign polyPhaseLength */ S->phaseLength = numTaps / L; /* Clear state buffer and size of buffer is always phaseLength + blockSize - 1 */ memset(pState, 0, (blockSize + ((uint32_t) S->phaseLength - 1U)) * sizeof(float32_t)); /* Assign state pointer */ S->pState = pState; status = ARM_MATH_SUCCESS; } return (status); } /** @} end of FIR_Interpolate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_interpolate_init_f32.c
C
apache-2.0
3,570
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_interpolate_init_q15.c * Description: Q15 FIR interpolator initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Interpolate @{ */ /** @brief Initialization function for the Q15 FIR interpolator. @param[in,out] S points to an instance of the Q15 FIR interpolator structure @param[in] L upsample factor @param[in] numTaps number of filter coefficients in the filter @param[in] pCoeffs points to the filter coefficient buffer @param[in] pState points to the state buffer @param[in] blockSize number of input samples to process per call @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_ARGUMENT_ERROR : filter length <code>numTaps</code> is not a multiple of the interpolation factor <code>L</code> @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[numTaps-2], ..., b[1], b[0]} </pre> The length of the filter <code>numTaps</code> must be a multiple of the interpolation factor <code>L</code>. @par <code>pState</code> points to the array of state variables. <code>pState</code> is of length <code>(numTaps/L)+blockSize-1</code> words where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_fir_interpolate_q15()</code>. */ arm_status arm_fir_interpolate_init_q15( arm_fir_interpolate_instance_q15 * S, uint8_t L, uint16_t numTaps, const q15_t * pCoeffs, q15_t * pState, uint32_t blockSize) { arm_status status; /* The filter length must be a multiple of the interpolation factor */ if ((numTaps % L) != 0U) { /* Set status as ARM_MATH_LENGTH_ERROR */ status = ARM_MATH_LENGTH_ERROR; } else { /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Assign Interpolation factor */ S->L = L; /* Assign polyPhaseLength */ S->phaseLength = numTaps / L; /* Clear state buffer and size of buffer is always phaseLength + blockSize - 1 */ memset(pState, 0, (blockSize + ((uint32_t) S->phaseLength - 1U)) * sizeof(q15_t)); /* Assign state pointer */ S->pState = pState; status = ARM_MATH_SUCCESS; } return (status); } /** @} end of FIR_Interpolate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_interpolate_init_q15.c
C
apache-2.0
3,519
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_interpolate_init_q31.c * Description: Q31 FIR interpolator initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Interpolate @{ */ /** @brief Initialization function for the Q31 FIR interpolator. @param[in,out] S points to an instance of the Q31 FIR interpolator structure @param[in] L upsample factor @param[in] numTaps number of filter coefficients in the filter @param[in] pCoeffs points to the filter coefficient buffer @param[in] pState points to the state buffer @param[in] blockSize number of input samples to process per call @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_ARGUMENT_ERROR : filter length <code>numTaps</code> is not a multiple of the interpolation factor <code>L</code> @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[numTaps-2], ..., b[1], b[0]} </pre> The length of the filter <code>numTaps</code> must be a multiple of the interpolation factor <code>L</code>. @par <code>pState</code> points to the array of state variables. <code>pState</code> is of length <code>(numTaps/L)+blockSize-1</code> words where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_fir_interpolate_q31()</code>. */ arm_status arm_fir_interpolate_init_q31( arm_fir_interpolate_instance_q31 * S, uint8_t L, uint16_t numTaps, const q31_t * pCoeffs, q31_t * pState, uint32_t blockSize) { arm_status status; /* The filter length must be a multiple of the interpolation factor */ if ((numTaps % L) != 0U) { /* Set status as ARM_MATH_LENGTH_ERROR */ status = ARM_MATH_LENGTH_ERROR; } else { /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Assign Interpolation factor */ S->L = L; /* Assign polyPhaseLength */ S->phaseLength = numTaps / L; /* Clear state buffer and size of buffer is always phaseLength + blockSize - 1 */ memset(pState, 0, (blockSize + ((uint32_t) S->phaseLength - 1U)) * sizeof(q31_t)); /* Assign state pointer */ S->pState = pState; status = ARM_MATH_SUCCESS; } return (status); } /** @} end of FIR_Interpolate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_interpolate_init_q31.c
C
apache-2.0
3,519
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_interpolate_q15.c * Description: Q15 FIR interpolation * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Interpolate @{ */ /** @brief Processing function for the Q15 FIR interpolator. @param[in] S points to an instance of the Q15 FIR interpolator structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using a 64-bit internal accumulator. Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits. Lastly, the accumulator is saturated to yield a result in 1.15 format. */ void arm_fir_interpolate_q15( const arm_fir_interpolate_instance_q15 * S, const q15_t * pSrc, q15_t * pDst, uint32_t blockSize) { #if (1) //#if !defined(ARM_MATH_CM0_FAMILY) q15_t *pState = S->pState; /* State pointer */ const q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *pStateCur; /* Points to the current sample of the state */ q15_t *ptr1; /* Temporary pointer for state buffer */ const q15_t *ptr2; /* Temporary pointer for coefficient buffer */ q63_t sum0; /* Accumulators */ uint32_t i, blkCnt, tapCnt; /* Loop counters */ uint32_t phaseLen = S->phaseLength; /* Length of each polyphase filter component */ uint32_t j; #if defined (ARM_MATH_LOOPUNROLL) q63_t acc0, acc1, acc2, acc3; q15_t x0, x1, x2, x3; q15_t c0, c1, c2, c3; #endif /* S->pState buffer contains previous frame (phaseLen - 1) samples */ /* pStateCur points to the location where the new input data should be written */ pStateCur = S->pState + (phaseLen - 1U); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Copy new input sample into the state buffer */ *pStateCur++ = *pSrc++; *pStateCur++ = *pSrc++; *pStateCur++ = *pSrc++; *pStateCur++ = *pSrc++; /* Address modifier index of coefficient buffer */ j = 1U; /* Loop over the Interpolation factor. */ i = (S->L); while (i > 0U) { /* Set accumulator to zero */ acc0 = 0; acc1 = 0; acc2 = 0; acc3 = 0; /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (S->L - j); /* Loop over the polyPhase length. Unroll by a factor of 4. Repeat until we've computed numTaps-(4*S->L) coefficients. */ tapCnt = phaseLen >> 2U; x0 = *(ptr1++); x1 = *(ptr1++); x2 = *(ptr1++); while (tapCnt > 0U) { /* Read the input sample */ x3 = *(ptr1++); /* Read the coefficient */ c0 = *(ptr2); /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; acc1 += (q63_t) x1 * c0; acc2 += (q63_t) x2 * c0; acc3 += (q63_t) x3 * c0; /* Read the coefficient */ c1 = *(ptr2 + S->L); /* Read the input sample */ x0 = *(ptr1++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x1 * c1; acc1 += (q63_t) x2 * c1; acc2 += (q63_t) x3 * c1; acc3 += (q63_t) x0 * c1; /* Read the coefficient */ c2 = *(ptr2 + S->L * 2); /* Read the input sample */ x1 = *(ptr1++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x2 * c2; acc1 += (q63_t) x3 * c2; acc2 += (q63_t) x0 * c2; acc3 += (q63_t) x1 * c2; /* Read the coefficient */ c3 = *(ptr2 + S->L * 3); /* Read the input sample */ x2 = *(ptr1++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x3 * c3; acc1 += (q63_t) x0 * c3; acc2 += (q63_t) x1 * c3; acc3 += (q63_t) x2 * c3; /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += 4 * S->L; /* Decrement loop counter */ tapCnt--; } /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */ tapCnt = phaseLen % 0x4U; while (tapCnt > 0U) { /* Read the input sample */ x3 = *(ptr1++); /* Read the coefficient */ c0 = *(ptr2); /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; acc1 += (q63_t) x1 * c0; acc2 += (q63_t) x2 * c0; acc3 += (q63_t) x3 * c0; /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* update states for next sample processing */ x0 = x1; x1 = x2; x2 = x3; /* Decrement loop counter */ tapCnt--; } /* The result is in the accumulator, store in the destination buffer. */ *(pDst ) = (q15_t) (__SSAT((acc0 >> 15), 16)); *(pDst + S->L) = (q15_t) (__SSAT((acc1 >> 15), 16)); *(pDst + 2 * S->L) = (q15_t) (__SSAT((acc2 >> 15), 16)); *(pDst + 3 * S->L) = (q15_t) (__SSAT((acc3 >> 15), 16)); pDst++; /* Increment the address modifier index of coefficient buffer */ j++; /* Decrement loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 4; pDst += S->L * 3; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy new input sample into the state buffer */ *pStateCur++ = *pSrc++; /* Address modifier index of coefficient buffer */ j = 1U; /* Loop over the Interpolation factor. */ i = S->L; while (i > 0U) { /* Set accumulator to zero */ sum0 = 0; /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (S->L - j); /* Loop over the polyPhase length. Repeat until we've computed numTaps-(4*S->L) coefficients. */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ tapCnt = phaseLen >> 2U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += (q63_t) *ptr1++ * *ptr2; /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; sum0 += (q63_t) *ptr1++ * *ptr2; ptr2 += S->L; sum0 += (q63_t) *ptr1++ * *ptr2; ptr2 += S->L; sum0 += (q63_t) *ptr1++ * *ptr2; ptr2 += S->L; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining outputs */ tapCnt = phaseLen % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = phaseLen; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += (q63_t) *ptr1++ * *ptr2; /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Decrement loop counter */ tapCnt--; } /* The result is in the accumulator, store in the destination buffer. */ *pDst++ = (q15_t) (__SSAT((sum0 >> 15), 16)); /* Increment the address modifier index of coefficient buffer */ j++; /* Decrement the loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 1; /* Decrement the loop counter */ blkCnt--; } /* Processing is complete. Now copy the last phaseLen - 1 samples to the satrt of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCur = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ tapCnt = (phaseLen - 1U) >> 2U; /* copy data */ while (tapCnt > 0U) { write_q15x2_ia (&pStateCur, read_q15x2_ia (&pState)); write_q15x2_ia (&pStateCur, read_q15x2_ia (&pState)); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining outputs */ tapCnt = (phaseLen - 1U) % 0x04U; #else /* Initialize tapCnt with number of samples */ tapCnt = (phaseLen - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #else /* alternate version for CM0_FAMILY */ q15_t *pState = S->pState; /* State pointer */ const q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *pStateCur; /* Points to the current sample of the state */ q15_t *ptr1; /* Temporary pointer for state buffer */ const q15_t *ptr2; /* Temporary pointer for coefficient buffer */ q63_t sum0; /* Accumulators */ uint32_t i, blkCnt, tapCnt; /* Loop counters */ uint32_t phaseLen = S->phaseLength; /* Length of each polyphase filter component */ /* S->pState buffer contains previous frame (phaseLen - 1) samples */ /* pStateCur points to the location where the new input data should be written */ pStateCur = S->pState + (phaseLen - 1U); /* Total number of intput samples */ blkCnt = blockSize; /* Loop over the blockSize. */ while (blkCnt > 0U) { /* Copy new input sample into the state buffer */ *pStateCur++ = *pSrc++; /* Loop over the Interpolation factor. */ i = S->L; while (i > 0U) { /* Set accumulator to zero */ sum0 = 0; /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (i - 1U); /* Loop over the polyPhase length */ tapCnt = phaseLen; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += ((q63_t) *ptr1++ * *ptr2); /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Decrement the loop counter */ tapCnt--; } /* Store the result after converting to 1.15 format in the destination buffer. */ *pDst++ = (q15_t) (__SSAT((sum0 >> 15), 16)); /* Decrement loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 1; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. ** Now copy the last phaseLen - 1 samples to the start of the state buffer. ** This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCur = S->pState; tapCnt = phaseLen - 1U; /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #endif /* #if !defined(ARM_MATH_CM0_FAMILY) */ } /** @} end of FIR_Interpolate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_interpolate_q15.c
C
apache-2.0
13,670
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_interpolate_q31.c * Description: Q31 FIR interpolation * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Interpolate @{ */ /** @brief Processing function for the Q31 FIR interpolator. @param[in] S points to an instance of the Q31 FIR interpolator structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. Thus, if the accumulator result overflows it wraps around rather than clip. In order to avoid overflows completely the input signal must be scaled down by <code>1/(numTaps/L)</code>. since <code>numTaps/L</code> additions occur per output sample. After all multiply-accumulates are performed, the 2.62 accumulator is truncated to 1.32 format and then saturated to 1.31 format. */ void arm_fir_interpolate_q31( const arm_fir_interpolate_instance_q31 * S, const q31_t * pSrc, q31_t * pDst, uint32_t blockSize) { #if (1) //#if !defined(ARM_MATH_CM0_FAMILY) q31_t *pState = S->pState; /* State pointer */ const q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q31_t *pStateCur; /* Points to the current sample of the state */ q31_t *ptr1; /* Temporary pointer for state buffer */ const q31_t *ptr2; /* Temporary pointer for coefficient buffer */ q63_t sum0; /* Accumulators */ uint32_t i, blkCnt, tapCnt; /* Loop counters */ uint32_t phaseLen = S->phaseLength; /* Length of each polyphase filter component */ uint32_t j; #if defined (ARM_MATH_LOOPUNROLL) q63_t acc0, acc1, acc2, acc3; q31_t x0, x1, x2, x3; q31_t c0, c1, c2, c3; #endif /* S->pState buffer contains previous frame (phaseLen - 1) samples */ /* pStateCur points to the location where the new input data should be written */ pStateCur = S->pState + (phaseLen - 1U); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Copy new input sample into the state buffer */ *pStateCur++ = *pSrc++; *pStateCur++ = *pSrc++; *pStateCur++ = *pSrc++; *pStateCur++ = *pSrc++; /* Address modifier index of coefficient buffer */ j = 1U; /* Loop over the Interpolation factor. */ i = (S->L); while (i > 0U) { /* Set accumulator to zero */ acc0 = 0; acc1 = 0; acc2 = 0; acc3 = 0; /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (S->L - j); /* Loop over the polyPhase length. Unroll by a factor of 4. Repeat until we've computed numTaps-(4*S->L) coefficients. */ tapCnt = phaseLen >> 2U; x0 = *(ptr1++); x1 = *(ptr1++); x2 = *(ptr1++); while (tapCnt > 0U) { /* Read the input sample */ x3 = *(ptr1++); /* Read the coefficient */ c0 = *(ptr2); /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; acc1 += (q63_t) x1 * c0; acc2 += (q63_t) x2 * c0; acc3 += (q63_t) x3 * c0; /* Read the coefficient */ c1 = *(ptr2 + S->L); /* Read the input sample */ x0 = *(ptr1++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x1 * c1; acc1 += (q63_t) x2 * c1; acc2 += (q63_t) x3 * c1; acc3 += (q63_t) x0 * c1; /* Read the coefficient */ c2 = *(ptr2 + S->L * 2); /* Read the input sample */ x1 = *(ptr1++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x2 * c2; acc1 += (q63_t) x3 * c2; acc2 += (q63_t) x0 * c2; acc3 += (q63_t) x1 * c2; /* Read the coefficient */ c3 = *(ptr2 + S->L * 3); /* Read the input sample */ x2 = *(ptr1++); /* Perform the multiply-accumulate */ acc0 += (q63_t) x3 * c3; acc1 += (q63_t) x0 * c3; acc2 += (q63_t) x1 * c3; acc3 += (q63_t) x2 * c3; /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += 4 * S->L; /* Decrement loop counter */ tapCnt--; } /* If the polyPhase length is not a multiple of 4, compute the remaining filter taps */ tapCnt = phaseLen % 0x4U; while (tapCnt > 0U) { /* Read the input sample */ x3 = *(ptr1++); /* Read the coefficient */ c0 = *(ptr2); /* Perform the multiply-accumulate */ acc0 += (q63_t) x0 * c0; acc1 += (q63_t) x1 * c0; acc2 += (q63_t) x2 * c0; acc3 += (q63_t) x3 * c0; /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* update states for next sample processing */ x0 = x1; x1 = x2; x2 = x3; /* Decrement loop counter */ tapCnt--; } /* The result is in the accumulator, store in the destination buffer. */ *(pDst ) = (q31_t) (acc0 >> 31); *(pDst + S->L) = (q31_t) (acc1 >> 31); *(pDst + 2 * S->L) = (q31_t) (acc2 >> 31); *(pDst + 3 * S->L) = (q31_t) (acc3 >> 31); pDst++; /* Increment the address modifier index of coefficient buffer */ j++; /* Decrement loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 4; pDst += S->L * 3; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy new input sample into the state buffer */ *pStateCur++ = *pSrc++; /* Address modifier index of coefficient buffer */ j = 1U; /* Loop over the Interpolation factor. */ i = S->L; while (i > 0U) { /* Set accumulator to zero */ sum0 = 0; /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (S->L - j); /* Loop over the polyPhase length. Repeat until we've computed numTaps-(4*S->L) coefficients. */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ tapCnt = phaseLen >> 2U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += (q63_t) *ptr1++ * *ptr2; /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; sum0 += (q63_t) *ptr1++ * *ptr2; ptr2 += S->L; sum0 += (q63_t) *ptr1++ * *ptr2; ptr2 += S->L; sum0 += (q63_t) *ptr1++ * *ptr2; ptr2 += S->L; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining outputs */ tapCnt = phaseLen % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = phaseLen; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += (q63_t) *ptr1++ * *ptr2; /* Upsampling is done by stuffing L-1 zeros between each sample. * So instead of multiplying zeros with coefficients, * Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Decrement loop counter */ tapCnt--; } /* The result is in the accumulator, store in the destination buffer. */ *pDst++ = (q31_t) (sum0 >> 31); /* Increment the address modifier index of coefficient buffer */ j++; /* Decrement the loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 1; /* Decrement the loop counter */ blkCnt--; } /* Processing is complete. Now copy the last phaseLen - 1 samples to the satrt of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCur = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ tapCnt = (phaseLen - 1U) >> 2U; /* copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining outputs */ tapCnt = (phaseLen - 1U) % 0x04U; #else /* Initialize tapCnt with number of samples */ tapCnt = (phaseLen - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #else /* alternate version for CM0_FAMILY */ q31_t *pState = S->pState; /* State pointer */ const q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q31_t *pStateCur; /* Points to the current sample of the state */ q31_t *ptr1; /* Temporary pointer for state buffer */ const q31_t *ptr2; /* Temporary pointer for coefficient buffer */ q63_t sum0; /* Accumulators */ uint32_t i, blkCnt, tapCnt; /* Loop counters */ uint32_t phaseLen = S->phaseLength; /* Length of each polyphase filter component */ /* S->pState buffer contains previous frame (phaseLen - 1) samples */ /* pStateCur points to the location where the new input data should be written */ pStateCur = S->pState + (phaseLen - 1U); /* Total number of intput samples */ blkCnt = blockSize; /* Loop over the blockSize. */ while (blkCnt > 0U) { /* Copy new input sample into the state buffer */ *pStateCur++ = *pSrc++; /* Loop over the Interpolation factor. */ i = S->L; while (i > 0U) { /* Set accumulator to zero */ sum0 = 0; /* Initialize state pointer */ ptr1 = pState; /* Initialize coefficient pointer */ ptr2 = pCoeffs + (i - 1U); /* Loop over the polyPhase length */ tapCnt = phaseLen; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum0 += ((q63_t) *ptr1++ * *ptr2); /* Increment the coefficient pointer by interpolation factor times. */ ptr2 += S->L; /* Decrement the loop counter */ tapCnt--; } /* The result is in the accumulator, store in the destination buffer. */ *pDst++ = (q31_t) (sum0 >> 31); /* Decrement loop counter */ i--; } /* Advance the state pointer by 1 * to process the next group of interpolation factor number samples */ pState = pState + 1; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. ** Now copy the last phaseLen - 1 samples to the start of the state buffer. ** This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCur = S->pState; tapCnt = phaseLen - 1U; /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #endif /* #if !defined(ARM_MATH_CM0_FAMILY) */ } /** @} end of FIR_Interpolate group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_interpolate_q31.c
C
apache-2.0
13,596
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_lattice_f32.c * Description: Processing function for floating-point FIR Lattice filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @defgroup FIR_Lattice Finite Impulse Response (FIR) Lattice Filters This set of functions implements Finite Impulse Response (FIR) lattice filters for Q15, Q31 and floating-point data types. Lattice filters are used in a variety of adaptive filter applications. The filter structure is feedforward and the net impulse response is finite length. The functions operate on blocks of input and output data and each call to the function processes <code>blockSize</code> samples through the filter. <code>pSrc</code> and <code>pDst</code> point to input and output arrays containing <code>blockSize</code> values. @par Algorithm \image html FIRLattice.gif "Finite Impulse Response Lattice filter" The following difference equation is implemented: @par <pre> f0[n] = g0[n] = x[n] fm[n] = fm-1[n] + km * gm-1[n-1] for m = 1, 2, ...M gm[n] = km * fm-1[n] + gm-1[n-1] for m = 1, 2, ...M y[n] = fM[n] </pre> @par <code>pCoeffs</code> points to tha array of reflection coefficients of size <code>numStages</code>. Reflection Coefficients are stored in the following order. @par <pre> {k1, k2, ..., kM} </pre> where M is number of stages @par <code>pState</code> points to a state array of size <code>numStages</code>. The state variables (g values) hold previous inputs and are stored in the following order. <pre> {g0[n], g1[n], g2[n] ...gM-1[n]} </pre> The state variables are updated after each block of data is processed; the coefficients are untouched. @par Instance Structure The coefficients and state variables for a filter are stored together in an instance data structure. A separate instance structure must be defined for each filter. Coefficient arrays may be shared among several instances while state variable arrays cannot be shared. There are separate instance structure declarations for each of the 3 supported data types. @par Initialization Functions There is also an associated initialization function for each data type. The initialization function performs the following operations: - Sets the values of the internal structure fields. - Zeros out the values in the state buffer. To do this manually without calling the init function, assign the follow subfields of the instance structure: numStages, pCoeffs, pState. Also set all of the values in pState to zero. @par Use of the initialization function is optional. However, if the initialization function is used, then the instance structure cannot be placed into a const data section. To place an instance structure into a const data section, the instance structure must be manually initialized. Set the values in the state buffer to zeros and then manually initialize the instance structure as follows: <pre> arm_fir_lattice_instance_f32 S = {numStages, pState, pCoeffs}; arm_fir_lattice_instance_q31 S = {numStages, pState, pCoeffs}; arm_fir_lattice_instance_q15 S = {numStages, pState, pCoeffs}; </pre> @par where <code>numStages</code> is the number of stages in the filter; <code>pState</code> is the address of the state buffer; <code>pCoeffs</code> is the address of the coefficient buffer. @par Fixed-Point Behavior Care must be taken when using the fixed-point versions of the FIR Lattice filter functions. In particular, the overflow and saturation behavior of the accumulator used in each function must be considered. Refer to the function specific documentation below for usage guidelines. */ /** @addtogroup FIR_Lattice @{ */ /** @brief Processing function for the floating-point FIR lattice filter. @param[in] S points to an instance of the floating-point FIR lattice structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none */ void arm_fir_lattice_f32( const arm_fir_lattice_instance_f32 * S, const float32_t * pSrc, float32_t * pDst, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ const float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *px; /* Temporary state pointer */ const float32_t *pk; /* Temporary coefficient pointer */ uint32_t numStages = S->numStages; /* Number of stages in the filter */ uint32_t blkCnt, stageCnt; /* Loop counters */ float32_t fcurr0, fnext0, gnext0, gcurr0; /* Temporary variables */ #if defined (ARM_MATH_LOOPUNROLL) float32_t fcurr1, fnext1, gnext1; /* Temporary variables for second sample in loop unrolling */ float32_t fcurr2, fnext2, gnext2; /* Temporary variables for third sample in loop unrolling */ float32_t fcurr3, fnext3, gnext3; /* Temporary variables for fourth sample in loop unrolling */ #endif gcurr0 = 0.0f; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Read two samples from input buffer */ /* f0(n) = x(n) */ fcurr0 = *pSrc++; fcurr1 = *pSrc++; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pk = pCoeffs; /* Read g0(n-1) from state buffer */ gcurr0 = *px; /* Process first sample for first tap */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = (gcurr0 * (*pk)) + fcurr0; /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext0 = (fcurr0 * (*pk)) + gcurr0; /* Process second sample for first tap */ fnext1 = (fcurr0 * (*pk)) + fcurr1; gnext1 = (fcurr1 * (*pk)) + fcurr0; /* Read next two samples from input buffer */ /* f0(n+2) = x(n+2) */ fcurr2 = *pSrc++; fcurr3 = *pSrc++; /* Process third sample for first tap */ fnext2 = (fcurr1 * (*pk)) + fcurr2; gnext2 = (fcurr2 * (*pk)) + fcurr1; /* Process fourth sample for first tap */ fnext3 = (fcurr2 * (*pk )) + fcurr3; gnext3 = (fcurr3 * (*pk++)) + fcurr2; /* Copy only last input sample into the state buffer which will be used for next samples processing */ *px++ = fcurr3; /* Update of f values for next coefficient set processing */ fcurr0 = fnext0; fcurr1 = fnext1; fcurr2 = fnext2; fcurr3 = fnext3; /* Loop unrolling. Process 4 taps at a time . */ stageCnt = (numStages - 1U) >> 2U; /* Loop over the number of taps. Unroll by a factor of 4. Repeat until we've computed numStages-3 coefficients. */ /* Process 2nd, 3rd, 4th and 5th taps ... here */ while (stageCnt > 0U) { /* Read g1(n-1), g3(n-1) .... from state */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = gnext3; /* Process first sample for 2nd, 6th .. tap */ /* Sample processing for K2, K6.... */ /* f2(n) = f1(n) + K2 * g1(n-1) */ fnext0 = (gcurr0 * (*pk)) + fcurr0; /* Process second sample for 2nd, 6th .. tap */ /* for sample 2 processing */ fnext1 = (gnext0 * (*pk)) + fcurr1; /* Process third sample for 2nd, 6th .. tap */ fnext2 = (gnext1 * (*pk)) + fcurr2; /* Process fourth sample for 2nd, 6th .. tap */ fnext3 = (gnext2 * (*pk)) + fcurr3; /* g2(n) = f1(n) * K2 + g1(n-1) */ /* Calculation of state values for next stage */ gnext3 = (fcurr3 * (*pk)) + gnext2; gnext2 = (fcurr2 * (*pk)) + gnext1; gnext1 = (fcurr1 * (*pk)) + gnext0; gnext0 = (fcurr0 * (*pk++)) + gcurr0; /* Read g2(n-1), g4(n-1) .... from state */ gcurr0 = *px; /* save g2(n) in state buffer */ *px++ = gnext3; /* Sample processing for K3, K7.... */ /* Process first sample for 3rd, 7th .. tap */ /* f3(n) = f2(n) + K3 * g2(n-1) */ fcurr0 = (gcurr0 * (*pk)) + fnext0; /* Process second sample for 3rd, 7th .. tap */ fcurr1 = (gnext0 * (*pk)) + fnext1; /* Process third sample for 3rd, 7th .. tap */ fcurr2 = (gnext1 * (*pk)) + fnext2; /* Process fourth sample for 3rd, 7th .. tap */ fcurr3 = (gnext2 * (*pk)) + fnext3; /* Calculation of state values for next stage */ /* g3(n) = f2(n) * K3 + g2(n-1) */ gnext3 = (fnext3 * (*pk)) + gnext2; gnext2 = (fnext2 * (*pk)) + gnext1; gnext1 = (fnext1 * (*pk)) + gnext0; gnext0 = (fnext0 * (*pk++)) + gcurr0; /* Read g1(n-1), g3(n-1) .... from state */ gcurr0 = *px; /* save g3(n) in state buffer */ *px++ = gnext3; /* Sample processing for K4, K8.... */ /* Process first sample for 4th, 8th .. tap */ /* f4(n) = f3(n) + K4 * g3(n-1) */ fnext0 = (gcurr0 * (*pk)) + fcurr0; /* Process second sample for 4th, 8th .. tap */ /* for sample 2 processing */ fnext1 = (gnext0 * (*pk)) + fcurr1; /* Process third sample for 4th, 8th .. tap */ fnext2 = (gnext1 * (*pk)) + fcurr2; /* Process fourth sample for 4th, 8th .. tap */ fnext3 = (gnext2 * (*pk)) + fcurr3; /* g4(n) = f3(n) * K4 + g3(n-1) */ /* Calculation of state values for next stage */ gnext3 = (fcurr3 * (*pk)) + gnext2; gnext2 = (fcurr2 * (*pk)) + gnext1; gnext1 = (fcurr1 * (*pk)) + gnext0; gnext0 = (fcurr0 * (*pk++)) + gcurr0; /* Read g2(n-1), g4(n-1) .... from state */ gcurr0 = *px; /* save g4(n) in state buffer */ *px++ = gnext3; /* Sample processing for K5, K9.... */ /* Process first sample for 5th, 9th .. tap */ /* f5(n) = f4(n) + K5 * g4(n-1) */ fcurr0 = (gcurr0 * (*pk)) + fnext0; /* Process second sample for 5th, 9th .. tap */ fcurr1 = (gnext0 * (*pk)) + fnext1; /* Process third sample for 5th, 9th .. tap */ fcurr2 = (gnext1 * (*pk)) + fnext2; /* Process fourth sample for 5th, 9th .. tap */ fcurr3 = (gnext2 * (*pk)) + fnext3; /* Calculation of state values for next stage */ /* g5(n) = f4(n) * K5 + g4(n-1) */ gnext3 = (fnext3 * (*pk)) + gnext2; gnext2 = (fnext2 * (*pk)) + gnext1; gnext1 = (fnext1 * (*pk)) + gnext0; gnext0 = (fnext0 * (*pk++)) + gcurr0; stageCnt--; } /* If the (filter length -1) is not a multiple of 4, compute the remaining filter taps */ stageCnt = (numStages - 1U) % 0x4U; while (stageCnt > 0U) { gcurr0 = *px; /* save g value in state buffer */ *px++ = gnext3; /* Process four samples for last three taps here */ fnext0 = (gcurr0 * (*pk)) + fcurr0; fnext1 = (gnext0 * (*pk)) + fcurr1; fnext2 = (gnext1 * (*pk)) + fcurr2; fnext3 = (gnext2 * (*pk)) + fcurr3; /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext3 = (fcurr3 * (*pk)) + gnext2; gnext2 = (fcurr2 * (*pk)) + gnext1; gnext1 = (fcurr1 * (*pk)) + gnext0; gnext0 = (fcurr0 * (*pk++)) + gcurr0; /* Update of f values for next coefficient set processing */ fcurr0 = fnext0; fcurr1 = fnext1; fcurr2 = fnext2; fcurr3 = fnext3; stageCnt--; } /* The results in the 4 accumulators, store in the destination buffer. */ /* y(n) = fN(n) */ *pDst++ = fcurr0; *pDst++ = fcurr1; *pDst++ = fcurr2; *pDst++ = fcurr3; blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* f0(n) = x(n) */ fcurr0 = *pSrc++; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pk = pCoeffs; /* read g2(n) from state buffer */ gcurr0 = *px; /* for sample 1 processing */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = (gcurr0 * (*pk)) + fcurr0; /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext0 = (fcurr0 * (*pk++)) + gcurr0; /* save g1(n) in state buffer */ *px++ = fcurr0; /* f1(n) is saved in fcurr0 for next stage processing */ fcurr0 = fnext0; stageCnt = (numStages - 1U); /* stage loop */ while (stageCnt > 0U) { /* read g2(n) from state buffer */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = gnext0; /* Sample processing for K2, K3.... */ /* f2(n) = f1(n) + K2 * g1(n-1) */ fnext0 = (gcurr0 * (*pk)) + fcurr0; /* g2(n) = f1(n) * K2 + g1(n-1) */ gnext0 = (fcurr0 * (*pk++)) + gcurr0; /* f1(n) is saved in fcurr0 for next stage processing */ fcurr0 = fnext0; stageCnt--; } /* y(n) = fN(n) */ *pDst++ = fcurr0; blkCnt--; } } /** @} end of FIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_lattice_f32.c
C
apache-2.0
14,615
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_lattice_init_f32.c * Description: Floating-point FIR Lattice filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Lattice @{ */ /** @brief Initialization function for the floating-point FIR lattice filter. @param[in] S points to an instance of the floating-point FIR lattice structure @param[in] numStages number of filter stages @param[in] pCoeffs points to the coefficient buffer. The array is of length numStages @param[in] pState points to the state buffer. The array is of length numStages @return none */ void arm_fir_lattice_init_f32( arm_fir_lattice_instance_f32 * S, uint16_t numStages, const float32_t * pCoeffs, float32_t * pState) { /* Assign filter taps */ S->numStages = numStages; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer and size is always numStages */ memset(pState, 0, (numStages) * sizeof(float32_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_lattice_init_f32.c
C
apache-2.0
2,095
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_lattice_init_q15.c * Description: Q15 FIR Lattice filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Lattice @{ */ /** @brief Initialization function for the Q15 FIR lattice filter. @param[in] S points to an instance of the Q15 FIR lattice structure @param[in] numStages number of filter stages @param[in] pCoeffs points to the coefficient buffer. The array is of length numStages @param[in] pState points to the state buffer. The array is of length numStages @return none */ void arm_fir_lattice_init_q15( arm_fir_lattice_instance_q15 * S, uint16_t numStages, const q15_t * pCoeffs, q15_t * pState) { /* Assign filter taps */ S->numStages = numStages; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer and size is always numStages */ memset(pState, 0, (numStages) * sizeof(q15_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_lattice_init_q15.c
C
apache-2.0
2,050
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_lattice_init_q31.c * Description: Q31 FIR lattice filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Lattice @{ */ /** @brief Initialization function for the Q31 FIR lattice filter. @param[in] S points to an instance of the Q31 FIR lattice structure @param[in] numStages number of filter stages @param[in] pCoeffs points to the coefficient buffer. The array is of length numStages @param[in] pState points to the state buffer. The array is of length numStages @return none */ void arm_fir_lattice_init_q31( arm_fir_lattice_instance_q31 * S, uint16_t numStages, const q31_t * pCoeffs, q31_t * pState) { /* Assign filter taps */ S->numStages = numStages; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer and size is always numStages */ memset(pState, 0, (numStages) * sizeof(q31_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_lattice_init_q31.c
C
apache-2.0
2,050
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_lattice_q15.c * Description: Q15 FIR lattice filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Lattice @{ */ /** @brief Processing function for Q15 FIR lattice filter. @param[in] S points to an instance of the Q15 FIR lattice structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none */ void arm_fir_lattice_q15( const arm_fir_lattice_instance_q15 * S, const q15_t * pSrc, q15_t * pDst, uint32_t blockSize) { q15_t *pState = S->pState; /* State pointer */ const q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *px; /* Temporary state pointer */ const q15_t *pk; /* Temporary coefficient pointer */ uint32_t numStages = S->numStages; /* Number of stages in the filter */ uint32_t blkCnt, stageCnt; /* Loop counters */ q31_t fcurr0, fnext0, gnext0, gcurr0; /* Temporary variables */ #if (1) //#if !defined(ARM_MATH_CM0_FAMILY) #if defined (ARM_MATH_LOOPUNROLL) q31_t fcurr1, fnext1, gnext1; /* Temporary variables for second sample in loop unrolling */ q31_t fcurr2, fnext2, gnext2; /* Temporary variables for third sample in loop unrolling */ q31_t fcurr3, fnext3, gnext3; /* Temporary variables for fourth sample in loop unrolling */ #endif gcurr0 = 0; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Read two samples from input buffer */ /* f0(n) = x(n) */ fcurr0 = *pSrc++; fcurr1 = *pSrc++; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pk = pCoeffs; /* Read g0(n-1) from state buffer */ gcurr0 = *px; /* Process first sample for first tap */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = (q31_t) ((gcurr0 * (*pk)) >> 15U) + fcurr0; fnext0 = __SSAT(fnext0, 16); /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext0 = (q31_t) ((fcurr0 * (*pk)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); /* Process second sample for first tap */ fnext1 = (q31_t) ((fcurr0 * (*pk)) >> 15U) + fcurr1; fnext1 = __SSAT(fnext1, 16); gnext1 = (q31_t) ((fcurr1 * (*pk)) >> 15U) + fcurr0; gnext1 = __SSAT(gnext1, 16); /* Read next two samples from input buffer */ /* f0(n+2) = x(n+2) */ fcurr2 = *pSrc++; fcurr3 = *pSrc++; /* Process third sample for first tap */ fnext2 = (q31_t) ((fcurr1 * (*pk)) >> 15U) + fcurr2; fnext2 = __SSAT(fnext2, 16); gnext2 = (q31_t) ((fcurr2 * (*pk)) >> 15U) + fcurr1; gnext2 = __SSAT(gnext2, 16); /* Process fourth sample for first tap */ fnext3 = (q31_t) ((fcurr2 * (*pk )) >> 15U) + fcurr3; fnext3 = __SSAT(fnext3, 16); gnext3 = (q31_t) ((fcurr3 * (*pk++)) >> 15U) + fcurr2; gnext3 = __SSAT(gnext3, 16); /* Copy only last input sample into the state buffer which will be used for next samples processing */ *px++ = (q15_t) fcurr3; /* Update of f values for next coefficient set processing */ fcurr0 = fnext0; fcurr1 = fnext1; fcurr2 = fnext2; fcurr3 = fnext3; /* Loop unrolling. Process 4 taps at a time . */ stageCnt = (numStages - 1U) >> 2U; /* Loop over the number of taps. Unroll by a factor of 4. Repeat until we've computed numStages-3 coefficients. */ /* Process 2nd, 3rd, 4th and 5th taps ... here */ while (stageCnt > 0U) { /* Read g1(n-1), g3(n-1) .... from state */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = (q15_t) gnext3; /* Process first sample for 2nd, 6th .. tap */ /* Sample processing for K2, K6.... */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = (q31_t) ((gcurr0 * (*pk)) >> 15U) + fcurr0; fnext0 = __SSAT(fnext0, 16); /* Process second sample for 2nd, 6th .. tap */ /* for sample 2 processing */ fnext1 = (q31_t) ((gnext0 * (*pk)) >> 15U) + fcurr1; fnext1 = __SSAT(fnext1, 16); /* Process third sample for 2nd, 6th .. tap */ fnext2 = (q31_t) ((gnext1 * (*pk)) >> 15U) + fcurr2; fnext2 = __SSAT(fnext2, 16); /* Process fourth sample for 2nd, 6th .. tap */ fnext3 = (q31_t) ((gnext2 * (*pk)) >> 15U) + fcurr3; fnext3 = __SSAT(fnext3, 16); /* g1(n) = f0(n) * K1 + g0(n-1) */ /* Calculation of state values for next stage */ gnext3 = (q31_t) ((fcurr3 * (*pk)) >> 15U) + gnext2; gnext3 = __SSAT(gnext3, 16); gnext2 = (q31_t) ((fcurr2 * (*pk)) >> 15U) + gnext1; gnext2 = __SSAT(gnext2, 16); gnext1 = (q31_t) ((fcurr1 * (*pk)) >> 15U) + gnext0; gnext1 = __SSAT(gnext1, 16); gnext0 = (q31_t) ((fcurr0 * (*pk++)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); /* Read g2(n-1), g4(n-1) .... from state */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = (q15_t) gnext3; /* Sample processing for K3, K7.... */ /* Process first sample for 3rd, 7th .. tap */ /* f3(n) = f2(n) + K3 * g2(n-1) */ fcurr0 = (q31_t) ((gcurr0 * (*pk)) >> 15U) + fnext0; fcurr0 = __SSAT(fcurr0, 16); /* Process second sample for 3rd, 7th .. tap */ fcurr1 = (q31_t) ((gnext0 * (*pk)) >> 15U) + fnext1; fcurr1 = __SSAT(fcurr1, 16); /* Process third sample for 3rd, 7th .. tap */ fcurr2 = (q31_t) ((gnext1 * (*pk)) >> 15U) + fnext2; fcurr2 = __SSAT(fcurr2, 16); /* Process fourth sample for 3rd, 7th .. tap */ fcurr3 = (q31_t) ((gnext2 * (*pk)) >> 15U) + fnext3; fcurr3 = __SSAT(fcurr3, 16); /* Calculation of state values for next stage */ /* g3(n) = f2(n) * K3 + g2(n-1) */ gnext3 = (q31_t) ((fnext3 * (*pk)) >> 15U) + gnext2; gnext3 = __SSAT(gnext3, 16); gnext2 = (q31_t) ((fnext2 * (*pk)) >> 15U) + gnext1; gnext2 = __SSAT(gnext2, 16); gnext1 = (q31_t) ((fnext1 * (*pk)) >> 15U) + gnext0; gnext1 = __SSAT(gnext1, 16); gnext0 = (q31_t) ((fnext0 * (*pk++)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); /* Read g1(n-1), g3(n-1) .... from state */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = (q15_t) gnext3; /* Sample processing for K4, K8.... */ /* Process first sample for 4th, 8th .. tap */ /* f4(n) = f3(n) + K4 * g3(n-1) */ fnext0 = (q31_t) ((gcurr0 * (*pk)) >> 15U) + fcurr0; fnext0 = __SSAT(fnext0, 16); /* Process second sample for 4th, 8th .. tap */ /* for sample 2 processing */ fnext1 = (q31_t) ((gnext0 * (*pk)) >> 15U) + fcurr1; fnext1 = __SSAT(fnext1, 16); /* Process third sample for 4th, 8th .. tap */ fnext2 = (q31_t) ((gnext1 * (*pk)) >> 15U) + fcurr2; fnext2 = __SSAT(fnext2, 16); /* Process fourth sample for 4th, 8th .. tap */ fnext3 = (q31_t) ((gnext2 * (*pk)) >> 15U) + fcurr3; fnext3 = __SSAT(fnext3, 16); /* g4(n) = f3(n) * K4 + g3(n-1) */ /* Calculation of state values for next stage */ gnext3 = (q31_t) ((fcurr3 * (*pk)) >> 15U) + gnext2; gnext3 = __SSAT(gnext3, 16); gnext2 = (q31_t) ((fcurr2 * (*pk)) >> 15U) + gnext1; gnext2 = __SSAT(gnext2, 16); gnext1 = (q31_t) ((fcurr1 * (*pk)) >> 15U) + gnext0; gnext1 = __SSAT(gnext1, 16); gnext0 = (q31_t) ((fcurr0 * (*pk++)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); /* Read g2(n-1), g4(n-1) .... from state */ gcurr0 = *px; /* save g4(n) in state buffer */ *px++ = (q15_t) gnext3; /* Sample processing for K5, K9.... */ /* Process first sample for 5th, 9th .. tap */ /* f5(n) = f4(n) + K5 * g4(n-1) */ fcurr0 = (q31_t) ((gcurr0 * (*pk)) >> 15U) + fnext0; fcurr0 = __SSAT(fcurr0, 16); /* Process second sample for 5th, 9th .. tap */ fcurr1 = (q31_t) ((gnext0 * (*pk)) >> 15U) + fnext1; fcurr1 = __SSAT(fcurr1, 16); /* Process third sample for 5th, 9th .. tap */ fcurr2 = (q31_t) ((gnext1 * (*pk)) >> 15U) + fnext2; fcurr2 = __SSAT(fcurr2, 16); /* Process fourth sample for 5th, 9th .. tap */ fcurr3 = (q31_t) ((gnext2 * (*pk)) >> 15U) + fnext3; fcurr3 = __SSAT(fcurr3, 16); /* Calculation of state values for next stage */ /* g5(n) = f4(n) * K5 + g4(n-1) */ gnext3 = (q31_t) ((fnext3 * (*pk)) >> 15U) + gnext2; gnext3 = __SSAT(gnext3, 16); gnext2 = (q31_t) ((fnext2 * (*pk)) >> 15U) + gnext1; gnext2 = __SSAT(gnext2, 16); gnext1 = (q31_t) ((fnext1 * (*pk)) >> 15U) + gnext0; gnext1 = __SSAT(gnext1, 16); gnext0 = (q31_t) ((fnext0 * (*pk++)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); stageCnt--; } /* If the (filter length -1) is not a multiple of 4, compute the remaining filter taps */ stageCnt = (numStages - 1U) % 0x4U; while (stageCnt > 0U) { gcurr0 = *px; /* save g value in state buffer */ *px++ = (q15_t) gnext3; /* Process four samples for last three taps here */ fnext0 = (q31_t) ((gcurr0 * (*pk)) >> 15U) + fcurr0; fnext0 = __SSAT(fnext0, 16); fnext1 = (q31_t) ((gnext0 * (*pk)) >> 15U) + fcurr1; fnext1 = __SSAT(fnext1, 16); fnext2 = (q31_t) ((gnext1 * (*pk)) >> 15U) + fcurr2; fnext2 = __SSAT(fnext2, 16); fnext3 = (q31_t) ((gnext2 * (*pk)) >> 15U) + fcurr3; fnext3 = __SSAT(fnext3, 16); /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext3 = (q31_t) ((fcurr3 * (*pk)) >> 15U) + gnext2; gnext3 = __SSAT(gnext3, 16); gnext2 = (q31_t) ((fcurr2 * (*pk)) >> 15U) + gnext1; gnext2 = __SSAT(gnext2, 16); gnext1 = (q31_t) ((fcurr1 * (*pk)) >> 15U) + gnext0; gnext1 = __SSAT(gnext1, 16); gnext0 = (q31_t) ((fcurr0 * (*pk++)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); /* Update of f values for next coefficient set processing */ fcurr0 = fnext0; fcurr1 = fnext1; fcurr2 = fnext2; fcurr3 = fnext3; stageCnt--; } /* The results in the 4 accumulators, store in the destination buffer. */ /* y(n) = fN(n) */ #ifndef ARM_MATH_BIG_ENDIAN write_q15x2_ia (&pDst, __PKHBT(fcurr0, fcurr1, 16)); write_q15x2_ia (&pDst, __PKHBT(fcurr2, fcurr3, 16)); #else write_q15x2_ia (&pDst, __PKHBT(fcurr1, fcurr0, 16)); write_q15x2_ia (&pDst, __PKHBT(fcurr3, fcurr2, 16)); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* f0(n) = x(n) */ fcurr0 = *pSrc++; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pk = pCoeffs; /* read g2(n) from state buffer */ gcurr0 = *px; /* for sample 1 processing */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = (((q31_t) gcurr0 * (*pk)) >> 15U) + fcurr0; fnext0 = __SSAT(fnext0, 16); /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext0 = (((q31_t) fcurr0 * (*pk++)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); /* save g1(n) in state buffer */ *px++ = (q15_t) fcurr0; /* f1(n) is saved in fcurr0 for next stage processing */ fcurr0 = fnext0; stageCnt = (numStages - 1U); /* stage loop */ while (stageCnt > 0U) { /* read g2(n) from state buffer */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = (q15_t) gnext0; /* Sample processing for K2, K3.... */ /* f2(n) = f1(n) + K2 * g1(n-1) */ fnext0 = (((q31_t) gcurr0 * (*pk)) >> 15U) + fcurr0; fnext0 = __SSAT(fnext0, 16); /* g2(n) = f1(n) * K2 + g1(n-1) */ gnext0 = (((q31_t) fcurr0 * (*pk++)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); /* f1(n) is saved in fcurr0 for next stage processing */ fcurr0 = fnext0; stageCnt--; } /* y(n) = fN(n) */ *pDst++ = __SSAT(fcurr0, 16); blkCnt--; } #else /* alternate version for CM0_FAMILY */ blkCnt = blockSize; while (blkCnt > 0U) { /* f0(n) = x(n) */ fcurr0 = *pSrc++; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pk = pCoeffs; /* read g0(n-1) from state buffer */ gcurr0 = *px; /* for sample 1 processing */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = ((gcurr0 * (*pk)) >> 15U) + fcurr0; fnext0 = __SSAT(fnext, 16); /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext0 = ((fcurr0 * (*pk++)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); /* save f0(n) in state buffer */ *px++ = (q15_t) fcurr0; /* f1(n) is saved in fcurr for next stage processing */ fcurr0 = fnext0; stageCnt = (numStages - 1U); /* stage loop */ while (stageCnt > 0U) { /* read g1(n-1) from state buffer */ gcurr0 = *px; /* save g0(n-1) in state buffer */ *px++ = (q15_t) gnext0; /* Sample processing for K2, K3.... */ /* f2(n) = f1(n) + K2 * g1(n-1) */ fnext0 = ((gcurr0 * (*pk)) >> 15U) + fcurr0; fnext0 = __SSAT(fnext0, 16); /* g2(n) = f1(n) * K2 + g1(n-1) */ gnext0 = ((fcurr0 * (*pk++)) >> 15U) + gcurr0; gnext0 = __SSAT(gnext0, 16); /* f1(n) is saved in fcurr0 for next stage processing */ fcurr0 = fnext0; stageCnt--; } /* y(n) = fN(n) */ *pDst++ = __SSAT(fcurr0, 16); blkCnt--; } #endif /* #if !defined(ARM_MATH_CM0_FAMILY) */ } /** @} end of FIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_lattice_q15.c
C
apache-2.0
15,012
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_lattice_q31.c * Description: Q31 FIR lattice filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Lattice @{ */ /** @brief Processing function for the Q31 FIR lattice filter. @param[in] S points to an instance of the Q31 FIR lattice structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior In order to avoid overflows the input signal must be scaled down by 2*log2(numStages) bits. */ void arm_fir_lattice_q31( const arm_fir_lattice_instance_q31 * S, const q31_t * pSrc, q31_t * pDst, uint32_t blockSize) { q31_t *pState = S->pState; /* State pointer */ const q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q31_t *px; /* Temporary state pointer */ const q31_t *pk; /* Temporary coefficient pointer */ uint32_t numStages = S->numStages; /* Number of stages in the filter */ uint32_t blkCnt, stageCnt; /* Loop counters */ q31_t fcurr0, fnext0, gnext0, gcurr0; /* Temporary variables */ #if (1) //#if !defined(ARM_MATH_CM0_FAMILY) #if defined (ARM_MATH_LOOPUNROLL) q31_t fcurr1, fnext1, gnext1; /* Temporary variables for second sample in loop unrolling */ q31_t fcurr2, fnext2, gnext2; /* Temporary variables for third sample in loop unrolling */ q31_t fcurr3, fnext3, gnext3; /* Temporary variables for fourth sample in loop unrolling */ #endif gcurr0 = 0; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Read two samples from input buffer */ /* f0(n) = x(n) */ fcurr0 = *pSrc++; fcurr1 = *pSrc++; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pk = pCoeffs; /* Read g0(n-1) from state buffer */ gcurr0 = *px; /* Process first sample for first tap */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fnext0 = (fnext0 << 1U) + fcurr0; /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext0 = (q31_t) (((q63_t) fcurr0 * (*pk)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; /* Process second sample for first tap */ fnext1 = (q31_t) (((q63_t) fcurr0 * (*pk)) >> 32U); fnext1 = (fnext1 << 1U) + fcurr1; gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk)) >> 32U); gnext1 = (gnext1 << 1U) + fcurr0; /* Read next two samples from input buffer */ /* f0(n+2) = x(n+2) */ fcurr2 = *pSrc++; fcurr3 = *pSrc++; /* Process third sample for first tap */ fnext2 = (q31_t) (((q63_t) fcurr1 * (*pk)) >> 32U); fnext2 = (fnext2 << 1U) + fcurr2; gnext2 = (q31_t) (((q63_t) fcurr2 * (*pk)) >> 32U); gnext2 = (gnext2 << 1U) + fcurr1; /* Process fourth sample for first tap */ fnext3 = (q31_t) (((q63_t) fcurr2 * (*pk )) >> 32U); fnext3 = (fnext3 << 1U) + fcurr3; gnext3 = (q31_t) (((q63_t) fcurr3 * (*pk++)) >> 32U); gnext3 = (gnext3 << 1U) + fcurr2; /* Copy only last input sample into the state buffer which will be used for next samples processing */ *px++ = fcurr3; /* Update of f values for next coefficient set processing */ fcurr0 = fnext0; fcurr1 = fnext1; fcurr2 = fnext2; fcurr3 = fnext3; /* Loop unrolling. Process 4 taps at a time . */ stageCnt = (numStages - 1U) >> 2U; /* Loop over the number of taps. Unroll by a factor of 4. Repeat until we've computed numStages-3 coefficients. */ /* Process 2nd, 3rd, 4th and 5th taps ... here */ while (stageCnt > 0U) { /* Read g1(n-1), g3(n-1) .... from state */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = gnext3; /* Process first sample for 2nd, 6th .. tap */ /* Sample processing for K2, K6.... */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fnext0 = (fnext0 << 1U) + fcurr0; /* Process second sample for 2nd, 6th .. tap */ /* for sample 2 processing */ fnext1 = (q31_t) (((q63_t) gnext0 * (*pk)) >> 32U); fnext1 = (fnext1 << 1U) + fcurr1; /* Process third sample for 2nd, 6th .. tap */ fnext2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 32U); fnext2 = (fnext2 << 1U) + fcurr2; /* Process fourth sample for 2nd, 6th .. tap */ fnext3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 32U); fnext3 = (fnext3 << 1U) + fcurr3; /* g1(n) = f0(n) * K1 + g0(n-1) */ /* Calculation of state values for next stage */ gnext3 = (q31_t) (((q63_t) fcurr3 * (*pk)) >> 32U); gnext3 = (gnext3 << 1U) + gnext2; gnext2 = (q31_t) (((q63_t) fcurr2 * (*pk)) >> 32U); gnext2 = (gnext2 << 1U) + gnext1; gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk)) >> 32U); gnext1 = (gnext1 << 1U) + gnext0; gnext0 = (q31_t) (((q63_t) fcurr0 * (*pk++)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; /* Read g2(n-1), g4(n-1) .... from state */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = gnext3; /* Sample processing for K3, K7.... */ /* Process first sample for 3rd, 7th .. tap */ /* f3(n) = f2(n) + K3 * g2(n-1) */ fcurr0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fcurr0 = (fcurr0 << 1U) + fnext0; /* Process second sample for 3rd, 7th .. tap */ fcurr1 = (q31_t) (((q63_t) gnext0 * (*pk)) >> 32U); fcurr1 = (fcurr1 << 1U) + fnext1; /* Process third sample for 3rd, 7th .. tap */ fcurr2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 32U); fcurr2 = (fcurr2 << 1U) + fnext2; /* Process fourth sample for 3rd, 7th .. tap */ fcurr3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 32U); fcurr3 = (fcurr3 << 1U) + fnext3; /* Calculation of state values for next stage */ /* g3(n) = f2(n) * K3 + g2(n-1) */ gnext3 = (q31_t) (((q63_t) fnext3 * (*pk)) >> 32U); gnext3 = (gnext3 << 1U) + gnext2; gnext2 = (q31_t) (((q63_t) fnext2 * (*pk)) >> 32U); gnext2 = (gnext2 << 1U) + gnext1; gnext1 = (q31_t) (((q63_t) fnext1 * (*pk)) >> 32U); gnext1 = (gnext1 << 1U) + gnext0; gnext0 = (q31_t) (((q63_t) fnext0 * (*pk++)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; /* Read g1(n-1), g3(n-1) .... from state */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = gnext3; /* Sample processing for K4, K8.... */ /* Process first sample for 4th, 8th .. tap */ /* f4(n) = f3(n) + K4 * g3(n-1) */ fnext0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fnext0 = (fnext0 << 1U) + fcurr0; /* Process second sample for 4th, 8th .. tap */ /* for sample 2 processing */ fnext1 = (q31_t) (((q63_t) gnext0 * (*pk)) >> 32U); fnext1 = (fnext1 << 1U) + fcurr1; /* Process third sample for 4th, 8th .. tap */ fnext2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 32U); fnext2 = (fnext2 << 1U) + fcurr2; /* Process fourth sample for 4th, 8th .. tap */ fnext3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 32U); fnext3 = (fnext3 << 1U) + fcurr3; /* g4(n) = f3(n) * K4 + g3(n-1) */ /* Calculation of state values for next stage */ gnext3 = (q31_t) (((q63_t) fcurr3 * (*pk)) >> 32U); gnext3 = (gnext3 << 1U) + gnext2; gnext2 = (q31_t) (((q63_t) fcurr2 * (*pk)) >> 32U); gnext2 = (gnext2 << 1U) + gnext1; gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk)) >> 32U); gnext1 = (gnext1 << 1U) + gnext0; gnext0 = (q31_t) (((q63_t) fcurr0 * (*pk++)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; /* Read g2(n-1), g4(n-1) .... from state */ gcurr0 = *px; /* save g4(n) in state buffer */ *px++ = gnext3; /* Sample processing for K5, K9.... */ /* Process first sample for 5th, 9th .. tap */ /* f5(n) = f4(n) + K5 * g4(n-1) */ fcurr0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fcurr0 = (fcurr0 << 1U) + fnext0; /* Process second sample for 5th, 9th .. tap */ fcurr1 = (q31_t) (((q63_t) gnext0 * (*pk)) >> 32U); fcurr1 = (fcurr1 << 1U) + fnext1; /* Process third sample for 5th, 9th .. tap */ fcurr2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 32U); fcurr2 = (fcurr2 << 1U) + fnext2; /* Process fourth sample for 5th, 9th .. tap */ fcurr3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 32U); fcurr3 = (fcurr3 << 1U) + fnext3; /* Calculation of state values for next stage */ /* g5(n) = f4(n) * K5 + g4(n-1) */ gnext3 = (q31_t) (((q63_t) fnext3 * (*pk)) >> 32U); gnext3 = (gnext3 << 1U) + gnext2; gnext2 = (q31_t) (((q63_t) fnext2 * (*pk)) >> 32U); gnext2 = (gnext2 << 1U) + gnext1; gnext1 = (q31_t) (((q63_t) fnext1 * (*pk)) >> 32U); gnext1 = (gnext1 << 1U) + gnext0; gnext0 = (q31_t) (((q63_t) fnext0 * (*pk++)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; stageCnt--; } /* If the (filter length -1) is not a multiple of 4, compute the remaining filter taps */ stageCnt = (numStages - 1U) % 0x4U; while (stageCnt > 0U) { gcurr0 = *px; /* save g value in state buffer */ *px++ = gnext3; /* Process four samples for last three taps here */ fnext0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fnext0 = (fnext0 << 1U) + fcurr0; fnext1 = (q31_t) (((q63_t) gnext0 * (*pk)) >> 32U); fnext1 = (fnext1 << 1U) + fcurr1; fnext2 = (q31_t) (((q63_t) gnext1 * (*pk)) >> 32U); fnext2 = (fnext2 << 1U) + fcurr2; fnext3 = (q31_t) (((q63_t) gnext2 * (*pk)) >> 32U); fnext3 = (fnext3 << 1U) + fcurr3; /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext3 = (q31_t) (((q63_t) fcurr3 * (*pk)) >> 32U); gnext3 = (gnext3 << 1U) + gnext2; gnext2 = (q31_t) (((q63_t) fcurr2 * (*pk)) >> 32U); gnext2 = (gnext2 << 1U) + gnext1; gnext1 = (q31_t) (((q63_t) fcurr1 * (*pk)) >> 32U); gnext1 = (gnext1 << 1U) + gnext0; gnext0 = (q31_t) (((q63_t) fcurr0 * (*pk++)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; /* Update of f values for next coefficient set processing */ fcurr0 = fnext0; fcurr1 = fnext1; fcurr2 = fnext2; fcurr3 = fnext3; stageCnt--; } /* The results in the 4 accumulators, store in the destination buffer. */ /* y(n) = fN(n) */ *pDst++ = fcurr0; *pDst++ = fcurr1; *pDst++ = fcurr2; *pDst++ = fcurr3; blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* f0(n) = x(n) */ fcurr0 = *pSrc++; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pk = pCoeffs; /* read g2(n) from state buffer */ gcurr0 = *px; /* for sample 1 processing */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fnext0 = (fnext0 << 1U) + fcurr0; /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext0 = (q31_t) (((q63_t) fcurr0 * (*pk++)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; /* save g1(n) in state buffer */ *px++ = fcurr0; /* f1(n) is saved in fcurr0 for next stage processing */ fcurr0 = fnext0; stageCnt = (numStages - 1U); /* stage loop */ while (stageCnt > 0U) { /* read g2(n) from state buffer */ gcurr0 = *px; /* save g1(n) in state buffer */ *px++ = gnext0; /* Sample processing for K2, K3.... */ /* f2(n) = f1(n) + K2 * g1(n-1) */ fnext0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fnext0 = (fnext0 << 1U) + fcurr0; /* g2(n) = f1(n) * K2 + g1(n-1) */ gnext0 = (q31_t) (((q63_t) fcurr0 * (*pk++)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; /* f1(n) is saved in fcurr0 for next stage processing */ fcurr0 = fnext0; stageCnt--; } /* y(n) = fN(n) */ *pDst++ = fcurr0; blkCnt--; } #else /* alternate version for CM0_FAMILY */ blkCnt = blockSize; while (blkCnt > 0U) { /* f0(n) = x(n) */ fcurr0 = *pSrc++; /* Initialize state pointer */ px = pState; /* Initialize coeff pointer */ pk = pCoeffs; /* read g0(n-1) from state buffer */ gcurr0 = *px; /* for sample 1 processing */ /* f1(n) = f0(n) + K1 * g0(n-1) */ fnext0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fnext0 = (fnext << 1U) + fcurr0; /* g1(n) = f0(n) * K1 + g0(n-1) */ gnext0 = (q31_t) (((q63_t) fcurr0 * (*pk++)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; /* save f0(n) in state buffer */ *px++ = fcurr0; /* f1(n) is saved in fcurr for next stage processing */ fcurr0 = fnext0; stageCnt = (numStages - 1U); /* stage loop */ while (stageCnt > 0U) { /* read g1(n-1) from state buffer */ gcurr0 = *px; /* save g0(n-1) in state buffer */ *px++ = gnext0; /* Sample processing for K2, K3.... */ /* f2(n) = f1(n) + K2 * g1(n-1) */ fnext0 = (q31_t) (((q63_t) gcurr0 * (*pk)) >> 32U); fnext0 = (fnext0 << 1U) + fcurr0; /* g2(n) = f1(n) * K2 + g1(n-1) */ gnext0 = (q31_t) (((q63_t) fcurr0 * (*pk++)) >> 32U); gnext0 = (gnext0 << 1U) + gcurr0; /* f1(n) is saved in fcurr0 for next stage processing */ fcurr0 = fnext0; stageCnt--; } /* y(n) = fN(n) */ *pDst++ = fcurr0; blkCnt--; } #endif /* #if !defined(ARM_MATH_CM0_FAMILY) */ } /** @} end of FIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_lattice_q31.c
C
apache-2.0
15,127
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_q15.c * Description: Q15 FIR filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR @{ */ /** @brief Processing function for the Q15 FIR filter. @param[in] S points to an instance of the Q15 FIR filter structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using a 64-bit internal accumulator. Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits. Lastly, the accumulator is saturated to yield a result in 1.15 format. @remark Refer to \ref arm_fir_fast_q15() for a faster but less precise implementation of this function. */ void arm_fir_q15( const arm_fir_instance_q15 * S, const q15_t * pSrc, q15_t * pDst, uint32_t blockSize) { q15_t *pState = S->pState; /* State pointer */ const q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *pStateCurnt; /* Points to the current sample of the state */ q15_t *px; /* Temporary pointer for state buffer */ const q15_t *pb; /* Temporary pointer for coefficient buffer */ q63_t acc0; /* Accumulators */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ #if defined (ARM_MATH_LOOPUNROLL) q63_t acc1, acc2, acc3; /* Accumulators */ q31_t x0, x1, x2, c0; /* Temporary variables to hold state and coefficient values */ #endif /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 output values simultaneously. * The variables acc0 ... acc3 hold output values that are being computed: * * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1] * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2] * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3] */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Copy 4 new input samples into the state buffer. */ *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; /* Set all accumulators to zero */ acc0 = 0; acc1 = 0; acc2 = 0; acc3 = 0; /* Typecast q15_t pointer to q31_t pointer for state reading in q31_t */ px = pState; /* Typecast q15_t pointer to q31_t pointer for coefficient reading in q31_t */ pb = pCoeffs; /* Read the first two samples from the state buffer: x[n-N], x[n-N-1] */ x0 = read_q15x2_ia (&px); /* Read the third and forth samples from the state buffer: x[n-N-2], x[n-N-3] */ x2 = read_q15x2_ia (&px); /* Loop over the number of taps. Unroll by a factor of 4. Repeat until we've computed numTaps-(numTaps%4) coefficients. */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Read the first two coefficients using SIMD: b[N] and b[N-1] coefficients */ c0 = read_q15x2_ia ((q15_t **) &pb); /* acc0 += b[N] * x[n-N] + b[N-1] * x[n-N-1] */ acc0 = __SMLALD(x0, c0, acc0); /* acc2 += b[N] * x[n-N-2] + b[N-1] * x[n-N-3] */ acc2 = __SMLALD(x2, c0, acc2); /* pack x[n-N-1] and x[n-N-2] */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x2, x0, 0); #else x1 = __PKHBT(x0, x2, 0); #endif /* Read state x[n-N-4], x[n-N-5] */ x0 = read_q15x2_ia (&px); /* acc1 += b[N] * x[n-N-1] + b[N-1] * x[n-N-2] */ acc1 = __SMLALDX(x1, c0, acc1); /* pack x[n-N-3] and x[n-N-4] */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x0, x2, 0); #else x1 = __PKHBT(x2, x0, 0); #endif /* acc3 += b[N] * x[n-N-3] + b[N-1] * x[n-N-4] */ acc3 = __SMLALDX(x1, c0, acc3); /* Read coefficients b[N-2], b[N-3] */ c0 = read_q15x2_ia ((q15_t **) &pb); /* acc0 += b[N-2] * x[n-N-2] + b[N-3] * x[n-N-3] */ acc0 = __SMLALD(x2, c0, acc0); /* Read state x[n-N-6], x[n-N-7] with offset */ x2 = read_q15x2_ia (&px); /* acc2 += b[N-2] * x[n-N-4] + b[N-3] * x[n-N-5] */ acc2 = __SMLALD(x0, c0, acc2); /* acc1 += b[N-2] * x[n-N-3] + b[N-3] * x[n-N-4] */ acc1 = __SMLALDX(x1, c0, acc1); /* pack x[n-N-5] and x[n-N-6] */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x2, x0, 0); #else x1 = __PKHBT(x0, x2, 0); #endif /* acc3 += b[N-2] * x[n-N-5] + b[N-3] * x[n-N-6] */ acc3 = __SMLALDX(x1, c0, acc3); /* Decrement tap count */ tapCnt--; } /* If the filter length is not a multiple of 4, compute the remaining filter taps. This is always be 2 taps since the filter length is even. */ if ((numTaps & 0x3U) != 0U) { /* Read last two coefficients */ c0 = read_q15x2_ia ((q15_t **) &pb); /* Perform the multiply-accumulates */ acc0 = __SMLALD(x0, c0, acc0); acc2 = __SMLALD(x2, c0, acc2); /* pack state variables */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x2, x0, 0); #else x1 = __PKHBT(x0, x2, 0); #endif /* Read last state variables */ x0 = read_q15x2 (px); /* Perform the multiply-accumulates */ acc1 = __SMLALDX(x1, c0, acc1); /* pack state variables */ #ifndef ARM_MATH_BIG_ENDIAN x1 = __PKHBT(x0, x2, 0); #else x1 = __PKHBT(x2, x0, 0); #endif /* Perform the multiply-accumulates */ acc3 = __SMLALDX(x1, c0, acc3); } /* The results in the 4 accumulators are in 2.30 format. Convert to 1.15 with saturation. Then store the 4 outputs in the destination buffer. */ #ifndef ARM_MATH_BIG_ENDIAN write_q15x2_ia (&pDst, __PKHBT(__SSAT((acc0 >> 15), 16), __SSAT((acc1 >> 15), 16), 16)); write_q15x2_ia (&pDst, __PKHBT(__SSAT((acc2 >> 15), 16), __SSAT((acc3 >> 15), 16), 16)); #else write_q15x2_ia (&pDst, __PKHBT(__SSAT((acc1 >> 15), 16), __SSAT((acc0 >> 15), 16), 16)); write_q15x2_ia (&pDst, __PKHBT(__SSAT((acc3 >> 15), 16), __SSAT((acc2 >> 15), 16), 16)); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Advance the state pointer by 4 to process the next group of 4 samples */ pState = pState + 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining output samples */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of taps */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy two samples into state buffer */ *pStateCurnt++ = *pSrc++; /* Set the accumulator to zero */ acc0 = 0; /* Use SIMD to hold states and coefficients */ px = pState; pb = pCoeffs; tapCnt = numTaps >> 1U; do { acc0 += (q31_t) *px++ * *pb++; acc0 += (q31_t) *px++ * *pb++; tapCnt--; } while (tapCnt > 0U); /* The result is in 2.30 format. Convert to 1.15 with saturation. Then store the output in the destination buffer. */ *pDst++ = (q15_t) (__SSAT((acc0 >> 15), 16)); /* Advance state pointer by 1 for the next sample */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCurnt = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = (numTaps - 1U) >> 2U; /* Copy data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Calculate remaining number of copies */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy remaining data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } /** @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_q15.c
C
apache-2.0
10,497
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_q31.c * Description: Q31 FIR filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR @{ */ /** @brief Processing function for Q31 FIR filter. @param[in] S points to an instance of the Q31 FIR filter structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. Thus, if the accumulator result overflows it wraps around rather than clip. In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits. After all multiply-accumulates are performed, the 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result. @remark Refer to \ref arm_fir_fast_q31() for a faster but less precise implementation of this filter. */ void arm_fir_q31( const arm_fir_instance_q31 * S, const q31_t * pSrc, q31_t * pDst, uint32_t blockSize) { q31_t *pState = S->pState; /* State pointer */ const q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q31_t *pStateCurnt; /* Points to the current sample of the state */ q31_t *px; /* Temporary pointer for state buffer */ const q31_t *pb; /* Temporary pointer for coefficient buffer */ q63_t acc0; /* Accumulator */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t i, tapCnt, blkCnt; /* Loop counters */ #if defined (ARM_MATH_LOOPUNROLL) q63_t acc1, acc2; /* Accumulators */ q31_t x0, x1, x2; /* Temporary variables to hold state values */ q31_t c0; /* Temporary variable to hold coefficient value */ #endif /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 output values simultaneously. * The variables acc0 ... acc3 hold output values that are being computed: * * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1] * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2] * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3] */ blkCnt = blockSize / 3; while (blkCnt > 0U) { /* Copy 3 new input samples into the state buffer. */ *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; /* Set all accumulators to zero */ acc0 = 0; acc1 = 0; acc2 = 0; /* Initialize state pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Read the first 2 samples from the state buffer: x[n-numTaps], x[n-numTaps-1] */ x0 = *px++; x1 = *px++; /* Loop unrolling: process 3 taps at a time. */ tapCnt = numTaps / 3; while (tapCnt > 0U) { /* Read the b[numTaps] coefficient */ c0 = *pb; /* Read x[n-numTaps-2] sample */ x2 = *(px++); /* Perform the multiply-accumulates */ acc0 += ((q63_t) x0 * c0); acc1 += ((q63_t) x1 * c0); acc2 += ((q63_t) x2 * c0); /* Read the coefficient and state */ c0 = *(pb + 1U); x0 = *(px++); /* Perform the multiply-accumulates */ acc0 += ((q63_t) x1 * c0); acc1 += ((q63_t) x2 * c0); acc2 += ((q63_t) x0 * c0); /* Read the coefficient and state */ c0 = *(pb + 2U); x1 = *(px++); /* update coefficient pointer */ pb += 3U; /* Perform the multiply-accumulates */ acc0 += ((q63_t) x2 * c0); acc1 += ((q63_t) x0 * c0); acc2 += ((q63_t) x1 * c0); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining outputs */ tapCnt = numTaps % 0x3U; while (tapCnt > 0U) { /* Read coefficients */ c0 = *(pb++); /* Fetch 1 state variable */ x2 = *(px++); /* Perform the multiply-accumulates */ acc0 += ((q63_t) x0 * c0); acc1 += ((q63_t) x1 * c0); acc2 += ((q63_t) x2 * c0); /* Reuse the present sample states for next sample */ x0 = x1; x1 = x2; /* Decrement loop counter */ tapCnt--; } /* Advance the state pointer by 3 to process the next group of 3 samples */ pState = pState + 3; /* The result is in 2.30 format. Convert to 1.31 and store in destination buffer. */ *pDst++ = (q31_t) (acc0 >> 31U); *pDst++ = (q31_t) (acc1 >> 31U); *pDst++ = (q31_t) (acc2 >> 31U); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining output samples */ blkCnt = blockSize % 0x3U; #else /* Initialize blkCnt with number of taps */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy one sample at a time into state buffer */ *pStateCurnt++ = *pSrc++; /* Set the accumulator to zero */ acc0 = 0; /* Initialize state pointer */ px = pState; /* Initialize Coefficient pointer */ pb = pCoeffs; i = numTaps; /* Perform the multiply-accumulates */ do { /* acc = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] */ acc0 += (q63_t) *px++ * *pb++; i--; } while (i > 0U); /* Result is in 2.62 format. Convert to 1.31 and store in destination buffer. */ *pDst++ = (q31_t) (acc0 >> 31U); /* Advance state pointer by 1 for the next sample */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCurnt = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = (numTaps - 1U) >> 2U; /* Copy data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Calculate remaining number of copies */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy remaining data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } /** @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_q31.c
C
apache-2.0
8,702
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_q7.c * Description: Q7 FIR filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR @{ */ /** @brief Processing function for Q7 FIR filter. @param[in] S points to an instance of the Q7 FIR filter structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using a 32-bit internal accumulator. Both coefficients and state variables are represented in 1.7 format and multiplications yield a 2.14 result. The 2.14 intermediate results are accumulated in a 32-bit accumulator in 18.14 format. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. The accumulator is converted to 18.7 format by discarding the low 7 bits. Finally, the result is truncated to 1.7 format. */ void arm_fir_q7( const arm_fir_instance_q7 * S, const q7_t * pSrc, q7_t * pDst, uint32_t blockSize) { q7_t *pState = S->pState; /* State pointer */ const q7_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q7_t *pStateCurnt; /* Points to the current sample of the state */ q7_t *px; /* Temporary pointer for state buffer */ const q7_t *pb; /* Temporary pointer for coefficient buffer */ q31_t acc0; /* Accumulators */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t i, tapCnt, blkCnt; /* Loop counters */ #if defined (ARM_MATH_LOOPUNROLL) q31_t acc1, acc2, acc3; /* Accumulators */ q7_t x0, x1, x2, x3, c0; /* Temporary variables to hold state */ #endif /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 output values simultaneously. * The variables acc0 ... acc3 hold output values that are being computed: * * acc0 = b[numTaps-1] * x[n-numTaps-1] + b[numTaps-2] * x[n-numTaps-2] + b[numTaps-3] * x[n-numTaps-3] +...+ b[0] * x[0] * acc1 = b[numTaps-1] * x[n-numTaps] + b[numTaps-2] * x[n-numTaps-1] + b[numTaps-3] * x[n-numTaps-2] +...+ b[0] * x[1] * acc2 = b[numTaps-1] * x[n-numTaps+1] + b[numTaps-2] * x[n-numTaps] + b[numTaps-3] * x[n-numTaps-1] +...+ b[0] * x[2] * acc3 = b[numTaps-1] * x[n-numTaps+2] + b[numTaps-2] * x[n-numTaps+1] + b[numTaps-3] * x[n-numTaps] +...+ b[0] * x[3] */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Copy 4 new input samples into the state buffer. */ *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; *pStateCurnt++ = *pSrc++; /* Set all accumulators to zero */ acc0 = 0; acc1 = 0; acc2 = 0; acc3 = 0; /* Initialize state pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Read the first 3 samples from the state buffer: * x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2] */ x0 = *px++; x1 = *px++; x2 = *px++; /* Loop unrolling. Process 4 taps at a time. */ tapCnt = numTaps >> 2U; /* Loop over the number of taps. Unroll by a factor of 4. Repeat until we've computed numTaps-4 coefficients. */ while (tapCnt > 0U) { /* Read the b[numTaps] coefficient */ c0 = *pb; /* Read x[n-numTaps-3] sample */ x3 = *px; /* acc0 += b[numTaps] * x[n-numTaps] */ acc0 += ((q15_t) x0 * c0); /* acc1 += b[numTaps] * x[n-numTaps-1] */ acc1 += ((q15_t) x1 * c0); /* acc2 += b[numTaps] * x[n-numTaps-2] */ acc2 += ((q15_t) x2 * c0); /* acc3 += b[numTaps] * x[n-numTaps-3] */ acc3 += ((q15_t) x3 * c0); /* Read the b[numTaps-1] coefficient */ c0 = *(pb + 1U); /* Read x[n-numTaps-4] sample */ x0 = *(px + 1U); /* Perform the multiply-accumulates */ acc0 += ((q15_t) x1 * c0); acc1 += ((q15_t) x2 * c0); acc2 += ((q15_t) x3 * c0); acc3 += ((q15_t) x0 * c0); /* Read the b[numTaps-2] coefficient */ c0 = *(pb + 2U); /* Read x[n-numTaps-5] sample */ x1 = *(px + 2U); /* Perform the multiply-accumulates */ acc0 += ((q15_t) x2 * c0); acc1 += ((q15_t) x3 * c0); acc2 += ((q15_t) x0 * c0); acc3 += ((q15_t) x1 * c0); /* Read the b[numTaps-3] coefficients */ c0 = *(pb + 3U); /* Read x[n-numTaps-6] sample */ x2 = *(px + 3U); /* Perform the multiply-accumulates */ acc0 += ((q15_t) x3 * c0); acc1 += ((q15_t) x0 * c0); acc2 += ((q15_t) x1 * c0); acc3 += ((q15_t) x2 * c0); /* update coefficient pointer */ pb += 4U; px += 4U; /* Decrement loop counter */ tapCnt--; } /* If the filter length is not a multiple of 4, compute the remaining filter taps */ tapCnt = numTaps % 0x4U; while (tapCnt > 0U) { /* Read coefficients */ c0 = *(pb++); /* Fetch 1 state variable */ x3 = *(px++); /* Perform the multiply-accumulates */ acc0 += ((q15_t) x0 * c0); acc1 += ((q15_t) x1 * c0); acc2 += ((q15_t) x2 * c0); acc3 += ((q15_t) x3 * c0); /* Reuse the present sample states for next sample */ x0 = x1; x1 = x2; x2 = x3; /* Decrement loop counter */ tapCnt--; } /* The results in the 4 accumulators are in 2.62 format. Convert to 1.31 Then store the 4 outputs in the destination buffer. */ acc0 = __SSAT((acc0 >> 7U), 8); *pDst++ = acc0; acc1 = __SSAT((acc1 >> 7U), 8); *pDst++ = acc1; acc2 = __SSAT((acc2 >> 7U), 8); *pDst++ = acc2; acc3 = __SSAT((acc3 >> 7U), 8); *pDst++ = acc3; /* Advance the state pointer by 4 to process the next group of 4 samples */ pState = pState + 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining output samples */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of taps */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Copy one sample at a time into state buffer */ *pStateCurnt++ = *pSrc++; /* Set the accumulator to zero */ acc0 = 0; /* Initialize state pointer */ px = pState; /* Initialize Coefficient pointer */ pb = pCoeffs; i = numTaps; /* Perform the multiply-accumulates */ do { acc0 += (q15_t) * (px++) * (*(pb++)); i--; } while (i > 0U); /* The result is in 2.14 format. Convert to 1.7 Then store the output in the destination buffer. */ *pDst++ = __SSAT((acc0 >> 7U), 8); /* Advance state pointer by 1 for the next sample */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the state buffer */ pStateCurnt = S->pState; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time */ tapCnt = (numTaps - 1U) >> 2U; /* Copy data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Calculate remaining number of copies */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of taps */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ /* Copy remaining data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement the loop counter */ tapCnt--; } } /** @} end of FIR group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_q7.c
C
apache-2.0
9,486
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_sparse_f32.c * Description: Floating-point sparse FIR filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @defgroup FIR_Sparse Finite Impulse Response (FIR) Sparse Filters This group of functions implements sparse FIR filters. Sparse FIR filters are equivalent to standard FIR filters except that most of the coefficients are equal to zero. Sparse filters are used for simulating reflections in communications and audio applications. There are separate functions for Q7, Q15, Q31, and floating-point data types. The functions operate on blocks of input and output data and each call to the function processes <code>blockSize</code> samples through the filter. <code>pSrc</code> and <code>pDst</code> points to input and output arrays respectively containing <code>blockSize</code> values. @par Algorithm The sparse filter instant structure contains an array of tap indices <code>pTapDelay</code> which specifies the locations of the non-zero coefficients. This is in addition to the coefficient array <code>b</code>. The implementation essentially skips the multiplications by zero and leads to an efficient realization. <pre> y[n] = b[0] * x[n-pTapDelay[0]] + b[1] * x[n-pTapDelay[1]] + b[2] * x[n-pTapDelay[2]] + ...+ b[numTaps-1] * x[n-pTapDelay[numTaps-1]] </pre> @par \image html FIRSparse.gif "Sparse FIR filter. b[n] represents the filter coefficients" @par <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>; <code>pTapDelay</code> points to an array of nonzero indices and is also of size <code>numTaps</code>; <code>pState</code> points to a state array of size <code>maxDelay + blockSize</code>, where <code>maxDelay</code> is the largest offset value that is ever used in the <code>pTapDelay</code> array. Some of the processing functions also require temporary working buffers. @par Instance Structure The coefficients and state variables for a filter are stored together in an instance data structure. A separate instance structure must be defined for each filter. Coefficient and offset arrays may be shared among several instances while state variable arrays cannot be shared. There are separate instance structure declarations for each of the 4 supported data types. @par Initialization Functions There is also an associated initialization function for each data type. The initialization function performs the following operations: - Sets the values of the internal structure fields. - Zeros out the values in the state buffer. To do this manually without calling the init function, assign the follow subfields of the instance structure: numTaps, pCoeffs, pTapDelay, maxDelay, stateIndex, pState. Also set all of the values in pState to zero. @par Use of the initialization function is optional. However, if the initialization function is used, then the instance structure cannot be placed into a const data section. To place an instance structure into a const data section, the instance structure must be manually initialized. Set the values in the state buffer to zeros before static initialization. The code below statically initializes each of the 4 different data type filter instance structures <pre> arm_fir_sparse_instance_f32 S = {numTaps, 0, pState, pCoeffs, maxDelay, pTapDelay}; arm_fir_sparse_instance_q31 S = {numTaps, 0, pState, pCoeffs, maxDelay, pTapDelay}; arm_fir_sparse_instance_q15 S = {numTaps, 0, pState, pCoeffs, maxDelay, pTapDelay}; arm_fir_sparse_instance_q7 S = {numTaps, 0, pState, pCoeffs, maxDelay, pTapDelay}; </pre> @par Fixed-Point Behavior Care must be taken when using the fixed-point versions of the sparse FIR filter functions. In particular, the overflow and saturation behavior of the accumulator used in each function must be considered. Refer to the function specific documentation below for usage guidelines. */ /** @addtogroup FIR_Sparse @{ */ /** @brief Processing function for the floating-point sparse FIR filter. @param[in] S points to an instance of the floating-point sparse FIR structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] pScratchIn points to a temporary buffer of size blockSize @param[in] blockSize number of input samples to process @return none */ void arm_fir_sparse_f32( arm_fir_sparse_instance_f32 * S, const float32_t * pSrc, float32_t * pDst, float32_t * pScratchIn, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ const float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *px; /* Scratch buffer pointer */ float32_t *py = pState; /* Temporary pointers for state buffer */ float32_t *pb = pScratchIn; /* Temporary pointers for scratch buffer */ float32_t *pOut; /* Destination pointer */ int32_t *pTapDelay = S->pTapDelay; /* Pointer to the array containing offset of the non-zero tap values. */ uint32_t delaySize = S->maxDelay + blockSize; /* state length */ uint16_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ int32_t readIndex; /* Read index of the state buffer */ uint32_t tapCnt, blkCnt; /* loop counters */ float32_t coeff = *pCoeffs++; /* Read the first coefficient value */ /* BlockSize of Input samples are copied into the state buffer */ /* StateIndex points to the starting position to write in the state buffer */ arm_circularWrite_f32((int32_t *) py, delaySize, &S->stateIndex, 1, (int32_t *) pSrc, 1, blockSize); /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_f32((int32_t *) py, delaySize, &readIndex, 1, (int32_t *) pb, (int32_t *) pb, blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pOut = pDst; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiplications and store in destination buffer */ *pOut++ = *px++ * coeff; *pOut++ = *px++ * coeff; *pOut++ = *px++ * coeff; *pOut++ = *px++ * coeff; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiplication and store in destination buffer */ *pOut++ = *px++ * coeff; /* Decrement loop counter */ blkCnt--; } /* Load the coefficient value and * increment the coefficient buffer for the next set of state values */ coeff = *pCoeffs++; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Loop over the number of taps. */ tapCnt = (uint32_t) numTaps - 2U; while (tapCnt > 0U) { /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_f32((int32_t *) py, delaySize, &readIndex, 1, (int32_t *) pb, (int32_t *) pb, blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pOut = pDst; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ *pOut++ += *px++ * coeff; *pOut++ += *px++ * coeff; *pOut++ += *px++ * coeff; *pOut++ += *px++ * coeff; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ *pOut++ += *px++ * coeff; /* Decrement loop counter */ blkCnt--; } /* Load the coefficient value and * increment the coefficient buffer for the next set of state values */ coeff = *pCoeffs++; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Decrement tap loop counter */ tapCnt--; } /* Compute last tap without the final read of pTapDelay */ /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_f32((int32_t *) py, delaySize, &readIndex, 1, (int32_t *) pb, (int32_t *) pb, blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pOut = pDst; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ *pOut++ += *px++ * coeff; *pOut++ += *px++ * coeff; *pOut++ += *px++ * coeff; *pOut++ += *px++ * coeff; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ *pOut++ += *px++ * coeff; /* Decrement loop counter */ blkCnt--; } } /** @} end of FIR_Sparse group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_sparse_f32.c
C
apache-2.0
12,285
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_sparse_init_f32.c * Description: Floating-point sparse FIR filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Sparse @{ */ /** @brief Initialization function for the floating-point sparse FIR filter. @param[in,out] S points to an instance of the floating-point sparse FIR structure @param[in] numTaps number of nonzero coefficients in the filter @param[in] pCoeffs points to the array of filter coefficients @param[in] pState points to the state buffer @param[in] pTapDelay points to the array of offset times @param[in] maxDelay maximum offset time supported @param[in] blockSize number of samples that will be processed per block @return none @par Details <code>pCoeffs</code> holds the filter coefficients and has length <code>numTaps</code>. <code>pState</code> holds the filter's state variables and must be of length <code>maxDelay + blockSize</code>, where <code>maxDelay</code> is the maximum number of delay line values. <code>blockSize</code> is the number of samples processed by the <code>arm_fir_sparse_f32()</code> function. */ void arm_fir_sparse_init_f32( arm_fir_sparse_instance_f32 * S, uint16_t numTaps, const float32_t * pCoeffs, float32_t * pState, int32_t * pTapDelay, uint16_t maxDelay, uint32_t blockSize) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Assign TapDelay pointer */ S->pTapDelay = pTapDelay; /* Assign MaxDelay */ S->maxDelay = maxDelay; /* reset the stateIndex to 0 */ S->stateIndex = 0U; /* Clear state buffer and size is always maxDelay + blockSize */ memset(pState, 0, (maxDelay + blockSize) * sizeof(float32_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR_Sparse group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_sparse_init_f32.c
C
apache-2.0
3,040
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_sparse_init_q15.c * Description: Q15 sparse FIR filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Sparse @{ */ /** @brief Initialization function for the Q15 sparse FIR filter. @param[in,out] S points to an instance of the Q15 sparse FIR structure @param[in] numTaps number of nonzero coefficients in the filter @param[in] pCoeffs points to the array of filter coefficients @param[in] pState points to the state buffer @param[in] pTapDelay points to the array of offset times @param[in] maxDelay maximum offset time supported @param[in] blockSize number of samples that will be processed per block @return none @par Details <code>pCoeffs</code> holds the filter coefficients and has length <code>numTaps</code>. <code>pState</code> holds the filter's state variables and must be of length <code>maxDelay + blockSize</code>, where <code>maxDelay</code> is the maximum number of delay line values. <code>blockSize</code> is the number of words processed by <code>arm_fir_sparse_q15()</code> function. */ void arm_fir_sparse_init_q15( arm_fir_sparse_instance_q15 * S, uint16_t numTaps, const q15_t * pCoeffs, q15_t * pState, int32_t * pTapDelay, uint16_t maxDelay, uint32_t blockSize) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Assign TapDelay pointer */ S->pTapDelay = pTapDelay; /* Assign MaxDelay */ S->maxDelay = maxDelay; /* reset the stateIndex to 0 */ S->stateIndex = 0U; /* Clear state buffer and size is always maxDelay + blockSize */ memset(pState, 0, (maxDelay + blockSize) * sizeof(q15_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR_Sparse group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_sparse_init_q15.c
C
apache-2.0
2,988
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_sparse_init_q31.c * Description: Q31 sparse FIR filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Sparse @{ */ /** @brief Initialization function for the Q31 sparse FIR filter. @param[in,out] S points to an instance of the Q31 sparse FIR structure @param[in] numTaps number of nonzero coefficients in the filter @param[in] pCoeffs points to the array of filter coefficients @param[in] pState points to the state buffer @param[in] pTapDelay points to the array of offset times @param[in] maxDelay maximum offset time supported @param[in] blockSize number of samples that will be processed per block @return none @par Details <code>pCoeffs</code> holds the filter coefficients and has length <code>numTaps</code>. <code>pState</code> holds the filter's state variables and must be of length <code>maxDelay + blockSize</code>, where <code>maxDelay</code> is the maximum number of delay line values. <code>blockSize</code> is the number of words processed by <code>arm_fir_sparse_q31()</code> function. */ void arm_fir_sparse_init_q31( arm_fir_sparse_instance_q31 * S, uint16_t numTaps, const q31_t * pCoeffs, q31_t * pState, int32_t * pTapDelay, uint16_t maxDelay, uint32_t blockSize) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Assign TapDelay pointer */ S->pTapDelay = pTapDelay; /* Assign MaxDelay */ S->maxDelay = maxDelay; /* reset the stateIndex to 0 */ S->stateIndex = 0U; /* Clear state buffer and size is always maxDelay + blockSize */ memset(pState, 0, (maxDelay + blockSize) * sizeof(q31_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR_Sparse group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_sparse_init_q31.c
C
apache-2.0
2,969
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_sparse_init_q7.c * Description: Q7 sparse FIR filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Sparse @{ */ /** @brief Initialization function for the Q7 sparse FIR filter. @param[in,out] S points to an instance of the Q7 sparse FIR structure @param[in] numTaps number of nonzero coefficients in the filter @param[in] pCoeffs points to the array of filter coefficients @param[in] pState points to the state buffer @param[in] pTapDelay points to the array of offset times @param[in] maxDelay maximum offset time supported @param[in] blockSize number of samples that will be processed per block @return none @par Details <code>pCoeffs</code> holds the filter coefficients and has length <code>numTaps</code>. <code>pState</code> holds the filter's state variables and must be of length <code>maxDelay + blockSize</code>, where <code>maxDelay</code> is the maximum number of delay line values. <code>blockSize</code> is the number of samples processed by the <code>arm_fir_sparse_q7()</code> function. */ void arm_fir_sparse_init_q7( arm_fir_sparse_instance_q7 * S, uint16_t numTaps, const q7_t * pCoeffs, q7_t * pState, int32_t * pTapDelay, uint16_t maxDelay, uint32_t blockSize) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Assign TapDelay pointer */ S->pTapDelay = pTapDelay; /* Assign MaxDelay */ S->maxDelay = maxDelay; /* reset the stateIndex to 0 */ S->stateIndex = 0U; /* Clear state buffer and size is always maxDelay + blockSize */ memset(pState, 0, (maxDelay + blockSize) * sizeof(q7_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of FIR_Sparse group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_sparse_init_q7.c
C
apache-2.0
2,984
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_sparse_q15.c * Description: Q15 sparse FIR filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Sparse @{ */ /** @brief Processing function for the Q15 sparse FIR filter. @param[in] S points to an instance of the Q15 sparse FIR structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] pScratchIn points to a temporary buffer of size blockSize @param[in] pScratchOut points to a temporary buffer of size blockSize @param[in] blockSize number of input samples to process per call @return none @par Scaling and Overflow Behavior The function is implemented using an internal 32-bit accumulator. The 1.15 x 1.15 multiplications yield a 2.30 result and these are added to a 2.30 accumulator. Thus the full precision of the multiplications is maintained but there is only a single guard bit in the accumulator. If the accumulator result overflows it will wrap around rather than saturate. After all multiply-accumulates are performed, the 2.30 accumulator is truncated to 2.15 format and then saturated to 1.15 format. In order to avoid overflows the input signal or coefficients must be scaled down by log2(numTaps) bits. */ void arm_fir_sparse_q15( arm_fir_sparse_instance_q15 * S, const q15_t * pSrc, q15_t * pDst, q15_t * pScratchIn, q31_t * pScratchOut, uint32_t blockSize) { q15_t *pState = S->pState; /* State pointer */ const q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *px; /* Temporary pointers for scratch buffer */ q15_t *py = pState; /* Temporary pointers for state buffer */ q15_t *pb = pScratchIn; /* Temporary pointers for scratch buffer */ q15_t *pOut = pDst; /* Working pointer for output */ int32_t *pTapDelay = S->pTapDelay; /* Pointer to the array containing offset of the non-zero tap values. */ uint32_t delaySize = S->maxDelay + blockSize; /* state length */ uint16_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ int32_t readIndex; /* Read index of the state buffer */ uint32_t tapCnt, blkCnt; /* loop counters */ q31_t *pScr2 = pScratchOut; /* Working pointer for scratch buffer of output values */ q15_t coeff = *pCoeffs++; /* Read the first coefficient value */ #if defined (ARM_MATH_LOOPUNROLL) q31_t in1, in2; /* Temporary variables */ #endif /* BlockSize of Input samples are copied into the state buffer */ /* StateIndex points to the starting position to write in the state buffer */ arm_circularWrite_q15(py, (int32_t) delaySize, &S->stateIndex, 1,pSrc, 1, blockSize); /* Loop over the number of taps. */ tapCnt = numTaps; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_q15(py, (int32_t) delaySize, &readIndex, 1, pb, pb, (int32_t) blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pScratchOut = pScr2; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform multiplication and store in the scratch buffer */ *pScratchOut++ = ((q31_t) *px++ * coeff); *pScratchOut++ = ((q31_t) *px++ * coeff); *pScratchOut++ = ((q31_t) *px++ * coeff); *pScratchOut++ = ((q31_t) *px++ * coeff); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiplication and store in the scratch buffer */ *pScratchOut++ = ((q31_t) *px++ * coeff); /* Decrement loop counter */ blkCnt--; } /* Load the coefficient value and * increment the coefficient buffer for the next set of state values */ coeff = *pCoeffs++; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Loop over the number of taps. */ tapCnt = (uint32_t) numTaps - 2U; while (tapCnt > 0U) { /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_q15(py, (int32_t) delaySize, &readIndex, 1, pb, pb, (int32_t) blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pScratchOut = pScr2; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ *pScratchOut++ += (q31_t) *px++ * coeff; *pScratchOut++ += (q31_t) *px++ * coeff; *pScratchOut++ += (q31_t) *px++ * coeff; *pScratchOut++ += (q31_t) *px++ * coeff; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ *pScratchOut++ += (q31_t) *px++ * coeff; /* Decrement loop counter */ blkCnt--; } /* Load the coefficient value and * increment the coefficient buffer for the next set of state values */ coeff = *pCoeffs++; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Decrement loop counter */ tapCnt--; } /* Compute last tap without the final read of pTapDelay */ /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_q15(py, (int32_t) delaySize, &readIndex, 1, pb, pb, (int32_t) blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pScratchOut = pScr2; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ *pScratchOut++ += (q31_t) *px++ * coeff; *pScratchOut++ += (q31_t) *px++ * coeff; *pScratchOut++ += (q31_t) *px++ * coeff; *pScratchOut++ += (q31_t) *px++ * coeff; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ *pScratchOut++ += (q31_t) *px++ * coeff; /* Decrement loop counter */ blkCnt--; } /* All the output values are in pScratchOut buffer. Convert them into 1.15 format, saturate and store in the destination buffer. */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { in1 = *pScr2++; in2 = *pScr2++; #ifndef ARM_MATH_BIG_ENDIAN write_q15x2_ia (&pOut, __PKHBT((q15_t) __SSAT(in1 >> 15, 16), (q15_t) __SSAT(in2 >> 15, 16), 16)); #else write_q15x2_ia (&pOut, __PKHBT((q15_t) __SSAT(in2 >> 15, 16), (q15_t) __SSAT(in1 >> 15, 16), 16)); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ in1 = *pScr2++; in2 = *pScr2++; #ifndef ARM_MATH_BIG_ENDIAN write_q15x2_ia (&pOut, __PKHBT((q15_t) __SSAT(in1 >> 15, 16), (q15_t) __SSAT(in2 >> 15, 16), 16)); #else write_q15x2_ia (&pOut, __PKHBT((q15_t) __SSAT(in2 >> 15, 16), (q15_t) __SSAT(in1 >> 15, 16), 16)); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { *pOut++ = (q15_t) __SSAT(*pScr2++ >> 15, 16); /* Decrement loop counter */ blkCnt--; } } /** @} end of FIR_Sparse group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_sparse_q15.c
C
apache-2.0
10,592
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_sparse_q31.c * Description: Q31 sparse FIR filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Sparse @{ */ /** @brief Processing function for the Q31 sparse FIR filter. @param[in] S points to an instance of the Q31 sparse FIR structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] pScratchIn points to a temporary buffer of size blockSize @param[in] blockSize number of input samples to process @return none @par Scaling and Overflow Behavior The function is implemented using an internal 32-bit accumulator. The 1.31 x 1.31 multiplications are truncated to 2.30 format. This leads to loss of precision on the intermediate multiplications and provides only a single guard bit. If the accumulator result overflows, it wraps around rather than saturate. In order to avoid overflows the input signal or coefficients must be scaled down by log2(numTaps) bits. */ void arm_fir_sparse_q31( arm_fir_sparse_instance_q31 * S, const q31_t * pSrc, q31_t * pDst, q31_t * pScratchIn, uint32_t blockSize) { q31_t *pState = S->pState; /* State pointer */ const q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q31_t *px; /* Scratch buffer pointer */ q31_t *py = pState; /* Temporary pointers for state buffer */ q31_t *pb = pScratchIn; /* Temporary pointers for scratch buffer */ q31_t *pOut; /* Destination pointer */ int32_t *pTapDelay = S->pTapDelay; /* Pointer to the array containing offset of the non-zero tap values. */ uint32_t delaySize = S->maxDelay + blockSize; /* state length */ uint16_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ int32_t readIndex; /* Read index of the state buffer */ uint32_t tapCnt, blkCnt; /* loop counters */ q31_t coeff = *pCoeffs++; /* Read the first coefficient value */ q31_t in; q63_t out; /* Temporary output variable */ /* BlockSize of Input samples are copied into the state buffer */ /* StateIndex points to the starting position to write in the state buffer */ arm_circularWrite_f32((int32_t *) py, delaySize, &S->stateIndex, 1, (int32_t *) pSrc, 1, blockSize); /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_f32((int32_t *) py, delaySize, &readIndex, 1, (int32_t *) pb, (int32_t *) pb, blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pOut = pDst; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiplications and store in destination buffer */ *pOut++ = (q31_t) (((q63_t) *px++ * coeff) >> 32); *pOut++ = (q31_t) (((q63_t) *px++ * coeff) >> 32); *pOut++ = (q31_t) (((q63_t) *px++ * coeff) >> 32); *pOut++ = (q31_t) (((q63_t) *px++ * coeff) >> 32); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiplication and store in destination buffer */ *pOut++ = (q31_t) (((q63_t) *px++ * coeff) >> 32); /* Decrement loop counter */ blkCnt--; } /* Load the coefficient value and * increment the coefficient buffer for the next set of state values */ coeff = *pCoeffs++; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Loop over the number of taps. */ tapCnt = (uint32_t) numTaps - 2U; while (tapCnt > 0U) { /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_f32((int32_t *) py, delaySize, &readIndex, 1, (int32_t *) pb, (int32_t *) pb, blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pOut = pDst; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ out = *pOut; out += ((q63_t) *px++ * coeff) >> 32; *pOut++ = (q31_t) (out); out = *pOut; out += ((q63_t) *px++ * coeff) >> 32; *pOut++ = (q31_t) (out); out = *pOut; out += ((q63_t) *px++ * coeff) >> 32; *pOut++ = (q31_t) (out); out = *pOut; out += ((q63_t) *px++ * coeff) >> 32; *pOut++ = (q31_t) (out); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ out = *pOut; out += ((q63_t) *px++ * coeff) >> 32; *pOut++ = (q31_t) (out); /* Decrement loop counter */ blkCnt--; } /* Load the coefficient value and * increment the coefficient buffer for the next set of state values */ coeff = *pCoeffs++; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Decrement tap loop counter */ tapCnt--; } /* Compute last tap without the final read of pTapDelay */ /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_f32((int32_t *) py, delaySize, &readIndex, 1, (int32_t *) pb, (int32_t *) pb, blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pOut = pDst; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ out = *pOut; out += ((q63_t) * px++ * coeff) >> 32; *pOut++ = (q31_t) (out); out = *pOut; out += ((q63_t) * px++ * coeff) >> 32; *pOut++ = (q31_t) (out); out = *pOut; out += ((q63_t) * px++ * coeff) >> 32; *pOut++ = (q31_t) (out); out = *pOut; out += ((q63_t) * px++ * coeff) >> 32; *pOut++ = (q31_t) (out); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ out = *pOut; out += ((q63_t) *px++ * coeff) >> 32; *pOut++ = (q31_t) (out); /* Decrement loop counter */ blkCnt--; } /* Working output pointer is updated */ pOut = pDst; /* Output is converted into 1.31 format. */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { in = *pOut << 1; *pOut++ = in; in = *pOut << 1; *pOut++ = in; in = *pOut << 1; *pOut++ = in; in = *pOut << 1; *pOut++ = in; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { in = *pOut << 1; *pOut++ = in; /* Decrement loop counter */ blkCnt--; } } /** @} end of FIR_Sparse group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_sparse_q31.c
C
apache-2.0
10,111
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_fir_sparse_q7.c * Description: Q7 sparse FIR filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup FIR_Sparse @{ */ /** @brief Processing function for the Q7 sparse FIR filter. @param[in] S points to an instance of the Q7 sparse FIR structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] pScratchIn points to a temporary buffer of size blockSize @param[in] pScratchOut points to a temporary buffer of size blockSize @param[in] blockSize number of input samples to process @return none @par Scaling and Overflow Behavior The function is implemented using a 32-bit internal accumulator. Both coefficients and state variables are represented in 1.7 format and multiplications yield a 2.14 result. The 2.14 intermediate results are accumulated in a 32-bit accumulator in 18.14 format. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. The accumulator is then converted to 18.7 format by discarding the low 7 bits. Finally, the result is truncated to 1.7 format. */ void arm_fir_sparse_q7( arm_fir_sparse_instance_q7 * S, const q7_t * pSrc, q7_t * pDst, q7_t * pScratchIn, q31_t * pScratchOut, uint32_t blockSize) { q7_t *pState = S->pState; /* State pointer */ const q7_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q7_t *px; /* Scratch buffer pointer */ q7_t *py = pState; /* Temporary pointers for state buffer */ q7_t *pb = pScratchIn; /* Temporary pointers for scratch buffer */ q7_t *pOut = pDst; /* Destination pointer */ int32_t *pTapDelay = S->pTapDelay; /* Pointer to the array containing offset of the non-zero tap values. */ uint32_t delaySize = S->maxDelay + blockSize; /* state length */ uint16_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ int32_t readIndex; /* Read index of the state buffer */ uint32_t tapCnt, blkCnt; /* loop counters */ q31_t *pScr2 = pScratchOut; /* Working pointer for scratch buffer of output values */ q31_t in; q7_t coeff = *pCoeffs++; /* Read the coefficient value */ #if defined (ARM_MATH_LOOPUNROLL) q7_t in1, in2, in3, in4; #endif /* BlockSize of Input samples are copied into the state buffer */ /* StateIndex points to the starting position to write in the state buffer */ arm_circularWrite_q7(py, (int32_t) delaySize, &S->stateIndex, 1, pSrc, 1, blockSize); /* Loop over the number of taps. */ tapCnt = numTaps; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_q7(py, (int32_t) delaySize, &readIndex, 1, pb, pb, (int32_t) blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pScratchOut = pScr2; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform multiplication and store in the scratch buffer */ *pScratchOut++ = ((q31_t) *px++ * coeff); *pScratchOut++ = ((q31_t) *px++ * coeff); *pScratchOut++ = ((q31_t) *px++ * coeff); *pScratchOut++ = ((q31_t) *px++ * coeff); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiplication and store in the scratch buffer */ *pScratchOut++ = ((q31_t) *px++ * coeff); /* Decrement loop counter */ blkCnt--; } /* Load the coefficient value and * increment the coefficient buffer for the next set of state values */ coeff = *pCoeffs++; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Loop over the number of taps. */ tapCnt = (uint32_t) numTaps - 2U; while (tapCnt > 0U) { /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_q7(py, (int32_t) delaySize, &readIndex, 1, pb, pb, (int32_t) blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pScratchOut = pScr2; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ in = *pScratchOut + ((q31_t) * px++ * coeff); *pScratchOut++ = in; in = *pScratchOut + ((q31_t) * px++ * coeff); *pScratchOut++ = in; in = *pScratchOut + ((q31_t) * px++ * coeff); *pScratchOut++ = in; in = *pScratchOut + ((q31_t) * px++ * coeff); *pScratchOut++ = in; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ in = *pScratchOut + ((q31_t) *px++ * coeff); *pScratchOut++ = in; /* Decrement loop counter */ blkCnt--; } /* Load the coefficient value and * increment the coefficient buffer for the next set of state values */ coeff = *pCoeffs++; /* Read Index, from where the state buffer should be read, is calculated. */ readIndex = (int32_t) (S->stateIndex - blockSize) - *pTapDelay++; /* Wraparound of readIndex */ if (readIndex < 0) { readIndex += (int32_t) delaySize; } /* Decrement loop counter */ tapCnt--; } /* Compute last tap without the final read of pTapDelay */ /* Working pointer for state buffer is updated */ py = pState; /* blockSize samples are read from the state buffer */ arm_circularRead_q7(py, (int32_t) delaySize, &readIndex, 1, pb, pb, (int32_t) blockSize, 1, blockSize); /* Working pointer for the scratch buffer of state values */ px = pb; /* Working pointer for scratch buffer of output values */ pScratchOut = pScr2; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ in = *pScratchOut + ((q31_t) *px++ * coeff); *pScratchOut++ = in; in = *pScratchOut + ((q31_t) *px++ * coeff); *pScratchOut++ = in; in = *pScratchOut + ((q31_t) *px++ * coeff); *pScratchOut++ = in; in = *pScratchOut + ((q31_t) *px++ * coeff); *pScratchOut++ = in; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Perform Multiply-Accumulate */ in = *pScratchOut + ((q31_t) *px++ * coeff); *pScratchOut++ = in; /* Decrement loop counter */ blkCnt--; } /* All the output values are in pScratchOut buffer. Convert them into 1.15 format, saturate and store in the destination buffer. */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time. */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { in1 = (q7_t) __SSAT(*pScr2++ >> 7, 8); in2 = (q7_t) __SSAT(*pScr2++ >> 7, 8); in3 = (q7_t) __SSAT(*pScr2++ >> 7, 8); in4 = (q7_t) __SSAT(*pScr2++ >> 7, 8); write_q7x4_ia (&pOut, __PACKq7(in1, in2, in3, in4)); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { *pOut++ = (q7_t) __SSAT(*pScr2++ >> 7, 8); /* Decrement loop counter */ blkCnt--; } } /** @} end of FIR_Sparse group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_fir_sparse_q7.c
C
apache-2.0
10,316
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_iir_lattice_f32.c * Description: Floating-point IIR Lattice filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @defgroup IIR_Lattice Infinite Impulse Response (IIR) Lattice Filters This set of functions implements lattice filters for Q15, Q31 and floating-point data types. Lattice filters are used in a variety of adaptive filter applications. The filter structure has feedforward and feedback components and the net impulse response is infinite length. The functions operate on blocks of input and output data and each call to the function processes <code>blockSize</code> samples through the filter. <code>pSrc</code> and <code>pDst</code> point to input and output arrays containing <code>blockSize</code> values. @par Algorithm \image html IIRLattice.gif "Infinite Impulse Response Lattice filter" @par <pre> fN(n) = x(n) fm-1(n) = fm(n) - km * gm-1(n-1) for m = N, N-1, ..., 1 gm(n) = km * fm-1(n) + gm-1(n-1) for m = N, N-1, ..., 1 y(n) = vN * gN(n) + vN-1 * gN-1(n) + ...+ v0 * g0(n) </pre> @par <code>pkCoeffs</code> points to array of reflection coefficients of size <code>numStages</code>. Reflection Coefficients are stored in time-reversed order. @par <pre> {kN, kN-1, ..., k1} </pre> @par <code>pvCoeffs</code> points to the array of ladder coefficients of size <code>(numStages+1)</code>. Ladder coefficients are stored in time-reversed order. <pre> {vN, vN-1, ..., v0} </pre> @par <code>pState</code> points to a state array of size <code>numStages + blockSize</code>. The state variables shown in the figure above (the g values) are stored in the <code>pState</code> array. The state variables are updated after each block of data is processed; the coefficients are untouched. @par Instance Structure The coefficients and state variables for a filter are stored together in an instance data structure. A separate instance structure must be defined for each filter. Coefficient arrays may be shared among several instances while state variable arrays cannot be shared. There are separate instance structure declarations for each of the 3 supported data types. @par Initialization Functions There is also an associated initialization function for each data type. The initialization function performs the following operations: - Sets the values of the internal structure fields. - Zeros out the values in the state buffer. To do this manually without calling the init function, assign the follow subfields of the instance structure: numStages, pkCoeffs, pvCoeffs, pState. Also set all of the values in pState to zero. @par Use of the initialization function is optional. However, if the initialization function is used, then the instance structure cannot be placed into a const data section. To place an instance structure into a const data section, the instance structure must be manually initialized. Set the values in the state buffer to zeros and then manually initialize the instance structure as follows: <pre> arm_iir_lattice_instance_f32 S = {numStages, pState, pkCoeffs, pvCoeffs}; arm_iir_lattice_instance_q31 S = {numStages, pState, pkCoeffs, pvCoeffs}; arm_iir_lattice_instance_q15 S = {numStages, pState, pkCoeffs, pvCoeffs}; </pre> @par where <code>numStages</code> is the number of stages in the filter; <code>pState</code> points to the state buffer array; <code>pkCoeffs</code> points to array of the reflection coefficients; <code>pvCoeffs</code> points to the array of ladder coefficients. @par Fixed-Point Behavior Care must be taken when using the fixed-point versions of the IIR lattice filter functions. In particular, the overflow and saturation behavior of the accumulator used in each function must be considered. Refer to the function specific documentation below for usage guidelines. */ /** @addtogroup IIR_Lattice @{ */ /** @brief Processing function for the floating-point IIR lattice filter. @param[in] S points to an instance of the floating-point IIR lattice structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none */ void arm_iir_lattice_f32( const arm_iir_lattice_instance_f32 * S, const float32_t * pSrc, float32_t * pDst, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ float32_t *pStateCur; /* State current pointer */ float32_t acc; /* Accumlator */ float32_t fnext1, fnext2, gcurr1, gnext; /* Temporary variables for lattice stages */ float32_t *px1, *px2, *pk, *pv; /* Temporary pointers for state and coef */ uint32_t numStages = S->numStages; /* Number of stages */ uint32_t blkCnt, tapCnt; /* Temporary variables for counts */ #if defined (ARM_MATH_LOOPUNROLL) float32_t gcurr2; /* Temporary variables for lattice stages */ float32_t k1, k2; float32_t v1, v2, v3, v4; #endif /* initialise loop count */ blkCnt = blockSize; /* Sample processing */ while (blkCnt > 0U) { /* Read Sample from input buffer */ /* fN(n) = x(n) */ fnext2 = *pSrc++; /* Initialize Ladder coeff pointer */ pv = &S->pvCoeffs[0]; /* Initialize Reflection coeff pointer */ pk = &S->pkCoeffs[0]; /* Initialize state read pointer */ px1 = pState; /* Initialize state write pointer */ px2 = pState; /* Set accumulator to zero */ acc = 0.0; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = (numStages) >> 2U; while (tapCnt > 0U) { /* Read gN-1(n-1) from state buffer */ gcurr1 = *px1; /* read reflection coefficient kN */ k1 = *pk; /* fN-1(n) = fN(n) - kN * gN-1(n-1) */ fnext1 = fnext2 - (k1 * gcurr1); /* read ladder coefficient vN */ v1 = *pv; /* read next reflection coefficient kN-1 */ k2 = *(pk + 1U); /* Read gN-2(n-1) from state buffer */ gcurr2 = *(px1 + 1U); /* read next ladder coefficient vN-1 */ v2 = *(pv + 1U); /* fN-2(n) = fN-1(n) - kN-1 * gN-2(n-1) */ fnext2 = fnext1 - (k2 * gcurr2); /* gN(n) = kN * fN-1(n) + gN-1(n-1) */ gnext = gcurr1 + (k1 * fnext1); /* read reflection coefficient kN-2 */ k1 = *(pk + 2U); /* write gN(n) into state for next sample processing */ *px2++ = gnext; /* Read gN-3(n-1) from state buffer */ gcurr1 = *(px1 + 2U); /* y(n) += gN(n) * vN */ acc += (gnext * v1); /* fN-3(n) = fN-2(n) - kN-2 * gN-3(n-1) */ fnext1 = fnext2 - (k1 * gcurr1); /* gN-1(n) = kN-1 * fN-2(n) + gN-2(n-1) */ gnext = gcurr2 + (k2 * fnext2); /* Read gN-4(n-1) from state buffer */ gcurr2 = *(px1 + 3U); /* y(n) += gN-1(n) * vN-1 */ acc += (gnext * v2); /* read reflection coefficient kN-3 */ k2 = *(pk + 3U); /* write gN-1(n) into state for next sample processing */ *px2++ = gnext; /* fN-4(n) = fN-3(n) - kN-3 * gN-4(n-1) */ fnext2 = fnext1 - (k2 * gcurr2); /* gN-2(n) = kN-2 * fN-3(n) + gN-3(n-1) */ gnext = gcurr1 + (k1 * fnext1); /* read ladder coefficient vN-2 */ v3 = *(pv + 2U); /* y(n) += gN-2(n) * vN-2 */ acc += (gnext * v3); /* write gN-2(n) into state for next sample processing */ *px2++ = gnext; /* update pointer */ pk += 4U; /* gN-3(n) = kN-3 * fN-4(n) + gN-4(n-1) */ gnext = (fnext2 * k2) + gcurr2; /* read next ladder coefficient vN-3 */ v4 = *(pv + 3U); /* y(n) += gN-4(n) * vN-4 */ acc += (gnext * v4); /* write gN-3(n) into state for next sample processing */ *px2++ = gnext; /* update pointers */ px1 += 4U; pv += 4U; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numStages % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numStages; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { gcurr1 = *px1++; /* Process sample for last taps */ fnext1 = fnext2 - ((*pk) * gcurr1); gnext = (fnext1 * (*pk++)) + gcurr1; /* Output samples for last taps */ acc += (gnext * (*pv++)); *px2++ = gnext; fnext2 = fnext1; /* Decrement loop counter */ tapCnt--; } /* y(n) += g0(n) * v0 */ acc += (fnext2 * (*pv)); *px2++ = fnext2; /* write out into pDst */ *pDst++ = acc; /* Advance the state pointer by 4 to process the next group of 4 samples */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy last S->numStages samples to start of the buffer for the preperation of next frame process */ /* Points to the start of the state buffer */ pStateCur = &S->pState[0]; pState = &S->pState[blockSize]; /* Copy data */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numStages >> 2U; while (tapCnt > 0U) { *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numStages % 0x4U; #else /* Initialize blkCnt with number of samples */ tapCnt = numStages; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } } /** @} end of IIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_iir_lattice_f32.c
C
apache-2.0
11,556
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_iir_lattice_init_f32.c * Description: Floating-point IIR lattice filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup IIR_Lattice @{ */ /** @brief Initialization function for the floating-point IIR lattice filter. @param[in] S points to an instance of the floating-point IIR lattice structure @param[in] numStages number of stages in the filter @param[in] pkCoeffs points to reflection coefficient buffer. The array is of length numStages @param[in] pvCoeffs points to ladder coefficient buffer. The array is of length numStages+1 @param[in] pState points to state buffer. The array is of length numStages+blockSize @param[in] blockSize number of samples to process @return none */ void arm_iir_lattice_init_f32( arm_iir_lattice_instance_f32 * S, uint16_t numStages, float32_t * pkCoeffs, float32_t * pvCoeffs, float32_t * pState, uint32_t blockSize) { /* Assign filter taps */ S->numStages = numStages; /* Assign reflection coefficient pointer */ S->pkCoeffs = pkCoeffs; /* Assign ladder coefficient pointer */ S->pvCoeffs = pvCoeffs; /* Clear state buffer and size is always blockSize + numStages */ memset(pState, 0, (numStages + blockSize) * sizeof(float32_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of IIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_iir_lattice_init_f32.c
C
apache-2.0
2,402
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_iir_lattice_init_q15.c * Description: Q15 IIR lattice filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup IIR_Lattice @{ */ /** @brief Initialization function for the Q15 IIR lattice filter. @param[in] S points to an instance of the Q15 IIR lattice structure @param[in] numStages number of stages in the filter @param[in] pkCoeffs points to reflection coefficient buffer. The array is of length numStages @param[in] pvCoeffs points to ladder coefficient buffer. The array is of length numStages+1 @param[in] pState points to state buffer. The array is of length numStages+blockSize @param[in] blockSize number of samples to process @return none */ void arm_iir_lattice_init_q15( arm_iir_lattice_instance_q15 * S, uint16_t numStages, q15_t * pkCoeffs, q15_t * pvCoeffs, q15_t * pState, uint32_t blockSize) { /* Assign filter taps */ S->numStages = numStages; /* Assign reflection coefficient pointer */ S->pkCoeffs = pkCoeffs; /* Assign ladder coefficient pointer */ S->pvCoeffs = pvCoeffs; /* Clear state buffer and size is always blockSize + numStages */ memset(pState, 0, (numStages + blockSize) * sizeof(q15_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of IIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_iir_lattice_init_q15.c
C
apache-2.0
2,325
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_iir_lattice_init_q31.c * Description: Initialization function for the Q31 IIR lattice filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup IIR_Lattice @{ */ /** @brief Initialization function for the Q31 IIR lattice filter. @param[in] S points to an instance of the Q31 IIR lattice structure @param[in] numStages number of stages in the filter @param[in] pkCoeffs points to reflection coefficient buffer. The array is of length numStages @param[in] pvCoeffs points to ladder coefficient buffer. The array is of length numStages+1 @param[in] pState points to state buffer. The array is of length numStages+blockSize @param[in] blockSize number of samples to process @return none */ void arm_iir_lattice_init_q31( arm_iir_lattice_instance_q31 * S, uint16_t numStages, q31_t * pkCoeffs, q31_t * pvCoeffs, q31_t * pState, uint32_t blockSize) { /* Assign filter taps */ S->numStages = numStages; /* Assign reflection coefficient pointer */ S->pkCoeffs = pkCoeffs; /* Assign ladder coefficient pointer */ S->pvCoeffs = pvCoeffs; /* Clear state buffer and size is always blockSize + numStages */ memset(pState, 0, (numStages + blockSize) * sizeof(q31_t)); /* Assign state pointer */ S->pState = pState; } /** @} end of IIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_iir_lattice_init_q31.c
C
apache-2.0
2,361
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_iir_lattice_q15.c * Description: Q15 IIR Lattice filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup IIR_Lattice @{ */ /** @brief Processing function for the Q15 IIR lattice filter. @param[in] S points to an instance of the Q15 IIR lattice structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits. Lastly, the accumulator is saturated to yield a result in 1.15 format. */ void arm_iir_lattice_q15( const arm_iir_lattice_instance_q15 * S, const q15_t * pSrc, q15_t * pDst, uint32_t blockSize) { q15_t *pState = S->pState; /* State pointer */ q15_t *pStateCur; /* State current pointer */ q31_t fcurr, fnext = 0, gcurr = 0, gnext; /* Temporary variables for lattice stages */ q63_t acc; /* Accumlator */ q15_t *px1, *px2, *pk, *pv; /* Temporary pointers for state and coef */ uint32_t numStages = S->numStages; /* Number of stages */ uint32_t blkCnt, tapCnt; /* Temporary variables for counts */ q15_t out; /* Temporary variable for output */ #if defined (ARM_MATH_DSP) && defined (ARM_MATH_LOOPUNROLL) q15_t gnext1, gnext2; /* Temporary variables for lattice stages */ q31_t v; /* Temporary variable for ladder coefficient */ #endif /* initialise loop count */ blkCnt = blockSize; #if defined (ARM_MATH_DSP) /* Sample processing */ while (blkCnt > 0U) { /* Read Sample from input buffer */ /* fN(n) = x(n) */ fcurr = *pSrc++; /* Initialize Ladder coeff pointer */ pv = &S->pvCoeffs[0]; /* Initialize Reflection coeff pointer */ pk = &S->pkCoeffs[0]; /* Initialize state read pointer */ px1 = pState; /* Initialize state write pointer */ px2 = pState; /* Set accumulator to zero */ acc = 0; /* Process sample for first tap */ gcurr = *px1++; /* fN-1(n) = fN(n) - kN * gN-1(n-1) */ fnext = fcurr - (((q31_t) gcurr * (*pk)) >> 15); fnext = __SSAT(fnext, 16); /* gN(n) = kN * fN-1(n) + gN-1(n-1) */ gnext = (((q31_t) fnext * (*pk++)) >> 15) + gcurr; gnext = __SSAT(gnext, 16); /* write gN(n) into state for next sample processing */ *px2++ = (q15_t) gnext; /* y(n) += gN(n) * vN */ acc += (q31_t) ((gnext * (*pv++))); /* Update f values for next coefficient processing */ fcurr = fnext; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = (numStages - 1U) >> 2U; while (tapCnt > 0U) { /* Process sample for 2nd, 6th ...taps */ /* Read gN-2(n-1) from state buffer */ gcurr = *px1++; /* fN-2(n) = fN-1(n) - kN-1 * gN-2(n-1) */ fnext = fcurr - (((q31_t) gcurr * (*pk)) >> 15); fnext = __SSAT(fnext, 16); /* gN-1(n) = kN-1 * fN-2(n) + gN-2(n-1) */ gnext = (((q31_t) fnext * (*pk++)) >> 15) + gcurr; gnext1 = (q15_t) __SSAT(gnext, 16); /* write gN-1(n) into state for next sample processing */ *px2++ = (q15_t) gnext1; /* Process sample for 3nd, 7th ...taps */ /* Read gN-3(n-1) from state buffer */ gcurr = *px1++; /* Process sample for 3rd, 7th .. taps */ /* fN-3(n) = fN-2(n) - kN-2 * gN-3(n-1) */ fcurr = fnext - (((q31_t) gcurr * (*pk)) >> 15); fcurr = __SSAT(fcurr, 16); /* gN-2(n) = kN-2 * fN-3(n) + gN-3(n-1) */ gnext = (((q31_t) fcurr * (*pk++)) >> 15) + gcurr; gnext2 = (q15_t) __SSAT(gnext, 16); /* write gN-2(n) into state */ *px2++ = (q15_t) gnext2; /* Read vN-1 and vN-2 at a time */ v = read_q15x2_ia (&pv); /* Pack gN-1(n) and gN-2(n) */ #ifndef ARM_MATH_BIG_ENDIAN gnext = __PKHBT(gnext1, gnext2, 16); #else gnext = __PKHBT(gnext2, gnext1, 16); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* y(n) += gN-1(n) * vN-1 */ /* process for gN-5(n) * vN-5, gN-9(n) * vN-9 ... */ /* y(n) += gN-2(n) * vN-2 */ /* process for gN-6(n) * vN-6, gN-10(n) * vN-10 ... */ acc = __SMLALD(gnext, v, acc); /* Process sample for 4th, 8th ...taps */ /* Read gN-4(n-1) from state buffer */ gcurr = *px1++; /* Process sample for 4th, 8th .. taps */ /* fN-4(n) = fN-3(n) - kN-3 * gN-4(n-1) */ fnext = fcurr - (((q31_t) gcurr * (*pk)) >> 15); fnext = __SSAT(fnext, 16); /* gN-3(n) = kN-3 * fN-1(n) + gN-1(n-1) */ gnext = (((q31_t) fnext * (*pk++)) >> 15) + gcurr; gnext1 = (q15_t) __SSAT(gnext, 16); /* write gN-3(n) for the next sample process */ *px2++ = (q15_t) gnext1; /* Process sample for 5th, 9th ...taps */ /* Read gN-5(n-1) from state buffer */ gcurr = *px1++; /* Process sample for 5th, 9th .. taps */ /* fN-5(n) = fN-4(n) - kN-4 * gN-5(n-1) */ fcurr = fnext - (((q31_t) gcurr * (*pk)) >> 15); fcurr = __SSAT(fcurr, 16); /* gN-4(n) = kN-4 * fN-5(n) + gN-5(n-1) */ gnext = (((q31_t) fcurr * (*pk++)) >> 15) + gcurr; gnext2 = (q15_t) __SSAT(gnext, 16); /* write gN-4(n) for the next sample process */ *px2++ = (q15_t) gnext2; /* Read vN-3 and vN-4 at a time */ v = read_q15x2_ia (&pv); /* Pack gN-3(n) and gN-4(n) */ #ifndef ARM_MATH_BIG_ENDIAN gnext = __PKHBT(gnext1, gnext2, 16); #else gnext = __PKHBT(gnext2, gnext1, 16); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* y(n) += gN-4(n) * vN-4 */ /* process for gN-8(n) * vN-8, gN-12(n) * vN-12 ... */ /* y(n) += gN-3(n) * vN-3 */ /* process for gN-7(n) * vN-7, gN-11(n) * vN-11 ... */ acc = __SMLALD(gnext, v, acc); /* Decrement loop counter */ tapCnt--; } fnext = fcurr; /* Loop unrolling: Compute remaining taps */ tapCnt = (numStages - 1U) % 0x4U; #else /* Initialize blkCnt with number of samples */ tapCnt = (numStages - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { gcurr = *px1++; /* Process sample for last taps */ fnext = fcurr - (((q31_t) gcurr * (*pk)) >> 15); fnext = __SSAT(fnext, 16); gnext = (((q31_t) fnext * (*pk++)) >> 15) + gcurr; gnext = __SSAT(gnext, 16); /* Output samples for last taps */ acc += (q31_t) (((q31_t) gnext * (*pv++))); *px2++ = (q15_t) gnext; fcurr = fnext; /* Decrement loop counter */ tapCnt--; } /* y(n) += g0(n) * v0 */ acc += (q31_t) (((q31_t) fnext * (*pv++))); out = (q15_t) __SSAT(acc >> 15, 16); *px2++ = (q15_t) fnext; /* write out into pDst */ *pDst++ = out; /* Advance the state pointer by 4 to process the next group of 4 samples */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy last S->numStages samples to start of the buffer for the preperation of next frame process */ /* Points to the start of the state buffer */ pStateCur = &S->pState[0]; pState = &S->pState[blockSize]; /* copy data */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numStages >> 2U; while (tapCnt > 0U) { write_q15x2_ia (&pStateCur, read_q15x2_ia (&pState)); write_q15x2_ia (&pStateCur, read_q15x2_ia (&pState)); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numStages % 0x4U; #else /* Initialize blkCnt with number of samples */ tapCnt = (numStages - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #else /* #if defined (ARM_MATH_DSP) */ /* Sample processing */ while (blkCnt > 0U) { /* Read Sample from input buffer */ /* fN(n) = x(n) */ fcurr = *pSrc++; /* Initialize Ladder coeff pointer */ pv = &S->pvCoeffs[0]; /* Initialize Reflection coeff pointer */ pk = &S->pkCoeffs[0]; /* Initialize state read pointer */ px1 = pState; /* Initialize state write pointer */ px2 = pState; /* Set accumulator to zero */ acc = 0; tapCnt = numStages; while (tapCnt > 0U) { gcurr = *px1++; /* Process sample */ /* fN-1(n) = fN(n) - kN * gN-1(n-1) */ fnext = fcurr - ((gcurr * (*pk)) >> 15); fnext = __SSAT(fnext, 16); /* gN(n) = kN * fN-1(n) + gN-1(n-1) */ gnext = ((fnext * (*pk++)) >> 15) + gcurr; gnext = __SSAT(gnext, 16); /* Output samples */ /* y(n) += gN(n) * vN */ acc += (q31_t) ((gnext * (*pv++))); /* write gN(n) into state for next sample processing */ *px2++ = (q15_t) gnext; /* Update f values for next coefficient processing */ fcurr = fnext; tapCnt--; } /* y(n) += g0(n) * v0 */ acc += (q31_t) ((fnext * (*pv++))); out = (q15_t) __SSAT(acc >> 15, 16); *px2++ = (q15_t) fnext; /* write out into pDst */ *pDst++ = out; /* Advance the state pointer by 1 to process the next group of samples */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy last S->numStages samples to start of the buffer for the preperation of next frame process */ /* Points to the start of the state buffer */ pStateCur = &S->pState[0]; pState = &S->pState[blockSize]; tapCnt = numStages; /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #endif /* #if defined (ARM_MATH_DSP) */ } /** @} end of IIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_iir_lattice_q15.c
C
apache-2.0
11,689
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_iir_lattice_q31.c * Description: Q31 IIR Lattice filter processing function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup IIR_Lattice @{ */ /** @brief Processing function for the Q31 IIR lattice filter. @param[in] S points to an instance of the Q31 IIR lattice structure @param[in] pSrc points to the block of input data @param[out] pDst points to the block of output data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. Thus, if the accumulator result overflows it wraps around rather than clip. In order to avoid overflows completely the input signal must be scaled down by 2*log2(numStages) bits. After all multiply-accumulates are performed, the 2.62 accumulator is saturated to 1.32 format and then truncated to 1.31 format. */ void arm_iir_lattice_q31( const arm_iir_lattice_instance_q31 * S, const q31_t * pSrc, q31_t * pDst, uint32_t blockSize) { q31_t *pState = S->pState; /* State pointer */ q31_t *pStateCur; /* State current pointer */ q31_t fcurr, fnext = 0, gcurr = 0, gnext; /* Temporary variables for lattice stages */ q63_t acc; /* Accumlator */ q31_t *px1, *px2, *pk, *pv; /* Temporary pointers for state and coef */ uint32_t numStages = S->numStages; /* Number of stages */ uint32_t blkCnt, tapCnt; /* Temporary variables for counts */ /* initialise loop count */ blkCnt = blockSize; #if defined (ARM_MATH_DSP) /* Sample processing */ while (blkCnt > 0U) { /* Read Sample from input buffer */ /* fN(n) = x(n) */ fcurr = *pSrc++; /* Initialize Ladder coeff pointer */ pv = &S->pvCoeffs[0]; /* Initialize Reflection coeff pointer */ pk = &S->pkCoeffs[0]; /* Initialize state read pointer */ px1 = pState; /* Initialize state write pointer */ px2 = pState; /* Set accumulator to zero */ acc = 0; /* Process sample for first tap */ gcurr = *px1++; /* fN-1(n) = fN(n) - kN * gN-1(n-1) */ fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk )) >> 31)); /* gN(n) = kN * fN-1(n) + gN-1(n-1) */ gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31)); /* write gN-1(n-1) into state for next sample processing */ *px2++ = gnext; /* y(n) += gN(n) * vN */ acc += ((q63_t) gnext * *pv++); /* Update f values for next coefficient processing */ fcurr = fnext; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = (numStages - 1U) >> 2U; while (tapCnt > 0U) { /* Process sample for 2nd, 6th ...taps */ /* Read gN-2(n-1) from state buffer */ gcurr = *px1++; /* fN-2(n) = fN-1(n) - kN-1 * gN-2(n-1) */ fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk )) >> 31)); /* gN-1(n) = kN-1 * fN-2(n) + gN-2(n-1) */ gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31)); /* y(n) += gN-1(n) * vN-1 */ /* process for gN-5(n) * vN-5, gN-9(n) * vN-9 ... */ acc += ((q63_t) gnext * *pv++); /* write gN-1(n) into state for next sample processing */ *px2++ = gnext; /* Process sample for 3nd, 7th ...taps */ /* Read gN-3(n-1) from state buffer */ gcurr = *px1++; /* Process sample for 3rd, 7th .. taps */ /* fN-3(n) = fN-2(n) - kN-2 * gN-3(n-1) */ fcurr = __QSUB(fnext, (q31_t) (((q63_t) gcurr * (*pk )) >> 31)); /* gN-2(n) = kN-2 * fN-3(n) + gN-3(n-1) */ gnext = __QADD(gcurr, (q31_t) (((q63_t) fcurr * (*pk++)) >> 31)); /* y(n) += gN-2(n) * vN-2 */ /* process for gN-6(n) * vN-6, gN-10(n) * vN-10 ... */ acc += ((q63_t) gnext * *pv++); /* write gN-2(n) into state for next sample processing */ *px2++ = gnext; /* Process sample for 4th, 8th ...taps */ /* Read gN-4(n-1) from state buffer */ gcurr = *px1++; /* Process sample for 4th, 8th .. taps */ /* fN-4(n) = fN-3(n) - kN-3 * gN-4(n-1) */ fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk )) >> 31)); /* gN-3(n) = kN-3 * fN-4(n) + gN-4(n-1) */ gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31)); /* y(n) += gN-3(n) * vN-3 */ /* process for gN-7(n) * vN-7, gN-11(n) * vN-11 ... */ acc += ((q63_t) gnext * *pv++); /* write gN-3(n) into state for next sample processing */ *px2++ = gnext; /* Process sample for 5th, 9th ...taps */ /* Read gN-5(n-1) from state buffer */ gcurr = *px1++; /* Process sample for 5th, 9th .. taps */ /* fN-5(n) = fN-4(n) - kN-4 * gN-1(n-1) */ fcurr = __QSUB(fnext, (q31_t) (((q63_t) gcurr * (*pk )) >> 31)); /* gN-4(n) = kN-4 * fN-5(n) + gN-5(n-1) */ gnext = __QADD(gcurr, (q31_t) (((q63_t) fcurr * (*pk++)) >> 31)); /* y(n) += gN-4(n) * vN-4 */ /* process for gN-8(n) * vN-8, gN-12(n) * vN-12 ... */ acc += ((q63_t) gnext * *pv++); /* write gN-4(n) into state for next sample processing */ *px2++ = gnext; /* Decrement loop counter */ tapCnt--; } fnext = fcurr; /* Loop unrolling: Compute remaining taps */ tapCnt = (numStages - 1U) % 0x4U; #else /* Initialize blkCnt with number of samples */ tapCnt = (numStages - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { gcurr = *px1++; /* Process sample for last taps */ fnext = __QSUB(fcurr, (q31_t) (((q63_t) gcurr * (*pk )) >> 31)); gnext = __QADD(gcurr, (q31_t) (((q63_t) fnext * (*pk++)) >> 31)); /* Output samples for last taps */ acc += ((q63_t) gnext * *pv++); *px2++ = gnext; fcurr = fnext; /* Decrement loop counter */ tapCnt--; } /* y(n) += g0(n) * v0 */ acc += ((q63_t) fnext * *pv++); *px2++ = fnext; /* write out into pDst */ *pDst++ = (q31_t) (acc >> 31U); /* Advance the state pointer by 4 to process the next group of 4 samples */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy last S->numStages samples to start of the buffer for the preperation of next frame process */ /* Points to the start of the state buffer */ pStateCur = &S->pState[0]; pState = &S->pState[blockSize]; /* Copy data */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numStages >> 2U; while (tapCnt > 0U) { *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numStages % 0x4U; #else /* Initialize blkCnt with number of samples */ tapCnt = (numStages - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #else /* #if defined (ARM_MATH_DSP) */ /* Sample processing */ while (blkCnt > 0U) { /* Read Sample from input buffer */ /* fN(n) = x(n) */ fcurr = *pSrc++; /* Initialize Ladder coeff pointer */ pv = &S->pvCoeffs[0]; /* Initialize Reflection coeff pointer */ pk = &S->pkCoeffs[0]; /* Initialize state read pointer */ px1 = pState; /* Initialize state write pointer */ px2 = pState; /* Set accumulator to zero */ acc = 0; tapCnt = numStages; while (tapCnt > 0U) { gcurr = *px1++; /* Process sample */ /* fN-1(n) = fN(n) - kN * gN-1(n-1) */ fnext = clip_q63_to_q31(((q63_t) fcurr - ((q31_t) (((q63_t) gcurr * (*pk )) >> 31)))); /* gN(n) = kN * fN-1(n) + gN-1(n-1) */ gnext = clip_q63_to_q31(((q63_t) gcurr + ((q31_t) (((q63_t) fnext * (*pk++)) >> 31)))); /* Output samples */ /* y(n) += gN(n) * vN */ acc += ((q63_t) gnext * *pv++); /* write gN-1(n-1) into state for next sample processing */ *px2++ = gnext; /* Update f values for next coefficient processing */ fcurr = fnext; tapCnt--; } /* y(n) += g0(n) * v0 */ acc += ((q63_t) fnext * *pv++); *px2++ = fnext; /* write out into pDst */ *pDst++ = (q31_t) (acc >> 31U); /* Advance the state pointer by 1 to process the next group of samples */ pState = pState + 1U; /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy last S->numStages samples to start of the buffer for the preperation of next frame process */ /* Points to the start of the state buffer */ pStateCur = &S->pState[0]; pState = &S->pState[blockSize]; tapCnt = numStages; /* Copy data */ while (tapCnt > 0U) { *pStateCur++ = *pState++; /* Decrement loop counter */ tapCnt--; } #endif /* #if defined (ARM_MATH_DSP) */ } /** @} end of IIR_Lattice group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_iir_lattice_q31.c
C
apache-2.0
10,489
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_f32.c * Description: Processing function for the floating-point LMS filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @defgroup LMS Least Mean Square (LMS) Filters LMS filters are a class of adaptive filters that are able to "learn" an unknown transfer functions. LMS filters use a gradient descent method in which the filter coefficients are updated based on the instantaneous error signal. Adaptive filters are often used in communication systems, equalizers, and noise removal. The CMSIS DSP Library contains LMS filter functions that operate on Q15, Q31, and floating-point data types. The library also contains normalized LMS filters in which the filter coefficient adaptation is indepedent of the level of the input signal. An LMS filter consists of two components as shown below. The first component is a standard transversal or FIR filter. The second component is a coefficient update mechanism. The LMS filter has two input signals. The "input" feeds the FIR filter while the "reference input" corresponds to the desired output of the FIR filter. That is, the FIR filter coefficients are updated so that the output of the FIR filter matches the reference input. The filter coefficient update mechanism is based on the difference between the FIR filter output and the reference input. This "error signal" tends towards zero as the filter adapts. The LMS processing functions accept the input and reference input signals and generate the filter output and error signal. \image html LMS.gif "Internal structure of the Least Mean Square filter" The functions operate on blocks of data and each call to the function processes <code>blockSize</code> samples through the filter. <code>pSrc</code> points to input signal, <code>pRef</code> points to reference signal, <code>pOut</code> points to output signal and <code>pErr</code> points to error signal. All arrays contain <code>blockSize</code> values. The functions operate on a block-by-block basis. Internally, the filter coefficients <code>b[n]</code> are updated on a sample-by-sample basis. The convergence of the LMS filter is slower compared to the normalized LMS algorithm. @par Algorithm The output signal <code>y[n]</code> is computed by a standard FIR filter: <pre> y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1] </pre> @par The error signal equals the difference between the reference signal <code>d[n]</code> and the filter output: <pre> e[n] = d[n] - y[n]. </pre> @par After each sample of the error signal is computed, the filter coefficients <code>b[k]</code> are updated on a sample-by-sample basis: <pre> b[k] = b[k] + e[n] * mu * x[n-k], for k=0, 1, ..., numTaps-1 </pre> where <code>mu</code> is the step size and controls the rate of coefficient convergence. @par In the APIs, <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>. Coefficients are stored in time reversed order. @par <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> @par <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>. Samples in the state buffer are stored in the order: @par <pre> {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]} </pre> @par Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code> samples. The increased state buffer length allows circular addressing, which is traditionally used in FIR filters, to be avoided and yields a significant speed improvement. The state variables are updated after each block of data is processed. @par Instance Structure The coefficients and state variables for a filter are stored together in an instance data structure. A separate instance structure must be defined for each filter and coefficient and state arrays cannot be shared among instances. There are separate instance structure declarations for each of the 3 supported data types. @par Initialization Functions There is also an associated initialization function for each data type. The initialization function performs the following operations: - Sets the values of the internal structure fields. - Zeros out the values in the state buffer. To do this manually without calling the init function, assign the follow subfields of the instance structure: numTaps, pCoeffs, mu, postShift (not for f32), pState. Also set all of the values in pState to zero. @par Use of the initialization function is optional. However, if the initialization function is used, then the instance structure cannot be placed into a const data section. To place an instance structure into a const data section, the instance structure must be manually initialized. Set the values in the state buffer to zeros before static initialization. The code below statically initializes each of the 3 different data type filter instance structures <pre> arm_lms_instance_f32 S = {numTaps, pState, pCoeffs, mu}; arm_lms_instance_q31 S = {numTaps, pState, pCoeffs, mu, postShift}; arm_lms_instance_q15 S = {numTaps, pState, pCoeffs, mu, postShift}; </pre> where <code>numTaps</code> is the number of filter coefficients in the filter; <code>pState</code> is the address of the state buffer; <code>pCoeffs</code> is the address of the coefficient buffer; <code>mu</code> is the step size parameter; and <code>postShift</code> is the shift applied to coefficients. @par Fixed-Point Behavior Care must be taken when using the Q15 and Q31 versions of the LMS filter. The following issues must be considered: - Scaling of coefficients - Overflow and saturation @par Scaling of Coefficients Filter coefficients are represented as fractional values and coefficients are restricted to lie in the range <code>[-1 +1)</code>. The fixed-point functions have an additional scaling parameter <code>postShift</code>. At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits. This essentially scales the filter coefficients by <code>2^postShift</code> and allows the filter coefficients to exceed the range <code>[+1 -1)</code>. The value of <code>postShift</code> is set by the user based on the expected gain through the system being modeled. @par Overflow and Saturation Overflow and saturation behavior of the fixed-point Q15 and Q31 versions are described separately as part of the function specific documentation below. */ /** @addtogroup LMS @{ */ /** @brief Processing function for floating-point LMS filter. @param[in] S points to an instance of the floating-point LMS filter structure @param[in] pSrc points to the block of input data @param[in] pRef points to the block of reference data @param[out] pOut points to the block of output data @param[out] pErr points to the block of error data @param[in] blockSize number of samples to process @return none */ #if defined(ARM_MATH_NEON) void arm_lms_f32( const arm_lms_instance_f32 * S, const float32_t * pSrc, float32_t * pRef, float32_t * pOut, float32_t * pErr, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *pStateCurnt; /* Points to the current sample of the state */ float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ float32_t mu = S->mu; /* Adaptive factor */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ float32_t sum, e, d; /* accumulator, error, reference data sample */ float32_t w = 0.0f; /* weight factor */ float32x4_t tempV, sumV, xV, bV; float32x2_t tempV2; e = 0.0f; d = 0.0f; /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); blkCnt = blockSize; while (blkCnt > 0U) { /* Copy the new input sample into the state buffer */ *pStateCurnt++ = *pSrc++; /* Initialize pState pointer */ px = pState; /* Initialize coeff pointer */ pb = (pCoeffs); /* Set the accumulator to zero */ sum = 0.0f; sumV = vdupq_n_f32(0.0); /* Process 4 taps at a time. */ tapCnt = numTaps >> 2; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ xV = vld1q_f32(px); bV = vld1q_f32(pb); sumV = vmlaq_f32(sumV, xV, bV); px += 4; pb += 4; /* Decrement the loop counter */ tapCnt--; } tempV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV)); sum = tempV2[0] + tempV2[1]; /* If the filter length is not a multiple of 4, compute the remaining filter taps */ tapCnt = numTaps % 0x4U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum += (*px++) * (*pb++); /* Decrement the loop counter */ tapCnt--; } /* The result in the accumulator, store in the destination buffer. */ *pOut++ = sum; /* Compute and store error */ d = (float32_t) (*pRef++); e = d - sum; *pErr++ = e; /* Calculation of Weighting factor for the updating filter coefficients */ w = e * mu; /* Initialize pState pointer */ px = pState; /* Initialize coeff pointer */ pb = (pCoeffs); /* Process 4 taps at a time. */ tapCnt = numTaps >> 2; /* Update filter coefficients */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ xV = vld1q_f32(px); bV = vld1q_f32(pb); px += 4; bV = vmlaq_n_f32(bV,xV,w); vst1q_f32(pb,bV); pb += 4; /* Decrement the loop counter */ tapCnt--; } /* If the filter length is not a multiple of 4, compute the remaining filter taps */ tapCnt = numTaps % 0x4U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ *pb = *pb + (w * (*px++)); pb++; /* Decrement the loop counter */ tapCnt--; } /* Advance state pointer by 1 for the next sample */ pState = pState + 1; /* Decrement the loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the satrt of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the pState buffer */ pStateCurnt = S->pState; /* Process 4 taps at a time for (numTaps - 1U) samples copy */ tapCnt = (numTaps - 1U) >> 2U; /* copy data */ while (tapCnt > 0U) { tempV = vld1q_f32(pState); vst1q_f32(pStateCurnt,tempV); pState += 4; pStateCurnt += 4; /* Decrement the loop counter */ tapCnt--; } /* Calculate remaining number of copies */ tapCnt = (numTaps - 1U) % 0x4U; /* Copy the remaining q31_t data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement the loop counter */ tapCnt--; } } #else void arm_lms_f32( const arm_lms_instance_f32 * S, const float32_t * pSrc, float32_t * pRef, float32_t * pOut, float32_t * pErr, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *pStateCurnt; /* Points to the current sample of the state */ float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ float32_t mu = S->mu; /* Adaptive factor */ float32_t acc, e; /* Accumulator, error */ float32_t w; /* Weight factor */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ /* Initializations of error, difference, Coefficient update */ e = 0.0f; w = 0.0f; /* S->pState points to state array which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); /* initialise loop count */ blkCnt = blockSize; while (blkCnt > 0U) { /* Copy the new input sample into the state buffer */ *pStateCurnt++ = *pSrc++; /* Initialize pState pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Set the accumulator to zero */ acc = 0.0f; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ acc += (*px++) * (*pb++); acc += (*px++) * (*pb++); acc += (*px++) * (*pb++); acc += (*px++) * (*pb++); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ acc += (*px++) * (*pb++); /* Decrement the loop counter */ tapCnt--; } /* Store the result from accumulator into the destination buffer. */ *pOut++ = acc; /* Compute and store error */ e = (float32_t) *pRef++ - acc; *pErr++ = e; /* Calculation of Weighting factor for updating filter coefficients */ w = e * mu; /* Initialize pState pointer */ /* Advance state pointer by 1 for the next sample */ px = pState++; /* Initialize coefficient pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; /* Update filter coefficients */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ *pb += w * (*px++); pb++; *pb += w * (*px++); pb++; *pb += w * (*px++); pb++; *pb += w * (*px++); pb++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ *pb += w * (*px++); pb++; /* Decrement loop counter */ tapCnt--; } /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the pState buffer */ pStateCurnt = S->pState; /* copy data */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = (numTaps - 1U) >> 2U; while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of LMS group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_f32.c
C
apache-2.0
17,964
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_init_f32.c * Description: Floating-point LMS filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @addtogroup LMS @{ */ /** @brief Initialization function for floating-point LMS filter. @param[in] S points to an instance of the floating-point LMS filter structure @param[in] numTaps number of filter coefficients @param[in] pCoeffs points to coefficient buffer @param[in] pState points to state buffer @param[in] mu step size that controls filter coefficient updates @param[in] blockSize number of samples to process @return none @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> The initial filter coefficients serve as a starting point for the adaptive filter. <code>pState</code> points to an array of length <code>numTaps+blockSize-1</code> samples, where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_lms_f32()</code>. */ void arm_lms_init_f32( arm_lms_instance_f32 * S, uint16_t numTaps, float32_t * pCoeffs, float32_t * pState, float32_t mu, uint32_t blockSize) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer and size is always blockSize + numTaps */ memset(pState, 0, (numTaps + (blockSize - 1)) * sizeof(float32_t)); /* Assign state pointer */ S->pState = pState; /* Assign Step size value */ S->mu = mu; } /** @} end of LMS group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_init_f32.c
C
apache-2.0
2,687
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_init_q15.c * Description: Q15 LMS filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup LMS @{ */ /** @brief Initialization function for the Q15 LMS filter. @param[in] S points to an instance of the Q15 LMS filter structure. @param[in] numTaps number of filter coefficients. @param[in] pCoeffs points to coefficient buffer. @param[in] pState points to state buffer. @param[in] mu step size that controls filter coefficient updates. @param[in] blockSize number of samples to process. @param[in] postShift bit shift applied to coefficients. @return none @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> The initial filter coefficients serve as a starting point for the adaptive filter. <code>pState</code> points to the array of state variables and size of array is <code>numTaps+blockSize-1</code> samples, where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_lms_q15()</code>. */ void arm_lms_init_q15( arm_lms_instance_q15 * S, uint16_t numTaps, q15_t * pCoeffs, q15_t * pState, q15_t mu, uint32_t blockSize, uint32_t postShift) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer and size is always blockSize + numTaps - 1 */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q15_t)); /* Assign state pointer */ S->pState = pState; /* Assign Step size value */ S->mu = mu; /* Assign postShift value to be applied */ S->postShift = postShift; } /** @} end of LMS group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_init_q15.c
C
apache-2.0
2,914
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_init_q31.c * Description: Q31 LMS filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup LMS @{ */ /** @brief Initialization function for Q31 LMS filter. @param[in] S points to an instance of the Q31 LMS filter structure @param[in] numTaps number of filter coefficients @param[in] pCoeffs points to coefficient buffer @param[in] pState points to state buffer @param[in] mu step size that controls filter coefficient updates @param[in] blockSize number of samples to process @param[in] postShift bit shift applied to coefficients @return none @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> The initial filter coefficients serve as a starting point for the adaptive filter. <code>pState</code> points to an array of length <code>numTaps+blockSize-1</code> samples, where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_lms_q31()</code>. */ void arm_lms_init_q31( arm_lms_instance_q31 * S, uint16_t numTaps, q31_t * pCoeffs, q31_t * pState, q31_t mu, uint32_t blockSize, uint32_t postShift) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer and size is always blockSize + numTaps - 1 */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q31_t)); /* Assign state pointer */ S->pState = pState; /* Assign Step size value */ S->mu = mu; /* Assign postShift value to be applied */ S->postShift = postShift; } /** @} end of LMS group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_init_q31.c
C
apache-2.0
2,872
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_norm_f32.c * Description: Processing function for the floating-point NLMS filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @defgroup LMS_NORM Normalized LMS Filters This set of functions implements a commonly used adaptive filter. It is related to the Least Mean Square (LMS) adaptive filter and includes an additional normalization factor which increases the adaptation rate of the filter. The CMSIS DSP Library contains normalized LMS filter functions that operate on Q15, Q31, and floating-point data types. A normalized least mean square (NLMS) filter consists of two components as shown below. The first component is a standard transversal or FIR filter. The second component is a coefficient update mechanism. The NLMS filter has two input signals. The "input" feeds the FIR filter while the "reference input" corresponds to the desired output of the FIR filter. That is, the FIR filter coefficients are updated so that the output of the FIR filter matches the reference input. The filter coefficient update mechanism is based on the difference between the FIR filter output and the reference input. This "error signal" tends towards zero as the filter adapts. The NLMS processing functions accept the input and reference input signals and generate the filter output and error signal. \image html LMS.gif "Internal structure of the NLMS adaptive filter" The functions operate on blocks of data and each call to the function processes <code>blockSize</code> samples through the filter. <code>pSrc</code> points to input signal, <code>pRef</code> points to reference signal, <code>pOut</code> points to output signal and <code>pErr</code> points to error signal. All arrays contain <code>blockSize</code> values. The functions operate on a block-by-block basis. Internally, the filter coefficients <code>b[n]</code> are updated on a sample-by-sample basis. The convergence of the LMS filter is slower compared to the normalized LMS algorithm. @par Algorithm The output signal <code>y[n]</code> is computed by a standard FIR filter: <pre> y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1] </pre> @par The error signal equals the difference between the reference signal <code>d[n]</code> and the filter output: <pre> e[n] = d[n] - y[n]. </pre> @par After each sample of the error signal is computed the instanteous energy of the filter state variables is calculated: <pre> E = x[n]^2 + x[n-1]^2 + ... + x[n-numTaps+1]^2. </pre> The filter coefficients <code>b[k]</code> are then updated on a sample-by-sample basis: <pre> b[k] = b[k] + e[n] * (mu/E) * x[n-k], for k=0, 1, ..., numTaps-1 </pre> where <code>mu</code> is the step size and controls the rate of coefficient convergence. @par In the APIs, <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>. Coefficients are stored in time reversed order. @par <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> @par <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>. Samples in the state buffer are stored in the order: @par <pre> {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]} </pre> @par Note that the length of the state buffer exceeds the length of the coefficient array by <code>blockSize-1</code> samples. The increased state buffer length allows circular addressing, which is traditionally used in FIR filters, to be avoided and yields a significant speed improvement. The state variables are updated after each block of data is processed. @par Instance Structure The coefficients and state variables for a filter are stored together in an instance data structure. A separate instance structure must be defined for each filter and coefficient and state arrays cannot be shared among instances. There are separate instance structure declarations for each of the 3 supported data types. @par Initialization Functions There is also an associated initialization function for each data type. The initialization function performs the following operations: - Sets the values of the internal structure fields. - Zeros out the values in the state buffer. To do this manually without calling the init function, assign the follow subfields of the instance structure: numTaps, pCoeffs, mu, energy, x0, pState. Also set all of the values in pState to zero. For Q7, Q15, and Q31 the following fields must also be initialized; recipTable, postShift @par Instance structure cannot be placed into a const data section and it is recommended to use the initialization function. @par Fixed-Point Behavior Care must be taken when using the Q15 and Q31 versions of the normalised LMS filter. The following issues must be considered: - Scaling of coefficients - Overflow and saturation @par Scaling of Coefficients Filter coefficients are represented as fractional values and coefficients are restricted to lie in the range <code>[-1 +1)</code>. The fixed-point functions have an additional scaling parameter <code>postShift</code>. At the output of the filter's accumulator is a shift register which shifts the result by <code>postShift</code> bits. This essentially scales the filter coefficients by <code>2^postShift</code> and allows the filter coefficients to exceed the range <code>[+1 -1)</code>. The value of <code>postShift</code> is set by the user based on the expected gain through the system being modeled. @par Overflow and Saturation Overflow and saturation behavior of the fixed-point Q15 and Q31 versions are described separately as part of the function specific documentation below. */ /** @addtogroup LMS_NORM @{ */ /** @brief Processing function for floating-point normalized LMS filter. @param[in] S points to an instance of the floating-point normalized LMS filter structure @param[in] pSrc points to the block of input data @param[in] pRef points to the block of reference data @param[out] pOut points to the block of output data @param[out] pErr points to the block of error data @param[in] blockSize number of samples to process @return none */ #if defined(ARM_MATH_NEON) void arm_lms_norm_f32( arm_lms_norm_instance_f32 * S, const float32_t * pSrc, float32_t * pRef, float32_t * pOut, float32_t * pErr, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *pStateCurnt; /* Points to the current sample of the state */ float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ float32_t mu = S->mu; /* Adaptive factor */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ float32_t energy; /* Energy of the input */ float32_t sum, e, d; /* accumulator, error, reference data sample */ float32_t w, x0, in; /* weight factor, temporary variable to hold input sample and state */ float32x4_t tempV, sumV, xV, bV; float32x2_t tempV2; /* Initializations of error, difference, Coefficient update */ e = 0.0f; d = 0.0f; w = 0.0f; energy = S->energy; x0 = S->x0; /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); /* Loop over blockSize number of values */ blkCnt = blockSize; while (blkCnt > 0U) { /* Copy the new input sample into the state buffer */ *pStateCurnt++ = *pSrc; /* Initialize pState pointer */ px = pState; /* Initialize coeff pointer */ pb = (pCoeffs); /* Read the sample from input buffer */ in = *pSrc++; /* Update the energy calculation */ energy -= x0 * x0; energy += in * in; /* Set the accumulator to zero */ sum = 0.0f; sumV = vdupq_n_f32(0.0); /* Process 4 taps at a time. */ tapCnt = numTaps >> 2; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ xV = vld1q_f32(px); bV = vld1q_f32(pb); sumV = vmlaq_f32(sumV, xV, bV); px += 4; pb += 4; /* Decrement the loop counter */ tapCnt--; } tempV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV)); sum = tempV2[0] + tempV2[1]; /* If the filter length is not a multiple of 4, compute the remaining filter taps */ tapCnt = numTaps % 0x4U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ sum += (*px++) * (*pb++); /* Decrement the loop counter */ tapCnt--; } /* The result in the accumulator, store in the destination buffer. */ *pOut++ = sum; /* Compute and store error */ d = (float32_t) (*pRef++); e = d - sum; *pErr++ = e; /* Calculation of Weighting factor for updating filter coefficients */ /* epsilon value 0.000000119209289f */ w = (e * mu) / (energy + 0.000000119209289f); /* Initialize pState pointer */ px = pState; /* Initialize coeff pointer */ pb = (pCoeffs); /* Process 4 taps at a time. */ tapCnt = numTaps >> 2; /* Update filter coefficients */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ xV = vld1q_f32(px); bV = vld1q_f32(pb); px += 4; bV = vmlaq_n_f32(bV,xV,w); vst1q_f32(pb,bV); pb += 4; /* Decrement the loop counter */ tapCnt--; } /* If the filter length is not a multiple of 4, compute the remaining filter taps */ tapCnt = numTaps % 0x4U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ *pb += w * (*px++); pb++; /* Decrement the loop counter */ tapCnt--; } x0 = *pState; /* Advance state pointer by 1 for the next sample */ pState = pState + 1; /* Decrement the loop counter */ blkCnt--; } S->energy = energy; S->x0 = x0; /* Processing is complete. Now copy the last numTaps - 1 samples to the satrt of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the pState buffer */ pStateCurnt = S->pState; /* Process 4 taps at a time for (numTaps - 1U)/4 samples copy */ tapCnt = (numTaps - 1U) >> 2U; /* copy data */ while (tapCnt > 0U) { tempV = vld1q_f32(pState); vst1q_f32(pStateCurnt,tempV); pState += 4; pStateCurnt += 4; /* Decrement the loop counter */ tapCnt--; } /* Calculate remaining number of copies */ tapCnt = (numTaps - 1U) % 0x4U; /* Copy the remaining q31_t data */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement the loop counter */ tapCnt--; } } #else void arm_lms_norm_f32( arm_lms_norm_instance_f32 * S, const float32_t * pSrc, float32_t * pRef, float32_t * pOut, float32_t * pErr, uint32_t blockSize) { float32_t *pState = S->pState; /* State pointer */ float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ float32_t *pStateCurnt; /* Points to the current sample of the state */ float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ float32_t mu = S->mu; /* Adaptive factor */ float32_t acc, e; /* Accumulator, error */ float32_t w; /* Weight factor */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ float32_t energy; /* Energy of the input */ float32_t x0, in; /* Temporary variable to hold input sample and state */ /* Initializations of error, difference, Coefficient update */ e = 0.0f; w = 0.0f; energy = S->energy; x0 = S->x0; /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); /* initialise loop count */ blkCnt = blockSize; while (blkCnt > 0U) { /* Copy the new input sample into the state buffer */ *pStateCurnt++ = *pSrc; /* Initialize pState pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Read the sample from input buffer */ in = *pSrc++; /* Update the energy calculation */ energy -= x0 * x0; energy += in * in; /* Set the accumulator to zero */ acc = 0.0f; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ acc += (*px++) * (*pb++); acc += (*px++) * (*pb++); acc += (*px++) * (*pb++); acc += (*px++) * (*pb++); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ acc += (*px++) * (*pb++); /* Decrement the loop counter */ tapCnt--; } /* Store the result from accumulator into the destination buffer. */ *pOut++ = acc; /* Compute and store error */ e = (float32_t) *pRef++ - acc; *pErr++ = e; /* Calculation of Weighting factor for updating filter coefficients */ /* epsilon value 0.000000119209289f */ w = (e * mu) / (energy + 0.000000119209289f); /* Initialize pState pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; /* Update filter coefficients */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ *pb += w * (*px++); pb++; *pb += w * (*px++); pb++; *pb += w * (*px++); pb++; *pb += w * (*px++); pb++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ *pb += w * (*px++); pb++; /* Decrement loop counter */ tapCnt--; } x0 = *pState; /* Advance state pointer by 1 for the next sample */ pState = pState + 1; /* Decrement loop counter */ blkCnt--; } /* Save energy and x0 values for the next frame */ S->energy = energy; S->x0 = x0; /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the pState buffer */ pStateCurnt = S->pState; /* copy data */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = (numTaps - 1U) >> 2U; while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of LMS_NORM group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_norm_f32.c
C
apache-2.0
18,255
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_norm_init_f32.c * Description: Floating-point NLMS filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup LMS_NORM @{ */ /** @brief Initialization function for floating-point normalized LMS filter. @param[in] S points to an instance of the floating-point LMS filter structure @param[in] numTaps number of filter coefficients @param[in] pCoeffs points to coefficient buffer @param[in] pState points to state buffer @param[in] mu step size that controls filter coefficient updates @param[in] blockSize number of samples to process @return none @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> The initial filter coefficients serve as a starting point for the adaptive filter. <code>pState</code> points to an array of length <code>numTaps+blockSize-1</code> samples, where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_lms_norm_f32()</code>. */ void arm_lms_norm_init_f32( arm_lms_norm_instance_f32 * S, uint16_t numTaps, float32_t * pCoeffs, float32_t * pState, float32_t mu, uint32_t blockSize) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer and size is always blockSize + numTaps - 1 */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(float32_t)); /* Assign state pointer */ S->pState = pState; /* Assign Step size value */ S->mu = mu; /* Initialise Energy to zero */ S->energy = 0.0f; /* Initialise x0 to zero */ S->x0 = 0.0f; } /** @} end of LMS_NORM group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_norm_init_f32.c
C
apache-2.0
2,918
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_norm_init_q15.c * Description: Q15 NLMS filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" #include "arm_common_tables.h" /** @addtogroup LMS_NORM @{ */ /** @brief Initialization function for Q15 normalized LMS filter. @param[in] S points to an instance of the Q15 normalized LMS filter structure. @param[in] numTaps number of filter coefficients. @param[in] pCoeffs points to coefficient buffer. @param[in] pState points to state buffer. @param[in] mu step size that controls filter coefficient updates. @param[in] blockSize number of samples to process. @param[in] postShift bit shift applied to coefficients. @return none @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> The initial filter coefficients serve as a starting point for the adaptive filter. <code>pState</code> points to the array of state variables and size of array is <code>numTaps+blockSize-1</code> samples, where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_lms_norm_q15()</code>. */ void arm_lms_norm_init_q15( arm_lms_norm_instance_q15 * S, uint16_t numTaps, q15_t * pCoeffs, q15_t * pState, q15_t mu, uint32_t blockSize, uint8_t postShift) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer and size is always blockSize + numTaps - 1 */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q15_t)); /* Assign post Shift value applied to coefficients */ S->postShift = postShift; /* Assign state pointer */ S->pState = pState; /* Assign Step size value */ S->mu = mu; /* Initialize reciprocal pointer table */ S->recipTable = (q15_t *) armRecipTableQ15; /* Initialise Energy to zero */ S->energy = 0; /* Initialise x0 to zero */ S->x0 = 0; } /** @} end of LMS_NORM group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_norm_init_q15.c
C
apache-2.0
3,193
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_norm_init_q31.c * Description: Q31 NLMS filter initialization function * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" #include "arm_common_tables.h" /** @addtogroup LMS_NORM @{ */ /** @brief Initialization function for Q31 normalized LMS filter. @param[in] S points to an instance of the Q31 normalized LMS filter structure. @param[in] numTaps number of filter coefficients. @param[in] pCoeffs points to coefficient buffer. @param[in] pState points to state buffer. @param[in] mu step size that controls filter coefficient updates. @param[in] blockSize number of samples to process. @param[in] postShift bit shift applied to coefficients. @return none @par Details <code>pCoeffs</code> points to the array of filter coefficients stored in time reversed order: <pre> {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]} </pre> The initial filter coefficients serve as a starting point for the adaptive filter. <code>pState</code> points to an array of length <code>numTaps+blockSize-1</code> samples, where <code>blockSize</code> is the number of input samples processed by each call to <code>arm_lms_norm_q31()</code>. */ void arm_lms_norm_init_q31( arm_lms_norm_instance_q31 * S, uint16_t numTaps, q31_t * pCoeffs, q31_t * pState, q31_t mu, uint32_t blockSize, uint8_t postShift) { /* Assign filter taps */ S->numTaps = numTaps; /* Assign coefficient pointer */ S->pCoeffs = pCoeffs; /* Clear state buffer and size is always blockSize + numTaps - 1 */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q31_t)); /* Assign post Shift value applied to coefficients */ S->postShift = postShift; /* Assign state pointer */ S->pState = pState; /* Assign Step size value */ S->mu = mu; /* Initialize reciprocal pointer table */ S->recipTable = (q31_t *) armRecipTableQ31; /* Initialise Energy to zero */ S->energy = 0; /* Initialise x0 to zero */ S->x0 = 0; } /** @} end of LMS_NORM group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_norm_init_q31.c
C
apache-2.0
3,143
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_norm_q15.c * Description: Processing function for Q15 normalized LMS filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup LMS_NORM @{ */ /** @brief Processing function for Q15 normalized LMS filter. @param[in] S points to an instance of the Q15 normalized LMS filter structure @param[in] pSrc points to the block of input data @param[in] pRef points to the block of reference data @param[out] pOut points to the block of output data @param[out] pErr points to the block of error data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using a 64-bit internal accumulator. Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits. Lastly, the accumulator is saturated to yield a result in 1.15 format. @par In this filter, filter coefficients are updated for each sample and the updation of filter cofficients are saturted. */ void arm_lms_norm_q15( arm_lms_norm_instance_q15 * S, const q15_t * pSrc, q15_t * pRef, q15_t * pOut, q15_t * pErr, uint32_t blockSize) { q15_t *pState = S->pState; /* State pointer */ q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *pStateCurnt; /* Points to the current sample of the state */ q15_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ q15_t mu = S->mu; /* Adaptive factor */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ q63_t acc; /* Accumulator */ q31_t energy; /* Energy of the input */ q15_t e = 0, d = 0; /* Error, reference data sample */ q15_t w = 0, in; /* Weight factor and state */ q15_t x0; /* Temporary variable to hold input sample */ q15_t errorXmu, oneByEnergy; /* Temporary variables to store error and mu product and reciprocal of energy */ q15_t postShift; /* Post shift to be applied to weight after reciprocal calculation */ q31_t coef; /* Temporary variable for coefficient */ q31_t acc_l, acc_h; /* Temporary input */ int32_t lShift = (15 - (int32_t) S->postShift); /* Post shift */ int32_t uShift = (32 - lShift); energy = S->energy; x0 = S->x0; /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); /* initialise loop count */ blkCnt = blockSize; while (blkCnt > 0U) { /* Copy the new input sample into the state buffer */ *pStateCurnt++ = *pSrc; /* Initialize pState pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Read the sample from input buffer */ in = *pSrc++; /* Update the energy calculation */ energy -= (((q31_t) x0 * (x0)) >> 15); energy += (((q31_t) in * (in)) >> 15); /* Set the accumulator to zero */ acc = 0; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ /* acc += b[N] * x[n-N] + b[N-1] * x[n-N-1] */ acc = __SMLALD(read_q15x2_ia (&px), read_q15x2_ia (&pb), acc); acc = __SMLALD(read_q15x2_ia (&px), read_q15x2_ia (&pb), acc); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ acc += (q63_t) (((q31_t) (*px++) * (*pb++))); /* Decrement the loop counter */ tapCnt--; } /* Calc lower part of acc */ acc_l = acc & 0xffffffff; /* Calc upper part of acc */ acc_h = (acc >> 32) & 0xffffffff; /* Apply shift for lower part of acc and upper part of acc */ acc = (uint32_t) acc_l >> lShift | acc_h << uShift; /* Converting the result to 1.15 format and saturate the output */ acc = __SSAT(acc, 16U); /* Store the result from accumulator into the destination buffer. */ *pOut++ = (q15_t) acc; /* Compute and store error */ d = *pRef++; e = d - (q15_t) acc; *pErr++ = e; /* Calculation of 1/energy */ postShift = arm_recip_q15((q15_t) energy + DELTA_Q15, &oneByEnergy, S->recipTable); /* Calculation of e * mu value */ errorXmu = (q15_t) (((q31_t) e * mu) >> 15); /* Calculation of (e * mu) * (1/energy) value */ acc = (((q31_t) errorXmu * oneByEnergy) >> (15 - postShift)); /* Weighting factor for the normalized version */ w = (q15_t) __SSAT((q31_t) acc, 16); /* Initialize pState pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; /* Update filter coefficients */ while (tapCnt > 0U) { coef = (q31_t) *pb + (((q31_t) w * (*px++)) >> 15); *pb++ = (q15_t) __SSAT(coef, 16); coef = (q31_t) *pb + (((q31_t) w * (*px++)) >> 15); *pb++ = (q15_t) __SSAT(coef, 16); coef = (q31_t) *pb + (((q31_t) w * (*px++)) >> 15); *pb++ = (q15_t) __SSAT(coef, 16); coef = (q31_t) *pb + (((q31_t) w * (*px++)) >> 15); *pb++ = (q15_t) __SSAT(coef, 16); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ coef = (q31_t) *pb + (((q31_t) w * (*px++)) >> 15); *pb++ = (q15_t) __SSAT(coef, 16); /* Decrement loop counter */ tapCnt--; } x0 = *pState; /* Advance state pointer by 1 for the next sample */ pState = pState + 1; /* Decrement loop counter */ blkCnt--; } /* Save energy and x0 values for the next frame */ S->energy = (q15_t) energy; S->x0 = x0; /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the pState buffer */ pStateCurnt = S->pState; /* copy data */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = (numTaps - 1U) >> 2U; while (tapCnt > 0U) { write_q15x2_ia (&pStateCurnt, read_q15x2_ia (&pState)); write_q15x2_ia (&pStateCurnt, read_q15x2_ia (&pState)); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } /** @} end of LMS_NORM group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_norm_q15.c
C
apache-2.0
9,383
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_norm_q31.c * Description: Processing function for the Q31 NLMS filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup LMS_NORM @{ */ /** @brief Processing function for Q31 normalized LMS filter. @param[in] S points to an instance of the Q31 normalized LMS filter structure @param[in] pSrc points to the block of input data @param[in] pRef points to the block of reference data @param[out] pOut points to the block of output data @param[out] pErr points to the block of error data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. Thus, if the accumulator result overflows it wraps around rather than clip. In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits. The reference signal should not be scaled down. After all multiply-accumulates are performed, the 2.62 accumulator is shifted and saturated to 1.31 format to yield the final result. The output signal and error signal are in 1.31 format. @par In this filter, filter coefficients are updated for each sample and the updation of filter cofficients are saturted. */ void arm_lms_norm_q31( arm_lms_norm_instance_q31 * S, const q31_t * pSrc, q31_t * pRef, q31_t * pOut, q31_t * pErr, uint32_t blockSize) { q31_t *pState = S->pState; /* State pointer */ q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q31_t *pStateCurnt; /* Points to the current sample of the state */ q31_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ q31_t mu = S->mu; /* Adaptive factor */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ q63_t acc; /* Accumulator */ q63_t energy; /* Energy of the input */ q31_t e = 0; /* Error data sample */ q31_t w = 0, in; /* Weight factor and state */ q31_t x0; /* Temporary variable to hold input sample */ q31_t errorXmu, oneByEnergy; /* Temporary variables to store error and mu product and reciprocal of energy */ q31_t postShift; /* Post shift to be applied to weight after reciprocal calculation */ q31_t coef; /* Temporary variable for coef */ q31_t acc_l, acc_h; /* Temporary input */ uint32_t uShift = ((uint32_t) S->postShift + 1U); uint32_t lShift = 32U - uShift; /* Shift to be applied to the output */ energy = S->energy; x0 = S->x0; /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); /* initialise loop count */ blkCnt = blockSize; while (blkCnt > 0U) { /* Copy the new input sample into the state buffer */ *pStateCurnt++ = *pSrc; /* Initialize pState pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Read the sample from input buffer */ in = *pSrc++; /* Update the energy calculation */ energy = (q31_t) ((((q63_t) energy << 32) - (((q63_t) x0 * x0) << 1)) >> 32); energy = (q31_t) (((((q63_t) in * in) << 1) + (energy << 32)) >> 32); /* Set the accumulator to zero */ acc = 0; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ /* acc += b[N] * x[n-N] */ acc += ((q63_t) (*px++)) * (*pb++); /* acc += b[N-1] * x[n-N-1] */ acc += ((q63_t) (*px++)) * (*pb++); /* acc += b[N-2] * x[n-N-2] */ acc += ((q63_t) (*px++)) * (*pb++); /* acc += b[N-3] * x[n-N-3] */ acc += ((q63_t) (*px++)) * (*pb++); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ acc += ((q63_t) (*px++)) * (*pb++); /* Decrement the loop counter */ tapCnt--; } /* Converting the result to 1.31 format */ /* Calc lower part of acc */ acc_l = acc & 0xffffffff; /* Calc upper part of acc */ acc_h = (acc >> 32) & 0xffffffff; acc = (uint32_t) acc_l >> lShift | acc_h << uShift; /* Store the result from accumulator into the destination buffer. */ *pOut++ = (q31_t) acc; /* Compute and store error */ e = *pRef++ - (q31_t) acc; *pErr++ = e; /* Calculates the reciprocal of energy */ postShift = arm_recip_q31(energy + DELTA_Q31, &oneByEnergy, &S->recipTable[0]); /* Calculation of product of (e * mu) */ errorXmu = (q31_t) (((q63_t) e * mu) >> 31); /* Weighting factor for the normalized version */ w = clip_q63_to_q31(((q63_t) errorXmu * oneByEnergy) >> (31 - postShift)); /* Initialize pState pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; /* Update filter coefficients */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ /* coef is in 2.30 format */ coef = (q31_t) (((q63_t) w * (*px++)) >> (32)); /* get coef in 1.31 format by left shifting */ *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); /* update coefficient buffer to next coefficient */ pb++; coef = (q31_t) (((q63_t) w * (*px++)) >> (32)); *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); pb++; coef = (q31_t) (((q63_t) w * (*px++)) >> (32)); *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); pb++; coef = (q31_t) (((q63_t) w * (*px++)) >> (32)); *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); pb++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ coef = (q31_t) (((q63_t) w * (*px++)) >> (32)); *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); pb++; /* Decrement loop counter */ tapCnt--; } /* Read the sample from state buffer */ x0 = *pState; /* Advance state pointer by 1 for the next sample */ pState = pState + 1; /* Decrement loop counter */ blkCnt--; } /* Save energy and x0 values for the next frame */ S->energy = (q31_t) energy; S->x0 = x0; /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the pState buffer */ pStateCurnt = S->pState; /* copy data */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = (numTaps - 1U) >> 2U; while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } /** @} end of LMS_NORM group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_norm_q31.c
C
apache-2.0
9,805
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_q15.c * Description: Processing function for Q15 LMS filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup LMS @{ */ /** @brief Processing function for Q15 LMS filter. @param[in] S points to an instance of the Q15 LMS filter structure @param[in] pSrc points to the block of input data @param[in] pRef points to the block of reference data @param[out] pOut points to the block of output data @param[out] pErr points to the block of error data @param[in] blockSize number of samples to process @return none @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. Both coefficients and state variables are represented in 1.15 format and multiplications yield a 2.30 result. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format. There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved. After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits. Lastly, the accumulator is saturated to yield a result in 1.15 format. @par In this filter, filter coefficients are updated for each sample and the updation of filter cofficients are saturted. */ void arm_lms_q15( const arm_lms_instance_q15 * S, const q15_t * pSrc, q15_t * pRef, q15_t * pOut, q15_t * pErr, uint32_t blockSize) { q15_t *pState = S->pState; /* State pointer */ q15_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q15_t *pStateCurnt; /* Points to the current sample of the state */ q15_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ q15_t mu = S->mu; /* Adaptive factor */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ q63_t acc; /* Accumulator */ q15_t e = 0; /* Error of data sample */ q15_t alpha; /* Intermediate constant for taps update */ q31_t coef; /* Temporary variable for coefficient */ q31_t acc_l, acc_h; /* Temporary input */ int32_t lShift = (15 - (int32_t) S->postShift); /* Post shift */ int32_t uShift = (32 - lShift); /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); /* initialise loop count */ blkCnt = blockSize; while (blkCnt > 0U) { /* Copy the new input sample into the state buffer */ *pStateCurnt++ = *pSrc++; /* Initialize pState pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Set the accumulator to zero */ acc = 0; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ /* acc += b[N] * x[n-N] + b[N-1] * x[n-N-1] */ acc = __SMLALD(read_q15x2_ia (&px), read_q15x2_ia (&pb), acc); acc = __SMLALD(read_q15x2_ia (&px), read_q15x2_ia (&pb), acc); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ acc += (q63_t) (((q31_t) (*px++) * (*pb++))); /* Decrement the loop counter */ tapCnt--; } /* Calc lower part of acc */ acc_l = acc & 0xffffffff; /* Calc upper part of acc */ acc_h = (acc >> 32) & 0xffffffff; /* Apply shift for lower part of acc and upper part of acc */ acc = (uint32_t) acc_l >> lShift | acc_h << uShift; /* Converting the result to 1.15 format and saturate the output */ acc = __SSAT(acc, 16U); /* Store the result from accumulator into the destination buffer. */ *pOut++ = (q15_t) acc; /* Compute and store error */ e = *pRef++ - (q15_t) acc; *pErr++ = (q15_t) e; /* Compute alpha i.e. intermediate constant for taps update */ alpha = (q15_t) (((q31_t) e * (mu)) >> 15); /* Initialize pState pointer */ /* Advance state pointer by 1 for the next sample */ px = pState++; /* Initialize coefficient pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; /* Update filter coefficients */ while (tapCnt > 0U) { coef = (q31_t) *pb + (((q31_t) alpha * (*px++)) >> 15); *pb++ = (q15_t) __SSAT((coef), 16); coef = (q31_t) *pb + (((q31_t) alpha * (*px++)) >> 15); *pb++ = (q15_t) __SSAT((coef), 16); coef = (q31_t) *pb + (((q31_t) alpha * (*px++)) >> 15); *pb++ = (q15_t) __SSAT((coef), 16); coef = (q31_t) *pb + (((q31_t) alpha * (*px++)) >> 15); *pb++ = (q15_t) __SSAT((coef), 16); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ coef = (q31_t) *pb + (((q31_t) alpha * (*px++)) >> 15); *pb++ = (q15_t) __SSAT((coef), 16); /* Decrement loop counter */ tapCnt--; } /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the pState buffer */ pStateCurnt = S->pState; /* copy data */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = (numTaps - 1U) >> 2U; while (tapCnt > 0U) { write_q15x2_ia (&pStateCurnt, read_q15x2_ia (&pState)); write_q15x2_ia (&pStateCurnt, read_q15x2_ia (&pState)); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } /** @} end of LMS group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_q15.c
C
apache-2.0
8,184
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_lms_q31.c * Description: Processing function for the Q31 LMS filter * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupFilters */ /** @addtogroup LMS @{ */ /** @brief Processing function for Q31 LMS filter. @param[in] S points to an instance of the Q31 LMS filter structure. @param[in] pSrc points to the block of input data. @param[in] pRef points to the block of reference data. @param[out] pOut points to the block of output data. @param[out] pErr points to the block of error data. @param[in] blockSize number of samples to process. @return none @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. Thus, if the accumulator result overflows it wraps around rather than clips. In order to avoid overflows completely the input signal must be scaled down by log2(numTaps) bits. The reference signal should not be scaled down. After all multiply-accumulates are performed, the 2.62 accumulator is shifted and saturated to 1.31 format to yield the final result. The output signal and error signal are in 1.31 format. @par In this filter, filter coefficients are updated for each sample and the updation of filter cofficients are saturted. */ void arm_lms_q31( const arm_lms_instance_q31 * S, const q31_t * pSrc, q31_t * pRef, q31_t * pOut, q31_t * pErr, uint32_t blockSize) { q31_t *pState = S->pState; /* State pointer */ q31_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */ q31_t *pStateCurnt; /* Points to the current sample of the state */ q31_t *px, *pb; /* Temporary pointers for state and coefficient buffers */ q31_t mu = S->mu; /* Adaptive factor */ uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */ uint32_t tapCnt, blkCnt; /* Loop counters */ q63_t acc; /* Accumulator */ q31_t e = 0; /* Error of data sample */ q31_t alpha; /* Intermediate constant for taps update */ q31_t coef; /* Temporary variable for coef */ q31_t acc_l, acc_h; /* Temporary input */ uint32_t uShift = ((uint32_t) S->postShift + 1U); uint32_t lShift = 32U - uShift; /* Shift to be applied to the output */ /* S->pState points to buffer which contains previous frame (numTaps - 1) samples */ /* pStateCurnt points to the location where the new input data should be written */ pStateCurnt = &(S->pState[(numTaps - 1U)]); /* initialise loop count */ blkCnt = blockSize; while (blkCnt > 0U) { /* Copy the new input sample into the state buffer */ *pStateCurnt++ = *pSrc++; /* Initialize pState pointer */ px = pState; /* Initialize coefficient pointer */ pb = pCoeffs; /* Set the accumulator to zero */ acc = 0; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; while (tapCnt > 0U) { /* Perform the multiply-accumulate */ /* acc += b[N] * x[n-N] */ acc += ((q63_t) (*px++)) * (*pb++); /* acc += b[N-1] * x[n-N-1] */ acc += ((q63_t) (*px++)) * (*pb++); /* acc += b[N-2] * x[n-N-2] */ acc += ((q63_t) (*px++)) * (*pb++); /* acc += b[N-3] * x[n-N-3] */ acc += ((q63_t) (*px++)) * (*pb++); /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ acc += ((q63_t) (*px++)) * (*pb++); /* Decrement the loop counter */ tapCnt--; } /* Converting the result to 1.31 format */ /* Calc lower part of acc */ acc_l = acc & 0xffffffff; /* Calc upper part of acc */ acc_h = (acc >> 32) & 0xffffffff; acc = (uint32_t) acc_l >> lShift | acc_h << uShift; /* Store the result from accumulator into the destination buffer. */ *pOut++ = (q31_t) acc; /* Compute and store error */ e = *pRef++ - (q31_t) acc; *pErr++ = e; /* Compute alpha i.e. intermediate constant for taps update */ alpha = (q31_t) (((q63_t) e * mu) >> 31); /* Initialize pState pointer */ /* Advance state pointer by 1 for the next sample */ px = pState++; /* Initialize coefficient pointer */ pb = pCoeffs; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = numTaps >> 2U; /* Update filter coefficients */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ /* coef is in 2.30 format */ coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32)); /* get coef in 1.31 format by left shifting */ *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); /* update coefficient buffer to next coefficient */ pb++; coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32)); *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); pb++; coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32)); *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); pb++; coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32)); *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); pb++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = numTaps % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = numTaps; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { /* Perform the multiply-accumulate */ coef = (q31_t) (((q63_t) alpha * (*px++)) >> (32)); *pb = clip_q63_to_q31((q63_t) * pb + (coef << 1U)); pb++; /* Decrement loop counter */ tapCnt--; } /* Decrement loop counter */ blkCnt--; } /* Processing is complete. Now copy the last numTaps - 1 samples to the start of the state buffer. This prepares the state buffer for the next function call. */ /* Points to the start of the pState buffer */ pStateCurnt = S->pState; /* copy data */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 taps at a time. */ tapCnt = (numTaps - 1U) >> 2U; while (tapCnt > 0U) { *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } /* Loop unrolling: Compute remaining taps */ tapCnt = (numTaps - 1U) % 0x4U; #else /* Initialize tapCnt with number of samples */ tapCnt = (numTaps - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (tapCnt > 0U) { *pStateCurnt++ = *pState++; /* Decrement loop counter */ tapCnt--; } } /** @} end of LMS group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/FilteringFunctions/arm_lms_q31.c
C
apache-2.0
8,659
cmake_minimum_required (VERSION 3.6) project(CMSISDSPMatrix) file(GLOB SRC "./*_*.c") add_library(CMSISDSPMatrix STATIC ${SRC}) configdsp(CMSISDSPMatrix ..) ### Includes target_include_directories(CMSISDSPMatrix PUBLIC "${DSP}/../../Include")
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/CMakeLists.txt
CMake
apache-2.0
252
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: MatrixFunctions.c * Description: Combination of all matrix function source files. * * $Date: 18. March 2019 * $Revision: V1.0.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_mat_add_f32.c" #include "arm_mat_add_q15.c" #include "arm_mat_add_q31.c" #include "arm_mat_cmplx_mult_f32.c" #include "arm_mat_cmplx_mult_q15.c" #include "arm_mat_cmplx_mult_q31.c" #include "arm_mat_init_f32.c" #include "arm_mat_init_q15.c" #include "arm_mat_init_q31.c" #include "arm_mat_inverse_f32.c" #include "arm_mat_inverse_f64.c" #include "arm_mat_mult_f32.c" #include "arm_mat_mult_fast_q15.c" #include "arm_mat_mult_fast_q31.c" #include "arm_mat_mult_q15.c" #include "arm_mat_mult_q31.c" #include "arm_mat_scale_f32.c" #include "arm_mat_scale_q15.c" #include "arm_mat_scale_q31.c" #include "arm_mat_sub_f32.c" #include "arm_mat_sub_q15.c" #include "arm_mat_sub_q31.c" #include "arm_mat_trans_f32.c" #include "arm_mat_trans_q15.c" #include "arm_mat_trans_q31.c"
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/MatrixFunctions.c
C
apache-2.0
1,834
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_add_f32.c * Description: Floating-point matrix addition * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @defgroup MatrixAdd Matrix Addition Adds two matrices. \image html MatrixAddition.gif "Addition of two 3 x 3 matrices" The functions check to make sure that <code>pSrcA</code>, <code>pSrcB</code>, and <code>pDst</code> have the same number of rows and columns. */ /** @addtogroup MatrixAdd @{ */ /** @brief Floating-point matrix addition. @param[in] pSrcA points to first input matrix structure @param[in] pSrcB points to second input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed */ #if defined(ARM_MATH_NEON) /* Neon version is assuming the matrix is small enough. So no blocking is used for taking into account cache effects. For big matrix, there exist better libraries for Neon. */ arm_status arm_mat_add_f32( const arm_matrix_instance_f32 * pSrcA, const arm_matrix_instance_f32 * pSrcB, arm_matrix_instance_f32 * pDst) { float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */ float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ float32_t inA1, inA2, inB1, inB2, out1, out2; /* temporary variables */ uint32_t numSamples; /* total number of elements in the matrix */ uint32_t blkCnt; /* loop counters */ arm_status status; /* status of matrix addition */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numRows != pSrcB->numRows) || (pSrcA->numCols != pSrcB->numCols) || (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols)) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif { float32x4_t vec1; float32x4_t vec2; float32x4_t res; /* Total number of samples in the input matrix */ numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols; blkCnt = numSamples >> 2U; /* Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) + B(m,n) */ /* Add and then store the results in the destination buffer. */ vec1 = vld1q_f32(pIn1); vec2 = vld1q_f32(pIn2); res = vaddq_f32(vec1, vec2); vst1q_f32(pOut, res); /* update pointers to process next samples */ pIn1 += 4U; pIn2 += 4U; pOut += 4U; /* Decrement the loop counter */ blkCnt--; } /* If the numSamples is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ blkCnt = numSamples % 0x4U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) + B(m,n) */ /* Add and then store the results in the destination buffer. */ *pOut++ = (*pIn1++) + (*pIn2++); /* Decrement the loop counter */ blkCnt--; } /* set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #else arm_status arm_mat_add_f32( const arm_matrix_instance_f32 * pSrcA, const arm_matrix_instance_f32 * pSrcB, arm_matrix_instance_f32 * pDst) { float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */ float32_t *pInB = pSrcB->pData; /* input data matrix pointer B */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ uint32_t numSamples; /* total number of elements in the matrix */ uint32_t blkCnt; /* loop counters */ arm_status status; /* status of matrix addition */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numRows != pSrcB->numRows) || (pSrcA->numCols != pSrcB->numCols) || (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Total number of samples in input matrix */ numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = numSamples >> 2U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) + B(m,n) */ /* Add and store result in destination buffer. */ *pOut++ = *pInA++ + *pInB++; *pOut++ = *pInA++ + *pInB++; *pOut++ = *pInA++ + *pInB++; *pOut++ = *pInA++ + *pInB++; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = numSamples % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = numSamples; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) + B(m,n) */ /* Add and store result in destination buffer. */ *pOut++ = *pInA++ + *pInB++; /* Decrement loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of MatrixAdd group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_add_f32.c
C
apache-2.0
6,652
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_add_q15.c * Description: Q15 matrix addition * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixAdd @{ */ /** @brief Q15 matrix addition. @param[in] pSrcA points to first input matrix structure @param[in] pSrcB points to second input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The function uses saturating arithmetic. Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated. */ arm_status arm_mat_add_q15( const arm_matrix_instance_q15 * pSrcA, const arm_matrix_instance_q15 * pSrcB, arm_matrix_instance_q15 * pDst) { q15_t *pInA = pSrcA->pData; /* input data matrix pointer A */ q15_t *pInB = pSrcB->pData; /* input data matrix pointer B */ q15_t *pOut = pDst->pData; /* output data matrix pointer */ uint32_t numSamples; /* total number of elements in the matrix */ uint32_t blkCnt; /* loop counters */ arm_status status; /* status of matrix addition */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numRows != pSrcB->numRows) || (pSrcA->numCols != pSrcB->numCols) || (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Total number of samples in input matrix */ numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = numSamples >> 2U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) + B(m,n) */ /* Add, saturate and store result in destination buffer. */ #if defined (ARM_MATH_DSP) write_q15x2_ia (&pOut, __QADD16(read_q15x2_ia (&pInA), read_q15x2_ia (&pInB))); write_q15x2_ia (&pOut, __QADD16(read_q15x2_ia (&pInA), read_q15x2_ia (&pInB))); #else *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16); *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16); *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16); *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16); #endif /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = numSamples % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = numSamples; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) + B(m,n) */ /* Add, saturate and store result in destination buffer. */ #if defined (ARM_MATH_DSP) *pOut++ = (q15_t) __QADD16(*pInA++, *pInB++); #else *pOut++ = (q15_t) __SSAT(((q31_t) *pInA++ + *pInB++), 16); #endif /* Decrement loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixAdd group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_add_q15.c
C
apache-2.0
4,468
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_add_q31.c * Description: Q31 matrix addition * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixAdd @{ */ /** @brief Q31 matrix addition. @param[in] pSrcA points to first input matrix structure @param[in] pSrcB points to second input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The function uses saturating arithmetic. Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] are saturated. */ arm_status arm_mat_add_q31( const arm_matrix_instance_q31 * pSrcA, const arm_matrix_instance_q31 * pSrcB, arm_matrix_instance_q31 * pDst) { q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */ q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */ q31_t *pOut = pDst->pData; /* output data matrix pointer */ uint32_t numSamples; /* total number of elements in the matrix */ uint32_t blkCnt; /* loop counters */ arm_status status; /* status of matrix addition */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numRows != pSrcB->numRows) || (pSrcA->numCols != pSrcB->numCols) || (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Total number of samples in input matrix */ numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = numSamples >> 2U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) + B(m,n) */ /* Add, saturate and store result in destination buffer. */ *pOut++ = __QADD(*pInA++, *pInB++); *pOut++ = __QADD(*pInA++, *pInB++); *pOut++ = __QADD(*pInA++, *pInB++); *pOut++ = __QADD(*pInA++, *pInB++); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = numSamples % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = numSamples; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) + B(m,n) */ /* Add, saturate and store result in destination buffer. */ *pOut++ = __QADD(*pInA++, *pInB++); /* Decrement loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixAdd group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_add_q31.c
C
apache-2.0
4,012
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_cmplx_mult_f32.c * Description: Floating-point matrix multiplication * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @defgroup CmplxMatrixMult Complex Matrix Multiplication Complex Matrix multiplication is only defined if the number of columns of the first matrix equals the number of rows of the second matrix. Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results in an <code>M x P</code> matrix. @par When matrix size checking is enabled, the functions check: - that the inner dimensions of <code>pSrcA</code> and <code>pSrcB</code> are equal; - that the size of the output matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>. */ /** @addtogroup CmplxMatrixMult @{ */ /** @brief Floating-point Complex matrix multiplication. @param[in] pSrcA points to first input complex matrix structure @param[in] pSrcB points to second input complex matrix structure @param[out] pDst points to output complex matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed */ #if defined(ARM_MATH_NEON) arm_status arm_mat_cmplx_mult_f32( const arm_matrix_instance_f32 * pSrcA, const arm_matrix_instance_f32 * pSrcB, arm_matrix_instance_f32 * pDst) { float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */ float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */ float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ float32_t *px; /* Temporary output data matrix pointer */ uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */ float32_t sumReal1, sumImag1; /* accumulator */ float32_t a0, b0, c0, d0; float32_t a1, a1B,b1, b1B, c1, d1; float32_t sumReal2, sumImag2; /* accumulator */ float32x4x2_t a0V, a1V; float32x4_t accR0,accI0, accR1,accI1,tempR, tempI; float32x2_t accum = vdup_n_f32(0); float32_t *pIn1B = pSrcA->pData; uint16_t col, i = 0U, j, rowCnt, row = numRowsA, colCnt; /* loop counters */ arm_status status; /* status of matrix multiplication */ float32_t sumReal1B, sumImag1B; float32_t sumReal2B, sumImag2B; float32_t *pxB; #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols)) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ rowCnt = row >> 1; /* Row loop */ while (rowCnt > 0U) { /* Output pointer is set to starting address of the row being processed */ px = pOut + 2 * i; pxB = px + 2 * numColsB; /* For every row wise process, the column loop counter is to be initiated */ col = numColsB; /* For every row wise process, the pIn2 pointer is set ** to the starting address of the pSrcB data */ pIn2 = pSrcB->pData; j = 0U; /* Column loop */ while (col > 0U) { /* Set the variable sum, that acts as accumulator, to zero */ sumReal1 = 0.0f; sumImag1 = 0.0f; sumReal1B = 0.0f; sumImag1B = 0.0f; sumReal2 = 0.0f; sumImag2 = 0.0f; sumReal2B = 0.0f; sumImag2B = 0.0f; /* Initiate the pointer pIn1 to point to the starting address of the column being processed */ pIn1 = pInA; pIn1B = pIn1 + 2*numColsA; accR0 = vdupq_n_f32(0.0); accI0 = vdupq_n_f32(0.0); accR1 = vdupq_n_f32(0.0); accI1 = vdupq_n_f32(0.0); /* Compute 4 MACs simultaneously. */ colCnt = numColsA >> 2; /* Matrix multiplication */ while (colCnt > 0U) { /* Reading real part of complex matrix A */ a0V = vld2q_f32(pIn1); // load & separate real/imag pSrcA (de-interleave 2) a1V = vld2q_f32(pIn1B); // load & separate real/imag pSrcA (de-interleave 2) pIn1 += 8; pIn1B += 8; tempR[0] = *pIn2; tempI[0] = *(pIn2 + 1U); pIn2 += 2 * numColsB; tempR[1] = *pIn2; tempI[1] = *(pIn2 + 1U); pIn2 += 2 * numColsB; tempR[2] = *pIn2; tempI[2] = *(pIn2 + 1U); pIn2 += 2 * numColsB; tempR[3] = *pIn2; tempI[3] = *(pIn2 + 1U); pIn2 += 2 * numColsB; accR0 = vmlaq_f32(accR0,a0V.val[0],tempR); accR0 = vmlsq_f32(accR0,a0V.val[1],tempI); accI0 = vmlaq_f32(accI0,a0V.val[1],tempR); accI0 = vmlaq_f32(accI0,a0V.val[0],tempI); accR1 = vmlaq_f32(accR1,a1V.val[0],tempR); accR1 = vmlsq_f32(accR1,a1V.val[1],tempI); accI1 = vmlaq_f32(accI1,a1V.val[1],tempR); accI1 = vmlaq_f32(accI1,a1V.val[0],tempI); /* Decrement the loop count */ colCnt--; } accum = vpadd_f32(vget_low_f32(accR0), vget_high_f32(accR0)); sumReal1 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(accI0), vget_high_f32(accI0)); sumImag1 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(accR1), vget_high_f32(accR1)); sumReal1B += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(accI1), vget_high_f32(accI1)); sumImag1B += accum[0] + accum[1]; /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here. ** No loop unrolling is used. */ colCnt = numColsA & 3; while (colCnt > 0U) { /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */ a1 = *pIn1; a1B = *pIn1B; c1 = *pIn2; b1 = *(pIn1 + 1U); b1B = *(pIn1B + 1U); d1 = *(pIn2 + 1U); sumReal1 += a1 * c1; sumImag1 += b1 * c1; sumReal1B += a1B * c1; sumImag1B += b1B * c1; pIn1 += 2U; pIn1B += 2U; pIn2 += 2 * numColsB; sumReal2 -= b1 * d1; sumImag2 += a1 * d1; sumReal2B -= b1B * d1; sumImag2B += a1B * d1; /* Decrement the loop counter */ colCnt--; } sumReal1 += sumReal2; sumImag1 += sumImag2; sumReal1B += sumReal2B; sumImag1B += sumImag2B; /* Store the result in the destination buffer */ *px++ = sumReal1; *px++ = sumImag1; *pxB++ = sumReal1B; *pxB++ = sumImag1B; /* Update the pointer pIn2 to point to the starting address of the next column */ j++; pIn2 = pSrcB->pData + 2U * j; /* Decrement the column loop counter */ col--; } /* Update the pointer pInA to point to the starting address of the next 2 row */ i = i + 2*numColsB; pInA = pInA + 4 * numColsA; /* Decrement the row loop counter */ rowCnt--; } rowCnt = row & 1; while (rowCnt > 0U) { /* Output pointer is set to starting address of the row being processed */ px = pOut + 2 * i; /* For every row wise process, the column loop counter is to be initiated */ col = numColsB; /* For every row wise process, the pIn2 pointer is set ** to the starting address of the pSrcB data */ pIn2 = pSrcB->pData; j = 0U; /* Column loop */ while (col > 0U) { /* Set the variable sum, that acts as accumulator, to zero */ sumReal1 = 0.0f; sumImag1 = 0.0f; sumReal2 = 0.0f; sumImag2 = 0.0f; /* Initiate the pointer pIn1 to point to the starting address of the column being processed */ pIn1 = pInA; accR0 = vdupq_n_f32(0.0); accI0 = vdupq_n_f32(0.0); /* Compute 4 MACs simultaneously. */ colCnt = numColsA >> 2; /* Matrix multiplication */ while (colCnt > 0U) { /* Reading real part of complex matrix A */ a0V = vld2q_f32(pIn1); // load & separate real/imag pSrcA (de-interleave 2) pIn1 += 8; tempR[0] = *pIn2; tempI[0] = *(pIn2 + 1U); pIn2 += 2 * numColsB; tempR[1] = *pIn2; tempI[1] = *(pIn2 + 1U); pIn2 += 2 * numColsB; tempR[2] = *pIn2; tempI[2] = *(pIn2 + 1U); pIn2 += 2 * numColsB; tempR[3] = *pIn2; tempI[3] = *(pIn2 + 1U); pIn2 += 2 * numColsB; accR0 = vmlaq_f32(accR0,a0V.val[0],tempR); accR0 = vmlsq_f32(accR0,a0V.val[1],tempI); accI0 = vmlaq_f32(accI0,a0V.val[1],tempR); accI0 = vmlaq_f32(accI0,a0V.val[0],tempI); /* Decrement the loop count */ colCnt--; } accum = vpadd_f32(vget_low_f32(accR0), vget_high_f32(accR0)); sumReal1 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(accI0), vget_high_f32(accI0)); sumImag1 += accum[0] + accum[1]; /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here. ** No loop unrolling is used. */ colCnt = numColsA & 3; while (colCnt > 0U) { /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */ a1 = *pIn1; c1 = *pIn2; b1 = *(pIn1 + 1U); d1 = *(pIn2 + 1U); sumReal1 += a1 * c1; sumImag1 += b1 * c1; pIn1 += 2U; pIn2 += 2 * numColsB; sumReal2 -= b1 * d1; sumImag2 += a1 * d1; /* Decrement the loop counter */ colCnt--; } sumReal1 += sumReal2; sumImag1 += sumImag2; /* Store the result in the destination buffer */ *px++ = sumReal1; *px++ = sumImag1; /* Update the pointer pIn2 to point to the starting address of the next column */ j++; pIn2 = pSrcB->pData + 2U * j; /* Decrement the column loop counter */ col--; } /* Update the pointer pInA to point to the starting address of the next row */ i = i + numColsB; pInA = pInA + 2 * numColsA; /* Decrement the row loop counter */ rowCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #else arm_status arm_mat_cmplx_mult_f32( const arm_matrix_instance_f32 * pSrcA, const arm_matrix_instance_f32 * pSrcB, arm_matrix_instance_f32 * pDst) { float32_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */ float32_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */ float32_t *pInA = pSrcA->pData; /* Input data matrix pointer A */ float32_t *pOut = pDst->pData; /* Output data matrix pointer */ float32_t *px; /* Temporary output data matrix pointer */ uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */ float32_t sumReal, sumImag; /* Accumulator */ float32_t a1, b1, c1, d1; uint32_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */ arm_status status; /* status of matrix multiplication */ #if defined (ARM_MATH_LOOPUNROLL) float32_t a0, b0, c0, d0; #endif #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* row loop */ do { /* Output pointer is set to starting address of the row being processed */ px = pOut + 2 * i; /* For every row wise process, the column loop counter is to be initiated */ col = numColsB; /* For every row wise process, the pIn2 pointer is set ** to the starting address of the pSrcB data */ pIn2 = pSrcB->pData; j = 0U; /* column loop */ do { /* Set the variable sum, that acts as accumulator, to zero */ sumReal = 0.0f; sumImag = 0.0f; /* Initiate pointer pIn1 to point to starting address of column being processed */ pIn1 = pInA; #if defined (ARM_MATH_LOOPUNROLL) /* Apply loop unrolling and compute 4 MACs simultaneously. */ colCnt = numColsA >> 2U; /* matrix multiplication */ while (colCnt > 0U) { /* Reading real part of complex matrix A */ a0 = *pIn1; /* Reading real part of complex matrix B */ c0 = *pIn2; /* Reading imaginary part of complex matrix A */ b0 = *(pIn1 + 1U); /* Reading imaginary part of complex matrix B */ d0 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += a0 * c0; sumImag += b0 * c0; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= b0 * d0; sumImag += a0 * d0; /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ /* read real and imag values from pSrcA and pSrcB buffer */ a1 = *(pIn1 ); c1 = *(pIn2 ); b1 = *(pIn1 + 1U); d1 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += a1 * c1; sumImag += b1 * c1; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= b1 * d1; sumImag += a1 * d1; a0 = *(pIn1 ); c0 = *(pIn2 ); b0 = *(pIn1 + 1U); d0 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += a0 * c0; sumImag += b0 * c0; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= b0 * d0; sumImag += a0 * d0; /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ a1 = *(pIn1 ); c1 = *(pIn2 ); b1 = *(pIn1 + 1U); d1 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += a1 * c1; sumImag += b1 * c1; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= b1 * d1; sumImag += a1 * d1; /* Decrement loop count */ colCnt--; } /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here. ** No loop unrolling is used. */ colCnt = numColsA % 0x4U; #else /* Initialize blkCnt with number of samples */ colCnt = numColsA; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ a1 = *(pIn1 ); c1 = *(pIn2 ); b1 = *(pIn1 + 1U); d1 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += a1 * c1; sumImag += b1 * c1; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= b1 * d1; sumImag += a1 * d1; /* Decrement loop counter */ colCnt--; } /* Store result in destination buffer */ *px++ = sumReal; *px++ = sumImag; /* Update pointer pIn2 to point to starting address of next column */ j++; pIn2 = pSrcB->pData + 2U * j; /* Decrement column loop counter */ col--; } while (col > 0U); /* Update pointer pInA to point to starting address of next row */ i = i + numColsB; pInA = pInA + 2 * numColsA; /* Decrement row loop counter */ row--; } while (row > 0U); /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of MatrixMult group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_cmplx_mult_f32.c
C
apache-2.0
18,348
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_cmplx_mat_mult_q15.c * Description: Q15 complex matrix multiplication * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup CmplxMatrixMult @{ */ /** @brief Q15 Complex matrix multiplication. @param[in] pSrcA points to first input complex matrix structure @param[in] pSrcB points to second input complex matrix structure @param[out] pDst points to output complex matrix structure @param[in] pScratch points to an array for storing intermediate results @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Conditions for optimum performance Input, output and state buffers should be aligned by 32-bit @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The inputs to the multiplications are in 1.15 format and multiplications yield a 2.30 result. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format. This approach provides 33 guard bits and there is no risk of overflow. The 34.30 result is then truncated to 34.15 format by discarding the low 15 bits and then saturated to 1.15 format. */ arm_status arm_mat_cmplx_mult_q15( const arm_matrix_instance_q15 * pSrcA, const arm_matrix_instance_q15 * pSrcB, arm_matrix_instance_q15 * pDst, q15_t * pScratch) { q15_t *pSrcBT = pScratch; /* input data matrix pointer for transpose */ q15_t *pInA = pSrcA->pData; /* input data matrix pointer A of Q15 type */ q15_t *pInB = pSrcB->pData; /* input data matrix pointer B of Q15 type */ q15_t *px; /* Temporary output data matrix pointer */ uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */ uint16_t numRowsB = pSrcB->numRows; /* number of rows of input matrix A */ q63_t sumReal, sumImag; /* accumulator */ uint32_t col, i = 0U, row = numRowsB, colCnt; /* Loop counters */ arm_status status; /* Status of matrix multiplication */ #if defined (ARM_MATH_DSP) q31_t prod1, prod2; q31_t pSourceA, pSourceB; #else q15_t a, b, c, d; #endif /* #if defined (ARM_MATH_DSP) */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Matrix transpose */ do { /* The pointer px is set to starting address of column being processed */ px = pSrcBT + i; #if defined (ARM_MATH_LOOPUNROLL) /* Apply loop unrolling and exchange the columns with row elements */ col = numColsB >> 2; /* First part of the processing with loop unrolling. Compute 4 outputs at a time. a second loop below computes the remaining 1 to 3 samples. */ while (col > 0U) { /* Read two elements from row */ write_q15x2 (px, read_q15x2_ia (&pInB)); /* Update pointer px to point to next row of transposed matrix */ px += numRowsB * 2; /* Read two elements from row */ write_q15x2 (px, read_q15x2_ia (&pInB)); /* Update pointer px to point to next row of transposed matrix */ px += numRowsB * 2; /* Read two elements from row */ write_q15x2 (px, read_q15x2_ia (&pInB)); /* Update pointer px to point to next row of transposed matrix */ px += numRowsB * 2; /* Read two elements from row */ write_q15x2 (px, read_q15x2_ia (&pInB)); /* Update pointer px to point to next row of transposed matrix */ px += numRowsB * 2; /* Decrement column loop counter */ col--; } /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ col = numColsB % 0x4U; #else /* Initialize blkCnt with number of samples */ col = numColsB; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (col > 0U) { /* Read two elements from row */ write_q15x2 (px, read_q15x2_ia (&pInB)); /* Update pointer px to point to next row of transposed matrix */ px += numRowsB * 2; /* Decrement column loop counter */ col--; } i = i + 2U; /* Decrement row loop counter */ row--; } while (row > 0U); /* Reset variables for usage in following multiplication process */ row = numRowsA; i = 0U; px = pDst->pData; /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* row loop */ do { /* For every row wise process, column loop counter is to be initiated */ col = numColsB; /* For every row wise process, pIn2 pointer is set to starting address of transposed pSrcB data */ pInB = pSrcBT; /* column loop */ do { /* Set variable sum, that acts as accumulator, to zero */ sumReal = 0; sumImag = 0; /* Initiate pointer pInA to point to starting address of column being processed */ pInA = pSrcA->pData + i * 2; /* Apply loop unrolling and compute 2 MACs simultaneously. */ colCnt = numColsA >> 1U; /* matrix multiplication */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ #if defined (ARM_MATH_DSP) /* read real and imag values from pSrcA and pSrcB buffer */ pSourceA = read_q15x2_ia ((q15_t **) &pInA); pSourceB = read_q15x2_ia ((q15_t **) &pInB); /* Multiply and Accumlates */ #ifdef ARM_MATH_BIG_ENDIAN prod1 = -__SMUSD(pSourceA, pSourceB); #else prod1 = __SMUSD(pSourceA, pSourceB); #endif prod2 = __SMUADX(pSourceA, pSourceB); sumReal += (q63_t) prod1; sumImag += (q63_t) prod2; /* read real and imag values from pSrcA and pSrcB buffer */ pSourceA = read_q15x2_ia ((q15_t **) &pInA); pSourceB = read_q15x2_ia ((q15_t **) &pInB); /* Multiply and Accumlates */ #ifdef ARM_MATH_BIG_ENDIAN prod1 = -__SMUSD(pSourceA, pSourceB); #else prod1 = __SMUSD(pSourceA, pSourceB); #endif prod2 = __SMUADX(pSourceA, pSourceB); sumReal += (q63_t) prod1; sumImag += (q63_t) prod2; #else /* #if defined (ARM_MATH_DSP) */ /* read real and imag values from pSrcA buffer */ a = *pInA; b = *(pInA + 1U); /* read real and imag values from pSrcB buffer */ c = *pInB; d = *(pInB + 1U); /* Multiply and Accumlates */ sumReal += (q31_t) a *c; sumImag += (q31_t) a *d; sumReal -= (q31_t) b *d; sumImag += (q31_t) b *c; /* read next real and imag values from pSrcA buffer */ a = *(pInA + 2U); b = *(pInA + 3U); /* read next real and imag values from pSrcB buffer */ c = *(pInB + 2U); d = *(pInB + 3U); /* update pointer */ pInA += 4U; /* Multiply and Accumlates */ sumReal += (q31_t) a * c; sumImag += (q31_t) a * d; sumReal -= (q31_t) b * d; sumImag += (q31_t) b * c; /* update pointer */ pInB += 4U; #endif /* #if defined (ARM_MATH_DSP) */ /* Decrement loop counter */ colCnt--; } /* process odd column samples */ if ((numColsA & 0x1U) > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ #if defined (ARM_MATH_DSP) /* read real and imag values from pSrcA and pSrcB buffer */ pSourceA = read_q15x2_ia ((q15_t **) &pInA); pSourceB = read_q15x2_ia ((q15_t **) &pInB); /* Multiply and Accumlates */ #ifdef ARM_MATH_BIG_ENDIAN prod1 = -__SMUSD(pSourceA, pSourceB); #else prod1 = __SMUSD(pSourceA, pSourceB); #endif prod2 = __SMUADX(pSourceA, pSourceB); sumReal += (q63_t) prod1; sumImag += (q63_t) prod2; #else /* #if defined (ARM_MATH_DSP) */ /* read real and imag values from pSrcA and pSrcB buffer */ a = *pInA++; b = *pInA++; c = *pInB++; d = *pInB++; /* Multiply and Accumlates */ sumReal += (q31_t) a * c; sumImag += (q31_t) a * d; sumReal -= (q31_t) b * d; sumImag += (q31_t) b * c; #endif /* #if defined (ARM_MATH_DSP) */ } /* Saturate and store result in destination buffer */ *px++ = (q15_t) (__SSAT(sumReal >> 15, 16)); *px++ = (q15_t) (__SSAT(sumImag >> 15, 16)); /* Decrement column loop counter */ col--; } while (col > 0U); i = i + numColsA; /* Decrement row loop counter */ row--; } while (row > 0U); /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixMult group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_cmplx_mult_q15.c
C
apache-2.0
10,896
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_cmplx_mult_q31.c * Description: Floating-point matrix multiplication * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup CmplxMatrixMult @{ */ /** @brief Q31 Complex matrix multiplication. @param[in] pSrcA points to first input complex matrix structure @param[in] pSrcB points to second input complex matrix structure @param[out] pDst points to output complex matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. There is no saturation on intermediate additions. Thus, if the accumulator overflows it wraps around and distorts the result. The input signals should be scaled down to avoid intermediate overflows. The input is thus scaled down by log2(numColsA) bits to avoid overflows, as a total of numColsA additions are performed internally. The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result. */ arm_status arm_mat_cmplx_mult_q31( const arm_matrix_instance_q31 * pSrcA, const arm_matrix_instance_q31 * pSrcB, arm_matrix_instance_q31 * pDst) { q31_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */ q31_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */ q31_t *pInA = pSrcA->pData; /* Input data matrix pointer A */ q31_t *pOut = pDst->pData; /* Output data matrix pointer */ q31_t *px; /* Temporary output data matrix pointer */ uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */ q63_t sumReal, sumImag; /* Accumulator */ q31_t a1, b1, c1, d1; uint32_t col, i = 0U, j, row = numRowsA, colCnt; /* loop counters */ arm_status status; /* status of matrix multiplication */ #if defined (ARM_MATH_LOOPUNROLL) q31_t a0, b0, c0, d0; #endif #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* row loop */ do { /* Output pointer is set to starting address of the row being processed */ px = pOut + 2 * i; /* For every row wise process, the column loop counter is to be initiated */ col = numColsB; /* For every row wise process, the pIn2 pointer is set ** to the starting address of the pSrcB data */ pIn2 = pSrcB->pData; j = 0U; /* column loop */ do { /* Set the variable sum, that acts as accumulator, to zero */ sumReal = 0.0; sumImag = 0.0; /* Initiate pointer pIn1 to point to starting address of column being processed */ pIn1 = pInA; #if defined (ARM_MATH_LOOPUNROLL) /* Apply loop unrolling and compute 4 MACs simultaneously. */ colCnt = numColsA >> 2U; /* matrix multiplication */ while (colCnt > 0U) { /* Reading real part of complex matrix A */ a0 = *pIn1; /* Reading real part of complex matrix B */ c0 = *pIn2; /* Reading imaginary part of complex matrix A */ b0 = *(pIn1 + 1U); /* Reading imaginary part of complex matrix B */ d0 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += (q63_t) a0 * c0; sumImag += (q63_t) b0 * c0; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= (q63_t) b0 * d0; sumImag += (q63_t) a0 * d0; /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ /* read real and imag values from pSrcA and pSrcB buffer */ a1 = *(pIn1 ); c1 = *(pIn2 ); b1 = *(pIn1 + 1U); d1 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += (q63_t) a1 * c1; sumImag += (q63_t) b1 * c1; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= (q63_t) b1 * d1; sumImag += (q63_t) a1 * d1; a0 = *(pIn1 ); c0 = *(pIn2 ); b0 = *(pIn1 + 1U); d0 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += (q63_t) a0 * c0; sumImag += (q63_t) b0 * c0; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= (q63_t) b0 * d0; sumImag += (q63_t) a0 * d0; /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ a1 = *(pIn1 ); c1 = *(pIn2 ); b1 = *(pIn1 + 1U); d1 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += (q63_t) a1 * c1; sumImag += (q63_t) b1 * c1; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= (q63_t) b1 * d1; sumImag += (q63_t) a1 * d1; /* Decrement loop count */ colCnt--; } /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here. ** No loop unrolling is used. */ colCnt = numColsA % 0x4U; #else /* Initialize blkCnt with number of samples */ colCnt = numColsA; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ a1 = *(pIn1 ); c1 = *(pIn2 ); b1 = *(pIn1 + 1U); d1 = *(pIn2 + 1U); /* Multiply and Accumlates */ sumReal += (q63_t) a1 * c1; sumImag += (q63_t) b1 * c1; /* update pointers */ pIn1 += 2U; pIn2 += 2 * numColsB; /* Multiply and Accumlates */ sumReal -= (q63_t) b1 * d1; sumImag += (q63_t) a1 * d1; /* Decrement loop counter */ colCnt--; } /* Store result in destination buffer */ *px++ = (q31_t) clip_q63_to_q31(sumReal >> 31); *px++ = (q31_t) clip_q63_to_q31(sumImag >> 31); /* Update pointer pIn2 to point to starting address of next column */ j++; pIn2 = pSrcB->pData + 2U * j; /* Decrement column loop counter */ col--; } while (col > 0U); /* Update pointer pInA to point to starting address of next row */ i = i + numColsB; pInA = pInA + 2 * numColsA; /* Decrement row loop counter */ row--; } while (row > 0U); /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixMult group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_cmplx_mult_q31.c
C
apache-2.0
8,938
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_init_f32.c * Description: Floating-point matrix initialization * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @defgroup MatrixInit Matrix Initialization Initializes the underlying matrix data structure. The functions set the <code>numRows</code>, <code>numCols</code>, and <code>pData</code> fields of the matrix data structure. */ /** @addtogroup MatrixInit @{ */ /** @brief Floating-point matrix initialization. @param[in,out] S points to an instance of the floating-point matrix structure @param[in] nRows number of rows in the matrix @param[in] nColumns number of columns in the matrix @param[in] pData points to the matrix data array @return none */ void arm_mat_init_f32( arm_matrix_instance_f32 * S, uint16_t nRows, uint16_t nColumns, float32_t * pData) { /* Assign Number of Rows */ S->numRows = nRows; /* Assign Number of Columns */ S->numCols = nColumns; /* Assign Data pointer */ S->pData = pData; } /** @} end of MatrixInit group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_init_f32.c
C
apache-2.0
2,042
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_init_q15.c * Description: Q15 matrix initialization * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixInit @{ */ /** @brief Q15 matrix initialization. @param[in,out] S points to an instance of the floating-point matrix structure @param[in] nRows number of rows in the matrix @param[in] nColumns number of columns in the matrix @param[in] pData points to the matrix data array @return none */ void arm_mat_init_q15( arm_matrix_instance_q15 * S, uint16_t nRows, uint16_t nColumns, q15_t * pData) { /* Assign Number of Rows */ S->numRows = nRows; /* Assign Number of Columns */ S->numCols = nColumns; /* Assign Data pointer */ S->pData = pData; } /** @} end of MatrixInit group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_init_q15.c
C
apache-2.0
1,776
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_init_q31.c * Description: Q31 matrix initialization * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @defgroup MatrixInit Matrix Initialization */ /** @addtogroup MatrixInit @{ */ /** @brief Q31 matrix initialization. @param[in,out] S points to an instance of the Q31 matrix structure @param[in] nRows number of rows in the matrix @param[in] nColumns number of columns in the matrix @param[in] pData points to the matrix data array @return none */ void arm_mat_init_q31( arm_matrix_instance_q31 * S, uint16_t nRows, uint16_t nColumns, q31_t * pData) { /* Assign Number of Rows */ S->numRows = nRows; /* Assign Number of Columns */ S->numCols = nColumns; /* Assign Data pointer */ S->pData = pData; } /** @} end of MatrixInit group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_init_q31.c
C
apache-2.0
1,821
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_inverse_f32.c * Description: Floating-point matrix inverse * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @defgroup MatrixInv Matrix Inverse Computes the inverse of a matrix. The inverse is defined only if the input matrix is square and non-singular (the determinant is non-zero). The function checks that the input and output matrices are square and of the same size. Matrix inversion is numerically sensitive and the CMSIS DSP library only supports matrix inversion of floating-point matrices. @par Algorithm The Gauss-Jordan method is used to find the inverse. The algorithm performs a sequence of elementary row-operations until it reduces the input matrix to an identity matrix. Applying the same sequence of elementary row-operations to an identity matrix yields the inverse matrix. If the input matrix is singular, then the algorithm terminates and returns error status <code>ARM_MATH_SINGULAR</code>. \image html MatrixInverse.gif "Matrix Inverse of a 3 x 3 matrix using Gauss-Jordan Method" */ /** @addtogroup MatrixInv @{ */ /** @brief Floating-point matrix inverse. @param[in] pSrc points to input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed - \ref ARM_MATH_SINGULAR : Input matrix is found to be singular (non-invertible) */ #if defined(ARM_MATH_NEON) arm_status arm_mat_inverse_f32( const arm_matrix_instance_f32 * pSrc, arm_matrix_instance_f32 * pDst) { float32_t *pIn = pSrc->pData; /* input data matrix pointer */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ float32_t *pInT1, *pInT2; /* Temporary input data matrix pointer */ float32_t *pOutT1, *pOutT2; /* Temporary output data matrix pointer */ float32_t *pPivotRowIn, *pPRT_in, *pPivotRowDst, *pPRT_pDst; /* Temporary input and output data matrix pointer */ uint32_t numRows = pSrc->numRows; /* Number of rows in the matrix */ uint32_t numCols = pSrc->numCols; /* Number of Cols in the matrix */ float32_t maxC; /* maximum value in the column */ float32_t Xchg, in = 0.0f, in1; /* Temporary input values */ uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */ arm_status status; /* status of matrix inverse */ float32x4_t vec1; float32x4_t vec2; float32x4_t tmpV; #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols) || (pSrc->numRows != pDst->numRows)) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /*-------------------------------------------------------------------------------------------------------------- * Matrix Inverse can be solved using elementary row operations. * * Gauss-Jordan Method: * * 1. First combine the identity matrix and the input matrix separated by a bar to form an * augmented matrix as follows: * _ _ _ _ * | a11 a12 | 1 0 | | X11 X12 | * | | | = | | * |_ a21 a22 | 0 1 _| |_ X21 X21 _| * * 2. In our implementation, pDst Matrix is used as identity matrix. * * 3. Begin with the first row. Let i = 1. * * 4. Check to see if the pivot for column i is the greatest of the column. * The pivot is the element of the main diagonal that is on the current row. * For instance, if working with row i, then the pivot element is aii. * If the pivot is not the most significant of the columns, exchange that row with a row * below it that does contain the most significant value in column i. If the most * significant value of the column is zero, then an inverse to that matrix does not exist. * The most significant value of the column is the absolute maximum. * * 5. Divide every element of row i by the pivot. * * 6. For every row below and row i, replace that row with the sum of that row and * a multiple of row i so that each new element in column i below row i is zero. * * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros * for every element below and above the main diagonal. * * 8. Now an identical matrix is formed to the left of the bar(input matrix, pSrc). * Therefore, the matrix to the right of the bar is our solution(pDst matrix, pDst). *----------------------------------------------------------------------------------------------------------------*/ /* Working pointer for destination matrix */ pOutT1 = pOut; /* Loop over the number of rows */ rowCnt = numRows; /* Making the destination matrix as identity matrix */ while (rowCnt > 0U) { /* Writing all zeroes in lower triangle of the destination matrix */ j = numRows - rowCnt; while (j > 0U) { *pOutT1++ = 0.0f; j--; } /* Writing all ones in the diagonal of the destination matrix */ *pOutT1++ = 1.0f; /* Writing all zeroes in upper triangle of the destination matrix */ j = rowCnt - 1U; while (j > 0U) { *pOutT1++ = 0.0f; j--; } /* Decrement the loop counter */ rowCnt--; } /* Loop over the number of columns of the input matrix. All the elements in each column are processed by the row operations */ loopCnt = numCols; /* Index modifier to navigate through the columns */ l = 0U; while (loopCnt > 0U) { /* Check if the pivot element is zero.. * If it is zero then interchange the row with non zero row below. * If there is no non zero element to replace in the rows below, * then the matrix is Singular. */ /* Working pointer for the input matrix that points * to the pivot element of the particular row */ pInT1 = pIn + (l * numCols); /* Working pointer for the destination matrix that points * to the pivot element of the particular row */ pOutT1 = pOut + (l * numCols); /* Temporary variable to hold the pivot value */ in = *pInT1; /* Grab the most significant value from column l */ maxC = 0; for (i = l; i < numRows; i++) { maxC = *pInT1 > 0 ? (*pInT1 > maxC ? *pInT1 : maxC) : (-*pInT1 > maxC ? -*pInT1 : maxC); pInT1 += numCols; } /* Update the status if the matrix is singular */ if (maxC == 0.0f) { return ARM_MATH_SINGULAR; } /* Restore pInT1 */ pInT1 = pIn; /* Destination pointer modifier */ k = 1U; /* Check if the pivot element is the most significant of the column */ if ( (in > 0.0f ? in : -in) != maxC) { /* Loop over the number rows present below */ i = numRows - (l + 1U); while (i > 0U) { /* Update the input and destination pointers */ pInT2 = pInT1 + (numCols * l); pOutT2 = pOutT1 + (numCols * k); /* Look for the most significant element to * replace in the rows below */ if ((*pInT2 > 0.0f ? *pInT2: -*pInT2) == maxC) { /* Loop over number of columns * to the right of the pilot element */ j = numCols - l; while (j > 0U) { /* Exchange the row elements of the input matrix */ Xchg = *pInT2; *pInT2++ = *pInT1; *pInT1++ = Xchg; /* Decrement the loop counter */ j--; } /* Loop over number of columns of the destination matrix */ j = numCols; while (j > 0U) { /* Exchange the row elements of the destination matrix */ Xchg = *pOutT2; *pOutT2++ = *pOutT1; *pOutT1++ = Xchg; /* Decrement the loop counter */ j--; } /* Flag to indicate whether exchange is done or not */ flag = 1U; /* Break after exchange is done */ break; } /* Update the destination pointer modifier */ k++; /* Decrement the loop counter */ i--; } } /* Update the status if the matrix is singular */ if ((flag != 1U) && (in == 0.0f)) { return ARM_MATH_SINGULAR; } /* Points to the pivot row of input and destination matrices */ pPivotRowIn = pIn + (l * numCols); pPivotRowDst = pOut + (l * numCols); /* Temporary pointers to the pivot row pointers */ pInT1 = pPivotRowIn; pInT2 = pPivotRowDst; /* Pivot element of the row */ in = *pPivotRowIn; tmpV = vdupq_n_f32(1.0/in); /* Loop over number of columns * to the right of the pilot element */ j = (numCols - l) >> 2; while (j > 0U) { /* Divide each element of the row of the input matrix * by the pivot element */ vec1 = vld1q_f32(pInT1); vec1 = vmulq_f32(vec1, tmpV); vst1q_f32(pInT1, vec1); pInT1 += 4; /* Decrement the loop counter */ j--; } /* Tail */ j = (numCols - l) & 3; while (j > 0U) { /* Divide each element of the row of the input matrix * by the pivot element */ in1 = *pInT1; *pInT1++ = in1 / in; /* Decrement the loop counter */ j--; } /* Loop over number of columns of the destination matrix */ j = numCols >> 2; while (j > 0U) { /* Divide each element of the row of the destination matrix * by the pivot element */ vec1 = vld1q_f32(pInT2); vec1 = vmulq_f32(vec1, tmpV); vst1q_f32(pInT2, vec1); pInT2 += 4; /* Decrement the loop counter */ j--; } /* Tail */ j = numCols & 3; while (j > 0U) { /* Divide each element of the row of the destination matrix * by the pivot element */ in1 = *pInT2; *pInT2++ = in1 / in; /* Decrement the loop counter */ j--; } /* Replace the rows with the sum of that row and a multiple of row i * so that each new element in column i above row i is zero.*/ /* Temporary pointers for input and destination matrices */ pInT1 = pIn; pInT2 = pOut; /* index used to check for pivot element */ i = 0U; /* Loop over number of rows */ /* to be replaced by the sum of that row and a multiple of row i */ k = numRows; while (k > 0U) { /* Check for the pivot element */ if (i == l) { /* If the processing element is the pivot element, only the columns to the right are to be processed */ pInT1 += numCols - l; pInT2 += numCols; } else { /* Element of the reference row */ in = *pInT1; tmpV = vdupq_n_f32(in); /* Working pointers for input and destination pivot rows */ pPRT_in = pPivotRowIn; pPRT_pDst = pPivotRowDst; /* Loop over the number of columns to the right of the pivot element, to replace the elements in the input matrix */ j = (numCols - l) >> 2; while (j > 0U) { /* Replace the element by the sum of that row and a multiple of the reference row */ vec1 = vld1q_f32(pInT1); vec2 = vld1q_f32(pPRT_in); vec1 = vmlsq_f32(vec1, tmpV, vec2); vst1q_f32(pInT1, vec1); pPRT_in += 4; pInT1 += 4; /* Decrement the loop counter */ j--; } /* Tail */ j = (numCols - l) & 3; while (j > 0U) { /* Replace the element by the sum of that row and a multiple of the reference row */ in1 = *pInT1; *pInT1++ = in1 - (in * *pPRT_in++); /* Decrement the loop counter */ j--; } /* Loop over the number of columns to replace the elements in the destination matrix */ j = numCols >> 2; while (j > 0U) { /* Replace the element by the sum of that row and a multiple of the reference row */ vec1 = vld1q_f32(pInT2); vec2 = vld1q_f32(pPRT_pDst); vec1 = vmlsq_f32(vec1, tmpV, vec2); vst1q_f32(pInT2, vec1); pPRT_pDst += 4; pInT2 += 4; /* Decrement the loop counter */ j--; } /* Tail */ j = numCols & 3; while (j > 0U) { /* Replace the element by the sum of that row and a multiple of the reference row */ in1 = *pInT2; *pInT2++ = in1 - (in * *pPRT_pDst++); /* Decrement the loop counter */ j--; } } /* Increment the temporary input pointer */ pInT1 = pInT1 + l; /* Decrement the loop counter */ k--; /* Increment the pivot index */ i++; } /* Increment the input pointer */ pIn++; /* Decrement the loop counter */ loopCnt--; /* Increment the index modifier */ l++; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; if ((flag != 1U) && (in == 0.0f)) { pIn = pSrc->pData; for (i = 0; i < numRows * numCols; i++) { if (pIn[i] != 0.0f) break; } if (i == numRows * numCols) status = ARM_MATH_SINGULAR; } } /* Return to application */ return (status); } #else arm_status arm_mat_inverse_f32( const arm_matrix_instance_f32 * pSrc, arm_matrix_instance_f32 * pDst) { float32_t *pIn = pSrc->pData; /* input data matrix pointer */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ float32_t *pInT1, *pInT2; /* Temporary input data matrix pointer */ float32_t *pOutT1, *pOutT2; /* Temporary output data matrix pointer */ float32_t *pPivotRowIn, *pPRT_in, *pPivotRowDst, *pPRT_pDst; /* Temporary input and output data matrix pointer */ uint32_t numRows = pSrc->numRows; /* Number of rows in the matrix */ uint32_t numCols = pSrc->numCols; /* Number of Cols in the matrix */ #if defined (ARM_MATH_DSP) float32_t maxC; /* maximum value in the column */ float32_t Xchg, in = 0.0f, in1; /* Temporary input values */ uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */ arm_status status; /* status of matrix inverse */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols) || (pSrc->numRows != pDst->numRows) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /*-------------------------------------------------------------------------------------------------------------- * Matrix Inverse can be solved using elementary row operations. * * Gauss-Jordan Method: * * 1. First combine the identity matrix and the input matrix separated by a bar to form an * augmented matrix as follows: * _ _ _ _ * | a11 a12 | 1 0 | | X11 X12 | * | | | = | | * |_ a21 a22 | 0 1 _| |_ X21 X21 _| * * 2. In our implementation, pDst Matrix is used as identity matrix. * * 3. Begin with the first row. Let i = 1. * * 4. Check to see if the pivot for column i is the greatest of the column. * The pivot is the element of the main diagonal that is on the current row. * For instance, if working with row i, then the pivot element is aii. * If the pivot is not the most significant of the columns, exchange that row with a row * below it that does contain the most significant value in column i. If the most * significant value of the column is zero, then an inverse to that matrix does not exist. * The most significant value of the column is the absolute maximum. * * 5. Divide every element of row i by the pivot. * * 6. For every row below and row i, replace that row with the sum of that row and * a multiple of row i so that each new element in column i below row i is zero. * * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros * for every element below and above the main diagonal. * * 8. Now an identical matrix is formed to the left of the bar(input matrix, pSrc). * Therefore, the matrix to the right of the bar is our solution(pDst matrix, pDst). *----------------------------------------------------------------------------------------------------------------*/ /* Working pointer for destination matrix */ pOutT1 = pOut; /* Loop over the number of rows */ rowCnt = numRows; /* Making the destination matrix as identity matrix */ while (rowCnt > 0U) { /* Writing all zeroes in lower triangle of the destination matrix */ j = numRows - rowCnt; while (j > 0U) { *pOutT1++ = 0.0f; j--; } /* Writing all ones in the diagonal of the destination matrix */ *pOutT1++ = 1.0f; /* Writing all zeroes in upper triangle of the destination matrix */ j = rowCnt - 1U; while (j > 0U) { *pOutT1++ = 0.0f; j--; } /* Decrement loop counter */ rowCnt--; } /* Loop over the number of columns of the input matrix. All the elements in each column are processed by the row operations */ loopCnt = numCols; /* Index modifier to navigate through the columns */ l = 0U; while (loopCnt > 0U) { /* Check if the pivot element is zero.. * If it is zero then interchange the row with non zero row below. * If there is no non zero element to replace in the rows below, * then the matrix is Singular. */ /* Working pointer for the input matrix that points * to the pivot element of the particular row */ pInT1 = pIn + (l * numCols); /* Working pointer for the destination matrix that points * to the pivot element of the particular row */ pOutT1 = pOut + (l * numCols); /* Temporary variable to hold the pivot value */ in = *pInT1; /* Grab the most significant value from column l */ maxC = 0; for (i = l; i < numRows; i++) { maxC = *pInT1 > 0 ? (*pInT1 > maxC ? *pInT1 : maxC) : (-*pInT1 > maxC ? -*pInT1 : maxC); pInT1 += numCols; } /* Update the status if the matrix is singular */ if (maxC == 0.0f) { return ARM_MATH_SINGULAR; } /* Restore pInT1 */ pInT1 = pIn; /* Destination pointer modifier */ k = 1U; /* Check if the pivot element is the most significant of the column */ if ( (in > 0.0f ? in : -in) != maxC) { /* Loop over the number rows present below */ i = numRows - (l + 1U); while (i > 0U) { /* Update the input and destination pointers */ pInT2 = pInT1 + (numCols * l); pOutT2 = pOutT1 + (numCols * k); /* Look for the most significant element to * replace in the rows below */ if ((*pInT2 > 0.0f ? *pInT2: -*pInT2) == maxC) { /* Loop over number of columns * to the right of the pilot element */ j = numCols - l; while (j > 0U) { /* Exchange the row elements of the input matrix */ Xchg = *pInT2; *pInT2++ = *pInT1; *pInT1++ = Xchg; /* Decrement the loop counter */ j--; } /* Loop over number of columns of the destination matrix */ j = numCols; while (j > 0U) { /* Exchange the row elements of the destination matrix */ Xchg = *pOutT2; *pOutT2++ = *pOutT1; *pOutT1++ = Xchg; /* Decrement loop counter */ j--; } /* Flag to indicate whether exchange is done or not */ flag = 1U; /* Break after exchange is done */ break; } /* Update the destination pointer modifier */ k++; /* Decrement loop counter */ i--; } } /* Update the status if the matrix is singular */ if ((flag != 1U) && (in == 0.0f)) { return ARM_MATH_SINGULAR; } /* Points to the pivot row of input and destination matrices */ pPivotRowIn = pIn + (l * numCols); pPivotRowDst = pOut + (l * numCols); /* Temporary pointers to the pivot row pointers */ pInT1 = pPivotRowIn; pInT2 = pPivotRowDst; /* Pivot element of the row */ in = *pPivotRowIn; /* Loop over number of columns * to the right of the pilot element */ j = (numCols - l); while (j > 0U) { /* Divide each element of the row of the input matrix * by the pivot element */ in1 = *pInT1; *pInT1++ = in1 / in; /* Decrement the loop counter */ j--; } /* Loop over number of columns of the destination matrix */ j = numCols; while (j > 0U) { /* Divide each element of the row of the destination matrix * by the pivot element */ in1 = *pInT2; *pInT2++ = in1 / in; /* Decrement the loop counter */ j--; } /* Replace the rows with the sum of that row and a multiple of row i * so that each new element in column i above row i is zero.*/ /* Temporary pointers for input and destination matrices */ pInT1 = pIn; pInT2 = pOut; /* index used to check for pivot element */ i = 0U; /* Loop over number of rows */ /* to be replaced by the sum of that row and a multiple of row i */ k = numRows; while (k > 0U) { /* Check for the pivot element */ if (i == l) { /* If the processing element is the pivot element, only the columns to the right are to be processed */ pInT1 += numCols - l; pInT2 += numCols; } else { /* Element of the reference row */ in = *pInT1; /* Working pointers for input and destination pivot rows */ pPRT_in = pPivotRowIn; pPRT_pDst = pPivotRowDst; /* Loop over the number of columns to the right of the pivot element, to replace the elements in the input matrix */ j = (numCols - l); while (j > 0U) { /* Replace the element by the sum of that row and a multiple of the reference row */ in1 = *pInT1; *pInT1++ = in1 - (in * *pPRT_in++); /* Decrement the loop counter */ j--; } /* Loop over the number of columns to replace the elements in the destination matrix */ j = numCols; while (j > 0U) { /* Replace the element by the sum of that row and a multiple of the reference row */ in1 = *pInT2; *pInT2++ = in1 - (in * *pPRT_pDst++); /* Decrement loop counter */ j--; } } /* Increment temporary input pointer */ pInT1 = pInT1 + l; /* Decrement loop counter */ k--; /* Increment pivot index */ i++; } /* Increment the input pointer */ pIn++; /* Decrement the loop counter */ loopCnt--; /* Increment the index modifier */ l++; } #else float32_t Xchg, in = 0.0f; /* Temporary input values */ uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */ arm_status status; /* status of matrix inverse */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols) || (pSrc->numRows != pDst->numRows) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /*-------------------------------------------------------------------------------------------------------------- * Matrix Inverse can be solved using elementary row operations. * * Gauss-Jordan Method: * * 1. First combine the identity matrix and the input matrix separated by a bar to form an * augmented matrix as follows: * _ _ _ _ _ _ _ _ * | | a11 a12 | | | 1 0 | | | X11 X12 | * | | | | | | | = | | * |_ |_ a21 a22 _| | |_0 1 _| _| |_ X21 X21 _| * * 2. In our implementation, pDst Matrix is used as identity matrix. * * 3. Begin with the first row. Let i = 1. * * 4. Check to see if the pivot for row i is zero. * The pivot is the element of the main diagonal that is on the current row. * For instance, if working with row i, then the pivot element is aii. * If the pivot is zero, exchange that row with a row below it that does not * contain a zero in column i. If this is not possible, then an inverse * to that matrix does not exist. * * 5. Divide every element of row i by the pivot. * * 6. For every row below and row i, replace that row with the sum of that row and * a multiple of row i so that each new element in column i below row i is zero. * * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros * for every element below and above the main diagonal. * * 8. Now an identical matrix is formed to the left of the bar(input matrix, src). * Therefore, the matrix to the right of the bar is our solution(dst matrix, dst). *----------------------------------------------------------------------------------------------------------------*/ /* Working pointer for destination matrix */ pOutT1 = pOut; /* Loop over the number of rows */ rowCnt = numRows; /* Making the destination matrix as identity matrix */ while (rowCnt > 0U) { /* Writing all zeroes in lower triangle of the destination matrix */ j = numRows - rowCnt; while (j > 0U) { *pOutT1++ = 0.0f; j--; } /* Writing all ones in the diagonal of the destination matrix */ *pOutT1++ = 1.0f; /* Writing all zeroes in upper triangle of the destination matrix */ j = rowCnt - 1U; while (j > 0U) { *pOutT1++ = 0.0f; j--; } /* Decrement loop counter */ rowCnt--; } /* Loop over the number of columns of the input matrix. All the elements in each column are processed by the row operations */ loopCnt = numCols; /* Index modifier to navigate through the columns */ l = 0U; while (loopCnt > 0U) { /* Check if the pivot element is zero.. * If it is zero then interchange the row with non zero row below. * If there is no non zero element to replace in the rows below, * then the matrix is Singular. */ /* Working pointer for the input matrix that points * to the pivot element of the particular row */ pInT1 = pIn + (l * numCols); /* Working pointer for the destination matrix that points * to the pivot element of the particular row */ pOutT1 = pOut + (l * numCols); /* Temporary variable to hold the pivot value */ in = *pInT1; /* Destination pointer modifier */ k = 1U; /* Check if the pivot element is zero */ if (*pInT1 == 0.0f) { /* Loop over the number rows present below */ for (i = (l + 1U); i < numRows; i++) { /* Update the input and destination pointers */ pInT2 = pInT1 + (numCols * l); pOutT2 = pOutT1 + (numCols * k); /* Check if there is a non zero pivot element to * replace in the rows below */ if (*pInT2 != 0.0f) { /* Loop over number of columns * to the right of the pilot element */ for (j = 0U; j < (numCols - l); j++) { /* Exchange the row elements of the input matrix */ Xchg = *pInT2; *pInT2++ = *pInT1; *pInT1++ = Xchg; } for (j = 0U; j < numCols; j++) { Xchg = *pOutT2; *pOutT2++ = *pOutT1; *pOutT1++ = Xchg; } /* Flag to indicate whether exchange is done or not */ flag = 1U; /* Break after exchange is done */ break; } /* Update the destination pointer modifier */ k++; } } /* Update the status if the matrix is singular */ if ((flag != 1U) && (in == 0.0f)) { return ARM_MATH_SINGULAR; } /* Points to the pivot row of input and destination matrices */ pPivotRowIn = pIn + (l * numCols); pPivotRowDst = pOut + (l * numCols); /* Temporary pointers to the pivot row pointers */ pInT1 = pPivotRowIn; pOutT1 = pPivotRowDst; /* Pivot element of the row */ in = *(pIn + (l * numCols)); /* Loop over number of columns * to the right of the pilot element */ for (j = 0U; j < (numCols - l); j++) { /* Divide each element of the row of the input matrix * by the pivot element */ *pInT1 = *pInT1 / in; pInT1++; } for (j = 0U; j < numCols; j++) { /* Divide each element of the row of the destination matrix * by the pivot element */ *pOutT1 = *pOutT1 / in; pOutT1++; } /* Replace the rows with the sum of that row and a multiple of row i * so that each new element in column i above row i is zero.*/ /* Temporary pointers for input and destination matrices */ pInT1 = pIn; pOutT1 = pOut; for (i = 0U; i < numRows; i++) { /* Check for the pivot element */ if (i == l) { /* If the processing element is the pivot element, only the columns to the right are to be processed */ pInT1 += numCols - l; pOutT1 += numCols; } else { /* Element of the reference row */ in = *pInT1; /* Working pointers for input and destination pivot rows */ pPRT_in = pPivotRowIn; pPRT_pDst = pPivotRowDst; /* Loop over the number of columns to the right of the pivot element, to replace the elements in the input matrix */ for (j = 0U; j < (numCols - l); j++) { /* Replace the element by the sum of that row and a multiple of the reference row */ *pInT1 = *pInT1 - (in * *pPRT_in++); pInT1++; } /* Loop over the number of columns to replace the elements in the destination matrix */ for (j = 0U; j < numCols; j++) { /* Replace the element by the sum of that row and a multiple of the reference row */ *pOutT1 = *pOutT1 - (in * *pPRT_pDst++); pOutT1++; } } /* Increment temporary input pointer */ pInT1 = pInT1 + l; } /* Increment the input pointer */ pIn++; /* Decrement the loop counter */ loopCnt--; /* Increment the index modifier */ l++; } #endif /* #if defined (ARM_MATH_DSP) */ /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; if ((flag != 1U) && (in == 0.0f)) { pIn = pSrc->pData; for (i = 0; i < numRows * numCols; i++) { if (pIn[i] != 0.0f) break; } if (i == numRows * numCols) status = ARM_MATH_SINGULAR; } } /* Return to application */ return (status); } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of MatrixInv group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_inverse_f32.c
C
apache-2.0
35,100
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_inverse_f64.c * Description: Floating-point matrix inverse * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixInv @{ */ /** @brief Floating-point (64 bit) matrix inverse. @param[in] pSrc points to input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed - \ref ARM_MATH_SINGULAR : Input matrix is found to be singular (non-invertible) */ arm_status arm_mat_inverse_f64( const arm_matrix_instance_f64 * pSrc, arm_matrix_instance_f64 * pDst) { float64_t *pIn = pSrc->pData; /* input data matrix pointer */ float64_t *pOut = pDst->pData; /* output data matrix pointer */ float64_t *pInT1, *pInT2; /* Temporary input data matrix pointer */ float64_t *pOutT1, *pOutT2; /* Temporary output data matrix pointer */ float64_t *pPivotRowIn, *pPRT_in, *pPivotRowDst, *pPRT_pDst; /* Temporary input and output data matrix pointer */ uint32_t numRows = pSrc->numRows; /* Number of rows in the matrix */ uint32_t numCols = pSrc->numCols; /* Number of Cols in the matrix */ #if defined (ARM_MATH_DSP) float64_t maxC; /* maximum value in the column */ float64_t Xchg, in = 0.0, in1; /* Temporary input values */ uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */ arm_status status; /* status of matrix inverse */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols) || (pSrc->numRows != pDst->numRows) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /*-------------------------------------------------------------------------------------------------------------- * Matrix Inverse can be solved using elementary row operations. * * Gauss-Jordan Method: * * 1. First combine the identity matrix and the input matrix separated by a bar to form an * augmented matrix as follows: * _ _ _ _ * | a11 a12 | 1 0 | | X11 X12 | * | | | = | | * |_ a21 a22 | 0 1 _| |_ X21 X21 _| * * 2. In our implementation, pDst Matrix is used as identity matrix. * * 3. Begin with the first row. Let i = 1. * * 4. Check to see if the pivot for column i is the greatest of the column. * The pivot is the element of the main diagonal that is on the current row. * For instance, if working with row i, then the pivot element is aii. * If the pivot is not the most significant of the columns, exchange that row with a row * below it that does contain the most significant value in column i. If the most * significant value of the column is zero, then an inverse to that matrix does not exist. * The most significant value of the column is the absolute maximum. * * 5. Divide every element of row i by the pivot. * * 6. For every row below and row i, replace that row with the sum of that row and * a multiple of row i so that each new element in column i below row i is zero. * * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros * for every element below and above the main diagonal. * * 8. Now an identical matrix is formed to the left of the bar(input matrix, pSrc). * Therefore, the matrix to the right of the bar is our solution(pDst matrix, pDst). *----------------------------------------------------------------------------------------------------------------*/ /* Working pointer for destination matrix */ pOutT1 = pOut; /* Loop over the number of rows */ rowCnt = numRows; /* Making the destination matrix as identity matrix */ while (rowCnt > 0U) { /* Writing all zeroes in lower triangle of the destination matrix */ j = numRows - rowCnt; while (j > 0U) { *pOutT1++ = 0.0; j--; } /* Writing all ones in the diagonal of the destination matrix */ *pOutT1++ = 1.0; /* Writing all zeroes in upper triangle of the destination matrix */ j = rowCnt - 1U; while (j > 0U) { *pOutT1++ = 0.0; j--; } /* Decrement loop counter */ rowCnt--; } /* Loop over the number of columns of the input matrix. All the elements in each column are processed by the row operations */ loopCnt = numCols; /* Index modifier to navigate through the columns */ l = 0U; while (loopCnt > 0U) { /* Check if the pivot element is zero.. * If it is zero then interchange the row with non zero row below. * If there is no non zero element to replace in the rows below, * then the matrix is Singular. */ /* Working pointer for the input matrix that points * to the pivot element of the particular row */ pInT1 = pIn + (l * numCols); /* Working pointer for the destination matrix that points * to the pivot element of the particular row */ pOutT1 = pOut + (l * numCols); /* Temporary variable to hold the pivot value */ in = *pInT1; /* Grab the most significant value from column l */ maxC = 0; for (i = l; i < numRows; i++) { maxC = *pInT1 > 0 ? (*pInT1 > maxC ? *pInT1 : maxC) : (-*pInT1 > maxC ? -*pInT1 : maxC); pInT1 += numCols; } /* Update the status if the matrix is singular */ if (maxC == 0.0) { return ARM_MATH_SINGULAR; } /* Restore pInT1 */ pInT1 = pIn; /* Destination pointer modifier */ k = 1U; /* Check if the pivot element is the most significant of the column */ if ( (in > 0.0 ? in : -in) != maxC) { /* Loop over the number rows present below */ i = numRows - (l + 1U); while (i > 0U) { /* Update the input and destination pointers */ pInT2 = pInT1 + (numCols * l); pOutT2 = pOutT1 + (numCols * k); /* Look for the most significant element to * replace in the rows below */ if ((*pInT2 > 0.0 ? *pInT2: -*pInT2) == maxC) { /* Loop over number of columns * to the right of the pilot element */ j = numCols - l; while (j > 0U) { /* Exchange the row elements of the input matrix */ Xchg = *pInT2; *pInT2++ = *pInT1; *pInT1++ = Xchg; /* Decrement the loop counter */ j--; } /* Loop over number of columns of the destination matrix */ j = numCols; while (j > 0U) { /* Exchange the row elements of the destination matrix */ Xchg = *pOutT2; *pOutT2++ = *pOutT1; *pOutT1++ = Xchg; /* Decrement loop counter */ j--; } /* Flag to indicate whether exchange is done or not */ flag = 1U; /* Break after exchange is done */ break; } /* Update the destination pointer modifier */ k++; /* Decrement loop counter */ i--; } } /* Update the status if the matrix is singular */ if ((flag != 1U) && (in == 0.0)) { return ARM_MATH_SINGULAR; } /* Points to the pivot row of input and destination matrices */ pPivotRowIn = pIn + (l * numCols); pPivotRowDst = pOut + (l * numCols); /* Temporary pointers to the pivot row pointers */ pInT1 = pPivotRowIn; pInT2 = pPivotRowDst; /* Pivot element of the row */ in = *pPivotRowIn; /* Loop over number of columns * to the right of the pilot element */ j = (numCols - l); while (j > 0U) { /* Divide each element of the row of the input matrix * by the pivot element */ in1 = *pInT1; *pInT1++ = in1 / in; /* Decrement the loop counter */ j--; } /* Loop over number of columns of the destination matrix */ j = numCols; while (j > 0U) { /* Divide each element of the row of the destination matrix * by the pivot element */ in1 = *pInT2; *pInT2++ = in1 / in; /* Decrement the loop counter */ j--; } /* Replace the rows with the sum of that row and a multiple of row i * so that each new element in column i above row i is zero.*/ /* Temporary pointers for input and destination matrices */ pInT1 = pIn; pInT2 = pOut; /* index used to check for pivot element */ i = 0U; /* Loop over number of rows */ /* to be replaced by the sum of that row and a multiple of row i */ k = numRows; while (k > 0U) { /* Check for the pivot element */ if (i == l) { /* If the processing element is the pivot element, only the columns to the right are to be processed */ pInT1 += numCols - l; pInT2 += numCols; } else { /* Element of the reference row */ in = *pInT1; /* Working pointers for input and destination pivot rows */ pPRT_in = pPivotRowIn; pPRT_pDst = pPivotRowDst; /* Loop over the number of columns to the right of the pivot element, to replace the elements in the input matrix */ j = (numCols - l); while (j > 0U) { /* Replace the element by the sum of that row and a multiple of the reference row */ in1 = *pInT1; *pInT1++ = in1 - (in * *pPRT_in++); /* Decrement the loop counter */ j--; } /* Loop over the number of columns to replace the elements in the destination matrix */ j = numCols; while (j > 0U) { /* Replace the element by the sum of that row and a multiple of the reference row */ in1 = *pInT2; *pInT2++ = in1 - (in * *pPRT_pDst++); /* Decrement loop counter */ j--; } } /* Increment temporary input pointer */ pInT1 = pInT1 + l; /* Decrement loop counter */ k--; /* Increment pivot index */ i++; } /* Increment the input pointer */ pIn++; /* Decrement the loop counter */ loopCnt--; /* Increment the index modifier */ l++; } #else float64_t Xchg, in = 0.0; /* Temporary input values */ uint32_t i, rowCnt, flag = 0U, j, loopCnt, k, l; /* loop counters */ arm_status status; /* status of matrix inverse */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pSrc->numCols) || (pDst->numRows != pDst->numCols) || (pSrc->numRows != pDst->numRows) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /*-------------------------------------------------------------------------------------------------------------- * Matrix Inverse can be solved using elementary row operations. * * Gauss-Jordan Method: * * 1. First combine the identity matrix and the input matrix separated by a bar to form an * augmented matrix as follows: * _ _ _ _ _ _ _ _ * | | a11 a12 | | | 1 0 | | | X11 X12 | * | | | | | | | = | | * |_ |_ a21 a22 _| | |_0 1 _| _| |_ X21 X21 _| * * 2. In our implementation, pDst Matrix is used as identity matrix. * * 3. Begin with the first row. Let i = 1. * * 4. Check to see if the pivot for row i is zero. * The pivot is the element of the main diagonal that is on the current row. * For instance, if working with row i, then the pivot element is aii. * If the pivot is zero, exchange that row with a row below it that does not * contain a zero in column i. If this is not possible, then an inverse * to that matrix does not exist. * * 5. Divide every element of row i by the pivot. * * 6. For every row below and row i, replace that row with the sum of that row and * a multiple of row i so that each new element in column i below row i is zero. * * 7. Move to the next row and column and repeat steps 2 through 5 until you have zeros * for every element below and above the main diagonal. * * 8. Now an identical matrix is formed to the left of the bar(input matrix, src). * Therefore, the matrix to the right of the bar is our solution(dst matrix, dst). *----------------------------------------------------------------------------------------------------------------*/ /* Working pointer for destination matrix */ pOutT1 = pOut; /* Loop over the number of rows */ rowCnt = numRows; /* Making the destination matrix as identity matrix */ while (rowCnt > 0U) { /* Writing all zeroes in lower triangle of the destination matrix */ j = numRows - rowCnt; while (j > 0U) { *pOutT1++ = 0.0; j--; } /* Writing all ones in the diagonal of the destination matrix */ *pOutT1++ = 1.0; /* Writing all zeroes in upper triangle of the destination matrix */ j = rowCnt - 1U; while (j > 0U) { *pOutT1++ = 0.0; j--; } /* Decrement loop counter */ rowCnt--; } /* Loop over the number of columns of the input matrix. All the elements in each column are processed by the row operations */ loopCnt = numCols; /* Index modifier to navigate through the columns */ l = 0U; while (loopCnt > 0U) { /* Check if the pivot element is zero.. * If it is zero then interchange the row with non zero row below. * If there is no non zero element to replace in the rows below, * then the matrix is Singular. */ /* Working pointer for the input matrix that points * to the pivot element of the particular row */ pInT1 = pIn + (l * numCols); /* Working pointer for the destination matrix that points * to the pivot element of the particular row */ pOutT1 = pOut + (l * numCols); /* Temporary variable to hold the pivot value */ in = *pInT1; /* Destination pointer modifier */ k = 1U; /* Check if the pivot element is zero */ if (*pInT1 == 0.0) { /* Loop over the number rows present below */ for (i = (l + 1U); i < numRows; i++) { /* Update the input and destination pointers */ pInT2 = pInT1 + (numCols * l); pOutT2 = pOutT1 + (numCols * k); /* Check if there is a non zero pivot element to * replace in the rows below */ if (*pInT2 != 0.0) { /* Loop over number of columns * to the right of the pilot element */ for (j = 0U; j < (numCols - l); j++) { /* Exchange the row elements of the input matrix */ Xchg = *pInT2; *pInT2++ = *pInT1; *pInT1++ = Xchg; } for (j = 0U; j < numCols; j++) { Xchg = *pOutT2; *pOutT2++ = *pOutT1; *pOutT1++ = Xchg; } /* Flag to indicate whether exchange is done or not */ flag = 1U; /* Break after exchange is done */ break; } /* Update the destination pointer modifier */ k++; } } /* Update the status if the matrix is singular */ if ((flag != 1U) && (in == 0.0)) { return ARM_MATH_SINGULAR; } /* Points to the pivot row of input and destination matrices */ pPivotRowIn = pIn + (l * numCols); pPivotRowDst = pOut + (l * numCols); /* Temporary pointers to the pivot row pointers */ pInT1 = pPivotRowIn; pOutT1 = pPivotRowDst; /* Pivot element of the row */ in = *(pIn + (l * numCols)); /* Loop over number of columns * to the right of the pilot element */ for (j = 0U; j < (numCols - l); j++) { /* Divide each element of the row of the input matrix * by the pivot element */ *pInT1 = *pInT1 / in; pInT1++; } for (j = 0U; j < numCols; j++) { /* Divide each element of the row of the destination matrix * by the pivot element */ *pOutT1 = *pOutT1 / in; pOutT1++; } /* Replace the rows with the sum of that row and a multiple of row i * so that each new element in column i above row i is zero.*/ /* Temporary pointers for input and destination matrices */ pInT1 = pIn; pOutT1 = pOut; for (i = 0U; i < numRows; i++) { /* Check for the pivot element */ if (i == l) { /* If the processing element is the pivot element, only the columns to the right are to be processed */ pInT1 += numCols - l; pOutT1 += numCols; } else { /* Element of the reference row */ in = *pInT1; /* Working pointers for input and destination pivot rows */ pPRT_in = pPivotRowIn; pPRT_pDst = pPivotRowDst; /* Loop over the number of columns to the right of the pivot element, to replace the elements in the input matrix */ for (j = 0U; j < (numCols - l); j++) { /* Replace the element by the sum of that row and a multiple of the reference row */ *pInT1 = *pInT1 - (in * *pPRT_in++); pInT1++; } /* Loop over the number of columns to replace the elements in the destination matrix */ for (j = 0U; j < numCols; j++) { /* Replace the element by the sum of that row and a multiple of the reference row */ *pOutT1 = *pOutT1 - (in * *pPRT_pDst++); pOutT1++; } } /* Increment temporary input pointer */ pInT1 = pInT1 + l; } /* Increment the input pointer */ pIn++; /* Decrement the loop counter */ loopCnt--; /* Increment the index modifier */ l++; } #endif /* #if defined (ARM_MATH_DSP) */ /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; if ((flag != 1U) && (in == 0.0)) { pIn = pSrc->pData; for (i = 0; i < numRows * numCols; i++) { if (pIn[i] != 0.0) break; } if (i == numRows * numCols) status = ARM_MATH_SINGULAR; } } /* Return to application */ return (status); } /** @} end of MatrixInv group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_inverse_f64.c
C
apache-2.0
21,196
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_mult_f32.c * Description: Floating-point matrix multiplication * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** * @ingroup groupMatrix */ /** * @defgroup MatrixMult Matrix Multiplication * * Multiplies two matrices. * * \image html MatrixMultiplication.gif "Multiplication of two 3 x 3 matrices" * Matrix multiplication is only defined if the number of columns of the * first matrix equals the number of rows of the second matrix. * Multiplying an <code>M x N</code> matrix with an <code>N x P</code> matrix results * in an <code>M x P</code> matrix. * When matrix size checking is enabled, the functions check: (1) that the inner dimensions of * <code>pSrcA</code> and <code>pSrcB</code> are equal; and (2) that the size of the output * matrix equals the outer dimensions of <code>pSrcA</code> and <code>pSrcB</code>. */ /** * @addtogroup MatrixMult * @{ */ /** * @brief Floating-point matrix multiplication. * @param[in] *pSrcA points to the first input matrix structure * @param[in] *pSrcB points to the second input matrix structure * @param[out] *pDst points to output matrix structure * @return The function returns either * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking. */ #if defined(ARM_MATH_NEON) #define GROUPOFROWS 8 arm_status arm_mat_mult_f32( const arm_matrix_instance_f32 * pSrcA, const arm_matrix_instance_f32 * pSrcB, arm_matrix_instance_f32 * pDst) { float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */ float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */ float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ float32_t *px; /* Temporary output data matrix pointer */ float32_t sum; /* Accumulator */ uint16_t numRowsA = pSrcA->numRows; /* number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* number of columns of input matrix A */ float32_t in1, in2, in3, in4; uint16_t col, i = 0U, j, row = numRowsA, rowCnt, colCnt; /* loop counters */ arm_status status; /* status of matrix multiplication */ float32x4_t a0V, a1V, a2V, a3V, a4V, a5V, a6V, a7V; float32x4_t acc0,acc1,acc2,acc3,acc4,acc5,acc6,acc7,temp; float32x2_t accum = vdup_n_f32(0); float32_t *pIn1B = pSrcA->pData; float32_t *pIn1C = pSrcA->pData; float32_t *pIn1D = pSrcA->pData; float32_t *pIn1E = pSrcA->pData; float32_t *pIn1F = pSrcA->pData; float32_t *pIn1G = pSrcA->pData; float32_t *pIn1H = pSrcA->pData; float32_t *pxB,*pxC, *pxD, *pxE, *pxF, *pxG, *pxH; /* Temporary output data matrix pointer */ float32_t sum0,sum1, sum2,sum3, sum4, sum5 , sum6, sum7; #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols)) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* Row loop */ rowCnt = row >> 3; while(rowCnt > 0) { /* Output pointer is set to starting address of the row being processed */ px = pOut + GROUPOFROWS*i; pxB = px + numColsB; pxC = px + 2*numColsB; pxD = px + 3*numColsB; pxE = px + 4*numColsB; pxF = px + 5*numColsB; pxG = px + 6*numColsB; pxH = px + 7*numColsB; /* For every row wise process, the column loop counter is to be initiated */ col = numColsB; /* For every row wise process, the pIn2 pointer is set ** to the starting address of the pSrcB data */ pIn2 = pSrcB->pData; j = 0U; /* Column loop */ do { /* Set the variable sum, that acts as accumulator, to zero */ sum0 = 0.0f; sum1 = 0.0f; sum2 = 0.0f; sum3 = 0.0f; sum4 = 0.0f; sum5 = 0.0f; sum6 = 0.0f; sum7 = 0.0f; /* Initiate the pointer pIn1 to point to the starting address of the column being processed */ pIn1 = pInA; pIn1B = pIn1 + numColsA; pIn1C = pIn1 + 2*numColsA; pIn1D = pIn1 + 3*numColsA; pIn1E = pIn1 + 4*numColsA; pIn1F = pIn1 + 5*numColsA; pIn1G = pIn1 + 6*numColsA; pIn1H = pIn1 + 7*numColsA; acc0 = vdupq_n_f32(0.0); acc1 = vdupq_n_f32(0.0); acc2 = vdupq_n_f32(0.0); acc3 = vdupq_n_f32(0.0); acc4 = vdupq_n_f32(0.0); acc5 = vdupq_n_f32(0.0); acc6 = vdupq_n_f32(0.0); acc7 = vdupq_n_f32(0.0); /* Compute 4 MACs simultaneously. */ colCnt = numColsA >> 2U; /* Matrix multiplication */ while (colCnt > 0U) { /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */ a0V = vld1q_f32(pIn1); a1V = vld1q_f32(pIn1B); a2V = vld1q_f32(pIn1C); a3V = vld1q_f32(pIn1D); a4V = vld1q_f32(pIn1E); a5V = vld1q_f32(pIn1F); a6V = vld1q_f32(pIn1G); a7V = vld1q_f32(pIn1H); pIn1 += 4; pIn1B += 4; pIn1C += 4; pIn1D += 4; pIn1E += 4; pIn1F += 4; pIn1G += 4; pIn1H += 4; temp[0] = *pIn2; pIn2 += numColsB; temp[1] = *pIn2; pIn2 += numColsB; temp[2] = *pIn2; pIn2 += numColsB; temp[3] = *pIn2; pIn2 += numColsB; acc0 = vmlaq_f32(acc0,a0V,temp); acc1 = vmlaq_f32(acc1,a1V,temp); acc2 = vmlaq_f32(acc2,a2V,temp); acc3 = vmlaq_f32(acc3,a3V,temp); acc4 = vmlaq_f32(acc4,a4V,temp); acc5 = vmlaq_f32(acc5,a5V,temp); acc6 = vmlaq_f32(acc6,a6V,temp); acc7 = vmlaq_f32(acc7,a7V,temp); /* Decrement the loop count */ colCnt--; } accum = vpadd_f32(vget_low_f32(acc0), vget_high_f32(acc0)); sum0 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(acc1), vget_high_f32(acc1)); sum1 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(acc2), vget_high_f32(acc2)); sum2 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(acc3), vget_high_f32(acc3)); sum3 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(acc4), vget_high_f32(acc4)); sum4 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(acc5), vget_high_f32(acc5)); sum5 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(acc6), vget_high_f32(acc6)); sum6 += accum[0] + accum[1]; accum = vpadd_f32(vget_low_f32(acc7), vget_high_f32(acc7)); sum7 += accum[0] + accum[1]; /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here. ** No loop unrolling is used. */ colCnt = numColsA & 3; while (colCnt > 0U) { /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */ sum0 += *pIn1++ * (*pIn2); sum1 += *pIn1B++ * (*pIn2); sum2 += *pIn1C++ * (*pIn2); sum3 += *pIn1D++ * (*pIn2); sum4 += *pIn1E++ * (*pIn2); sum5 += *pIn1F++ * (*pIn2); sum6 += *pIn1G++ * (*pIn2); sum7 += *pIn1H++ * (*pIn2); pIn2 += numColsB; /* Decrement the loop counter */ colCnt--; } /* Store the result in the destination buffer */ *px++ = sum0; *pxB++ = sum1; *pxC++ = sum2; *pxD++ = sum3; *pxE++ = sum4; *pxF++ = sum5; *pxG++ = sum6; *pxH++ = sum7; /* Update the pointer pIn2 to point to the starting address of the next column */ j++; pIn2 = pSrcB->pData + j; /* Decrement the column loop counter */ col--; } while (col > 0U); /* Update the pointer pInA to point to the starting address of the next row */ i = i + numColsB; pInA = pInA + GROUPOFROWS*numColsA; /* Decrement the row loop counter */ rowCnt--; } /* i was the index of a group of rows computed by previous loop. Now i is the index of a row since below code is computing row per row and no more group of row per group of rows. */ i = GROUPOFROWS*i; rowCnt = row & 7; while(rowCnt > 0) { /* Output pointer is set to starting address of the row being processed */ px = pOut + i; /* For every row wise process, the column loop counter is to be initiated */ col = numColsB; /* For every row wise process, the pIn2 pointer is set ** to the starting address of the pSrcB data */ pIn2 = pSrcB->pData; j = 0U; /* Column loop */ do { /* Set the variable sum, that acts as accumulator, to zero */ sum = 0.0f; /* Initiate the pointer pIn1 to point to the starting address of the column being processed */ pIn1 = pInA; acc0 = vdupq_n_f32(0.0); /* Compute 4 MACs simultaneously. */ colCnt = numColsA >> 2U; /* Matrix multiplication */ while (colCnt > 0U) { /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */ a0V = vld1q_f32(pIn1); // load & separate real/imag pSrcA (de-interleave 2) pIn1 += 4; temp[0] = *pIn2; pIn2 += numColsB; temp[1] = *pIn2; pIn2 += numColsB; temp[2] = *pIn2; pIn2 += numColsB; temp[3] = *pIn2; pIn2 += numColsB; acc0 = vmlaq_f32(acc0,a0V,temp); /* Decrement the loop count */ colCnt--; } accum = vpadd_f32(vget_low_f32(acc0), vget_high_f32(acc0)); sum += accum[0] + accum[1]; /* If the columns of pSrcA is not a multiple of 4, compute any remaining MACs here. ** No loop unrolling is used. */ colCnt = numColsA % 0x4U; while (colCnt > 0U) { /* c(m,n) = a(1,1)*b(1,1) + a(1,2)*b(2,1) + ... + a(m,p)*b(p,n) */ sum += *pIn1++ * (*pIn2); pIn2 += numColsB; /* Decrement the loop counter */ colCnt--; } /* Store the result in the destination buffer */ *px++ = sum; /* Update the pointer pIn2 to point to the starting address of the next column */ j++; pIn2 = pSrcB->pData + j; /* Decrement the column loop counter */ col--; } while (col > 0U); /* Update the pointer pInA to point to the starting address of the next row */ i = i + numColsB; pInA = pInA + numColsA; /* Decrement the row loop counter */ rowCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #else arm_status arm_mat_mult_f32( const arm_matrix_instance_f32 * pSrcA, const arm_matrix_instance_f32 * pSrcB, arm_matrix_instance_f32 * pDst) { float32_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */ float32_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */ float32_t *pInA = pSrcA->pData; /* Input data matrix pointer A */ float32_t *pInB = pSrcB->pData; /* Input data matrix pointer B */ float32_t *pOut = pDst->pData; /* Output data matrix pointer */ float32_t *px; /* Temporary output data matrix pointer */ float32_t sum; /* Accumulator */ uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */ uint32_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */ arm_status status; /* Status of matrix multiplication */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* row loop */ do { /* Output pointer is set to starting address of row being processed */ px = pOut + i; /* For every row wise process, column loop counter is to be initiated */ col = numColsB; /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */ pIn2 = pSrcB->pData; /* column loop */ do { /* Set the variable sum, that acts as accumulator, to zero */ sum = 0.0f; /* Initialize pointer pIn1 to point to starting address of column being processed */ pIn1 = pInA; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 MACs at a time. */ colCnt = numColsA >> 2U; /* matrix multiplication */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ /* Perform the multiply-accumulates */ sum += *pIn1++ * *pIn2; pIn2 += numColsB; sum += *pIn1++ * *pIn2; pIn2 += numColsB; sum += *pIn1++ * *pIn2; pIn2 += numColsB; sum += *pIn1++ * *pIn2; pIn2 += numColsB; /* Decrement loop counter */ colCnt--; } /* Loop unrolling: Compute remaining MACs */ colCnt = numColsA % 0x4U; #else /* Initialize cntCnt with number of columns */ colCnt = numColsA; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ /* Perform the multiply-accumulates */ sum += *pIn1++ * *pIn2; pIn2 += numColsB; /* Decrement loop counter */ colCnt--; } /* Store result in destination buffer */ *px++ = sum; /* Decrement column loop counter */ col--; /* Update pointer pIn2 to point to starting address of next column */ pIn2 = pInB + (numColsB - col); } while (col > 0U); /* Update pointer pInA to point to starting address of next row */ i = i + numColsB; pInA = pInA + numColsA; /* Decrement row loop counter */ row--; } while (row > 0U); /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #endif /* #if defined(ARM_MATH_NEON) */ /** * @} end of MatrixMult group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_mult_f32.c
C
apache-2.0
16,480
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_mult_fast_q15.c * Description: Q15 matrix multiplication (fast variant) * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixMult @{ */ /** @brief Q15 matrix multiplication (fast variant). @param[in] pSrcA points to the first input matrix structure @param[in] pSrcB points to the second input matrix structure @param[out] pDst points to output matrix structure @param[in] pState points to the array for storing intermediate results @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The difference between the function \ref arm_mat_mult_q15() and this fast variant is that the fast variant use a 32-bit rather than a 64-bit accumulator. The result of each 1.15 x 1.15 multiplication is truncated to 2.30 format. These intermediate results are accumulated in a 32-bit register in 2.30 format. Finally, the accumulator is saturated and converted to a 1.15 result. @par The fast version has the same overflow behavior as the standard version but provides less precision since it discards the low 16 bits of each multiplication result. In order to avoid overflows completely the input signals must be scaled down. Scale down one of the input matrices by log2(numColsA) bits to avoid overflows, as a total of numColsA additions are computed internally for each output element. @remark Refer to \ref arm_mat_mult_q15() for a slower implementation of this function which uses 64-bit accumulation to provide higher precision. */ arm_status arm_mat_mult_fast_q15( const arm_matrix_instance_q15 * pSrcA, const arm_matrix_instance_q15 * pSrcB, arm_matrix_instance_q15 * pDst, q15_t * pState) { q31_t sum; /* Accumulator */ q15_t *pSrcBT = pState; /* Input data matrix pointer for transpose */ q15_t *pInA = pSrcA->pData; /* Input data matrix pointer A of Q15 type */ q15_t *pInB = pSrcB->pData; /* Input data matrix pointer B of Q15 type */ q15_t *px; /* Temporary output data matrix pointer */ uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */ uint16_t numRowsB = pSrcB->numRows; /* Number of rows of input matrix A */ uint32_t col, i = 0U, row = numRowsB, colCnt; /* Loop counters */ arm_status status; /* Status of matrix multiplication */ #if defined (ARM_MATH_DSP) q31_t in; /* Temporary variable to hold the input value */ q31_t inA1, inB1, inA2, inB2; q31_t sum2, sum3, sum4; q15_t *pInA2, *pInB2, *px2; uint32_t j = 0; #else q15_t in; /* Temporary variable to hold the input value */ q15_t inA1, inB1, inA2, inB2; #endif /* #if defined (ARM_MATH_DSP) */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Matrix transpose */ do { /* The pointer px is set to starting address of column being processed */ px = pSrcBT + i; /* Apply loop unrolling and exchange columns with row elements */ col = numColsB >> 2U; /* First part of the processing with loop unrolling. Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (col > 0U) { #if defined (ARM_MATH_DSP) /* Read two elements from row */ in = read_q15x2_ia ((q15_t **) &pInB); /* Unpack and store one element in destination */ #ifndef ARM_MATH_BIG_ENDIAN *px = (q15_t) in; #else *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Update pointer px to point to next row of transposed matrix */ px += numRowsB; /* Unpack and store second element in destination */ #ifndef ARM_MATH_BIG_ENDIAN *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #else *px = (q15_t) in; #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Update pointer px to point to next row of transposed matrix */ px += numRowsB; in = read_q15x2_ia ((q15_t **) &pInB); #ifndef ARM_MATH_BIG_ENDIAN *px = (q15_t) in; #else *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ px += numRowsB; #ifndef ARM_MATH_BIG_ENDIAN *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #else *px = (q15_t) in; #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ px += numRowsB; #else /* #if defined (ARM_MATH_DSP) */ /* Read one element from row */ in = *pInB++; /* Store one element in destination */ *px = in; /* Update pointer px to point to next row of transposed matrix */ px += numRowsB; in = *pInB++; *px = in; px += numRowsB; in = *pInB++; *px = in; px += numRowsB; in = *pInB++; *px = in; px += numRowsB; #endif /* #if defined (ARM_MATH_DSP) */ /* Decrement column loop counter */ col--; } /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ col = numColsB % 0x4U; while (col > 0U) { /* Read and store input element in destination */ *px = *pInB++; /* Update pointer px to point to next row of transposed matrix */ px += numRowsB; /* Decrement column loop counter */ col--; } i++; /* Decrement row loop counter */ row--; } while (row > 0U); /* Reset variables for usage in following multiplication process */ row = numRowsA; i = 0U; px = pDst->pData; #if defined (ARM_MATH_DSP) /* Process two rows from matrix A at a time and output two rows at a time */ row = row >> 1U; px2 = px + numColsB; #endif /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* row loop */ while (row > 0U) { /* For every row wise process, column loop counter is to be initiated */ col = numColsB; /* For every row wise process, pIn2 pointer is set to starting address of transposed pSrcB data */ pInB = pSrcBT; #if defined (ARM_MATH_DSP) /* Process two (transposed) columns from matrix B at a time */ col = col >> 1U; j = 0; #endif /* column loop */ while (col > 0U) { /* Set variable sum, that acts as accumulator, to zero */ sum = 0; /* Initiate pointer pInA to point to starting address of column being processed */ pInA = pSrcA->pData + i; #if defined (ARM_MATH_DSP) sum2 = 0; sum3 = 0; sum4 = 0; pInB = pSrcBT + j; pInA2 = pInA + numColsA; pInB2 = pInB + numRowsB; /* Read in two elements at once - alows dual MAC instruction */ colCnt = numColsA >> 1U; #else colCnt = numColsA >> 2U; #endif /* matrix multiplication */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ #if defined (ARM_MATH_DSP) /* read real and imag values from pSrcA and pSrcB buffer */ inA1 = read_q15x2_ia ((q15_t **) &pInA); inB1 = read_q15x2_ia ((q15_t **) &pInB); inA2 = read_q15x2_ia ((q15_t **) &pInA2); inB2 = read_q15x2_ia ((q15_t **) &pInB2); /* Multiply and Accumlates */ sum = __SMLAD(inA1, inB1, sum); sum2 = __SMLAD(inA1, inB2, sum2); sum3 = __SMLAD(inA2, inB1, sum3); sum4 = __SMLAD(inA2, inB2, sum4); #else /* read real and imag values from pSrcA and pSrcB buffer */ inA1 = *pInA++; inB1 = *pInB++; /* Multiply and Accumlates */ sum += inA1 * inB1; inA2 = *pInA++; inB2 = *pInB++; sum += inA2 * inB2; inA1 = *pInA++; inB1 = *pInB++; sum += inA1 * inB1; inA2 = *pInA++; inB2 = *pInB++; sum += inA2 * inB2; #endif /* #if defined (ARM_MATH_DSP) */ /* Decrement loop counter */ colCnt--; } /* process odd column samples */ #if defined (ARM_MATH_DSP) if (numColsA & 1U) { inA1 = *pInA++; inB1 = *pInB++; inA2 = *pInA2++; inB2 = *pInB2++; sum += inA1 * inB1; sum2 += inA1 * inB2; sum3 += inA2 * inB1; sum4 += inA2 * inB2; } #else colCnt = numColsA % 0x4U; while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ sum += (q31_t) *pInA++ * *pInB++; /* Decrement loop counter */ colCnt--; } #endif /* #if defined (ARM_MATH_DSP) */ /* Saturate and store result in destination buffer */ *px++ = (q15_t) (sum >> 15); #if defined (ARM_MATH_DSP) *px++ = (q15_t) (sum2 >> 15); *px2++ = (q15_t) (sum3 >> 15); *px2++ = (q15_t) (sum4 >> 15); j += numRowsB * 2; #endif /* Decrement column loop counter */ col--; } i = i + numColsA; #if defined (ARM_MATH_DSP) i = i + numColsA; px = px2 + (numColsB & 1U); px2 = px + numColsB; #endif /* Decrement row loop counter */ row--; } /* Compute any remaining odd row/column below */ #if defined (ARM_MATH_DSP) /* Compute remaining output column */ if (numColsB & 1U) { /* Avoid redundant computation of last element */ row = numRowsA & (~0x1); /* Point to remaining unfilled column in output matrix */ px = pDst->pData + numColsB-1; pInA = pSrcA->pData; /* row loop */ while (row > 0) { /* point to last column in matrix B */ pInB = pSrcBT + numRowsB * (numColsB-1); /* Set variable sum, that acts as accumulator, to zero */ sum = 0; /* Compute 4 columns at once */ colCnt = numColsA >> 2U; /* matrix multiplication */ while (colCnt > 0U) { inA1 = read_q15x2_ia ((q15_t **) &pInA); inA2 = read_q15x2_ia ((q15_t **) &pInA); inB1 = read_q15x2_ia ((q15_t **) &pInB); inB2 = read_q15x2_ia ((q15_t **) &pInB); sum = __SMLAD(inA1, inB1, sum); sum = __SMLAD(inA2, inB2, sum); /* Decrement loop counter */ colCnt--; } colCnt = numColsA & 3U; while (colCnt > 0U) { sum += (q31_t) (*pInA++) * (*pInB++); colCnt--; } /* Store result in destination buffer */ *px = (q15_t) (sum >> 15); px += numColsB; /* Decrement row loop counter */ row--; } } /* Compute remaining output row */ if (numRowsA & 1U) { /* point to last row in output matrix */ px = pDst->pData + (numColsB) * (numRowsA-1); pInB = pSrcBT; col = numColsB; i = 0U; /* col loop */ while (col > 0) { /* point to last row in matrix A */ pInA = pSrcA->pData + (numRowsA-1) * numColsA; /* Set variable sum, that acts as accumulator, to zero */ sum = 0; /* Compute 4 columns at once */ colCnt = numColsA >> 2U; /* matrix multiplication */ while (colCnt > 0U) { inA1 = read_q15x2_ia ((q15_t **) &pInA); inA2 = read_q15x2_ia ((q15_t **) &pInA); inB1 = read_q15x2_ia ((q15_t **) &pInB); inB2 = read_q15x2_ia ((q15_t **) &pInB); sum = __SMLAD(inA1, inB1, sum); sum = __SMLAD(inA2, inB2, sum); /* Decrement loop counter */ colCnt--; } colCnt = numColsA % 4U; while (colCnt > 0U) { sum += (q31_t) (*pInA++) * (*pInB++); colCnt--; } /* Store result in destination buffer */ *px++ = (q15_t) (sum >> 15); /* Decrement column loop counter */ col--; } } #endif /* #if defined (ARM_MATH_DSP) */ /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixMult group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_mult_fast_q15.c
C
apache-2.0
14,400
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_mult_fast_q31.c * Description: Q31 matrix multiplication (fast variant) * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixMult @{ */ /** @brief Q31 matrix multiplication (fast variant). @param[in] pSrcA points to the first input matrix structure @param[in] pSrcB points to the second input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The difference between the function \ref arm_mat_mult_q31() and this fast variant is that the fast variant use a 32-bit rather than a 64-bit accumulator. The result of each 1.31 x 1.31 multiplication is truncated to 2.30 format. These intermediate results are accumulated in a 32-bit register in 2.30 format. Finally, the accumulator is saturated and converted to a 1.31 result. @par The fast version has the same overflow behavior as the standard version but provides less precision since it discards the low 32 bits of each multiplication result. In order to avoid overflows completely the input signals must be scaled down. Scale down one of the input matrices by log2(numColsA) bits to avoid overflows, as a total of numColsA additions are computed internally for each output element. @remark Refer to \ref arm_mat_mult_q31() for a slower implementation of this function which uses 64-bit accumulation to provide higher precision. */ arm_status arm_mat_mult_fast_q31( const arm_matrix_instance_q31 * pSrcA, const arm_matrix_instance_q31 * pSrcB, arm_matrix_instance_q31 * pDst) { q31_t *pInA = pSrcA->pData; /* Input data matrix pointer A */ q31_t *pInB = pSrcB->pData; /* Input data matrix pointer B */ q31_t *pInA2; q31_t *px; /* Temporary output data matrix pointer */ q31_t *px2; q31_t sum1, sum2, sum3, sum4; /* Accumulator */ q31_t inA1, inA2, inB1, inB2; uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */ uint32_t col, i = 0U, j, row = numRowsA, colCnt; /* Loop counters */ arm_status status; /* Status of matrix multiplication */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { px = pDst->pData; row = row >> 1U; px2 = px + numColsB; /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* row loop */ while (row > 0U) { /* For every row wise process, column loop counter is to be initiated */ col = numColsB; /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */ pInB = pSrcB->pData; j = 0U; col = col >> 1U; /* column loop */ while (col > 0U) { /* Set the variable sum, that acts as accumulator, to zero */ sum1 = 0; sum2 = 0; sum3 = 0; sum4 = 0; /* Initiate data pointers */ pInA = pSrcA->pData + i; pInB = pSrcB->pData + j; pInA2 = pInA + numColsA; colCnt = numColsA; /* matrix multiplication */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ inA1 = *pInA++; inB1 = pInB[0]; inA2 = *pInA2++; inB2 = pInB[1]; pInB += numColsB; #if defined (ARM_MATH_DSP) sum1 = __SMMLA(inA1, inB1, sum1); sum2 = __SMMLA(inA1, inB2, sum2); sum3 = __SMMLA(inA2, inB1, sum3); sum4 = __SMMLA(inA2, inB2, sum4); #else sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) inA1 * inB1)) >> 32); sum2 = (q31_t) ((((q63_t) sum2 << 32) + ((q63_t) inA1 * inB2)) >> 32); sum3 = (q31_t) ((((q63_t) sum3 << 32) + ((q63_t) inA2 * inB1)) >> 32); sum4 = (q31_t) ((((q63_t) sum4 << 32) + ((q63_t) inA2 * inB2)) >> 32); #endif /* Decrement loop counter */ colCnt--; } /* Convert the result from 2.30 to 1.31 format and store in destination buffer */ *px++ = sum1 << 1; *px++ = sum2 << 1; *px2++ = sum3 << 1; *px2++ = sum4 << 1; j += 2; /* Decrement column loop counter */ col--; } i = i + (numColsA << 1U); px = px2 + (numColsB & 1U); px2 = px + numColsB; /* Decrement row loop counter */ row--; } /* Compute any remaining odd row/column below */ /* Compute remaining output column */ if (numColsB & 1U) { /* Avoid redundant computation of last element */ row = numRowsA & (~1U); /* Point to remaining unfilled column in output matrix */ px = pDst->pData + numColsB-1; pInA = pSrcA->pData; /* row loop */ while (row > 0) { /* point to last column in matrix B */ pInB = pSrcB->pData + numColsB-1; /* Set variable sum1, that acts as accumulator, to zero */ sum1 = 0; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 columns at a time. */ colCnt = numColsA >> 2U; /* matrix multiplication */ while (colCnt > 0U) { #if defined (ARM_MATH_DSP) sum1 = __SMMLA(*pInA++, *pInB, sum1); #else sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) *pInA++ * *pInB)) >> 32); #endif pInB += numColsB; #if defined (ARM_MATH_DSP) sum1 = __SMMLA(*pInA++, *pInB, sum1); #else sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) *pInA++ * *pInB)) >> 32); #endif pInB += numColsB; #if defined (ARM_MATH_DSP) sum1 = __SMMLA(*pInA++, *pInB, sum1); #else sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) *pInA++ * *pInB)) >> 32); #endif pInB += numColsB; #if defined (ARM_MATH_DSP) sum1 = __SMMLA(*pInA++, *pInB, sum1); #else sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) *pInA++ * *pInB)) >> 32); #endif pInB += numColsB; /* Decrement loop counter */ colCnt--; } /* Loop unrolling: Compute remaining column */ colCnt = numColsA % 4U; #else /* Initialize colCnt with number of columns */ colCnt = numColsA; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (colCnt > 0U) { #if defined (ARM_MATH_DSP) sum1 = __SMMLA(*pInA++, *pInB, sum1); #else sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) *pInA++ * *pInB)) >> 32); #endif pInB += numColsB; colCnt--; } /* Convert the result from 2.30 to 1.31 format and store in destination buffer */ *px = sum1 << 1; px += numColsB; /* Decrement row loop counter */ row--; } } /* Compute remaining output row */ if (numRowsA & 1U) { /* point to last row in output matrix */ px = pDst->pData + (numColsB) * (numRowsA-1); col = numColsB; i = 0U; /* col loop */ while (col > 0) { /* point to last row in matrix A */ pInA = pSrcA->pData + (numRowsA-1) * numColsA; pInB = pSrcB->pData + i; /* Set variable sum1, that acts as accumulator, to zero */ sum1 = 0; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 columns at a time. */ colCnt = numColsA >> 2U; /* matrix multiplication */ while (colCnt > 0U) { inA1 = *pInA++; inA2 = *pInA++; inB1 = *pInB; pInB += numColsB; inB2 = *pInB; pInB += numColsB; #if defined (ARM_MATH_DSP) sum1 = __SMMLA(inA1, inB1, sum1); sum1 = __SMMLA(inA2, inB2, sum1); #else sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) inA1 * inB1)) >> 32); sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) inA2 * inB2)) >> 32); #endif inA1 = *pInA++; inA2 = *pInA++; inB1 = *pInB; pInB += numColsB; inB2 = *pInB; pInB += numColsB; #if defined (ARM_MATH_DSP) sum1 = __SMMLA(inA1, inB1, sum1); sum1 = __SMMLA(inA2, inB2, sum1); #else sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) inA1 * inB1)) >> 32); sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) inA2 * inB2)) >> 32); #endif /* Decrement loop counter */ colCnt--; } /* Loop unrolling: Compute remaining column */ colCnt = numColsA % 4U; #else /* Initialize colCnt with number of columns */ colCnt = numColsA; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (colCnt > 0U) { #if defined (ARM_MATH_DSP) sum1 = __SMMLA(*pInA++, *pInB, sum1); #else sum1 = (q31_t) ((((q63_t) sum1 << 32) + ((q63_t) *pInA++ * *pInB)) >> 32); #endif pInB += numColsB; colCnt--; } /* Saturate and store the result in the destination buffer */ *px++ = sum1 << 1; i++; /* Decrement col loop counter */ col--; } } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixMult group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_mult_fast_q31.c
C
apache-2.0
11,190
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_mult_q15.c * Description: Q15 matrix multiplication * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixMult @{ */ /** @brief Q15 matrix multiplication. @param[in] pSrcA points to the first input matrix structure @param[in] pSrcB points to the second input matrix structure @param[out] pDst points to output matrix structure @param[in] pState points to the array for storing intermediate results (Unused) @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The inputs to the multiplications are in 1.15 format and multiplications yield a 2.30 result. The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format. This approach provides 33 guard bits and there is no risk of overflow. The 34.30 result is then truncated to 34.15 format by discarding the low 15 bits and then saturated to 1.15 format. @par Refer to \ref arm_mat_mult_fast_q15() for a faster but less precise version of this function. */ arm_status arm_mat_mult_q15( const arm_matrix_instance_q15 * pSrcA, const arm_matrix_instance_q15 * pSrcB, arm_matrix_instance_q15 * pDst, q15_t * pState) { q63_t sum; /* Accumulator */ #if defined (ARM_MATH_DSP) /* != CM0 */ q15_t *pSrcBT = pState; /* Input data matrix pointer for transpose */ q15_t *pInA = pSrcA->pData; /* Input data matrix pointer A of Q15 type */ q15_t *pInB = pSrcB->pData; /* Input data matrix pointer B of Q15 type */ q15_t *px; /* Temporary output data matrix pointer */ uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */ uint16_t numRowsB = pSrcB->numRows; /* Number of rows of input matrix A */ uint32_t col, i = 0U, row = numRowsB, colCnt; /* Loop counters */ arm_status status; /* Status of matrix multiplication */ q31_t in; /* Temporary variable to hold the input value */ q31_t inA1, inB1, inA2, inB2; #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Matrix transpose */ do { /* The pointer px is set to starting address of column being processed */ px = pSrcBT + i; /* Apply loop unrolling and exchange columns with row elements */ col = numColsB >> 2U; /* First part of the processing with loop unrolling. Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (col > 0U) { /* Read two elements from row */ in = read_q15x2_ia ((q15_t **) &pInB); /* Unpack and store one element in destination */ #ifndef ARM_MATH_BIG_ENDIAN *px = (q15_t) in; #else *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Update pointer px to point to next row of transposed matrix */ px += numRowsB; /* Unpack and store second element in destination */ #ifndef ARM_MATH_BIG_ENDIAN *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #else *px = (q15_t) in; #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Update pointer px to point to next row of transposed matrix */ px += numRowsB; /* Read two elements from row */ in = read_q15x2_ia ((q15_t **) &pInB); /* Unpack and store one element in destination */ #ifndef ARM_MATH_BIG_ENDIAN *px = (q15_t) in; #else *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ px += numRowsB; #ifndef ARM_MATH_BIG_ENDIAN *px = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #else *px = (q15_t) in; #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ px += numRowsB; /* Decrement column loop counter */ col--; } /* If the columns of pSrcB is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ col = numColsB % 0x4U; while (col > 0U) { /* Read and store input element in destination */ *px = *pInB++; /* Update pointer px to point to next row of transposed matrix */ px += numRowsB; /* Decrement column loop counter */ col--; } i++; /* Decrement row loop counter */ row--; } while (row > 0U); /* Reset variables for usage in following multiplication process */ row = numRowsA; i = 0U; px = pDst->pData; /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* row loop */ do { /* For every row wise process, column loop counter is to be initiated */ col = numColsB; /* For every row wise process, pIn2 pointer is set to starting address of transposed pSrcB data */ pInB = pSrcBT; /* column loop */ do { /* Set variable sum, that acts as accumulator, to zero */ sum = 0; /* Initiate pointer pInA to point to starting address of column being processed */ pInA = pSrcA->pData + i; /* Apply loop unrolling and compute 2 MACs simultaneously. */ colCnt = numColsA >> 2U; /* matrix multiplication */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ /* read real and imag values from pSrcA and pSrcB buffer */ inA1 = read_q15x2_ia ((q15_t **) &pInA); inB1 = read_q15x2_ia ((q15_t **) &pInB); inA2 = read_q15x2_ia ((q15_t **) &pInA); inB2 = read_q15x2_ia ((q15_t **) &pInB); /* Multiply and Accumlates */ sum = __SMLALD(inA1, inB1, sum); sum = __SMLALD(inA2, inB2, sum); /* Decrement loop counter */ colCnt--; } /* process remaining column samples */ colCnt = numColsA % 0x4U; while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ sum += *pInA++ * *pInB++; /* Decrement loop counter */ colCnt--; } /* Saturate and store result in destination buffer */ *px = (q15_t) (__SSAT((sum >> 15), 16)); px++; /* Decrement column loop counter */ col--; } while (col > 0U); i = i + numColsA; /* Decrement row loop counter */ row--; } while (row > 0U); #else /* #if defined (ARM_MATH_DSP) */ q15_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */ q15_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */ q15_t *pInA = pSrcA->pData; /* Input data matrix pointer A of Q15 type */ q15_t *pInB = pSrcB->pData; /* Input data matrix pointer B of Q15 type */ q15_t *pOut = pDst->pData; /* Output data matrix pointer */ q15_t *px; /* Temporary output data matrix pointer */ uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */ uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */ uint32_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */ arm_status status; /* Status of matrix multiplication */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* row loop */ do { /* Output pointer is set to starting address of the row being processed */ px = pOut + i; /* For every row wise process, column loop counter is to be initiated */ col = numColsB; /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */ pIn2 = pSrcB->pData; /* column loop */ do { /* Set the variable sum, that acts as accumulator, to zero */ sum = 0; /* Initiate pointer pIn1 to point to starting address of pSrcA */ pIn1 = pInA; /* Matrix A columns number of MAC operations are to be performed */ colCnt = numColsA; /* matrix multiplication */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ /* Perform multiply-accumulates */ sum += (q31_t) * pIn1++ * *pIn2; pIn2 += numColsB; /* Decrement loop counter */ colCnt--; } /* Convert result from 34.30 to 1.15 format and store saturated value in destination buffer */ /* Saturate and store result in destination buffer */ *px++ = (q15_t) __SSAT((sum >> 15), 16); /* Decrement column loop counter */ col--; /* Update pointer pIn2 to point to starting address of next column */ pIn2 = pInB + (numColsB - col); } while (col > 0U); /* Update pointer pSrcA to point to starting address of next row */ i = i + numColsB; pInA = pInA + numColsA; /* Decrement row loop counter */ row--; } while (row > 0U); #endif /* #if defined (ARM_MATH_DSP) */ /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixMult group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_mult_q15.c
C
apache-2.0
11,943
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_mult_q31.c * Description: Q31 matrix multiplication * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixMult @{ */ /** @brief Q31 matrix multiplication. @param[in] pSrcA points to the first input matrix structure @param[in] pSrcB points to the second input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit. There is no saturation on intermediate additions. Thus, if the accumulator overflows it wraps around and distorts the result. The input signals should be scaled down to avoid intermediate overflows. The input is thus scaled down by log2(numColsA) bits to avoid overflows, as a total of numColsA additions are performed internally. The 2.62 accumulator is right shifted by 31 bits and saturated to 1.31 format to yield the final result. @remark Refer to \ref arm_mat_mult_fast_q31() for a faster but less precise implementation of this function. */ arm_status arm_mat_mult_q31( const arm_matrix_instance_q31 * pSrcA, const arm_matrix_instance_q31 * pSrcB, arm_matrix_instance_q31 * pDst) { q31_t *pIn1 = pSrcA->pData; /* Input data matrix pointer A */ q31_t *pIn2 = pSrcB->pData; /* Input data matrix pointer B */ q31_t *pInA = pSrcA->pData; /* Input data matrix pointer A */ q31_t *pInB = pSrcB->pData; /* Input data matrix pointer B */ q31_t *pOut = pDst->pData; /* Output data matrix pointer */ q31_t *px; /* Temporary output data matrix pointer */ q63_t sum; /* Accumulator */ uint16_t numRowsA = pSrcA->numRows; /* Number of rows of input matrix A */ uint16_t numColsB = pSrcB->numCols; /* Number of columns of input matrix B */ uint16_t numColsA = pSrcA->numCols; /* Number of columns of input matrix A */ uint32_t col, i = 0U, row = numRowsA, colCnt; /* Loop counters */ arm_status status; /* Status of matrix multiplication */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numCols != pSrcB->numRows) || (pSrcA->numRows != pDst->numRows) || (pSrcB->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* The following loop performs the dot-product of each row in pSrcA with each column in pSrcB */ /* row loop */ do { /* Output pointer is set to starting address of row being processed */ px = pOut + i; /* For every row wise process, column loop counter is to be initiated */ col = numColsB; /* For every row wise process, pIn2 pointer is set to starting address of pSrcB data */ pIn2 = pSrcB->pData; /* column loop */ do { /* Set the variable sum, that acts as accumulator, to zero */ sum = 0; /* Initialize pointer pIn1 to point to starting address of column being processed */ pIn1 = pInA; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 MACs at a time. */ colCnt = numColsA >> 2U; /* matrix multiplication */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ /* Perform the multiply-accumulates */ sum += (q63_t) *pIn1++ * *pIn2; pIn2 += numColsB; sum += (q63_t) *pIn1++ * *pIn2; pIn2 += numColsB; sum += (q63_t) *pIn1++ * *pIn2; pIn2 += numColsB; sum += (q63_t) *pIn1++ * *pIn2; pIn2 += numColsB; /* Decrement loop counter */ colCnt--; } /* Loop unrolling: Compute remaining MACs */ colCnt = numColsA % 0x4U; #else /* Initialize cntCnt with number of columns */ colCnt = numColsA; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (colCnt > 0U) { /* c(m,n) = a(1,1) * b(1,1) + a(1,2) * b(2,1) + .... + a(m,p) * b(p,n) */ /* Perform the multiply-accumulates */ sum += (q63_t) *pIn1++ * *pIn2; pIn2 += numColsB; /* Decrement loop counter */ colCnt--; } /* Convert result from 2.62 to 1.31 format and store in destination buffer */ *px++ = (q31_t) (sum >> 31); /* Decrement column loop counter */ col--; /* Update pointer pIn2 to point to starting address of next column */ pIn2 = pInB + (numColsB - col); } while (col > 0U); /* Update pointer pInA to point to starting address of next row */ i = i + numColsB; pInA = pInA + numColsA; /* Decrement row loop counter */ row--; } while (row > 0U); /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixMult group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_mult_q31.c
C
apache-2.0
6,678
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_scale_f32.c * Description: Multiplies a floating-point matrix by a scalar * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @defgroup MatrixScale Matrix Scale Multiplies a matrix by a scalar. This is accomplished by multiplying each element in the matrix by the scalar. For example: \image html MatrixScale.gif "Matrix Scaling of a 3 x 3 matrix" The function checks to make sure that the input and output matrices are of the same size. In the fixed-point Q15 and Q31 functions, <code>scale</code> is represented by a fractional multiplication <code>scaleFract</code> and an arithmetic shift <code>shift</code>. The shift allows the gain of the scaling operation to exceed 1.0. The overall scale factor applied to the fixed-point data is <pre> scale = scaleFract * 2^shift. </pre> */ /** @addtogroup MatrixScale @{ */ /** @brief Floating-point matrix scaling. @param[in] pSrc points to input matrix @param[in] scale scale factor to be applied @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed */ #if defined(ARM_MATH_NEON_EXPERIMENTAL) arm_status arm_mat_scale_f32( const arm_matrix_instance_f32 * pSrc, float32_t scale, arm_matrix_instance_f32 * pDst) { float32_t *pIn = pSrc->pData; /* input data matrix pointer */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ uint32_t numSamples; /* total number of elements in the matrix */ uint32_t blkCnt; /* loop counters */ arm_status status; /* status of matrix scaling */ float32_t in1, in2, in3, in4; /* temporary variables */ float32_t out1, out2, out3, out4; /* temporary variables */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols)) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { float32x4_t vec1; float32x4_t res; /* Total number of samples in the input matrix */ numSamples = (uint32_t) pSrc->numRows * pSrc->numCols; blkCnt = numSamples >> 2; /* Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) * scale */ /* Scaling and results are stored in the destination buffer. */ vec1 = vld1q_f32(pIn); res = vmulq_f32(vec1, vdupq_n_f32(scale)); vst1q_f32(pOut, res); /* update pointers to process next sampels */ pIn += 4U; pOut += 4U; /* Decrement the numSamples loop counter */ blkCnt--; } /* If the numSamples is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ blkCnt = numSamples % 0x4U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) * scale */ /* The results are stored in the destination buffer. */ *pOut++ = (*pIn++) * scale; /* Decrement the loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #else arm_status arm_mat_scale_f32( const arm_matrix_instance_f32 * pSrc, float32_t scale, arm_matrix_instance_f32 * pDst) { float32_t *pIn = pSrc->pData; /* Input data matrix pointer */ float32_t *pOut = pDst->pData; /* Output data matrix pointer */ uint32_t numSamples; /* Total number of elements in the matrix */ uint32_t blkCnt; /* Loop counters */ arm_status status; /* Status of matrix scaling */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Total number of samples in input matrix */ numSamples = (uint32_t) pSrc->numRows * pSrc->numCols; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = numSamples >> 2U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) * scale */ /* Scale and store result in destination buffer. */ *pOut++ = (*pIn++) * scale; *pOut++ = (*pIn++) * scale; *pOut++ = (*pIn++) * scale; *pOut++ = (*pIn++) * scale; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = numSamples % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = numSamples; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) * scale */ /* Scale and store result in destination buffer. */ *pOut++ = (*pIn++) * scale; /* Decrement loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of MatrixScale group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_scale_f32.c
C
apache-2.0
6,563
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_scale_q15.c * Description: Multiplies a Q15 matrix by a scalar * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixScale @{ */ /** @brief Q15 matrix scaling. @param[in] pSrc points to input matrix @param[in] scaleFract fractional portion of the scale factor @param[in] shift number of bits to shift the result by @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format. These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format. */ arm_status arm_mat_scale_q15( const arm_matrix_instance_q15 * pSrc, q15_t scaleFract, int32_t shift, arm_matrix_instance_q15 * pDst) { q15_t *pIn = pSrc->pData; /* Input data matrix pointer */ q15_t *pOut = pDst->pData; /* Output data matrix pointer */ uint32_t numSamples; /* Total number of elements in the matrix */ uint32_t blkCnt; /* Loop counter */ arm_status status; /* Status of matrix scaling */ int32_t kShift = 15 - shift; /* Total shift to apply after scaling */ #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP) q31_t inA1, inA2; q31_t out1, out2, out3, out4; /* Temporary output variables */ q15_t in1, in2, in3, in4; /* Temporary input variables */ #endif #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Total number of samples in input matrix */ numSamples = (uint32_t) pSrc->numRows * pSrc->numCols; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = numSamples >> 2U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) * k */ #if defined (ARM_MATH_DSP) /* read 2 times 2 samples at a time from source */ inA1 = read_q15x2_ia ((q15_t **) &pIn); inA2 = read_q15x2_ia ((q15_t **) &pIn); /* Scale inputs and store result in temporary variables * in single cycle by packing the outputs */ out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract); out2 = (q31_t) ((q15_t) (inA1 ) * scaleFract); out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract); out4 = (q31_t) ((q15_t) (inA2 ) * scaleFract); /* apply shifting */ out1 = out1 >> kShift; out2 = out2 >> kShift; out3 = out3 >> kShift; out4 = out4 >> kShift; /* saturate the output */ in1 = (q15_t) (__SSAT(out1, 16)); in2 = (q15_t) (__SSAT(out2, 16)); in3 = (q15_t) (__SSAT(out3, 16)); in4 = (q15_t) (__SSAT(out4, 16)); /* store result to destination */ write_q15x2_ia (&pOut, __PKHBT(in2, in1, 16)); write_q15x2_ia (&pOut, __PKHBT(in4, in3, 16)); #else *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16)); *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16)); *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16)); *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16)); #endif /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = numSamples % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = numSamples; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) * k */ /* Scale, saturate and store result in destination buffer. */ *pOut++ = (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> kShift, 16)); /* Decrement loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixScale group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_scale_q15.c
C
apache-2.0
5,552
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_scale_q31.c * Description: Multiplies a Q31 matrix by a scalar * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixScale @{ */ /** @brief Q31 matrix scaling. @param[in] pSrc points to input matrix @param[in] scaleFract fractional portion of the scale factor @param[in] shift number of bits to shift the result by @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.31 format. These are multiplied to yield a 2.62 intermediate result which is shifted with saturation to 1.31 format. */ arm_status arm_mat_scale_q31( const arm_matrix_instance_q31 * pSrc, q31_t scaleFract, int32_t shift, arm_matrix_instance_q31 * pDst) { q31_t *pIn = pSrc->pData; /* Input data matrix pointer */ q31_t *pOut = pDst->pData; /* Output data matrix pointer */ uint32_t numSamples; /* Total number of elements in the matrix */ uint32_t blkCnt; /* Loop counter */ arm_status status; /* Status of matrix scaling */ int32_t kShift = shift + 1; /* Shift to apply after scaling */ q31_t in, out; /* Temporary variabels */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Total number of samples in input matrix */ numSamples = (uint32_t) pSrc->numRows * pSrc->numCols; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = numSamples >> 2U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) * k */ /* Scale, saturate and store result in destination buffer. */ in = *pIn++; /* read four inputs from source */ in = ((q63_t) in * scaleFract) >> 32; /* multiply input with scaler value */ out = in << kShift; /* apply shifting */ if (in != (out >> kShift)) /* saturate the results. */ out = 0x7FFFFFFF ^ (in >> 31); *pOut++ = out; /* Store result destination */ in = *pIn++; in = ((q63_t) in * scaleFract) >> 32; out = in << kShift; if (in != (out >> kShift)) out = 0x7FFFFFFF ^ (in >> 31); *pOut++ = out; in = *pIn++; in = ((q63_t) in * scaleFract) >> 32; out = in << kShift; if (in != (out >> kShift)) out = 0x7FFFFFFF ^ (in >> 31); *pOut++ = out; in = *pIn++; in = ((q63_t) in * scaleFract) >> 32; out = in << kShift; if (in != (out >> kShift)) out = 0x7FFFFFFF ^ (in >> 31); *pOut++ = out; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = numSamples % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = numSamples; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) * k */ /* Scale, saturate and store result in destination buffer. */ in = *pIn++; in = ((q63_t) in * scaleFract) >> 32; out = in << kShift; if (in != (out >> kShift)) out = 0x7FFFFFFF ^ (in >> 31); *pOut++ = out; /* Decrement loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixScale group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_scale_q31.c
C
apache-2.0
5,135
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_sub_f32.c * Description: Floating-point matrix subtraction * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @defgroup MatrixSub Matrix Subtraction Subtract two matrices. \image html MatrixSubtraction.gif "Subraction of two 3 x 3 matrices" The functions check to make sure that <code>pSrcA</code>, <code>pSrcB</code>, and <code>pDst</code> have the same number of rows and columns. */ /** @addtogroup MatrixSub @{ */ /** @brief Floating-point matrix subtraction. @param[in] pSrcA points to the first input matrix structure @param[in] pSrcB points to the second input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed */ #if defined(ARM_MATH_NEON) arm_status arm_mat_sub_f32( const arm_matrix_instance_f32 * pSrcA, const arm_matrix_instance_f32 * pSrcB, arm_matrix_instance_f32 * pDst) { float32_t *pIn1 = pSrcA->pData; /* input data matrix pointer A */ float32_t *pIn2 = pSrcB->pData; /* input data matrix pointer B */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ float32_t inA1, inA2, inB1, inB2, out1, out2; /* temporary variables */ uint32_t numSamples; /* total number of elements in the matrix */ uint32_t blkCnt; /* loop counters */ arm_status status; /* status of matrix subtraction */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numRows != pSrcB->numRows) || (pSrcA->numCols != pSrcB->numCols) || (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols)) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { float32x4_t vec1; float32x4_t vec2; float32x4_t res; /* Total number of samples in the input matrix */ numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols; blkCnt = numSamples >> 2U; /* Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) - B(m,n) */ /* Subtract and then store the results in the destination buffer. */ /* Read values from source A */ vec1 = vld1q_f32(pIn1); vec2 = vld1q_f32(pIn2); res = vsubq_f32(vec1, vec2); vst1q_f32(pOut, res); /* Update pointers to process next samples */ pIn1 += 4U; pIn2 += 4U; pOut += 4U; /* Decrement the loop counter */ blkCnt--; } /* If the numSamples is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ blkCnt = numSamples % 0x4U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) - B(m,n) */ /* Subtract and then store the results in the destination buffer. */ *pOut++ = (*pIn1++) - (*pIn2++); /* Decrement the loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #else arm_status arm_mat_sub_f32( const arm_matrix_instance_f32 * pSrcA, const arm_matrix_instance_f32 * pSrcB, arm_matrix_instance_f32 * pDst) { float32_t *pInA = pSrcA->pData; /* input data matrix pointer A */ float32_t *pInB = pSrcB->pData; /* input data matrix pointer B */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ uint32_t numSamples; /* total number of elements in the matrix */ uint32_t blkCnt; /* loop counters */ arm_status status; /* status of matrix subtraction */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numRows != pSrcB->numRows) || (pSrcA->numCols != pSrcB->numCols) || (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Total number of samples in input matrix */ numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = numSamples >> 2U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) - B(m,n) */ /* Subtract and store result in destination buffer. */ *pOut++ = (*pInA++) - (*pInB++); *pOut++ = (*pInA++) - (*pInB++); *pOut++ = (*pInA++) - (*pInB++); *pOut++ = (*pInA++) - (*pInB++); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = numSamples % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = numSamples; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) - B(m,n) */ /* Subtract and store result in destination buffer. */ *pOut++ = (*pInA++) - (*pInB++); /* Decrement loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of MatrixSub group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_sub_f32.c
C
apache-2.0
6,622
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_sub_q15.c * Description: Q15 Matrix subtraction * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixSub @{ */ /** @brief Q15 matrix subtraction. @param[in] pSrcA points to the first input matrix structure @param[in] pSrcB points to the second input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The function uses saturating arithmetic. Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated. */ arm_status arm_mat_sub_q15( const arm_matrix_instance_q15 * pSrcA, const arm_matrix_instance_q15 * pSrcB, arm_matrix_instance_q15 * pDst) { q15_t *pInA = pSrcA->pData; /* input data matrix pointer A */ q15_t *pInB = pSrcB->pData; /* input data matrix pointer B */ q15_t *pOut = pDst->pData; /* output data matrix pointer */ uint32_t numSamples; /* total number of elements in the matrix */ uint32_t blkCnt; /* loop counters */ arm_status status; /* status of matrix subtraction */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numRows != pSrcB->numRows) || (pSrcA->numCols != pSrcB->numCols) || (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Total number of samples in input matrix */ numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = numSamples >> 2U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) - B(m,n) */ /* Subtract, Saturate and store result in destination buffer. */ #if defined (ARM_MATH_DSP) write_q15x2_ia (&pOut, __QSUB16(read_q15x2_ia ((q15_t **) &pInA), read_q15x2_ia ((q15_t **) &pInB))); write_q15x2_ia (&pOut, __QSUB16(read_q15x2_ia ((q15_t **) &pInA), read_q15x2_ia ((q15_t **) &pInB))); #else *pOut++ = (q15_t) __SSAT(((q31_t) * pInA++ - *pInB++), 16); *pOut++ = (q15_t) __SSAT(((q31_t) * pInA++ - *pInB++), 16); *pOut++ = (q15_t) __SSAT(((q31_t) * pInA++ - *pInB++), 16); *pOut++ = (q15_t) __SSAT(((q31_t) * pInA++ - *pInB++), 16); #endif /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = numSamples % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = numSamples; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) - B(m,n) */ /* Subtract and store result in destination buffer. */ #if defined (ARM_MATH_DSP) *pOut++ = (q15_t) __QSUB16(*pInA++, *pInB++); #else *pOut++ = (q15_t) __SSAT(((q31_t) * pInA++ - *pInB++), 16); #endif /* Decrement loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixSub group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_sub_q15.c
C
apache-2.0
4,523
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_sub_q31.c * Description: Q31 matrix subtraction * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixSub @{ */ /** @brief Q31 matrix subtraction. @param[in] pSrcA points to the first input matrix structure @param[in] pSrcB points to the second input matrix structure @param[out] pDst points to output matrix structure @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed @par Scaling and Overflow Behavior The function uses saturating arithmetic. Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] are saturated. */ arm_status arm_mat_sub_q31( const arm_matrix_instance_q31 * pSrcA, const arm_matrix_instance_q31 * pSrcB, arm_matrix_instance_q31 * pDst) { q31_t *pInA = pSrcA->pData; /* input data matrix pointer A */ q31_t *pInB = pSrcB->pData; /* input data matrix pointer B */ q31_t *pOut = pDst->pData; /* output data matrix pointer */ uint32_t numSamples; /* total number of elements in the matrix */ uint32_t blkCnt; /* loop counters */ arm_status status; /* status of matrix subtraction */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrcA->numRows != pSrcB->numRows) || (pSrcA->numCols != pSrcB->numCols) || (pSrcA->numRows != pDst->numRows) || (pSrcA->numCols != pDst->numCols) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Total number of samples in input matrix */ numSamples = (uint32_t) pSrcA->numRows * pSrcA->numCols; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = numSamples >> 2U; while (blkCnt > 0U) { /* C(m,n) = A(m,n) - B(m,n) */ /* Subtract, saturate and then store the results in the destination buffer. */ *pOut++ = __QSUB(*pInA++, *pInB++); *pOut++ = __QSUB(*pInA++, *pInB++); *pOut++ = __QSUB(*pInA++, *pInB++); *pOut++ = __QSUB(*pInA++, *pInB++); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = numSamples % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = numSamples; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C(m,n) = A(m,n) - B(m,n) */ /* Subtract, saturate and store result in destination buffer. */ *pOut++ = __QSUB(*pInA++, *pInB++); /* Decrement loop counter */ blkCnt--; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixSub group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_sub_q31.c
C
apache-2.0
4,053
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_trans_f32.c * Description: Floating-point matrix transpose * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @defgroup MatrixTrans Matrix Transpose Tranposes a matrix. Transposing an <code>M x N</code> matrix flips it around the center diagonal and results in an <code>N x M</code> matrix. \image html MatrixTranspose.gif "Transpose of a 3 x 3 matrix" */ /** @addtogroup MatrixTrans @{ */ /** @brief Floating-point matrix transpose. @param[in] pSrc points to input matrix @param[out] pDst points to output matrix @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed */ #if defined(ARM_MATH_NEON) arm_status arm_mat_trans_f32( const arm_matrix_instance_f32 * pSrc, arm_matrix_instance_f32 * pDst) { float32_t *pIn = pSrc->pData; /* input data matrix pointer */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ float32_t *px; /* Temporary output data matrix pointer */ uint16_t nRows = pSrc->numRows; /* number of rows */ uint16_t nColumns = pSrc->numCols; /* number of columns */ uint16_t blkCnt, rowCnt, i = 0U, row = nRows; /* loop counters */ arm_status status; /* status of matrix transpose */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows)) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Matrix transpose by exchanging the rows with columns */ /* Row loop */ rowCnt = row >> 2; while (rowCnt > 0U) { float32x4_t row0V,row1V,row2V,row3V; float32x4x2_t ra0,ra1,rb0,rb1; blkCnt = nColumns >> 2; /* The pointer px is set to starting address of the column being processed */ px = pOut + i; /* Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (blkCnt > 0U) /* Column loop */ { row0V = vld1q_f32(pIn); row1V = vld1q_f32(pIn + 1 * nColumns); row2V = vld1q_f32(pIn + 2 * nColumns); row3V = vld1q_f32(pIn + 3 * nColumns); pIn += 4; ra0 = vzipq_f32(row0V,row2V); ra1 = vzipq_f32(row1V,row3V); rb0 = vzipq_f32(ra0.val[0],ra1.val[0]); rb1 = vzipq_f32(ra0.val[1],ra1.val[1]); vst1q_f32(px,rb0.val[0]); px += nRows; vst1q_f32(px,rb0.val[1]); px += nRows; vst1q_f32(px,rb1.val[0]); px += nRows; vst1q_f32(px,rb1.val[1]); px += nRows; /* Decrement the column loop counter */ blkCnt--; } /* Perform matrix transpose for last 3 samples here. */ blkCnt = nColumns % 0x4U; while (blkCnt > 0U) { /* Read and store the input element in the destination */ *px++ = *pIn; *px++ = *(pIn + 1 * nColumns); *px++ = *(pIn + 2 * nColumns); *px++ = *(pIn + 3 * nColumns); px += (nRows - 4); pIn++; /* Decrement the column loop counter */ blkCnt--; } i += 4; pIn += 3 * nColumns; /* Decrement the row loop counter */ rowCnt--; } /* Row loop end */ rowCnt = row & 3; while (rowCnt > 0U) { blkCnt = nColumns ; /* The pointer px is set to starting address of the column being processed */ px = pOut + i; while (blkCnt > 0U) { /* Read and store the input element in the destination */ *px = *pIn++; /* Update the pointer px to point to the next row of the transposed matrix */ px += nRows; /* Decrement the column loop counter */ blkCnt--; } i++; rowCnt -- ; } /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #else arm_status arm_mat_trans_f32( const arm_matrix_instance_f32 * pSrc, arm_matrix_instance_f32 * pDst) { float32_t *pIn = pSrc->pData; /* input data matrix pointer */ float32_t *pOut = pDst->pData; /* output data matrix pointer */ float32_t *px; /* Temporary output data matrix pointer */ uint16_t nRows = pSrc->numRows; /* number of rows */ uint16_t nCols = pSrc->numCols; /* number of columns */ uint32_t col, row = nRows, i = 0U; /* Loop counters */ arm_status status; /* status of matrix transpose */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Matrix transpose by exchanging the rows with columns */ /* row loop */ do { /* Pointer px is set to starting address of column being processed */ px = pOut + i; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ col = nCols >> 2U; while (col > 0U) /* column loop */ { /* Read and store input element in destination */ *px = *pIn++; /* Update pointer px to point to next row of transposed matrix */ px += nRows; *px = *pIn++; px += nRows; *px = *pIn++; px += nRows; *px = *pIn++; px += nRows; /* Decrement column loop counter */ col--; } /* Loop unrolling: Compute remaining outputs */ col = nCols % 0x4U; #else /* Initialize col with number of samples */ col = nCols; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (col > 0U) { /* Read and store input element in destination */ *px = *pIn++; /* Update pointer px to point to next row of transposed matrix */ px += nRows; /* Decrement column loop counter */ col--; } i++; /* Decrement row loop counter */ row--; } while (row > 0U); /* row loop end */ /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } #endif /* #if defined(ARM_MATH_NEON) */ /** * @} end of MatrixTrans group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_trans_f32.c
C
apache-2.0
7,697
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_trans_q15.c * Description: Q15 matrix transpose * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixTrans @{ */ /** @brief Q15 matrix transpose. @param[in] pSrc points to input matrix @param[out] pDst points to output matrix @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed */ arm_status arm_mat_trans_q15( const arm_matrix_instance_q15 * pSrc, arm_matrix_instance_q15 * pDst) { q15_t *pIn = pSrc->pData; /* input data matrix pointer */ q15_t *pOut = pDst->pData; /* output data matrix pointer */ uint16_t nRows = pSrc->numRows; /* number of rows */ uint16_t nCols = pSrc->numCols; /* number of columns */ uint32_t col, row = nRows, i = 0U; /* Loop counters */ arm_status status; /* status of matrix transpose */ #if defined (ARM_MATH_LOOPUNROLL) q31_t in; /* variable to hold temporary output */ #endif #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Matrix transpose by exchanging the rows with columns */ /* row loop */ do { /* Pointer pOut is set to starting address of column being processed */ pOut = pDst->pData + i; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ col = nCols >> 2U; while (col > 0U) /* column loop */ { /* Read two elements from row */ in = read_q15x2_ia ((q15_t **) &pIn); /* Unpack and store one element in destination */ #ifndef ARM_MATH_BIG_ENDIAN *pOut = (q15_t) in; #else *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Update pointer pOut to point to next row of transposed matrix */ pOut += nRows; /* Unpack and store second element in destination */ #ifndef ARM_MATH_BIG_ENDIAN *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #else *pOut = (q15_t) in; #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Update pointer pOut to point to next row of transposed matrix */ pOut += nRows; /* Read two elements from row */ in = read_q15x2_ia ((q15_t **) &pIn); /* Unpack and store one element in destination */ #ifndef ARM_MATH_BIG_ENDIAN *pOut = (q15_t) in; #else *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Update pointer pOut to point to next row of transposed matrix */ pOut += nRows; /* Unpack and store second element in destination */ #ifndef ARM_MATH_BIG_ENDIAN *pOut = (q15_t) ((in & (q31_t) 0xffff0000) >> 16); #else *pOut = (q15_t) in; #endif /* #ifndef ARM_MATH_BIG_ENDIAN */ /* Update pointer pOut to point to next row of transposed matrix */ pOut += nRows; /* Decrement column loop counter */ col--; } /* Loop unrolling: Compute remaining outputs */ col = nCols % 0x4U; #else /* Initialize col with number of samples */ col = nCols; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (col > 0U) { /* Read and store input element in destination */ *pOut = *pIn++; /* Update pointer pOut to point to next row of transposed matrix */ pOut += nRows; /* Decrement column loop counter */ col--; } i++; /* Decrement row loop counter */ row--; } while (row > 0U); /* row loop end */ /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixTrans group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_trans_q15.c
C
apache-2.0
5,192
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mat_trans_q31.c * Description: Q31 matrix transpose * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupMatrix */ /** @addtogroup MatrixTrans @{ */ /** @brief Q31 matrix transpose. @param[in] pSrc points to input matrix @param[out] pDst points to output matrix @return execution status - \ref ARM_MATH_SUCCESS : Operation successful - \ref ARM_MATH_SIZE_MISMATCH : Matrix size check failed */ arm_status arm_mat_trans_q31( const arm_matrix_instance_q31 * pSrc, arm_matrix_instance_q31 * pDst) { q31_t *pIn = pSrc->pData; /* input data matrix pointer */ q31_t *pOut = pDst->pData; /* output data matrix pointer */ q31_t *px; /* Temporary output data matrix pointer */ uint16_t nRows = pSrc->numRows; /* number of rows */ uint16_t nCols = pSrc->numCols; /* number of columns */ uint32_t col, row = nRows, i = 0U; /* Loop counters */ arm_status status; /* status of matrix transpose */ #ifdef ARM_MATH_MATRIX_CHECK /* Check for matrix mismatch condition */ if ((pSrc->numRows != pDst->numCols) || (pSrc->numCols != pDst->numRows) ) { /* Set status as ARM_MATH_SIZE_MISMATCH */ status = ARM_MATH_SIZE_MISMATCH; } else #endif /* #ifdef ARM_MATH_MATRIX_CHECK */ { /* Matrix transpose by exchanging the rows with columns */ /* row loop */ do { /* Pointer px is set to starting address of column being processed */ px = pOut + i; #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ col = nCols >> 2U; while (col > 0U) /* column loop */ { /* Read and store input element in destination */ *px = *pIn++; /* Update pointer px to point to next row of transposed matrix */ px += nRows; *px = *pIn++; px += nRows; *px = *pIn++; px += nRows; *px = *pIn++; px += nRows; /* Decrement column loop counter */ col--; } /* Loop unrolling: Compute remaining outputs */ col = nCols % 0x4U; #else /* Initialize col with number of samples */ col = nCols; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (col > 0U) { /* Read and store input element in destination */ *px = *pIn++; /* Update pointer px to point to next row of transposed matrix */ px += nRows; /* Decrement column loop counter */ col--; } i++; /* Decrement row loop counter */ row--; } while (row > 0U); /* row loop end */ /* Set status as ARM_MATH_SUCCESS */ status = ARM_MATH_SUCCESS; } /* Return to application */ return (status); } /** @} end of MatrixTrans group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/MatrixFunctions/arm_mat_trans_q31.c
C
apache-2.0
3,932
cmake_minimum_required (VERSION 3.6) project(CMSISDSPStatistics) file(GLOB SRC "./*_*.c") add_library(CMSISDSPStatistics STATIC ${SRC}) configdsp(CMSISDSPStatistics ..) ### Includes target_include_directories(CMSISDSPStatistics PUBLIC "${DSP}/../../Include")
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/CMakeLists.txt
CMake
apache-2.0
268
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: StatisticsFunctions.c * Description: Combination of all statistics function source files. * * $Date: 18. March 2019 * $Revision: V1.0.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_max_f32.c" #include "arm_max_q15.c" #include "arm_max_q31.c" #include "arm_max_q7.c" #include "arm_mean_f32.c" #include "arm_mean_q15.c" #include "arm_mean_q31.c" #include "arm_mean_q7.c" #include "arm_min_f32.c" #include "arm_min_q15.c" #include "arm_min_q31.c" #include "arm_min_q7.c" #include "arm_power_f32.c" #include "arm_power_q15.c" #include "arm_power_q31.c" #include "arm_power_q7.c" #include "arm_rms_f32.c" #include "arm_rms_q15.c" #include "arm_rms_q31.c" #include "arm_std_f32.c" #include "arm_std_q15.c" #include "arm_std_q31.c" #include "arm_var_f32.c" #include "arm_var_q15.c" #include "arm_var_q31.c"
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/StatisticsFunctions.c
C
apache-2.0
1,691
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_max_f32.c * Description: Maximum value of a floating-point vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" #if defined(ARM_MATH_NEON) #include <limits.h> #endif /** @ingroup groupStats */ /** @defgroup Max Maximum Computes the maximum value of an array of data. The function returns both the maximum value and its position within the array. There are separate functions for floating-point, Q31, Q15, and Q7 data types. */ /** @addtogroup Max @{ */ /** @brief Maximum value of a floating-point vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult maximum value returned here @param[out] pIndex index of maximum value returned here @return none */ #if defined(ARM_MATH_NEON) void arm_max_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult, uint32_t * pIndex) { float32_t maxVal1, maxVal2, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex, count; /* loop counter */ float32x4_t outV, srcV; float32x2_t outV2; uint32x4_t idxV; uint32x4_t maxIdx={ULONG_MAX,ULONG_MAX,ULONG_MAX,ULONG_MAX}; uint32x4_t index={4,5,6,7}; uint32x4_t delta={4,4,4,4}; uint32x4_t countV={0,1,2,3}; uint32x2_t countV2; /* Initialise the count value. */ count = 0U; /* Initialise the index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparison */ if (blockSize <= 3) { out = *pSrc++; blkCnt = blockSize - 1; while (blkCnt > 0U) { /* Initialize maxVal to the next consecutive values one by one */ maxVal1 = *pSrc++; /* compare for the maximum value */ if (out < maxVal1) { /* Update the maximum value and it's index */ out = maxVal1; outIndex = blockSize - blkCnt; } /* Decrement the loop counter */ blkCnt--; } } else { outV = vld1q_f32(pSrc); pSrc += 4; /* Compute 4 outputs at a time */ blkCnt = (blockSize - 4 ) >> 2U; while (blkCnt > 0U) { srcV = vld1q_f32(pSrc); pSrc += 4; idxV = vcgtq_f32(srcV, outV); outV = vbslq_f32(idxV, srcV, outV ); countV = vbslq_u32(idxV, index,countV ); index = vaddq_u32(index,delta); /* Decrement the loop counter */ blkCnt--; } outV2 = vpmax_f32(vget_low_f32(outV),vget_high_f32(outV)); outV2 = vpmax_f32(outV2,outV2); out = outV2[0]; idxV = vceqq_f32(outV, vdupq_n_f32(out)); countV = vbslq_u32(idxV, countV,maxIdx); countV2 = vpmin_u32(vget_low_u32(countV),vget_high_u32(countV)); countV2 = vpmin_u32(countV2,countV2); outIndex = countV2[0]; /* if (blockSize - 1U) is not multiple of 4 */ blkCnt = (blockSize - 4 ) % 4U; while (blkCnt > 0U) { /* Initialize maxVal to the next consecutive values one by one */ maxVal1 = *pSrc++; /* compare for the maximum value */ if (out < maxVal1) { /* Update the maximum value and it's index */ out = maxVal1; outIndex = blockSize - blkCnt ; } /* Decrement the loop counter */ blkCnt--; } } /* Store the maximum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } #else void arm_max_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult, uint32_t * pIndex) { float32_t maxVal, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex; /* Loop counter */ #if defined (ARM_MATH_LOOPUNROLL) uint32_t index; /* index of maximum value */ #endif /* Initialise index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparision */ out = *pSrc++; #if defined (ARM_MATH_LOOPUNROLL) /* Initialise index of maximum value. */ index = 0U; /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = (blockSize - 1U) >> 2U; while (blkCnt > 0U) { /* Initialize maxVal to next consecutive values one by one */ maxVal = *pSrc++; /* compare for the maximum value */ if (out < maxVal) { /* Update the maximum value and it's index */ out = maxVal; outIndex = index + 1U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 2U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 3U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 4U; } index += 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = (blockSize - 1U) % 4U; #else /* Initialize blkCnt with number of samples */ blkCnt = (blockSize - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Initialize maxVal to the next consecutive values one by one */ maxVal = *pSrc++; /* compare for the maximum value */ if (out < maxVal) { /* Update the maximum value and it's index */ out = maxVal; outIndex = blockSize - blkCnt; } /* Decrement loop counter */ blkCnt--; } /* Store the maximum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of Max group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_max_f32.c
C
apache-2.0
6,741
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_max_q15.c * Description: Maximum value of a Q15 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup Max @{ */ /** @brief Maximum value of a Q15 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult maximum value returned here @param[out] pIndex index of maximum value returned here @return none */ void arm_max_q15( const q15_t * pSrc, uint32_t blockSize, q15_t * pResult, uint32_t * pIndex) { q15_t maxVal, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex; /* Loop counter */ #if defined (ARM_MATH_LOOPUNROLL) uint32_t index; /* index of maximum value */ #endif /* Initialise index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparision */ out = *pSrc++; #if defined (ARM_MATH_LOOPUNROLL) /* Initialise index of maximum value. */ index = 0U; /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = (blockSize - 1U) >> 2U; while (blkCnt > 0U) { /* Initialize maxVal to next consecutive values one by one */ maxVal = *pSrc++; /* compare for the maximum value */ if (out < maxVal) { /* Update the maximum value and it's index */ out = maxVal; outIndex = index + 1U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 2U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 3U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 4U; } index += 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = (blockSize - 1U) % 4U; #else /* Initialize blkCnt with number of samples */ blkCnt = (blockSize - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Initialize maxVal to the next consecutive values one by one */ maxVal = *pSrc++; /* compare for the maximum value */ if (out < maxVal) { /* Update the maximum value and it's index */ out = maxVal; outIndex = blockSize - blkCnt; } /* Decrement loop counter */ blkCnt--; } /* Store the maximum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } /** @} end of Max group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_max_q15.c
C
apache-2.0
3,595
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_max_q31.c * Description: Maximum value of a Q31 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup Max @{ */ /** @brief Maximum value of a Q31 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult maximum value returned here @param[out] pIndex index of maximum value returned here @return none */ void arm_max_q31( const q31_t * pSrc, uint32_t blockSize, q31_t * pResult, uint32_t * pIndex) { q31_t maxVal, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex; /* Loop counter */ #if defined (ARM_MATH_LOOPUNROLL) uint32_t index; /* index of maximum value */ #endif /* Initialise index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparision */ out = *pSrc++; #if defined (ARM_MATH_LOOPUNROLL) /* Initialise index of maximum value. */ index = 0U; /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = (blockSize - 1U) >> 2U; while (blkCnt > 0U) { /* Initialize maxVal to next consecutive values one by one */ maxVal = *pSrc++; /* compare for the maximum value */ if (out < maxVal) { /* Update the maximum value and it's index */ out = maxVal; outIndex = index + 1U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 2U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 3U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 4U; } index += 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = (blockSize - 1U) % 4U; #else /* Initialize blkCnt with number of samples */ blkCnt = (blockSize - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Initialize maxVal to the next consecutive values one by one */ maxVal = *pSrc++; /* compare for the maximum value */ if (out < maxVal) { /* Update the maximum value and it's index */ out = maxVal; outIndex = blockSize - blkCnt; } /* Decrement loop counter */ blkCnt--; } /* Store the maximum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } /** @} end of Max group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_max_q31.c
C
apache-2.0
3,595
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_max_q7.c * Description: Maximum value of a Q7 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup Max @{ */ /** @brief Maximum value of a Q7 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult maximum value returned here @param[out] pIndex index of maximum value returned here @return none */ void arm_max_q7( const q7_t * pSrc, uint32_t blockSize, q7_t * pResult, uint32_t * pIndex) { q7_t maxVal, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex; /* Loop counter */ #if defined (ARM_MATH_LOOPUNROLL) uint32_t index; /* index of maximum value */ #endif /* Initialise index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparision */ out = *pSrc++; #if defined (ARM_MATH_LOOPUNROLL) /* Initialise index of maximum value. */ index = 0U; /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = (blockSize - 1U) >> 2U; while (blkCnt > 0U) { /* Initialize maxVal to next consecutive values one by one */ maxVal = *pSrc++; /* compare for the maximum value */ if (out < maxVal) { /* Update the maximum value and it's index */ out = maxVal; outIndex = index + 1U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 2U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 3U; } maxVal = *pSrc++; if (out < maxVal) { out = maxVal; outIndex = index + 4U; } index += 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = (blockSize - 1U) % 4U; #else /* Initialize blkCnt with number of samples */ blkCnt = (blockSize - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Initialize maxVal to the next consecutive values one by one */ maxVal = *pSrc++; /* compare for the maximum value */ if (out < maxVal) { /* Update the maximum value and it's index */ out = maxVal; outIndex = blockSize - blkCnt; } /* Decrement loop counter */ blkCnt--; } /* Store the maximum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } /** @} end of Max group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_max_q7.c
C
apache-2.0
3,589
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mean_f32.c * Description: Mean value of a floating-point vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @defgroup mean Mean Calculates the mean of the input vector. Mean is defined as the average of the elements in the vector. The underlying algorithm is used: <pre> Result = (pSrc[0] + pSrc[1] + pSrc[2] + ... + pSrc[blockSize-1]) / blockSize; </pre> There are separate functions for floating-point, Q31, Q15, and Q7 data types. */ /** @addtogroup mean @{ */ /** @brief Mean value of a floating-point vector. @param[in] pSrc points to the input vector. @param[in] blockSize number of samples in input vector. @param[out] pResult mean value returned here. @return none */ #if defined(ARM_MATH_NEON_EXPERIMENTAL) void arm_mean_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult) { float32_t sum = 0.0f; /* Temporary result storage */ float32x4_t sumV = vdupq_n_f32(0.0f); /* Temporary result storage */ float32x2_t sumV2; uint32_t blkCnt; /* Loop counter */ float32_t in1, in2, in3, in4; float32x4_t inV; blkCnt = blockSize >> 2U; /* Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ inV = vld1q_f32(pSrc); sumV = vaddq_f32(sumV, inV); pSrc += 4; /* Decrement the loop counter */ blkCnt--; } sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV)); sum = sumV2[0] + sumV2[1]; /* If the blockSize is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ blkCnt = blockSize & 3; while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ sum += *pSrc++; /* Decrement the loop counter */ blkCnt--; } /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */ /* Store the result to the destination */ *pResult = sum / (float32_t) blockSize; } #else void arm_mean_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult) { uint32_t blkCnt; /* Loop counter */ float32_t sum = 0.0f; /* Temporary result storage */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ sum += *pSrc++; sum += *pSrc++; sum += *pSrc++; sum += *pSrc++; /* Decrement the loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ sum += *pSrc++; /* Decrement loop counter */ blkCnt--; } /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */ /* Store result to destination */ *pResult = (sum / blockSize); } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of mean group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_mean_f32.c
C
apache-2.0
4,292
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mean_q15.c * Description: Mean value of a Q15 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup mean @{ */ /** @brief Mean value of a Q15 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult mean value returned here @return none @par Scaling and Overflow Behavior The function is implemented using a 32-bit internal accumulator. The input is represented in 1.15 format and is accumulated in a 32-bit accumulator in 17.15 format. There is no risk of internal overflow with this approach, and the full precision of intermediate result is preserved. Finally, the accumulator is truncated to yield a result of 1.15 format. */ void arm_mean_q15( const q15_t * pSrc, uint32_t blockSize, q15_t * pResult) { uint32_t blkCnt; /* Loop counter */ q31_t sum = 0; /* Temporary result storage */ #if defined (ARM_MATH_LOOPUNROLL) q31_t in; #endif #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ in = read_q15x2_ia ((q15_t **) &pSrc); sum += ((in << 16U) >> 16U); sum += (in >> 16U); in = read_q15x2_ia ((q15_t **) &pSrc); sum += ((in << 16U) >> 16U); sum += (in >> 16U); /* Decrement the loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ sum += *pSrc++; /* Decrement loop counter */ blkCnt--; } /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */ /* Store result to destination */ *pResult = (q15_t) (sum / (int32_t) blockSize); } /** @} end of mean group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_mean_q15.c
C
apache-2.0
3,210
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mean_q31.c * Description: Mean value of a Q31 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup mean @{ */ /** @brief Mean value of a Q31 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult mean value returned here @return none @par Scaling and Overflow Behavior The function is implemented using a 64-bit internal accumulator. The input is represented in 1.31 format and is accumulated in a 64-bit accumulator in 33.31 format. There is no risk of internal overflow with this approach, and the full precision of intermediate result is preserved. Finally, the accumulator is truncated to yield a result of 1.31 format. */ void arm_mean_q31( const q31_t * pSrc, uint32_t blockSize, q31_t * pResult) { uint32_t blkCnt; /* Loop counter */ q63_t sum = 0; /* Temporary result storage */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ sum += *pSrc++; sum += *pSrc++; sum += *pSrc++; sum += *pSrc++; /* Decrement the loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ sum += *pSrc++; /* Decrement loop counter */ blkCnt--; } /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */ /* Store result to destination */ *pResult = (q31_t) (sum / blockSize); } /** @} end of mean group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_mean_q31.c
C
apache-2.0
3,020
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_mean_q7.c * Description: Mean value of a Q7 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup mean @{ */ /** @brief Mean value of a Q7 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult mean value returned here @return none @par Scaling and Overflow Behavior The function is implemented using a 32-bit internal accumulator. The input is represented in 1.7 format and is accumulated in a 32-bit accumulator in 25.7 format. There is no risk of internal overflow with this approach, and the full precision of intermediate result is preserved. Finally, the accumulator is truncated to yield a result of 1.7 format. */ void arm_mean_q7( const q7_t * pSrc, uint32_t blockSize, q7_t * pResult) { uint32_t blkCnt; /* Loop counter */ q31_t sum = 0; /* Temporary result storage */ #if defined (ARM_MATH_LOOPUNROLL) q31_t in; #endif #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ in = read_q7x4_ia ((q7_t **) &pSrc); sum += ((in << 24U) >> 24U); sum += ((in << 16U) >> 24U); sum += ((in << 8U) >> 24U); sum += (in >> 24U); /* Decrement the loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ sum += *pSrc++; /* Decrement loop counter */ blkCnt--; } /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */ /* Store result to destination */ *pResult = (q7_t) (sum / (int32_t) blockSize); } /** @} end of mean group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_mean_q7.c
C
apache-2.0
3,162
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_min_f32.c * Description: Minimum value of a floating-point vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" #include <limits.h> /** @ingroup groupStats */ /** @defgroup Min Minimum Computes the minimum value of an array of data. The function returns both the minimum value and its position within the array. There are separate functions for floating-point, Q31, Q15, and Q7 data types. */ /** @addtogroup Min @{ */ /** @brief Minimum value of a floating-point vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult minimum value returned here @param[out] pIndex index of minimum value returned here @return none */ #if defined(ARM_MATH_NEON) void arm_min_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult, uint32_t * pIndex) { float32_t maxVal1, maxVal2, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex, count; /* loop counter */ float32x4_t outV, srcV; float32x2_t outV2; uint32x4_t idxV; uint32x4_t maxIdx={ULONG_MAX,ULONG_MAX,ULONG_MAX,ULONG_MAX}; uint32x4_t index={4,5,6,7}; uint32x4_t delta={4,4,4,4}; uint32x4_t countV={0,1,2,3}; uint32x2_t countV2; /* Initialise the count value. */ count = 0U; /* Initialise the index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparison */ if (blockSize <= 3) { out = *pSrc++; blkCnt = blockSize - 1; while (blkCnt > 0U) { /* Initialize maxVal to the next consecutive values one by one */ maxVal1 = *pSrc++; /* compare for the maximum value */ if (out > maxVal1) { /* Update the maximum value and it's index */ out = maxVal1; outIndex = blockSize - blkCnt; } /* Decrement the loop counter */ blkCnt--; } } else { outV = vld1q_f32(pSrc); pSrc += 4; /* Compute 4 outputs at a time */ blkCnt = (blockSize - 4 ) >> 2U; while (blkCnt > 0U) { srcV = vld1q_f32(pSrc); pSrc += 4; idxV = vcltq_f32(srcV, outV); outV = vbslq_f32(idxV, srcV, outV ); countV = vbslq_u32(idxV, index,countV ); index = vaddq_u32(index,delta); /* Decrement the loop counter */ blkCnt--; } outV2 = vpmin_f32(vget_low_f32(outV),vget_high_f32(outV)); outV2 = vpmin_f32(outV2,outV2); out = outV2[0]; idxV = vceqq_f32(outV, vdupq_n_f32(out)); countV = vbslq_u32(idxV, countV,maxIdx); countV2 = vpmin_u32(vget_low_u32(countV),vget_high_u32(countV)); countV2 = vpmin_u32(countV2,countV2); outIndex = countV2[0]; /* if (blockSize - 1U) is not multiple of 4 */ blkCnt = (blockSize - 4 ) % 4U; while (blkCnt > 0U) { /* Initialize maxVal to the next consecutive values one by one */ maxVal1 = *pSrc++; /* compare for the maximum value */ if (out > maxVal1) { /* Update the maximum value and it's index */ out = maxVal1; outIndex = blockSize - blkCnt ; } /* Decrement the loop counter */ blkCnt--; } } /* Store the maximum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } #else void arm_min_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult, uint32_t * pIndex) { float32_t minVal, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex; /* Loop counter */ #if defined (ARM_MATH_LOOPUNROLL) uint32_t index; /* index of maximum value */ #endif /* Initialise index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparision */ out = *pSrc++; #if defined (ARM_MATH_LOOPUNROLL) /* Initialise index of maximum value. */ index = 0U; /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = (blockSize - 1U) >> 2U; while (blkCnt > 0U) { /* Initialize minVal to next consecutive values one by one */ minVal = *pSrc++; /* compare for the minimum value */ if (out > minVal) { /* Update the minimum value and it's index */ out = minVal; outIndex = index + 1U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 2U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 3U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 4U; } index += 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = (blockSize - 1U) % 4U; #else /* Initialize blkCnt with number of samples */ blkCnt = (blockSize - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Initialize minVal to the next consecutive values one by one */ minVal = *pSrc++; /* compare for the minimum value */ if (out > minVal) { /* Update the minimum value and it's index */ out = minVal; outIndex = blockSize - blkCnt; } /* Decrement loop counter */ blkCnt--; } /* Store the minimum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of Min group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_min_f32.c
C
apache-2.0
6,699
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_min_q15.c * Description: Minimum value of a Q15 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup Min @{ */ /** @brief Minimum value of a Q15 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult minimum value returned here @param[out] pIndex index of minimum value returned here @return none */ void arm_min_q15( const q15_t * pSrc, uint32_t blockSize, q15_t * pResult, uint32_t * pIndex) { q15_t minVal, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex; /* Loop counter */ #if defined (ARM_MATH_LOOPUNROLL) uint32_t index; /* index of maximum value */ #endif /* Initialise index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparision */ out = *pSrc++; #if defined (ARM_MATH_LOOPUNROLL) /* Initialise index of maximum value. */ index = 0U; /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = (blockSize - 1U) >> 2U; while (blkCnt > 0U) { /* Initialize minVal to next consecutive values one by one */ minVal = *pSrc++; /* compare for the minimum value */ if (out > minVal) { /* Update the minimum value and it's index */ out = minVal; outIndex = index + 1U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 2U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 3U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 4U; } index += 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = (blockSize - 1U) % 4U; #else /* Initialize blkCnt with number of samples */ blkCnt = (blockSize - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Initialize minVal to the next consecutive values one by one */ minVal = *pSrc++; /* compare for the minimum value */ if (out > minVal) { /* Update the minimum value and it's index */ out = minVal; outIndex = blockSize - blkCnt; } /* Decrement loop counter */ blkCnt--; } /* Store the minimum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } /** @} end of Min group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_min_q15.c
C
apache-2.0
3,596
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_min_q31.c * Description: Minimum value of a Q31 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup Min @{ */ /** @brief Minimum value of a Q31 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult minimum value returned here @param[out] pIndex index of minimum value returned here @return none */ void arm_min_q31( const q31_t * pSrc, uint32_t blockSize, q31_t * pResult, uint32_t * pIndex) { q31_t minVal, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex; /* Loop counter */ #if defined (ARM_MATH_LOOPUNROLL) uint32_t index; /* index of maximum value */ #endif /* Initialise index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparision */ out = *pSrc++; #if defined (ARM_MATH_LOOPUNROLL) /* Initialise index of maximum value. */ index = 0U; /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = (blockSize - 1U) >> 2U; while (blkCnt > 0U) { /* Initialize minVal to next consecutive values one by one */ minVal = *pSrc++; /* compare for the minimum value */ if (out > minVal) { /* Update the minimum value and it's index */ out = minVal; outIndex = index + 1U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 2U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 3U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 4U; } index += 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = (blockSize - 1U) % 4U; #else /* Initialize blkCnt with number of samples */ blkCnt = (blockSize - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Initialize minVal to the next consecutive values one by one */ minVal = *pSrc++; /* compare for the minimum value */ if (out > minVal) { /* Update the minimum value and it's index */ out = minVal; outIndex = blockSize - blkCnt; } /* Decrement loop counter */ blkCnt--; } /* Store the minimum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } /** @} end of Min group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_min_q31.c
C
apache-2.0
3,596
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_min_q7.c * Description: Minimum value of a Q7 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup Min @{ */ /** @brief Minimum value of a Q7 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult minimum value returned here @param[out] pIndex index of minimum value returned here @return none */ void arm_min_q7( const q7_t * pSrc, uint32_t blockSize, q7_t * pResult, uint32_t * pIndex) { q7_t minVal, out; /* Temporary variables to store the output value. */ uint32_t blkCnt, outIndex; /* Loop counter */ #if defined (ARM_MATH_LOOPUNROLL) uint32_t index; /* index of maximum value */ #endif /* Initialise index value to zero. */ outIndex = 0U; /* Load first input value that act as reference value for comparision */ out = *pSrc++; #if defined (ARM_MATH_LOOPUNROLL) /* Initialise index of maximum value. */ index = 0U; /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = (blockSize - 1U) >> 2U; while (blkCnt > 0U) { /* Initialize minVal to next consecutive values one by one */ minVal = *pSrc++; /* compare for the minimum value */ if (out > minVal) { /* Update the minimum value and it's index */ out = minVal; outIndex = index + 1U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 2U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 3U; } minVal = *pSrc++; if (out > minVal) { out = minVal; outIndex = index + 4U; } index += 4U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = (blockSize - 1U) % 4U; #else /* Initialize blkCnt with number of samples */ blkCnt = (blockSize - 1U); #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* Initialize minVal to the next consecutive values one by one */ minVal = *pSrc++; /* compare for the minimum value */ if (out > minVal) { /* Update the minimum value and it's index */ out = minVal; outIndex = blockSize - blkCnt; } /* Decrement loop counter */ blkCnt--; } /* Store the minimum value and it's index into destination pointers */ *pResult = out; *pIndex = outIndex; } /** @} end of Min group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_min_q7.c
C
apache-2.0
3,590
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_power_f32.c * Description: Sum of the squares of the elements of a floating-point vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @defgroup power Power Calculates the sum of the squares of the elements in the input vector. The underlying algorithm is used: <pre> Result = pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + pSrc[2] * pSrc[2] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]; </pre> There are separate functions for floating point, Q31, Q15, and Q7 data types. */ /** @addtogroup power @{ */ /** @brief Sum of the squares of the elements of a floating-point vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult sum of the squares value returned here @return none */ #if defined(ARM_MATH_NEON) void arm_power_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult) { float32_t sum = 0.0f; /* accumulator */ float32_t in; /* Temporary variable to store input value */ uint32_t blkCnt; /* loop counter */ float32x4_t sumV = vdupq_n_f32(0.0f); /* Temporary result storage */ float32x2_t sumV2; float32x4_t inV; blkCnt = blockSize >> 2U; /* Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power and then store the result in a temporary variable, sum. */ inV = vld1q_f32(pSrc); sumV = vmlaq_f32(sumV, inV, inV); pSrc += 4; /* Decrement the loop counter */ blkCnt--; } sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV)); sum = sumV2[0] + sumV2[1]; /* If the blockSize is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ blkCnt = blockSize % 0x4U; while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */ /* compute power and then store the result in a temporary variable, sum. */ in = *pSrc++; sum += in * in; /* Decrement the loop counter */ blkCnt--; } /* Store the result to the destination */ *pResult = sum; } #else void arm_power_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult) { uint32_t blkCnt; /* Loop counter */ float32_t sum = 0.0f; /* Temporary result storage */ float32_t in; /* Temporary variable to store input value */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power and store result in a temporary variable, sum. */ in = *pSrc++; sum += in * in; in = *pSrc++; sum += in * in; in = *pSrc++; sum += in * in; in = *pSrc++; sum += in * in; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power and store result in a temporary variable, sum. */ in = *pSrc++; sum += in * in; /* Decrement loop counter */ blkCnt--; } /* Store result to destination */ *pResult = sum; } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of power group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_power_f32.c
C
apache-2.0
4,850
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_power_q15.c * Description: Sum of the squares of the elements of a Q15 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup power @{ */ /** @brief Sum of the squares of the elements of a Q15 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult sum of the squares value returned here @return none @par Scaling and Overflow Behavior The function is implemented using a 64-bit internal accumulator. The input is represented in 1.15 format. Intermediate multiplication yields a 2.30 format, and this result is added without saturation to a 64-bit accumulator in 34.30 format. With 33 guard bits in the accumulator, there is no risk of overflow, and the full precision of the intermediate multiplication is preserved. Finally, the return result is in 34.30 format. */ void arm_power_q15( const q15_t * pSrc, uint32_t blockSize, q63_t * pResult) { uint32_t blkCnt; /* Loop counter */ q63_t sum = 0; /* Temporary result storage */ q15_t in; /* Temporary variable to store input value */ #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP) q31_t in32; /* Temporary variable to store packed input value */ #endif #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power and store result in a temporary variable, sum. */ #if defined (ARM_MATH_DSP) in32 = read_q15x2_ia ((q15_t **) &pSrc); sum = __SMLALD(in32, in32, sum); in32 = read_q15x2_ia ((q15_t **) &pSrc); sum = __SMLALD(in32, in32, sum); #else in = *pSrc++; sum += ((q31_t) in * in); in = *pSrc++; sum += ((q31_t) in * in); in = *pSrc++; sum += ((q31_t) in * in); in = *pSrc++; sum += ((q31_t) in * in); #endif /* #if defined (ARM_MATH_DSP) */ /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power and store result in a temporary variable, sum. */ in = *pSrc++; sum += ((q31_t) in * in); /* Decrement loop counter */ blkCnt--; } /* Store result in 34.30 format */ *pResult = sum; } /** @} end of power group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_power_q15.c
C
apache-2.0
3,928
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_power_q31.c * Description: Sum of the squares of the elements of a Q31 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup power @{ */ /** @brief Sum of the squares of the elements of a Q31 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult sum of the squares value returned here @return none @par Scaling and Overflow Behavior The function is implemented using a 64-bit internal accumulator. The input is represented in 1.31 format. Intermediate multiplication yields a 2.62 format, and this result is truncated to 2.48 format by discarding the lower 14 bits. The 2.48 result is then added without saturation to a 64-bit accumulator in 16.48 format. With 15 guard bits in the accumulator, there is no risk of overflow, and the full precision of the intermediate multiplication is preserved. Finally, the return result is in 16.48 format. */ void arm_power_q31( const q31_t * pSrc, uint32_t blockSize, q63_t * pResult) { uint32_t blkCnt; /* Loop counter */ q63_t sum = 0; /* Temporary result storage */ q31_t in; /* Temporary variable to store input value */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power then shift intermediate results by 14 bits to maintain 16.48 format and store result in a temporary variable sum, providing 15 guard bits. */ in = *pSrc++; sum += ((q63_t) in * in) >> 14U; in = *pSrc++; sum += ((q63_t) in * in) >> 14U; in = *pSrc++; sum += ((q63_t) in * in) >> 14U; in = *pSrc++; sum += ((q63_t) in * in) >> 14U; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power and store result in a temporary variable, sum. */ in = *pSrc++; sum += ((q63_t) in * in) >> 14U; /* Decrement loop counter */ blkCnt--; } /* Store results in 16.48 format */ *pResult = sum; } /** @} end of power group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_power_q31.c
C
apache-2.0
3,743
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_power_q7.c * Description: Sum of the squares of the elements of a Q7 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup power @{ */ /** @brief Sum of the squares of the elements of a Q7 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult sum of the squares value returned here @return none @par Scaling and Overflow Behavior The function is implemented using a 32-bit internal accumulator. The input is represented in 1.7 format. Intermediate multiplication yields a 2.14 format, and this result is added without saturation to an accumulator in 18.14 format. With 17 guard bits in the accumulator, there is no risk of overflow, and the full precision of the intermediate multiplication is preserved. Finally, the return result is in 18.14 format. */ void arm_power_q7( const q7_t * pSrc, uint32_t blockSize, q31_t * pResult) { uint32_t blkCnt; /* Loop counter */ q31_t sum = 0; /* Temporary result storage */ q7_t in; /* Temporary variable to store input value */ #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP) q31_t in32; /* Temporary variable to store packed input value */ q31_t in1, in2; /* Temporary variables to store input value */ #endif #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power and store result in a temporary variable, sum. */ #if defined (ARM_MATH_DSP) in32 = read_q7x4_ia ((q7_t **) &pSrc); in1 = __SXTB16(__ROR(in32, 8)); in2 = __SXTB16(in32); /* calculate power and accumulate to accumulator */ sum = __SMLAD(in1, in1, sum); sum = __SMLAD(in2, in2, sum); #else in = *pSrc++; sum += ((q15_t) in * in); in = *pSrc++; sum += ((q15_t) in * in); in = *pSrc++; sum += ((q15_t) in * in); in = *pSrc++; sum += ((q15_t) in * in); #endif /* #if defined (ARM_MATH_DSP) */ /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power and store result in a temporary variable, sum. */ in = *pSrc++; sum += ((q15_t) in * in); /* Decrement loop counter */ blkCnt--; } /* Store result in 18.14 format */ *pResult = sum; } /** @} end of power group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_power_q7.c
C
apache-2.0
4,084
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_rms_f32.c * Description: Root mean square value of the elements of a floating-point vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @defgroup RMS Root mean square (RMS) Calculates the Root Mean Square of the elements in the input vector. The underlying algorithm is used: <pre> Result = sqrt(((pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]) / blockSize)); </pre> There are separate functions for floating point, Q31, and Q15 data types. */ /** @addtogroup RMS @{ */ /** @brief Root Mean Square of the elements of a floating-point vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult root mean square value returned here @return none */ #if defined(ARM_MATH_NEON) void arm_rms_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult) { float32_t sum = 0.0f; /* accumulator */ float32_t in; /* Temporary variable to store input value */ uint32_t blkCnt; /* loop counter */ float32x4_t sumV = vdupq_n_f32(0.0f); /* Temporary result storage */ float32x2_t sumV2; float32x4_t inV; blkCnt = blockSize >> 2U; /* Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute Power and then store the result in a temporary variable, sum. */ inV = vld1q_f32(pSrc); sumV = vmlaq_f32(sumV, inV, inV); pSrc += 4; /* Decrement the loop counter */ blkCnt--; } sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV)); sum = sumV2[0] + sumV2[1]; /* If the blockSize is not a multiple of 4, compute any remaining output samples here. ** No loop unrolling is used. */ blkCnt = blockSize % 0x4U; while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */ /* compute power and then store the result in a temporary variable, sum. */ in = *pSrc++; sum += in * in; /* Decrement the loop counter */ blkCnt--; } /* Compute Rms and store the result in the destination */ arm_sqrt_f32(sum / (float32_t) blockSize, pResult); } #else void arm_rms_f32( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult) { uint32_t blkCnt; /* Loop counter */ float32_t sum = 0.0f; /* Temporary result storage */ float32_t in; /* Temporary variable to store input value */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ in = *pSrc++; /* Compute sum of squares and store result in a temporary variable, sum. */ sum += in * in; in = *pSrc++; sum += in * in; in = *pSrc++; sum += in * in; in = *pSrc++; sum += in * in; /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ in = *pSrc++; /* Compute sum of squares and store result in a temporary variable. */ sum += ( in * in); /* Decrement loop counter */ blkCnt--; } /* Compute Rms and store result in destination */ arm_sqrt_f32(sum / (float32_t) blockSize, pResult); } #endif /* #if defined(ARM_MATH_NEON) */ /** @} end of RMS group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_rms_f32.c
C
apache-2.0
4,972
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_rms_q15.c * Description: Root Mean Square of the elements of a Q15 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup RMS @{ */ /** @brief Root Mean Square of the elements of a Q15 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult root mean square value returned here @return none @par Scaling and Overflow Behavior The function is implemented using a 64-bit internal accumulator. The input is represented in 1.15 format. Intermediate multiplication yields a 2.30 format, and this result is added without saturation to a 64-bit accumulator in 34.30 format. With 33 guard bits in the accumulator, there is no risk of overflow, and the full precision of the intermediate multiplication is preserved. Finally, the 34.30 result is truncated to 34.15 format by discarding the lower 15 bits, and then saturated to yield a result in 1.15 format. */ void arm_rms_q15( const q15_t * pSrc, uint32_t blockSize, q15_t * pResult) { uint32_t blkCnt; /* Loop counter */ q63_t sum = 0; /* Temporary result storage */ q15_t in; /* Temporary variable to store input value */ #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP) q31_t in32; /* Temporary variable to store input value */ #endif #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ /* Compute sum of squares and store result in a temporary variable. */ #if defined (ARM_MATH_DSP) in32 = read_q15x2_ia ((q15_t **) &pSrc); sum = __SMLALD(in32, in32, sum); in32 = read_q15x2_ia ((q15_t **) &pSrc); sum = __SMLALD(in32, in32, sum); #else in = *pSrc++; sum += ((q31_t) in * in); in = *pSrc++; sum += ((q31_t) in * in); in = *pSrc++; sum += ((q31_t) in * in); in = *pSrc++; sum += ((q31_t) in * in); #endif /* #if defined (ARM_MATH_DSP) */ /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ in = *pSrc++; /* Compute sum of squares and store result in a temporary variable. */ sum += ((q31_t) in * in); /* Decrement loop counter */ blkCnt--; } /* Truncating and saturating the accumulator to 1.15 format */ /* Store result in destination */ arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult); } /** @} end of RMS group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_rms_q15.c
C
apache-2.0
4,143
/* ---------------------------------------------------------------------- * Project: CMSIS DSP Library * Title: arm_rms_q31.c * Description: Root Mean Square of the elements of a Q31 vector * * $Date: 18. March 2019 * $Revision: V1.6.0 * * Target Processor: Cortex-M cores * -------------------------------------------------------------------- */ /* * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "arm_math.h" /** @ingroup groupStats */ /** @addtogroup RMS @{ */ /** @brief Root Mean Square of the elements of a Q31 vector. @param[in] pSrc points to the input vector @param[in] blockSize number of samples in input vector @param[out] pResult root mean square value returned here @return none @par Scaling and Overflow Behavior The function is implemented using an internal 64-bit accumulator. The input is represented in 1.31 format, and intermediate multiplication yields a 2.62 format. The accumulator maintains full precision of the intermediate multiplication results, but provides only a single guard bit. There is no saturation on intermediate additions. If the accumulator overflows, it wraps around and distorts the result. In order to avoid overflows completely, the input signal must be scaled down by log2(blockSize) bits, as a total of blockSize additions are performed internally. Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value. */ void arm_rms_q31( const q31_t * pSrc, uint32_t blockSize, q31_t * pResult) { uint32_t blkCnt; /* Loop counter */ uint64_t sum = 0; /* Temporary result storage (can get never negative. changed type from q63 to uint64 */ q31_t in; /* Temporary variable to store input value */ #if defined (ARM_MATH_LOOPUNROLL) /* Loop unrolling: Compute 4 outputs at a time */ blkCnt = blockSize >> 2U; while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ in = *pSrc++; /* Compute sum of squares and store result in a temporary variable, sum. */ sum += ((q63_t) in * in); in = *pSrc++; sum += ((q63_t) in * in); in = *pSrc++; sum += ((q63_t) in * in); in = *pSrc++; sum += ((q63_t) in * in); /* Decrement loop counter */ blkCnt--; } /* Loop unrolling: Compute remaining outputs */ blkCnt = blockSize % 0x4U; #else /* Initialize blkCnt with number of samples */ blkCnt = blockSize; #endif /* #if defined (ARM_MATH_LOOPUNROLL) */ while (blkCnt > 0U) { /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */ in = *pSrc++; /* Compute sum of squares and store result in a temporary variable. */ sum += ((q63_t) in * in); /* Decrement loop counter */ blkCnt--; } /* Convert data in 2.62 to 1.31 by 31 right shifts and saturate */ /* Compute Rms and store result in destination vector */ arm_sqrt_q31(clip_q63_to_q31((sum / (q63_t) blockSize) >> 31), pResult); } /** @} end of RMS group */
YifuLiu/AliOS-Things
components/cmsis/DSP/Source/StatisticsFunctions/arm_rms_q31.c
C
apache-2.0
4,001