| | |
|
|
MATLAB
Problem 1.2 Gradient Descent
Matlab
Code:
clear all;
close all;
clc;
for theta = [pi, 7/8*pi, 6/8*pi, 4/8*pi,
1/8*pi];
n = 0:1:100;
N = 2;
phi = 0;
%h = [1;5];
%h = [7;9];
h = [3;8];
varianz_nu = 0.5;
c0_r = -10:0.1:15;
c1_r = -10:0.1:15;
nu = sqrt(varianz_nu)*randn(size(n));
x = cos(theta*n + phi);
figure;
plot(x(1:min([2*ceil(2*pi/theta),length(x)])),'bx-');
X = [x;[0, x(1:end-1)]];
d = h'*X + nu;
rxx = xcorr(x, N-1,'unbiased');
rxx = rxx(N:end);
Rxx = toeplitz(rxx)
cond(Rxx);
p = xcorr(d,x,N-1,'unbiased');
p = p(N:end)';
J = zeros(length(c1_r),length(c0_r));
for i = 1:length(c0_r)
for j = 1:length(c1_r)
c_cur = [c0_r(i); c1_r(j)];
J(j,i) = mean((d-c_cur'*X).^2);
end
end
figure;
contour(c0_r,c1_r, J, 20);
grid on;
xlabel('c0');
ylabel('c1');
axis equal;
axis([c0_r(1) c0_r(end) c1_r(1)
c1_r(end)]);
steps = 30;
c = zeros(2,steps+1);
c(:,1) = [0; 0];
mu = 0.8;
hold on;
plot(h(1),h(2),'xk','MarkerSize',20,'LineWidth',2);
for i = 1:steps
c(:,i+1) = c(:,i) + mu*(p-Rxx*c(:,i));
plot([c(1,i),c(1,i+1)],[c(2,i),c(2,i+1)],'ro-');
text(c(1,i+1), c(2,i+1),sprintf('
%d',i));
pause(0.1);
end
c_end = c(:,end);
end
Theta = л: 
 Theta = 7/8 * л:
This paragraph has been concealed! Download the complete document for free! • Click on download to get complete and readable text • This is a free of charge document sharing network • First upload your own document, and you get a word document per email • No registration necessary, gratis Swap homeworks and notes at no charge! Gratis scripts for students and pupils!
 Theta = 6/8 * л:
 Theta = 4/8 * л:
 
Theta = 1/8 * л:
Diskussion:
Bei der Frequenz л/2 wird Rxx zu
einer Diagonalmatrix. Erhöht bzw. erniedrigt man ausgehend von л/2 die
Frequenz wird Rxx immer linear unabhängiger.
Tap- input Vector scant nicht mehr die
ganze Ebene ( persistently exciting).
MATLAB Problem 1.3
Least-Squares Tracking of a Time Varying System
Matlab Code:
clear all;
close all;
clc;
% a)
n = 0:999;
h = [ 1; -1; 1];
x = sin(2/16*pi*n) + cos(pi/4*n)+
sin(3/16*pi*n) - cos(10/16*pi*n);
d = conv(h,x);
d = d(1:end-2) + randn(size(n));
c = ls_filter(x, d, 3);
% b)
n = 0:999;
N = 3;
x = randn(size(n));
h = [ ones(size(n))
-1 + 0.002*n
+1 - 0.002*n ];
X = toeplitz([x(1); 0; 0], x);
d = zeros(size(n));
for i = 1:length(n)
d(i) = h(:,i)'*X(:,i);
end
figure;
waterfall(h);
for lambda = [0.1, 1.0]
for M = [10,50]
x_p = [zeros(1, M-1), x];
d_p = [zeros(1, M-1), d];
c = zeros(N, length(n));
for k = M:length(x_p)
x_cur = x_p((k-M+1):k);
d_cur = d_p((k-M+1):k);
c(:,k-M+1) = ls_filter(x_cur,
d_cur, N, lambda);
end
figure;
plot(n, h(1,:), '-r'); hold on;
plot(n, c(1,:), '-b');
figure;
plot(n, h(2,:), '-r'); hold on;
plot(n, c(2,:), '-b');
This paragraph has been concealed! Download the complete document for free! • Click on download to get complete and readable text • This is a free of charge document sharing network • First upload your own document, and you get a word document per email • No registration necessary, gratis Swap homeworks and notes at no charge! Gratis scripts for students and pupils!
plot(n, h(3,:), '-r'); hold on;
plot(n,
c(3,:), '-b');
figure;
waterfall(c);
end
end
function [ c ]
= ls_filter( x, d, N, lambda )
% x ... input
signal
% d ... desired
output signal (of same length as x)
% N ... number
of filter coeffcients
% lambda ...
optional "forgetting factor" 0<lambda<=1 (default =1)
if(nargin <
4)
lambda = 1;
end
n0 = length(x)-1;
col_1 = x';
row_1 = [x(1),
zeros(1,N-1)];
X =
toeplitz(col_1, row_1);
w =
lambda.^(n0:-1:0);
W = diag(w);
c =
(W.^(1/2)*X)\(W.^(1/2)*d');
h:
 Lambda = 0.1
M = 10
 
 Lambda = 0.1
M = 50
 
 Lambda = 1.0
M = 10
 
 
Lambda = 1.0
M = 50
 
Diskussion:
Man sieht dass hier das Vergessen
notwendig ist da sich das System anscheinend relativ stark ändert.
MATLAB Problem 1.4
Bonus Problem
Matlab Code:
clear all;
close all;
clc;
h = [1; 2];
N = length(h);
n = 0:100;
x1 = cos(0.5*pi*n);
x2 = cos(pi*n);
x3 = cos(pi*n)+2;
x4 = randn(size(n));
Signals = [x1;x2;x3;x4];
for i = 1:4
x = Signals(i,:);
X =
toeplitz([x(1); 0], x)
d = h' * X;
steps = 10;
c = zeros(N, steps+1);
c(:,1) = [-1 -2];
for j = 1:steps
y = c(:,j)'*X(:,j);
c(:,j+1) = c(:,j) +
1/(X(:,j)'*X(:,j))*(d(j)-y)*X(:,j);
end
figure();
plot(h(1), h(2), 'xk', 'MarkerSize', 10,
'LineWidth', 2); hold on;
axis([-10 10 -10 10]);
plot(c(1,:), c(2,:), '-or');
end
This paragraph has been concealed! Download the complete document for free! • Click on download to get complete and readable text • This is a free of charge document sharing network • First upload your own document, and you get a word document per email • No registration necessary, gratis Swap homeworks and notes at no charge! Gratis scripts for students and pupils!
Cos(0.5*л*n) 
Cos(л*n)
 Cos(л*n)+2
Randn:
Diskussion:
Bei allem Signalen bis auf cos(л*n)
kann das unbekannte System identifiziert werden. Bei diesem Signal liegt wieder
eine lineare Abhängigkeit vor.
X = %für cos(л*n)
Columns 1 through 10
1 -1 1 -1 1 -1 1
-1 1 -1
0 1 -1 1 -1 1 -1
1 -1 1
| Page 1 of 1
|
|