%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%LMS自适应滤波器性能分析
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
clc
close all
%%%%%%%%%%%%%%参数设置%%%%%%%%%%%%%%%%%%%%%%%
w=3;
% step_len=0.075;%步长
step_len=0.05;%步长
variance_v=0.01;%方差
repeat_times=20;%重复次数
iteration_times=500;%迭代次数
filt_len=2;%滤波器长度
delay=fix(filt_len/2)-1;
delay1=1;
%%%%%%%%%%%%%%%%%%%%变量及数组初始化%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Chan_factor=(1+cos(2*pi*([1:1:3]-2)/w))/2
Square_err=zeros(repeat_times,iteration_times);
% Chan_factor1=[-0.195 0.95]
Chan_factor1=[-0.195 0.95]
X=zeros(filt_len,1);
W1=[];
W2=[];
Wm1=[repeat_times,iteration_times];
Wm2=[repeat_times,iteration_times];
%%%%%%%%%%%%%外循环,重复做repeat_timws次实验,平均值%%%%%%%%%%%%%%%%
for loop1=1:repeat_times%循环次数
%%%%%%%%%%%%%%内循环变量及数组初始化%%%%%%%%%%%%%%%%%%%%%%%%
Source=zeros(iteration_times,1);
W=zeros(filt_len,1);
%%%%%%%%%%%%%内循环,做iteration_times次迭代%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:iteration_times
%%%%%%%%%%%%%%信源,产生等概率分布的正负1%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if rand<0.5
Source(i)=-1;
else
Source(i)=1;
end
end
%%%%%%%%%%%%%%信道,滤波和加噪声%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Revdata=filter(Chan_factor1,1,Source)+randn(iteration_times,1)*sqrt(variance_v);
% Revdata=filter(Chan_factor,1,Source)+randn(iteration_times,1)*sqrt(variance_v);
% Revdata=filter(Chan_factor1,1,Source);
% RT=filter(Chan_factor1,1,Source);
%%%%%%%%%%%%%%均衡器,基于LMS算法%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for loop2=1:iteration_times-filt_len+1
% U=Revdata(loop2:loop2+filt_len-1);
% y(n)=W'*U=W'*x(n),U=x(n)=输入
% d为输出
U=Source(loop2:loop2+filt_len-1);
d=Revdata(loop2+1);
% d=Source(loop2+delay);
% X= x(3)=v(3)-a1*x(2)-a2*x(1);
% d=RT(loop2+delay1);
% Y(loop2)=W'*U;
e=d-W'*U;
% e=d-W'*U;
W=W+step_len*U*e;
Wm1(loop2,loop1)=W(1);
Wm2(loop2,loop1)=W(2);
% W1(loop2)=W(1);
% W2(loop2)=W(2);
Square_err(loop1,loop2)=e.^2;
end
end
%%%%%%%%%%%%%%作图,显示结果%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Square_err_mean=sum(Square_err(:,:))/repeat_times;
% Wm2
W1=Wm1(:,repeat_times);
W2=Wm2(:,repeat_times);
meanW1=mean(Wm1');
meanW2=mean(Wm2');
Square_err;
%semilogy(Square_err_mean);
figure(1);
plot(Square_err_mean);
hold on;
plot(Square_err(1,:),'r');
legend('20次重复试验平均误差','单次误差');
xlabel('迭代次数');
ylabel('均方误差');
title('步长为0.075,500次迭代均方误差变化曲线');
grid on;
hold on;
figure(2);
plot(W1);
hold on;
plot(W2,'r');
hold on;
plot(meanW1,'k');
hold on;
plot(meanW2,'--k');
legend('W1','W2','20次重复试验W1','20次重复试验W2');
xlabel('迭代次数');
ylabel('抽头权值');
title('步长为0.075,500次迭代权值学习曲线');
grid on;