When using r2c and c2r FFTW in Fortran, are the forward and backward dimensions same?

后端 未结 2 727
旧巷少年郎
旧巷少年郎 2021-01-27 17:26

Blow is a main file

PROGRAM SPHEROID

USE nrtype
USE SUB_INFO
INCLUDE \"/usr/local/include/fftw3.f\"

INTEGER(I8B) :: plan_forward, plan_backward
INTEGER(I4B) ::         


        
2条回答
  •  时光取名叫无心
    2021-01-27 18:25

    First, although you claim your example is minimal, it is still pretty large, I have no time to study it.

    But I updated my gist code https://gist.github.com/LadaF/73eb430682ef527eea9972ceb96116c5 to show also the backward transform and to answer the title question about the transform dimensions.

    The logical size of the transform is the size of the real array (Real-data DFT Array Format) but the complex part is smaller due to inherent symmetries.

    But when you make first r2c transform from real array of size n to complex array of size n/2+1. and then an opposite transform back, the real array should be again of size n.

    This is my minimal example from the gist:

    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
    

    Note that I am using the modern Fortran interface. I don't like using the old one.

提交回复
热议问题