1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_rfft_f32.c
10 * Description: RFFT & RIFFT Floating point process function
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
29 * Version 0.0.7 2010/06/10
30 * Misra-C changes done
31 * -------------------------------------------------------------------- */
36 * @ingroup groupTransforms
40 * @defgroup RFFT_RIFFT Real FFT Functions
43 * Complex FFT/IFFT typically assumes complex input and output. However many applications use real valued data in time domain.
44 * Real FFT/IFFT efficiently process real valued sequences with the advantage of requirement of low memory and with less complexity.
47 * This set of functions implements Real Fast Fourier Transforms(RFFT) and Real Inverse Fast Fourier Transform(RIFFT)
48 * for Q15, Q31, and floating-point data types.
53 * <b>Real Fast Fourier Transform:</b>
55 * Real FFT of N-point is calculated using CFFT of N/2-point and Split RFFT process as shown below figure.
57 * \image html RFFT.gif "Real Fast Fourier Transform"
59 * The RFFT functions operate on blocks of input and output data and each call to the function processes
60 * <code>fftLenR</code> samples through the transform. <code>pSrc</code> points to input array containing <code>fftLenR</code> values.
61 * <code>pDst</code> points to output array containing <code>2*fftLenR</code> values. \n
62 * Input for real FFT is in the order of
63 * <pre>{real[0], real[1], real[2], real[3], ..}</pre>
64 * Output for real FFT is complex and are in the order of
65 * <pre>{real(0), imag(0), real(1), imag(1), ...}</pre>
67 * <b>Real Inverse Fast Fourier Transform:</b>
69 * Real IFFT of N-point is calculated using Split RIFFT process and CFFT of N/2-point as shown below figure.
71 * \image html RIFFT.gif "Real Inverse Fast Fourier Transform"
73 * The RIFFT functions operate on blocks of input and output data and each call to the function processes
74 * <code>2*fftLenR</code> samples through the transform. <code>pSrc</code> points to input array containing <code>2*fftLenR</code> values.
75 * <code>pDst</code> points to output array containing <code>fftLenR</code> values. \n
76 * Input for real IFFT is complex and are in the order of
77 * <pre>{real(0), imag(0), real(1), imag(1), ...}</pre>
78 * Output for real IFFT is real and in the order of
79 * <pre>{real[0], real[1], real[2], real[3], ..}</pre>
81 * \par Lengths supported by the transform:
83 * Real FFT/IFFT supports the lengths [128, 512, 2048], as it internally uses CFFT/CIFFT.
85 * \par Instance Structure
86 * A separate instance structure must be defined for each Instance but the twiddle factors can be reused.
87 * There are separate instance structure declarations for each of the 3 supported data types.
89 * \par Initialization Functions
90 * There is also an associated initialization function for each data type.
91 * The initialization function performs the following operations:
92 * - Sets the values of the internal structure fields.
93 * - Initializes twiddle factor tables.
94 * - Initializes CFFT data structure fields.
96 * Use of the initialization function is optional.
97 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
98 * To place an instance structure into a const data section, the instance structure must be manually initialized.
99 * Manually initialize the instance structure as follows:
101 *arm_rfft_instance_f32 S = {fftLenReal, fftLenBy2, ifftFlagR, bitReverseFlagR, twidCoefRModifier, pTwiddleAReal, pTwiddleBReal, pCfft};
102 *arm_rfft_instance_q31 S = {fftLenReal, fftLenBy2, ifftFlagR, bitReverseFlagR, twidCoefRModifier, pTwiddleAReal, pTwiddleBReal, pCfft};
103 *arm_rfft_instance_q15 S = {fftLenReal, fftLenBy2, ifftFlagR, bitReverseFlagR, twidCoefRModifier, pTwiddleAReal, pTwiddleBReal, pCfft};
105 * where <code>fftLenReal</code> length of RFFT/RIFFT; <code>fftLenBy2</code> length of CFFT/CIFFT.
106 * <code>ifftFlagR</code> Flag for selection of RFFT or RIFFT(Set ifftFlagR to calculate RIFFT otherwise calculates RFFT);
107 * <code>bitReverseFlagR</code> Flag for selection of output order(Set bitReverseFlagR to output in normal order otherwise output in bit reversed order);
108 * <code>twidCoefRModifier</code> modifier for twiddle factor table which supports 128, 512, 2048 RFFT lengths with same table;
109 * <code>pTwiddleAReal</code>points to A array of twiddle coefficients; <code>pTwiddleBReal</code>points to B array of twiddle coefficients;
110 * <code>pCfft</code> points to the CFFT Instance structure. The CFFT structure also needs to be initialized, refer to arm_cfft_radix4_f32() for details regarding
111 * static initialization of cfft structure.
113 * \par Fixed-Point Behavior
114 * Care must be taken when using the fixed-point versions of the RFFT/RIFFT function.
115 * Refer to the function specific documentation below for usage guidelines.
118 /*--------------------------------------------------------------------
119 * Internal functions prototypes
120 *--------------------------------------------------------------------*/
122 void arm_split_rfft_f32(
129 void arm_split_rifft_f32(
138 * @addtogroup RFFT_RIFFT
143 * @brief Processing function for the floating-point RFFT/RIFFT.
144 * @param[in] *S points to an instance of the floating-point RFFT/RIFFT structure.
145 * @param[in] *pSrc points to the input buffer.
146 * @param[out] *pDst points to the output buffer.
151 const arm_rfft_instance_f32 * S,
155 const arm_cfft_radix4_instance_f32 *S_CFFT = S->pCfft;
158 /* Calculation of Real IFFT of input */
159 if(S->ifftFlagR == 1u)
161 /* Real IFFT core process */
162 arm_split_rifft_f32(pSrc, S->fftLenBy2, S->pTwiddleAReal,
163 S->pTwiddleBReal, pDst, S->twidCoefRModifier);
166 /* Complex radix-4 IFFT process */
167 arm_radix4_butterfly_inverse_f32(pDst, S_CFFT->fftLen,
169 S_CFFT->twidCoefModifier,
170 S_CFFT->onebyfftLen);
172 /* Bit reversal process */
173 if(S->bitReverseFlagR == 1u)
175 arm_bitreversal_f32(pDst, S_CFFT->fftLen,
176 S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
182 /* Calculation of RFFT of input */
184 /* Complex radix-4 FFT process */
185 arm_radix4_butterfly_f32(pSrc, S_CFFT->fftLen,
186 S_CFFT->pTwiddle, S_CFFT->twidCoefModifier);
188 /* Bit reversal process */
189 if(S->bitReverseFlagR == 1u)
191 arm_bitreversal_f32(pSrc, S_CFFT->fftLen,
192 S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
196 /* Real FFT core process */
197 arm_split_rfft_f32(pSrc, S->fftLenBy2, S->pTwiddleAReal,
198 S->pTwiddleBReal, pDst, S->twidCoefRModifier);
204 * @} end of RFFT_RIFFT group
208 * @brief Core Real FFT process
209 * @param[in] *pSrc points to the input buffer.
210 * @param[in] fftLen length of FFT.
211 * @param[in] *pATable points to the twiddle Coef A buffer.
212 * @param[in] *pBTable points to the twiddle Coef B buffer.
213 * @param[out] *pDst points to the output buffer.
214 * @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
218 void arm_split_rfft_f32(
226 uint32_t i; /* Loop Counter */
227 float32_t outR, outI; /* Temporary variables for output */
228 float32_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
229 float32_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
230 float32_t *pDst1 = &pDst[2], *pDst2 = &pDst[(4u * fftLen) - 1u]; /* temp pointers for output buffer */
231 float32_t *pSrc1 = &pSrc[2], *pSrc2 = &pSrc[(2u * fftLen) - 1u]; /* temp pointers for input buffer */
234 pSrc[2u * fftLen] = pSrc[0];
235 pSrc[(2u * fftLen) + 1u] = pSrc[1];
237 /* Init coefficient pointers */
238 pCoefA = &pATable[modifier * 2u];
239 pCoefB = &pBTable[modifier * 2u];
246 outR = (pSrc[2 * i] * pATable[2 * i] - pSrc[2 * i + 1] * pATable[2 * i + 1]
247 + pSrc[2 * n - 2 * i] * pBTable[2 * i] +
248 pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
251 /* outI = (pIn[2 * i + 1] * pATable[2 * i] + pIn[2 * i] * pATable[2 * i + 1] +
252 pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
253 pIn[2 * n - 2 * i + 1] * pBTable[2 * i]); */
255 /* read pATable[2 * i] */
257 /* pATable[2 * i + 1] */
260 /* pSrc[2 * i] * pATable[2 * i] */
261 outR = *pSrc1 * CoefA1;
262 /* pSrc[2 * i] * CoefA2 */
263 outI = *pSrc1++ * CoefA2;
265 /* (pSrc[2 * i + 1] + pSrc[2 * fftLen - 2 * i + 1]) * CoefA2 */
266 outR -= (*pSrc1 + *pSrc2) * CoefA2;
267 /* pSrc[2 * i + 1] * CoefA1 */
268 outI += *pSrc1++ * CoefA1;
272 /* pSrc[2 * fftLen - 2 * i + 1] * CoefB1 */
273 outI -= *pSrc2-- * CoefB1;
274 /* pSrc[2 * fftLen - 2 * i] * CoefA2 */
275 outI -= *pSrc2 * CoefA2;
277 /* pSrc[2 * fftLen - 2 * i] * CoefB1 */
278 outR += *pSrc2-- * CoefB1;
284 /* write complex conjugate output */
288 /* update coefficient pointer */
289 pCoefB = pCoefB + (modifier * 2u);
290 pCoefA = pCoefA + ((modifier * 2u) - 1u);
296 pDst[2u * fftLen] = pSrc[0] - pSrc[1];
297 pDst[(2u * fftLen) + 1u] = 0.0f;
299 pDst[0] = pSrc[0] + pSrc[1];
306 * @brief Core Real IFFT process
307 * @param[in] *pSrc points to the input buffer.
308 * @param[in] fftLen length of FFT.
309 * @param[in] *pATable points to the twiddle Coef A buffer.
310 * @param[in] *pBTable points to the twiddle Coef B buffer.
311 * @param[out] *pDst points to the output buffer.
312 * @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
316 void arm_split_rifft_f32(
324 float32_t outR, outI; /* Temporary variables for output */
325 float32_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
326 float32_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
327 float32_t *pSrc1 = &pSrc[0], *pSrc2 = &pSrc[(2u * fftLen) + 1u];
329 pCoefA = &pATable[0];
330 pCoefB = &pBTable[0];
335 outR = (pIn[2 * i] * pATable[2 * i] + pIn[2 * i + 1] * pATable[2 * i + 1] +
336 pIn[2 * n - 2 * i] * pBTable[2 * i] -
337 pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
339 outI = (pIn[2 * i + 1] * pATable[2 * i] - pIn[2 * i] * pATable[2 * i + 1] -
340 pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
341 pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
348 /* outR = (pSrc[2 * i] * CoefA1 */
349 outR = *pSrc1 * CoefA1;
351 /* - pSrc[2 * i] * CoefA2 */
352 outI = -(*pSrc1++) * CoefA2;
354 /* (pSrc[2 * i + 1] + pSrc[2 * fftLen - 2 * i + 1]) * CoefA2 */
355 outR += (*pSrc1 + *pSrc2) * CoefA2;
357 /* pSrc[2 * i + 1] * CoefA1 */
358 outI += (*pSrc1++) * CoefA1;
362 /* - pSrc[2 * fftLen - 2 * i + 1] * CoefB1 */
363 outI -= *pSrc2-- * CoefB1;
365 /* pSrc[2 * fftLen - 2 * i] * CoefB1 */
366 outR += *pSrc2 * CoefB1;
368 /* pSrc[2 * fftLen - 2 * i] * CoefA2 */
369 outI += *pSrc2-- * CoefA2;
375 /* update coefficient pointer */
376 pCoefB = pCoefB + (modifier * 2u);
377 pCoefA = pCoefA + ((modifier * 2u) - 1u);
379 /* Decrement loop count */