如题,笔记,无废话,无格式,仅作记录,欢迎交流。
1.案例一
clear;close all;clc
% 原始信号参数
fs = 1000; % 采样频率
t = 0:1/fs:1; % 时间向量
f = 10; % 信号频率
x = sin(2*pi*f*t); % 原始信号
% 添加高斯噪声
snr = 10; % 信噪比(以dB为单位)
noise = randn(size(x)); % 生成高斯噪声
noise = noise / sqrt(sum(noise.^2)) * sqrt(sum(x.^2)) / (10^(snr/20)); % 根据信噪比调整噪声的幅度
noisySignal = x + noise; % 带噪声的信号
% 选择小波基函数和去噪参数
wavelet = 'db4'; % 小波基函数
level = 5; % 小波分解的层数
% 执行小波变换
[coefficients, lengths] = wavedec(noisySignal, level, wavelet);
% 估计噪声水平
sigma = median(abs(coefficients - median(coefficients))) / 0.6745;
% 设置阈值
threshold = sigma * sqrt(2 * log(length(noisySignal)));
% 应用阈值
denoisedCoefficients = wthresh(coefficients, 's', threshold);
% 执行小波逆变换
denoisedSignal = waverec(denoisedCoefficients, lengths, wavelet);
% 绘制原始信号、带噪声的信号和去噪后的信号
figure;
subplot(3,1,1);
plot(t, x);
xlabel('时间 (s)');
ylabel('幅度');
title('原始信号');
subplot(3,1,2);
plot(t, noisySignal);
xlabel('时间 (s)');
ylabel('幅度');
title('带噪声的信号');
subplot(3,1,3);
plot(t, denoisedSignal);
xlabel('时间 (s)');
ylabel('幅度');
title('去噪后的信号');
2.案例二
% 原始信号参数
fs = 1000; % 采样频率
t = 0:1/fs:1; % 时间向量
f = 10; % 信号频率
x = sin(2*pi*f*t); % 原始信号
% 添加高斯噪声
snr = 10; % 信噪比(以dB为单位)
noise = randn(size(x)); % 生成高斯噪声
noise = noise / sqrt(sum(noise.^2)) * sqrt(sum(x.^2)) / (10^(snr/20)); % 根据信噪比调整噪声的幅度
noisySignal = x + noise; % 带噪声的信号
% 小波去噪参数设置
wavelet = 'db10'; % 小波基函数
level = 5; % 小波分解的层数
threshold = 'soft'; % 阈值选择方法为 'soft'
% 小波去噪
denoisedSignal = wdenoise(noisySignal, level, ...
'Wavelet', wavelet, 'DenoisingMethod', 'UniversalThreshold', 'ThresholdRule', threshold);
% 绘制加噪信号和去噪信号
t = 1:length(noisySignal);
figure;
subplot(2,1,1);
plot(t, noisySignal);
title('加噪信号');
xlabel('时间');
ylabel('幅值');
subplot(2,1,2);
plot(t, denoisedSignal);
title('去噪信号');
xlabel('时间');
ylabel('幅值');