我试图用小波变换压缩时间序列,我看到它可以平滑信号,但我只想得到最重要的点及其位置并删除其余的,但我不知道一旦我有了小波系数我该如何压缩信号,点数更少
import matplotlib.pyplot as plt
plt.plot(noisy_signal)
plt.title("Noisy Signal")
coeffs = pywt.wavedec(noisy_signal, 'db2', level=2)
threshold = 0.5
coeffs_thresholded = [pywt.threshold(c, threshold, mode='soft') for c in coeffs]
# Reconstruct the signal from the thresholded coefficients
denoised_signal = pywt.waverec(coeffs_thresholded, 'db2')
plt.plot(denoised_signal)
“coeffs_thresholded”列表的长度将与“coeffs”列表的长度相同,因为您正在对“coeffs”中的每个系数应用阈值。该代码不是删除高于阈值的系数,而是用默认值 0 的替代值替换它们。(检查文档)
首先,验证 coeffs 列表中是否有低于阈值的值。然后你就可以做
coeffs_thresholded = [pywt.threshold(c, threshold, mode='soft') if pywt.threshold(c, threshold, mode='soft') > 0 for c in coeffs]