1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
4 * $Date: 17. January 2013
7 * Project: CMSIS DSP Library
8 * Title: arm_rfft_q31.c
10 * Description: RFFT & RIFFT Q31 process function
13 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * - Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * - Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
24 * - Neither the name of ARM LIMITED nor the names of its contributors
25 * may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 * -------------------------------------------------------------------- */
44 void arm_radix4_butterfly_inverse_q31(
48 uint32_t twidCoefModifier);
50 void arm_radix4_butterfly_q31(
54 uint32_t twidCoefModifier);
56 void arm_bitreversal_q31(
59 uint16_t bitRevFactor,
60 uint16_t * pBitRevTab);
62 /*--------------------------------------------------------------------
63 * Internal functions prototypes
64 --------------------------------------------------------------------*/
66 void arm_split_rfft_q31(
74 void arm_split_rifft_q31(
88 * @brief Processing function for the Q31 RFFT/RIFFT.
89 * @param[in] *S points to an instance of the Q31 RFFT/RIFFT structure.
90 * @param[in] *pSrc points to the input buffer.
91 * @param[out] *pDst points to the output buffer.
94 * \par Input an output formats:
96 * Internally input is downscaled by 2 for every stage to avoid saturations inside CFFT/CIFFT process.
97 * Hence the output format is different for different RFFT sizes.
98 * The input and output formats for different RFFT sizes and number of bits to upscale are mentioned in the tables below for RFFT and RIFFT:
100 * \image html RFFTQ31.gif "Input and Output Formats for Q31 RFFT"
103 * \image html RIFFTQ31.gif "Input and Output Formats for Q31 RIFFT"
107 const arm_rfft_instance_q31 * S,
111 const arm_cfft_radix4_instance_q31 *S_CFFT = S->pCfft;
113 /* Calculation of RIFFT of input */
114 if(S->ifftFlagR == 1u)
116 /* Real IFFT core process */
117 arm_split_rifft_q31(pSrc, S->fftLenBy2, S->pTwiddleAReal,
118 S->pTwiddleBReal, pDst, S->twidCoefRModifier);
120 /* Complex readix-4 IFFT process */
121 arm_radix4_butterfly_inverse_q31(pDst, S_CFFT->fftLen,
123 S_CFFT->twidCoefModifier);
124 /* Bit reversal process */
125 if(S->bitReverseFlagR == 1u)
127 arm_bitreversal_q31(pDst, S_CFFT->fftLen,
128 S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
133 /* Calculation of RFFT of input */
135 /* Complex readix-4 FFT process */
136 arm_radix4_butterfly_q31(pSrc, S_CFFT->fftLen,
137 S_CFFT->pTwiddle, S_CFFT->twidCoefModifier);
139 /* Bit reversal process */
140 if(S->bitReverseFlagR == 1u)
142 arm_bitreversal_q31(pSrc, S_CFFT->fftLen,
143 S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
146 /* Real FFT core process */
147 arm_split_rfft_q31(pSrc, S->fftLenBy2, S->pTwiddleAReal,
148 S->pTwiddleBReal, pDst, S->twidCoefRModifier);
155 * @} end of RealFFT group
159 * @brief Core Real FFT process
160 * @param[in] *pSrc points to the input buffer.
161 * @param[in] fftLen length of FFT.
162 * @param[in] *pATable points to the twiddle Coef A buffer.
163 * @param[in] *pBTable points to the twiddle Coef B buffer.
164 * @param[out] *pDst points to the output buffer.
165 * @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
169 void arm_split_rfft_q31(
177 uint32_t i; /* Loop Counter */
178 q31_t outR, outI; /* Temporary variables for output */
179 q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
180 q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
181 q31_t *pOut1 = &pDst[2], *pOut2 = &pDst[(4u * fftLen) - 1u];
182 q31_t *pIn1 = &pSrc[2], *pIn2 = &pSrc[(2u * fftLen) - 1u];
184 /* Init coefficient pointers */
185 pCoefA = &pATable[modifier * 2u];
186 pCoefB = &pBTable[modifier * 2u];
193 outR = (pSrc[2 * i] * pATable[2 * i] - pSrc[2 * i + 1] * pATable[2 * i + 1]
194 + pSrc[2 * n - 2 * i] * pBTable[2 * i] +
195 pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
198 /* outI = (pIn[2 * i + 1] * pATable[2 * i] + pIn[2 * i] * pATable[2 * i + 1] +
199 pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
200 pIn[2 * n - 2 * i + 1] * pBTable[2 * i]); */
205 /* outR = (pSrc[2 * i] * pATable[2 * i] */
206 outR = ((int32_t) (((q63_t) * pIn1 * CoefA1) >> 32));
208 /* outI = pIn[2 * i] * pATable[2 * i + 1] */
209 outI = ((int32_t) (((q63_t) * pIn1++ * CoefA2) >> 32));
211 /* - pSrc[2 * i + 1] * pATable[2 * i + 1] */
213 (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn1 * (-CoefA2))) >> 32);
215 /* (pIn[2 * i + 1] * pATable[2 * i] */
217 (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn1++ * (CoefA1))) >> 32);
219 /* pSrc[2 * n - 2 * i] * pBTable[2 * i] */
221 (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn2 * (-CoefA2))) >> 32);
224 /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
226 (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn2-- * (-CoefB1))) >> 32);
228 /* pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
230 (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn2 * (CoefB1))) >> 32);
232 /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
234 (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn2-- * (-CoefA2))) >> 32);
237 *pOut1++ = (outR << 1u);
238 *pOut1++ = (outI << 1u);
240 /* write complex conjugate output */
241 *pOut2-- = -(outI << 1u);
242 *pOut2-- = (outR << 1u);
244 /* update coefficient pointer */
245 pCoefB = pCoefB + (modifier * 2u);
246 pCoefA = pCoefA + ((modifier * 2u) - 1u);
252 pDst[2u * fftLen] = pSrc[0] - pSrc[1];
253 pDst[(2u * fftLen) + 1u] = 0;
255 pDst[0] = pSrc[0] + pSrc[1];
262 * @brief Core Real IFFT process
263 * @param[in] *pSrc points to the input buffer.
264 * @param[in] fftLen length of FFT.
265 * @param[in] *pATable points to the twiddle Coef A buffer.
266 * @param[in] *pBTable points to the twiddle Coef B buffer.
267 * @param[out] *pDst points to the output buffer.
268 * @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
272 void arm_split_rifft_q31(
280 q31_t outR, outI; /* Temporary variables for output */
281 q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
282 q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
283 q31_t *pIn1 = &pSrc[0], *pIn2 = &pSrc[(2u * fftLen) + 1u];
285 pCoefA = &pATable[0];
286 pCoefB = &pBTable[0];
291 outR = (pIn[2 * i] * pATable[2 * i] + pIn[2 * i + 1] * pATable[2 * i + 1] +
292 pIn[2 * n - 2 * i] * pBTable[2 * i] -
293 pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
295 outI = (pIn[2 * i + 1] * pATable[2 * i] - pIn[2 * i] * pATable[2 * i + 1] -
296 pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
297 pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
303 /* outR = (pIn[2 * i] * pATable[2 * i] */
304 outR = ((int32_t) (((q63_t) * pIn1 * CoefA1) >> 32));
306 /* - pIn[2 * i] * pATable[2 * i + 1] */
307 outI = -((int32_t) (((q63_t) * pIn1++ * CoefA2) >> 32));
309 /* pIn[2 * i + 1] * pATable[2 * i + 1] */
311 (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn1 * (CoefA2))) >> 32);
313 /* pIn[2 * i + 1] * pATable[2 * i] */
315 (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn1++ * (CoefA1))) >> 32);
317 /* pIn[2 * n - 2 * i] * pBTable[2 * i] */
319 (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn2 * (CoefA2))) >> 32);
323 /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
325 (q31_t) ((((q63_t) outI << 32) - ((q63_t) * pIn2-- * (CoefB1))) >> 32);
327 /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
329 (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn2 * (CoefB1))) >> 32);
331 /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
333 (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn2-- * (CoefA2))) >> 32);
336 *pDst++ = (outR << 1u);
337 *pDst++ = (outI << 1u);
339 /* update coefficient pointer */
340 pCoefB = pCoefB + (modifier * 2u);
341 pCoefA = pCoefA + ((modifier * 2u) - 1u);
343 /* Decrement loop count */