% SVD5b  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,
% 

close all;
clear all;

%read data from database
[Resp,RPos] = LeDadoIt(25);

DL = 47;
[a, b] = size(Resp');
%shapper for the signal that collects charge
Y=ones(a-4,1)-Resp(8,a+2-DL:a)';
Y=[zeros(4,1);Y];

%replace the contact that colected charge with the signal after going to the shapper
Resp(8,:)=Y';
Resp(:,1:DL)=Resp(:,a-DL+1:a);

%creates noise and add it to the input signal
noise = 0.00166 .* randn(size(Resp));
Respnoisy = Resp + noise;

%without noise
[Gains, yhat] = lagdecomp(Resp);
%with noise
[Gainsnoisy, yhatnoisy] = lagdecomp(Respnoisy);

% plots signal with and w/o noise
for p=1:b
    %without noise
    figure, plot(Resp(p,:))'
    hold;
    plot(yhat(p,:),'r');
    titletext = sprintf('signal %d of %d', p, b);
    title(titletext);
    %witnnoise
    figure, plot(Respnoisy(p,:))'
    hold;
    plot(yhatnoisy(p,:),'g');
    titletext = sprintf('signal with noise %d of %d', p, b);
    title(titletext);
    
end

%calculates the mse for both case on time
mse = diag((Resp-yhat)*(Resp-yhat)');
msewnoise = diag((Respnoisy-yhatnoisy)*(Respnoisy-yhatnoisy)');


