fmd特征模态分解 python
时间: 2025-05-20 17:10:35 浏览: 58
### 特征模态分解(FMD)的Python实现
特征模态分解(Feature Mode Decomposition, FMD)是一种用于处理一维信号的新方法,能够有效地将复杂信号分解为若干个本征模态分量(Intrinsic Mode Function, IMF),从而便于进一步分析和建模。以下是基于已有资源[^1]中的Python实现方式。
#### 实现步骤概述
通过调用开源库或编写自定义代码来完成FMD算法的核心逻辑。以下是一个完整的示例代码:
```python
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
def emd(signal, t=None, max_imfs=10, sd_threshold=0.1):
"""
经验模态分解 (EMD),作为FMD的基础部分。
参数:
signal: 输入的一维信号数组
t: 时间轴,默认为 None,则自动创建
max_imfs: 提取的最大IMF数量
sd_threshold: 停止条件阈值
返回:
imfs: 分解后的IMF列表
"""
if t is None:
t = np.arange(len(signal))
residual = signal.copy()
imfs = []
for _ in range(max_imfs):
prev_res = residual.copy()
h = residual
while True:
s_max, s_min = find_extrema(t, h)
top = cubic_spline_interpolation(t, s_max)
bottom = cubic_spline_interpolation(t, s_min)
mean_env = (top + bottom) / 2
h_prev = h.copy()
h = h - mean_env
# 计算停止准则
sd = calculate_sd(h_prev, h)
if sd < sd_threshold:
break
imfs.append(residual - h)
residual = h
if len(imfs) >= 2 and np.linalg.norm(residual) < sd_threshold * np.linalg.norm(prev_res):
break
imfs.append(residual)
return np.array(imfs)
def find_extrema(x, y):
""" 找到局部最大值和最小值的位置及其对应的y值"""
peaks = np.r_[True, y[1:] > y[:-1]] & np.r_[y[:-1] > y[1:], True]
troughs = np.r_[True, y[1:] < y[:-1]] & np.r_[y[:-1] < y[1:], True]
peak_x = x[peaks]
peak_y = y[peaks]
trough_x = x[troughs]
trough_y = y[troughs]
return (peak_x, peak_y), (trough_x, trough_y)
def cubic_spline_interpolation(x, extrema_points):
""" 使用三次样条插值拟合包络线"""
if len(extrema_points[0]) == 0 or len(extrema_points[1]) == 0:
return np.zeros_like(x)
f_interp = interp1d(np.concatenate((extrema_points[0], extrema_points[1])),
np.concatenate((extrema_points[1], extrema_points[0])), kind='cubic', fill_value="extrapolate")
return f_interp(x)
def calculate_sd(s1, s2):
""" 计算两个连续迭代之间的标准差SD"""
norm_sq_diff = np.sum((s1 - s2)**2)
denom = np.sqrt(np.sum(s1**2)) + np.sqrt(np.sum(s2**2))
if denom != 0:
return np.sqrt(norm_sq_diff) / denom
else:
return 0
# 测试数据
t = np.linspace(0, 1, 500)
signal = np.cos(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 20 * t) + 0.5 * np.random.randn(len(t))
imfs = emd(signal, t=t, max_imfs=10, sd_threshold=0.1)
plt.figure(figsize=(10, 6))
for i, imf in enumerate(imfs[:-1]):
plt.subplot(len(imfs), 1, i+1)
plt.plot(t, imf, label=f'IMF {i+1}')
plt.legend()
plt.subplot(len(imfs), 1, len(imfs))
plt.plot(t, imfs[-1], label='Residue')
plt.legend()
plt.show()
```
上述代码实现了经验模态分解(EMD),这是FMD的一个重要组成部分。具体来说,它利用了极值点提取、三次样条插值以及停机准则计算等功能模块。
---
#### 关键技术细节说明
1. **极值点检测**: `find_extrema` 函数负责找到输入信号的所有局部极大值和极小值位置,并返回其坐标集合。
2. **三次样条插值**: 利用 SciPy 的 `interp1d` 方法构建上下包络曲线,确保平滑性和准确性。
3. **停止准则**: 定义了一个标准化差异指标 \( SD \)[^2] 来判断当前迭代是否收敛至稳定状态。
4. **多层分解**: 将原始信号逐步分离成多个 IMF 和残余项,直到满足预设条件为止。
此版本仅提供基础框架;对于更复杂的场景或者更高精度需求,可以引入其他优化策略如遗传算法(GA) 或灰狼优化器(GWO)[^3] 对超参数进行微调。
---
阅读全文
相关推荐

















