实现信号的小波分解和重构
基于MATLAB实现的信号小波分解和重构
1. 一维小波分解和重构
% 生成测试信号
t = linspace(0, 1, 1024);
signal = sin(2 * pi * 50 * t) + 0.5 * sin(2 * pi * 120 * t) + randn(size(t)) * 0.2;% 小波分解
waveletName = 'sym4'; % 选择小波基函数
[cA, cD] = dwt(signal, waveletName); % 一维离散小波变换% 小波重构
reconstructedSignal = idwt(cA, cD, waveletName); % 一维离散小波反变换% 显示结果
figure;
subplot(3, 1, 1);
plot(t, signal);
title('Original Signal');
xlabel('Time');
ylabel('Amplitude');subplot(3, 1, 2);
plot(t, cA);
title('Approximation Coefficients');
xlabel('Time');
ylabel('Amplitude');subplot(3, 1, 3);
plot(t, reconstructedSignal);
title('Reconstructed Signal');
xlabel('Time');
ylabel('Amplitude');
2. 二维小波分解和重构
% 生成测试图像
[X, Y] = meshgrid(1:128, 1:128);
image = sin(X / 10) + cos(Y / 10) + randn(size(X)) * 0.1;% 小波分解
waveletName = 'sym4'; % 选择小波基函数
[C, S] = wavedec2(image, 2, waveletName); % 二维多层小波分解% 提取各层细节分量
[appCoefs, detCoefs] = detcoef2('all', C, S, 2); % 提取所有细节分量% 小波重构
reconstructedImage = waverec2(C, S, waveletName); % 二维多层小波重构% 显示结果
figure;
subplot(2, 2, 1);
imagesc(image);
title('Original Image');
colormap('gray');subplot(2, 2, 2);
imagesc(appCoefs);
title('Approximation Coefficients');
colormap('gray');subplot(2, 2, 3);
imagesc(detCoefs(:,:,1));
title('Horizontal Detail Coefficients');
colormap('gray');subplot(2, 2, 4);
imagesc(reconstructedImage);
title('Reconstructed Image');
colormap('gray');
说明
-
一维小波分解和重构:
- 使用
dwt
函数进行一维离散小波分解,得到近似分量cA
和细节分量cD
。 - 使用
idwt
函数进行一维离散小波反变换,重构原始信号。 - 显示原始信号、近似分量和重构信号的波形图。
- 使用
-
二维小波分解和重构:
- 使用
wavedec2
函数进行二维多层小波分解,得到多层分解的系数矩阵C
和尺寸矩阵S
。 - 使用
detcoef2
函数提取各层细节分量。 - 使用
waverec2
函数进行二维多层小波重构,重构原始图像。 - 显示原始图像、近似分量、细节分量和重构图像的灰度图。
- 使用
参考代码 实现信号的小波分解和重构 www.youwenfan.com/contentcsf/82096.html