簡體   English   中英

兩個相似信號的Python FFT和幅度頻譜具有不同的頻率

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

我的目標是比較相似信號的FFT。 出於某種原因,當我獲取相同長度的兩個信號的幅度譜時,頻率是不同的……因此,我無法對兩個信號進行簡單的並排比較。 任何人都對如何在信號上獲得相同的FFT有任何建議?

因此,例如Signal1提供以下內容:

信號1信號2

更新:這是從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數組的長度與每個閾值的開始處相同:

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

(請記住,切片符號是[start:stop] ,而不是[start:length]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM