XcorrUtils.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 = $"\"{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. public async Task<IEnumerable<DmcResult>> DmcCheckAsync(string fileName, double fsHz, DmcType dmcType)
  72. {
  73. string dmcCmd = "all";
  74. dmp = new Process();
  75. string pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\dmc.exe");
  76. string pArguments = string.Empty;
  77. switch (dmcType)
  78. {
  79. case DmcType.DAMA:
  80. dmcCmd = "dm";
  81. pArguments = $"{dmcCmd} \"{fileName}\"";// -f {fs}" -c --dmfirst";
  82. break;
  83. case DmcType.IBS:
  84. dmcCmd = "nd";
  85. pArguments = $"{dmcCmd} \"{fileName}\"";// -f {fs}" -c --dmfirst";
  86. break;
  87. case DmcType.Ky5758:
  88. pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\enc.exe");
  89. //enc.exe enc-test.dat 0.096 5 0全部文件
  90. pArguments = $"{fileName} {fsHz * 1e-6} {5} {0}";
  91. break;
  92. default:
  93. break;
  94. }
  95. return await Task.Run(() =>
  96. {
  97. dmp.StartInfo.FileName = pFileName;
  98. dmp.StartInfo.Arguments = pArguments;
  99. dmp.StartInfo.CreateNoWindow = true;
  100. dmp.StartInfo.RedirectStandardError = true;
  101. dmp.StartInfo.RedirectStandardOutput = true;
  102. dmp.StartInfo.UseShellExecute = false;
  103. dmp.Start();
  104. Stopwatch stopWatch = new Stopwatch();
  105. stopWatch.Start();
  106. dmp.WaitForExit();
  107. stopWatch.Stop();
  108. TimeSpan ts = stopWatch.Elapsed;
  109. var str = dmp.StandardOutput.ReadToEnd();
  110. return ConvertDmcResult(str, ts.TotalMilliseconds);
  111. });
  112. }
  113. private IEnumerable<DmcResult> ConvertDmcResult(string res, double tm)
  114. {
  115. var lines = res.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);
  116. foreach (var line in lines)
  117. {
  118. var items = line.Split('\t');
  119. if (items.Length < 2) continue;
  120. int start = int.Parse(items[0]);
  121. int length = int.Parse(items[1]);
  122. string userName = "";
  123. if (items.Length >= 3)
  124. userName = items[2];
  125. yield return new DmcResult(start, length, userName, (int)tm);
  126. }
  127. }
  128. public void StopDm()
  129. {
  130. try
  131. {
  132. if (dmp != null)
  133. {
  134. dmp.Kill();
  135. }
  136. }
  137. catch (Exception)
  138. {
  139. }
  140. }
  141. public void StopCalc()
  142. {
  143. try
  144. {
  145. if (cafp != null)
  146. {
  147. cafp.Kill();
  148. }
  149. }
  150. catch (Exception)
  151. {
  152. }
  153. }
  154. }
  155. }