查看小波分解各层图像的程序

仅作为记录,大佬请跳过。程序可直接运行

文章目录

数据

下载
提取码:fc9r

python版

import numpy as np
import matplotlib.pyplot as plt

import pywt
import pywt.data


# ecg = pywt.data.ecg()
#
# data1 = np.concatenate((np.arange(1, 400),
#                         np.arange(398, 600),
#                         np.arange(601, 1024)))
# x = np.linspace(0.082, 2.128, num=1024)[::-1]
# data2 = np.sin(40 * np.log(x)) * np.sign((np.log(x)))

mode = pywt.Modes.smooth


def plot_signal_decomp(data, w, title):
    """Decompose and plot a signal S.
    S = An + Dn + Dn-1 + ... + D1
    """
    w = pywt.Wavelet(w)#选取小波函数
    a = data
    ca = []#近似分量
    cd = []#细节分量
    for i in range(8):
        (a, d) = pywt.dwt(a, w, mode)#进行5阶离散小波变换
        ca.append(a)
        cd.append(d)

    rec_a = []
    rec_d = []

    for i, coeff in enumerate(ca):
        coeff_list = [coeff, None] + [None] * i
        rec_a.append(pywt.waverec(coeff_list, w))#重构

    for i, coeff in enumerate(cd):
        coeff_list = [None, coeff] + [None] * i
        if i ==3:
            print(len(coeff))
            print(len(coeff_list))
        rec_d.append(pywt.waverec(coeff_list, w))

    fig = plt.figure()
    ax_main = fig.add_subplot(len(rec_a) + 1, 1, 1)
    ax_main.set_title(title)
    ax_main.plot(data)
    ax_main.set_xlim(0, len(data) - 1)

    for i, y in enumerate(rec_a):
        ax = fig.add_subplot(len(rec_a) + 1, 2, 3 + i * 2)
        ax.plot(y, 'r')
        ax.set_xlim(0, len(y) - 1)
        ax.set_ylabel("A%d" % (i + 1))

    for i, y in enumerate(rec_d):
        ax = fig.add_subplot(len(rec_d) + 1, 2, 4 + i * 2)
        ax.plot(y, 'g')
        ax.set_xlim(0, len(y) - 1)
        ax.set_ylabel("D%d" % (i + 1))


#plot_signal_decomp(data1, 'coif5', "DWT: Signal irregularity")
#plot_signal_decomp(data2, 'sym5',
#                   "DWT: Frequency and phase change - Symmlets5")

ecg=np.loadtxt('L2.txt',dtype='int')
plot_signal_decomp(ecg, 'sym5', "DWT: Ecg sample - Symmlets5")


plt.show()

print('sec1')

显示

在这里插入图片描述

matlab版

clear;clc;close all;

[filename,filepath]=uigetfile('*.txt','打开文件')
file=fullfile(filepath,filename); 

a = load(file); %打开文件夹
% a=a(828:1914);
% subplot(2,1,1),
subplot(311);
plot(a);title('原信号');%grid on;


%% 一维离散小波分解重构 p217-5-2
figure(1);plot(a);ylabel('幅值 A');xlabel('样本序号 n');
[c,l]=wavedec(a,5,'db3');
a5=wrcoef('a',c,l,'db3',5);
a4=wrcoef('a',c,l,'db3',4);
a3=wrcoef('a',c,l,'db3',3);
a2=wrcoef('a',c,l,'db3',2);
a1=wrcoef('a',c,l,'db3',1);
figure(2);
subplot(521);plot(a5,'LineWidth',2);ylabel('a5');xlabel('样本序号5');
subplot(523);plot(a4,'LineWidth',2);ylabel('a4');xlabel('样本序号4');
subplot(525);plot(a3,'LineWidth',2);ylabel('a3');xlabel('样本序号3');
subplot(527);plot(a2,'LineWidth',2);ylabel('a2');xlabel('样本序号2');
subplot(529);plot(a1,'LineWidth',2);ylabel('a1');xlabel('样本序号1');
d5=wrcoef('d',c,l,'db3',5);
d4=wrcoef('d',c,l,'db3',4);
d3=wrcoef('d',c,l,'db3',3);
d2=wrcoef('d',c,l,'db3',2);
d1=wrcoef('d',c,l,'db3',1);
subplot(522);plot(d5,'LineWidth',2);ylabel('d5');xlabel('样本序号5');
subplot(524);plot(d4,'LineWidth',2);ylabel('d4');xlabel('样本序号4');
subplot(526);plot(d3,'LineWidth',2);ylabel('d3');xlabel('样本序号3');
subplot(528);plot(d2,'LineWidth',2);ylabel('d2');xlabel('样本序号2');
subplot(5,2,10);plot(d1,'LineWidth',2);ylabel('d1');xlabel('样本序号1');

显示

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41529093/article/details/113552785