XcorrUtils.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using XdCxRhDW.Dto;
  7. namespace CpuCgServer
  8. {
  9. public class XcorrStruct
  10. {
  11. public String file1 { get; set; }
  12. public String file2 { get; set; }
  13. public long smpStart { get; set; } //开始样点
  14. public double smpCount { get; set; } //样点数
  15. public double samplingRate { get; set; } //采样率
  16. public double dtCenter { get; set; } //时差中心
  17. public double dtRange { get; set; } //时差范围
  18. public double dfRange { get; set; } //频差范围
  19. public double snrThreshold { get; set; } //信噪比门限
  20. public int TimeoutSeconds { get; set; }
  21. public XcorrStruct Copy()
  22. {
  23. XcorrStruct xItem = new XcorrStruct();
  24. xItem.file1 = file1;
  25. xItem.file2 = file2;
  26. xItem.smpCount = smpCount;
  27. xItem.samplingRate = samplingRate;
  28. xItem.dtCenter = dtCenter;
  29. xItem.dtRange = dtRange;
  30. xItem.dfRange = dfRange;
  31. xItem.smpStart = smpStart;
  32. xItem.snrThreshold = snrThreshold;
  33. return xItem;
  34. }
  35. }
  36. public class MultiXcorrStruct
  37. {
  38. public String file1 { get; set; }
  39. public String file2 { get; set; }
  40. public List<XcorrSmp> xcorrSmps { get; set; }
  41. public double samplingRate { get; set; } //采样率
  42. public double dtCenter { get; set; } //时差中心
  43. public double dtRange { get; set; } //时差范围
  44. public double dfRange { get; set; } //频差范围
  45. public double snrThreshold { get; set; } //信噪比门限
  46. public int TimeoutSeconds { get; set; }
  47. }
  48. /// <summary>
  49. /// 时隙样点信息
  50. /// </summary>
  51. public class XcorrSmp
  52. {
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. /// <param name="smpStart"></param>
  57. /// <param name="smpCount"></param>
  58. public XcorrSmp(long smpStart, long smpCount)
  59. {
  60. this.smpStart = smpStart;
  61. this.smpCount = smpCount;
  62. }
  63. public long smpStart { get; set; }
  64. public long smpCount { get; set; }
  65. }
  66. public class XcorrUtils
  67. {
  68. Process cafp;
  69. Process mucafp;
  70. // D:/data/test/r1.dat D:/data/test/r4.dat 1562500 -250000 20 16384 100000 1048576 11
  71. public async Task<CafResultDto> Calc(XcorrStruct xs)
  72. {
  73. CafResultDto res = new CafResultDto();
  74. cafp = new Process();
  75. await Task.Run(() =>
  76. {
  77. int flag = 0;
  78. if (xs.smpStart == 0)
  79. {
  80. flag = 2;//文件参估,避免滑动时溢出
  81. cafp.StartInfo.Arguments = $"{flag} \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpCount} {xs.snrThreshold}";
  82. }
  83. else
  84. {
  85. var minStart = xs.dtRange / 2 / 1e6 * xs.samplingRate;
  86. if (xs.smpStart < minStart)//滑动计算起时值必须要大于等于此数
  87. {
  88. xs.smpStart = (long)minStart;
  89. }
  90. cafp.StartInfo.Arguments = $"{flag} \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold}";
  91. }
  92. var exePath = Path.Combine(cafp.StartInfo.WorkingDirectory, "AddIns");
  93. cafp.StartInfo.WorkingDirectory = exePath;
  94. cafp.StartInfo.FileName = Path.Combine(exePath, "XcorrCpu.exe");
  95. cafp.StartInfo.CreateNoWindow = true;
  96. cafp.StartInfo.RedirectStandardError = true;
  97. cafp.StartInfo.RedirectStandardOutput = true;
  98. cafp.StartInfo.UseShellExecute = false;
  99. cafp.Start();
  100. Stopwatch stopWatch = new Stopwatch();
  101. stopWatch.Start();
  102. var suceed = cafp.WaitForExit(xs.TimeoutSeconds * 1000);
  103. if (!suceed)
  104. {
  105. throw new Exception("CPU参估超时");
  106. }
  107. stopWatch.Stop();
  108. TimeSpan ts = stopWatch.Elapsed;
  109. var str = cafp.StandardOutput.ReadToEnd();
  110. res.FromLine(str);
  111. res.file1 = xs.file1;
  112. res.file2 = xs.file2;
  113. res.tm = (int)stopWatch.Elapsed.TotalMilliseconds;
  114. res.smpstart = xs.smpStart;
  115. res.smplen = (long)xs.smpCount;
  116. });
  117. return res;
  118. }
  119. public async Task<List<CafResultDto>> MultiCalc(MultiXcorrStruct xs)
  120. {
  121. List<CafResultDto> res = new List<CafResultDto>();
  122. mucafp = new Process();
  123. await Task.Run(() =>
  124. {
  125. int flag = 4;
  126. string smparg = $"{xs.xcorrSmps.Count}";
  127. xs.xcorrSmps.ForEach(n => smparg += $" {n.smpStart} {n.smpCount}");
  128. mucafp.StartInfo.Arguments = $"{flag} \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.snrThreshold} {smparg}";
  129. var exePath = Path.Combine(mucafp.StartInfo.WorkingDirectory, "AddIns");
  130. mucafp.StartInfo.WorkingDirectory = exePath;
  131. mucafp.StartInfo.FileName = Path.Combine(exePath, "XcorrCpu.exe");
  132. mucafp.StartInfo.CreateNoWindow = true;
  133. mucafp.StartInfo.RedirectStandardError = true;
  134. mucafp.StartInfo.RedirectStandardOutput = true;
  135. mucafp.StartInfo.UseShellExecute = false;
  136. mucafp.Start();
  137. Stopwatch stopWatch = new Stopwatch();
  138. stopWatch.Start();
  139. bool succeed = mucafp.WaitForExit(xs.TimeoutSeconds * 1000);
  140. if (!succeed)
  141. throw new Exception("CPU多时隙参估计算超时");
  142. stopWatch.Stop();
  143. TimeSpan ts = stopWatch.Elapsed;
  144. var str = mucafp.StandardOutput.ReadToEnd();
  145. var items = str.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  146. for (int i = 0; i < items.Length; i++)
  147. {
  148. var item = items[i];
  149. var cafres = item.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  150. if (cafres.Length != 3)
  151. {
  152. continue;
  153. }
  154. CafResultDto caf = new CafResultDto();
  155. caf.file1 = xs.file1;
  156. caf.file2 = xs.file2;
  157. caf.tm = (int)stopWatch.Elapsed.TotalMilliseconds;
  158. caf.smpstart = xs.xcorrSmps[i].smpStart;
  159. caf.smplen = xs.xcorrSmps[i].smpCount;
  160. double snr = Math.Round(double.Parse(cafres[2]), 1);
  161. if (snr == 0)
  162. {
  163. caf.dt = 0;
  164. caf.df = 0;
  165. caf.snr = 0;
  166. }
  167. else
  168. {
  169. caf.dt = Math.Round(double.Parse(cafres[0]), 3);
  170. caf.df = Math.Round(double.Parse(cafres[1]), 3);
  171. caf.snr = snr;
  172. }
  173. res.Add(caf);
  174. }
  175. });
  176. return res;
  177. }
  178. }
  179. }