arm_rfft_fast_f32.c 12 KB

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