基于不同小波族的信号降噪方法(Python)

B站影视 2025-01-11 12:31 2

摘要:import numpy as npimport sysimport ossys.path.append('../')from Wavelet_1D import Wavelet_1Dimport matplotlib.pyplot as pltSAVE_OU

import numpy as npimport sysimport ossys.path.append('../')from Wavelet_1D import Wavelet_1Dimport matplotlib.pyplot as pltSAVE_OUNTPUTS = Falseif SAVE_OUNTPUTS:OUTPUT_FORMAT = '.pdf'OUTPUT_DIR = './Outputs'if not os.path.isdir(OUTPUT_DIR):os.mkdir(OUTPUT_DIR)plt.rc('text', usetex=True)plt.rc('font', family='serif')# X = np.arange(0, 512, 1).reshape(-1, 1)# Y = 2*(X/100)**2 + np.sin(4*X/100)*10 + np.cos(X/100**2)**2; Y = Y.reshape(-1, 1)X = np.linspace(0, 1, 512).reshape(-1, 1)Y = np.sin(2*np.pi*(0.5 +5*X)*X)*25# X = np.linspace(-4, 4, 512).reshape(-1, 1)# Y = 50/(1+X**2)noise = np.random.normal(loc=0, scale=1, size=len(X)).reshape(-1, 1)Yn = Y + noiseplt.figure(figsize=(10,4))plt.plot(X, Yn, color='r', label='Signal With Noise')plt.xlabel('Time'); plt.ylabel('Amplitude')plt.plot(X, Y, color='k', label='Main Signal')plt.gridplt.legendif SAVE_OUNTPUTS:plt.savefig(OUTPUT_DIR+'/1D_Signal'+OUTPUT_FORMAT, bbox_inches='tight')plt.showMAX_LEVEL = 5S_haar_n, D_haar_n = Wavelet_1D(Yn, 'Haar', MAX_LEVEL)S_db4_n, D_db4_n = Wavelet_1D(Yn, 'Db4', MAX_LEVEL)S_db6_n, D_db6_n = Wavelet_1D(Yn, 'Db6', MAX_LEVEL)S_mh_n, D_mh_n = Wavelet_1D(Yn, 'MexicanHat', MAX_LEVEL)S_sym2_n, D_sym2_n = Wavelet_1D(Yn, 'sym2', MAX_LEVEL)import matplotlib.pyplot as pltplt.rc('text', usetex=True)plt.rc('font', family='serif')# %matplotlib tkn = len(S_haar_n)plt.figure(figsize=(13,14))for i in range(n):plt.subplot(n+1, 2, i*2+1)plt.plot(X, Yn,linestyle='-', color='k', label = 'Original Signal')plt.plot(X, S_haar_n[i],linestyle='-', color='r', label = f'Smooth ($S^{i+1}$)')plt.ylabel('Amplitude')plt.gridplt.title(f'Smooth - Level {i+1} ($S^{i+1}$)')if i == n-1:plt.xlabel('Time')else:plt.gca.set_xticklabels()plt.subplot(n+1, 2, i*2+2)plt.plot(X, D_haar_n[i],linestyle='-', color='b', label = f'Details ($D^{i+1}$)')plt.gridplt.title(f'Detail - Level {i+1} ($D^{i+1}$)')if i == n-1:plt.xlabel('Time') else:plt.gca.set_xticklabels()plt.suptitle('Haar Wavelet - Signal With Noise', fontsize=20)plt.tight_layoutif SAVE_OUNTPUTS:plt.savefig(OUTPUT_DIR+'/HaarWavelet_SignalWithNoise'+OUTPUT_FORMAT, bbox_inches='tight')plt.showimport matplotlib.pyplot as pltplt.rc('text', usetex=True)plt.rc('font', family='serif')# %matplotlib tkn = len(S_db4_n)plt.figure(figsize=(13,14))for i in range(n):plt.subplot(n+1, 2, i*2+1)plt.plot(X, Yn,linestyle='-', color='k', label = 'Original Signal')plt.plot(X, S_db4_n[i],linestyle='-', color='r', label = f'Smooth ($S^{i+1}$)')plt.ylabel('Amplitude')plt.gridplt.title(f'Smooth - Level {i+1} ($S^{i+1}$)')if i == n-1:plt.xlabel('Time')else:plt.gca.set_xticklabels()plt.subplot(n+1, 2, i*2+2)plt.plot(X, D_db4_n[i],linestyle='-', color='b', label = f'Details ($D^{i+1}$)')plt.gridplt.title(f'Detail - Level {i+1} ($D^{i+1}$)')if i == n-1:plt.xlabel('Time') else:plt.gca.set_xticklabels()plt.suptitle('Daubechies4 Wavelet - Signal With Noise', fontsize=20)plt.tight_layoutif SAVE_OUNTPUTS:plt.savefig(OUTPUT_DIR+'/Daubechies4Wavelet_SignalWithNoise'+OUTPUT_FORMAT, bbox_inches='tight')plt.showimport matplotlib.pyplot as pltplt.rc('text', usetex=True)plt.rc('font', family='serif')# %matplotlib tkn = len(S_db6_n)plt.figure(figsize=(13,14))for i in range(n):plt.subplot(n+1, 2, i*2+1)plt.plot(X, Yn,linestyle='-', color='k', label = 'Original Signal')plt.plot(X, S_db6_n[i],linestyle='-', color='r', label = f'Smooth ($S^{i+1}$)')plt.ylabel('Amplitude')plt.gridplt.title(f'Smooth - Level {i+1} ($S^{i+1}$)')if i == n-1:plt.xlabel('Time')else:plt.gca.set_xticklabels()plt.subplot(n+1, 2, i*2+2)plt.plot(X, D_db6_n[i],linestyle='-', color='b', label = f'Details ($D^{i+1}$)')plt.gridplt.title(f'Detail - Level {i+1} ($D^{i+1}$)')if i == n-1:plt.xlabel('Time') else:plt.gca.set_xticklabels()plt.suptitle('Daubechies6 Wavelet - Signal With Noise', fontsize=20)plt.tight_layoutif SAVE_OUNTPUTS:plt.savefig(OUTPUT_DIR+'/Daubechies6Wavelet_SignalWithNoise'+OUTPUT_FORMAT, bbox_inches='tight')plt.showimport matplotlib.pyplot as pltplt.rc('text', usetex=True)plt.rc('font', family='serif')# %matplotlib tkn = len(S_mh_n)plt.figure(figsize=(13,14))for i in range(n):plt.subplot(n+1, 2, i*2+1)plt.plot(X, Yn,linestyle='-', color='k', label = 'Original Signal')plt.plot(X, S_mh_n[i],linestyle='-', color='r', label = f'Smooth ($S^{i+1}$)')plt.ylabel('Amplitude')plt.gridplt.title(f'Smooth - Level {i+1} ($S^{i+1}$)')if i == n-1:plt.xlabel('Time')else:plt.gca.set_xticklabels()plt.subplot(n+1, 2, i*2+2)plt.plot(X, D_mh_n[i],linestyle='-', color='b', label = f'Details ($D^{i+1}$)')plt.gridplt.title(f'Detail - Level {i+1} ($D^{i+1}$)')if i == n-1:plt.xlabel('Time') else:plt.gca.set_xticklabels()plt.suptitle('Mexican Hat Wavelet - Signal With Noise', fontsize=20)plt.tight_layoutif SAVE_OUNTPUTS:plt.savefig(OUTPUT_DIR+'/MexicanHatWavelet_SignalWithNoise'+OUTPUT_FORMAT, bbox_inches='tight')plt.showimport matplotlib.pyplot as pltplt.rc('text', usetex=True)plt.rc('font', family='serif')# %matplotlib tkn = len(S_sym2_n)plt.figure(figsize=(13,14))for i in range(n):plt.subplot(n+1, 2, i*2+1)plt.plot(X, Yn,linestyle='-', color='k', label = 'Original Signal')plt.plot(X, S_sym2_n[i],linestyle='-', color='r', label = f'Smooth ($S^{i+1}$)')plt.ylabel('Amplitude')plt.gridplt.title(f'Smooth - Level {i+1} ($S^{i+1}$)')if i == n-1:plt.xlabel('Time')else:plt.gca.set_xticklabels()plt.subplot(n+1, 2, i*2+2)plt.plot(X, D_sym2_n[i],linestyle='-', color='b', label = f'Details ($D^{i+1}$)')plt.gridplt.title(f'Detail - Level {i+1} ($D^{i+1}$)')if i == n-1:plt.xlabel('Time') else:plt.gca.set_xticklabels()plt.suptitle('Symlet2 Wavelet - Signal With Noise', fontsize=20)plt.tight_layoutif SAVE_OUNTPUTS:plt.savefig(OUTPUT_DIR+'/Symlet2Wavelet_SignalWithNoise'+OUTPUT_FORMAT, bbox_inches='tight')plt.show

知乎学术咨询:

https://www.zhihu.com/consult/people/792359672131756032?isMe=1

担任《Mechanical System and Signal Processing》《中国电机工程学报》等期刊审稿专家,擅长领域:信号滤波/降噪,机器学习/深度学习,时间序列预分析/预测,设备故障诊断/缺陷检测/异常检测。

分割线分割线

基于改进高斯-拉普拉斯滤波器的微震信号平滑降噪方法(MATLAB)

完整数据可通过知乎学术咨询获得

非平稳信号的凸一维全变差降噪方法(MATLAB)

算法可迁移至金融时间序列,地震/微震信号,机械振动信号,声发射信号,电压/电流信号,语音信号,声信号,生理信号(ECG,EEG,EMG)等信号。

完整数据可通过知乎学术咨询获得

基于广义交叉验证阈值的微震信号降噪方法(MATLAB)

算法可迁移至金融时间序列,地震/微震信号,机械振动信号,声发射信号,电压/电流信号,语音信号,声信号,生理信号(ECG,EEG,EMG)等信号。

完整数据可通过知乎学术咨询获得

来源:科技新武器

相关推荐