XcorrUtils.cs 7.4 KB

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