clc;
clear;
close all;
N=1000;
a1=1.558;
a2=-0.81;
v=randn(1,N);
x(1)=v(1);
x(2)=a1*x(1)+v(2);
for n=3:N
x(n)=a1*x(n-1)+a2*x(n-2)+v(n);
end
%% RLS
delta=0.0005;
lambda=0.99;%遗忘因子
e1=zeros(1,N);
w=zeros(2,N);
c1=1/delta*eye(2);
for n=3:N
xt1=[x(n-1); x(n-2)];
mu1=xt1'*c1*xt1;
g1=c1*xt1/(lambda+mu1);
e1(n)=x(n)-xt1'*w(:,n-1);
w(:,n)=w(:,n-1)+g1*e1(n);
c1=(c1-g1*xt1'*c1)/lambda;
end
%绘图
figure;
plot(w(1,:)); hold on;
plot(w(2,:),'r');
legend('a1','a2','Location','Best'); %图注
plot(a1*ones(1,N),'k');
plot(a2*ones(1,N),'k');
title('RLS');
%% 逆QE-RLS
M = 2;
P_root = 1/delta*eye(M);
W = zeros(M, N);
K = zeros(M, N);
lambda_root = lambda^-0.5;
u = zeros(M, 1);
z = zeros(M, 1);
gamma = zeros(N, 1);
e = zeros(N, 1);
% initialize predictor until Phi_root is full-rank '
for n = 2:M
% compute post-array from pre-array via QRD. Note that computation
% of p_H, eta, and W_H are omitted in the initialization period.
u = [x(n - 1); u(1:end - 1)];
pre_array = [ [1, lambda_root*u'*P_root]; [z, lambda_root*P_root] ];
post_array = triu(qr(pre_array'))';
P_root = post_array(M:end, M:end);
gamma_root = post_array(1, 1);
kn = post_array(M:end, 1)/gamma_root;
% save results
K(:, n) = kn;
gamma(n) = 1/gamma_root^2;
end
% main loop
for n = (M+1):N
% compute post-array from pre-array via QRD
u = [x(n - 1); u(1:end - 1)];
pre_array = [ [1, lambda_root*u'*P_root]; [z, lambda_root*P_root] ];
post_array = triu(qr(pre_array'))';
P_root = post_array(M:end, M:end);
gamma_root = post_array(1, 1);
kn = post_array(M:end, 1)/gamma_root;
% save results
K(:, n) = kn;
gamma(n) = 1/gamma_root^2;
% predict next sample and compute error
e(n) = x(n) - W(:, n - 1)'*u;
W(:, n) = W(:, n - 1) + K(:, n)*e(n);
end
figure;
plot(W(1,:)); hold on;
plot(W(2,:), 'r');
legend('a1','a2','Location','Best'); %图注
plot(a1*ones(1,N),'k');
plot(a2*ones(1,N),'k');
title('iQR-RLS');
评论1