Skip to content
Snippets Groups Projects
Commit 4a50c82b authored by Carl Philipp Klemm's avatar Carl Philipp Klemm
Browse files

inital commit

parents
No related branches found
No related tags found
No related merge requests found
<Project>
<PropertyGroup>
<AssemblyName>cstest</AssemblyName>
<OutputPath>bin\</OutputPath>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Compile Include="program.cs"/>
<Compile Include="testclass.cs"/>
</ItemGroup>
<Target Name="Build">
<MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
<Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
</Target>
<Target Name="Clean">
<Delete Files="$(OutputPath)$(AssemblyName).exe"/>
</Target>
</Project>
using System;
public class Program
{
private static void PrintSpectra(Kiss.Spectra spectra)
{
for(int i = 0; i < spectra.Real.Length; ++i)
Console.WriteLine("{0:F}+{1:F}i", spectra.Real[i], spectra.Imaginary[i]);
}
public static void Result(float[] result, ref Kiss.Network network, ref object userData)
{
}
public static int Main(string[] args)
{
Kiss.VersionFixed version = Kiss.VersionFixed.GetVersion();
Console.WriteLine("libkissinference version: {0:D}.{1:D}.{2:D}", version.Major, version.Minor, version.Patch);
var spectra = new Kiss.Spectra(Kiss.Utils.CreateRange(1, 10, 10, true), Kiss.Utils.CreateRange(1, 10, 10, true), Kiss.Utils.CreateRange(1, 10, 10, true));
Console.WriteLine("A spectra:");
PrintSpectra(spectra);
spectra.Resample(5);
Console.WriteLine("Resampled spectra:");
PrintSpectra(spectra);
spectra.Normalize();
Console.WriteLine("Normalized spectra:");
PrintSpectra(spectra);
if(args.Length < 1)
{
Console.WriteLine("Unable to test network inference as no network file name was provided");
return 1;
}
var filename = args[0];
Console.WriteLine("Loading: {0:S}", filename);
try
{
var net = new Kiss.Network(filename, Result, true);
}
catch (System.IO.FileLoadException e)
{
Console.WriteLine(e.Message);
}
return 0;
}
}
using System;
using System.Runtime.InteropServices;
namespace Kiss
{
[StructLayout (LayoutKind.Sequential)]
public struct VersionFixed
{
public int Major;
public int Minor;
public int Patch;
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, EntryPoint = "kiss_get_version", CallingConvention=CallingConvention.StdCall)]
public static extern VersionFixed GetVersion();
}
public delegate void resultDlg(float[] result, ref Network network, ref object userData);
public class Network
{
private CNetwork net;
private bool disposed = false;
public Network(string path, resultDlg callback, bool verbose = false)
{
byte ret = Capi.kiss_load_network_prealloc(ref net, path, callback, Convert.ToByte(verbose));
Console.WriteLine("ret: {0:D}", ret);
if(ret == 0)
throw new System.IO.FileLoadException(getError());
}
public void Run(Spectra spectra, object userData)
{
if(spectra.Real.Length*2 != (int)net.inputSize)
throw new ArgumentException("Spectra is the wrong size for network, use resample() first");
Capi.kiss_async_run_inference_complex(ref net, spectra.Real, spectra.Imaginary, userData);
}
public void Run(float[] data, object userData)
{
if(data.Length != (int)net.inputSize)
throw new ArgumentException("Data is the wrong size for network");
Capi.kiss_async_run_inference(ref net, data, userData);
}
private string getError()
{
return Marshal.PtrToStringAnsi(Capi.kiss_get_strerror(ref net));
}
protected virtual void Dispose(bool disposing)
{
if(!disposed)
{
disposed = true;
Capi.kiss_free_network_prealloc(ref net);
}
}
public int InputSize
{
get{return (int)net.inputSize;}
}
public int OutputSize
{
get{return (int)net.outputSize;}
}
public string Purpose
{
get{return Marshal.PtrToStringAnsi(net.purpose);}
}
public string InputLabel
{
get{return Marshal.PtrToStringAnsi(net.inputLabel);}
}
public bool ComplexInput
{
get{return Convert.ToBoolean(net.complexInput);}
}
/*
public string[] OutputLabels
{
get{}
}
*/
}
public class Spectra
{
public float[] Real;
public float[] Imaginary;
public float[] Omega;
public Spectra(float[] Real, float[] Imaginary, float[] Omega)
{
if(Real.Length != Imaginary.Length || Real.Length != Omega.Length)
throw new ArgumentException("the real, imaginary and omega parts of the spectra have to be of the same size");
this.Real = Real;
this.Imaginary = Imaginary;
this.Omega = Omega;
}
public Spectra(double[] Real, double[] Imaginary, double[] Omega)
{
if(Real.Length != Imaginary.Length || Real.Length != Omega.Length)
throw new ArgumentException("the real, imaginary and omega parts of the spectra have to be of the same size");
this.Real = DoubleToFloat(Real);
this.Imaginary = DoubleToFloat(Imaginary);
this.Omega = DoubleToFloat(Omega);
}
public void Normalize()
{
Capi.kiss_normalize_spectra(Real, Imaginary, (UIntPtr)Real.Length);
}
public void Resample(int newSize)
{
var ret_ptr_real = new IntPtr();
var ret_ptr_imag = new IntPtr();
Capi.kiss_resample_spectra(Real, Imaginary, (UIntPtr)Real.Length, ref ret_ptr_real, ref ret_ptr_imag, (UIntPtr)newSize);
Real = new float[newSize];
Imaginary = new float[newSize];
Marshal.Copy(ret_ptr_real, Real, 0, newSize);
Marshal.Copy(ret_ptr_imag, Imaginary, 0, newSize);
Capi.free(ret_ptr_real);
Capi.free(ret_ptr_imag);
}
public Tuple<float, float> Absgrad(int index)
{
var resultArray = new float[2];
IntPtr ptr = Capi.kiss_absgrad(Real, Imaginary, Omega, (UIntPtr)Real.Length, (UIntPtr)index);
Marshal.Copy(ptr, resultArray, 0, 2);
Capi.free(ptr);
return new Tuple<float, float>(resultArray[0], resultArray[1]);
}
public bool Filter(float threshFactor, bool useSecondDeriv)
{
var ret_ptr_real = new IntPtr();
var ret_ptr_imag = new IntPtr();
var lengthPtr = new UIntPtr();
byte ret = Capi.kiss_filter_spectra(Real, Imaginary, Omega, (UIntPtr)Real.Length, threshFactor, Convert.ToByte(useSecondDeriv), ref ret_ptr_real, ref ret_ptr_imag, ref lengthPtr);
if(ret == 1)
{
Real = new float[(int)lengthPtr];
Imaginary = new float[(int)lengthPtr];
Marshal.Copy(ret_ptr_real, Real, 0, (int)lengthPtr);
Marshal.Copy(ret_ptr_imag, Imaginary, 0, (int)lengthPtr);
Capi.free(ret_ptr_real);
Capi.free(ret_ptr_imag);
}
return Convert.ToBoolean(ret);
}
public bool Reduce(float threshFactor, bool useSecondDeriv)
{
var ret_ptr_real = new IntPtr();
var ret_ptr_imag = new IntPtr();
var lengthPtr = new UIntPtr();
byte ret = Capi.kiss_reduce_spectra(Real, Imaginary, Omega, (UIntPtr)Real.Length, threshFactor, Convert.ToByte(useSecondDeriv), ref ret_ptr_real, ref ret_ptr_imag, ref lengthPtr);
if(ret == 1)
{
Real = new float[(int)lengthPtr];
Imaginary = new float[(int)lengthPtr];
Marshal.Copy(ret_ptr_real, Real, 0, (int)lengthPtr);
Marshal.Copy(ret_ptr_imag, Imaginary, 0, (int)lengthPtr);
Capi.free(ret_ptr_real);
Capi.free(ret_ptr_imag);
}
return Convert.ToBoolean(ret);
}
private static float[] DoubleToFloat(double[] input)
{
var res = new float[input.Length];
for(int i = 0; i < input.Length; ++i)
res[i] = (float)input[i];
return res;
}
}
public class Utils
{
public static float[] CreateRange(float start, float end, int size, bool log)
{
IntPtr ptr = Capi.kiss_create_range(start, end, (UIntPtr)size, Convert.ToByte(log));
var ret = new float[size];
Marshal.Copy(ptr, ret, 0, size);
Capi.free(ptr);
return ret;
}
public static float Grad(float[] data, float[] omegas, int index)
{
if(data.Length != omegas.Length)
throw new ArgumentException("the data and omegas must be the same length");
return Capi.kiss_grad(data, omegas, (UIntPtr)data.Length, (UIntPtr)index);
}
public static float Median(float[] data)
{
return Capi.kiss_median(data, (UIntPtr)data.Length);
}
}
[StructLayout (LayoutKind.Sequential)]
public struct CNetwork
{
public UIntPtr inputSize;
public UIntPtr outputSize;
public IntPtr purpose;
public IntPtr inputLabel;
public IntPtr outputLabels;
public byte complexInput;
public byte ready;
}
internal class Capi
{
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, EntryPoint = "kiss_free", CallingConvention=CallingConvention.StdCall)]
public static extern void free(IntPtr ptr);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern IntPtr kiss_create_range(float start, float end, UIntPtr length, byte log);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern void kiss_resample_spectra(float[] in_re, float[] in_im, UIntPtr input_length, ref IntPtr ret_ptr_real, ref IntPtr ret_ptr_imag, UIntPtr output_length);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern void kiss_normalize_spectra(float[] in_re, float[] in_im, UIntPtr input_length);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern IntPtr kiss_absgrad(float[] in_re, float[] in_im, float[] omegas, UIntPtr input_length, UIntPtr index);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern float kiss_grad(float[] data, float[] omegas, UIntPtr input_length, UIntPtr index);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern float kiss_median(float[] data, UIntPtr input_length);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern byte kiss_reduce_spectra(float[] in_re, float[] in_im, float[] omegas, UIntPtr input_length, float thresh_factor,
byte use_second_deriv, ref IntPtr out_re, ref IntPtr out_im, ref UIntPtr output_length);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern byte kiss_filter_spectra(float[] in_re, float[] in_im, float[] omegas, UIntPtr input_length, float thresh_factor,
byte use_second_deriv, ref IntPtr out_re, ref IntPtr out_im, ref UIntPtr output_length);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern byte kiss_load_network_prealloc(ref CNetwork net, string path, resultDlg callback, byte verbose);
[DllImport("libc", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern void puts(string path);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern void kiss_free_network_prealloc(ref CNetwork net);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern byte kiss_async_run_inference(ref CNetwork net, [Out] float[] inputs, [MarshalAs(UnmanagedType.AsAny)] object userData);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern byte kiss_async_run_inference_complex(ref CNetwork net, [Out] float[] real, [Out] float[] imag, [MarshalAs(UnmanagedType.AsAny)] object userData);
[DllImport("kissinference", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention=CallingConvention.StdCall)]
public static extern IntPtr kiss_get_strerror(ref CNetwork net);
public static unsafe string[] IntPtrToAnsiArray(IntPtr* array)
{
int size = 0;
while(array[size] != null)
++size;
string[] OutputStrArray = new string[size];
for(int i = 0; i < size; i++)
OutputStrArray[i] = Marshal.PtrToStringAnsi(array[i]);
return OutputStrArray;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment