arm_rfft_fast_f32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rfft_f32.c
  4. * Description: RFFT & RIFFT Floating point process function
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  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
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  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.
  27. */
  28. #include "arm_math.h"
  29. void stage_rfft_f32(
  30. arm_rfft_fast_instance_f32 * S,
  31. float32_t * p, float32_t * pOut)
  32. {
  33. uint32_t k; /* Loop Counter */
  34. float32_t twR, twI; /* RFFT Twiddle coefficients */
  35. float32_t * pCoeff = S->pTwiddleRFFT; /* Points to RFFT Twiddle factors */
  36. float32_t *pA = p; /* increasing pointer */
  37. float32_t *pB = p; /* decreasing pointer */
  38. float32_t xAR, xAI, xBR, xBI; /* temporary variables */
  39. float32_t t1a, t1b; /* temporary variables */
  40. float32_t p0, p1, p2, p3; /* temporary variables */
  41. k = (S->Sint).fftLen - 1;
  42. /* Pack first and last sample of the frequency domain together */
  43. xBR = pB[0];
  44. xBI = pB[1];
  45. xAR = pA[0];
  46. xAI = pA[1];
  47. twR = *pCoeff++ ;
  48. twI = *pCoeff++ ;
  49. // U1 = XA(1) + XB(1); % It is real
  50. t1a = xBR + xAR ;
  51. // U2 = XB(1) - XA(1); % It is imaginary
  52. t1b = xBI + xAI ;
  53. // real(tw * (xB - xA)) = twR * (xBR - xAR) - twI * (xBI - xAI);
  54. // imag(tw * (xB - xA)) = twI * (xBR - xAR) + twR * (xBI - xAI);
  55. *pOut++ = 0.5f * ( t1a + t1b );
  56. *pOut++ = 0.5f * ( t1a - t1b );
  57. // XA(1) = 1/2*( U1 - imag(U2) + i*( U1 +imag(U2) ));
  58. pB = p + 2*k;
  59. pA += 2;
  60. do
  61. {
  62. /*
  63. function X = my_split_rfft(X, ifftFlag)
  64. % X is a series of real numbers
  65. L = length(X);
  66. XC = X(1:2:end) +i*X(2:2:end);
  67. XA = fft(XC);
  68. XB = conj(XA([1 end:-1:2]));
  69. TW = i*exp(-2*pi*i*[0:L/2-1]/L).';
  70. for l = 2:L/2
  71. XA(l) = 1/2 * (XA(l) + XB(l) + TW(l) * (XB(l) - XA(l)));
  72. end
  73. 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))));
  74. X = XA;
  75. */
  76. xBI = pB[1];
  77. xBR = pB[0];
  78. xAR = pA[0];
  79. xAI = pA[1];
  80. twR = *pCoeff++;
  81. twI = *pCoeff++;
  82. t1a = xBR - xAR ;
  83. t1b = xBI + xAI ;
  84. // real(tw * (xB - xA)) = twR * (xBR - xAR) - twI * (xBI - xAI);
  85. // imag(tw * (xB - xA)) = twI * (xBR - xAR) + twR * (xBI - xAI);
  86. p0 = twR * t1a;
  87. p1 = twI * t1a;
  88. p2 = twR * t1b;
  89. p3 = twI * t1b;
  90. *pOut++ = 0.5f * (xAR + xBR + p0 + p3 ); //xAR
  91. *pOut++ = 0.5f * (xAI - xBI + p1 - p2 ); //xAI
  92. pA += 2;
  93. pB -= 2;
  94. k--;
  95. } while (k > 0U);
  96. }
  97. /* Prepares data for inverse cfft */
  98. void merge_rfft_f32(
  99. arm_rfft_fast_instance_f32 * S,
  100. float32_t * p, float32_t * pOut)
  101. {
  102. uint32_t k; /* Loop Counter */
  103. float32_t twR, twI; /* RFFT Twiddle coefficients */
  104. float32_t *pCoeff = S->pTwiddleRFFT; /* Points to RFFT Twiddle factors */
  105. float32_t *pA = p; /* increasing pointer */
  106. float32_t *pB = p; /* decreasing pointer */
  107. float32_t xAR, xAI, xBR, xBI; /* temporary variables */
  108. float32_t t1a, t1b, r, s, t, u; /* temporary variables */
  109. k = (S->Sint).fftLen - 1;
  110. xAR = pA[0];
  111. xAI = pA[1];
  112. pCoeff += 2 ;
  113. *pOut++ = 0.5f * ( xAR + xAI );
  114. *pOut++ = 0.5f * ( xAR - xAI );
  115. pB = p + 2*k ;
  116. pA += 2 ;
  117. while (k > 0U)
  118. {
  119. /* G is half of the frequency complex spectrum */
  120. //for k = 2:N
  121. // Xk(k) = 1/2 * (G(k) + conj(G(N-k+2)) + Tw(k)*( G(k) - conj(G(N-k+2))));
  122. xBI = pB[1] ;
  123. xBR = pB[0] ;
  124. xAR = pA[0];
  125. xAI = pA[1];
  126. twR = *pCoeff++;
  127. twI = *pCoeff++;
  128. t1a = xAR - xBR ;
  129. t1b = xAI + xBI ;
  130. r = twR * t1a;
  131. s = twI * t1b;
  132. t = twI * t1a;
  133. u = twR * t1b;
  134. // real(tw * (xA - xB)) = twR * (xAR - xBR) - twI * (xAI - xBI);
  135. // imag(tw * (xA - xB)) = twI * (xAR - xBR) + twR * (xAI - xBI);
  136. *pOut++ = 0.5f * (xAR + xBR - r - s ); //xAR
  137. *pOut++ = 0.5f * (xAI - xBI + t - u ); //xAI
  138. pA += 2;
  139. pB -= 2;
  140. k--;
  141. }
  142. }
  143. /**
  144. * @ingroup groupTransforms
  145. */
  146. /**
  147. * @defgroup RealFFT Real FFT Functions
  148. *
  149. * \par
  150. * The CMSIS DSP library includes specialized algorithms for computing the
  151. * FFT of real data sequences. The FFT is defined over complex data but
  152. * in many applications the input is real. Real FFT algorithms take advantage
  153. * of the symmetry properties of the FFT and have a speed advantage over complex
  154. * algorithms of the same length.
  155. * \par
  156. * The Fast RFFT algorith relays on the mixed radix CFFT that save processor usage.
  157. * \par
  158. * The real length N forward FFT of a sequence is computed using the steps shown below.
  159. * \par
  160. * \image html RFFT.gif "Real Fast Fourier Transform"
  161. * \par
  162. * The real sequence is initially treated as if it were complex to perform a CFFT.
  163. * Later, a processing stage reshapes the data to obtain half of the frequency spectrum
  164. * in complex format. Except the first complex number that contains the two real numbers
  165. * X[0] and X[N/2] all the data is complex. In other words, the first complex sample
  166. * contains two real values packed.
  167. * \par
  168. * The input for the inverse RFFT should keep the same format as the output of the
  169. * forward RFFT. A first processing stage pre-process the data to later perform an
  170. * inverse CFFT.
  171. * \par
  172. * \image html RIFFT.gif "Real Inverse Fast Fourier Transform"
  173. * \par
  174. * The algorithms for floating-point, Q15, and Q31 data are slightly different
  175. * and we describe each algorithm in turn.
  176. * \par Floating-point
  177. * The main functions are arm_rfft_fast_f32() and arm_rfft_fast_init_f32().
  178. * The older functions arm_rfft_f32() and arm_rfft_init_f32() have been
  179. * deprecated but are still documented.
  180. * \par
  181. * The FFT of a real N-point sequence has even symmetry in the frequency
  182. * domain. The second half of the data equals the conjugate of the first
  183. * half flipped in frequency. Looking at the data, we see that we can
  184. * uniquely represent the FFT using only N/2 complex numbers. These are
  185. * packed into the output array in alternating real and imaginary
  186. * components:
  187. * \par
  188. * X = { real[0], imag[0], real[1], imag[1], real[2], imag[2] ...
  189. * real[(N/2)-1], imag[(N/2)-1 }
  190. * \par
  191. * It happens that the first complex number (real[0], imag[0]) is actually
  192. * all real. real[0] represents the DC offset, and imag[0] should be 0.
  193. * (real[1], imag[1]) is the fundamental frequency, (real[2], imag[2]) is
  194. * the first harmonic and so on.
  195. * \par
  196. * The real FFT functions pack the frequency domain data in this fashion.
  197. * The forward transform outputs the data in this form and the inverse
  198. * transform expects input data in this form. The function always performs
  199. * the needed bitreversal so that the input and output data is always in
  200. * normal order. The functions support lengths of [32, 64, 128, ..., 4096]
  201. * samples.
  202. * \par Q15 and Q31
  203. * The real algorithms are defined in a similar manner and utilize N/2 complex
  204. * transforms behind the scenes.
  205. * \par
  206. * The complex transforms used internally include scaling to prevent fixed-point
  207. * overflows. The overall scaling equals 1/(fftLen/2).
  208. * \par
  209. * A separate instance structure must be defined for each transform used but
  210. * twiddle factor and bit reversal tables can be reused.
  211. * \par
  212. * There is also an associated initialization function for each data type.
  213. * The initialization function performs the following operations:
  214. * - Sets the values of the internal structure fields.
  215. * - Initializes twiddle factor table and bit reversal table pointers.
  216. * - Initializes the internal complex FFT data structure.
  217. * \par
  218. * Use of the initialization function is optional.
  219. * However, if the initialization function is used, then the instance structure
  220. * cannot be placed into a const data section. To place an instance structure
  221. * into a const data section, the instance structure should be manually
  222. * initialized as follows:
  223. * <pre>
  224. *arm_rfft_instance_q31 S = {fftLenReal, fftLenBy2, ifftFlagR, bitReverseFlagR, twidCoefRModifier, pTwiddleAReal, pTwiddleBReal, pCfft};
  225. *arm_rfft_instance_q15 S = {fftLenReal, fftLenBy2, ifftFlagR, bitReverseFlagR, twidCoefRModifier, pTwiddleAReal, pTwiddleBReal, pCfft};
  226. * </pre>
  227. * where <code>fftLenReal</code> is the length of the real transform;
  228. * <code>fftLenBy2</code> length of the internal complex transform.
  229. * <code>ifftFlagR</code> Selects forward (=0) or inverse (=1) transform.
  230. * <code>bitReverseFlagR</code> Selects bit reversed output (=0) or normal order
  231. * output (=1).
  232. * <code>twidCoefRModifier</code> stride modifier for the twiddle factor table.
  233. * The value is based on the FFT length;
  234. * <code>pTwiddleAReal</code>points to the A array of twiddle coefficients;
  235. * <code>pTwiddleBReal</code>points to the B array of twiddle coefficients;
  236. * <code>pCfft</code> points to the CFFT Instance structure. The CFFT structure
  237. * must also be initialized. Refer to arm_cfft_radix4_f32() for details regarding
  238. * static initialization of the complex FFT instance structure.
  239. */
  240. /**
  241. * @addtogroup RealFFT
  242. * @{
  243. */
  244. /**
  245. * @brief Processing function for the floating-point real FFT.
  246. * @param[in] *S points to an arm_rfft_fast_instance_f32 structure.
  247. * @param[in] *p points to the input buffer.
  248. * @param[in] *pOut points to the output buffer.
  249. * @param[in] ifftFlag RFFT if flag is 0, RIFFT if flag is 1
  250. * @return none.
  251. */
  252. void arm_rfft_fast_f32(
  253. arm_rfft_fast_instance_f32 * S,
  254. float32_t * p, float32_t * pOut,
  255. uint8_t ifftFlag)
  256. {
  257. arm_cfft_instance_f32 * Sint = &(S->Sint);
  258. Sint->fftLen = S->fftLenRFFT / 2;
  259. /* Calculation of Real FFT */
  260. if (ifftFlag)
  261. {
  262. /* Real FFT compression */
  263. merge_rfft_f32(S, p, pOut);
  264. /* Complex radix-4 IFFT process */
  265. arm_cfft_f32( Sint, pOut, ifftFlag, 1);
  266. }
  267. else
  268. {
  269. /* Calculation of RFFT of input */
  270. arm_cfft_f32( Sint, p, ifftFlag, 1);
  271. /* Real FFT extraction */
  272. stage_rfft_f32(S, p, pOut);
  273. }
  274. }
  275. /**
  276. * @} end of RealFFT group
  277. */