Advertisement
WarPie90

FFTW

Apr 10th, 2018
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.15 KB | None | 0 0
  1. program new;
  2. {$I SRL/OSR.simba}
  3.  
  4. const
  5.  FFTW_FORWARD        = -1;
  6.  FFTW_BACKWARD        = 1;
  7.  
  8.  FFTW_MEASURE         = 0;
  9.  FFTW_DESTROY_INPUT   = 1;   {1U << 0}
  10.  FFTW_UNALIGNED       = 2;   {1U << 1}
  11.  FFTW_CONSERVE_MEMORY = 4;   {1U << 2}
  12.  FFTW_EXHAUSTIVE      = 8;   {1U << 3} {NO_EXHAUSTIVE is default }
  13.  FFTW_PRESERVE_INPUT  = 16;  {1U << 4} {cancels FFTW_DESTROY_INPUT}
  14.  FFTW_PATIENT         = 32;  {1U << 5} {IMPATIENT is default }
  15.  FFTW_ESTIMATE        = 64;  {1U << 6}
  16.  
  17.  
  18. type
  19.   PComplex = ^Complex;
  20.   Complex = packed record
  21.     RE,IM: Single;
  22.   end;
  23.   TComplexArray = array of Complex;
  24.  
  25.   FFTW_PLAN  = type Pointer;
  26.  
  27.   LP_FFTW_MALLOC    = function(n: Int32): Pointer;
  28.   LP_FFTW_FREE      = procedure(p: Pointer);
  29.   LP_FFTW_PLAN_DFT1 = function(n: Int32; inData, outData: PComplex; sign: Int32; flags: UInt32): FFTW_PLAN;
  30.   LP_FFTW_EXEC_DFT1 = procedure(plan: FFTW_PLAN; inData, outData: PComplex);
  31.   LP_FFTW_EXEC      = procedure(plan: FFTW_PLAN);
  32.  
  33.   FN_FFTW_MALLOC    = native(LP_FFTW_MALLOC, ffi_cdecl);
  34.   FN_FFTW_FREE      = native(LP_FFTW_FREE, ffi_cdecl);
  35.   FN_FFTW_PLAN_DFT1 = native(LP_FFTW_PLAN_DFT1, ffi_cdecl);
  36.   FN_FFTW_EXEC_DFT1 = native(LP_FFTW_EXEC_DFT1, ffi_cdecl);
  37.   FN_FFTW_EXEC      = native(LP_FFTW_EXEC, ffi_cdecl);
  38.  
  39. var
  40.   FFTW: THandle;
  41.   n_fftw_malloc:    FN_FFTW_MALLOC;
  42.   n_fftw_free:      FN_FFTW_FREE;
  43.   n_fftw_plan_dft1: FN_FFTW_PLAN_DFT1;
  44.   n_fftw_exec:      FN_FFTW_EXEC;
  45.  
  46.   fftw_malloc:    LP_FFTW_MALLOC;
  47.   fftw_free:      LP_FFTW_FREE;
  48.   fftw_plan_dft1: LP_FFTW_PLAN_DFT1;
  49.   fftw_exec:      LP_FFTW_EXEC;
  50.  
  51.  
  52. function ToString(x: Complex): String; override;
  53. begin
  54.   if Round(x.im,3) >= 0 then
  55.     Result := ToStr(Round(x.re,3))+'+'+ToStr(Round(x.im,3))+'i'
  56.   else
  57.     Result := ToStr(Round(x.re,3))+ToStr(Round(x.im,3))+'i'
  58. end;
  59.  
  60. function AsComplex(x: array of Extended): TComplexArray;
  61. var i:Int32;
  62. begin
  63.   SetLength(Result, Length(x));
  64.   for i:=0 to High(x) do
  65.   begin
  66.     Result[i].re := x[i];
  67.     Result[i].im := 0;
  68.   end;
  69. end;
  70.  
  71.  
  72. procedure LoadFFTW();
  73. begin
  74.   FFTW := LoadLibrary('libfftw3f-3.dll');
  75.  
  76.   n_fftw_malloc    := GetProcAddress(FFTW, 'fftwf_malloc');
  77.   n_fftw_free      := GetProcAddress(FFTW, 'fftwf_free');
  78.   n_fftw_plan_dft1 := GetProcAddress(FFTW, 'fftwf_plan_dft_1d');
  79.   n_fftw_exec      := GetProcAddress(FFTW, 'fftwf_execute');
  80.  
  81.   fftw_malloc    := Lapify(n_fftw_malloc);
  82.   fftw_free      := Lapify(n_fftw_free);
  83.   fftw_plan_dft1 := Lapify(n_fftw_plan_dft1);
  84.   fftw_exec      := Lapify(n_fftw_exec);
  85. end;
  86.  
  87. var
  88.   raw: TComplexArray;
  89.  
  90.   test, res: PComplex;
  91.   plan: FFTW_PLAN;
  92.   N: Int32;
  93.  
  94.   t: Double;
  95. begin
  96.   LoadFFTW();
  97.  
  98.   //raw  := AsComplex([1,1,1,1,0,0,0,0]);
  99.   SetLength(raw, 1024*1024);
  100.  
  101.   N    := Length(raw);
  102.   test := fftw_malloc(SizeOf(Complex) * N);
  103.   res  := fftw_malloc(SizeOf(Complex) * N);
  104.  
  105.   t := PerformanceTimer();
  106.   plan := fftw_plan_dft1(N, test, res, FFTW_FORWARD, FFTW_ESTIMATE);
  107.   WriteLn(PerformanceTimer() - t, 'ms');
  108.  
  109.   t := PerformanceTimer();
  110.   MemMove(raw[0], test^, SizeOf(Complex)*N);
  111.   fftw_exec(plan);
  112.   MemMove(res^, raw[0], SizeOf(Complex)*N);
  113.  
  114.   WriteLn(PerformanceTimer() - t, 'ms');
  115.  
  116.   FreeLibrary(FFTW);
  117. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement