简体   繁体   English

在FFT图中检测峰值

[英]Detecting Peaks in a FFT Plot

I was wondering how is it possible to detect new peaks within an FFT plot in Python. 我想知道如何在Python中检测FFT图中的新峰值。 let's say i have this simple Plot: 让我说我有这个简单的情节: 在此输入图像描述 And i want to automatically measure the 'Similarity' or the Peaks location within a noisy Signal, i have tried to use the cosine Similarity but my real Signal is way too noisy, and with even if i add a new peak to the signal, i keep getting a Cosine of 0.9 since it's only one peak. 我想在嘈杂的信号中自动测量“相似度”或“峰值”位置,我试图使用余弦相似性,但我的真实信号太嘈杂了,即使我在信号中添加了一个新的峰值,我继续获得0.9的余弦,因为它只是一个峰值。 This is an example of my real signal, and i also have the problem that my signal can be shiffted within the measures, so i can't get a stable frequency array they can be within a window of +/- 100 Hz : 这是我的真实信号的一个例子,我也有一个问题,我的信号可以在测量中被调整,所以我无法得到一个稳定的频率数组,他们可以在+/- 100 Hz的窗口内: 在此输入图像描述 This the code that used for the first Plot : 这是用于第一个Plot的代码:

import numpy as np
from pylab import *
import scipy.fftpack

# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y1 = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)+ 0.7*np.sin(30.0 * 2.0*np.pi*x)+ 0.5*np.sin(10.0 * 2.0*np.pi*x)
y2 = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)+ 0.2*np.sin(60.0 * 2.0*np.pi*x)+ 0.4*np.sin(40.0 * 2.0*np.pi*x)
yf1 = scipy.fftpack.fft(y1)
yf2 = scipy.fftpack.fft(y2)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)

fig, ax = plt.subplots()
plot(xf, 2.0/N * np.abs(yf1[:N/2]))
plot(xf, 2.0/N * np.abs(yf2[:N/2]))
xlabel('Freq (Hz)',fontsize=16,weight='bold')
ylabel('|Y(freq)|',fontsize=16,weight='bold')
ax = gca()
fontsize = 14
for tick in ax.xaxis.get_major_ticks():
    tick.label1.set_fontsize(fontsize)
    tick.label1.set_fontweight('bold')
for tick in ax.yaxis.get_major_ticks():
    tick.label1.set_fontsize(fontsize)
    tick.label1.set_fontweight('bold')
grid(True)
show()
def cosine_similarity(v1,v2):
    "compute cosine similarity of v1 to v2: (v1 dot v2)/{||v1||*||v2||)"
    sumxx, sumxy, sumyy = 0, 0, 0
    for i in range(len(v1)):
        x = v1[i]; y = v2[i]
        sumxx += x*x
        sumyy += y*y
        sumxy += x*y
    return sumxy/math.sqrt(sumxx*sumyy)

print 'Cosine Similarity', cosine_similarity(2.0/N * np.abs(yf1[:N/2]),2.0/N * np.abs(yf2[:N/2]))

I have also though of setting a threshold, but sometime the peaks within the real signal can be smaller than the pre-defined Threshold. 我也设置了一个阈值,但有时真实信号中的峰值可能小于预定义的阈值。 Any ideas ? 有任何想法吗 ?

There are many ways to find peaks, and even to interpolate their sub-sample location. 有许多方法可以找到峰值,甚至可以插入它们的子样本位置。 Once you have the peaks, just check if you find a new one. 一旦你有了峰值,只需检查你是否找到了新的峰值。

You can use the peakutils package to find the peaks. 您可以使用peakutils包来查找峰。 You can set there the threshold and minimum distance between peaks. 您可以设置峰值之间的阈值和最小距离。

import numpy as np
from pylab import *
import scipy.fftpack

# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y1 = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)+ 0.7*np.sin(30.0 * 2.0*np.pi*x)+ 0.5*np.sin(10.0 * 2.0*np.pi*x)
y2 = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)+ 0.2*np.sin(60.0 * 2.0*np.pi*x)+ 0.4*np.sin(40.0 * 2.0*np.pi*x)
yf1 = scipy.fftpack.fft(y1)
yf2 = scipy.fftpack.fft(y2)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)

v1 = 2.0/N * np.abs(yf1[:N/2])
v2 = 2.0/N * np.abs(yf2[:N/2])

# Find peaks
import peakutils
peaks_ind1 = peakutils.indexes(v1, thres=0.2, min_dist=5)
peaks_ind2 = peakutils.indexes(v2, thres=0.2, min_dist=5)

dist_th_for_new_peaks = 3
new_peaks = []
for p in peaks_ind2:
    found_new_peak = np.all(np.abs(p - peaks_ind1) > dist_th_for_new_peaks)
    if found_new_peak:
        new_peaks.append(p)
        print("New Peak!! - %d" % p)

fig, ax = plt.subplots()
plot(xf, v1, color='blue')
plot(xf, v2, color='green')
for p in peaks_ind1:
    ax.scatter(xf[p], v1[p], s=40, marker='s', color='blue', label='v1')
for p in peaks_ind2:
    ax.scatter(xf[p], v2[p], s=40, marker='s', color='green', label='v2')    
for p in new_peaks:
    ax.scatter(xf[p], v2[p], s=40, marker='s', color='red', label='new peaks')        

xlabel('Freq (Hz)',fontsize=16,weight='bold')
ylabel('|Y(freq)|',fontsize=16,weight='bold')

ax = gca()
fontsize = 14
for tick in ax.xaxis.get_major_ticks():
    tick.label1.set_fontsize(fontsize)
    tick.label1.set_fontweight('bold')
for tick in ax.yaxis.get_major_ticks():
    tick.label1.set_fontsize(fontsize)
    tick.label1.set_fontweight('bold')
ax.set_xlim([0,400])
ax.set_ylim([0,0.8])
grid(True)
show()

The red squares are the new peaks that were found in the green signal: 红色方块是绿色信号中的新峰: 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM