% SVD4  Laguerre approximation
% Question: finding the MSE in time is the same as finding the
% MSE for the adjustable parameters????
% uses the samples of the segment that collects the charge,
% 

clear
hold off
% better a=.74
T=.1; a = .59
K = sqrt((1-a^2)*T);
DL=47;
n = [0 K];               %find the outputs of the laguerre polynomial
d = [1 -1*a];
u = dimpulse(n,d,DL); 
nt = [-a 1];
dt = [1 -a];

for i=1:20
 n = [-a 1];
 d = [1 -a];
 u = [u dlsim(nt,dt,u(:,i))]; 
end

load shapes2
Y=shapes(:,50+1-DL:50)';
I=size(Y);
y_hat=zeros(I);
invutuut=inv(u'*u)*u';

for i=1:I(1,2)
  theta(:,i)=invutuut*Y(:,i);
  y_hat(:,i)=u*theta(:,i);
  mse(i)=[Y(:,i)-y_hat(:,i)]'*[Y(:,i)-y_hat(:,i)];

plot(1:DL,Y(:,i),'r',1:DL,y_hat(:,i),'g')
%pause(1)

end
sum(mse)


