[英]Finding a local Maxima/minimum using python
我的代码基于以下内容中的注释: 在一维numpy数组中使用Numpy查找局部最大值/最小值
它有效,但是并不能为我再现所有高峰。 它总是似乎错过了第一个高峰。 有什么方法可以使此代码在确定每个峰值时更加健壮?
我尝试过平滑数据,更改顺序等都无济于事。
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])
问题似乎出在您的原始数据中。 与其他峰不同,第一个峰由两次-16329, -16329,
相同的值组成。 即使在应用高斯滤波器之后,它仍然将是平稳的,而不是峰值。
当您使用np.greater
作为比较器时,由于输入数据的性质,它会失败。 一个快速的解决方案是使用np.greater_equal
作为比较器,但这将两次注册第一个“峰值”。 另一个选择是检查输入数据中是否有重复的值或应用另一个过滤器。
local_maxima = argrelextrema(profile, np.greater_equal, order = 15, mode = 'clip')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.