python求极值点主要用到scipy库。

1. 首先可先选择一个函数或者拟合一个函数,这里选择拟合数据:np.polyfit

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal #滤波等 xxx = np.arange(0, 1000)
yyy = np.sin(xxx*np.pi/180) z1 = np.polyfit(xxx, yyy, 7) # 用7次多项式拟合
p1 = np.poly1d(z1) #多项式系数
print(p1) # 在屏幕上打印拟合多项式
yvals=p1(xxx) plt.plot(xxx, yyy, '*',label='original values')
plt.plot(xxx, yvals, 'r',label='polyfit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4)
plt.title('polyfitting')
plt.show()

得到的图形是:

2. 求波峰值,也就是极大值,得到:signal.find_peaks,官方文档:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html#scipy-signal-find-peaks。

scipy.signal.find_peaks(x, height=None, threshold=None, distance=None, prominence=None, width=None, wlen=None, rel_height=0.5, plateau_size=None)

Parameters:
x : sequence. A signal with peaks.
height : number or ndarray or sequence, optional. Required height of peaks. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied, as the maximal required height.
threshold : number or ndarray or sequence, optional. Required threshold of peaks, the vertical distance to its neighbouring samples. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied, as the maximal required threshold.
distance : number, optional. Required minimal horizontal distance (>= 1) in samples between neighbouring peaks. Smaller peaks are removed first until the condition is fulfilled for all remaining peaks.
prominence : number or ndarray or sequence, optional. Required prominence of peaks. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied, as the maximal required prominence.
width : number or ndarray or sequence, optional. Required width of peaks in samples. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied, as the maximal required width.
wlen : int, optional. Used for calculation of the peaks prominences, thus it is only used if one of the arguments prominence or width is given. See argument wlen in peak_prominences for a full description of its effects.
rel_height : float, optional. Used for calculation of the peaks width, thus it is only used if width is given. See argument rel_height in peak_widths for a full description of its effects.
plateau_size : number or ndarray or sequence, optional. Required size of the flat top of peaks in samples. Either a number, None, an array matching x or a 2-element sequence of the former. The first element is always interpreted as the minimal and the second, if supplied as the maximal required plateau size.
New in version 1.2.0.
# 极值
num_peak_3 = signal.find_peaks(yvals, distance=10) #distance表极大值点的距离至少大于等于10个水平单位
print(num_peak_3[0])
print('the number of peaks is ' + str(len(num_peak_3[0])))
plt.plot(xxx, yyy, '*',label='original values')
plt.plot(xxx, yvals, 'r',label='polyfit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4)
plt.title('polyfitting')
for ii in range(len(num_peak_3[0])):
plt.plot(num_peak_3[0][ii], yvals[num_peak_3[0][ii]],'*',markersize=10)
plt.show()

3. 在可导的情形下,可以求导来求极值点,同时得到极大值和极小值点:np.polyder

yyyd = np.polyder(p1,1) # 1表示一阶导
print(yyyd)

此时:yyyd.r 即可就得导数为0的点,可以与上述的极大值点对应比较

4. 直接函数分别求极大值和极小值:signal.argrelextrema 函数

print(yvals[signal.argrelextrema(yvals, np.greater)]) #极大值的y轴, yvals为要求极值的序列
print(signal.argrelextrema(yvals, np.greater)) #极大值的x轴
peak_ind = signal.argrelextrema(yvals,np.greater)[0] #极大值点,改为np.less即可得到极小值点
plt.plot(xxx, yyy, '*',label='original values')
plt.plot(xxx, yvals, 'r',label='polyfit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4)
plt.title('polyfitting')
plt.plot(signal.argrelextrema(yvals,np.greater)[0],yvals[signal.argrelextrema(yvals, np.greater)],'o', markersize=10) #极大值点
plt.plot(signal.argrelextrema(yvals,np.less)[0],yvals[signal.argrelextrema(yvals, np.less)],'+', markersize=10) #极小值点
plt.show()

## ----- end ------

python求极值点(波峰波谷)的更多相关文章

  1. 使用python求字符串或文件的MD5

    使用python求字符串或文件的MD5 五月 21st, 2008 #以下可在python3000运行. #字符串md5,用你的字符串代替'字符串'中的内容. import hashlib md5=h ...

  2. python求微分方程组的数值解曲线01

    本人最近在写一篇关于神经网络同步的文章,其一部分模型为: x_i^{\Delta}(t)= -a_i*x_i(t)+ b_i* f(x_i(t))+ \sum\limits_{j \in\{i-1, ...

  3. Python 求点到直线的垂足

    Python 求点到直线的垂足 在已知一个点,和一条已知两个点的直线的情况下 运算公式参考链接:https://www.cnblogs.com/mazhenyu/p/3508735.html def ...

  4. gc图波峰波谷一直上升问题

    垃圾回收曲线,波峰和波谷一直上升.正常是波峰波谷在同一水平线上,可以想象如果程序继续运行下去,老年代内存回收后也不断上升,当达到老年代满了的时候,就会报内存溢出错误. 用jmap -histo pid ...

  5. python求100以内素数

    python求100以内素数之和 from math import sqrt # 使用isPrime函数 def isPrime(n): if n <= 1: return False for ...

  6. Python 求两个文本文件以行为单位的交集 并集 差集

    Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...

  7. Faas 典型场景——应用负载有显著的波峰波谷,典型用例-基于事件的数据处理

    Serverless适用的两大场景 场景一:应用负载有显著的波峰波谷 Serverless化与否的评判标准并不是公司规模的大小,而是其业务背后的具体技术问题,比如业务波峰波谷明显,如何实现削峰填谷.一 ...

  8. Python求一个数字列表的元素总和

    Python求一个数字列表的元素总和.练手: 第一种方法,直接sum(list): 1 lst = list(range(1,11)) #创建一个1-10的数字列表 2 total = 0 #初始化总 ...

  9. python 求MD5值

    (一)求字符串的MD5值 import hashlib #导入功能模块,此模块有MD5,SHA1,SHA256等方法 m = hashlib.md5() #声明一个对象 m.update(b'hell ...

随机推荐

  1. vue搭建手顺

    1.环境准备:node.js  vue-cli: $ npm install vue-cli -g 2.建立项目目录:vuedemo,并cd到该目录下 3.$ vue init webpack  vu ...

  2. 0.96寸OLED显示屏驱动手册(SSD1306)

    MCU IIC接口 IIC通信接口由从地址位SA0,IIC总线数据信号SDA(输出SDAout/D2和输入SDAin /D1)和IIC总线时钟信号SCL(D0).不管是数据线还是时钟线都需要连接上拉电 ...

  3. 手把手教你如何构建Vue前端组件库

    在前端开发中可能会遇到将相同的功能模板集合成一个组件,供他人调用,这样可以减少重复造轮子,也可以节约人力.财力,更能够提高代码的可维护度:下面将通过详细的步骤教你如何构建一个Vue前端组件. 1.在本 ...

  4. 个性化和云端孤岛困扰SaaS用户,低代码PaaS或成解决之道 ZT

    近日,中国软件行业协会.中国软件网联合阿里云推出了<2020中国SaaS产业十大趋势>,其中明确指出企业软件SaaS化是大势所趋,但个性化和云端孤岛成为2020年SaaS用户关注的两大问题 ...

  5. 关于MY Sql 查询锁表信息和解锁表

    1.查询锁住表信息 show OPEN TABLES where In_use > 0; 2.查看进程  show processlist; 3.解开锁住的表 需要杀掉锁住表的相关进程Id. k ...

  6. JUnit套件测试(共通类测试)

    @RunWith(Suite.class)@Suite.SuiteClasses({ TestClass1.class, TestClass2.class })public class SuiteTe ...

  7. 智能家居为MCU带来巨大需求量

    新一代年轻消费族群对于生活品质的需求逐渐提高,不仅小米要发展智能家居,中兴通讯也在于近日在北京揭晓智"智能家居"将成为市场主流,而智能家居的崛起也必然引爆MCU的需求量迅速攀升,众 ...

  8. 最小生成树算法总结(Kruskal,Prim)

    今天复习最小生成树算法. 最小生成树指的是在一个图中选择n-1条边将所有n个顶点连起来,且n-1条边的权值之和最小.形象一点说就是找出一条路线遍历完所有点,不能形成回路且总路程最短. Kurskal算 ...

  9. Math, Date,JSON对象

    Math 对象 Math是 JavaScript 的原生对象,提供各种数学功能.该对象不是构造函数,不能生成实例,所有的属性和方法都必须在Math对象上调用. 静态属性 Math对象的静态属性,提供以 ...

  10. GitKraken 快速配置 SSH Key

    快速使用 GitKraken 配置SSH keys git是现在最流行的版本管理工具,应用范围非常广泛,推荐一款git的可视化工具,这款 工具特别方便 它的官方如下https://www.gitkra ...