%%This file will use the signals present in cardio_data.mat.  The signals
%%are the ventricular signal and the shock signal.  

load cardio_data;
%ShockSig--1x1450 vector
%VentSig---1x1450 vector

%The impulse response vector will have 2*N+1 elements
N=100;

%Take First Half of both signals
h_ShockSig=ShockSig(1:725);
h_VentSig=VentSig(1:725);

%Obtain the full 725x725 Toeplitz Matrix
rev_hVentSig(725:-1:1)=h_VentSig;
Toe=Toeplitz(rev_hVentSig);

%This is the Toeplitz Matrix we want
V=Toe(1:525,525:725);

%Invert it in the Overdetermined, Least Squares sense to get the impulse
%resonse h.  h will be a 201x1 vector
h=pinv(V)*h_ShockSig(101:625)';

%Now estimate whole signal with this impulse response
%Need to reject first 100 and last 100 elements of VentSig due to length of
%impulse response.
rev_VentSig(1450:-1:1)=VentSig;
F_Toe=Toeplitz(rev_VentSig);
F_V=F_Toe(1:1250,1250:1450);

%Here is the Linear System using h as the impulse response
F_S=F_V*h;
g=1:1250;

%Plot things up
subplot(2,1,1);
plot(h);
xlabel('t');ylabel('h(t)');legend('Impulse Repsonse for ICD');
subplot(2,1,2);
plot(g,F_S,g,ShockSig(100:1349));
xlabel('t');ylabel('S(t)');legend('Estimated Shock Signal', 'Recorded Shock Signal');