GpuCgHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /// <returns></returns>
  40. public static List<GpuCgResponseDto> Calc(string file1, string file2, double fsHz, double smpCount,
  41. double dtoCenterus, double dtoRangeus, double dfoRange, double snr0)
  42. {
  43. FileInfo f1 = new FileInfo(file1);
  44. FileInfo f2 = new FileInfo(file2);
  45. long len = f1.Length <= f2.Length ? f1.Length / 4 : f2.Length / 4;
  46. if (smpCount <= 0 || smpCount > len)
  47. smpCount = len;
  48. if (smpCount > 0 && smpCount < 1)
  49. {
  50. smpCount = (long)(len * smpCount);
  51. }
  52. if (string.IsNullOrWhiteSpace(exePath))
  53. throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径");
  54. if (!Directory.Exists(exePath))
  55. throw new Exception($"路径[{exePath}]不存在");
  56. var exeFile = Path.Combine(exePath, exeName);
  57. if (!File.Exists(exeFile))
  58. throw new Exception($"文件[{exeFile}]不存在");
  59. Process p = new Process();
  60. p.StartInfo.WorkingDirectory = exePath;
  61. p.StartInfo.FileName = exeFile;
  62. p.StartInfo.Arguments = $"-m \"{file1}\" -a \"{file2}\" -s {smpCount} -f {fsHz} -c {dtoCenterus} -r {dtoRangeus} -j {dfoRange} -t {snr0} -p {0}";
  63. p.StartInfo.CreateNoWindow = true;
  64. p.StartInfo.RedirectStandardError = true;
  65. p.StartInfo.RedirectStandardOutput = true;
  66. p.StartInfo.UseShellExecute = false;
  67. Stopwatch sw = new Stopwatch();
  68. sw.Start();
  69. p.Start();
  70. var succeed = p.WaitForExit(120 * 1000);
  71. if (!succeed)
  72. {
  73. throw new Exception($"进程[{exeName}]超时(120秒)未完成!");
  74. }
  75. var res = p.StandardOutput.ReadToEnd();
  76. sw.Stop();
  77. if (sw.ElapsedMilliseconds < 800)
  78. {
  79. throw new Exception("没有GPU");
  80. }
  81. List<GpuCgResponseDto> list = new List<GpuCgResponseDto>();
  82. //3:-0.000*-0.00054.553+72.917*0.000*37.756+-72.917*0.000*37.671+
  83. if (res.Length < 3)
  84. {
  85. list.Add(new GpuCgResponseDto()
  86. {
  87. Smplen=(long)smpCount,
  88. TimeMs=(int)sw.ElapsedMilliseconds,
  89. });
  90. return list;
  91. }
  92. else
  93. {
  94. var xgfArr = res.Substring(2).Split("+".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  95. foreach (var item in xgfArr)
  96. {
  97. var dtdfArr = item.Split("*".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  98. double.TryParse(dtdfArr[0], out double dt);
  99. double.TryParse(dtdfArr[1], out double df);
  100. double.TryParse(dtdfArr[2], out double snr);
  101. dt = Math.Round(dt, 4);
  102. df = Math.Round(df, 3);
  103. snr = Math.Round(snr, 1);
  104. GpuCgResponseDto dto = new GpuCgResponseDto()
  105. {
  106. Dt = dt,
  107. Df = df,
  108. Smplen = (long)smpCount,
  109. Snr = snr,
  110. TimeMs = (int)sw.ElapsedMilliseconds
  111. };
  112. list.Add(dto);
  113. }
  114. return list;
  115. }
  116. }
  117. }
  118. }