gpt4 book ai didi

python - 如何从 2d FFT 计算波数域坐标

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:44 25 4
gpt4 key购买 nike

我有一个二维复数数组,代表在真实空间中沿平面测量的势场。假设阵列是 128 x 128 单元,平面的总面积是 500m x 500m。该数组中的每个单元表示空间域中的一个点,其坐标在 x 和 y 中给出。

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

最佳答案

这里有一些代码可以充分展示问题和我能够找到的解决方案。

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

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

关于python - 如何从 2d FFT 计算波数域坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7161417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com