import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
# 生成示例信号
x = np.array([1, 3, 7, 1, 2, 6, 0, 4, 3, 2, 5, 1])
# 寻找波峰
peaks, _ = find_peaks(x)
# 寻找波谷(使用信号的负数形式)
valleys, _ = find_peaks(-x)
# 绘制信号
plt.plot(x)
plt.xlabel('Sample')
plt.ylabel('Amplitude')
# 标记波峰
plt.plot(peaks, x[peaks], 'x', color='r', markersize=10, label='Peaks')
# 标记波谷
plt.plot(valleys, x[valleys], 'o', color='g', markersize=10, label='Valleys')
plt.legend()
plt.show()