时域/频域转换

一维数据
def show(ori_func, ft, sampling_period = 5): 
    n = len(ori_func) 
    interval = sampling_period / np.float(n)
    # 绘制原始函数
    plt.subplot(2, 1, 1) 
    plt.plot(np.arange(0, sampling_period, interval), ori_func, 'black') 
    plt.xlabel('Time'), plt.ylabel('Amplitude') 
    # 绘制变换后的函数
    plt.subplot(2,1,2) 
    frequency = np.arange(n / 2) / (n * interval) 
    nfft = abs(ft[range(int(n / 2))] / n ) 
    plt.plot(frequency, nfft, 'red') 
    plt.xlabel('Freq (Hz)'), plt.ylabel('Amp. Spectrum') 
    plt.show()

qs = q # (1,1000)
fs = fft(q) # np.fft.fft
show(qs,fs)
二维数据
def show(ori_func, ft, sampling_period = 5):  # 0.21s or 5.
    n = len(ori_func) 
    interval = sampling_period / np.float(n)
    # 绘制原始函数
    plt.subplot(2, 1, 1) 
    plt.plot(np.arange(0, sampling_period, interval), ori_func) 
    plt.xlabel('Time'), plt.ylabel('Amplitude') 
    # 绘制变换后的函数
    plt.subplot(2,1,2) 
    frequency = [np.arange(n / 2.) / (n * interval)]*ft.shape[0]

    nfft = abs(ft[:,range(int(n / 2.))] / n ) 
    plt.plot(frequency, nfft) 
    plt.xlabel('Freq (Hz)'), plt.ylabel('Amp. Spectrum') 
    plt.show()

qs = q # (200,1000)
fs = fft(q)
show(qs,fs)

参考

  • https://www.jianshu.com/p/58630d0489ea

你可能感兴趣的:(时域/频域转换)