| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 | using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using XdCxRhDW.Dto;namespace GpuCgServer{    /// <summary>    /// GPU参数估计帮助类    /// </summary>    public static class GpuCgHelper    {        private static string exePath = "AddIns";        private const string exeName = "XcorrGpu.exe";        /// <summary>        /// 设置【ReSample.exe】文件所在路径,支持相对路径        /// </summary>        public static void SetExePath(string path)        {            if (string.IsNullOrWhiteSpace(path)) return;            if (path.StartsWith("\\"))//相对路径要么开头不带\,要么是 .\                path = path.Remove(0, 1);            exePath = path;        }        /// <summary>        /// GPU参估计算        /// </summary>        /// <param name="file1">输入文件1</param>        /// <param name="file2">输入文件2</param>        /// <param name="fsHz">采样率Hz</param>        /// <param name="smpCount">样点数,0-1之间的小数表示样点百分比</param>        /// <param name="dtoCenterus">时差中心(us)</param>        /// <param name="dtoRangeus">时差范围(us)</param>        /// <param name="dfoRange">频差范围(Hz)</param>        /// <param name="snr0">信噪比门限</param>        /// <param name="timeoutSeconds">超时时间(s)</param>        /// <returns></returns>        public static List<GpuCgResponseDto> Calc(string file1, string file2, double fsHz, double smpCount,            double dtoCenterus, double dtoRangeus, double dfoRange, double snr0, int timeoutSeconds = 120)        {            if (timeoutSeconds <= 0) timeoutSeconds = 120;            FileInfo f1 = new FileInfo(file1);            FileInfo f2 = new FileInfo(file2);            long len = f1.Length <= f2.Length ? f1.Length / 4 : f2.Length / 4;            if (smpCount <= 0 || smpCount > len)                smpCount = len;            if (smpCount > 0 && smpCount < 1)            {                smpCount = (long)(len * smpCount);            }            if (string.IsNullOrWhiteSpace(exePath))                throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径");            if (!Directory.Exists(exePath))                throw new Exception($"路径[{exePath}]不存在");            var exeFile = Path.Combine(exePath, exeName);            if (!File.Exists(exeFile))                throw new Exception($"文件[{exeFile}]不存在");                        Process p = new Process();            p.StartInfo.WorkingDirectory = exePath;            p.StartInfo.FileName = exeFile;            p.StartInfo.Arguments = $"-m \"{file1}\" -a \"{file2}\" -s {smpCount} -f {fsHz} -c {dtoCenterus} -r {dtoRangeus} -j {dfoRange} -t {snr0} -p {0}";            p.StartInfo.CreateNoWindow = true;            p.StartInfo.RedirectStandardError = true;            p.StartInfo.RedirectStandardOutput = true;            p.StartInfo.UseShellExecute = false;            Stopwatch sw = new Stopwatch();            sw.Start();            p.Start();            var succeed = p.WaitForExit(timeoutSeconds * 1000);            if (!succeed)            {                throw new Exception($"进程[{exeName}]超时未完成!");            }            var res = p.StandardOutput.ReadToEnd();            sw.Stop();            if (sw.ElapsedMilliseconds < 800)            {                throw new Exception("没有GPU");            }            List<GpuCgResponseDto> list = new List<GpuCgResponseDto>();            //3:-0.000*-0.00054.553+72.917*0.000*37.756+-72.917*0.000*37.671+            if (res.Length<3)                return list;            else            {                var xgfArr = res.Substring(2).Split("+".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);                foreach (var item in xgfArr)                {                    var dtdfArr = item.Split("*".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);                    double.TryParse(dtdfArr[0], out double dt);                    double.TryParse(dtdfArr[1], out double df);                    double.TryParse(dtdfArr[2], out double snr);                    dt = Math.Round(dt, 4);                    df = Math.Round(df, 3);                    snr = Math.Round(snr, 1);                    GpuCgResponseDto dto = new GpuCgResponseDto()                    {                        Dt = dt,                        Df = df,                        Smplen = (long)smpCount,                        Snr = snr,                        TimeMs = (int)sw.ElapsedMilliseconds                    };                    list.Add(dto);                }                return list;            }        }    }}
 |