123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using CliWrap;
- using CliWrap.Buffered;
- using Ips.Library.Entity;
- using System.Diagnostics;
- namespace Ips.Tpdx
- {
- public class TpdxUtil
- {
- static readonly string CliFile = Path.Combine(IpsPath.CliRootDir, "tpdx", "defruiter.exe");
- public static async Task<string> ExecAsync(string fileIn, string outDir, DxSigItem item, bool useGpu = false, CancellationToken token = default)
- {
- string ofile = Path.Combine(outDir, Path.GetFileNameWithoutExtension(fileIn) + "_tpdx.dat");
- await Task.Run(() =>
- {
- //Int64 fsample = 6250000;
- //double bandwidth = 2000000; //Hz
- //double fc = 0; //Hz 干扰-信号
- //int Mod = (int)SignalMod._BPSK;
- double ps = -1;
- int order = 20;
- int proCount = 1;
- dxfun(proCount, order, fileIn, ofile, item.Fs, item.Bandwidth, item.Fc, ps, item.Mod);
- });
- return ofile;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="proCount">进程数</param>
- /// <param name="order">单次处理样点数 默认 20==1M</param>
- /// <param name="ifile">输入文件</param>
- /// <param name="ofile">输出文件</param>
- /// <param name="fsample">采样率</param>
- /// <param name="bandwidth">带宽 Hz</param>
- /// <param name="fc">频偏移 Hz</param>
- /// <param name="mod">模式</param>
- private static void dxfun(int proCount, int order, String ifile, String ofile, Int64 fsample, double bandwidth, double fc, double ps, int mod)
- {
- int len = 1 << order;
- int firlen = 1 << (order - 5);
- FileInfo fi = new FileInfo(ifile);
- Int64 total = fi.Length / 4; // 总样点数
- int oversmpl = 40000; // 进程之间重复的点数
- int minSeg = 5;
- //可分小段的总数
- int totalSeg = (int)Math.Ceiling((total - len) * 1.0 / (len - firlen)) + 1;
- int maxProcCount = (int)(totalSeg / minSeg);
- if (proCount > maxProcCount) proCount = maxProcCount;
- int realSeg = (int)Math.Ceiling(totalSeg * 1.0 / proCount);
- int oneSmpCount = len;
- if (realSeg > 1)
- {
- oneSmpCount += ((realSeg - 1) * (len - firlen));
- }
- int woneSmpCount = oneSmpCount;
- Int64 pos = 0;
- List<Process> lps = new List<Process>();
- for (int idx = 0; idx < proCount; ++idx)
- {
- if (idx > 0)
- {
- pos = idx * (oneSmpCount - oversmpl);
- }
- if (idx == proCount - 1)
- {
- oneSmpCount = (int)(total - pos);
- }
- lps.Add(proc(ifile, ofile, fsample, fc, bandwidth, ps, mod, order, pos, oneSmpCount));
- }
- pos = 0;
- using (BinaryWriter binwriter = new BinaryWriter(File.Open(ofile, FileMode.Create)))
- {
- for (int idx = 0; idx < proCount; ++idx)
- {
- if (idx > 0)
- {
- pos = idx * (woneSmpCount - oversmpl);
- }
- if (idx == proCount - 1)
- {
- woneSmpCount = (int)(total - pos);
- }
- var tfile = $@"{ofile}_{pos}_{woneSmpCount}.~def";
- lps[idx].WaitForExit();
- using (BinaryReader binReader = new BinaryReader(File.OpenRead(tfile)))
- {
- if (idx != 0)
- {
- binReader.BaseStream.Seek(oversmpl * 4, SeekOrigin.Begin);
- }
- while (true)
- {
- var buff = binReader.ReadBytes(1024 * 1024 * 4);
- if (buff == null || buff.Length <= 0)
- {
- break;
- }
- binwriter.Write(buff, 0, buff.Length);
- }
- }
- File.Delete(tfile);
- Console.WriteLine($"{tfile} finish");
- }
- }
- Console.WriteLine($"finish");
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="ifile">输入文件</param>
- /// <param name="ofile">输出文件</param>
- /// <param name="fs">采样率</param>
- /// <param name="fc">频偏移</param>
- /// <param name="bw">带宽</param>
- /// <param name="mod">模式</param>
- /// <param name="order">单次处理样点数 默认 20==1M</param>
- /// <param name="pos">开始位置</param>
- /// <param name="len">长度</param>
- /// <returns></returns>
- private static Process proc(String ifile, String ofile, Int64 fs, double fc, double bw, double ps, int mod, int order, Int64 pos, Int64 len)
- {
- Process pro = new Process();
- pro.StartInfo.CreateNoWindow = true;
- pro.StartInfo.UseShellExecute = false;
- pro.StartInfo.WorkingDirectory = Path.GetDirectoryName(CliFile);
- pro.StartInfo.FileName = CliFile;
- pro.StartInfo.Arguments = $"\"{ifile}\" \"{ofile}\" {fs} {fc} {bw} {ps} {mod} {order} {pos} {len}";
- Console.WriteLine(pro.StartInfo.Arguments);
- pro.Start();
- return pro;
- }
- }
- }
|