XcorrUtils.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 long smpStart { get; set; } //开始样点
  17. public long 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. int flag = 0;
  50. if (xs.smpStart == 0)
  51. {
  52. flag = 2;//文件参估,避免滑动时溢出
  53. }
  54. else
  55. {
  56. var minStart = xs.dtRange / 2 / 1e6 * xs.samplingRate;
  57. if (xs.smpStart < minStart)//滑动计算起时值必须要大于等于此数
  58. {
  59. xs.smpStart = (long)minStart;
  60. }
  61. }
  62. cafp.StartInfo.FileName = Path.Combine(cafp.StartInfo.WorkingDirectory, "xcorr\\XcorrCpu.exe");
  63. cafp.StartInfo.Arguments = $"{flag} \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold}";
  64. cafp.StartInfo.CreateNoWindow = true;
  65. cafp.StartInfo.RedirectStandardError = true;
  66. cafp.StartInfo.RedirectStandardOutput = true;
  67. cafp.StartInfo.UseShellExecute = false;
  68. cafp.Start();
  69. Stopwatch stopWatch = new Stopwatch();
  70. stopWatch.Start();
  71. cafp.WaitForExit();
  72. stopWatch.Stop();
  73. TimeSpan ts = stopWatch.Elapsed;
  74. var str = cafp.StandardOutput.ReadToEnd();
  75. res.FromLine(str);
  76. res.file1 = xs.file1;
  77. res.file2 = xs.file2;
  78. res.tm = (int)stopWatch.Elapsed.TotalMilliseconds;
  79. res.smpstart = xs.smpStart;
  80. res.smplen = xs.smpCount;
  81. });
  82. return res;
  83. }
  84. /// <summary>
  85. /// 参估画图
  86. /// </summary>
  87. /// <returns></returns>
  88. public async Task<List<ImageResult>> DrawImage(XcorrStruct xs)
  89. {
  90. cafp = new Process();
  91. List<ImageResult> list = new List<ImageResult>();
  92. await Task.Run(() =>
  93. {
  94. string outFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CgImag.txt");
  95. cafp.StartInfo.FileName = Path.Combine(cafp.StartInfo.WorkingDirectory, "xcorr\\XcorrCpu.exe");
  96. cafp.StartInfo.Arguments = $"1 \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold} {outFile}";
  97. cafp.StartInfo.CreateNoWindow = true;
  98. cafp.StartInfo.RedirectStandardError = true;
  99. cafp.StartInfo.RedirectStandardOutput = true;
  100. cafp.StartInfo.UseShellExecute = false;
  101. cafp.Start();
  102. Stopwatch stopWatch = new Stopwatch();
  103. stopWatch.Start();
  104. cafp.WaitForExit();
  105. stopWatch.Stop();
  106. TimeSpan ts = stopWatch.Elapsed;
  107. var str = cafp.StandardOutput.ReadToEnd();
  108. if (str.StartsWith("1:") && File.Exists(outFile))
  109. {
  110. var lines = File.ReadAllLines(outFile);
  111. if (lines.Length > 1)
  112. {
  113. var lineFirstArr = lines[0].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  114. int fsHz = Convert.ToInt32(lineFirstArr[0]);
  115. int xFlag = Convert.ToInt32(lineFirstArr[1]);
  116. int xMax = Convert.ToInt32(lineFirstArr[2]);
  117. float yMax = Convert.ToSingle(lineFirstArr[3]);
  118. for (int i = 1; i < lines.Length; i++)
  119. {
  120. var lineArr = lines[i].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  121. ImageResult ir = new ImageResult()
  122. {
  123. FsHz = fsHz,
  124. XFlag = xFlag,
  125. XMax = xMax,
  126. YMax = yMax,
  127. XValue = Convert.ToInt32(lineArr[0]),
  128. YValue = Convert.ToSingle(lineArr[1]),
  129. Snr = Convert.ToSingle(lineArr[2]),
  130. };
  131. list.Add(ir);
  132. }
  133. }
  134. File.Delete(outFile);
  135. }
  136. });
  137. return list;
  138. }
  139. public async Task<IEnumerable<DmcResult>> DmcCheckAsync(string fileName, double fsHz, DmcType dmcType)
  140. {
  141. string dmcCmd = "all";
  142. dmp = new Process();
  143. string pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\dmc.exe");
  144. string pArguments = string.Empty;
  145. switch (dmcType)
  146. {
  147. case DmcType.DAMA:
  148. dmcCmd = "dm";
  149. if (fsHz != 96000)
  150. throw new Exception("DAMA只支持96K采样率");
  151. pArguments = $"{dmcCmd} \"{fileName}\" -c true";//-c包含ccow
  152. break;
  153. case DmcType.IBS:
  154. dmcCmd = "nd";
  155. pArguments = $"{dmcCmd} \"{fileName}\"";// -f {fs}" -c --dmfirst";
  156. break;
  157. case DmcType.Ky5758:
  158. pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\enc.exe");
  159. //enc.exe enc-test.dat 0.096 5 0全部文件
  160. pArguments = $"{fileName} {fsHz * 1e-6} {5} {0}";
  161. break;
  162. default:
  163. break;
  164. }
  165. return await Task.Run(() =>
  166. {
  167. dmp.StartInfo.FileName = pFileName;
  168. dmp.StartInfo.Arguments = pArguments;
  169. dmp.StartInfo.CreateNoWindow = true;
  170. dmp.StartInfo.RedirectStandardError = true;
  171. dmp.StartInfo.RedirectStandardOutput = true;
  172. dmp.StartInfo.UseShellExecute = false;
  173. dmp.Start();
  174. Stopwatch stopWatch = new Stopwatch();
  175. stopWatch.Start();
  176. dmp.WaitForExit();
  177. stopWatch.Stop();
  178. TimeSpan ts = stopWatch.Elapsed;
  179. var str = dmp.StandardOutput.ReadToEnd();
  180. return ConvertDmcResult(dmcType, str, ts.TotalMilliseconds);
  181. });
  182. }
  183. private IEnumerable<DmcResult> ConvertDmcResult(DmcType type, string res, double tm)
  184. {
  185. var lines = res.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);
  186. foreach (var line in lines)
  187. {
  188. var items = line.Split('\t');
  189. if (items.Length < 2) continue;
  190. int start = int.Parse(items[0]);
  191. int length = int.Parse(items[1]);
  192. string userName = "";
  193. if (items.Length >= 3)
  194. userName = items[2];
  195. var item = new DmcResult(start, length, userName, (int)tm);
  196. if (type == DmcType.DAMA)
  197. item.SigType = "BPSK";
  198. else
  199. item.SigType = string.Empty;
  200. yield return item;
  201. }
  202. }
  203. public void StopDm()
  204. {
  205. try
  206. {
  207. if (dmp != null)
  208. {
  209. dmp.Kill();
  210. }
  211. }
  212. catch (Exception)
  213. {
  214. }
  215. }
  216. public void StopCalc()
  217. {
  218. try
  219. {
  220. if (cafp != null)
  221. {
  222. cafp.Kill();
  223. }
  224. }
  225. catch (Exception)
  226. {
  227. }
  228. }
  229. }
  230. }