fun9_tdoa1.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //
  2. // File: tdoa1.cpp
  3. //
  4. // MATLAB Coder version : 5.2
  5. // C/C++ source code generated on : 24-Mar-2023 11:40:08
  6. //
  7. // Include Files
  8. #include "fun9_tdoa1.h"
  9. #include "fun9_pinv.h"
  10. #include "rt_nonfinite.h"
  11. #include "coder_array.h"
  12. #include <cmath>
  13. #include <iostream>
  14. // Function Definitions
  15. //
  16. // TDOA Implementation
  17. // 输入:Pos :站的坐标
  18. // T :站测得的到达时间
  19. // M :总站数
  20. // 输出:targetPos :定位目标坐标
  21. // parameters
  22. //
  23. // Arguments : const coder::array<double, 2U> &Pos
  24. // const coder::array<double, 2U> &T
  25. // double M
  26. // double targetPos[3]
  27. // Return Type : void
  28. //
  29. static coder::array<double, 2U> argInit_Pos()
  30. {
  31. coder::array<double, 2U> result;
  32. // Set the size of the array.
  33. // Change this size to the value that the application requires.
  34. result.set_size(3, 2);
  35. // Loop over the array to initialize each element.
  36. for (int idx0{0}; idx0 < result.size(0); idx0++) {
  37. for (int idx1{0}; idx1 < result.size(1); idx1++) {
  38. // Set the value of the array element.
  39. // Change this value to the value that the application requires.
  40. if (idx0 == 0 && idx1 == 0) {
  41. result[idx0 + result.size(0) * idx1] = (1.0e+06)*3.8304;
  42. } else if (idx0 == 0 && idx1 == 1) {
  43. result[idx0 + result.size(0) * idx1] = (1.0e+06)*3.7824;
  44. } else if (idx0 == 1 && idx1 == 0) {
  45. result[idx0 + result.size(0) * idx1] = (1.0e+06)*5.0721;
  46. } else if (idx0 == 1 && idx1 == 1) {
  47. result[idx0 + result.size(0) * idx1] = (1.0e+06)*5.0957;
  48. } else if (idx0 == 2 && idx1 == 0) {
  49. result[idx0 + result.size(0) * idx1] = (1.0e+06)*2.0493;
  50. } else if (idx0 == 2 && idx1 == 1) {
  51. result[idx0 + result.size(0) * idx1] = (1.0e+06)*2.0795;
  52. }
  53. }
  54. }
  55. return result;
  56. }
  57. static coder::array<double, 2U> argInit_T()
  58. {
  59. coder::array<double, 2U> result;
  60. // Set the size of the array.
  61. // Change this size to the value that the application requires.
  62. result.set_size(1, 2);
  63. // Loop over the array to initialize each element.
  64. for (int idx0{0}; idx0 < result.size(0); idx0++) {
  65. for (int idx1{0}; idx1 < result.size(1); idx1++) {
  66. // Set the value of the array element.
  67. // Change this value to the value that the application requires.
  68. if (idx0 == 0 && idx1 == 0) {
  69. result[idx0 + result.size(0) * idx1] = (1.0e-03)*0;
  70. } else if (idx0 == 0 && idx1 == 1) {
  71. result[idx0 + result.size(0) * idx1] = (1.0e-03)*-0.1496;
  72. }
  73. }
  74. }
  75. return result;
  76. }
  77. void tdoa1(const coder::array<double, 2U> &Pos,
  78. const coder::array<double, 2U> &T, double M, double targetPos[3])
  79. {
  80. coder::array<double, 2U> A;
  81. coder::array<double, 2U> dummy;
  82. coder::array<double, 2U> r;
  83. coder::array<double, 2U> tdoa;
  84. coder::array<double, 2U> y;
  85. coder::array<double, 1U> b;
  86. coder::array<double, 1U> bsum;
  87. coder::array<double, 1U> varargin_3;
  88. double b_Pos;
  89. int k;
  90. int nblocks;
  91. int ncolx;
  92. int nrowx;
  93. int nxin;
  94. int nxout;
  95. signed char b_input_sizes_idx_1;
  96. signed char c_input_sizes_idx_1;
  97. signed char input_sizes_idx_1;
  98. boolean_T empty_non_axis_sizes;
  99. // the speed of light
  100. // number of sensors
  101. // TDOA method flag, 0 for linear 1 for Taylor
  102. // sensor positon vectors
  103. // includes all sensor positon vectors
  104. // includes all toa information
  105. tdoa.set_size(T.size(0), T.size(1));
  106. ncolx = T.size(0) * T.size(1);
  107. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  108. tdoa[nblocks] = T[nblocks] - T[0];
  109. }
  110. nxin = tdoa.size(0) * tdoa.size(1) - 2;
  111. nrowx = tdoa.size(0);
  112. ncolx = tdoa.size(1);
  113. nxout = tdoa.size(0) * tdoa.size(1) - 1;
  114. for (k = 0; k < nxout; k++) {
  115. tdoa[k] = tdoa[k + 1];
  116. }
  117. if ((nrowx != 1) && (ncolx == 1)) {
  118. if (1 > nxout) {
  119. ncolx = 0;
  120. } else {
  121. ncolx = nxin + 1;
  122. }
  123. bsum.set_size(ncolx);
  124. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  125. bsum[nblocks] = tdoa[nblocks];
  126. }
  127. tdoa.set_size(ncolx, 1);
  128. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  129. tdoa[nblocks] = bsum[nblocks];
  130. }
  131. } else {
  132. if (1 > nxout) {
  133. ncolx = -1;
  134. } else {
  135. ncolx = nxin;
  136. }
  137. for (nblocks = 0; nblocks <= ncolx; nblocks++) {
  138. tdoa[nblocks] = tdoa[nblocks];
  139. }
  140. tdoa.set_size(1, ncolx + 1);
  141. }
  142. // tdoa为时差
  143. // %% Linear Solution
  144. if (2.0 > M) {
  145. nblocks = 0;
  146. nxout = 0;
  147. } else {
  148. nblocks = 1;
  149. nxout = static_cast<int>(M);
  150. }
  151. ncolx = Pos.size(0);
  152. nxin = nxout - nblocks;
  153. dummy.set_size(nxin, Pos.size(0));
  154. for (nxout = 0; nxout < ncolx; nxout++) {
  155. for (nrowx = 0; nrowx < nxin; nrowx++) {
  156. dummy[nrowx + dummy.size(0) * nxout] =
  157. Pos[nxout + Pos.size(0) * (nblocks + nrowx)];
  158. }
  159. }
  160. y.set_size(tdoa.size(0), tdoa.size(1));
  161. ncolx = tdoa.size(0) * tdoa.size(1);
  162. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  163. y[nblocks] = -3.0E+8 * tdoa[nblocks];
  164. }
  165. b_Pos = Pos[0];
  166. ncolx = dummy.size(0);
  167. bsum.set_size(dummy.size(0));
  168. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  169. bsum[nblocks] = b_Pos - dummy[nblocks];
  170. }
  171. b_Pos = Pos[1];
  172. ncolx = dummy.size(0);
  173. b.set_size(dummy.size(0));
  174. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  175. b[nblocks] = b_Pos - dummy[nblocks + dummy.size(0)];
  176. }
  177. b_Pos = Pos[2];
  178. ncolx = dummy.size(0);
  179. varargin_3.set_size(dummy.size(0));
  180. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  181. varargin_3[nblocks] = b_Pos - dummy[nblocks + dummy.size(0) * 2];
  182. }
  183. if (bsum.size(0) != 0) {
  184. nxin = bsum.size(0);
  185. } else if (b.size(0) != 0) {
  186. nxin = b.size(0);
  187. } else if (varargin_3.size(0) != 0) {
  188. nxin = varargin_3.size(0);
  189. } else if ((y.size(0) != 0) && (y.size(1) != 0)) {
  190. nxin = y.size(0);
  191. } else {
  192. nxin = 0;
  193. if (y.size(0) > 0) {
  194. nxin = y.size(0);
  195. }
  196. }
  197. empty_non_axis_sizes = (nxin == 0);
  198. if (empty_non_axis_sizes || (bsum.size(0) != 0)) {
  199. input_sizes_idx_1 = 1;
  200. } else {
  201. input_sizes_idx_1 = 0;
  202. }
  203. if (empty_non_axis_sizes || (b.size(0) != 0)) {
  204. b_input_sizes_idx_1 = 1;
  205. } else {
  206. b_input_sizes_idx_1 = 0;
  207. }
  208. if (empty_non_axis_sizes || (varargin_3.size(0) != 0)) {
  209. c_input_sizes_idx_1 = 1;
  210. } else {
  211. c_input_sizes_idx_1 = 0;
  212. }
  213. if (empty_non_axis_sizes || ((y.size(0) != 0) && (y.size(1) != 0))) {
  214. nrowx = y.size(1);
  215. } else {
  216. nrowx = 0;
  217. }
  218. A.set_size(nxin,
  219. ((input_sizes_idx_1 + b_input_sizes_idx_1) + c_input_sizes_idx_1) +
  220. nrowx);
  221. ncolx = input_sizes_idx_1;
  222. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  223. for (nxout = 0; nxout < nxin; nxout++) {
  224. A[nxout] = 2.0 * bsum[nxout];
  225. }
  226. }
  227. ncolx = b_input_sizes_idx_1;
  228. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  229. for (nxout = 0; nxout < nxin; nxout++) {
  230. A[nxout + A.size(0) * input_sizes_idx_1] = 2.0 * b[nxout];
  231. }
  232. }
  233. ncolx = c_input_sizes_idx_1;
  234. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  235. for (nxout = 0; nxout < nxin; nxout++) {
  236. A[nxout + A.size(0) * (input_sizes_idx_1 + b_input_sizes_idx_1)] =
  237. 2.0 * varargin_3[nxout];
  238. }
  239. }
  240. for (nblocks = 0; nblocks < nrowx; nblocks++) {
  241. for (nxout = 0; nxout < nxin; nxout++) {
  242. A[nxout +
  243. A.size(0) * (((nblocks + input_sizes_idx_1) + b_input_sizes_idx_1) +
  244. c_input_sizes_idx_1)] = 2.0 * y[nxout + nxin * nblocks];
  245. }
  246. }
  247. ncolx = tdoa.size(0) * tdoa.size(1);
  248. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  249. tdoa[nblocks] = 3.0E+8 * tdoa[nblocks];
  250. }
  251. r.set_size(tdoa.size(0), tdoa.size(1));
  252. nxin = tdoa.size(0) * tdoa.size(1);
  253. for (k = 0; k < nxin; k++) {
  254. r[k] = tdoa[k] * tdoa[k];
  255. }
  256. if (Pos.size(0) == 0) {
  257. b_Pos = 0.0;
  258. } else {
  259. b_Pos = 0.0;
  260. if (Pos.size(0) == 1) {
  261. b_Pos = std::abs(Pos[0]);
  262. } else {
  263. double scale;
  264. scale = 3.3121686421112381E-170;
  265. nblocks = Pos.size(0) - 1;
  266. for (k = 0; k <= nblocks; k++) {
  267. double absxk;
  268. absxk = std::abs(Pos[k]);
  269. if (absxk > scale) {
  270. double t;
  271. t = scale / absxk;
  272. b_Pos = b_Pos * t * t + 1.0;
  273. scale = absxk;
  274. } else {
  275. double t;
  276. t = absxk / scale;
  277. b_Pos += t * t;
  278. }
  279. }
  280. b_Pos = scale * std::sqrt(b_Pos);
  281. }
  282. }
  283. b_Pos *= b_Pos;
  284. y.set_size(dummy.size(0), dummy.size(1));
  285. nxin = dummy.size(0) * dummy.size(1);
  286. for (k = 0; k < nxin; k++) {
  287. y[k] = dummy[k] * dummy[k];
  288. }
  289. if ((y.size(0) == 0) || (y.size(1) == 0)) {
  290. b.set_size(y.size(0));
  291. ncolx = y.size(0);
  292. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  293. b[nblocks] = 0.0;
  294. }
  295. } else {
  296. int lastBlockLength;
  297. int xj;
  298. int xoffset;
  299. ncolx = y.size(0) - 1;
  300. nxout = y.size(0) << 10;
  301. b.set_size(y.size(0));
  302. bsum.set_size(y.size(0));
  303. if (y.size(1) <= 1024) {
  304. nxin = y.size(1);
  305. lastBlockLength = 0;
  306. nblocks = 1;
  307. } else {
  308. nxin = 1024;
  309. nblocks = y.size(1) / 1024;
  310. lastBlockLength = y.size(1) - (nblocks << 10);
  311. if (lastBlockLength > 0) {
  312. nblocks++;
  313. } else {
  314. lastBlockLength = 1024;
  315. }
  316. }
  317. for (xj = 0; xj <= ncolx; xj++) {
  318. b[xj] = y[xj];
  319. bsum[xj] = 0.0;
  320. }
  321. for (k = 2; k <= nxin; k++) {
  322. xoffset = (k - 1) * (ncolx + 1);
  323. for (xj = 0; xj <= ncolx; xj++) {
  324. b[xj] = b[xj] + y[xoffset + xj];
  325. }
  326. }
  327. for (int ib{2}; ib <= nblocks; ib++) {
  328. nxin = (ib - 1) * nxout;
  329. for (xj = 0; xj <= ncolx; xj++) {
  330. bsum[xj] = y[nxin + xj];
  331. }
  332. if (ib == nblocks) {
  333. nrowx = lastBlockLength;
  334. } else {
  335. nrowx = 1024;
  336. }
  337. for (k = 2; k <= nrowx; k++) {
  338. xoffset = nxin + (k - 1) * (ncolx + 1);
  339. for (xj = 0; xj <= ncolx; xj++) {
  340. bsum[xj] = bsum[xj] + y[xoffset + xj];
  341. }
  342. }
  343. for (xj = 0; xj <= ncolx; xj++) {
  344. b[xj] = b[xj] + bsum[xj];
  345. }
  346. }
  347. }
  348. ncolx = r.size(0);
  349. b.set_size(r.size(0));
  350. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  351. b[nblocks] = (r[nblocks] + b_Pos) - b[nblocks];
  352. }
  353. if (A.size(0) < A.size(1)) {
  354. tdoa.set_size(A.size(1), A.size(0));
  355. ncolx = A.size(0);
  356. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  357. nxin = A.size(1);
  358. for (nxout = 0; nxout < nxin; nxout++) {
  359. tdoa[nxout + tdoa.size(0) * nblocks] = A[nblocks + A.size(0) * nxout];
  360. }
  361. }
  362. coder::eml_pinv(tdoa, r);
  363. tdoa.set_size(r.size(1), r.size(0));
  364. ncolx = r.size(0);
  365. for (nblocks = 0; nblocks < ncolx; nblocks++) {
  366. nxin = r.size(1);
  367. for (nxout = 0; nxout < nxin; nxout++) {
  368. tdoa[nxout + tdoa.size(0) * nblocks] = r[nblocks + r.size(0) * nxout];
  369. }
  370. }
  371. } else {
  372. coder::eml_pinv(A, tdoa);
  373. }
  374. nxin = tdoa.size(0) - 1;
  375. nrowx = tdoa.size(1);
  376. bsum.set_size(tdoa.size(0));
  377. for (nxout = 0; nxout <= nxin; nxout++) {
  378. bsum[nxout] = 0.0;
  379. }
  380. for (k = 0; k < nrowx; k++) {
  381. ncolx = k * tdoa.size(0);
  382. for (nxout = 0; nxout <= nxin; nxout++) {
  383. bsum[nxout] = bsum[nxout] + tdoa[ncolx + nxout] * b[k];
  384. }
  385. }
  386. targetPos[0] = bsum[0];
  387. targetPos[1] = bsum[1];
  388. targetPos[2] = bsum[2];
  389. }
  390. //int main()
  391. //{
  392. // //输入参数
  393. // coder::array<double, 2U> Pos;
  394. // coder::array<double, 2U> T;
  395. // double M;
  396. // //输出参数
  397. // double targetPos[3];
  398. // //给Pos赋初始值
  399. // Pos = argInit_Pos();
  400. // //给T赋初始值
  401. // T = argInit_T();
  402. // //给M赋初始值
  403. // M = 2;
  404. // tdoa1(Pos, T, M, targetPos);
  405. // for (int i = 0; i < 3; ++i) {
  406. // std::cout << targetPos[i] << " ";
  407. // }
  408. //}