% magshape
%
% Graphical user interface for shaping filter specification.
% Each filter is specified by a few characteristic points of
% its magnitude vs. frequency profile.  A rational fit of
% these data points is then performed and the SYSTEM matrix
% of each filter is written in the MATLAB variable of the same
% name.
%
% Several filters can be specified in parallel. If some filters
% are already defined in the MATLAB environment, their magnitude
% response is automatically drawn in the MAGSHAPE worksheet.
%
%
% See also  MRFIT, FRFIT.

% Author: P. Gahinet   10/94
% Copyright 1995-2004 The MathWorks, Inc.
%       $Revision: 1.1.6.1 $


fn = [tempname '.mat'];
save(fn)

xmin=-3; xmax=3; ymin=-80; ymax=80;
HDL_fix=zeros(12,1); HDL_filt=[];
Filt_who=[]; Active_filt=-1; Order_filt=-1;

DefUIBgC = get(0,'DefaultUIcontrolBackgroundColor');
comp = computer;
if all(comp(1:2)=='PC'),
   DefUIBgC = 192/255*ones(1,3);
end


% Figure window
%--------------
figure('units','nor','position',[.1 .1  .6 .7],...
               'name','Graphical specification of shaping filters',...
               'user','magshape','menubar','none');



% Dialog boxes
%-------------

HDL_fix(8:9)=[uicontrol('sty','text','units','nor','pos',[.25 .5 .5 .2])
              uicontrol('sty','text','horiz','center','units','nor',...
              'pos',[.25 .6 .5 .1],...
              'str','Specify the filter names (maximum of 5)')];


% Axes
%-----
set(gca,'Xscale','log','Xlabel',text(0,0,'Frequency (rd/s)'),...
        'Ylabel',text(0,0,'Magnitude (dB)'),...
        'position',[0.07 0.29 0.84 0.69],...
        'xlim',[10^xmin,10^xmax],'ylim',[ymin,ymax],...
        'nextplot','add','xgrid','on','ygrid','on','box','on');



% UI area
% -------
uicontrol('sty','text','units','nor','pos',[0 0 1 .22],'backg',DefUIBgC);
uicontrol('sty','text','units','nor','pos',[0.01 .185 .15 .03],...
          'horiz','left','str','Filter names: ','backg',DefUIBgC);
uicontrol('sty','text','units','nor','pos',[0.51 .185 .45 .03],...
          'horiz','left','str',' ','tag','MAGstatus');
HDL_fix(1)=uicontrol('sty','edit','string','  ','backg','w','horiz','left',...
          'units','nor','pos',[.17 .185 .3 .03],'callb',...
          ['[HDL_filt,Filt_who]=magreq([HDL_filt;HDL_fix(8:9)],' ...
           'Filt_who,''filname'',get(HDL_fix(1),''str''));' ...
           'if ~isempty(HDL_filt), ' ...
              'eval(get(HDL_filt(1),''callb'')); end,' ]);


% Shape Specification
%--------------------
uicontrol('sty','frame','units','nor','pos',[0.005 0.005 .20 .17],...
          'backg',DefUIBgC);
uicontrol('sty','text','units','nor','pos',[0.015 0.14 .18 .03],...
          'horiz','center','str','Filter shaping','backg',DefUIBgC);
HDL_fix(2)=uicontrol('style','radio','units','nor','posi',...
          [.015 .096 .18 .035],'string','add point','value',1, ...
          'backg',DefUIBgC,'callb',...
          ['set(HDL_fix(2),''value'',1);set(HDL_fix([3,4]),''value'',0);' ...
           'set(gcf,''WindowButtonDownFcn'',' ...
           ' ''magreq(HDL_filt,Active_filt,''''addpt'''');'');']);

HDL_fix(3)=uicontrol('style','radio','units','nor','posi',...
          [.015 .054 .18 .035],'string','move point',...
          'backg',DefUIBgC,'callb',...
          ['set(HDL_fix(3),''value'',1);set(HDL_fix([2,4]),''value'',0);' ...
           'set(gcf,''WindowButtonDownFcn'',' ...
           ' ''magreq(HDL_filt,Active_filt,''''movpt'''');'');']);


HDL_fix(4)=uicontrol('style','radio','units','nor','posi',...
          [.015 .012 .18 .035],'string','delete point',...
          'backg',DefUIBgC,'callb',...
          ['set(HDL_fix(4),''value'',1);set(HDL_fix([2,3]),''value'',0);' ...
           'set(gcf,''WindowButtonDownFcn'',' ...
           ' ''magreq(HDL_filt,Active_filt,''''delpt'''');'');']);
eval(get(HDL_fix(2),'callb'));




% Fitting
%--------
uicontrol('sty','frame','units','nor','pos',[0.215 0.005 .30 .17],...
          'backg',DefUIBgC);
uicontrol('sty','text','units','nor','pos',[0.225 0.14 .28 .03],...
          'horiz','center','str','Rational fit','backg',DefUIBgC);

HDL_fix(12)=uicontrol('style','push','units','nor','posi',...
          [.295 .02 .14 .04],'string','fit data','callb',...
          ['if Order_filt < 0, ' ...
             'set(HDL_fix(9),''str'',''Specify the filter order'');' ...
             'set(HDL_fix(8:9),''vis'',''on'');' ...
           'else, ' ...
             'new_wf=magreq(HDL_filt,Active_filt,''fit'',Order_filt);' ...
             'if ~isempty(new_wf),' ...
             'eval([dblnk(Filt_who(Active_filt,:)) ''=new_wf;'']); ' ...
             'end , clear new_wf;' ...
           'end']);

uicontrol('sty','text','units','nor','pos',[0.235 .08 0.11 .03],...
          'horiz','left','str','filter order: ','backg',DefUIBgC);
HDL_fix(7)=uicontrol('sty','edit','string','  ','backg','w','horiz','left',...
          'units','nor','pos',[.35 .08 0.12 .03],'callb', ...
          ['if ~isempty(dblnk(get(HDL_fix(7),''str''))),' ...
              'Order_filt=eval(get(HDL_fix(7),''str''));' ...
              'set(HDL_fix(8:9),''vis'',''off''); ' ...
              'eval(get(HDL_fix(12),''callb''));' ...
           'else, Order_filt=-1; end']);





% Axes control
%-------------
uicontrol('sty','frame','units','nor','pos',[0.525 0.005 .47 .17],...
          'backg',DefUIBgC);
uicontrol('sty','text','units','nor','pos',[0.535 0.14 .45 .03],...
          'horiz','center','str','Axes layout','backg',DefUIBgC);
uicontrol('sty','text','units','nor','horiz','left',...
          'pos',[0.54 .10 .16 .03],'str','xmin,xmax (log): ',...
          'backg',DefUIBgC);
HDL_fix(5)=uicontrol('sty','edit','string','  ','backg','w','horiz','left',...
          'units','nor','pos',[.72 .10 0.2 .03],...
          'callb','magreq(gca,get(HDL_fix(5),''str''),''setx'');');
set(HDL_fix(5),'str',[' ' num2str(xmin) blanks(3) num2str(xmax)]);

uicontrol('sty','text','units','nor','pos',[0.54 .065 .16 .03],...
          'horiz','left','str','ymin,ymax (dB): ',...
          'backg',DefUIBgC);
HDL_fix(6)=uicontrol('sty','edit','string','  ','backg','w','horiz','left',...
          'units','nor','pos',[.72 .065 0.2 .03],...
          'callb','magreq(gca,get(HDL_fix(6),''str''),''sety'');');
set(HDL_fix(6),'str',[' ' num2str(ymin) blanks(3) num2str(ymax)]);

uicontrol('style','push','units','nor','posi',[.545 .015 .10 .04],...
          'string','grid on/off','callback','grid');

uicontrol('style','push','units','nor','posi',[.655 .015 .10 .04],...
          'string','clear','callb',...
          'magreq(HDL_filt,Active_filt,''clear'')');

uicontrol('style','push','units','nor','posi',[.765 .015 .10 .04],...
          'string','refresh','callb','refresh');

uicontrol('style','push','units','nor','posi',[.875 .015 .10 .04],...
          'string','close','callb',...
          ['delete(gcf);' ...
           'clear Active_filt Filt_who HDL_fix HDL_filt Order_filt;']);

set(gcf,'HandleVisibility','callback')





% restore environment

clear xmin xmax ymin ymax DefUIBgC comp
load(fn)
delete(fn), clear fn

