using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace XdCxRhDW.Api
{
public static class DataEmulationHelper
{
private const string dll = @"AddIns\信号仿真\SignalEmulation.dll";
#region cpp dll Interop
[DllImport(dll, EntryPoint = "genSignalsWithDtDf", CallingConvention = CallingConvention.Cdecl)]
private extern static void GenSignalsWithDtDfCore(string file1, string file2, long fsHz, int sigLenMs, double dt, double df, int snr2, int snr1);
[DllImport(dll, EntryPoint = "genSignalsWithTwoDtDf", CallingConvention = CallingConvention.Cdecl)]
private extern static void GenSignalsWithTwoDtDf(string file1, string file2, string file3, long fsHz, int sigLenMs, double dt1, double df1, double dt2, double df2, int snr2, int snr1, int snr3);
//生成噪声,文件,采样率,时长ms,最大幅度
[DllImport(dll, EntryPoint = "genNoizes", CallingConvention = CallingConvention.Cdecl)]
private extern static void GenNoizes(string file1, long fsHz, int tlenms, int Am);
#endregion
///
/// 生成一组(ch1、ch2两个文件)常规信号文件
///
/// 输出文件1
/// 输出文件2
/// 采样率Hz
/// 信号时长(毫秒)
/// 时差秒
/// 频差Hz
/// 文件1信噪比
/// 文件2信噪比
public static void GenNormalFiles(string file1, string file2, long fsHz, int sigTimeLenMs, double dt, double df, int snr1, int snr2)
{
GenSignalsWithDtDfCore(file1, file2, fsHz, sigTimeLenMs, dt, df, snr2, snr1);
}
///
/// 生成两组(ch1、ch2、ch3三个文件)常规信号文件
///
/// 输出文件1
/// 输出文件2
/// 采样率Hz
/// 信号时长(毫秒)
/// 时差秒
/// 频差Hz
/// 文件1信噪比
/// 文件2信噪比
public static void GenNormalFiles(string file1, string file2, string file3, long fsHz, int sigTimeLenMs, double dt1, double df1, double dt2, double df2, int snr1, int snr2, int snr3)
{
GenSignalsWithTwoDtDf(file1, file2, file3, fsHz, sigTimeLenMs, dt1, df1, dt2, df2, snr2, snr1, snr3);
}
///
/// 生成单个噪声文件
///
///
///
///
///
public static void GenNoizesFile(string file1, long fsHz, int timeLenMs, int am)
{
GenNoizes(file1, fsHz, timeLenMs, am);
}
///
/// 生成一组(ch1、ch2两个文件)突发信号文件
///
/// 输出文件1
/// 输出文件2
/// 采样率Hz
/// 突发个数
/// 时差秒
/// 频差Hz
public static void GenBrustFiles(string file1, string file2, long fsHz, int brustCount, double dt, double df)
{
GenNoizes("n11.dat", fsHz, 900, 40);
GenNoizes("n12.dat", fsHz, 900, 40);
GenNormalFiles("s1.dat", "s2.dat", fsHz, 100, dt, df, 5, -5);
GenNoizes("n21.dat", fsHz, 900, 40);
GenNoizes("n22.dat", fsHz, 900, 40);
var n11 = File.ReadAllBytes("n11.dat");
var n12 = File.ReadAllBytes("n12.dat");
var s1 = File.ReadAllBytes("s1.dat");
var s2 = File.ReadAllBytes("s2.dat");
var n21 = File.ReadAllBytes("n21.dat");
var n22 = File.ReadAllBytes("n22.dat");
using (FileStream fs = new FileStream(file1, FileMode.Create))
{
for (int i = 0; i < brustCount; i++)
{
fs.Write(n11, 0, n11.Length);
fs.Write(s1, 0, s1.Length);
fs.Write(n21, 0, n21.Length);
}
}
using (FileStream fs = new FileStream(file2, FileMode.Create))
{
for (int i = 0; i < brustCount; i++)
{
fs.Write(n12, 0, n12.Length);
fs.Write(s2, 0, s2.Length);
fs.Write(n22, 0, n22.Length);
}
}
File.Delete("n11.dat");
File.Delete("n12.dat");
File.Delete("s1.dat");
File.Delete("s2.dat");
File.Delete("n21.dat");
File.Delete("n22.dat");
}
///
/// 生成两组(ch1、ch2、ch3三个文件)突发信号文件
///
/// 输出文件1
/// 输出文件2
/// 采样率Hz
/// 突发个数
/// 时差秒
/// 频差Hz
public static void GenBrustFiles(string file1, string file2, string file3, long fsHz, int brustCount, double dt1, double df1, double dt2, double df2)
{
GenNoizes("n11.dat", fsHz, 900, 40);
GenNoizes("n12.dat", fsHz, 900, 40);
GenNoizes("n13.dat", fsHz, 900, 40);
GenNormalFiles("s1.dat", "s2.dat","s3.dat", fsHz, 100, dt1, df1, dt2, df2, 5, -5, 5);
GenNoizes("n21.dat", fsHz, 900, 40);
GenNoizes("n22.dat", fsHz, 900, 40);
GenNoizes("n23.dat", fsHz, 900, 40);
var n11 = File.ReadAllBytes("n11.dat");
var n12 = File.ReadAllBytes("n12.dat");
var n13 = File.ReadAllBytes("n13.dat");
var s1 = File.ReadAllBytes("s1.dat");
var s2 = File.ReadAllBytes("s2.dat");
var s3 = File.ReadAllBytes("s2.dat");
var n21 = File.ReadAllBytes("n21.dat");
var n22 = File.ReadAllBytes("n22.dat");
var n23 = File.ReadAllBytes("n23.dat");
using (FileStream fs = new FileStream(file1, FileMode.Create))
{
for (int i = 0; i < brustCount; i++)
{
fs.Write(n11, 0, n11.Length);
fs.Write(s1, 0, s1.Length);
fs.Write(n21, 0, n21.Length);
}
}
using (FileStream fs = new FileStream(file2, FileMode.Create))
{
for (int i = 0; i < brustCount; i++)
{
fs.Write(n12, 0, n12.Length);
fs.Write(s2, 0, s2.Length);
fs.Write(n22, 0, n22.Length);
}
}
using (FileStream fs = new FileStream(file3, FileMode.Create))
{
for (int i = 0; i < brustCount; i++)
{
fs.Write(n13, 0, n13.Length);
fs.Write(s3, 0, s3.Length);
fs.Write(n23, 0, n23.Length);
}
}
File.Delete("n11.dat");
File.Delete("n12.dat");
File.Delete("n13.dat");
File.Delete("s1.dat");
File.Delete("s2.dat");
File.Delete("s3.dat");
File.Delete("n21.dat");
File.Delete("n22.dat");
File.Delete("n23.dat");
}
}
}