【问题标题】:Numpy FFT stabilityNumpy FFT 稳定性
【发布时间】:2012-12-11 16:03:02
【问题描述】:

我试图弄明白这两个 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' )

在一种情况下,可以清楚地检测到信号中的单一频率,而在另一种情况下则没有。一种程序比另一种更正确吗?

【问题讨论】:

    标签: numpy signal-processing fft


    【解决方案1】:

    这两个情节比你想象的更相似。请记住,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' )
    

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 2018-10-03
      • 1970-01-01
      • 2012-04-30
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 2017-10-20
      相关资源
      最近更新 更多