简体   繁体   中英

How to specify the number of peaks in Python

So far I found 4 ways to find peaks in Python, however none of them can specify the number of peaks like Matlab does. Can someone provide some insight?

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)`

Below is the Matlab code that I want to emulate:

[pks,locs] = findpeaks(data,'Npeaks',n)

If you want the exact function Matlab has, why not just use that function? If you have the rest of your data in Python, then you can just use the module provided by Matlab.

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

Try the findpeaks library.

pip install findpeaks

Lets create some peaks:

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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