www.gusucode.com > 自适应滤波源码程序 > 自适应滤波源码程序/Adaptive.m

    %LMS algorithm-weight update with coefficient of filter
clc;
clear all;
close all;

nr=1;
dr=[1 -1.2728 0.81];
v=randn(1,500);
x=filter(nr,dr,v);
 Rxx=xcorr(x);
rdx=xcorr(x,v);

mu=0.02;
w_int=[0,0]';
steps=length(x);
w_old=w_int;
wn0=zeros(1,steps);
wn1=wn0;
w_0=0;
 w_1=0;
for n=3:steps
    %saving past wt
    x_cap(n)=w_0*x(n-1)+w_1*x(n-2);
    e(n)=x(n)-x_cap(n);
    %updating past wt
    w_0=w_0+mu*(e(n)*conj(x(n)));
    wn0(n)=w_0;
    w_1=w_1+mu*(e(n)*conj(x(n-1)));
     wn1(n)=w_1;
end
es=e.^2;
plot(1:length(x),wn0,'r',1:length(x),wn1)
legend('W0','W1');
xlabel('Iteration');
ylabel('Coefficient of weights');
figure;
plot(1:length(x),es)
xlabel('Iteration');
ylabel('Squared Error');


RXX=toeplitz(Rxx(1:2));
Rdx=rdx(1:2);
w_opt=inv(RXX)*Rdx';
w=[w_0,w_1];
disp('W optimum')
disp(w_opt)
disp('W0 and W1')
disp(w)