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实现的本地文件搜索功能示例【测试可用】

本文实例讲述了Python实现的本地文件搜索功能。分享给大家供大家参考,具体如下: 偶尔需要搜索指定文件,不想每次都在windows下面去搜索,想用代码来实现搜索,而且能够收集搜索结果,...

跟老齐学Python之编写类之四再论继承

跟老齐学Python之编写类之四再论继承

在上一讲代码的基础上,做进一步修改,成为了如下程序,请看官研习这个程序: 复制代码 代码如下: #!/usr/bin/env python #coding:utf-8 class Per...

python 3调用百度OCR API实现剪贴板文字识别

本程序调用百度OCR API对剪贴板的图片文字识别,配合CaptureScreen软件,可快速识别文字。 #!python3 import urllib.request, urlli...

Python自动连接ssh的方法

本文实例讲述了Python自动连接ssh的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/python #-*- coding:utf-8 -*- import...

Python简单操作sqlite3的方法示例

本文实例讲述了Python简单操作sqlite3的方法。分享给大家供大家参考,具体如下: import sqlite3 def Test1(): #con =sqlite3.co...