function pzmodel_helper(varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: PZMODEL_HELPER opens the LTPDA pzmodel GUI.
%
% CALL:        pzmodel_helper()
% 
% VERSION:     $Id: pzmodel_helper.m,v 1.12 2009/09/07 17:23:07 nicola Exp $
%
% HISTORY:     07-09-2009 N Tateo
%                 Creation
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% This function draws the PZmodel helper main figure.

  % Some initial setup
  import utils.const.*
  prefs = getappdata(0, 'LTPDApreferences');
  utils.helper.msg(msg.PROC1, 'building Main Figure');

  Screen = get(0,'screensize');  
  mainfig.Gproperties.Screen   = Screen;
  mainfig.Gproperties.Gwidth   = 0.6;
  mainfig.Gproperties.Gheight  = 0.9;
  mainfig.Gproperties.Gborder  = 10;
  fontSize = (prefs.repository.fontsize+2)*(1440/Screen(3));
  
  l = (0.5-mainfig.Gproperties.Gwidth/2);
  b = (0.5-mainfig.Gproperties.Gheight/2);
  w = mainfig.Gproperties.Gwidth;
  h = mainfig.Gproperties.Gheight;
  mainfig.Gproperties.Gposition = [l b w h];
  
  if ~isempty(varargin) && ishandle(varargin{1})
    mainfig.handle = varargin{1};
    set(mainfig.handle, 'Tag', 'PZMODELhelpermainfig');
    set(mainfig.handle, 'Units', 'normalized');
    set(mainfig.handle, 'Visible', 'on');
  else
    %  Initialize and hide the GUI as it is being constructed.
    mainfig.handle = figure('Name', 'PZModel Helper GUI',...
      'NumberTitle', 'off',...
      'Visible','on',...
      'Units','normalized',...
      'Position',mainfig.Gproperties.Gposition,...
      'Toolbar', 'none',...
      'MenuBar', 'none',...
      'Color', 'w',...
      'Resize', 'on',...
      'Tag', 'PZMODELhelpermainfig');
  end
  
%  % DEFINE THE HANDLES VARIABLE
%  handles = struct();
%  handles.pzEdit = findobj(gcf,'Tag','pzEdit');
%  handles.phaseAxes = findobj(gcf,'Tag','phaseAxes');
%  handles.filterStrEdit = findobj(gcf,'Tag','filterStrEdit');
%  handles.fsEdit = findobj(gcf,'Tag','fsEdit');
%  handles.nfEdit = findobj(gcf,'Tag','nfEdit');
%  handles.f2Edit = findobj(gcf,'Tag','f2Edit');
%  handles.f1Edit = findobj(gcf,'Tag','f1Edit');
%  handles.gainEdit = findobj(gcf,'Tag','gainEdit');
%  handles.magAxes = findobj(gcf,'Tag','magAxes');
%  handles.poleList = findobj(gcf,'Tag','poleList');
%  handles.zeroList = findobj(gcf,'Tag','zeroList');
%  handles.main = gcf;
% %  handles. = findobj(gcf,'Tag','');
% %  handles. = findobj(gcf,'Tag','');
% 
% %     'uipanel1'
% %     'uipanel2'
% %     'saveAObtn'
% %     'figureBtn'
% %     'replotBtn'
% %     'GainTxt'
% %     'addZeroBtn'
% %     'addPoleBtn'
% %     'deleteZerosBtn'
% %     'deletePolesBtn'
% %     'clearBtn'
% %     'output' 

  % create empty lists
  poles = [];
  zeros = [];
  setappdata(gcf, 'poles', poles);
  setappdata(gcf, 'zeros', zeros);
  setappdata(gcf, 'filt', []);
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%% DRAW CONTENTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  uip1 = uipanel('Title', 'Response',...
             'FontSize', fontSize-1,...
             'BackgroundColor','white',...
             'Tag', 'uipanel1',...
             'Position',[.35 .15 .64 .84]);

  uip2 = uipanel('Title', 'Pole/zero entry',...
             'FontSize', fontSize-1,...
             'BackgroundColor','white',...
             'Tag', 'uipanel2',...
             'Position',[.01 .6 .32 .39]);

  uip3 = uipanel('Title', 'Plot controls',...
             'FontSize', fontSize-1,...
             'BackgroundColor','white',...
             'Tag', 'uipanel3',...
             'Position',[.01 .15 .32 .44]);

  uicontrol('Style','text',...
             'String', 'MIIR constructor string:',...
             'HorizontalAlignment', 'left',...
             'FontSize', fontSize+2,...
             'FontWeight','bold',...
             'ForeGroundColor','black',...
             'backgroundColor','white',...
             'Units','normalized',...
             'Position', [.02 .115 .3 .03])
  
  uicontrol('Style','edit',...
             'Tag', 'filterStrEdit',...
             'Max',10,...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize+2,...
             'ForeGroundColor','black',...
             'backgroundColor',[.94 .94 .94],...
             'Units','normalized',...
             'Position', [.02 .01 .96 .1])
         
  %%%%%%%%%%%%%%%%%%%%%%
  % First panel contents
  %%%%%%%%%%%%%%%%%%%%%%

  axes('Parent',uip1,...
              'Tag','phaseAxes',...
              'Box','on',...
              'Units','normalized',...
              'Position', [.11 .08 .8 .35]);

  axes('Parent',uip1,...
              'Tag','magAxes',...
              'Box','on',...
              'Units','normalized',...
              'Position', [.11 .52 .8 .43]);
  
  %%%%%%%%%%%%%%%%%%%%%%%
  % Second panel contents
  %%%%%%%%%%%%%%%%%%%%%%%

  uicontrol('Parent',uip2, ...
             'Style','edit',...
             'Tag', 'pzEdit',...
             'Max',1,...
             'String', '1 0',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor',[.92 .92 .92],...
             'Units','normalized',...
             'Position', [.1 .85 .35 .1])

  uicontrol('Parent',uip2, ...
             'Style','text',...
             'String', 'Frequency [Hz] (Q)',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Position', [.45 .83 .5 .1])
  
  uicontrol('Parent',uip2, ...
             'Style','pushbutton',...
             'String', 'Add pole',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Callback', @addPoleBtn_Callback,...
             'Position', [.075 .75 .4 .08])
  
  uicontrol('Parent',uip2, ...
             'Style','pushbutton',...
             'String', 'Add zero',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Callback', @addZeroBtn_Callback,...
             'Position', [.525 .75 .4 .08])
  
  uicontrol('Parent',uip2, ...
             'Style','listbox',...
             'Tag','poleList',...
             'String', '-',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             ... 'Callback', @poleList_Callback,...
             'Position', [.075 .25 .4 .45])
  
  uicontrol('Parent',uip2, ...
             'Style','listbox',...
             'Tag','zeroList',...
             'String', '-',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             ... 'Callback', @zeroList_Callback,...
             'Position', [.525 .25 .4 .45])
 
  uicontrol('Parent',uip2, ...
             'Style','pushbutton',...
             'String', 'Delete',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Callback', @deletePolesBtn_Callback,...
             'Position', [.075 .15 .4 .08])
  
  uicontrol('Parent',uip2, ...
             'Style','pushbutton',...
             'String', 'Delete',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Callback', @deleteZerosBtn_Callback,...
             'Position', [.525 .15 .4 .08])
  
  uicontrol('Parent',uip2, ...
             'Style','pushbutton',...
             'String', 'Clear lists',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Callback', @clearBtn_Callback,...
             'Position', [.075 .04 .85 .08])
         
  %%%%%%%%%%%%%%%%%%%%%%
  % Third panel contents
  %%%%%%%%%%%%%%%%%%%%%%
  
  uicontrol('Parent',uip3, ...
             'Style','edit',...
             'Tag', 'gainEdit',...
             'Callback',@valueEdit_Callback, ...
             'Max',1,...
             'String', '1',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor',[.92 .92 .92],...
             'Units','normalized',...
             'Position', [.1 .87 .3 .08])
  uicontrol('Parent',uip3, ...
             'Style','text',...
             'String', 'Gain',...
             'HorizontalAlignment', 'left',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Position', [.47 .855 .4 .08])

  uicontrol('Parent',uip3, ...
             'Style','edit',...
             'Tag', 'fsEdit',...
             'Callback',@valueEdit_Callback, ...
             'Max',1,...
             'String', '',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor',[.92 .92 .92],...
             'Units','normalized',...
             'Position', [.1 .75 .3 .08])
  uicontrol('Parent',uip3, ...
             'Style','text',...
             'String', 'Sample rate',...
             'HorizontalAlignment', 'left',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Position', [.47 .735 .4 .08])


  uicontrol('Parent',uip3, ...
             'Style','edit',...
             'Tag', 'f1Edit',...
             'Callback',@valueEdit_Callback, ...
             'Max',1,...
             'String', '',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor',[.92 .92 .92],...
             'Units','normalized',...
             'Position', [.1 .63 .3 .08])
  uicontrol('Parent',uip3, ...
             'Style','text',...
             'String', 'f1',...
             'HorizontalAlignment', 'left',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Position', [.47 .615 .4 .08])

  uicontrol('Parent',uip3, ...
             'Style','edit',...
             'Tag', 'f2Edit',...
             'Callback',@valueEdit_Callback, ...
             'Max',1,...
             'String', '',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor',[.92 .92 .92],...
             'Units','normalized',...
             'Position', [.1 .51 .3 .08])
  uicontrol('Parent',uip3, ...
             'Style','text',...
             'String', 'f2',...
             'HorizontalAlignment', 'left',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Position', [.47 .495 .4 .08])

  uicontrol('Parent',uip3, ...
             'Style','edit',...
             'Tag', 'nfEdit',...
             'Callback',@valueEdit_Callback, ...
             'Max',1,...
             'String', '',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor',[.92 .92 .92],...
             'Units','normalized',...
             'Position', [.1 .39 .3 .08])
  uicontrol('Parent',uip3, ...
             'Style','text',...
             'String', 'Nf',...
             'HorizontalAlignment', 'left',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor','w',...
             'Units','normalized',...
             'Position', [.47 .375 .4 .08])

  uicontrol('Parent',uip3, ...
             'Style','pushbutton',...
             'Tag', 'replotBtn',...
             'Callback', @replotBtn_Callback, ...
             'Max',1,...
             'String', 'Replot',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor',[.92 .92 .92],...
             'Units','normalized',...
             'Position', [.1 .27 .8 .07])

  uicontrol('Parent',uip3, ...
             'Style','pushbutton',...
             'Tag', 'figureBtn',...
             'Callback', @figureBtn_Callback, ...
             'Max',1,...
             'String', 'Figure',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor',[.92 .92 .92],...
             'Units','normalized',...
             'Position', [.1 .16 .8 .07])

  uicontrol('Parent',uip3, ...
             'Style','pushbutton',...
             'Tag', 'saveAObtn',...
             'Callback', @saveAObtn_Callback, ...
             'Max',1,...
             'String', 'Save as',...
             'HorizontalAlignment', 'center',...
             'FontSize', fontSize,...
             'ForeGroundColor','black',...
             'backgroundColor',[.92 .92 .92],...
             'Units','normalized',...
             'Position', [.1 .05 .8 .07])



%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%% SUBFUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % --- Executes on button press in addPoleBtn.
    function addPoleBtn_Callback(varargin)
        % hObject    handle to addPoleBtn (see GCBO)
        % eventdata  reserved - to be defined in a future version of MATLAB
        
        % get values from pzEdit
        strVal = get(findobj(gcf,'Tag','pzEdit'), 'String');
        val    = str2num(strVal);
        f      = val(1);
        if length(val) == 2
            q = val(2);
            p = pz(f,q);
        else
            p = pz(f);
        end
        
        poles = getappdata(gcf, 'poles');
        poles = [poles p];
        setappdata(gcf, 'poles', poles);
        
        % update list
        updatePolesList();
        
        % update model
        updateModelResp();
        
    end

    % --- Executes on button press in addZeroBtn.
    function addZeroBtn_Callback(varargin)
        % hObject    handle to addZeroBtn (see GCBO)
        % eventdata  reserved - to be defined in a future version of MATLAB
        
        % get values from pzEdit
        strVal = get(findobj(gcf,'tag','pzEdit'), 'String');
        val    = str2num(strVal);
        f      = val(1);
        if length(val) == 2
            q = val(2);
            z = pz(f,q);
        else
            z = pz(f);
        end
        
        zeros = getappdata(gcf, 'zeros');
        zeros = [zeros z];
        setappdata(gcf, 'zeros', zeros);
        
        % update list
        updateZerosList();
        
        % update model
        updateModelResp();
        
    end

    %----------- Update Poles List -----------------------------
    function updatePolesList(varargin)
        
        poles = getappdata(gcf, 'poles');
        pstr = [];
        for n=1:length(poles)
            p = poles(n);
            f = p.f;
            q = p.q;
            if q>0.5
                pstr = strvcat(pstr, sprintf('%2.2f Hz   Q=%2.2f', f, q));
            else
                pstr = strvcat(pstr, sprintf('%2.2f Hz', f));
            end
        end
        
        if isempty(poles)
            set(findobj(gcf,'tag','poleList'), 'String', '  ');
        else
            set(findobj(gcf,'tag','poleList'), 'String', pstr);
        end
        set(findobj(gcf,'tag','poleList'), 'Value', 1);
        
    end

    %----------- Update Zeros List -----------------------------
    function updateZerosList(varargin)
        
        zeros = getappdata(gcf, 'zeros');
        pstr = [];
        for n=1:length(zeros)
            p = zeros(n);
            f = p.f;
            q = p.q;
            if q>0.5
                pstr = strvcat(pstr, sprintf('%2.2f Hz   Q=%2.2f', f, q));
            else
                pstr = strvcat(pstr, sprintf('%2.2f Hz', f));
            end
        end
        
        if isempty(zeros)
            set(findobj(gcf,'tag','zeroList'), 'String', '  ');
        else
            set(findobj(gcf,'tag','zeroList'), 'String', pstr);
        end
        set(findobj(gcf,'tag','zeroList'), 'Value', 1);
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %----------- Update Model response -----------------------------
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function updateModelResp(varargin)
        
        gain = str2double(get(findobj(gcf,'tag','gainEdit'), 'String'));
        poles = getappdata(gcf, 'poles');
        zeros = getappdata(gcf, 'zeros');
        
        pzm = pzmodel(gain, poles, zeros);
        
        f1s = get(findobj(gcf,'tag','f1Edit'), 'String');
        f2s = get(findobj(gcf,'tag','f2Edit'), 'String');
        nfs = get(findobj(gcf,'tag','nfEdit'), 'String');
        fss = get(findobj(gcf,'tag','fsEdit'), 'String');
        
        if isempty(f1s)
            f1 = getlowerFreq(pzm)/10;
        else
            f1 = str2double(f1s);
        end
        if isempty(f2s)
            f2 = getupperFreq(pzm)*10;
        else
            f2 = str2double(f2s);
        end
        if isempty(nfs)
            nf = 1000;
        else
            nf = str2double(nfs);
        end
        
        if ~isempty(pzm.poles) || ~isempty(pzm.zeros)
            
            % make model response
            a  = resp(pzm, f1, f2, nf);
            d  = a.data;
            mx = d.x;
            my = d.y;
            
            % make IIR response
            if isempty(fss)
                filt     = miir(plist([param('pzmodel', pzm)]));
            else
                filt     = miir(plist([param('pzmodel', pzm) param('fs', str2double(fss))]));
            end
            setappdata(gcf, 'filt', filt);
            set(findobj(gcf,'tag','filterStrEdit'), 'String', ['miir(' string(pzm) ')']);
            
            filtresp = resp(filt, plist(param('f', mx.')));
            d        = filtresp.data;
            fx       = d.x;
            fy       = d.y;
            
            axes(findobj(gcf,'tag','magAxes')); % Select the proper axes
            loglog(mx,abs(my), fx, abs(fy), 'r--')
            grid on;
            axis tight
            xlabel('');
            ylabel('Magnitude');
            legend('Pole/Zero model', 'IIR','Location','SouthWest')
            set(gca,'tag','magAxes')
            
            axes(findobj(gcf,'tag','phaseAxes')); % Select the proper axes
            semilogx(mx, utils.math.phase(my), fx, utils.math.phase(fy), 'r--')
            grid on;
            axis tight
            xlabel('Frequency [Hz]');
            ylabel('Phase [deg]');
            set(gca,'tag','phaseAxes')
        else
            axes(findobj(gcf,'tag','magAxes')); % Select the proper axes
            cla
            axes(findobj(gcf,'tag','phaseAxes')); % Select the proper axes
            cla
        end
        
    end

    function valueEdit_Callback(varargin)                
        updateModelResp()
    end

    % --- Executes on button press in replotBtn.
    function replotBtn_Callback(varargin)
        updateModelResp()
    end

    % --- Executes on button press in figureBtn.
    function figureBtn_Callback(varargin)
        
        gain = str2double(get(findobj(gcf,'tag','gainEdit'), 'String'));
        poles = getappdata(gcf, 'poles');
        zeros = getappdata(gcf, 'zeros');
        
        pzm = pzmodel(gain, poles, zeros);
        
        f1s = get(findobj(gcf,'tag','f1Edit'), 'String');
        f2s = get(findobj(gcf,'tag','f2Edit'), 'String');
        nfs = get(findobj(gcf,'tag','nfEdit'), 'String');
        fss = get(findobj(gcf,'tag','fsEdit'), 'String');
        
        if isempty(f1s)
            f1 = getlowerFreq(pzm)/10;
        else
            f1 = str2double(f1s);
        end
        if isempty(f2s)
            f2 = getupperFreq(pzm)*10;
        else
            f2 = str2double(f2s);
        end
        if isempty(nfs)
            nf = 1000;
        else
            nf = str2double(nfs);
        end
        
        % make model response
        a  = resp(pzm, f1, f2, nf);
        a.setName('PZmodel');
        
        % make IIR response
        if isempty(fss)
            filt     = miir(plist([param('pzmodel', pzm)]));
        else
            filt     = miir(plist([param('pzmodel', pzm) param('fs', str2double(fss))]));
        end
        
        
        filtresp = resp(filt, plist(param('f', a.data.getX)));
        filtresp.setName('IIR filter');
        
        iplot(a, filtresp)
    end

    % --- Executes on button press in clearBtn.
    function clearBtn_Callback(varargin)
        
        setappdata(gcf, 'poles', []);
        setappdata(gcf, 'zeros', []);
        
        % update list
        updateZerosList();
        updatePolesList();
        
        % update model
        updateModelResp();
    end
        
        
    % --- Executes on button press in deletePolesBtn.
    function deletePolesBtn_Callback(varargin)
        
        % get selection from list
        values = get(findobj(gcf,'tag','poleList'), 'Value');
        % get poles
        poles = getappdata(gcf, 'poles');
        po = [];
        for j=1:length(poles)
            if j~=values
                po = [po poles(j)]; %#ok<AGROW>
            end
        end
        setappdata(gcf, 'poles', po);
        updatePolesList();
        % update model
        updateModelResp();
        
    end

    % --- Executes on button press in deleteZerosBtn.
    function deleteZerosBtn_Callback(varargin)
        
        % get selection from list
        values = get(findobj(gcf,'tag','zeroList'), 'Value');
        % get zeros
        zeros = getappdata(gcf, 'zeros');
        zo = [];
        for j=1:length(zeros)
            if j~=values
                zo = [zo zeros(j)]; %#ok<AGROW>
            end
        end
        setappdata(gcf, 'zeros', zo);
        updateZerosList();
        % update model
        updateModelResp();
    end

    % --- Executes on button press in saveAObtn.
    function saveAObtn_Callback(varargin)
        
        % get response
        gain = str2double(get(findobj(gcf,'tag','gainEdit'), 'String'));
        poles = getappdata(gcf, 'poles');
        zeros = getappdata(gcf, 'zeros');
        pzm = pzmodel(gain, poles, zeros);
        
        f1s = get(findobj(gcf,'tag','f1Edit'), 'String');
        f2s = get(findobj(gcf,'tag','f2Edit'), 'String');
        nfs = get(findobj(gcf,'tag','nfEdit'), 'String');
        fss = get(findobj(gcf,'tag','fsEdit'), 'String');
        
        if isempty(f1s)
            f1 = getlowerFreq(pzm)/10;
        else
            f1 = str2double(f1s);
        end
        if isempty(f2s)
            f2 = getupperFreq(pzm)*10;
        else
            f2 = str2double(f2s);
        end
        if isempty(nfs)
            nf = 1000;
        else
            nf = str2double(nfs);
        end
        
        % make model response
        a  = resp(pzm, f1, f2, nf);
        
        % Get filename
        [filename, pathname] = uiputfile('*.xml', 'LTPDA XML file (*.xml)', 'Save as');
        if isequal(filename,0) || isequal(pathname,0)
        else
            fname = fullfile(pathname, filename);
            save(a, fname);
        end
    end


end