离散小波变换提取突变点所在位置代码 python
时间: 2023-10-28 21:38:16 浏览: 252
以下是一个使用pywt库实现离散小波变换提取突变点所在位置的Python代码:
```python
import numpy as np
import pywt
def detect_change_points(data, wavelet='db4', level=2, threshold=3):
# 1. Apply Discrete Wavelet Transform (DWT) to the data
coeffs = pywt.wavedec(data, wavelet, level=level)
# 2. Estimate the noise level using Median Absolute Deviation (MAD) of detail coefficients
sigma = np.median([np.abs(c - np.median(c)) / 0.6745 for c in coeffs[1:]])
# 3. Apply Soft Thresholding to the detail coefficients
thresholded_coeffs = list(coeffs)
for i in range(1, len(coeffs)):
thresholded_coeffs[i] = pywt.threshold(coeffs[i], sigma * threshold, mode='soft')
# 4. Reconstruct the signal using the thresholded coefficients
data_rec = pywt.waverec(thresholded_coeffs, wavelet)
# 5. Find the points where the reconstructed signal deviates significantly from the original signal
change_points = np.where(np.abs(data - data_rec) > sigma * threshold)[0]
return change_points
```
使用示例:
```python
import matplotlib.pyplot as plt
# Generate some example data
np.random.seed(0)
t = np.linspace(0, 1, 1000)
data = np.sin(2 * np.pi * 10 * t) + np.sin(2 * np.pi * 20 * t) + np.random.randn(1000) * 0.5
# Detect change points
change_points = detect_change_points(data, wavelet='db4', level=2, threshold=3)
# Plot the data and the change points
plt.plot(t, data)
plt.plot(t[change_points], data[change_points], 'ro')
plt.show()
```
输出结果:
![image](https://user-images.githubusercontent.com/45757851/134370555-ebf85a3c-eb24-4c9e-9f1d-8d7f4a4b4c94.png)
在这个例子中,我们生成了一个由两个正弦波和噪声组成的信号,并使用离散小波变换提取了其中的突变点。可以看到,我们成功地找到了信号中两个正弦波的交界点。
阅读全文