使用matlab2019b版本中最新的DspToolbox可以非常容易的实现主动降噪的基本LMS算法,并且可以对步长,滤波器长度进行调节。详细设定移步另一篇关于dsp.LMSFilter的博客。
%% 初始化设置 将信号x传递给FIR滤波器。未知系统的输出是期望信号d,它是未知系统(FIR滤波器)的输出和加性噪声信号n的和
%% Copyright © 2020 by RichardYang. All rights reserved.
num = fir1(31,0.5);%设定FIR滤波器参数,设定一个长度为32的FIR滤波器
fir = dsp.FIRFilter('Numerator',num);
iir = dsp.IIRFilter('Numerator',sqrt(0.75),...
'Denominator',[1 -0.5]);
x = iir(sign(randn(2000,25))); %假设输入信号
n = 0.1*randn(size(x)); %假设噪声信号
d = fir(x) + n; %假设输出信号
l = 32;
mu = 0.008;%设定步长
m = 5;%抽取因子
lms = dsp.LMSFilter('Length',l,'StepSize',mu);%设置lms长度L为32,步长为0.008
[mmse,emse,meanW,mse,traceK] = msepred(lms,x,d,m);
[simmse,meanWsim,Wsim,traceKsim] = msesim(lms,x,d,m);
%% 显示MSE结果
subplot(2,2,1)
plot(x)
title('输入信号')
ylim([-2 2])
subplot(2,2,2)
plot(d)
title('期望信号')
subplot(2,2,3)
plot(y)
title('输出信号')
subplot(2,2,4)
plot(err)
title('误差信号')
figure
nn = m:m:size(x,1);
semilogy(nn,simmse,[0 size(x,1)],[(emse+mmse)...
(emse+mmse)],nn,mse,[0 size(x,1)],[mmse mmse])
title('MSE的性能表现')
axis([0 size(x,1) 0.001 10])
legend('MSE (Sim.)','Final MSE','MSE','Min. MSE')
xlabel('Time Index')
ylabel('均方误差MSE的值')
figure
plot(nn,meanWsim(:,12),'b',nn,meanW(:,12),'r',nn,...
meanWsim(:,13:15),'b',nn,meanW(:,13:15),'r')
PlotTitle ={'权值系数变化轨迹';...
'W(12), W(13), W(14), and W(15)'}
title(PlotTitle)
legend('仿真值','理论值')
xlabel('Time Index')
ylabel('权值w的值')
grid
figure
semilogy(nn,traceKsim,nn,traceK,'r')
title('Sum-of-Squared Coefficient Errors')
axis([0 size(x,1) 0.0001 1])
legend('Simulation','Theory')
xlabel('Time Index')
ylabel('平方误差值')
Copyright © 2020 by RichardYang. All rights reserved.
仅供参考,严禁转载,感谢。