【问题标题】:Python FFT: freq shiftPython FFT:频移
【发布时间】:2020-04-06 12:22:42
【问题描述】:

在对信号进行频谱分析时,我遇到了一个奇怪的问题,即绘制的信号频率发生了偏移(或加倍)。这是一个显示我的方法的简单示例:以 100kHz 采样 1kHz 正弦信号。最后,信号仓出现在 2kHz 而不是 1kHz。

import numpy as np
import matplotlib.pyplot as plt

time_step = 1.0/100e3
t = np.arange(0, 2**14) * time_step

sig = np.sin(2*np.pi*1e3*t)

sig_fft = np.fft.rfft(sig)

#calculate the power spectral density
sig_psd = np.abs(sig_fft) ** 2 + 1

#create the frequencies
fftfreq = np.fft.fftfreq(len(sig_psd), d=time_step)

#filter out the positive freq
i = fftfreq > 0

plt.plot(fftfreq[i], 10*np.log10(sig_psd[i]))
plt.xscale("log")

【问题讨论】:

    标签: python numpy matplotlib fft frequency-analysis


    【解决方案1】:

    您使用了错误的函数来计算频率。实信号的 FFT 变换具有负频率分量,它们是正频率分量的复共轭,即频谱是 Hermitian 对称的。 rfft() 利用这一事实,不输出负频率,只输出直流分量和正频率。因此,sig_psd 的长度是使用 fft() 而不是 rfft() 并将其传递给 fftfreq() 时所获得的长度的两倍,有效地使频率加倍。

    解决方案:改用rfftfreq()

    import numpy as np
    import matplotlib.pyplot as plt
    
    time_step = 1.0/100e3
    t = np.arange(0, 2**14) * time_step
    
    sig = np.sin(2*np.pi*1e3*t)
    
    sig_fft = np.fft.rfft(sig)
    
    #calculate the power spectral density
    sig_psd = np.abs(sig_fft) ** 2 + 1
    
    #create the frequencies
    fftfreq = np.fft.rfftfreq(len(sig), d=time_step)  # <-- note: len(sig), not len(sig_psd)
    
    #filter out the positive freq
    i = fftfreq > 0  # <-- note: Not really needed, this just removes the DC component
    
    plt.plot(fftfreq[i], 10*np.log10(sig_psd[i]))
    plt.xscale("log")
    

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 1970-01-01
      • 2010-11-21
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2020-10-14
      相关资源
      最近更新 更多