我的目标是比较类似信号的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文件......正确的文件现在已经上传。
答案 0 :(得分:2)
我猜你的信号长度实际上并不相同。如果您单独对它们进行阈值处理,则thresh_start
值将不相同,因此:
onesec = x[thresh_start-1:one_sec]
将为这两个文件提供不同长度的数组。您可以单独计算threshold
值,然后将该数字作为常量提供给此模块,或者使您的onesec
数组与每个阈值的开头相同值:
onesec = x[thresh_start-1:one_sec+thresh_start-1]
(请注意,切片表示法是[start:stop]
,而不是[start:length]
)