使用python查找本地Maxima / minimum

时间:2015-05-06 09:20:44

标签: python numpy

我的代码基于以下评论: Finding local maxima/minima with Numpy in a 1D numpy array

它有效,但它不能为我重现所有的高峰。它总是似乎错过了第一个高峰。有没有办法让这段代码在确定每个峰值时更加健壮?

我尝试过平滑数据,更改顺序等无济于事。

import numpy as np
from scipy.signal import argrelextrema

profile1 = "data posted below"

profile1_filtered = ndimage.filters.gaussian_filter(profile1, 1, mode='nearest')

plot(profile1_filtered[300:740])

# for local maxima
result = argrelextrema(profile1[300:740], np.greater, order =15)

print result

for i in result:
    plot([i,i],[-16380,-16300], color='k', linestyle='--', linewidth=1)

提前感谢您的任何帮助/建议!

当我在工作时,我的上传/下载非常有限。通常我会发布一个链接到我的数据,但我希望这样做: (我将在以后回家的时候用我的数据链接对其进行修改)

array([-16368, -16366, -16365, -16369, -16366, -16369, -16368, -16362,
       -16368, -16366, -16367, -16364, -16367, -16367, -16367, -16366,
       -16363, -16366, -16369, -16370, -16365, -16364, -16362, -16363,
       -16365, -16363, -16364, -16362, -16361, -16362, -16364, -16364,
       -16365, -16361, -16363, -16363, -16366, -16361, -16360, -16363,
       -16362, -16362, -16357, -16360, -16359, -16362, -16359, -16359,
       -16358, -16358, -16355, -16356, -16355, -16349, -16350, -16349,
       -16343, -16338, -16332, -16329, -16329, -16339, -16339, -16348,
       -16351, -16351, -16354, -16355, -16355, -16356, -16357, -16355,
       -16355, -16356, -16356, -16358, -16354, -16355, -16356, -16357,
       -16356, -16355, -16355, -16352, -16357, -16353, -16353, -16352,
       -16357, -16351, -16347, -16347, -16345, -16342, -16332, -16321,
       -16315, -16311, -16315, -16320, -16327, -16335, -16341, -16346,
       -16347, -16349, -16350, -16350, -16347, -16351, -16354, -16354,
       -16352, -16353, -16352, -16354, -16356, -16354, -16358, -16354,
       -16353, -16356, -16351, -16352, -16351, -16351, -16352, -16347,
       -16344, -16344, -16340, -16335, -16326, -16315, -16305, -16299,
       -16303, -16309, -16322, -16331, -16335, -16342, -16342, -16346,
       -16349, -16353, -16352, -16350, -16350, -16350, -16354, -16353,
       -16353, -16355, -16355, -16356, -16353, -16352, -16352, -16356,
       -16354, -16354, -16354, -16353, -16354, -16352, -16355, -16349,
       -16350, -16347, -16346, -16342, -16338, -16334, -16338, -16335,
       -16341, -16344, -16345, -16349, -16349, -16349, -16353, -16353,
       -16353, -16353, -16354, -16353, -16351, -16351, -16352, -16354,
       -16353, -16353, -16353, -16355, -16354, -16352, -16353, -16356,
       -16354, -16353, -16353, -16353, -16352, -16349, -16350, -16347,
       -16345, -16340, -16337, -16331, -16332, -16329, -16337, -16339,
       -16342, -16344, -16345, -16348, -16351, -16351, -16350, -16352,
       -16351, -16346, -16353, -16349, -16350, -16352, -16353, -16349,
       -16352, -16351, -16354, -16352, -16354, -16352, -16354, -16352,
       -16354, -16349, -16348, -16347, -16347, -16343, -16343, -16338,
       -16333, -16330, -16331, -16336, -16340, -16341, -16345, -16346,
       -16347, -16346, -16348, -16352, -16350, -16349, -16351, -16351,
       -16354, -16353, -16353, -16355, -16351, -16352, -16353, -16354,
       -16351, -16353, -16351, -16352, -16349, -16351, -16350, -16351,
       -16352, -16348, -16352, -16349, -16345, -16346, -16341, -16337,
       -16338, -16339, -16342, -16346, -16344, -16348, -16352, -16350,
       -16352, -16353, -16350, -16353, -16356, -16355, -16356, -16354,
       -16352, -16357, -16355, -16356, -16353, -16352, -16354, -16356,
       -16355, -16358, -16352, -16352, -16353, -16351, -16353, -16350,
       -16350, -16349, -16351, -16346, -16344, -16340, -16339, -16338,
       -16339, -16343, -16344, -16345, -16348, -16351, -16350, -16351,
       -16353, -16354, -16353, -16350, -16353, -16354, -16352, -16355,
       -16353, -16356, -16353, -16354, -16356, -16352, -16353, -16355,
       -16353, -16353, -16351, -16352, -16354, -16349, -16349, -16349,
       -16348, -16343, -16340, -16334, -16323, -16322, -16326, -16328,
       -16334, -16343, -16347, -16349, -16348, -16350, -16353, -16353,
       -16354, -16354, -16357, -16356, -16353, -16354, -16358, -16356,
       -16354, -16357, -16357, -16358, -16356, -16357, -16356, -16354,
       -16355, -16356, -16354, -16355, -16353, -16352, -16351, -16351,
       -16346, -16342, -16336, -16333, -16334, -16336, -16342, -16345,
       -16347, -16354, -16354, -16357, -16358, -16359, -16359, -16362,
       -16361, -16360, -16361, -16360, -16363, -16362, -16362, -16364,
       -16363, -16363, -16363, -16363, -16364, -16363, -16364, -16366,
       -16365, -16363, -16362, -16365, -16366, -16364, -16368, -16367])

1 个答案:

答案 0 :(得分:1)

问题似乎源于您的原始数据。第一个峰值与所有其他峰值不同,由两个-16329, -16329,的相同值组成。即使应用高斯滤波器后,这仍然是一个高原而不是高峰。

当您使用np.greater作为比较器时,由于输入数据的性质,它会失败。一个快速的解决方案是使用np.greater_equal作为比较器,但这将两次注册第一个“峰值”。另一种选择可能是检查输入数据的重复值或应用另一个过滤器。

local_maxima = argrelextrema(profile, np.greater_equal, order = 15, mode = 'clip')

enter image description here