% TEST_AO_LINLSQSVD tests the linlsqsvd method of the AO class.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% L Ferraioli 10-11-2010
%
% $Id: test_ao_linlsqsvd.m,v 1.2 2011/02/18 16:15:28 luigi Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% 1) Determine the coefficients of a linear combination of noises and
%% comapre with lscov:
%
% Make some data
fs    = 10;
nsecs = 10;
B1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs, 'yunits', 'T'));
B1.setName;
B2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs, 'yunits', 'T'));
B2.setName;
B3 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs, 'yunits', 'T'));
B3.setName;
n  = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs, 'yunits', 'm'));
c = [ao(1,plist('yunits','m/T')) ao(2,plist('yunits','m/T')) ao(3,plist('yunits','m T^-1'))];
y = c(1)*B1 + c(2)*B2 + c(3)*B3 + n;
y.simplifyYunits;
%%% Get a fit with linlsqsvd
pobj1 = linlsqsvd(B1, B2, B3, y);
% do linear combination: using lincom
yfit1 = lincom(B1, B2, B3, pobj1);
yfit1.simplifyYunits;
%%% do fit using lscov
pobj2 = lscov(B1, B2, B3, y);
% do linear combination: using lincom
yfit2 = lincom(B1, B2, B3, pobj2);
yfit2.simplifyYunits;

%%% do linear combination: using eval
yfit3 = pobj1.eval(B1, B2, B3);


% Plot (compare data with fit)
iplot(y, yfit1, yfit2, yfit3)

%% 2) Determine the coefficients of a linear combination of noises:
%
% Make some data
fs    = 10;
nsecs = 10;
x1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs, 'yunits', 'T'));
x1.setName;
x2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs, 'yunits', 'm'));
x2.setName;
x3 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs, 'yunits', 'C'));
x3.setName;
n  = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', nsecs, 'yunits', 'm'));
c = [ao(1,plist('yunits','m/T')) ao(2,plist('yunits','m/m')) ao(3,plist('yunits','m C^-1'))];
y = c(1)*x1 + c(2)*x2 + c(3)*x3 + n;
y.simplifyYunits;

%%% Get a fit for c
pobj = linlsqsvd(x1, x2, x3, y);
% do linear combination: using lincom
yfit1 = lincom(x1, x2, x3, pobj);

%%% do linear combination: using eval
pl_split = plist('times', [1 5]);
yfit2 = pobj.eval(plist('Xdata', {split(x1, pl_split), split(x2, pl_split), split(x3, pl_split)}));

% Plot (compare data with fit)
iplot(y, yfit1, yfit2, plist('Linestyles', {'-','--'}))


