Python 中二维矩阵的傅里叶变换

Fourier Transformation of 2D Matrix in 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)))

我没有使用随机数据,但我不想让示例变得那么复杂。事实上,当我现在使用以下 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')

我的结果看起来总是一样,我不知道哪里出了问题。同样对于我的相关值,它具有很大的对称性,情节看起来是一样的。

你不能'see'频谱,因为直流主导。

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');