fun8_3_jamming_H.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // File: jamming_H.cpp
  3. //
  4. // MATLAB Coder version : 5.2
  5. // C/C++ source code generated on : 10-Mar-2023 15:16:29
  6. //
  7. // Include Files
  8. #include "fun8_3_jamming_H.h"
  9. #include "fun8_3_ifft.h"
  10. #include "fun8_3_jamming_H_data.h"
  11. #include "fun8_3_randn.h"
  12. #include "rt_nonfinite.h"
  13. #include "coder_array.h"
  14. #include <cmath>
  15. #include <iostream>
  16. #include <iomanip>
  17. #include "fun8_3_eml_rand_mt19937ar_stateful.h"
  18. void jamming_H_initialize()
  19. {
  20. eml_rand_mt19937ar_stateful_init_8_3();
  21. isInitialized_jamming_H = true;
  22. }
  23. // Function Declarations
  24. static double rt_hypotd_snf(double u0, double u1);
  25. // Function Definitions
  26. //
  27. // Arguments : double u0
  28. // double u1
  29. // Return Type : double
  30. //
  31. static double rt_hypotd_snf(double u0, double u1)
  32. {
  33. double a;
  34. double y;
  35. a = std::abs(u0);
  36. y = std::abs(u1);
  37. if (a < y) {
  38. a /= y;
  39. y *= std::sqrt(a * a + 1.0);
  40. } else if (a > y) {
  41. y /= a;
  42. y = a * std::sqrt(y * y + 1.0);
  43. } else if (!std::isnan(y)) {
  44. y = a * 1.4142135623730951;
  45. }
  46. return y;
  47. }
  48. //
  49. // 生成射频噪声干扰信号
  50. // 参数:
  51. // N - 信号长度
  52. // f_s - 系统采样率
  53. // A - 信号幅度
  54. // B_n - 射频信号要产生的带宽
  55. // f_0 - 调制信号频率
  56. // 返回值:
  57. // s - 生成的射频噪声干扰信号
  58. //
  59. // Arguments : double N
  60. // double f_s
  61. // double A
  62. // double B_n
  63. // double f_0
  64. // coder::array<creal_T, 2U> &s
  65. // Return Type : void
  66. //
  67. void jamming_H(double N, double f_s, double A, double B_n, double f_0,
  68. coder::array<creal_T, 2U> &s)
  69. {
  70. coder::array<creal_T, 2U> S;
  71. coder::array<creal_T, 2U> s_n;
  72. coder::array<double, 2U> b_r;
  73. coder::array<double, 2U> c_b;
  74. coder::array<double, 2U> n;
  75. coder::array<double, 2U> r;
  76. coder::array<double, 1U> absdiff;
  77. double b_b[2];
  78. double N_n;
  79. double b;
  80. double bsum_im;
  81. double bsum_re;
  82. double xbar_im;
  83. int b_n;
  84. int bsum_re_tmp;
  85. int firstBlockLength;
  86. int k;
  87. int xblockoffset;
  88. N = 1024;
  89. f_s = 10e6;
  90. A = 1;
  91. B_n = 2e6;
  92. f_0 = 2e6;
  93. if (!isInitialized_jamming_H) {
  94. jamming_H_initialize();
  95. }
  96. if (std::isnan(N)) {
  97. n.set_size(1, 1);
  98. n[0] = rtNaN;
  99. } else if (N < 1.0) {
  100. n.set_size(1, 0);
  101. } else if (std::isinf(N) && (1.0 == N)) {
  102. n.set_size(1, 1);
  103. n[0] = rtNaN;
  104. } else {
  105. firstBlockLength = static_cast<int>(std::floor(N - 1.0));
  106. n.set_size(1, firstBlockLength + 1);
  107. for (bsum_re_tmp = 0; bsum_re_tmp <= firstBlockLength; bsum_re_tmp++) {
  108. n[bsum_re_tmp] = static_cast<double>(bsum_re_tmp) + 1.0;
  109. }
  110. }
  111. b = 1.0 / f_s;
  112. // 调制噪声参数
  113. // delta_F = B_n / 2; % 射频信号要产生的带宽B_n时,噪声要产生的带宽
  114. N_n = std::round(N * (B_n / (2.0 * f_s)));
  115. // 频谱上采样点数
  116. b_b[0] = 1.0;
  117. b_b[1] = static_cast<int>(N);
  118. coder::randn(b_b, r);
  119. firstBlockLength = static_cast<int>(N);
  120. b_r.set_size(1, static_cast<int>(N));
  121. for (bsum_re_tmp = 0; bsum_re_tmp < firstBlockLength; bsum_re_tmp++) {
  122. b_r[bsum_re_tmp] = r[bsum_re_tmp];
  123. }
  124. b_b[0] = 1.0;
  125. b_b[1] = static_cast<int>(N);
  126. coder::randn(b_b, r);
  127. firstBlockLength = static_cast<int>(N);
  128. c_b.set_size(1, static_cast<int>(N));
  129. for (bsum_re_tmp = 0; bsum_re_tmp < firstBlockLength; bsum_re_tmp++) {
  130. c_b[bsum_re_tmp] = r[bsum_re_tmp];
  131. }
  132. S.set_size(1, b_r.size(1));
  133. firstBlockLength = b_r.size(1);
  134. for (bsum_re_tmp = 0; bsum_re_tmp < firstBlockLength; bsum_re_tmp++) {
  135. S[bsum_re_tmp].re = b_r[bsum_re_tmp] + 0.0 * c_b[bsum_re_tmp];
  136. S[bsum_re_tmp].im = c_b[bsum_re_tmp];
  137. }
  138. // 高斯白噪声频谱,中值0,标准差1,维度(1,N)
  139. bsum_re_tmp = static_cast<int>(((N - N_n) - 1.0) + (1.0 - N_n));
  140. for (xblockoffset = 0; xblockoffset < bsum_re_tmp; xblockoffset++) {
  141. firstBlockLength =
  142. static_cast<int>(N_n + static_cast<double>(xblockoffset)) - 1;
  143. S[firstBlockLength].re = 0.0;
  144. S[firstBlockLength].im = 0.0;
  145. }
  146. coder::ifft_8_3(S, s_n);
  147. // 对做逆傅里叶变换就可以得到时域的有一定带宽的噪声信号
  148. b_n = s_n.size(1);
  149. if (s_n.size(1) == 0) {
  150. xbar_im = rtNaN;
  151. } else if (s_n.size(1) == 1) {
  152. if ((!std::isinf(s_n[0].re)) && (!std::isinf(s_n[0].im)) &&
  153. ((!std::isnan(s_n[0].re)) && (!std::isnan(s_n[0].im)))) {
  154. xbar_im = 0.0;
  155. } else {
  156. xbar_im = rtNaN;
  157. }
  158. } else {
  159. int lastBlockLength;
  160. int nblocks;
  161. if (s_n.size(1) <= 1024) {
  162. firstBlockLength = s_n.size(1);
  163. lastBlockLength = 0;
  164. nblocks = 1;
  165. } else {
  166. firstBlockLength = 1024;
  167. nblocks = s_n.size(1) / 1024;
  168. lastBlockLength = s_n.size(1) - (nblocks << 10);
  169. if (lastBlockLength > 0) {
  170. nblocks++;
  171. } else {
  172. lastBlockLength = 1024;
  173. }
  174. }
  175. N_n = s_n[0].re;
  176. xbar_im = s_n[0].im;
  177. for (k = 2; k <= firstBlockLength; k++) {
  178. N_n += s_n[k - 1].re;
  179. xbar_im += s_n[k - 1].im;
  180. }
  181. for (int ib{2}; ib <= nblocks; ib++) {
  182. xblockoffset = (ib - 1) << 10;
  183. bsum_re = s_n[xblockoffset].re;
  184. bsum_im = s_n[xblockoffset].im;
  185. if (ib == nblocks) {
  186. firstBlockLength = lastBlockLength;
  187. } else {
  188. firstBlockLength = 1024;
  189. }
  190. for (k = 2; k <= firstBlockLength; k++) {
  191. bsum_re_tmp = (xblockoffset + k) - 1;
  192. bsum_re += s_n[bsum_re_tmp].re;
  193. bsum_im += s_n[bsum_re_tmp].im;
  194. }
  195. N_n += bsum_re;
  196. xbar_im += bsum_im;
  197. }
  198. if (xbar_im == 0.0) {
  199. bsum_im = N_n / static_cast<double>(s_n.size(1));
  200. N_n = 0.0;
  201. } else if (N_n == 0.0) {
  202. bsum_im = 0.0;
  203. N_n = xbar_im / static_cast<double>(s_n.size(1));
  204. } else {
  205. bsum_im = N_n / static_cast<double>(s_n.size(1));
  206. N_n = xbar_im / static_cast<double>(s_n.size(1));
  207. }
  208. absdiff.set_size(s_n.size(1));
  209. for (k = 0; k < b_n; k++) {
  210. absdiff[k] = rt_hypotd_snf(s_n[k].re - bsum_im, s_n[k].im - N_n);
  211. }
  212. xbar_im = 0.0;
  213. N_n = 3.3121686421112381E-170;
  214. firstBlockLength = s_n.size(1);
  215. for (k = 0; k < firstBlockLength; k++) {
  216. if (absdiff[k] > N_n) {
  217. bsum_re = N_n / absdiff[k];
  218. xbar_im = xbar_im * bsum_re * bsum_re + 1.0;
  219. N_n = absdiff[k];
  220. } else {
  221. bsum_re = absdiff[k] / N_n;
  222. xbar_im += bsum_re * bsum_re;
  223. }
  224. }
  225. xbar_im = N_n * std::sqrt(xbar_im);
  226. xbar_im /= std::sqrt(static_cast<double>(s_n.size(1)) - 1.0);
  227. }
  228. s_n.set_size(1, s_n.size(1));
  229. firstBlockLength = s_n.size(1) - 1;
  230. for (bsum_re_tmp = 0; bsum_re_tmp <= firstBlockLength; bsum_re_tmp++) {
  231. N_n = s_n[bsum_re_tmp].re;
  232. bsum_re = s_n[bsum_re_tmp].im;
  233. if (bsum_re == 0.0) {
  234. bsum_im = N_n / xbar_im;
  235. N_n = 0.0;
  236. } else if (N_n == 0.0) {
  237. bsum_im = 0.0;
  238. N_n = bsum_re / xbar_im;
  239. } else {
  240. bsum_im = N_n / xbar_im;
  241. N_n = bsum_re / xbar_im;
  242. }
  243. s_n[bsum_re_tmp].re = bsum_im;
  244. s_n[bsum_re_tmp].im = N_n;
  245. }
  246. // 归一化,方差为1
  247. // 产生需要调制的射频噪声干扰s_0
  248. bsum_re = f_0 * 0.0;
  249. bsum_im = f_0 * 6.2831853071795862;
  250. s.set_size(1, n.size(1));
  251. firstBlockLength = n.size(1);
  252. for (bsum_re_tmp = 0; bsum_re_tmp < firstBlockLength; bsum_re_tmp++) {
  253. s[bsum_re_tmp].re = b * (n[bsum_re_tmp] * bsum_re);
  254. s[bsum_re_tmp].im = b * (n[bsum_re_tmp] * bsum_im);
  255. }
  256. firstBlockLength = s.size(1);
  257. for (k = 0; k < firstBlockLength; k++) {
  258. if (s[k].im == 0.0) {
  259. s[k].re = std::exp(s[k].re);
  260. s[k].im = 0.0;
  261. } else if (std::isinf(s[k].im) && std::isinf(s[k].re) && (s[k].re < 0.0)) {
  262. s[k].re = 0.0;
  263. s[k].im = 0.0;
  264. } else {
  265. N_n = std::exp(s[k].re / 2.0);
  266. s[k].re = N_n * (N_n * std::cos(s[k].im));
  267. s[k].im = N_n * (N_n * std::sin(s[k].im));
  268. }
  269. }
  270. s.set_size(1, s.size(1));
  271. firstBlockLength = s.size(1) - 1;
  272. for (bsum_re_tmp = 0; bsum_re_tmp <= firstBlockLength; bsum_re_tmp++) {
  273. N_n = A * s[bsum_re_tmp].re;
  274. bsum_re = A * s[bsum_re_tmp].im;
  275. s[bsum_re_tmp].re =
  276. N_n * s_n[bsum_re_tmp].re - bsum_re * s_n[bsum_re_tmp].im;
  277. s[bsum_re_tmp].im =
  278. N_n * s_n[bsum_re_tmp].im + bsum_re * s_n[bsum_re_tmp].re;
  279. // printf("%f %f ", s[bsum_re_tmp].re, s[bsum_re_tmp].im);
  280. // printf("%f\n", s[bsum_re_tmp]);
  281. }
  282. }
  283. //int main()
  284. //{
  285. // double N=1024;
  286. // double f_s=10e6;
  287. // double A=1;
  288. // double B_n=2e6;
  289. // double f_0=2e6;
  290. // coder::array<creal_T, 2U> s;
  291. // jamming_H(N, f_s, A, B_n, f_0, s);
  292. // for (int i = 0; i < N; i++) {
  293. // std::cout << std::showpos << std::fixed << std::setprecision(6) << s[i].re << std::noshowpos << std::showpos << std::fixed << std::setprecision(6) << s[i].im << "i, ";
  294. // }
  295. // std::cout << "\n";
  296. // return 0;
  297. //}