Scilab Code For Implementing LMS Algorithm (Function) For P 2
Scilab Code For Implementing LMS Algorithm (Function) For P 2
p=2
N = length(x);
// no. of iterations
y = zeros(N,1);
w1 = w0;
wn = cat(3,w1);
p=2;
x2 = [x(1) ; 0];
y(1) = ((x2')*( wn(:,:,1))); // computing w2
en(1) = d(1)-y(1);
w2 = u*en(1)* [x(1);0];
wn = cat(3,wn,w2);
for i = 2 : N
x1 = [x(i) ; x(i-1)];
y(i) = (x1')*(wn(:,:,i-1));
en(i) = d(i)- y(i);
// lms algorithm
endfunction;
clear all;
clc;
d = 'C:\Users\nikunj patel\Desktop\d.txt'
x = 'C:\Users\nikunj patel\Desktop\x.txt' // giving address of source files
N = length(x);
y = zeros(N,1);
w0 = [0 ; 0];
for i = 1:2000
wn1_0(i) = wn1(1,1,i);
wn1_1(i) = wn1(2,1,i);
wn2_0(i) = wn2(1,1,i);
wn2_1(i) = wn2(2,1,i);
wn3_0(i) = wn3(1,1,i);
wn3_1(i) = wn3(2,1,i);
end
i = 1:2000;
plot(i,wn1_0,'r',i,wn2_0,'g',i,wn3_0,'b'); // plotting wn[0]
title('Wn[0] versus n');
xlabel('n');
ylabel('Wn[0]')
legend('u=0.001','u=0.01','u=0.1');
figure;
figure;
subplot(3,1,1);
plot(en1);
title('error for step size u=0.001');
xlabel('n');
ylabel('e[n]');
subplot(3,1,2);
plot(en2);
title('error for step size u=0.01');
xlabel('n');
ylabel('e[n]');
subplot(3,1,3);
plot(en3);
title('error for step size u=0.1');
xlabel('n');
ylabel('e[n]');
Plots :
1) wn[0] versus n
2) wn[1] versus n :