2016-08-29 87 views
0

我有x-y-z形式的數據並想要創建沿x-y的功率譜。下面是我張貼檢查,我會用我的實際數據是想錯了一個基本的例子:2d fft numpy/python混淆

import numpy as np 
from matplotlib import pyplot as plt 

fq = 10; N = 20 
x = np.linspace(0,8,N); y = x 
space = x[1] -x[0] 

xx, yy = np.meshgrid(x,y) 
fnc = np.sin(2*np.pi*fq*xx) 
ft = np.fft.fft2(fnc) 
ft = np.fft.fftshift(ft) 

freq_x = np.fft.fftfreq(ft.shape[0], d=space) 
freq_y = np.fft.fftfreq(ft.shape[1], d=space) 
plt.imshow(
    abs(ft), 
    aspect='auto', 
    extent=(freq_x.min(),freq_x.max(),freq_y.min(),freq_y.max()) 
) 
plt.figure() 
plt.imshow(fnc) 

這將導致以下function & frequency數字與不正確的頻率。謝謝。

+0

'插值='nearest''會讓你的結果更加清晰 – Eric

回答

2

您的問題之一是matplotlib的imshow使用不同的座標系來達到您的預期。提供一個origin='lower'參數,如預期的那樣,峯值出現在y=0

,你有另一個問題是,fftfreq需要告訴你的時間步長,而你的情況是8/(N - 1)

import numpy as np 
from matplotlib import pyplot as plt 

fq = 10; N = 20 
x = np.linspace(0,8,N); y = x 
xx, yy = np.meshgrid(x,y) 
fnc = np.sin(2*np.pi*fq*xx) 
ft = np.fft.fft2(fnc) 
ft = np.fft.fftshift(ft) 

freq_x = np.fft.fftfreq(ft.shape[0], d=8/(N - 1)) # this takes an argument for the timestep 
freq_y = np.fft.fftfreq(ft.shape[1], d=8/(N - 1)) 
plt.imshow(
    abs(ft), 
    aspect='auto', 
    extent=(freq_x.min(),freq_x.max(),freq_y.min(),freq_y.max()), 
    origin='lower' ,   # this fixes your problem 
    interpolation='nearest', # this makes it easier to see what is happening 
    cmap='viridis'   # let's use a better color map too 
) 
plt.grid() 
plt.show() 

enter image description here

你可能會說「,但頻率爲10,而不是0.5 !」但是,如果要採樣10的頻率,則需要比8/19更快地進行採樣!奈奎斯特定理說,你需要超過 20的採樣率有所有

+0

有任何希望,感謝歐 – user6769140

+0

任何想法,我怎麼能擺脫奈奎斯特f以上的東西,即只獲得單一頻率?謝謝。 – user6769140

+0

@ user6769140:你已經擺脫了那些東西 - 奈奎斯特頻率是20,但是在你的圖上面的圖中只能達到〜1.2 – Eric