% Training session Topic 5 exercise 02
% 
% System identification in z-domain 2
% 
% 1) Load fsdata object from file
% 2) Fit loaded TF data with zDomainFit and fixed order
% 3) Compare results
% 
% L FERRAIOLI 22-02-09
%
% $Id: TrainigSession_T5_Ex02.m,v 1.3 2009/02/25 18:18:45 luigi Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% 1) load fsdata

% load AO from file
rfilt = ao(plist('filename', 'topic5\T5_Ex02_rfilt.xml'));
iplot(rfilt)

%% 2) Fitting TF - fixed model order

% Loaded fsdata are the response of an order 19 partial fractioned filter.
% We now try to fitting the loaded filter response with zDomainFit with a
% fixed model order.
% We set Autosearch to off, in this case the function do not perform
% accuracy test but simply run how far maximum number of iteration is
% reached. Model order is fixed by minorder parameter. 

plfit1 = plist('FS',10,... % Sampling frequency for the model filters
  'AutoSearch','off',... % Automatically search for a good model
  'StartPolesOpt','c1',... % Define the properties of the starting poles - complex distributed in the unitary circle
  'maxiter',30,... % maximum number of iteration per model order
  'minorder',19,... % fixed model order
  'weightparam','abs',... % assign weights as 1./abs(data)
  'Plot','on',... % set the plot on or off
  'ForceStability','on',... % force to output a stable ploes model
  'CheckProgress','off'); % display fitting progress on the command window

% Do the fit
fobj = zDomainFit(rfilt,plfit1);
% setting input and output units for fitted model

%% 3) Compare results

% Extracting residues and poles from fit results
fRes = zeros(numel(fobj),1); % fit residue vector initialization
fPoles = zeros(numel(fobj),1); % fit poles vector initialization

% extracting data from fitted filters
for ii = 1:numel(fobj)
  fRes(ii,1) = fobj(ii).a(1);
  fPoles(ii,1) = -1*fobj(ii).b(2);
end
[fRes,idx] = sort(fRes);
fPoles = fPoles(idx);

% starting model residues and poles
mRes = [2.44554138162509e-011 - 1.79482547894083e-011i;
2.44554138162509e-011 + 1.79482547894083e-011i;
2.66402334803101e-009 +  1.1025122049153e-009i;
2.66402334803101e-009 -  1.1025122049153e-009i;
-7.3560293387644e-009;
-1.82811618589835e-009 - 1.21803627800855e-009i;
-1.82811618589835e-009 + 1.21803627800855e-009i;
1.16258677367555e-009;
1.65216557639319e-016;                         
-1.78092396888606e-016;
-2.80420398962379e-017;
9.21305973049041e-013 - 8.24686706827269e-014i;
9.21305973049041e-013 + 8.24686706827269e-014i;
5.10730060739905e-010 - 3.76571756625722e-011i;
5.10730060739905e-010 + 3.76571756625722e-011i;
3.45893698149735e-009;
3.98139182134446e-014 - 8.25503935419059e-014i;
3.98139182134446e-014 + 8.25503935419059e-014i;
-1.40595719147164e-011];
[mRes,idx] = sort(mRes);

mPoles = [0.843464045655194 - 0.0959986292915475i;
0.843464045655194 + 0.0959986292915475i;
0.953187595424927 - 0.0190043625473383i;
0.953187595424927 + 0.0190043625473383i;
0.967176277937188;
0.995012027005247 - 0.00268322602801729i;
0.995012027005247 + 0.00268322602801729i;
0.996564761885673;
0.999999366165445;
0.999981722418555;
0.999921882627659;
0.999624431675213 - 0.000813407848742761i;
0.999624431675213 + 0.000813407848742761i;
0.997312006278751 - 0.00265611346834941i;
0.997312006278751 + 0.00265611346834941i;
0.990516544257531;
0.477796923118318 - 0.311064085401834i;
0.477796923118318 + 0.311064085401834i;
0];
mPoles = mPoles(idx);

% Check the relative difference
(mRes-fRes)./abs(mRes)
(mPoles-fPoles)./abs(mPoles)
% Results are accurate to the 7th decimal digit

