使用python查找本地最大值/最小值。

时间:2022-08-22 13:03:49

My code is based on a comment in: Finding local maxima/minima with Numpy in a 1D numpy array

我的代码基于一个注释:在1D Numpy数组中查找本地maxima/minima和Numpy。

It works, however it does not reproduce all the peaks for me. It always seems to miss the first peak. Is there any way to make this code a bit more robust in determining each peak?

它是有效的,然而它并没有为我复制所有的高峰。它似乎总是错过第一个高峰。有没有什么方法可以让这个代码在确定每个峰值时更加健壮?

I have tried smoothing the data, changing the order etc to no avail.

我已经尝试过平滑数据,修改订单等等,但都没有效果。

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)

Thanks in advance for any help/advice!

感谢您的帮助/建议!

As I am in work, my upload/download is very restricted. Usually I would post a link to my data, however I hope this will do: (i will amend this with a link to my data when I get home later)

当我在工作的时候,我的上传/下载是非常受限的。通常我会将一个链接发布到我的数据上,但是我希望这能做到:(当我稍后回家时,我将修改它与我的数据链接)

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 个解决方案

#1


1  

The problem seems to be originating in your original data. The first peak, unlike all the other ones, consists of the same value twice -16329, -16329,. Even after applying the Gauss filter this will still be a plateau rather than a peak.

问题似乎源自你的原始数据。第一个峰,不像其他的峰,由相同的值组成,它的值是-16329,-16329。即使应用了高斯滤波器,这仍然是一个平台,而不是一个峰值。

When you use np.greater as a comparator it fails due to the nature of your input data. A quick solution would be to use np.greater_equal as comparator, but this will register the first 'peak' twice. Another option might be to check your input data for repeated values or apply another filter.

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

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

使用python查找本地最大值/最小值。

#1


1  

The problem seems to be originating in your original data. The first peak, unlike all the other ones, consists of the same value twice -16329, -16329,. Even after applying the Gauss filter this will still be a plateau rather than a peak.

问题似乎源自你的原始数据。第一个峰,不像其他的峰,由相同的值组成,它的值是-16329,-16329。即使应用了高斯滤波器,这仍然是一个平台,而不是一个峰值。

When you use np.greater as a comparator it fails due to the nature of your input data. A quick solution would be to use np.greater_equal as comparator, but this will register the first 'peak' twice. Another option might be to check your input data for repeated values or apply another filter.

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

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

使用python查找本地最大值/最小值。