Skip to content

Instantly share code, notes, and snippets.

@tenomoto
Created September 21, 2023 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenomoto/ed64c5be74d8b563438aa1cb7b307eb8 to your computer and use it in GitHub Desktop.
Save tenomoto/ed64c5be74d8b563438aa1cb7b307eb8 to your computer and use it in GitHub Desktop.
FFTW test
program fftw_test
use, intrinsic :: iso_c_binding
implicit none
include 'fftw3.f03'
integer(c_int), parameter :: n = 16, k = 2
integer(c_size_t) :: plan_forward, plan_backward
integer(c_int) :: i
real(c_double) :: pi
real(c_double), dimension(n) :: gin, gout
complex(c_double_complex), dimension(n/2) :: w
call dfftw_plan_dft_r2c_1d(plan_forward, n, gin, w, FFTW_ESTIMATE)
call dfftw_plan_dft_c2r_1d(plan_backward, n, w, gout, FFTW_ESTIMATE)
pi = acos(-1.0_c_double)
print *, "init"
do i = 1, n
gin(i) = sin(2 * pi * k * (i - 1) / n)
print *, i, gin(i)
end do
call dfftw_execute_dft_r2c(plan_forward, gin, w)
print *, "analysis"
do i = 1, n/2
print *, i-1, w(i)
end do
call dfftw_execute_dft_c2r(plan_backward, w, gout)
gout(:) = gout(:) / n
print *, "synthesis"
do i = 1, n
print *, i, gout(i), gout(i) - gin(i)
end do
call dfftw_destroy_plan(plan_forward)
call dfftw_destroy_plan(plan_backward)
end program fftw_test
.SUFFIXES:
.SUFFIXES: .o .f90
FC = gfortran12
LDLIBS = -lfftw3
LDFLAGS = -L/usr/local/lib
FFLAGS = -O2 -I/usr/local/include
SRCS = $(TARGET).f90
OBJS = $(SRCS:.f90=.o)
TARGET = fftw_test
$(TARGET) : $(OBJS)
$(FC) $(LDFLAGS) -o $@ $? $(LDLIBS)
clean:
rm -f $(OBJS) $(TARGET)
.f90.o:
$(FC) $(FFLAGS) -c $<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment