我试图找到一个价格序列中仅仅具有左基数的python中峰值突出的累积总和。换句话说,它是撤消冲销或回收的总和。
首先我尝试了以下方法:
from scipy.signal import find_peaks, peak_prominences
import matplotlib.pyplot as plt
x = np.linspace(0, 6 * np.pi, 1000)
x = np.sin(x) + 0.6 * np.sin(2.6 * x)
peaks, _ = find_peaks(x)
prominences = peak_prominences(x, peaks)[0]
prominences
contour_heights = x[peaks] - prominences
plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.vlines(x=peaks, ymin=contour_heights, ymax=x[peaks])
plt.show()
结果和情节是一个很好的起点。但是以下是一些问题:
what i got from the code as a starting point
一旦视觉确认,我需要计算所有峰值的总和(峰值到谷之间的距离)。请帮忙。
答案 0 :(得分:1)
假设我没有误解您的要求(如果可以,请告诉您),这可能会解决您的问题:
您可以使用peak_prominences返回的left_bases
计算“左突出度”:
from scipy.signal import find_peaks, peak_prominences
import matplotlib.pyplot as plt
x = np.linspace(0, 6 * np.pi, 1000)
x = np.sin(x) + 0.6 * np.sin(2.6 * x)
peaks, _ = find_peaks(x)
_, left_bases, _ = peak_prominences(x, peaks)
left_prominences = x[peaks] - x[left_bases]
这应该可以解决您的第一个问题。如果我对您的理解正确,那么您想忽略一个较大峰的左底部之后的峰:
keep = ~(
(left_prominences[:-1] < left_prominences[1:])
& (peaks[:-1] > left_bases[1:])
)
keep = np.concatenate((keep, [True])) # last peak is always kept
peaks = peaks[keep]
left_prominences = left_prominences[keep]
plt.plot(x)
plt.vlines(x=peaks, ymin=x[peaks]-left_prominences, ymax=x[peaks])
最后,总和很容易计算:
np.sum(left_prominences) # returns 8.740800984535955
这种方法应该合理快速。