里面有祥盯些代码有问题,可以参考,代码还是自己写:!
%基于RLS算法的自适应线性预测
clc;
clear all;
N=300;
M=100;%计算的次数
w1=zeros(N,M);w2=zeros(N,M);I=eye(2);e1=zeros(N,M);
for k=1:M
%产生白噪声
Pv=0.008;%定义白噪声方差
a1=-0.195;a2=0.95;o=0.02;r=0.95;
m=5000;%产生5000个随机数
v=randn(1,m);
v=v*sqrt(Pv);%产生均值为0,方差为Pv的白噪声
%m=1:N;
v=v(1:N);%取出前1000个
%plot(m,v);title('均值为0,方差为0.0965的白谨配和噪声');ylabel('v(n)');xlabel('n');
v=v';
%向量初使化
x=zeros(1,N);
x(1)=v(1);%x(0)=v(0)
x(2)=v(2)-a1*v(1);%x(1)=v(1)-a1*v(0)
w=zeros(2,N);
w(:,1)=[0 0]';%w(0)=[0 0]';
X=zeros(2,N);
X(:,2)=[v(1) 0]';%X(0)=[0 0]';X(1)=[v(0) 0]'
C=zeros(2,2*N);
C(:,1:2)=1/o.*I;%C(0)=1/o*I
e=zeros(1,N)';%定义误差向量
u=zeros(1,N);
g=zeros(2,N);
%根据RLS算法进行递推
for n=1:N-2
x(n+2)=v(n+2)-a1*x(n+1)-a2*x(n);
X(:,n+2)=[x(n+1) x(n)]';
u(n)=X(:,n+1)'*C(:,2*n-1:2*n)*X(:,n+1);
g(:,n)=(C(:,2*n-1:2*n)*X(:,n+1))./(r+u(n));
w(:,n+1)=w(:,n)+g(:,n)*(x(n+1)-X(:,n+1)'*w(:,n));
C(:,2*n+1:2*(n+1))=1/r.*(C(:,2*n-1:2*n)-g(:,n)*X(:,n+1)'*C(:,2*n-1:2*n));
e(n)=x(n+1)-X(:,n+1)'*w(:,n);
w1(:,k)=w(1,:)'; w2(:,k)=w(2,:)';%将每次计算得到的权矢量值储存
e1(:,k)=e(:,1);%将每次计算得到的误差储存
end
end
%求权矢量和误差的M次的平均值
wa1=zeros(N,1);wa2=zeros(N,1);en=zeros(N,1);
for k=1:M
wa1(:,1)=wa1(:,1)+w1(:,k);
wa2(:,1)=wa2(:,1)+w2(:,k);
en(:,1)=en(:,1)+e1(:,k);
end
n=1:N;
subplot(221)
plot(n,w(1,n),n,w(2,n));%作出单次计算权矢量的变化曲线
xlabel('n');ylabel('w(n)');title('w1(n)和w2(n)的单次变化曲线(线性预测,RLS)')
subplot(222)
plot(n,wa1(n,1)./M,n,wa2(n,1)./M);%作出100次计算权矢量的平均变化曲线
xlabel('n');ylabel('w(n)');title('w1(n)和w2(n)的100次平均变化曲线')
subplot(223)
plot(n,e(n,1).^2);%作出单次计算e^2的变化曲线
xlabel('n');ylabel('e^2');title('单次计算e^2的变化曲线');
subplot(224)
plot(n,(en(n,1)/M).^2);%作出M次计算e^2的平均变化曲线
xlabel('n');ylabel('e^2');title('100次计卖圆算e^2的平均变化曲线');
%
RLS
算法
randn('seed',
0)
;
rand('seed',
0)
;
NoOfData
=
8000
;
%
Set
no
of
data
points
used
for
training
Order
=
32
;
%
Set
the
adaptive
filter
order
Lambda
=
0.98
;
%
Set
the
forgetting
factor
Delta
=
0.001
;
%
R
initialized
to
Delta*I
x
=
randn(NoOfData,
1)
;%
Input
assumed
to
be
white
h
=
rand(Order,
1)
;
%
System
picked
randomly
d
=
filter(h,
1,
x)
;
%
Generate
output
(desired
signal)
%
Initialize
RLS
P
=
Delta
*
eye
(
Order,
Order
)
;
<碰则br>w
=
zeros
(
Order,
1
)
;
%
RLS
Adaptation
for
n
=
Order
:
NoOfData
;
u
=
x(n:-1:n-Order+1)
;
pi_
=
u'
*
P
;
k
=
Lambda
+
pi_
*
u
;
K
=
pi_'/k;
e(n)
=
d(n)
-
w'
*
u
;
w
=
w
+
K
*
e(n)
;
PPrime
=
K
*
pi_
;
P
=
(
P
-
PPrime
)
/
Lambda
;
w_err(n)
=
norm(h
-
w)
;
<局吵告br>
end
;
<桐明br>
%
Plot
results
figure
;
plot(20*log10(abs(e)))
;
title('Learning
Curve')
;
xlabel('Iteration
Number')
;
ylabel('Output
Estimation
Error
in
dB')
;
figure
;
semilogy(w_err)
;
title('Weight
Estimation
Error')
;
xlabel('Iteration
Number')
;
ylabel('Weight
Error
in
dB')
;