% Test script for zDomainFit in two dimensional case
% 
% L. Ferraioli 02-12-08
% 
% $Id: test_ao_zDomainFit_2.m,v 1.5 2009/12/02 16:50:49 luigi Exp $
% 
%% Building a frequency response

% Create a frequency-series AO
pl_data1 = plist('fsfcn', '(1e-3./(f).^2 + 1e3./(0.001+f) + 1e5.*f.^2).*1e-10', 'f1', 1e-6, 'f2', 5, 'nf', 100);
a1 = ao(pl_data1);
a1.setName;

pl_data2 = plist('fsfcn', '(5e-3./(2e-5+f).^2 + 5e3./(0.001+f) + 5e4.*f.^2).*1e-10', 'f1', 1e-6, 'f2', 5, 'nf', 100);
a2 = ao(pl_data2);
a2.setName;

iplot(a1,a2)
a = [a1,a2];

%% Fitting

% Fitting parameter list
pl_fit = plist('FS',10,...
  'AutoSearch','on',...
  'StartPoles',[],...
  'StartPolesOpt','clog',...
  'maxiter',60,...
  'minorder',15,...
  'maxorder',35,...
  'weights',[],...
  'weightparam','abs',...
  'CONDTYPE','MSE',...
  'FITTOL',1e-4,... % check if MSE is lower than 1e-4
  'MSEVARTOL',1e-2,...
  'Plot','off',...
  'ForceStability','off',...
  'CheckProgress','off');

% Do fit
mod = zDomainFit(a, pl_fit);

%% Comparison
resp(2,1) = ao;
resids(2,1) = ao;
mse(2,1) = ao;

for ii = 1:numel(mod.objs)
  resp(ii)    = mod.objs(ii).procinfo.find('FIT_RESP');
  resids(ii)  = mod.objs(ii).procinfo.find('FIT_RESIDUALS');
  mse(ii)     = mod.objs(ii).procinfo.find('FIT_MSE');
end

iplot(a,resp,abs(resids))

