1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_rfft_f32.c
4 * Description: RFFT & RIFFT Floating point process function
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
20 * www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
32 arm_rfft_fast_instance_f32
* S
,
33 float32_t
* p
, float32_t
* pOut
)
35 uint32_t k
; /* Loop Counter */
36 float32_t twR
, twI
; /* RFFT Twiddle coefficients */
37 float32_t
* pCoeff
= S
->pTwiddleRFFT
; /* Points to RFFT Twiddle factors */
38 float32_t
*pA
= p
; /* increasing pointer */
39 float32_t
*pB
= p
; /* decreasing pointer */
40 float32_t xAR
, xAI
, xBR
, xBI
; /* temporary variables */
41 float32_t t1a
, t1b
; /* temporary variables */
42 float32_t p0
, p1
, p2
, p3
; /* temporary variables */
45 k
= (S
->Sint
).fftLen
- 1;
47 /* Pack first and last sample of the frequency domain together */
57 // U1 = XA(1) + XB(1); % It is real
60 // U2 = XB(1) - XA(1); % It is imaginary
63 // real(tw * (xB - xA)) = twR * (xBR - xAR) - twI * (xBI - xAI);
64 // imag(tw * (xB - xA)) = twI * (xBR - xAR) + twR * (xBI - xAI);
65 *pOut
++ = 0.5f
* ( t1a
+ t1b
);
66 *pOut
++ = 0.5f
* ( t1a
- t1b
);
68 // XA(1) = 1/2*( U1 - imag(U2) + i*( U1 +imag(U2) ));
75 function X = my_split_rfft(X, ifftFlag)
76 % X is a series of real numbers
78 XC = X(1:2:end) +i*X(2:2:end);
80 XB = conj(XA([1 end:-1:2]));
81 TW = i*exp(-2*pi*i*[0:L/2-1]/L).';
83 XA(l) = 1/2 * (XA(l) + XB(l) + TW(l) * (XB(l) - XA(l)));
85 XA(1) = 1/2* (XA(1) + XB(1) + TW(1) * (XB(1) - XA(1))) + i*( 1/2*( XA(1) + XB(1) + i*( XA(1) - XB(1))));
100 // real(tw * (xB - xA)) = twR * (xBR - xAR) - twI * (xBI - xAI);
101 // imag(tw * (xB - xA)) = twI * (xBR - xAR) + twR * (xBI - xAI);
107 *pOut
++ = 0.5f
* (xAR
+ xBR
+ p0
+ p3
); //xAR
108 *pOut
++ = 0.5f
* (xAI
- xBI
+ p1
- p2
); //xAI
116 /* Prepares data for inverse cfft */
118 arm_rfft_fast_instance_f32
* S
,
119 float32_t
* p
, float32_t
* pOut
)
121 uint32_t k
; /* Loop Counter */
122 float32_t twR
, twI
; /* RFFT Twiddle coefficients */
123 float32_t
*pCoeff
= S
->pTwiddleRFFT
; /* Points to RFFT Twiddle factors */
124 float32_t
*pA
= p
; /* increasing pointer */
125 float32_t
*pB
= p
; /* decreasing pointer */
126 float32_t xAR
, xAI
, xBR
, xBI
; /* temporary variables */
127 float32_t t1a
, t1b
, r
, s
, t
, u
; /* temporary variables */
129 k
= (S
->Sint
).fftLen
- 1;
136 *pOut
++ = 0.5f
* ( xAR
+ xAI
);
137 *pOut
++ = 0.5f
* ( xAR
- xAI
);
144 /* G is half of the frequency complex spectrum */
146 // Xk(k) = 1/2 * (G(k) + conj(G(N-k+2)) + Tw(k)*( G(k) - conj(G(N-k+2))));
163 // real(tw * (xA - xB)) = twR * (xAR - xBR) - twI * (xAI - xBI);
164 // imag(tw * (xA - xB)) = twI * (xAR - xBR) + twR * (xAI - xBI);
165 *pOut
++ = 0.5f
* (xAR
+ xBR
- r
- s
); //xAR
166 *pOut
++ = 0.5f
* (xAI
- xBI
+ t
- u
); //xAI
176 * @ingroup groupTransforms
180 * @defgroup RealFFT Real FFT Functions
183 * The CMSIS DSP library includes specialized algorithms for computing the
184 * FFT of real data sequences. The FFT is defined over complex data but
185 * in many applications the input is real. Real FFT algorithms take advantage
186 * of the symmetry properties of the FFT and have a speed advantage over complex
187 * algorithms of the same length.
189 * The Fast RFFT algorith relays on the mixed radix CFFT that save processor usage.
191 * The real length N forward FFT of a sequence is computed using the steps shown below.
193 * \image html RFFT.gif "Real Fast Fourier Transform"
195 * The real sequence is initially treated as if it were complex to perform a CFFT.
196 * Later, a processing stage reshapes the data to obtain half of the frequency spectrum
197 * in complex format. Except the first complex number that contains the two real numbers
198 * X[0] and X[N/2] all the data is complex. In other words, the first complex sample
199 * contains two real values packed.
201 * The input for the inverse RFFT should keep the same format as the output of the
202 * forward RFFT. A first processing stage pre-process the data to later perform an
205 * \image html RIFFT.gif "Real Inverse Fast Fourier Transform"
207 * The algorithms for floating-point, Q15, and Q31 data are slightly different
208 * and we describe each algorithm in turn.
209 * \par Floating-point
210 * The main functions are arm_rfft_fast_f32() and arm_rfft_fast_init_f32().
211 * The older functions arm_rfft_f32() and arm_rfft_init_f32() have been
212 * deprecated but are still documented.
214 * The FFT of a real N-point sequence has even symmetry in the frequency
215 * domain. The second half of the data equals the conjugate of the first
216 * half flipped in frequency. Looking at the data, we see that we can
217 * uniquely represent the FFT using only N/2 complex numbers. These are
218 * packed into the output array in alternating real and imaginary
221 * X = { real[0], imag[0], real[1], imag[1], real[2], imag[2] ...
222 * real[(N/2)-1], imag[(N/2)-1 }
224 * It happens that the first complex number (real[0], imag[0]) is actually
225 * all real. real[0] represents the DC offset, and imag[0] should be 0.
226 * (real[1], imag[1]) is the fundamental frequency, (real[2], imag[2]) is
227 * the first harmonic and so on.
229 * The real FFT functions pack the frequency domain data in this fashion.
230 * The forward transform outputs the data in this form and the inverse
231 * transform expects input data in this form. The function always performs
232 * the needed bitreversal so that the input and output data is always in
233 * normal order. The functions support lengths of [32, 64, 128, ..., 4096]
236 * The real algorithms are defined in a similar manner and utilize N/2 complex
237 * transforms behind the scenes.
239 * The complex transforms used internally include scaling to prevent fixed-point
240 * overflows. The overall scaling equals 1/(fftLen/2).
242 * A separate instance structure must be defined for each transform used but
243 * twiddle factor and bit reversal tables can be reused.
245 * There is also an associated initialization function for each data type.
246 * The initialization function performs the following operations:
247 * - Sets the values of the internal structure fields.
248 * - Initializes twiddle factor table and bit reversal table pointers.
249 * - Initializes the internal complex FFT data structure.
251 * Use of the initialization function is optional.
252 * However, if the initialization function is used, then the instance structure
253 * cannot be placed into a const data section. To place an instance structure
254 * into a const data section, the instance structure should be manually
255 * initialized as follows:
257 *arm_rfft_instance_q31 S = {fftLenReal, fftLenBy2, ifftFlagR, bitReverseFlagR, twidCoefRModifier, pTwiddleAReal, pTwiddleBReal, pCfft};
258 *arm_rfft_instance_q15 S = {fftLenReal, fftLenBy2, ifftFlagR, bitReverseFlagR, twidCoefRModifier, pTwiddleAReal, pTwiddleBReal, pCfft};
260 * where <code>fftLenReal</code> is the length of the real transform;
261 * <code>fftLenBy2</code> length of the internal complex transform.
262 * <code>ifftFlagR</code> Selects forward (=0) or inverse (=1) transform.
263 * <code>bitReverseFlagR</code> Selects bit reversed output (=0) or normal order
265 * <code>twidCoefRModifier</code> stride modifier for the twiddle factor table.
266 * The value is based on the FFT length;
267 * <code>pTwiddleAReal</code>points to the A array of twiddle coefficients;
268 * <code>pTwiddleBReal</code>points to the B array of twiddle coefficients;
269 * <code>pCfft</code> points to the CFFT Instance structure. The CFFT structure
270 * must also be initialized. Refer to arm_cfft_radix4_f32() for details regarding
271 * static initialization of the complex FFT instance structure.
275 * @addtogroup RealFFT
280 * @brief Processing function for the floating-point real FFT.
281 * @param[in] *S points to an arm_rfft_fast_instance_f32 structure.
282 * @param[in] *p points to the input buffer.
283 * @param[in] *pOut points to the output buffer.
284 * @param[in] ifftFlag RFFT if flag is 0, RIFFT if flag is 1
288 void arm_rfft_fast_f32(
289 arm_rfft_fast_instance_f32
* S
,
290 float32_t
* p
, float32_t
* pOut
,
293 arm_cfft_instance_f32
* Sint
= &(S
->Sint
);
294 Sint
->fftLen
= S
->fftLenRFFT
/ 2;
296 /* Calculation of Real FFT */
299 /* Real FFT compression */
300 merge_rfft_f32(S
, p
, pOut
);
302 /* Complex radix-4 IFFT process */
303 arm_cfft_f32( Sint
, pOut
, ifftFlag
, 1);
307 /* Calculation of RFFT of input */
308 arm_cfft_f32( Sint
, p
, ifftFlag
, 1);
310 /* Real FFT extraction */
311 stage_rfft_f32(S
, p
, pOut
);
316 * @} end of RealFFT group