TpdxUtil.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using CliWrap;
  2. using CliWrap.Buffered;
  3. using Ips.Library.Entity;
  4. using System.Diagnostics;
  5. namespace Ips.Tpdx
  6. {
  7. public class TpdxUtil
  8. {
  9. static readonly string CliFile = Path.Combine(IpsPath.CliRootDir, "tpdx", "defruiter.exe");
  10. public static async Task<string> ExecAsync(string fileIn, string outDir, DxSigItem item, bool useGpu = false, CancellationToken token = default)
  11. {
  12. string ofile = Path.Combine(outDir, Path.GetFileNameWithoutExtension(fileIn) + "_tpdx.dat");
  13. await Task.Run(() =>
  14. {
  15. //Int64 fsample = 6250000;
  16. //double bandwidth = 2000000; //Hz
  17. //double fc = 0; //Hz 干扰-信号
  18. //int Mod = (int)SignalMod._BPSK;
  19. double ps = -1;
  20. int order = 20;
  21. int proCount = 1;
  22. dxfun(proCount, order, fileIn, ofile, item.Fs, item.Bandwidth, item.Fc, ps, item.Mod);
  23. });
  24. return ofile;
  25. }
  26. /// <summary>
  27. ///
  28. /// </summary>
  29. /// <param name="proCount">进程数</param>
  30. /// <param name="order">单次处理样点数 默认 20==1M</param>
  31. /// <param name="ifile">输入文件</param>
  32. /// <param name="ofile">输出文件</param>
  33. /// <param name="fsample">采样率</param>
  34. /// <param name="bandwidth">带宽 Hz</param>
  35. /// <param name="fc">频偏移 Hz</param>
  36. /// <param name="mod">模式</param>
  37. private static void dxfun(int proCount, int order, String ifile, String ofile, Int64 fsample, double bandwidth, double fc, double ps, int mod)
  38. {
  39. int len = 1 << order;
  40. int firlen = 1 << (order - 5);
  41. FileInfo fi = new FileInfo(ifile);
  42. Int64 total = fi.Length / 4; // 总样点数
  43. int oversmpl = 40000; // 进程之间重复的点数
  44. int minSeg = 5;
  45. //可分小段的总数
  46. int totalSeg = (int)Math.Ceiling((total - len) * 1.0 / (len - firlen)) + 1;
  47. int maxProcCount = (int)(totalSeg / minSeg);
  48. if (proCount > maxProcCount) proCount = maxProcCount;
  49. int realSeg = (int)Math.Ceiling(totalSeg * 1.0 / proCount);
  50. int oneSmpCount = len;
  51. if (realSeg > 1)
  52. {
  53. oneSmpCount += ((realSeg - 1) * (len - firlen));
  54. }
  55. int woneSmpCount = oneSmpCount;
  56. Int64 pos = 0;
  57. List<Process> lps = new List<Process>();
  58. for (int idx = 0; idx < proCount; ++idx)
  59. {
  60. if (idx > 0)
  61. {
  62. pos = idx * (oneSmpCount - oversmpl);
  63. }
  64. if (idx == proCount - 1)
  65. {
  66. oneSmpCount = (int)(total - pos);
  67. }
  68. lps.Add(proc(ifile, ofile, fsample, fc, bandwidth, ps, mod, order, pos, oneSmpCount));
  69. }
  70. pos = 0;
  71. using (BinaryWriter binwriter = new BinaryWriter(File.Open(ofile, FileMode.Create)))
  72. {
  73. for (int idx = 0; idx < proCount; ++idx)
  74. {
  75. if (idx > 0)
  76. {
  77. pos = idx * (woneSmpCount - oversmpl);
  78. }
  79. if (idx == proCount - 1)
  80. {
  81. woneSmpCount = (int)(total - pos);
  82. }
  83. var tfile = $@"{ofile}_{pos}_{woneSmpCount}.~def";
  84. lps[idx].WaitForExit();
  85. using (BinaryReader binReader = new BinaryReader(File.OpenRead(tfile)))
  86. {
  87. if (idx != 0)
  88. {
  89. binReader.BaseStream.Seek(oversmpl * 4, SeekOrigin.Begin);
  90. }
  91. while (true)
  92. {
  93. var buff = binReader.ReadBytes(1024 * 1024 * 4);
  94. if (buff == null || buff.Length <= 0)
  95. {
  96. break;
  97. }
  98. binwriter.Write(buff, 0, buff.Length);
  99. }
  100. }
  101. File.Delete(tfile);
  102. Console.WriteLine($"{tfile} finish");
  103. }
  104. }
  105. Console.WriteLine($"finish");
  106. }
  107. /// <summary>
  108. ///
  109. /// </summary>
  110. /// <param name="ifile">输入文件</param>
  111. /// <param name="ofile">输出文件</param>
  112. /// <param name="fs">采样率</param>
  113. /// <param name="fc">频偏移</param>
  114. /// <param name="bw">带宽</param>
  115. /// <param name="mod">模式</param>
  116. /// <param name="order">单次处理样点数 默认 20==1M</param>
  117. /// <param name="pos">开始位置</param>
  118. /// <param name="len">长度</param>
  119. /// <returns></returns>
  120. private static Process proc(String ifile, String ofile, Int64 fs, double fc, double bw, double ps, int mod, int order, Int64 pos, Int64 len)
  121. {
  122. Process pro = new Process();
  123. pro.StartInfo.CreateNoWindow = true;
  124. pro.StartInfo.UseShellExecute = false;
  125. pro.StartInfo.WorkingDirectory = Path.GetDirectoryName(CliFile);
  126. pro.StartInfo.FileName = CliFile;
  127. pro.StartInfo.Arguments = $"\"{ifile}\" \"{ofile}\" {fs} {fc} {bw} {ps} {mod} {order} {pos} {len}";
  128. Console.WriteLine(pro.StartInfo.Arguments);
  129. pro.Start();
  130. return pro;
  131. }
  132. }
  133. }