1.根据极值概念:简单实现
import matplotlib.pyplot as plt
import numpy as np
def nms_3(data):
length=len(data)
ans=[]
for i in range(2,length-2):
if data[i]>data[i-1] and data[i]>data[i+1] :
ans.append(i)
return ans
x = np.array([0, 15, 15, 15, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3,
1, 20, 7, 3, 0 ])
plt.figure(figsize=(6,4))
plt.plot(np.arange(len(x)), x)
point = nms_5(x)
plt.plot(point, x[point], "o")
plt.show()
print(point)
exit()
注: 但是存在一个问题,在极值有左右相同点的时候无法识别峰值
2.使用 scipy.signal 的 argrelextrema 函数(API)
import numpy as np
import pylab as pl
import matplotlib.pyplot as plt
import scipy.signal as signal
x=np.array([
0, 6, 25, 20, 15, 8, 15, 6, 0, 6, 0, -5, -15, -3, 4, 10, 8, 13, 8, 10, 3,
1, 20, 7, 3, 0 ])
plt.figure(figsize=(16,4))
plt.plot(np.arange(len(x)),x)
print x[signal.argrelextrema(x, np.greater)]
print signal.argrelextrema(x, np.greater)
plt.plot(signal.argrelextrema(x,np.greater)[0],x[signal.argrelextrema(x, np.greater)],'o')
plt.plot(signal.argrelextrema(-x,np.greater)[0],x[signal.argrelextrema(-x, np.greater)],'+')
plt.show()
注: 同样利用极值概念原理,窗口可调,同样存在一个问题,在极值有左右相同点的时候无法找到峰值
3. scipy.signal.find_peaks()
文档解析:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html#scipy-signal-find-peaks
find_peaks()算法理解
https://blog.csdn.net/yfl_jybq/article/details/100114952
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import find_peaks
x = np.array([1,1,1,4,1,3,4,7,6,1,3,2,1,9,0,8,8,8,7,1,1,1,6,2,9,9,9])
plt.figure(figsize=(6,4))
plt.plot(np.arange(len(x)), x)
indices = find_peaks(x, height=None, threshold=None, distance=5,
prominence=None, width=None., wlen=None, rel_height=None,
plateau_size=None)
print(indices)
plt.plot(indices[0], x[indices[0]], 'o')
plt.show()
https://blog.csdn.net/yfl_jybq/article/details/100114952
解决水平波进行处理,使得每一个水平波中仅具有一个峰值转向点,各参数可调,首选API
4. scipy.signal.find_peaks_cwt()
官方文档:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks_cwt.html#scipy.signal.find_peaks_cwt
先进行一定的平滑处理
参考:
https://github.com/MonsieurV/py-findpeaks
https://blog.csdn.net/weijifen000/article/details/80070520
https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.argrelmax.html
https://blog.ytotech.com/2015/11/01/findpeaks-in-python/