import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
# 建立函数
t = np.linspace(0, 10*np.pi, 1000)
data = np.sin(t) + 0.5*np.sin(5*t)
# 反转函数
data1 = -np.sin(t) - 0.5*np.sin(5*t)
# 查找波峰和波谷
peaks, _ = find_peaks(data)
peaks1, _ = find_peaks(data1)
# 输出结果
print("波峰出现的时间:", t[peaks])
print("波谷出现的时间:", t[peaks1])
# 定义颜色变量
color = ['c', 'b', 'g', 'r', 'm', 'y', 'k', 'w']
# 绘图
# plt.scatter(t, data, c=color[1], edgecolors='r')
plt.scatter(t, data1, c=color[1], edgecolors='r')
plt.show()
找了一小时,scipy.signal.find_peaks只有峰值,没有峰谷监测,只好手动改下曲线方向...凑合着用