Numpy-FFT稳定性

Numpy-FFT稳定性,numpy,signal-processing,fft,Numpy,Signal Processing,Fft,我试图弄清楚这两种numpy傅里叶变换之间的区别: 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

我试图弄清楚这两种numpy傅里叶变换之间的区别:

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返回一个复杂数组。此外,输入函数的偏移会导致“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' )