如何计算数组中每个复数的大小?

How to compute the magnitude of each complex number in an array?

我正在尝试测试一个计算信号(即正弦波)的离散傅立叶变换的程序。为了测试它,我需要绘制我的结果。然而,结果是一个大小为 N 的数组(当前为 400),并填充了 z = x + iy 形式的复数。所以我知道要测试它我需要绘制这些结果,为此我需要绘制 |z|。这是我的程序:

program DFT
implicit none
integer :: k, N, x, y, j, r, l, istat, p
integer, parameter :: dp = selected_real_kind(15,300)
real, allocatable,dimension(:) :: h
complex, allocatable, dimension(:) :: rst
complex, dimension(:,:), allocatable :: W
real(kind=dp) :: pi
p = 2*pi
!open file to write results to
open(unit=100, file="dft.dat", status='replace')

N = 400
!allocate arrays as length N, apart from W (NxN)
allocate(h(N))
allocate(rst(N))
allocate(W(-N/2:N/2,1:N))

pi = 3.14159265359
!loop to create the sample containing array
do k=1,N
  h(k) = sin((2*pi*k)/N)
end do

!loop to fill the product matrix with values
do j = -N/2,N/2
do k = 1, N

    W(j,k) = EXP((2.0_dp*pi*cmplx(0.0_dp,1.0_dp)*j*k)/N)

end do
end do
!use of matmul command to multiply matrices
rst = matmul(W,h)
print *, h, w
write(100,*) rst

end program

所以我的问题是如何计算数组中所有单个复数的大小?

ABS 内在函数 return 是 Fortran 中复数的大小。它也是一个基本函数,因此对于复杂类型的数组,简单的 ABS( array ) 将 return 一个与包含您想要的结果的原始数组类型相同的真实数组。