ceemdan分解的python代码
时间: 2024-02-18 21:58:21 浏览: 319
CEEMDAN(Complete Ensemble Empirical Mode Decomposition with Adaptive Noise)是一种信号分解方法,用于将非线性和非平稳信号分解成一组本征模态函数(Intrinsic Mode Functions,简称IMFs)。以下是一个简单的CEEMDAN分解的Python代码示例:
```python
import numpy as np
import matplotlib.pyplot as plt
def ceemdan(signal, num_sifts, ensemble_size):
imfs = []
for _ in range(ensemble_size):
ensemble = signal + np.random.randn(len(signal))
imf = np.zeros_like(signal)
for _ in range(num_sifts):
imf = sift(ensemble)
ensemble -= imf
imfs.append(imf)
signal -= imf
return imfs
def sift(signal):
max_sifts = 100
sift_eps = 0.3
imf = np.zeros_like(signal)
while True:
maxima, minima = find_extrema(signal)
mean_env = (maxima + minima) / 2
sifted = signal - mean_env
if np.sum(np.abs(sifted)) < sift_eps or max_sifts == 0:
imf += sifted
break
else:
signal = sifted
max_sifts -= 1
imf += sifted
return imf
def find_extrema(signal):
maxima = []
minima = []
for i in range(1, len(signal)-1):
if (signal[i] > signal[i-1] and signal[i] > signal[i+1]) or (signal[i] < signal[i-1] and signal[i] < signal[i+1]):
maxima.append(i)
elif (signal[i] < signal[i-1] and signal[i] < signal[i+1]) or (signal[i] > signal[i-1] and signal[i] > signal[i+1]):
minima.append(i)
return np.array(maxima), np.array(minima)
# 示例用法
signal = np.random.randn(1000) # 输入信号
num_sifts = 5 # SIFT迭代次数
ensemble_size = 10 # 集成数
imfs = ceemdan(signal, num_sifts, ensemble_size)
# 绘制结果
plt.figure(figsize=(10, 6))
plt.subplot(len(imfs)+1, 1, 1)
plt.plot(signal)
plt.title('Original Signal')
for i, imf in enumerate(imfs):
plt.subplot(len(imfs)+1, 1, i+2)
plt.plot(imf)
plt.title(f'IMF {i+1}')
plt.tight_layout()
plt.show()
```
这段代码实现了CEEMDAN信号分解算法。首先定义了`ceemdan`函数,它接受输入信号、SIFT迭代次数和集成数作为参数,并返回一组IMFs。然后定义了`sift`函数,它是SIFT(Sifting Process)的具体实现。最后,定义了`find_extrema`函数,用于寻找信号的极值点。
示例用法中,我们生成了一个随机信号,并使用CEEMDAN算法将其分解成5个IMFs。然后使用Matplotlib库绘制了原始信号和每个IMF的图像。
阅读全文