fun7_DRFMRF.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // File: DRFMRF.cpp
  3. //
  4. // MATLAB Coder version : 5.2
  5. // C/C++ source code generated on : 04-Mar-2023 17:44:30
  6. //
  7. // Include Files
  8. #include "fun7_DRFMRF.h"
  9. #include "fun7_DRFMRF_data.h"
  10. #include "fun7_DRFMRF_initialize.h"
  11. #include "fun7_ifft.h"
  12. #include "fun7_randn.h"
  13. #include "coder_array.h"
  14. #include <cmath>
  15. #include <iostream>
  16. using namespace std;
  17. // Function Declarations
  18. static double rt_hypotd_snf(double u0, double u1);
  19. // Function Definitions
  20. //
  21. // Arguments : double u0
  22. // double u1
  23. // Return Type : double
  24. //
  25. static double rt_hypotd_snf(double u0, double u1)
  26. {
  27. double a;
  28. double y;
  29. a = std::abs(u0);
  30. y = std::abs(u1);
  31. if (a < y) {
  32. a /= y;
  33. y *= std::sqrt(a * a + 1.0);
  34. } else if (a > y) {
  35. y /= a;
  36. y = a * std::sqrt(y * y + 1.0);
  37. } else if (!std::isnan(y)) {
  38. y = a * 1.4142135623730951;
  39. }
  40. return y;
  41. }
  42. //
  43. // Arguments : const coder::array<double, 1U> &pulse_sig
  44. // double f_s
  45. // double B_n
  46. // double forward_time_length
  47. // double forward_times
  48. // coder::array<double, 2U> &rxsig_noise
  49. // coder::array<double, 1U> &s
  50. // Return Type : void
  51. //
  52. void DRFMRF(const coder::array<double, 1U> &pulse_sig, double f_s, double B_n,
  53. double forward_time_length, double forward_times,
  54. coder::array<double, 2U> &rxsig_noise, coder::array<double, 1U> &s)
  55. {
  56. coder::array<creal_T, 2U> S;
  57. coder::array<creal_T, 2U> s_n;
  58. coder::array<double, 2U> b_b;
  59. coder::array<double, 2U> b_r;
  60. coder::array<double, 2U> pulse_sig_noise_t;
  61. coder::array<double, 2U> r;
  62. coder::array<double, 2U> varargin_2;
  63. coder::array<double, 1U> absdiff;
  64. coder::array<boolean_T, 1U> x;
  65. double b[2];
  66. double bsum_im;
  67. double bsum_re;
  68. double idx1;
  69. double xbar_im;
  70. int N;
  71. int ibcol;
  72. unsigned int idx_data;
  73. int ii_data;
  74. int input_sizes_idx_0;
  75. int k;
  76. int nblocks;
  77. int nx;
  78. if (!isInitialized_DRFMRF) {
  79. DRFMRF_initialize();
  80. }
  81. if (pulse_sig.size(0) > 100) {
  82. boolean_T exitg1;
  83. nx = pulse_sig.size(0);
  84. absdiff.set_size(pulse_sig.size(0));
  85. for (k = 0; k < nx; k++) {
  86. absdiff[k] = std::abs(pulse_sig[k]);
  87. }
  88. x.set_size(absdiff.size(0));
  89. nx = absdiff.size(0);
  90. for (nblocks = 0; nblocks < nx; nblocks++) {
  91. x[nblocks] = (absdiff[nblocks] > 0.0);
  92. }
  93. nx = 0;
  94. ibcol = 1;
  95. input_sizes_idx_0 = 0;
  96. exitg1 = false;
  97. while ((!exitg1) && (input_sizes_idx_0 <= x.size(0) - 1)) {
  98. if (x[input_sizes_idx_0]) {
  99. nx = 1;
  100. ii_data = input_sizes_idx_0 + 1;
  101. exitg1 = true;
  102. } else {
  103. input_sizes_idx_0++;
  104. }
  105. }
  106. if (nx == 0) {
  107. ibcol = 0;
  108. }
  109. for (nblocks = 0; nblocks < ibcol; nblocks++) {
  110. idx_data = static_cast<unsigned int>(ii_data);
  111. }
  112. N = pulse_sig.size(0);
  113. }
  114. idx1 = static_cast<double>(idx_data) + std::round(f_s * 2.0E-6);
  115. bsum_re = std::round(f_s * forward_time_length);
  116. r.set_size(1, static_cast<int>(bsum_re) + 1);
  117. nx = static_cast<int>(bsum_re);
  118. for (nblocks = 0; nblocks <= nx; nblocks++) {
  119. r[nblocks] = idx1 + static_cast<double>(nblocks);
  120. }
  121. b[0] = idx1 + static_cast<double>(r.size(1));
  122. varargin_2.set_size(r.size(1) * static_cast<int>(forward_times), 1);
  123. nx = r.size(1);
  124. ii_data = static_cast<int>(forward_times);
  125. for (input_sizes_idx_0 = 0; input_sizes_idx_0 < ii_data;
  126. input_sizes_idx_0++) {
  127. ibcol = input_sizes_idx_0 * nx;
  128. for (k = 0; k < nx; k++) {
  129. varargin_2[ibcol + k] = pulse_sig[static_cast<int>(r[k]) - 1];
  130. }
  131. }
  132. if (static_cast<int>(b[0]) != 0) {
  133. input_sizes_idx_0 = static_cast<int>(b[0]);
  134. } else {
  135. input_sizes_idx_0 = 0;
  136. }
  137. if (varargin_2.size(0) != 0) {
  138. nx = varargin_2.size(0);
  139. } else {
  140. nx = 0;
  141. }
  142. pulse_sig_noise_t.set_size(input_sizes_idx_0 + nx, 1);
  143. for (nblocks = 0; nblocks < input_sizes_idx_0; nblocks++) {
  144. pulse_sig_noise_t[nblocks] = 0.0;
  145. }
  146. for (nblocks = 0; nblocks < nx; nblocks++) {
  147. pulse_sig_noise_t[nblocks + input_sizes_idx_0] = varargin_2[nblocks];
  148. }
  149. if (static_cast<double>(r.size(1)) * forward_times + idx1 >=
  150. pulse_sig.size(0)) {
  151. nx = pulse_sig.size(0);
  152. rxsig_noise.set_size(pulse_sig.size(0), 1);
  153. for (nblocks = 0; nblocks < nx; nblocks++) {
  154. rxsig_noise[nblocks] = pulse_sig_noise_t[nblocks];
  155. }
  156. } else {
  157. nx = pulse_sig_noise_t.size(0);
  158. if (nx <= 1) {
  159. nx = 1;
  160. }
  161. if (pulse_sig_noise_t.size(0) == 0) {
  162. nx = 0;
  163. }
  164. if (pulse_sig.size(0) > nx) {
  165. nx = pulse_sig_noise_t.size(0);
  166. if (nx <= 1) {
  167. nx = 1;
  168. }
  169. if (pulse_sig_noise_t.size(0) == 0) {
  170. nx = 0;
  171. }
  172. nx = pulse_sig.size(0) - nx;
  173. } else {
  174. nx = 0;
  175. }
  176. if (pulse_sig_noise_t.size(0) != 0) {
  177. input_sizes_idx_0 = pulse_sig_noise_t.size(0);
  178. } else {
  179. input_sizes_idx_0 = 0;
  180. }
  181. if (nx == 0) {
  182. nx = 0;
  183. }
  184. rxsig_noise.set_size(input_sizes_idx_0 + nx, 1);
  185. for (nblocks = 0; nblocks < input_sizes_idx_0; nblocks++) {
  186. rxsig_noise[nblocks] = pulse_sig_noise_t[nblocks];
  187. }
  188. for (nblocks = 0; nblocks < nx; nblocks++) {
  189. rxsig_noise[nblocks + input_sizes_idx_0] = 0.0;
  190. }
  191. }
  192. idx1 = std::round(static_cast<double>(N) * (B_n / (2.0 * f_s)));
  193. b[0] = 1.0;
  194. b[1] = N;
  195. coder::randn_7(b, r);
  196. b_r.set_size(1, N);
  197. for (nblocks = 0; nblocks < N; nblocks++) {
  198. b_r[nblocks] = r[nblocks];
  199. }
  200. b[0] = 1.0;
  201. b[1] = N;
  202. coder::randn_7(b, r);
  203. b_b.set_size(1, N);
  204. for (nblocks = 0; nblocks < N; nblocks++) {
  205. b_b[nblocks] = r[nblocks];
  206. }
  207. S.set_size(1, b_r.size(1));
  208. nx = b_r.size(1);
  209. for (nblocks = 0; nblocks < nx; nblocks++) {
  210. S[nblocks].re = b_r[nblocks] + 0.0 * b_b[nblocks];
  211. S[nblocks].im = b_b[nblocks];
  212. }
  213. nblocks =
  214. static_cast<int>(((static_cast<double>(N) - idx1) - 1.0) + (1.0 - idx1));
  215. for (ii_data = 0; ii_data < nblocks; ii_data++) {
  216. nx = static_cast<int>(idx1 + static_cast<double>(ii_data)) - 1;
  217. S[nx].re = 0.0;
  218. S[nx].im = 0.0;
  219. }
  220. coder::ifft_7(S, s_n);
  221. ibcol = s_n.size(1);
  222. if (s_n.size(1) <= 1024) {
  223. nx = s_n.size(1);
  224. N = 0;
  225. nblocks = 1;
  226. } else {
  227. nx = 1024;
  228. nblocks = s_n.size(1) / 1024;
  229. N = s_n.size(1) - (nblocks << 10);
  230. if (N > 0) {
  231. nblocks++;
  232. } else {
  233. N = 1024;
  234. }
  235. }
  236. idx1 = s_n[0].re;
  237. xbar_im = s_n[0].im;
  238. for (k = 2; k <= nx; k++) {
  239. idx1 += s_n[k - 1].re;
  240. xbar_im += s_n[k - 1].im;
  241. }
  242. for (int ib{2}; ib <= nblocks; ib++) {
  243. nx = (ib - 1) << 10;
  244. bsum_re = s_n[nx].re;
  245. bsum_im = s_n[nx].im;
  246. if (ib == nblocks) {
  247. ii_data = N;
  248. } else {
  249. ii_data = 1024;
  250. }
  251. for (k = 2; k <= ii_data; k++) {
  252. input_sizes_idx_0 = (nx + k) - 1;
  253. bsum_re += s_n[input_sizes_idx_0].re;
  254. bsum_im += s_n[input_sizes_idx_0].im;
  255. }
  256. idx1 += bsum_re;
  257. xbar_im += bsum_im;
  258. }
  259. if (xbar_im == 0.0) {
  260. bsum_im = idx1 / static_cast<double>(s_n.size(1));
  261. idx1 = 0.0;
  262. } else if (idx1 == 0.0) {
  263. bsum_im = 0.0;
  264. idx1 = xbar_im / static_cast<double>(s_n.size(1));
  265. } else {
  266. bsum_im = idx1 / static_cast<double>(s_n.size(1));
  267. idx1 = xbar_im / static_cast<double>(s_n.size(1));
  268. }
  269. absdiff.set_size(s_n.size(1));
  270. for (k = 0; k < ibcol; k++) {
  271. absdiff[k] = rt_hypotd_snf(s_n[k].re - bsum_im, s_n[k].im - idx1);
  272. }
  273. xbar_im = 0.0;
  274. idx1 = 3.3121686421112381E-170;
  275. nx = s_n.size(1);
  276. for (k = 0; k < nx; k++) {
  277. if (absdiff[k] > idx1) {
  278. bsum_re = idx1 / absdiff[k];
  279. xbar_im = xbar_im * bsum_re * bsum_re + 1.0;
  280. idx1 = absdiff[k];
  281. } else {
  282. bsum_re = absdiff[k] / idx1;
  283. xbar_im += bsum_re * bsum_re;
  284. }
  285. }
  286. xbar_im = idx1 * std::sqrt(xbar_im);
  287. xbar_im /= std::sqrt(static_cast<double>(s_n.size(1)) - 1.0);
  288. s_n.set_size(1, s_n.size(1));
  289. nx = s_n.size(1) - 1;
  290. for (nblocks = 0; nblocks <= nx; nblocks++) {
  291. idx1 = s_n[nblocks].re;
  292. bsum_re = s_n[nblocks].im;
  293. if (bsum_re == 0.0) {
  294. bsum_im = idx1 / xbar_im;
  295. idx1 = 0.0;
  296. } else if (idx1 == 0.0) {
  297. bsum_im = 0.0;
  298. idx1 = bsum_re / xbar_im;
  299. } else {
  300. bsum_im = idx1 / xbar_im;
  301. idx1 = bsum_re / xbar_im;
  302. }
  303. s_n[nblocks].re = bsum_im;
  304. s_n[nblocks].im = idx1;
  305. }
  306. s.set_size(rxsig_noise.size(0));
  307. nx = rxsig_noise.size(0);
  308. for (nblocks = 0; nblocks < nx; nblocks++) {
  309. s[nblocks] = rxsig_noise[nblocks] * s_n[nblocks].re;
  310. }
  311. }
  312. static coder::array<double, 1U> argInit_Unboundedx1_real_T()
  313. {
  314. coder::array<double, 1U> result;
  315. // Set the size of the array.
  316. // Change this size to the value that the application requires.
  317. result.set_size(101);
  318. // Loop over the array to initialize each element.
  319. for (int idx0{0}; idx0 < result.size(0); idx0++) {
  320. // Set the value of the array element.
  321. // Change this value to the value that the application requires.
  322. result[idx0] = 2;
  323. }
  324. return result;
  325. }
  326. //int main()
  327. //{
  328. // coder::array<double, 1U> pulse_sig;
  329. // coder::array<double, 2U> rxsig_noise;
  330. // coder::array<double, 1U> s;
  331. // double f_s = 10e6;
  332. // double B_n = 1e6;
  333. // double forward_time_length = 2e-6;
  334. // double forward_times = 4;
  335. // pulse_sig = argInit_Unboundedx1_real_T();
  336. //
  337. // // Call the entry-point 'DRFMRF'.
  338. // DRFMRF(pulse_sig, f_s, B_n, forward_time_length, forward_times, rxsig_noise, s);
  339. // cout << "rxsig_noise=" <<endl;
  340. // cout << "rxsig_noise's length =" <<rxsig_noise.size(0)<<endl;
  341. // for (int i = 0; i < rxsig_noise.size(0); i++) {
  342. // std::cout << rxsig_noise[i] << " ";
  343. // }
  344. // cout << endl;
  345. // cout << "S's length =" <<s.size(0)<<endl;
  346. // cout << "S=" <<endl;
  347. // for (int i = 0; i < s.size(0); i++) {
  348. // std::cout << s[i] << " ";
  349. // }
  350. // return 0;
  351. //}