XcorrUtils.cs 7.7 KB

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