到目前为止,我找到了4种在Python中找到峰值的方法,但是没有一种能像Matlab那样指定峰值数量。有人可以提供一些见解吗?
import scipy.signal as sg
import numpy as np
# Method 1
sg.find_peaks_cwt(vector, np.arange(1,4),max_distances=np.arange(1, 4)*2)
# Method 2
sg.argrelextrema(np.array(vector),comparator=np.greater,order=2)
# Method 3
sg.find_peaks(vector, height=7, distance=2.1)
# Method 4
detect_peaks.detect_peaks(vector, mph=7, mpd=2)`
以下是我想要模仿的Matlab代码:
[pks,locs] = findpeaks(data,'Npeaks',n)
答案 0 :(得分:1)
如果你想要Matlab的精确功能,为什么不直接使用该功能呢?如果您在Python中拥有其余数据,那么您可以使用Matlab提供的module。
import matlab.engine #import matlab engine
eng = matlab.engine.start_matlab() #Start matlab engine
a = a = [(0.1*i)*(0.1*i-1)*(0.1*i-2) for i in range(50)] #Create some data with peaks
b = eng.findpeaks(matlab.double(a),'Npeaks',1) #Find 1 peak
答案 1 :(得分:0)
尝试使用findpeaks
库。
pip install findpeaks
让我们创造一些高峰:
i = 10000
xs = np.linspace(0,3.7*np.pi,i)
X = (0.3*np.sin(xs) + np.sin(1.3 * xs) + 0.9 * np.sin(4.2 * xs) + 0.06 *
np.random.randn(i))
# import library
from findpeaks import findpeaks
# Initialize
fp = findpeaks()
# Find the peaks (high/low)
results = fp.fit(X)
# Make plot
fp.plot()
# Some of the results:
results['df']