首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从二维FFT计算波数域坐标

如何从二维FFT计算波数域坐标
EN

Stack Overflow用户
提问于 2011-08-23 21:02:03
回答 3查看 7.7K关注 0票数 3

我有一个复数的二维数组,它表示在实空间中沿着平面测量的势场。假设阵列是128个单元乘以128个单元,平面的总面积是500m x 500m。此数组中的每个单元格表示空间域中的一个点,坐标在x和y中给定。

当我在这个二维阵列上使用来自scipy.fftpack的二维FFT时,我得到了在波域中表示的相同信息。如何计算输出数组中各点的波域坐标kx和ky?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-24 20:29:39

这里有一些代码来完整地演示这个问题和我能够找到的解决方案。

代码语言:javascript
复制
from numpy import linspace , arange , reshape ,zeros
from scipy.fftpack import fft2 , fftfreq
from cmath import pi

# create some arbitrary data
some_data = arange(0.0 , 16384.0 , dtype = complex)

# reshape it to be a 128x128 2d grid
some_data_grid = reshape(some_data , (128 , 128) )

# assign some real spatial co-ordinates to the grid points   
# first define the edge values
x_min = -250.0
x_max = 250.0
y_min = -250.0
y_max = 250

# then create some empty 2d arrays to hold the individual cell values
x_array = zeros( (128,128) , dtype = float )
y_array = zeros( (128,128) , dtype = float )

# now fill the arrays with the associated values
for row , y_value in enumerate(linspace (y_min , y_max , num = 128) ):

  for column , x_value in enumerate(linspace (x_min , x_max , num = 128) ):

    x_array[row][column] = x_value
    y_array[row][column] = y_value

# now for any row,column pair the x_array and y_array hold the spatial domain
# co-ordinates of the associated point in some_data_grid

# now use the fft to transform the data to the wavenumber domain
some_data_wavedomain = fft2(some_data_grid)

# now we can use fftfreq to give us a base for the wavenumber co-ords
# this returns [0.0 , 1.0 , 2.0 , ... , 62.0 , 63.0 , -64.0 , -63.0 , ... , -2.0 , -1.0 ]
n_value = fftfreq( 128 , (1.0 / 128.0 ) )

# now we can initialize some arrays to hold the wavenumber co-ordinates of each cell
kx_array = zeros( (128,128) , dtype = float )
ky_array = zeros( (128,128) , dtype = float )

# before we can calculate the wavenumbers we need to know the total length of the spatial
# domain data in x and y. This assumes that the spatial domain units are metres and
# will result in wavenumber domain units of radians / metre.
x_length = x_max - x_min
y_length = y_max - y_min

# now the loops to calculate the wavenumbers
for row in xrange(128):

  for column in xrange(128):

    kx_array[row][column] = ( 2.0 * pi * n_value[column] ) / x_length
    ky_array[row][column] = ( 2.0 * pi * n_value[row] ) / y_length

# now for any row,column pair kx_array , and ky_array will hold the wavedomain coordinates
# of the correspoing point in some_data_wavedomain

我知道这可能不是最有效的方法,但希望它很容易理解。我希望这能帮助一些人避免一点挫败感。

票数 6
EN

Stack Overflow用户

发布于 2011-08-24 21:02:31

嗯,似乎如果我使用FFT函数,DC会在元素0结束,在你的例子中,频率之间的间隔是1/500m。因此,以下(不是很简洁)代码片段将获得您的频率轴:

代码语言:javascript
复制
meter = 1.0
L     = 500.0 * meter
N     = 128

dF    = 1.0 / L
freqs = arange(0, N/L, dF)  # array of spatial frequencies.

当然,这些频率是以每米周数为单位,而不是以每米弧度为单位。如果我希望kx和ky是以rads/m为单位的空间频率数组,我就会说

代码语言:javascript
复制
 kx = 2*pi*freqs
 ky = 2*pi*freqs

(假设我已经导入了像arange和pi这样的东西)。

编辑

关于奈奎斯特以上的频率,Stu提出了一个很好的观点,你可能更愿意认为它是负的(我通常是这样认为的,但不是在代码中)。您可以随时执行以下操作:

代码语言:javascript
复制
freqs[freqs > 0.5*N/(2*L)] -= N/L

但如果你真的想要负频率,你可能也想玩fftshift --另一个蠕虫罐头。

票数 0
EN

Stack Overflow用户

发布于 2015-12-03 21:17:04

我在下面的FORTRAN代码中找到了子程序,并尝试将其实现为matlab函数。如果我的翻译正确,请给我一个评论。开始了,这是FORTRAN子程序:

代码语言:javascript
复制
subroutine kvalue(i,j,nx,ny,dkx,dky,kx,ky)
  c Subroutine KVALUE finds the wavenumber coordinates of one
  c element of a rectangular grid from subroutine FOURN.
  c
  c Input parameters:
  c i - index in the ky direction,
  c j - index in the kx direction.
  c nx - dimension of grid in ky direction (a power of two).
  c ny - dimension of grid in kx direction (a power of two).
  c dkx - sample interval in the kx direction,
  c dky - sample interval in the ky direction,
  c
  c Output parameters:
  c kx - the wavenumber coordinate in the kx direction,
  c ky - the wavenumber coordinate in the ky direction,
  c
  real kx,ky
  nyqx=nx/2+l
  nyqy=ny/2+l
  if(j.le.nyqx)then
      kx=(j-l)*dkx
  else
      kx=(j-nx-l)*dkx
  end if
  if(i.le.nyqy)then
          ky=(i-l)*dky
  else
          ky=(i-ny-l)*dky
  end if
  return
  end

我翻译成MATLAB函数:

代码语言:javascript
复制
function [kx,ky]=kvalue(gz,nx,ny,dkx,dky)
nyq_x=nx/2+1;
nyq_y=ny/2+1;
for i=1:length(gz)
    for j=1:length(gz)
        if j <= nyq_x
            kx(j)=(j-1)*dkx;
        else
            kx(j)=(j-nx-1)*dkx;
        end
        if i <= nyq_y
            ky(i)=(i-1)*dky;
        else
            ky(i)=(i-ny-1)*dky;
        end
    end
end

谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7161417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档