小波变换示例

文章目录


1.小波变换

代码如下(示例):

import torch
import torch.nn as nn


def Normalize(x):
    ymax = 255
    ymin = 0
    xmax = x.max()
    xmin = x.min()
    return (ymax-ymin)*(x-xmin)/(xmax-xmin) + ymin


def dwt_init(x):

    x01 = x[:, :, 0::2, :] / 2
    x02 = x[:, :, 1::2, :] / 2
    x1 = x01[:, :, :, 0::2]
    x2 = x02[:, :, :, 0::2]
    x3 = x01[:, :, :, 1::2]
    x4 = x02[:, :, :, 1::2]
    x_LL = x1 + x2 + x3 + x4
    x_HL = -x1 - x2 + x3 + x4
    x_LH = -x1 + x2 - x3 + x4
    x_HH = x1 - x2 - x3 + x4

    return torch.cat((x_LL, x_HL, x_LH, x_HH), 0)


# 使用哈尔 haar 小波变换来实现二维离散小波
def iwt_init(x):
    r = 2
    in_batch, in_channel, in_height, in_width = x.size()
    out_batch, out_channel, out_height, out_width = int(in_batch/(r**2)),in_channel, r * in_height, r * in_width
    x1 = x[0:out_batch, :, :] / 2
    x2 = x[out_batch:out_batch * 2, :, :, :] / 2
    x3 = x[out_batch * 2:out_batch * 3, :, :, :] / 2
    x4 = x[out_batch * 3:out_batch * 4, :, :, :] / 2

    h = torch.zeros([out_batch, out_channel, out_height,
                     out_width]).float().to(x.device)

    h[:, :, 0::2, 0::2] = x1 - x2 - x3 + x4
    h[:, :, 1::2, 0::2] = x1 - x2 + x3 - x4
    h[:, :, 0::2, 1::2] = x1 + x2 - x3 - x4
    h[:, :, 1::2, 1::2] = x1 + x2 + x3 + x4

    return h


class DWT(nn.Module):
    def __init__(self):
        super(DWT, self).__init__()
        self.requires_grad = False  # 信号处理,非卷积运算,不需要进行梯度求导

    def forward(self, x):
        return dwt_init(x)


class IWT(nn.Module):
    def __init__(self):
        super(IWT, self).__init__()
        self.requires_grad = False

    def forward(self, x):
        return iwt_init(x)

原始图像:
Raw Image
小波变换图像:
DWT Image
小波逆变换图像:
IWT Image

f1=50; % 频率1 f2=100; % 频率2 fs=2*(f1+f2); % 采样频率 Ts=1/fs; % 采样间隔 N=120; % 采样点数 n=1:N; y=sin(2*pi*f1*n*Ts)+sin(2*pi*f2*n*Ts); % 正弦波混合 figure(1) plot(y); title('两个正弦信号') figure(2) stem(abs(fft(y))); title('两信号频谱') %% 2.小波滤波器谱分析 h=wfilters('db30','l'); % 低通 g=wfilters('db30','h'); % 高通 h=[h,zeros(1,N-length(h))]; % 补零(圆周卷积,且增大分辨率变于观察) g=[g,zeros(1,N-length(g))]; % 补零(圆周卷积,且增大分辨率变于观察) figure(3); stem(abs(fft(h))); title('低通滤波器图'); figure(4); stem(abs(fft(g))); title('高通滤波器图') %% 3.MALLET分解算法(圆周卷积的快速傅里叶变换实现) sig1=ifft(fft(y).*fft(h)); % 低通(低频分量) sig2=ifft(fft(y).*fft(g)); % 高通(高频分量) figure(5); % 信号图 subplot(2,1,1) plot(real(sig1)); title('分解信号1') subplot(2,1,2) plot(real(sig2)); title('分解信号2') figure(6); % 频谱图 subplot(2,1,1) stem(abs(fft(sig1))); title('分解信号1频谱') subplot(2,1,2) stem(abs(fft(sig2))); title('分解信号2频谱') %% 4.MALLET重构算法 sig1=dyaddown(sig1); % 2抽取 sig2=dyaddown(sig2); % 2抽取 sig1=dyadup(sig1); % 2插值 sig2=dyadup(sig2); % 2插值 sig1=sig1(1,[1:N]); % 去掉最后一个零 sig2=sig2(1,[1:N]); % 去掉最后一个零 hr=h(end:-1:1); % 重构低通 gr=g(end:-1:1); % 重构高通 hr=circshift(hr',1)'; % 位置调整圆周右移一位 gr=circshift(gr',1)'; % 位置调整圆周右移一位 sig1=ifft(fft(hr).*fft(sig1)); % 低频 sig2=ifft(fft(gr).*fft(sig2)); % 高频 sig=sig1+sig2; % 源信号 %% 5.比较 figure(7); subplot(2,1,1) plot(real(sig1)); title('重构低频信号'); subplot(2,1,2) plot(real(sig2)); title('重构高频信号'); figure(8); subplot(2,1,1) stem(abs(fft(sig1))); title('重构低频信号频谱'); subplot(2,1,2) stem(abs(fft(sig2))); title('重构高频信号频谱'); figure(9) plot(real(sig),'r','linewidth',2); hold on; plot(y); legend('重构信号','原始信号') title('重构信号与原始信号比较') f1=50; % 频率1 f2=100; % 频率2 fs=2*(f1+f2); % 采样频率 Ts=1/fs; % 采样间隔 N=120; % 采样点数 n=1:N; y=sin(2*pi*f1*n*Ts)+sin(2*pi*f2*n*Ts); % 正弦波混合 figure(1) plot(y); title('两个正弦信号') figure(2) stem(abs(fft(y))); title('两信号频谱') %% 2.小波滤波器谱分析 h=wfilters('db30','l'); % 低通 g=wfilters('db30','h'); % 高通 h=[h,zeros(1,N-length(h))]; % 补零(圆周卷积,且增大分辨率变于观察) g=[g,zeros(1,N-length(g))]; % 补零(圆周卷积,且增大分辨率变于观察) figure(3); stem(abs(fft(h))); title('低通滤波器图'); figure(4); stem(abs(fft(g))); title('高通滤波器图') %% 3.MALLET分解算法(圆周卷积的快速傅里叶变换实现) sig1=ifft(fft(y).*fft(h)); % 低通(低频分量) sig2=ifft(fft(y).*fft(g)); % 高通(高频分量) figure(5); % 信号图 subplot(2,1,1) plot(real(sig1)); title('分解信号1') subplot(2,1,2) plot(real(sig2)); title('分解信号2') figure(6); % 频谱图 subplot(2,1,1) stem(abs(fft(sig1))); title('分解信号1频谱') subplot(2,1,2) stem(abs(fft(sig2))); title('分解信号2频谱') %% 4.MALLET重构算法 sig1=dyaddown(sig1); % 2抽取 sig2=dyaddown(sig2); % 2抽取 sig1=dyadup(sig1); % 2插值 sig2=dyadup(sig2); % 2插值 sig1=sig1(1,[1:N]); % 去掉最后一个零 sig2=sig2(1,[1:N]); % 去掉最后一个零 hr=h(end:-1:1); % 重构低通 gr=g(end:-1:1); % 重构高通 hr=circshift(hr',1)'; % 位置调整圆周右移一位 gr=circshift(gr',1)'; % 位置调整圆周右移一位 sig1=ifft(fft(hr).*fft(sig1)); % 低频 sig2=ifft(fft(gr).*fft(sig2)); % 高频 sig=sig1+sig2; % 源信号 %% 5.比较 figure(7); subplot(2,1,1) plot(real(sig1)); title('重构低频信号'); subplot(2,1,2) plot(real(sig2)); title('重构高频信号'); figure(8); subplot(2,1,1) stem(abs(fft(sig1))); title('重构低频信号频谱'); subplot(2,1,2) stem(abs(fft(sig2))); title('重构高频信号频谱'); figure(9) plot(real(sig),'r','linewidth',2); hold on; plot(y); legend('重构信号','原始信号') title('重构信号与原始信号比较')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值