申请空间时,请使用GCHandle.Alloc,这里只是调用DLL,并没有将其静态连接的 .lib 文件
将libfftw3-3.dll 文件放到项目中
//申请空间
//IntPtr In_intPtr= FFT.malloc(N_FFT*8);
//IntPtr Out_intPtr= FFT.malloc(N_FFT * 8*2);
GCHandle In_intPtr = GCHandle.Alloc(DataIn_y1, GCHandleType.Pinned);
GCHandle Out_intPtr = GCHandle.Alloc(fftw_Complex, GCHandleType.Pinned);
//正变换
IntPtr plan = FFT.plan_r2c_ld(N_FFT, In_intPtr.AddrOfPinnedObject(), Out_intPtr.AddrOfPinnedObject(), 0);
//执行变换
FFT.execute(plan);//可能重复执行,
FFT.execute(plan);
//释放输入输出数组
FFT.destroy_plan(plan);
//释放空间
In_intPtr.Free();
Out_intPtr.Free();
TTF
public class FFT
{
#region FFTW 库调用申明
/// <summary>
/// Allocates FFTW-optimized unmanaged memory
/// </summary>
/// <param name="length">Amount to allocate, in bytes</param>
/// <returns>Pointer to allocated memory</returns>
[DllImport("libfftw3-3.dll", EntryPoint = "fftw_malloc", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr malloc(int length);
/// <summary>
/// 释放内存空间
/// </summary>
/// <param name="length">指针</param>
[DllImport("libfftw3-3.dll", EntryPoint = "fftw_free", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void free(IntPtr length);
/// <summary>
/// 一维实数据的DFT,正变换
/// </summary>
/// <param name="n">数据个数</param>
/// <param name="In">double 实数输入</param>
/// <param name="Out">fftw_complex 复数输出</param>
/// <param name="flags">FFTW_MEASURE 0</param>
/// <returns></returns>
[DllImport("libfftw3-3.dll", EntryPoint = "fftw_plan_dft_r2c_1d", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr plan_r2c_ld(int n, IntPtr In, IntPtr Out, int flags);
/// <summary>
/// 一维实数据的DFT,逆变换
/// </summary>
/// <param name="n">数据个数</param>
/// <param name="In">fftw_complex 复数输入</param>
/// <param name="Out">double 实数输出</param>
/// <param name="flags">FFTW_MEASURE 0</param>
/// <returns></returns>
[DllImport("libfftw3-3.dll", EntryPoint = "fftw_plan_dft_c2r_1d", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr plan_c2r_ld(int n, IntPtr In, IntPtr Out, int flags);
/// <summary>
/// 执行变换
/// </summary>
/// <param name="length"></param>
[DllImport("libfftw3-3.dll", EntryPoint = "fftw_execute", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void execute(IntPtr plan);
/// <summary>
/// 不使用Plan,将其和输入输出数组销毁
/// </summary>
/// <param name="length"></param>
[DllImport("libfftw3-3.dll", EntryPoint = "fftw_destroy_plan", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void destroy_plan(IntPtr plan);
//这里还涉及到一个代理 GCHandle (using System.Runtime.InteropServices)他可以帮我们在地址和对象之间进行转换
// IntPtr plan;
// double[] fin = new double[nx * ny * 2];
// GCHandle hin = GCHandle.Alloc(fin, GCHandleType.Pinned);
//// plan = FFT.dft_2d(nx, ny, hin.AddrOfPinnedObject(), hin.AddrOfPinnedObject(), fftw_direction.Forward, fftw_flags.Estimate);
#endregion
//-----定义复数结构体-----------------------//
public struct Compx
{
/// <summary>
/// 实部
/// </summary>
public float real;
/// <summary>
/// 虚部
/// </summary>
public float imag;
}
}
来源:CSDN
作者:广_
链接:https://blog.csdn.net/baidu_38131614/article/details/103604114