赞
踩
采用小波分解方式对数据进行分解,然后重构,演示整体代码
import pywt
import matplotlib.pyplot as plt
import pandas as pd
sampleData = [ 6. , 7.2, 5.4, 6. , 4.8, 4.6, 6.7, 7.1, 6.8, 0. , 5.2,
6.4, 4.8, 5. , 7.8, 6.4, 3.1, 3.6, 4.7, 5.5, 7.1, 6. ,
8.2, 7.2, 6.5, 9.3, 6.8, 8.4, 7.4, 6.7, 8.6, 5.4, 4.1,
3.8, 5.6, 4.1, 7.3, 6.7, 6.5, 7.6, 8.6, 4.3, 6.2, 7.7,
7.2, 6.9, 5.1, 4.6, 6.3, 5.2, 6. , 6.1, 6.2, 7.7, 8.1,
6.9, 6.4, 6.4, 6.5, 6.8, 5.6, 5.2, 8.9, 5.4, 3.8, 5.9,
5.8, 4.9, 4.3, 8.5, 6.2, 3.1, 3.9, 2.4, 7.3, 5.8, 7.5,
7.3, 6. , 7.1, 7. , 7.3, 7.5, 4.6, 5.7, 4. , 7.6, 5.5,
3.8, 7. , 4.2, 4.3, 5.7, 6.2, 6.9, 6.1, 5.5, 9.5, 8.9,
6.1, 7. , 8.8, 4.8, 7.6, 9.5, 7.6, 7. , 4.6, 8.4, 5.4,
7.9, 9.2, 5.8, 10.2, 8.9, 5.8, 7.2, 8.9, 6.8, 8.8, 7.7,
6.4, 8. , 10.6, 7.9, 9.4, 5.8, 7.2, 7.1, 9.2, 7.3, 10.3,
3.1, 9.8, 6.8, 11.4, 8.1, 7.7, 7.5, 7.0]
w = pywt.Wavelet('db8') # 选用Daubechies8小波
maxlev = pywt.dwt_max_level(len(sampleData), w.dec_len)
print("maxlev: ", maxlev)
threshold = 0.05 # Threshold for filtering
# 将信号进行小波分解
for i in range(1, len(coeffs)):
# 将噪声滤波
sampleData_rec = pywt.waverec(coeffs, 'db8') # 将信号进行小波重构
# 保存
dataframe = pd.DataFrame(sampleData_rec)
dataframe.to_csv(r"imf-1_rec.csv",index=False,sep=',')
plt.plot(sampleData,"b",label='true_data')
plt.plot(sampleData_rec,"r",label='rec_data')
print(len(sampleData_rec))
maxlev: 3
(30,)
77
[array([14.89060208, 17.48774106, 14.62732431, 14.16111973, 17.39674364,
15.23687513, 13.66198872, 16.98478131, 16.04582462, 13.46882215,
16.5251981 , 16.84176687, 13.05983399, 18.43398009, 18.69129045,
18.13412361, 17.22175699, 18.33764118, 18.91146961, 13.79601851,
18.22412343, 16.31726674, 16.19902123, 20.36499062, 20.34979924,
22.27118849, 22.1946934 , 22.49473075, 22.026928 , 21.37043685]), array([ 0.02597377, 0.04627224, 0.83377698, -0.4094727 , -0.66537233,
0.9654267 , 2.37728835, 1.73186496, -0.13460124, 4.2582881 ,
-1.6431639 , 3.08247987, 0.06009821, 1.84186106, 1.99219538,
-0.86047581, -0.26259531, -2.15543405, 0.12983753, -2.04923737,
0.45461431, 0.26755979, -1.97147193, -1.14392232, 2.75158527,
0.496431 , -0.76652795, 0.97028847, 1.45129 , 0.42297384]), array([-1.53165283, 3.90167297, 1.0713417 , -0.88960218, -0.93377672,
-4.37583791, -0.28086216, -2.60909404, -0.02110962, 0.34448458,
-0.49989578, -1.12639543, -0.15444765, -0.4400528 , 0.82299644,
1.48393952, -0.32030378, -0.76129654, -1.01577166, -1.11048871,
0.96183349, -0.99455448, -1.64585353, 2.25273347, -1.06781376,
-0.76168693, 1.67411943, 0.47655085, 0.43817838, 1.63985866,
1.33641088, -0.74595449, 0.96958856, -2.15826138, 0.03824809,
2.0088925 , -2.82540503, -1.50755191, 1.44302522, -0.7894779 ,
-0.10165996, 2.97673399, 0.17404927, -2.01625666, 1.99990472,
0.66159429]), array([ 0.34482481, -1.29156053, -0.47973075, 0.52203713, 0.51414751,
3.92438116, -1.26310503, 0.85073165, 0.07343796, -0.97518959,
0.9308112 , 1.19327555, 0.06607814, -2.22254163, -0.62430618,
0.99976817, 1.20568031, -0.24606229, 2.19671547, 0.0838348 ,
-0.21327042, 2.68511782, -1.43872316, 0.19455079, 0.09690823,
1.17238324, -0.38030448, -0.36054123, 0.6819401 , -0.47211671,
-0.15042381, 1.07032598, 1.58819977, -2.1150514 , 0.9529688 ,
-2.83481343, 2.23710703, 1.26627991, 1.99366416, -0.81516264,
-0.65308376, 0.41792825, 1.53076635, 1.39865169, 0.74781752,
-3.02966721, 0.85346178, 0.10372268, 0.09628719, -2.0333159 ,
2.13431488, -2.70836621, -0.55006196, 1.67096465, 1.23637851,
2.42158039, -2.04923712, -2.270118 , 2.33425608, -2.02102422,
-0.78420639, 0.89368156, -1.85879552, -1.61098924, -1.07499289,
-1.01434951, -3.61867498, -4.44962534, -1.56543207, -0.0356764 ,
0.12405043, -0.15293619, 1.03965714, 3.61517855, 4.13996129,
2.10191932, 0.57358088])]
140
需要源代码和数据集,或者想要沟通交流,请私聊,谢谢.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。