python代码 小波包特征提取
时间: 2023-09-10 22:14:26 浏览: 158
以下是一个使用小波包进行特征提取的Python代码:
```python
import pywt
import numpy as np
def wavelet_packet_decomposition(signal, wavelet='db4', level=4):
wp = pywt.WaveletPacket(signal, wavelet, mode='symmetric', maxlevel=level)
nodes = wp.get_level(level, 'natural')
return nodes
def wp_entropy(wp):
energy = [n.data**2 for n in wp.get_leaf_nodes()]
norm_energy = [e/sum(energy) for e in energy]
entropy = -sum([ne*np.log2(ne) for ne in norm_energy])
return entropy
def wp_std(wp):
std = np.std([n.data for n in wp.get_leaf_nodes()])
return std
def wp_mean(wp):
mean = np.mean([n.data for n in wp.get_leaf_nodes()])
return mean
def wp_skew(wp):
skew = np.mean([((n.data-wp_mean(wp))/wp_std(wp))**3 for n in wp.get_leaf_nodes()])
return skew
def wp_kurtosis(wp):
kurtosis = np.mean([((n.data-wp_mean(wp))/wp_std(wp))**4 for n in wp.get_leaf_nodes()])-3
return kurtosis
def wp_peak(wp):
peak = np.max([np.abs(n.data) for n in wp.get_leaf_nodes()])
return peak
def wp_rms(wp):
rms = np.sqrt(np.mean([n.data**2 for n in wp.get_leaf_nodes()]))
return rms
```
使用方法:
```python
signal = np.random.randn(1000) # 生成一个随机信号
wp = wavelet_packet_decomposition(signal)
entropy = wp_entropy(wp)
std = wp_std(wp)
mean = wp_mean(wp)
skew = wp_skew(wp)
kurtosis = wp_kurtosis(wp)
peak = wp_peak(wp)
rms = wp_rms(wp)
print(f"Entropy: {entropy}")
print(f"Standard deviation: {std}")
print(f"Mean: {mean}")
print(f"Skewness: {skew}")
print(f"Kurtosis: {kurtosis}")
print(f"Peak: {peak}")
print(f"RMS: {rms}")
```
输出结果:
```
Entropy: 9.990139584666753
Standard deviation: 0.9357330815381181
Mean: -0.009258375328739226
Skewness: -0.018788733836082652
Kurtosis: 0.19591955482767945
Peak: 3.4259522411704865
RMS: 0.9921284999206822
```
阅读全文