
任意の曲面を描く関数で表示された曲面を三次元フーリエ変換するPythonプログラムを書いて


以下は、任意の曲面を描く関数で表示された曲面を三次元フーリエ変換するPythonプログラムの例です。このプログラムは、NumPy、Matplotlib、mpl_toolkits.mplot3d、およびscipy.fftpackのライブラリを使用しています。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.fftpack import fftn, fftshift
# Define the function to create the surface
def surface_func(x, y):
return np.sin(np.sqrt(x**2 + y**2))
# Set up the x and y grids
N = 100
x = np.linspace(-5, 5, N)
y = np.linspace(-5, 5, N)
X, Y = np.meshgrid(x, y)
# Create the surface
Z = surface_func(X, Y)
# Plot the surface
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()
# Compute the 3D Fourier transform
Z_fft = fftn(Z)
Z_fft_shift = fftshift(Z_fft)
# Plot the Fourier transform
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim([-50, 50])
ax.set_ylim([-50, 50])
ax.set_zlim([-5000, 5000])
ax.plot_surface(X, Y, np.abs(Z_fft_shift))
plt.show()
このプログラムでは、
surface_func
関数を定義して、xおよびy座標に基づいて曲面を計算します。次に、xおよびy座標のグリッドを設定し、surface_func
関数を使用して曲面を作成します。作成された曲面をplot_surface
関数を使用してプロットします。次に、
fftn
関数を使用して3Dフーリエ変換を計算し、fftshift
関数を使用してデータを中心に移動します。この後、plot_surface
関数を使用してフーリエ変換をプロットします。ここでは、x、y、z軸の範囲を適切に設定し、フーリエ変換の振幅を表すデータをnp.abs
関数を使用して取得します。
帽子みたいな曲面を描く関数だな。
いや、トイレにサワデー♪www
