2007-07-30 02:16:01
关于自适应滤波的,LMS和RLS。
clear;
close all;
N=1500;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 以下利用同余法生成白噪的过程 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=6;
x_0=1;
M=255;
f=2;
for i=1:N
x_A=A*x_0;
xi=mod(x_A,M);
vi=xi/256;
v(i)=(vi-0.5)*f;
x_0=xi;
end %噪声v
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 以下利用System Identification Toolbox %
% 工具箱提供系统辨识的输入信号函数idinput %
% 生成M序列的过程 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
input=idinput([15 1 100],'prbs',[0 1],[-1 1]); %u采用15拍M序列
input=input'; %输入input
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 以下利用Model Predictive Control Toolbox %
% 工具箱建立数学模型并模拟输入的过程 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
d=zeros(1,N);
A=[1];
B=[1 0.4 1.5 2.2];
C=[1];
m=idpoly(A,B,C); %建立数学模型
d=sim(m,[input'v']); %得到输出值
d=d';
m
order=4; %系统阶数为4
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% LMS %%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
w=zeros(1,order); %初始化w
mu=0.003043; %设置最优的mu,由mu.txt运行得到
for i=order:N %主要迭代过程,解释见原理部分
u=input(i:-1:i-order+1);
y(i)=w*u';
e(i)=d(i)-y(i);
w=w+2*mu*e(i)*u;
w_delta(i)=norm(w-B); %w_delta用来记录每次迭代w接近B的程度
end
figure
subplot(211)
plot(d,'b')
hold on
plot(y,'r')
legend('期望输出','估计输出值y')
title('运行时LMS算法输出值拟和')
xlabel('时间采样')
ylabel('输出值')
subplot(212)
semilogy(abs(e));
title('LMS算法输出误差值e')
xlabel('时间采样')
ylabel('误差值e')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%% RLS %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
w_1=zeros(order,1);
%初始化w
Lambda=0.99999; %设置Lambda
Delta=0.00000001; %设置Delta
c=(1/Delta)*eye(order,order); %设置c(0)
for i=order:N %主要迭代过程,解释见原理部分
u=input(i:-1:i-order+1);
g=c*u'/(Lambda+u*c*u');
c=(c-g*u*c)/Lambda;
y_1(i)=w_1'*u';
e_1(i)=d(i)-y_1(i);
w_1=w_1+g*e_1(i);
w_delta_1(i)=norm(w_1-B'); %w_delta_1用来记录每次迭代w接近B的程度
end
figure;
subplot(211);
plot(d,'b')
hold on;
plot(y_1,'r');
legend('期望输出','估计输出值y')
title('运行时RLS算法输出值拟和') ;
xlabel('时间采样')
ylabel('输出值')
subplot(212);
semilogy((abs(e_1))) ;
title('RLS算法输出拟和误差值e') ;
xlabel('采样间隔') ;
ylabel('误差值e') ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% LMS_Advance %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
w_2=zeros(1,order); %初始化w
for i=order:N %主要迭代过程,解释见原理部分
u=input(i:-1:i-order+1);
y_2(i)=w_2*u';
e_2(i)=d(i)-y_2(i);
if i mu=0.05; %设置最佳拐点值,由拐点值.txt运行得到
else
mu=0.003043; %设置最优的mu,由mu.txt运行得到
end
w_2=w_2+2*mu*e_2(i)*u;
w_delta_2(i)=norm(w_2-B); %w_delta_2用来记录每次迭代w接近B的程度
end
figure
subplot(211)
plot(d,'b')
hold on
plot(y_2,'r')
legend('期望输出','估计输出值y')
title('运行时LMS_Advance算法输出值拟和')
xlabel('时间采样')
ylabel('输出值')
subplot(212)
semilogy(abs(e_2));
title('LMS_Advance算法输出误差值e')
xlabel('时间采样')
ylabel('误差值e')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%% 三种算法比较 %%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m_1=idpoly(1,w,1); %建立数学模型
d_1=sim(m_1,[input'v']); %得到输出值
figure
subplot(311)
plot(d); %绘图:LMS算法拟和输出值
hold on;
plot(d_1,'r');
title('LMS算法拟和输出值')
legend('期望输出','拟和输出值')
xlabel('时间采样')
ylabel('输出值')
m_2=idpoly(1,w_1',1); %建立数学模型
d_2=sim(m_2,[input'v']); %得到输出值
subplot(312)
plot(d); %绘图:RLS算法拟和输出值
hold on;
plot(d_2,'r');
title('RLS算法拟和输出值')
legend('期望输出','拟和输出值')
xlabel('时间采样')
ylabel('输出值')
m_3=idpoly(1,w_2,1); %建立数学模型
d_3=sim(m_3,[input'v']); %得到输出值
subplot(313)
plot(d); %绘图:LMS_Advance算法拟和输出值
hold on;
plot(d_3,'r');
title('LMS_Advance算法拟和输出值')
legend('期望输出','拟和输出值')
xlabel('时间采样')
ylabel('输出值')
figure %绘图:拟和输出误差比较,并计算各个算方法误差矩阵的范数
plot(abs(d-d_1'));
norm_error_output_LMS=norm(abs(d-d_1'))
hold on;
plot(abs(d-d_2'),'r');
error_output_RLS=norm(abs(d-d_2'))
hold on;
plot(abs(d-d_3'),'g');
norm_error_output_LMS_Advance=norm(abs(d-d_3'))
hold on;
title('拟和输出误差比较')
legend('LMS估计的拟和输出误差','RLS估计的拟和输出误差','LMS_Advance估计的拟和输出误差')
xlabel('时间采样')
ylabel('输出误差')
figure %绘图:拟和输出误差比较,并显示各个算法的权值估计结果
plot(B,'bo')
Actual_weights=B
hold on
plot(w,'r*')
w_LMS=w
hold on
plot(w_1,'g*')
w_RLS=w_1'
hold on
plot(w_2,'y*')
w_LMS_Advance=w_2
legend('实际权值','LMS估计的权值','RLS估计的权值','LMS_Advance估计的权值')
title('三种算法估计权值的比较')
figure %绘图:三种算法权值收敛性的比较,并计算稳定后各个算法权值估 计误差矩阵的范数
semilogy(abs(w_delta));
norm_error_w_LMS=norm(w_delta(800:900))
hold on;
semilogy(abs(w_delta_1),'r');
norm_error_w_RLS=norm(w_delta_1(800:900))
hold on;
semilogy(abs(w_delta_2),'g');
norm_error_w_LMS_Advance=norm(w_delta_2(800:900))
legend('LMS算法估计与真实权值的误差变化','RLS算法估计与真实权值的误差变化','LMS_Advance算法估计与真实权值的误差变化')
title('三种算法权值收敛性的比较') ;