XcorrUtils.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using XdCxRhDw.Dto;
  10. namespace XdCxRhDW.Core.Api
  11. {
  12. public class XcorrStruct
  13. {
  14. public String file1 { get; set; }
  15. public String file2 { get; set; }
  16. public int smpStart { get; set; } //开始样点
  17. public int smpCount { get; set; } //样点数
  18. public double samplingRate { get; set; } //采样率
  19. public double dtCenter { get; set; } //时差中心
  20. public double dtRange { get; set; } //时差范围
  21. public double dfRange { get; set; } //频差范围
  22. public double snrThreshold { get; set; } //信噪比门限
  23. public XcorrStruct Copy()
  24. {
  25. XcorrStruct xItem = new XcorrStruct();
  26. xItem.file1 = file1;
  27. xItem.file2 = file2;
  28. xItem.smpCount = smpCount;
  29. xItem.samplingRate = samplingRate;
  30. xItem.dtCenter = dtCenter;
  31. xItem.dtRange = dtRange;
  32. xItem.dfRange = dfRange;
  33. xItem.smpStart = smpStart;
  34. xItem.snrThreshold = snrThreshold;
  35. return xItem;
  36. }
  37. }
  38. public class XcorrUtils
  39. {
  40. Process cafp;
  41. Process dmp;
  42. // D:/data/test/r1.dat D:/data/test/r4.dat 1562500 -250000 20 16384 100000 1048576 11
  43. public async Task<CafResult> Calc(XcorrStruct xs)
  44. {
  45. CafResult res = new CafResult();
  46. cafp = new Process();
  47. await Task.Run(() =>
  48. {
  49. cafp.StartInfo.FileName = Path.Combine(cafp.StartInfo.WorkingDirectory, "xcorr\\XcorrCpu.exe");
  50. cafp.StartInfo.Arguments = $"0 \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold}";
  51. cafp.StartInfo.CreateNoWindow = true;
  52. cafp.StartInfo.RedirectStandardError = true;
  53. cafp.StartInfo.RedirectStandardOutput = true;
  54. cafp.StartInfo.UseShellExecute = false;
  55. cafp.Start();
  56. Stopwatch stopWatch = new Stopwatch();
  57. stopWatch.Start();
  58. cafp.WaitForExit();
  59. stopWatch.Stop();
  60. TimeSpan ts = stopWatch.Elapsed;
  61. var str = cafp.StandardOutput.ReadToEnd();
  62. res.FromLine(str);
  63. res.file1 = xs.file1;
  64. res.file2 = xs.file2;
  65. res.tm = (int)stopWatch.Elapsed.TotalMilliseconds;
  66. res.smpstart = xs.smpStart;
  67. res.smplen = xs.smpCount;
  68. });
  69. return res;
  70. }
  71. /// <summary>
  72. /// 参估画图
  73. /// </summary>
  74. /// <returns></returns>
  75. public async Task<List<ImageResult>> DrawImage(XcorrStruct xs)
  76. {
  77. cafp = new Process();
  78. List<ImageResult> list = new List<ImageResult>();
  79. await Task.Run(() =>
  80. {
  81. string outFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CgImag.txt");
  82. cafp.StartInfo.FileName = Path.Combine(cafp.StartInfo.WorkingDirectory, "xcorr\\XcorrCpu.exe");
  83. cafp.StartInfo.Arguments = $"1 \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold} {outFile}";
  84. cafp.StartInfo.CreateNoWindow = true;
  85. cafp.StartInfo.RedirectStandardError = true;
  86. cafp.StartInfo.RedirectStandardOutput = true;
  87. cafp.StartInfo.UseShellExecute = false;
  88. cafp.Start();
  89. Stopwatch stopWatch = new Stopwatch();
  90. stopWatch.Start();
  91. cafp.WaitForExit();
  92. stopWatch.Stop();
  93. TimeSpan ts = stopWatch.Elapsed;
  94. var str = cafp.StandardOutput.ReadToEnd();
  95. if (str.StartsWith("1:") && File.Exists(outFile))
  96. {
  97. var lines = File.ReadAllLines(outFile);
  98. if (lines.Length > 1)
  99. {
  100. var lineFirstArr = lines[0].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  101. int fsHz = Convert.ToInt32(lineFirstArr[0]);
  102. int xFlag = Convert.ToInt32(lineFirstArr[1]);
  103. int xMax = Convert.ToInt32(lineFirstArr[2]);
  104. float yMax = Convert.ToSingle(lineFirstArr[3]);
  105. for (int i = 1; i < lines.Length; i++)
  106. {
  107. var lineArr = lines[i].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  108. ImageResult ir = new ImageResult()
  109. {
  110. FsHz = fsHz,
  111. XFlag = xFlag,
  112. XMax = xMax,
  113. YMax = yMax,
  114. XValue = Convert.ToInt32(lineArr[0]),
  115. YValue = Convert.ToSingle(lineArr[1]),
  116. Snr = Convert.ToSingle(lineArr[2]),
  117. };
  118. list.Add(ir);
  119. }
  120. }
  121. File.Delete(outFile);
  122. }
  123. });
  124. return list;
  125. }
  126. public async Task<IEnumerable<DmcResult>> DmcCheckAsync(string fileName, double fsHz, DmcType dmcType)
  127. {
  128. string dmcCmd = "all";
  129. dmp = new Process();
  130. string pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\dmc.exe");
  131. string pArguments = string.Empty;
  132. switch (dmcType)
  133. {
  134. case DmcType.DAMA:
  135. dmcCmd = "dm";
  136. pArguments = $"{dmcCmd} \"{fileName}\"";// -f {fs}" -c --dmfirst";
  137. break;
  138. case DmcType.IBS:
  139. dmcCmd = "nd";
  140. pArguments = $"{dmcCmd} \"{fileName}\"";// -f {fs}" -c --dmfirst";
  141. break;
  142. case DmcType.Ky5758:
  143. pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\enc.exe");
  144. //enc.exe enc-test.dat 0.096 5 0全部文件
  145. pArguments = $"{fileName} {fsHz * 1e-6} {5} {0}";
  146. break;
  147. default:
  148. break;
  149. }
  150. return await Task.Run(() =>
  151. {
  152. dmp.StartInfo.FileName = pFileName;
  153. dmp.StartInfo.Arguments = pArguments;
  154. dmp.StartInfo.CreateNoWindow = true;
  155. dmp.StartInfo.RedirectStandardError = true;
  156. dmp.StartInfo.RedirectStandardOutput = true;
  157. dmp.StartInfo.UseShellExecute = false;
  158. dmp.Start();
  159. Stopwatch stopWatch = new Stopwatch();
  160. stopWatch.Start();
  161. dmp.WaitForExit();
  162. stopWatch.Stop();
  163. TimeSpan ts = stopWatch.Elapsed;
  164. var str = dmp.StandardOutput.ReadToEnd();
  165. return ConvertDmcResult(dmcType,str, ts.TotalMilliseconds);
  166. });
  167. }
  168. private IEnumerable<DmcResult> ConvertDmcResult(DmcType type,string res, double tm)
  169. {
  170. var lines = res.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);
  171. foreach (var line in lines)
  172. {
  173. var items = line.Split('\t');
  174. if (items.Length < 2) continue;
  175. int start = int.Parse(items[0]);
  176. int length = int.Parse(items[1]);
  177. string userName = "";
  178. if (items.Length >= 3)
  179. userName = items[2];
  180. var item= new DmcResult(start, length, userName, (int)tm);
  181. if (type == DmcType.DAMA)
  182. item.SigType = "BPSK";
  183. else
  184. item.SigType = string.Empty;
  185. yield return item;
  186. }
  187. }
  188. public void StopDm()
  189. {
  190. try
  191. {
  192. if (dmp != null)
  193. {
  194. dmp.Kill();
  195. }
  196. }
  197. catch (Exception)
  198. {
  199. }
  200. }
  201. public void StopCalc()
  202. {
  203. try
  204. {
  205. if (cafp != null)
  206. {
  207. cafp.Kill();
  208. }
  209. }
  210. catch (Exception)
  211. {
  212. }
  213. }
  214. }
  215. }