Python 两个相似信号的 FFT 和幅度谱具有不同的频率

Python FFT & Magnitude Spectrum of two similar signals have different frequencies

我的目标是比较相似信号的 FFT。出于某种原因,当我采用相同长度的两个信号的幅度谱时,频率不同......因此我无法对两个信号进行简单的并排比较。有人对如何在信号上获得相同的 FFT 有任何提示吗?

例如 Signal1 提供以下内容:

更新:这是从 0-400Hz 绘制的两个信号

这是我的代码: 代码背后的逻辑是导入信号,找到声音开始的地方,将信号截断为1秒长度,对信号进行FFT比较。

import numpy as np
from scipy.io.wavfile import read
from pylab import plot
from pylab import plot, psd, magnitude_spectrum
import matplotlib.pyplot as plt

#Hello Signal!!!
(fs, x) = read('C:\Desktop\Spectral Work\EB_AB_1_2.wav') 

#Remove silence out of beginning of signal with threshold of 1000 

def indices(a, func):
    #This allows to use the lambda function for equivalent of find() in matlab
    return [i for (i, val) in enumerate(a) if func(val)]

#Make the signal smaller so it uses less resources
x_tiny = x[0:100000]
#threshold is 1000, 0 is calling the first index greater than 1000
thresh = indices(x_tiny, lambda y: y > 1000)[1]
# backs signal up 20 bins, so to not ignore the initial pluck sound...
thresh_start = thresh-20
#starts at threshstart ends at end of signal (-1 is just a referencing thing)
analysis_signal = x[thresh_start-1:] 

#Split signal so it is 1 second long
one_sec = 1*fs
onesec = x[thresh_start-1:one_sec]

#***unsure is just a placeholder because it spits out a weird error if I don't use
#a third variable
(xsig, ysig, unsure) = magnitude_spectrum(onesec, Fs=fs)

xsig 是振幅,ysig 是频率。

如果您有兴趣亲自尝试,请点击此处链接到 .wav 文件: .wav1 .wav2 注意:最初我上传了错误的 .wav1 文件...现在正确的已经上传了。

我猜你的信号长度实际上不一样。如果您独立地对它们进行阈值处理,您的 thresh_start 值将不相同,因此:

onesec = x[thresh_start-1:one_sec]

将为您提供两个文件的不同长度数组。您可以单独计算 threshold 值,然后将该数字作为常量提供给该模块,或者使您的 onesec 数组与 each[=24 的开头长度相同=] 阈值:

onesec = x[thresh_start-1:one_sec+thresh_start-1]

(请记住切片符号是 [start:stop],而不是 [start:length]