Skip to content

Instantly share code, notes, and snippets.

@LadaF
Last active April 27, 2016 17:48
Show Gist options
  • Save LadaF/73eb430682ef527eea9972ceb96116c5 to your computer and use it in GitHub Desktop.
Save LadaF/73eb430682ef527eea9972ceb96116c5 to your computer and use it in GitHub Desktop.
Simple r2c and c2r FFT with FFTW in Fortran
module FFTW3
use, intrinsic :: iso_c_binding
include "fftw3.f03"
end module
use FFTW3
implicit none
integer, parameter :: n = 100
real(c_double), allocatable :: data_in(:)
complex(c_double_complex), allocatable :: data_out(:)
type(c_ptr) :: planf, planb
allocate(data_in(n))
allocate(data_out(n/2+1))
call random_number(data_in)
planf = fftw_plan_dft_r2c_1d(size(data_in), data_in, data_out, FFTW_ESTIMATE+FFTW_UNALIGNED)
planb = fftw_plan_dft_c2r_1d(size(data_in), data_out, data_in, FFTW_ESTIMATE+FFTW_UNALIGNED)
print *, "real input:", real(data_in)
call fftw_execute_dft_r2c(planf, data_in, data_out)
print *, "result real part:", real(data_out)
print *, "result imaginary part:", aimag(data_out)
call fftw_execute_dft_c2r(planb, data_out, data_in)
print *, "real output:", real(data_in)/n
call fftw_destroy_plan(planf)
call fftw_destroy_plan(planb)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment