Fortran Coder

查看: 7156|回复: 4
打印 上一主题 下一主题

[数学库] intel fortran调用FFTW,傅里叶正变换值为何显示N/2+1

[复制链接]

2

帖子

1

主题

0

精华

新人

F 币
23 元
贡献
10 点
跳转到指定楼层
楼主
发表于 2018-8-2 17:11:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
intel fortran调用FFTW,real to complex。傅里叶正变换值为何只显示N/2+1数据,结果如下,怎样才能使得傅里叶变化的数据都显示出来呢?

程序:
program test1d
implicit none

   integer,parameter::N=5
   double precision in(N)
   double complex out(N)
   double precision var1(N)
   integer :: plan
   integer :: i
   integer,parameter :: fftw_estimate = 64
   !
    do i=1,N
         in(i)=i
    end do

   call dfftw_plan_dft_r2c_1d(plan,N,in,out,FFTW_ESTIMATE) ! in ---> out  1 means ifft
   call dfftw_execute_dft_r2c(plan,in,out)
   call dfftw_destroy_plan(plan)

   call dfftw_plan_dft_c2r_1d(plan,N,out,var1,FFTW_ESTIMATE) ! out ---> var1  -1 means fft
   call dfftw_execute_dft_c2r(plan,out,var1)
   call dfftw_destroy_plan(plan)
   
   
   do i=1,N
!     var1(i)=var1(i)/N
   write(*,*) i, in(i), out(i)!,var1(i)

   end do

end program test1d

计算结果:
1  1.000000000000   (15.000000000000, 0.000000000000000)
2  2.000000000000   (-2.5000000000000, 3.44095480117793)
3  3.000000000000   (-2.5000000000000, 0.812299240582266)
4  4.000000000000   (0.0000000000000, 0.000000000000000)
5  5.000000000000   (0.0000000000000, 0.000000000000000)
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
沙发
发表于 2018-8-2 17:40:33 | 只看该作者
后面是共轭对称的呀。

2

帖子

1

主题

0

精华

新人

F 币
23 元
贡献
10 点
板凳
 楼主| 发表于 2018-8-2 17:59:41 | 只看该作者
vvt 发表于 2018-8-2 17:40
后面是共轭对称的呀。

就是2,3和4,5是共轭对称的?我用matlab验证了一下,好像是这样;我逆变换(ifft)后,也返回原值了。谢谢您。

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
地板
发表于 2018-8-2 18:23:08 | 只看该作者
是的。
回复

使用道具 举报

6

帖子

2

主题

0

精华

入门

F 币
52 元
贡献
23 点
5#
发表于 2019-11-20 11:11:45 | 只看该作者
您好,我算了一个4*4的数组,得到的结果和您类似,结果中也只显示了N/2+1的数据,N/2+1以后的数据都是0,通过对比matlab算的结果,发现偏差很大;
麻烦问一下,如果我要得到N/2+1以后的数据,应该怎么做呢??
[Fortran] 纯文本查看 复制代码
program main

USE globalvars

	implicit none
	
	include 'fftw3.f'

	integer :: i,j
	real,allocatable  :: eta(:,:), input(:,:)
	complex,allocatable :: etak(:,:), output(:,:)
	
	allocate (eta(nx,ny), input(nx,ny))
	allocate (etak(nx,ny), output(nx,ny))
	
	eta = reshape([3,0,-1,0,&
				   -1,-2,1,1,&
				   -2,2,2,2,&
				   2,-1,4,3],shape(eta)) 
	
	do i=1,nx
		do j=1,ny
		
			write(*,*) eta(j,i)
		
		enddo

	enddo

	CALL sfftw_plan_dft_r2c_2d(p_up, nx, ny, input, output, FFTW_EXHAUSTIVE)
	CALL sfftw_plan_dft_c2r_2d(p_dn, nx, ny, output, input, FFTW_EXHAUSTIVE)
	
	call forward(eta,etak)
	
	do i=1,nx
		do j=1,ny
		
			write(*,*) etak(j,i)
		
		enddo

	enddo
	
	call backward(etak,eta)
	
	do i=1,nx
		do j=1,ny
		
			write(*,*) eta(j,i)
		
		enddo

	enddo
	
	deallocate (eta, input)
	deallocate (etak, output)

 end program



MODULE globalvars

IMPLICIT NONE

! Simulation cell parameters:
	integer,parameter:: Nx = 4, Ny = 4
	integer(kind = 8) :: p_up, p_dn
     
END MODULE globalvars


subroutine backward(output,input)

USE globalvars
!calculates backward fourier transform (complex to real) using FFTW
	implicit none
	
	COMPLEX, INTENT(IN) :: output(nx,ny)
	REAL, INTENT(OUT)   :: input(nx,ny)
	COMPLEX :: tempk(nx,ny)
	INTEGER :: i,j
	REAL :: sizescale

	sizescale = 1.0/FLOAT(nx * ny)
	tempk = 0.0
	DO j=1,ny
		DO i=1,nx
			tempk(i,j) = output(i,j)
		END DO
	END DO

	CALL sfftw_execute_dft_c2r(p_dn,tempk,input)
	DO j=1,ny
		DO i=1,nx
			input(i,j) = sizescale*input(i,j)
		END DO
	END DO
!	RETURN
END subroutine backward


subroutine forward(input,output)

USE globalvars
!calculates forward fourier transform (real to complex) using FFTW
	implicit none
	
	real, intent(IN) :: input(nx,ny)
	complex, intent(OUT) :: output(nx,ny)

	call sfftw_execute_dft_r2c(p_up,input,output)
!	RETURN
end subroutine forward
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-20 19:44

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表