Python中二维矩阵的Fourier变换

2024-04-27 04:24:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个矩阵,有72x72个值,每个值对应于一个有72x72个点的三角形晶格上的一些能量。我试图对这些值进行傅里叶变换,但我不明白如何使用np.fft.fftn进行傅里叶变换

为了说明我的问题,我用一些随机值编写了以下基本代码。三角形给出了晶格的x,y坐标

import numpy as np
import matplotlib.pyplot as plt

    def triangular(nsize):
        x=0
        y=0
        X=np.zeros((nsize,nsize))
        Y=np.zeros((nsize,nsize))
        for i in range(nsize):
            for j in range(nsize):
                X[i,j]+=1/2*j+i
                Y[i,j]+=np.sqrt(3)/2*j
        return(X,Y)

xx = triangular(72)[0]
yy = triangular(72)[1]


plt.figure()
plt.pcolormesh(xx, yy, np.reshape(np.random.rand(72**2),(72,72)))

enter image description here

我没有使用随机数据,但我不想让示例变得那么复杂。事实上,当我现在使用以下FFT时,每次都会看到相同的图:

matrix = []

matrix.append(triangular(72)[0])

matrix.append(triangular(72)[1])

matrix.append(np.reshape(np.random.rand(72**2),(72,72)))

spectrum_3d = np.fft.fftn(matrix)                # Fourrier transform along x, y, energy  

kx = np.linspace(-4*np.pi/3,4*np.pi/3,72)      #this is the range I want to plot

ky = np.linspace(-2*np.pi/np.sqrt(3),2*np.pi/np.sqrt(3),72)



Ky, Kx = np.meshgrid(ky, kx, indexing='ij')       #making a grid 



plt.figure(figsize=(11,9))
psd = plt.pcolormesh(Kx, Ky, abs(spectrum_3d[2])**2)
cbar = plt.colorbar(psd)
plt.xlabel('kx')
plt.ylabel('ky')

enter image description here

我的结果看起来总是一样的,我不知道出了什么问题。同样对于我的相关值,它有一个很大的对称性,图看起来是一样的


Tags: fftnppirangepltsqrttriangular晶格
1条回答
网友
1楼 · 发布于 2024-04-27 04:24:45

由于直流占主导地位,你无法“看到”频谱

import numpy as np
import matplotlib.pyplot as p
%matplotlib inline

n=72
x=np.arange(n)
y=np.arange(n)
X,Y= np.meshgrid(x,y)

data=np.reshape(np.random.rand(n**2),(n,n))

data_wo_DC= data- np.mean(data)

spectrum = np.fft.fftshift(np.fft.fft2(data)) 
spectrum_wo_DC = np.fft.fftshift(np.fft.fft2(data_wo_DC)) 

freqx=np.fft.fftshift(np.fft.fftfreq(72,1))   #q(n, d=1.0)
freqy=np.fft.fftshift(np.fft.fftfreq(72,1))   
fX,fY= np.meshgrid(freqx,freqy)


p.figure(figsize=(20,6))
p.subplot(131)
p.pcolormesh(X,Y, data)  
p.colorbar()

p.subplot(132)
p.pcolormesh(fX,fY,np.abs(spectrum)) 
p.colorbar()
p.title('most data is in the DC')

p.subplot(133)
p.pcolormesh(fX,fY,np.abs(spectrum_wo_DC)) 
p.colorbar()
p.title('wo DC we can see the structure');

enter image description here

相关问题 更多 >