XcorrUtils.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /// <summary>
  66. /// 54专用,避免样点转时间出现毫秒级误差
  67. /// </summary>
  68. public float TimeSeconds { get; set; }
  69. }
  70. public class XcorrUtils
  71. {
  72. Process cafp;
  73. Process mucafp;
  74. // D:/data/test/r1.dat D:/data/test/r4.dat 1562500 -250000 20 16384 100000 1048576 11
  75. public async Task<CafResultDto> Calc(XcorrStruct xs)
  76. {
  77. CafResultDto res = new CafResultDto();
  78. cafp = new Process();
  79. await Task.Run(() =>
  80. {
  81. int flag = 0;
  82. if (xs.smpStart == 0)
  83. {
  84. flag = 2;//文件参估,避免滑动时溢出
  85. cafp.StartInfo.Arguments = $"{flag} \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpCount} {xs.snrThreshold}";
  86. }
  87. else
  88. {
  89. var minStart = xs.dtRange / 2 / 1e6 * xs.samplingRate;
  90. if (xs.smpStart < minStart)//滑动计算起时值必须要大于等于此数
  91. {
  92. xs.smpStart = (long)minStart;
  93. }
  94. cafp.StartInfo.Arguments = $"{flag} \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold}";
  95. }
  96. var exePath = Path.Combine(cafp.StartInfo.WorkingDirectory, "AddIns");
  97. cafp.StartInfo.WorkingDirectory = exePath;
  98. cafp.StartInfo.FileName = Path.Combine(exePath, "XcorrCpu.exe");
  99. cafp.StartInfo.CreateNoWindow = true;
  100. cafp.StartInfo.RedirectStandardError = true;
  101. cafp.StartInfo.RedirectStandardOutput = true;
  102. cafp.StartInfo.UseShellExecute = false;
  103. cafp.Start();
  104. Stopwatch stopWatch = new Stopwatch();
  105. stopWatch.Start();
  106. var suceed = cafp.WaitForExit(xs.TimeoutSeconds * 1000);
  107. if (!suceed)
  108. {
  109. throw new Exception("CPU参估超时");
  110. }
  111. stopWatch.Stop();
  112. TimeSpan ts = stopWatch.Elapsed;
  113. var str = cafp.StandardOutput.ReadToEnd();
  114. res.FromLine(str);
  115. res.file1 = xs.file1;
  116. res.file2 = xs.file2;
  117. res.tm = (int)stopWatch.Elapsed.TotalMilliseconds;
  118. res.smpstart = xs.smpStart;
  119. res.smplen = (long)xs.smpCount;
  120. });
  121. return res;
  122. }
  123. public async Task<List<CafResultDto>> MultiCalc(MultiXcorrStruct xs)
  124. {
  125. List<CafResultDto> res = new List<CafResultDto>();
  126. mucafp = new Process();
  127. await Task.Run(() =>
  128. {
  129. int flag = 4;
  130. string smparg = $"{xs.xcorrSmps.Count}";
  131. xs.xcorrSmps.ForEach(n => smparg += $" {n.smpStart} {n.smpCount}");
  132. mucafp.StartInfo.Arguments = $"{flag} \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.snrThreshold} {smparg}";
  133. var exePath = Path.Combine(mucafp.StartInfo.WorkingDirectory, "AddIns");
  134. mucafp.StartInfo.WorkingDirectory = exePath;
  135. mucafp.StartInfo.FileName = Path.Combine(exePath, "XcorrCpu.exe");
  136. mucafp.StartInfo.CreateNoWindow = true;
  137. mucafp.StartInfo.RedirectStandardError = true;
  138. mucafp.StartInfo.RedirectStandardOutput = true;
  139. mucafp.StartInfo.UseShellExecute = false;
  140. mucafp.Start();
  141. Stopwatch stopWatch = new Stopwatch();
  142. stopWatch.Start();
  143. bool succeed = mucafp.WaitForExit(xs.TimeoutSeconds * 1000);
  144. if (!succeed)
  145. throw new Exception("CPU多时隙参估计算超时");
  146. stopWatch.Stop();
  147. TimeSpan ts = stopWatch.Elapsed;
  148. var str = mucafp.StandardOutput.ReadToEnd();
  149. var items = str.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  150. for (int i = 0; i < items.Length; i++)
  151. {
  152. var item = items[i];
  153. var cafres = item.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  154. if (cafres.Length != 3)
  155. {
  156. continue;
  157. }
  158. CafResultDto caf = new CafResultDto();
  159. caf.file1 = xs.file1;
  160. caf.file2 = xs.file2;
  161. caf.tm = (int)stopWatch.Elapsed.TotalMilliseconds;
  162. caf.smpstart = xs.xcorrSmps[i].smpStart;
  163. caf.smplen = xs.xcorrSmps[i].smpCount;
  164. double snr = Math.Round(double.Parse(cafres[2]), 1);
  165. if (snr == 0)
  166. {
  167. caf.dt = 0;
  168. caf.df = 0;
  169. caf.snr = 0;
  170. }
  171. else
  172. {
  173. caf.dt = Math.Round(double.Parse(cafres[0]), 3);
  174. caf.df = Math.Round(double.Parse(cafres[1]), 3);
  175. caf.snr = snr;
  176. }
  177. res.Add(caf);
  178. }
  179. });
  180. return res;
  181. }
  182. }
  183. }