GpuCgHelper.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using XdCxRhDW.Dto;
  9. namespace GpuCgServer
  10. {
  11. /// <summary>
  12. /// GPU参数估计帮助类
  13. /// </summary>
  14. public static class GpuCgHelper
  15. {
  16. private static string exePath = "AddIns";
  17. private const string exeName = "XcorrGpu.exe";
  18. /// <summary>
  19. /// 设置【ReSample.exe】文件所在路径,支持相对路径
  20. /// </summary>
  21. public static void SetExePath(string path)
  22. {
  23. if (string.IsNullOrWhiteSpace(path)) return;
  24. if (path.StartsWith("\\"))//相对路径要么开头不带\,要么是 .\
  25. path = path.Remove(0, 1);
  26. exePath = path;
  27. }
  28. /// <summary>
  29. /// GPU参估计算
  30. /// </summary>
  31. /// <param name="file1">输入文件1</param>
  32. /// <param name="file2">输入文件2</param>
  33. /// <param name="fsHz">采样率Hz</param>
  34. /// <param name="smpCount">样点数,0-1之间的小数表示样点百分比</param>
  35. /// <param name="dtoCenterus">时差中心(us)</param>
  36. /// <param name="dtoRangeus">时差范围(us)</param>
  37. /// <param name="dfoRange">频差范围(Hz)</param>
  38. /// <param name="snr0">信噪比门限</param>
  39. /// <param name="timeoutSeconds">超时时间(s)</param>
  40. /// <returns></returns>
  41. public static List<GpuCgResponseDto> Calc(string file1, string file2, double fsHz, double smpCount,
  42. double dtoCenterus, double dtoRangeus, double dfoRange, double snr0, int timeoutSeconds = 120)
  43. {
  44. if (timeoutSeconds <= 0) timeoutSeconds = 120;
  45. FileInfo f1 = new FileInfo(file1);
  46. FileInfo f2 = new FileInfo(file2);
  47. long len = f1.Length <= f2.Length ? f1.Length / 4 : f2.Length / 4;
  48. if (smpCount <= 0 || smpCount > len)
  49. smpCount = len;
  50. if (smpCount > 0 && smpCount < 1)
  51. {
  52. smpCount = (long)(len * smpCount);
  53. }
  54. if (string.IsNullOrWhiteSpace(exePath))
  55. throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径");
  56. if (!Directory.Exists(exePath))
  57. throw new Exception($"路径[{exePath}]不存在");
  58. var exeFile = Path.Combine(exePath, exeName);
  59. if (!File.Exists(exeFile))
  60. throw new Exception($"文件[{exeFile}]不存在");
  61. Process p = new Process();
  62. p.StartInfo.WorkingDirectory = exePath;
  63. p.StartInfo.FileName = exeFile;
  64. p.StartInfo.Arguments = $"-m \"{file1}\" -a \"{file2}\" -s {smpCount} -f {fsHz} -c {dtoCenterus} -r {dtoRangeus} -j {dfoRange} -t {snr0} -p {0}";
  65. p.StartInfo.CreateNoWindow = true;
  66. p.StartInfo.RedirectStandardError = true;
  67. p.StartInfo.RedirectStandardOutput = true;
  68. p.StartInfo.UseShellExecute = false;
  69. Stopwatch sw = new Stopwatch();
  70. sw.Start();
  71. p.Start();
  72. var succeed = p.WaitForExit(timeoutSeconds * 1000);
  73. if (!succeed)
  74. {
  75. throw new Exception($"进程[{exeName}]超时未完成!");
  76. }
  77. var res = p.StandardOutput.ReadToEnd();
  78. sw.Stop();
  79. if (sw.ElapsedMilliseconds < 800)
  80. {
  81. throw new Exception("没有GPU");
  82. }
  83. List<GpuCgResponseDto> list = new List<GpuCgResponseDto>();
  84. //3:-0.000*-0.00054.553+72.917*0.000*37.756+-72.917*0.000*37.671+
  85. if (res.Length < 3)
  86. {
  87. list.Add(new GpuCgResponseDto()
  88. {
  89. Smplen=(long)smpCount,
  90. TimeMs=(int)sw.ElapsedMilliseconds,
  91. });
  92. return list;
  93. }
  94. else
  95. {
  96. var xgfArr = res.Substring(2).Split("+".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  97. foreach (var item in xgfArr)
  98. {
  99. var dtdfArr = item.Split("*".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  100. double.TryParse(dtdfArr[0], out double dt);
  101. double.TryParse(dtdfArr[1], out double df);
  102. double.TryParse(dtdfArr[2], out double snr);
  103. dt = Math.Round(dt, 4);
  104. df = Math.Round(df, 3);
  105. snr = Math.Round(snr, 1);
  106. GpuCgResponseDto dto = new GpuCgResponseDto()
  107. {
  108. Dt = dt,
  109. Df = df,
  110. Smplen = (long)smpCount,
  111. Snr = snr,
  112. TimeMs = (int)sw.ElapsedMilliseconds
  113. };
  114. list.Add(dto);
  115. }
  116. return list;
  117. }
  118. }
  119. }
  120. }