% Test script for sDomainFit
% 
% L. Ferraioli 02-12-08
% 
% $Id: test_ao_sDomainFit_1.m,v 1.5 2009/08/27 11:39:23 luigi Exp $
% 
%% Building a frequency response

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

iplot(a)

%% Fitting 1
% Check if Mean Square Error is lower than FITTOL and its relative
% variation is lower than MSEVARTOL

% Fitting parameter list
pl_fit = plist('AutoSearch','on',...
  'StartPoles',[],...
  'StartPolesOpt','clog',...
  'maxiter',40,...
  'minorder',15,...
  'maxorder',20,...
  'weights',[],...
  'weightparam','abs',...
  'CONDTYPE','MSE',...
  'FITTOL',1e-3,...
  'MSEVARTOL',1e-2,...
  'Plot','on',...
  'ForceStability','off',...
  'direct term','off',...
  'CheckProgress','off');

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

%% Fitting 2
% Check if Rresiduals log difference is larger than FITTOL and MSE relative
% variation is lower than MSEVARTOL

% Fitting parameter list
pl_fit = plist('AutoSearch','on',...
  'StartPoles',[],...
  'StartPolesOpt','clog',...
  'maxiter',40,...
  'minorder',15,...
  'maxorder',20,...
  'weights',[],...
  'weightparam','abs',...
  'CONDTYPE','RLD',...
  'FITTOL',1,...
  'MSEVARTOL',1e-2,...
  'Plot','off',...
  'ForceStability','off',...
  'direct term','off',...
  'CheckProgress','off');

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

%% Fitting 3
% Check if Rresiduals spectral flatness is larger than FITTOL and MSE relative
% variation is lower than MSEVARTOL

% Fitting parameter list
pl_fit = plist('AutoSearch','on',...
  'StartPoles',[],...
  'StartPolesOpt','clog',...
  'maxiter',40,...
  'minorder',15,...
  'maxorder',20,...
  'weights',[],...
  'weightparam','abs',...
  'CONDTYPE','RSF',...
  'FITTOL',0.03,...
  'MSEVARTOL',1e-2,...
  'Plot','off',...
  'ForceStability','off',...
  'direct term','off',...
  'CheckProgress','off');

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

%% Comparison

resp    = mod.objs.procinfo.find('FIT_RESP');
resids  = mod.objs.procinfo.find('FIT_RESIDUALS');
mse     = mod.objs.procinfo.find('FIT_MSE');
iplot(a,resp,abs(resids))
iplot(mse,plist('Yscales','log'))

%% Help test

pl_data = plist('fsfcn', '(1e-3./(f).^2 + 1e3./(0.001+f) + 1e5.*f.^2).*1e-10', 'f1', 1e-6, 'f2', 5, 'nf', 1000);
a = ao(pl_data);
a.setName;

% Fitting parameter list
pl_fit = plist(...
  'AutoSearch','on',...
  'StartPolesOpt','clog',...
  'maxiter',50,...
  'minorder',7,...
  'maxorder',15,...
  'weightparam','abs',...
  'CONDTYPE','MSE',...
  'FITTOL',1e-3,...
  'MSEVARTOL',1e-2,...
  'Plot','on',...
  'ForceStability','off');

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




