检测python图中的峰值

时间:2017-06-09 20:34:42

标签: python python-3.x numpy

我的数据文件在以下链接中共享。

我们可以使用以下脚本绘制此数据。

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook

def read_datafile(file_name):

    data = np.loadtxt(file_name, delimiter=',')
    return data

data = read_datafile('mah_data.csv')


fig = plt.figure()

ax1 = fig.add_subplot(111)

ax1.set_title("Data")    
ax1.set_xlabel('t')
ax1.set_ylabel('s')

ax1.plot(x,y, c='r', label='My data')

leg = ax1.legend()

plt.show()

我们如何检测python中的峰值?我在Python中找不到合适的峰值检测算法。

3 个答案:

答案 0 :(得分:2)

尝试使用peakutil(Out-of-Context Hook Functions)。以下是使用peakutil解决问题的方法。

import pandas as pd
import peakutils
data = pd.read_csv("mah_data.csv", header=None)
ts = data[0:10000][1] # Get the second column in the csv file
print(ts[0:10]) # Print the first 10 rows, for quick testing
# check peakutils for all the parameters.
# indices are the index of the points where peaks appear
indices = peakutils.indexes(ts, thres=0.4, min_dist=1000)
print(indices) 

您还应该在scipy(http://pythonhosted.org/PeakUtils/

中查看高峰发现

答案 1 :(得分:2)

您可以使用 scipy.signal 中的 argrelextrema 函数返回数组的局部最大值或局部最小值的索引。这也适用于多维数组,也可以通过指定轴来实现。

from scipy.signal import argrelextrema

ind_max = argrelextrema(z, np.greater) # indices of the local maxima
ind_min = argrelextrema(z, np.lesser)  # indices of the local minima

maxvals = z[ind_max]
minvals = z[ind_min]

更具体地说,可以使用 argrelmax argrelmin 来查找局部最大值或局部最小值。这也适用于使用axis参数的多维数组。

from scipy.signal import argrelmax, argrelmin

ind_max = argrelmax(z, np.greater) # indices of the local maxima
ind_min = argrelmin(z, np.lesser)  # indices of the local minima

maxvals = z[ind_max]
minvals = z[ind_min]

有关详细信息,请参阅此链接:https://docs.scipy.org/doc/scipy/reference/signal.html#peak-finding

答案 2 :(得分:0)

尝试使用findpeaks库。

pip install findpeaks

我找不到附带的数据,但假设该数据是矢量并存储在数据中:

import pandas as pd
data = pd.read_csv("mah_data.csv", header=None).values

# Import library
from findpeaks import findpeaks

# If the resolution of your data is low, I would recommend the ``lookahead`` parameter, and if your data is "bumpy", also the ``smooth`` parameter.
fp = findpeaks(lookahead=1, interpolate=10)
# Find peaks
results = fp.fit(data)
# Make plot
fp.plot()

# Results with respect to original input data.
results['df']

# Results based on interpolated smoothed data.
results['df_interp']