python 寻找离散序列极值点的方法

yipeiwu_com5年前Python基础

使用 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.plot(peakutils.index(-x),x[peakutils.index(-x)],'*')
plt.show()
[25 15 6 10 13 10 20]
(array([ 2, 6, 9, 15, 17, 19, 22]),)

但是存在一个问题,在极值有左右相同点的时候无法识别,但是个人认为在实际的使用过程中极少会出现这种情况,所以可以忽略。

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=(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.less)[0],x[signal.argrelextrema(x, np.less)],'+')
plt.show()
[15 6 10 13 10 20]
(array([ 6, 9, 15, 17, 19, 22]),)

以上这篇python 寻找离散序列极值点的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python创建和删除目录的方法

本文实例讲述了python创建和删除目录的方法。分享给大家供大家参考。具体分析如下: 下面的代码可以先创建一个目录,然后调用自定义的deleteDir函数删除整个目录 #------...

基于python socketserver框架全面解析

基于python socketserver框架全面解析

socketserver框架是一个基本的socket服务器端框架, 使用了threading来处理多个客户端的连接, 使用seletor模块来处理高并发访问, 是值得一看的python...

python的dict,set,list,tuple应用详解

本文深入剖析了python中dict,set,list,tuple应用及对应示例,有助于读者对其概念及原理的掌握。具体如下: 1.字典(dict) dict 用 {} 包围 dict....

PyQt QCombobox设置行高的方法

之前在网上查找了很多相关资料,有说设置icon高度来支持item的,有说要添加自己写指定高度的view来填充的,但是对于一个只有文字的Qcombobox来说,这些无疑是太过繁琐了。研究了...

python实现目录树生成示例

复制代码 代码如下:#!/usr/bin/env python# -*- coding: utf-8 -*-import osimport optparse LOCATION_NONE&...