function sheet04

% generate some data
T = linspace(0, 10, 1000);
X1 = sin(pi*T);
X2 = 2*(T - floor(T)) - 1;
X3 = 0.1*randn(1, 1000);

X = [X1; X2; X3];

% plot the sources
figure(1)
plotsources(T, X)

% generate a random mixing matrix
A = randn(3, 3);
Y = A * X;

% plot the mixed sources
figure(2)
plotsources(T, Y);

% compute time-lagged 
B = tdsep(Y, 5);

figure(3)
plotsources(T, B'*Y)

function plotsources(T, X)
N = size(X, 1);
for I = 1:N
  subplot(N, 1, I)
  plot(T, X(I, :))
end

% Compute the TDSEP estimate by diagonalizing both matrices
% simultaneously. Use matlabs EIG function for two matrices.
function B = tdsep(Y, T)
C0 = tdcov(Y, 0);
CT = tdcov(Y, T);
% ...

% Compute the time-lagged covariance matrices. Extract a "normal" and a
% lagged version of the signal and estimate the covariance matrix for
% both parts.
function C = tdcov(X, T)
% ...
