fun8_2_FMjamming_H.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // File: FMjamming_H.cpp
  3. //
  4. // MATLAB Coder version : 5.2
  5. // C/C++ source code generated on : 10-Mar-2023 11:08:29
  6. //
  7. // Include Files
  8. #include "fun8_2_FMjamming_H.h"
  9. #include "fun8_2_FMjamming_H_data.h"
  10. #include "fun8_2_ifft.h"
  11. #include "fun8_2_randn.h"
  12. #include "rt_nonfinite.h"
  13. #include "fun8_2_std.h"
  14. #include "coder_array.h"
  15. #include <cmath>
  16. #include <iostream>
  17. #include "fun8_2_eml_rand_mt19937ar_stateful.h"
  18. using namespace std;
  19. void FMjamming_H1_initialize()
  20. {
  21. eml_rand_mt19937ar_stateful_init_8_2();
  22. isInitialized_FMjamming_H1 = true;
  23. }
  24. // Function Declarations
  25. static double rt_hypotd_snf(double u0, double u1);
  26. // Function Definitions
  27. //
  28. // Arguments : double u0
  29. // double u1
  30. // Return Type : double
  31. //
  32. static double rt_hypotd_snf(double u0, double u1)
  33. {
  34. double a;
  35. double y;
  36. a = std::abs(u0);
  37. y = std::abs(u1);
  38. if (a < y) {
  39. a /= y;
  40. y *= std::sqrt(a * a + 1.0);
  41. } else if (a > y) {
  42. y /= a;
  43. y = a * std::sqrt(y * y + 1.0);
  44. } else if (!std::isnan(y)) {
  45. y = a * 1.4142135623730951;
  46. }
  47. return y;
  48. }
  49. //
  50. // Arguments : double N
  51. // double B_n
  52. // double f_s
  53. // double T_s
  54. // double m_fe
  55. // double A
  56. // double f_0
  57. // coder::array<double, 2U> &s
  58. // coder::array<double, 2U> &s_0
  59. // Return Type : void
  60. //
  61. void FMjamming_H(double N, double B_n, double f_s, double T_s, double m_fe,
  62. double A, double f_0, coder::array<double, 2U> &s,
  63. coder::array<double, 2U> &s_0)
  64. {
  65. coder::array<creal_T, 2U> S;
  66. coder::array<creal_T, 2U> c_s_n;
  67. coder::array<double, 2U> b;
  68. coder::array<double, 2U> b_s_n;
  69. coder::array<double, 2U> sum;
  70. coder::array<double, 1U> absdiff;
  71. double s_n[2];
  72. double N_n;
  73. double bsum_im;
  74. double bsum_re;
  75. double xbar_im;
  76. int bsum_re_tmp;
  77. int firstBlockLength;
  78. int k;
  79. int loop_ub_tmp;
  80. int n;
  81. int xblockoffset;
  82. if (!isInitialized_FMjamming_H1) {
  83. FMjamming_H1_initialize();
  84. }
  85. if (std::isnan(N)) {
  86. s.set_size(1, 1);
  87. s[0] = rtNaN;
  88. } else if (N < 1.0) {
  89. s.set_size(s.size(0), 0);
  90. } else if (std::isinf(N) && (1.0 == N)) {
  91. s.set_size(1, 1);
  92. s[0] = rtNaN;
  93. } else {
  94. firstBlockLength = static_cast<int>(std::floor(N - 1.0));
  95. s.set_size(1, firstBlockLength + 1);
  96. for (bsum_re_tmp = 0; bsum_re_tmp <= firstBlockLength; bsum_re_tmp++) {
  97. s[bsum_re_tmp] = static_cast<double>(bsum_re_tmp) + 1.0;
  98. }
  99. }
  100. N_n = std::round(N * (B_n / (2.0 * f_s)));
  101. loop_ub_tmp = static_cast<int>(N);
  102. s_n[0] = 1.0;
  103. s_n[1] = static_cast<int>(N);
  104. coder::randn_8_2(s_n, sum);
  105. b_s_n.set_size(1, loop_ub_tmp);
  106. for (bsum_re_tmp = 0; bsum_re_tmp < loop_ub_tmp; bsum_re_tmp++) {
  107. b_s_n[bsum_re_tmp] = sum[bsum_re_tmp];
  108. }
  109. s_n[0] = 1.0;
  110. s_n[1] = static_cast<int>(N);
  111. coder::randn_8_2(s_n, sum);
  112. b.set_size(1, loop_ub_tmp);
  113. for (bsum_re_tmp = 0; bsum_re_tmp < loop_ub_tmp; bsum_re_tmp++) {
  114. b[bsum_re_tmp] = sum[bsum_re_tmp];
  115. }
  116. S.set_size(1, b_s_n.size(1));
  117. firstBlockLength = b_s_n.size(1);
  118. for (bsum_re_tmp = 0; bsum_re_tmp < firstBlockLength; bsum_re_tmp++) {
  119. S[bsum_re_tmp].re = b_s_n[bsum_re_tmp] + 0.0 * b[bsum_re_tmp];
  120. S[bsum_re_tmp].im = b[bsum_re_tmp];
  121. }
  122. bsum_re_tmp = static_cast<int>(((N - N_n) - 1.0) + (1.0 - N_n));
  123. for (xblockoffset = 0; xblockoffset < bsum_re_tmp; xblockoffset++) {
  124. firstBlockLength =
  125. static_cast<int>(N_n + static_cast<double>(xblockoffset)) - 1;
  126. S[firstBlockLength].re = 0.0;
  127. S[firstBlockLength].im = 0.0;
  128. }
  129. coder::ifft_8_2(S, c_s_n);
  130. n = c_s_n.size(1);
  131. if (c_s_n.size(1) == 0) {
  132. xbar_im = rtNaN;
  133. } else if (c_s_n.size(1) == 1) {
  134. if ((!std::isinf(c_s_n[0].re)) && (!std::isinf(c_s_n[0].im)) &&
  135. ((!std::isnan(c_s_n[0].re)) && (!std::isnan(c_s_n[0].im)))) {
  136. xbar_im = 0.0;
  137. } else {
  138. xbar_im = rtNaN;
  139. }
  140. } else {
  141. int lastBlockLength;
  142. int nblocks;
  143. if (c_s_n.size(1) <= 1024) {
  144. firstBlockLength = c_s_n.size(1);
  145. lastBlockLength = 0;
  146. nblocks = 1;
  147. } else {
  148. firstBlockLength = 1024;
  149. nblocks = c_s_n.size(1) / 1024;
  150. lastBlockLength = c_s_n.size(1) - (nblocks << 10);
  151. if (lastBlockLength > 0) {
  152. nblocks++;
  153. } else {
  154. lastBlockLength = 1024;
  155. }
  156. }
  157. N_n = c_s_n[0].re;
  158. xbar_im = c_s_n[0].im;
  159. for (k = 2; k <= firstBlockLength; k++) {
  160. N_n += c_s_n[k - 1].re;
  161. xbar_im += c_s_n[k - 1].im;
  162. }
  163. for (int ib{2}; ib <= nblocks; ib++) {
  164. xblockoffset = (ib - 1) << 10;
  165. bsum_re = c_s_n[xblockoffset].re;
  166. bsum_im = c_s_n[xblockoffset].im;
  167. if (ib == nblocks) {
  168. firstBlockLength = lastBlockLength;
  169. } else {
  170. firstBlockLength = 1024;
  171. }
  172. for (k = 2; k <= firstBlockLength; k++) {
  173. bsum_re_tmp = (xblockoffset + k) - 1;
  174. bsum_re += c_s_n[bsum_re_tmp].re;
  175. bsum_im += c_s_n[bsum_re_tmp].im;
  176. }
  177. N_n += bsum_re;
  178. xbar_im += bsum_im;
  179. }
  180. if (xbar_im == 0.0) {
  181. bsum_im = N_n / static_cast<double>(c_s_n.size(1));
  182. N_n = 0.0;
  183. } else if (N_n == 0.0) {
  184. bsum_im = 0.0;
  185. N_n = xbar_im / static_cast<double>(c_s_n.size(1));
  186. } else {
  187. bsum_im = N_n / static_cast<double>(c_s_n.size(1));
  188. N_n = xbar_im / static_cast<double>(c_s_n.size(1));
  189. }
  190. absdiff.set_size(c_s_n.size(1));
  191. for (k = 0; k < n; k++) {
  192. absdiff[k] = rt_hypotd_snf(c_s_n[k].re - bsum_im, c_s_n[k].im - N_n);
  193. }
  194. n = c_s_n.size(1);
  195. xbar_im = 0.0;
  196. N_n = 3.3121686421112381E-170;
  197. for (k = 0; k < n; k++) {
  198. if (absdiff[k] > N_n) {
  199. bsum_re = N_n / absdiff[k];
  200. xbar_im = xbar_im * bsum_re * bsum_re + 1.0;
  201. N_n = absdiff[k];
  202. } else {
  203. bsum_re = absdiff[k] / N_n;
  204. xbar_im += bsum_re * bsum_re;
  205. }
  206. }
  207. xbar_im = N_n * std::sqrt(xbar_im);
  208. xbar_im /= std::sqrt(static_cast<double>(c_s_n.size(1)) - 1.0);
  209. }
  210. c_s_n.set_size(1, c_s_n.size(1));
  211. firstBlockLength = c_s_n.size(1) - 1;
  212. for (bsum_re_tmp = 0; bsum_re_tmp <= firstBlockLength; bsum_re_tmp++) {
  213. N_n = c_s_n[bsum_re_tmp].re;
  214. bsum_re = c_s_n[bsum_re_tmp].im;
  215. if (bsum_re == 0.0) {
  216. bsum_im = N_n / xbar_im;
  217. N_n = 0.0;
  218. } else if (N_n == 0.0) {
  219. bsum_im = 0.0;
  220. N_n = bsum_re / xbar_im;
  221. } else {
  222. bsum_im = N_n / xbar_im;
  223. N_n = bsum_re / xbar_im;
  224. }
  225. c_s_n[bsum_re_tmp].re = bsum_im;
  226. c_s_n[bsum_re_tmp].im = N_n;
  227. }
  228. b_s_n.set_size(1, c_s_n.size(1));
  229. firstBlockLength = c_s_n.size(1);
  230. for (bsum_re_tmp = 0; bsum_re_tmp < firstBlockLength; bsum_re_tmp++) {
  231. b_s_n[bsum_re_tmp] = c_s_n[bsum_re_tmp].re;
  232. }
  233. sum.set_size(1, loop_ub_tmp);
  234. for (bsum_re_tmp = 0; bsum_re_tmp < loop_ub_tmp; bsum_re_tmp++) {
  235. sum[bsum_re_tmp] = 0.0;
  236. }
  237. bsum_re_tmp = static_cast<int>(N - 1.0);
  238. for (xblockoffset = 0; xblockoffset < bsum_re_tmp; xblockoffset++) {
  239. sum[xblockoffset + 1] = b_s_n[xblockoffset] + sum[xblockoffset];
  240. }
  241. if (m_fe >= 0.75) {
  242. N_n = B_n / 2.35 / coder::b_std_8_2(b_s_n);
  243. } else {
  244. N_n = m_fe * (B_n / (3.1415926535897931 * (m_fe * m_fe))) /
  245. coder::b_std_8_2(b_s_n);
  246. }
  247. bsum_re = 6.2831853071795862 * f_0;
  248. s.set_size(1, s.size(1));
  249. firstBlockLength = s.size(1) - 1;
  250. for (bsum_re_tmp = 0; bsum_re_tmp <= firstBlockLength; bsum_re_tmp++) {
  251. s[bsum_re_tmp] = bsum_re * s[bsum_re_tmp] * T_s;
  252. }
  253. s_0.set_size(1, s.size(1));
  254. firstBlockLength = s.size(1);
  255. for (bsum_re_tmp = 0; bsum_re_tmp < firstBlockLength; bsum_re_tmp++) {
  256. s_0[bsum_re_tmp] = s[bsum_re_tmp];
  257. }
  258. firstBlockLength = s.size(1);
  259. for (k = 0; k < firstBlockLength; k++) {
  260. s_0[k] = std::cos(s_0[k]);
  261. }
  262. s_0.set_size(1, s_0.size(1));
  263. firstBlockLength = s_0.size(1) - 1;
  264. for (bsum_re_tmp = 0; bsum_re_tmp <= firstBlockLength; bsum_re_tmp++) {
  265. s_0[bsum_re_tmp] = A * s_0[bsum_re_tmp];
  266. }
  267. bsum_re = 6.2831853071795862 * N_n;
  268. s.set_size(1, s.size(1));
  269. firstBlockLength = s.size(1) - 1;
  270. for (bsum_re_tmp = 0; bsum_re_tmp <= firstBlockLength; bsum_re_tmp++) {
  271. s[bsum_re_tmp] = s[bsum_re_tmp] + bsum_re * (sum[bsum_re_tmp] * T_s);
  272. }
  273. firstBlockLength = s.size(1);
  274. for (k = 0; k < firstBlockLength; k++) {
  275. s[k] = std::cos(s[k]);
  276. }
  277. s.set_size(1, s.size(1));
  278. firstBlockLength = s.size(1) - 1;
  279. for (bsum_re_tmp = 0; bsum_re_tmp <= firstBlockLength; bsum_re_tmp++) {
  280. s[bsum_re_tmp] = A * s[bsum_re_tmp];
  281. }
  282. }
  283. //int main()
  284. //{
  285. // coder::array<double, 2U> s;
  286. // coder::array<double, 2U> s_0;
  287. // double N=10000;
  288. // double B_n=2e6;
  289. // double f_s=100e6;
  290. // double T_s=100e-6;
  291. // double m_fe=1;
  292. // double A=1;
  293. // double f_0=10e6;
  294. // FMjamming_H(N, B_n, f_s, T_s, m_fe, A, f_0, s, s_0);
  295. // // 输出s
  296. // cout << "s=" <<endl;
  297. // for (int i = 0; i < s.size(1); i++) {
  298. // std::cout << s[i] << " ";
  299. // }
  300. // cout << endl;
  301. //
  302. //// // 输出s_0
  303. //// cout << "s_0=" <<endl;
  304. //// for (int i = 0; i < s_0.size(1); i++) {
  305. //// std::cout << s_0[i] << " ";
  306. //// }
  307. //// cout << endl;
  308. // return 0;
  309. //}