fun1_fft.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // File: fft.cpp
  3. //
  4. // MATLAB Coder version : 5.2
  5. // C/C++ source code generated on : 11-Mar-2023 10:38:07
  6. //
  7. // Include Files
  8. #include "fun1_fft.h"
  9. #include "fun1_FFTImplementationCallback.h"
  10. #include "rt_nonfinite.h"
  11. #include "coder_array.h"
  12. #include <cmath>
  13. // Function Definitions
  14. //
  15. // Arguments : const ::coder::array<double, 2U> &x
  16. // ::coder::array<creal_T, 2U> &y
  17. // Return Type : void
  18. //
  19. namespace coder {
  20. void fft(const ::coder::array<double, 2U> &x, ::coder::array<creal_T, 2U> &y)
  21. {
  22. array<creal_T, 1U> b_fv;
  23. array<creal_T, 1U> fv;
  24. array<creal_T, 1U> wwc;
  25. array<creal_T, 1U> yCol;
  26. array<double, 2U> costab;
  27. array<double, 2U> costab1q;
  28. array<double, 2U> sintab;
  29. array<double, 2U> sintabinv;
  30. array<double, 1U> b_x;
  31. int N2blue;
  32. int nRows;
  33. if (x.size(1) == 0) {
  34. y.set_size(1, 0);
  35. } else {
  36. double nt_re;
  37. int i;
  38. int k;
  39. int nd2;
  40. boolean_T useRadix2;
  41. useRadix2 = ((x.size(1) & (x.size(1) - 1)) == 0);
  42. internal::fun1_FFTImplementationCallback::get_algo_sizes(x.size(1), useRadix2,
  43. &N2blue, &nRows);
  44. nt_re = 6.2831853071795862 / static_cast<double>(nRows);
  45. nRows = nRows / 2 / 2;
  46. costab1q.set_size(1, nRows + 1);
  47. costab1q[0] = 1.0;
  48. nd2 = nRows / 2 - 1;
  49. for (k = 0; k <= nd2; k++) {
  50. costab1q[k + 1] = std::cos(nt_re * (static_cast<double>(k) + 1.0));
  51. }
  52. i = nd2 + 2;
  53. nd2 = nRows - 1;
  54. for (k = i; k <= nd2; k++) {
  55. costab1q[k] = std::sin(nt_re * static_cast<double>(nRows - k));
  56. }
  57. costab1q[nRows] = 0.0;
  58. if (!useRadix2) {
  59. nRows = costab1q.size(1) - 1;
  60. nd2 = (costab1q.size(1) - 1) << 1;
  61. costab.set_size(1, nd2 + 1);
  62. sintab.set_size(1, nd2 + 1);
  63. costab[0] = 1.0;
  64. sintab[0] = 0.0;
  65. sintabinv.set_size(1, nd2 + 1);
  66. for (k = 0; k < nRows; k++) {
  67. sintabinv[k + 1] = costab1q[(nRows - k) - 1];
  68. }
  69. i = costab1q.size(1);
  70. for (k = i; k <= nd2; k++) {
  71. sintabinv[k] = costab1q[k - nRows];
  72. }
  73. for (k = 0; k < nRows; k++) {
  74. costab[k + 1] = costab1q[k + 1];
  75. sintab[k + 1] = -costab1q[(nRows - k) - 1];
  76. }
  77. i = costab1q.size(1);
  78. for (k = i; k <= nd2; k++) {
  79. costab[k] = -costab1q[nd2 - k];
  80. sintab[k] = -costab1q[k - nRows];
  81. }
  82. } else {
  83. nRows = costab1q.size(1) - 1;
  84. nd2 = (costab1q.size(1) - 1) << 1;
  85. costab.set_size(1, nd2 + 1);
  86. sintab.set_size(1, nd2 + 1);
  87. costab[0] = 1.0;
  88. sintab[0] = 0.0;
  89. for (k = 0; k < nRows; k++) {
  90. costab[k + 1] = costab1q[k + 1];
  91. sintab[k + 1] = -costab1q[(nRows - k) - 1];
  92. }
  93. i = costab1q.size(1);
  94. for (k = i; k <= nd2; k++) {
  95. costab[k] = -costab1q[nd2 - k];
  96. sintab[k] = -costab1q[k - nRows];
  97. }
  98. sintabinv.set_size(1, 0);
  99. }
  100. if (useRadix2) {
  101. yCol.set_size(x.size(1));
  102. if (x.size(1) != 1) {
  103. nd2 = x.size(1);
  104. b_x = x.reshape(nd2);
  105. internal::fun1_FFTImplementationCallback::doHalfLengthRadix2(
  106. b_x, yCol, x.size(1), costab, sintab);
  107. } else {
  108. yCol[0].re = x[0];
  109. yCol[0].im = 0.0;
  110. }
  111. } else {
  112. int nfft;
  113. nd2 = x.size(1);
  114. b_x = x.reshape(nd2);
  115. nfft = x.size(1);
  116. if ((nfft != 1) && ((nfft & 1) == 0)) {
  117. int nInt2;
  118. int nInt2m1;
  119. int rt;
  120. nRows = nfft / 2;
  121. nInt2m1 = (nRows + nRows) - 1;
  122. wwc.set_size(nInt2m1);
  123. rt = 0;
  124. wwc[nRows - 1].re = 1.0;
  125. wwc[nRows - 1].im = 0.0;
  126. nInt2 = nRows << 1;
  127. for (k = 0; k <= nRows - 2; k++) {
  128. double nt_im;
  129. nd2 = ((k + 1) << 1) - 1;
  130. if (nInt2 - rt <= nd2) {
  131. rt += nd2 - nInt2;
  132. } else {
  133. rt += nd2;
  134. }
  135. nt_im = -3.1415926535897931 * static_cast<double>(rt) /
  136. static_cast<double>(nRows);
  137. if (nt_im == 0.0) {
  138. nt_re = 1.0;
  139. nt_im = 0.0;
  140. } else {
  141. nt_re = std::cos(nt_im);
  142. nt_im = std::sin(nt_im);
  143. }
  144. i = (nRows - k) - 2;
  145. wwc[i].re = nt_re;
  146. wwc[i].im = -nt_im;
  147. }
  148. i = nInt2m1 - 1;
  149. for (k = i; k >= nRows; k--) {
  150. wwc[k] = wwc[(nInt2m1 - k) - 1];
  151. }
  152. } else {
  153. int nInt2;
  154. int nInt2m1;
  155. int rt;
  156. nInt2m1 = (nfft + nfft) - 1;
  157. wwc.set_size(nInt2m1);
  158. rt = 0;
  159. wwc[nfft - 1].re = 1.0;
  160. wwc[nfft - 1].im = 0.0;
  161. nInt2 = nfft << 1;
  162. for (k = 0; k <= nfft - 2; k++) {
  163. double nt_im;
  164. nd2 = ((k + 1) << 1) - 1;
  165. if (nInt2 - rt <= nd2) {
  166. rt += nd2 - nInt2;
  167. } else {
  168. rt += nd2;
  169. }
  170. nt_im = -3.1415926535897931 * static_cast<double>(rt) /
  171. static_cast<double>(nfft);
  172. if (nt_im == 0.0) {
  173. nt_re = 1.0;
  174. nt_im = 0.0;
  175. } else {
  176. nt_re = std::cos(nt_im);
  177. nt_im = std::sin(nt_im);
  178. }
  179. i = (nfft - k) - 2;
  180. wwc[i].re = nt_re;
  181. wwc[i].im = -nt_im;
  182. }
  183. i = nInt2m1 - 1;
  184. for (k = i; k >= nfft; k--) {
  185. wwc[k] = wwc[(nInt2m1 - k) - 1];
  186. }
  187. }
  188. yCol.set_size(nfft);
  189. if (nfft > b_x.size(0)) {
  190. yCol.set_size(nfft);
  191. for (i = 0; i < nfft; i++) {
  192. yCol[i].re = 0.0;
  193. yCol[i].im = 0.0;
  194. }
  195. }
  196. if ((N2blue != 1) && ((nfft & 1) == 0)) {
  197. internal::fun1_FFTImplementationCallback::doHalfLengthBluestein(
  198. b_x, yCol, b_x.size(0), nfft, N2blue, wwc, costab, sintab, costab,
  199. sintabinv);
  200. } else {
  201. nd2 = b_x.size(0);
  202. if (nfft < nd2) {
  203. nd2 = nfft;
  204. }
  205. for (k = 0; k < nd2; k++) {
  206. nRows = (nfft + k) - 1;
  207. yCol[k].re = wwc[nRows].re * b_x[k];
  208. yCol[k].im = wwc[nRows].im * -b_x[k];
  209. }
  210. i = nd2 + 1;
  211. for (k = i; k <= nfft; k++) {
  212. yCol[k - 1].re = 0.0;
  213. yCol[k - 1].im = 0.0;
  214. }
  215. internal::fun1_FFTImplementationCallback::r2br_r2dit_trig_impl(
  216. yCol, N2blue, costab, sintab, fv);
  217. internal::fun1_FFTImplementationCallback::r2br_r2dit_trig_impl(
  218. wwc, N2blue, costab, sintab, b_fv);
  219. b_fv.set_size(fv.size(0));
  220. nd2 = fv.size(0);
  221. for (i = 0; i < nd2; i++) {
  222. nt_re = fv[i].re * b_fv[i].im + fv[i].im * b_fv[i].re;
  223. b_fv[i].re = fv[i].re * b_fv[i].re - fv[i].im * b_fv[i].im;
  224. b_fv[i].im = nt_re;
  225. }
  226. internal::fun1_FFTImplementationCallback::r2br_r2dit_trig_impl(
  227. b_fv, N2blue, costab, sintabinv, fv);
  228. if (fv.size(0) > 1) {
  229. nt_re = 1.0 / static_cast<double>(fv.size(0));
  230. nd2 = fv.size(0);
  231. for (i = 0; i < nd2; i++) {
  232. fv[i].re = nt_re * fv[i].re;
  233. fv[i].im = nt_re * fv[i].im;
  234. }
  235. }
  236. i = wwc.size(0);
  237. for (k = nfft; k <= i; k++) {
  238. nd2 = k - nfft;
  239. yCol[nd2].re =
  240. wwc[k - 1].re * fv[k - 1].re + wwc[k - 1].im * fv[k - 1].im;
  241. yCol[nd2].im =
  242. wwc[k - 1].re * fv[k - 1].im - wwc[k - 1].im * fv[k - 1].re;
  243. }
  244. }
  245. }
  246. y.set_size(1, x.size(1));
  247. nd2 = x.size(1);
  248. for (i = 0; i < nd2; i++) {
  249. y[i] = yCol[i];
  250. }
  251. }
  252. }
  253. } // namespace coder
  254. //
  255. // File trailer for fft.cpp
  256. //
  257. // [EOF]
  258. //