XcorrUtils.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using XdCxRhDw.Dto;
  10. namespace XdCxRhDW.Core.Api
  11. {
  12. public class XcorrStruct
  13. {
  14. public String file1 { get; set; }
  15. public String file2 { get; set; }
  16. public long smpStart { get; set; } //开始样点
  17. public long smpCount { get; set; } //样点数
  18. public double samplingRate { get; set; } //采样率
  19. public double dtCenter { get; set; } //时差中心
  20. public double dtRange { get; set; } //时差范围
  21. public double dfRange { get; set; } //频差范围
  22. public double snrThreshold { get; set; } //信噪比门限
  23. public XcorrStruct Copy()
  24. {
  25. XcorrStruct xItem = new XcorrStruct();
  26. xItem.file1 = file1;
  27. xItem.file2 = file2;
  28. xItem.smpCount = smpCount;
  29. xItem.samplingRate = samplingRate;
  30. xItem.dtCenter = dtCenter;
  31. xItem.dtRange = dtRange;
  32. xItem.dfRange = dfRange;
  33. xItem.smpStart = smpStart;
  34. xItem.snrThreshold = snrThreshold;
  35. return xItem;
  36. }
  37. }
  38. public class XcorrUtils
  39. {
  40. Process cafp;
  41. Process dmp;
  42. // D:/data/test/r1.dat D:/data/test/r4.dat 1562500 -250000 20 16384 100000 1048576 11
  43. public async Task<CafResult> Calc(XcorrStruct xs)
  44. {
  45. CafResult res = new CafResult();
  46. cafp = new Process();
  47. await Task.Run(() =>
  48. {
  49. int flag = 0;
  50. if (xs.smpStart == 0)
  51. {
  52. flag = 2;//文件参估,避免滑动时溢出
  53. cafp.StartInfo.Arguments = $"{flag} \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpCount} {xs.snrThreshold}";
  54. }
  55. else
  56. {
  57. var minStart = xs.dtRange / 2 / 1e6 * xs.samplingRate;
  58. if (xs.smpStart < minStart)//滑动计算起时值必须要大于等于此数
  59. {
  60. xs.smpStart = (long)minStart;
  61. }
  62. cafp.StartInfo.Arguments = $"{flag} \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold}";
  63. }
  64. cafp.StartInfo.FileName = Path.Combine(cafp.StartInfo.WorkingDirectory, "xcorr\\XcorrCpu.exe");
  65. cafp.StartInfo.CreateNoWindow = true;
  66. cafp.StartInfo.RedirectStandardError = true;
  67. cafp.StartInfo.RedirectStandardOutput = true;
  68. cafp.StartInfo.UseShellExecute = false;
  69. cafp.Start();
  70. Stopwatch stopWatch = new Stopwatch();
  71. stopWatch.Start();
  72. cafp.WaitForExit();
  73. stopWatch.Stop();
  74. TimeSpan ts = stopWatch.Elapsed;
  75. var str = cafp.StandardOutput.ReadToEnd();
  76. res.FromLine(str);
  77. res.file1 = xs.file1;
  78. res.file2 = xs.file2;
  79. res.tm = (int)stopWatch.Elapsed.TotalMilliseconds;
  80. res.smpstart = xs.smpStart;
  81. res.smplen = xs.smpCount;
  82. });
  83. return res;
  84. }
  85. /// <summary>
  86. /// 参估画图
  87. /// </summary>
  88. /// <returns></returns>
  89. public async Task<List<ImageResult>> DrawImage(XcorrStruct xs)
  90. {
  91. cafp = new Process();
  92. List<ImageResult> list = new List<ImageResult>();
  93. await Task.Run(() =>
  94. {
  95. string outFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CgImag.txt");
  96. cafp.StartInfo.FileName = Path.Combine(cafp.StartInfo.WorkingDirectory, "xcorr\\XcorrCpu.exe");
  97. cafp.StartInfo.Arguments = $"1 \"{xs.file1}\" \"{xs.file2}\" {(Int64)(xs.samplingRate)} {xs.dtCenter} {xs.dtRange} {xs.dfRange} {xs.smpStart} {xs.smpCount} {xs.snrThreshold} {outFile}";
  98. cafp.StartInfo.CreateNoWindow = true;
  99. cafp.StartInfo.RedirectStandardError = true;
  100. cafp.StartInfo.RedirectStandardOutput = true;
  101. cafp.StartInfo.UseShellExecute = false;
  102. cafp.Start();
  103. Stopwatch stopWatch = new Stopwatch();
  104. stopWatch.Start();
  105. cafp.WaitForExit();
  106. stopWatch.Stop();
  107. TimeSpan ts = stopWatch.Elapsed;
  108. var str = cafp.StandardOutput.ReadToEnd();
  109. if (str.StartsWith("1:") && File.Exists(outFile))
  110. {
  111. var lines = File.ReadAllLines(outFile);
  112. if (lines.Length > 1)
  113. {
  114. var lineFirstArr = lines[0].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  115. int fsHz = Convert.ToInt32(lineFirstArr[0]);
  116. int xFlag = Convert.ToInt32(lineFirstArr[1]);
  117. int xMax = Convert.ToInt32(lineFirstArr[2]);
  118. float yMax = Convert.ToSingle(lineFirstArr[3]);
  119. for (int i = 1; i < lines.Length; i++)
  120. {
  121. var lineArr = lines[i].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  122. ImageResult ir = new ImageResult()
  123. {
  124. FsHz = fsHz,
  125. XFlag = xFlag,
  126. XMax = xMax,
  127. YMax = yMax,
  128. XValue = Convert.ToInt32(lineArr[0]),
  129. YValue = Convert.ToSingle(lineArr[1]),
  130. Snr = Convert.ToSingle(lineArr[2]),
  131. };
  132. list.Add(ir);
  133. }
  134. }
  135. File.Delete(outFile);
  136. }
  137. });
  138. return list;
  139. }
  140. public async Task<IEnumerable<DmcResult>> DmcCheckAsync(string fileName, double fsHz, DmcType dmcType, double? bandKHz = null)
  141. {
  142. if (bandKHz == null || bandKHz.Value <= 0) bandKHz = 25;
  143. string dmcCmd = "all";
  144. dmp = new Process();
  145. string pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\dmc.exe");
  146. string pArguments = string.Empty;
  147. switch (dmcType)
  148. {
  149. case DmcType.DAMA:
  150. dmcCmd = "dm";
  151. if (fsHz != 96000)
  152. throw new Exception("DAMA只支持96K采样率");
  153. pArguments = $"{dmcCmd} \"{fileName}\" -c true";//-c包含ccow
  154. break;
  155. case DmcType.IBS:
  156. dmcCmd = "nd";
  157. pArguments = $"{dmcCmd} \"{fileName}\" -w {bandKHz}";// -f {fs}" -c --dmfirst";
  158. break;
  159. case DmcType.Ky5758:
  160. pFileName = Path.Combine(dmp.StartInfo.WorkingDirectory, "xcorr\\enc.exe");
  161. //enc.exe enc-test.dat 0.096 5 0全部文件
  162. pArguments = $"{fileName} {fsHz * 1e-6} {5} {0}";
  163. break;
  164. default:
  165. break;
  166. }
  167. return await Task.Run(() =>
  168. {
  169. dmp.StartInfo.FileName = pFileName;
  170. dmp.StartInfo.Arguments = pArguments;
  171. dmp.StartInfo.CreateNoWindow = true;
  172. dmp.StartInfo.RedirectStandardError = true;
  173. dmp.StartInfo.RedirectStandardOutput = true;
  174. dmp.StartInfo.UseShellExecute = false;
  175. dmp.Start();
  176. Stopwatch stopWatch = new Stopwatch();
  177. stopWatch.Start();
  178. dmp.WaitForExit();
  179. stopWatch.Stop();
  180. TimeSpan ts = stopWatch.Elapsed;
  181. var str = dmp.StandardOutput.ReadToEnd();
  182. return ConvertDmcResult(dmcType, str, ts.TotalMilliseconds);
  183. });
  184. }
  185. private IEnumerable<DmcResult> ConvertDmcResult(DmcType type, string res, double tm)
  186. {
  187. var lines = res.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);
  188. foreach (var line in lines)
  189. {
  190. var items = line.Split('\t');
  191. if (items.Length < 2) continue;
  192. int start = int.Parse(items[0]);
  193. int length = int.Parse(items[1]);
  194. string userName = "";
  195. if (items.Length >= 3)
  196. userName = items[2];
  197. var item = new DmcResult(start, length, userName, (int)tm);
  198. if (type == DmcType.DAMA)
  199. item.ModType = "BPSK";
  200. else
  201. item.ModType = string.Empty;
  202. yield return item;
  203. }
  204. }
  205. public void StopDm()
  206. {
  207. try
  208. {
  209. if (dmp != null)
  210. {
  211. dmp.Kill();
  212. }
  213. }
  214. catch (Exception)
  215. {
  216. }
  217. }
  218. public void StopCalc()
  219. {
  220. try
  221. {
  222. if (cafp != null)
  223. {
  224. cafp.Kill();
  225. }
  226. }
  227. catch (Exception)
  228. {
  229. }
  230. }
  231. }
  232. }