2012-12-11 77 views
3

我試圖使這兩個numpy的傅立葉之間的差異感變換:numpy的FFT穩定性

import numpy as np 

samples = 256 

# define the domain in slightly different ways 
t_1 = np.linspace(0.0, 1.0, samples) 
t_2 = np.arange(0.0, 1.0, 1.0/samples) 

## The two domains are not identical, but they're close 
print np.sum((t_1 - t_2) ** 2) 
# 0.0013046364379084878 

# simple sin wave 
f = lambda t : 2 * np.sin(2 * 2 * pi * t) 

# signals over each domain 
s_1 = f(t_1) 
s_2 = f(t_2) 

# fourier transform 
fft_1 = np.fft.fft(s_1) 
fft_2 = np.fft.fft(s_2) 

freq = np.fft.fftfreq(samples) 

# plot the FFT differences 
plt.figure() 
plt.subplot(2,1,1) 
plt.plot(freq, fft_1, 'x') 
plt.subplot(2,1,2) 
plt.plot(freq, fft_2, 'x') 

fft_plot

在一種情況下,在信號的單一頻率被清楚地檢測到,並在另一個它不是。一個程序比另一個更正確嗎?

回答

3

這兩個地塊比你意識到的更相似。請記住,fft返回一個複雜的數組。此外,輸入函數的移位導致「k空間」中的相移。因爲2*sin(a*pi*x) == i*(exp(i*a*pi*x) - exp(-i*a*pi*x)),s_2在k空間的虛部中具有所有的能量(注意y軸的數量級爲1e-12),所以s_1稍微移動一點,以便在k的真實分量中看到一點信號 - 空間,但大部分力量仍然在虛構部分。看看當我繪製幅度abs(k-space)時會發生什麼情況,而不是繪製真實的組件(這是matplotlib在給定複數時似乎是這樣做的)。

import numpy as np 

samples = 256 

# define the domain in slightly different ways 
t_1 = np.linspace(0.0, 1.0, samples) 
t_2 = np.arange(0.0, 1.0, 1.0/samples) 

## The two domains are not identical, but they're close 
print np.sum((t_1 - t_2) ** 2) 
# 0.0013046364379084878 

# simple sin wave 
f = lambda t : 2 * np.sin(2 * 2 * pi * t) 

# signals over each domain 
s_1 = f(t_1) 
s_2 = f(t_2) 

# fourier transform 
fft_1 = np.fft.fft(s_1) 
fft_2 = np.fft.fft(s_2) 

freq = np.fft.fftfreq(samples) 

# plot the FFT differences 
plt.figure() 
plt.subplot(2,1,1) 
plt.plot(freq, np.abs(fft_1.imag), 'x') 
plt.subplot(2,1,2) 
plt.plot(freq, np.abs(fft_2.imag), 'x') 

PLot of abs(fft(f))