2012-12-11 74 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))