%
%  guibut.m
%  Graphical interface for the design of digital Butterworth 
%  filters with unequal numerator and denominator degrees.
%  input
%    Z  : total number of zeros
%    P  : total number of (nontrivial) poles
%
%  Example
%       Z = 10; P = 2;
%       guibut
%
%  Ivan Selesnick (selesi@ece.rice.edu)
%  Rice University, March 1996.
%  See: "Generalized Digital Butterworth Filter Design"
%  in the proc. of ICASSP 96, Atlanta, 
%  by I. W. Selesnick and C. S. Burrus 

%  required subprograms : btable.m, bupdate.m, choose.m


table = btable(Z,P);

%%%%%%%%%%%%%%%%%%%% DISPLAY TABLE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp(' Table:')
disp(' ')
disp('    L         M         N         wo_min/pi wo_max/pi')
disp(' ')
disp(table)

%  L+M : total number of zeros
%  N   : total number of poles
%
%  L : number of zeros at z=-1
%  M : number of zeros contributing to passband flatness
%  N : number of poles


f1 = figure(1);
set(f1,'backingstore','off')
clg

%%%%%%%%%%%%%% slide bar dimensions etc %%%%%%%%%%%

width = 0.6;
slide_ht = 0.05;
txt_ht = 0.04;
left_sd = 0.2;
bot_sd = 0.23;
space = 0.12;

xo_init = 0.2;
fine_init = 0.5;
fine_scale = 0.025;

%%%%%%%%%%%%%%%% main slide bar %%%%%%%%%%%%%%%%%%%

aslide = uicontrol(f1,...
        'style', 'slider',...
	'units','normalized',...
        'position', [left_sd bot_sd width slide_ht],...
	'value',xo_init,...
	'callback','[old_Mag,old_ar,old_br,old_L]=bupdate(Z,P,(get(aslide,''value'')+fine_scale*(get(bslide,''value'')-0.5))*pi,table,mag_ax,pz_ax,old_Mag,old_ar,old_br,old_L);');

%%%%%%%%%%%%%%%% text for main slide bar %%%%%%%%%%

aslide_lbl = uicontrol(f1,...
	'style','text',...
        'units','normalized',...
	'string','half-mag. freq.',...
	'position',[left_sd bot_sd+slide_ht width txt_ht]);

%%%%%%%%%%%%%%%% fine tuning slide bar %%%%%%%%%%%%

bslide = uicontrol(f1,...
        'style', 'slider',...
        'units','normalized',...
        'position', [left_sd bot_sd-space width slide_ht],...
        'value',fine_init,...
	'callback','[old_Mag,old_ar,old_br,old_L]=bupdate(Z,P,(get(aslide,''value'')+fine_scale*(get(bslide,''value'')-0.5))*pi,table,mag_ax,pz_ax,old_Mag,old_ar,old_br,old_L);');

%%%%%%%%%%%%%%%% text for fine tuning slide bar %%%

bslide_lbl = uicontrol(f1,...
        'style','text',...
        'units','normalized',...
        'string','fine tuning',...
        'position',[left_sd bot_sd+slide_ht-space width txt_ht]);


%%%%%%%%%%%%%%%% set up plot stuff %%%%%%%%%%%%%%%%

% ---- PLOT MAGNITUDE ----
mag_ax = axes;
set(mag_ax,'xlabel',text(0,0,'w/pi'),...
	'title',text(0,0,'Frequency response magnitude'),...
	'box','on',...
	'xlim' ,[0 1],...
	'ylim' ,[-0.1 1.1],...
	'drawmode','fast',...
	'position',[.08 0.4 0.45 0.45])

% ---- PLOT POLE-ZERO DIAGRAM ----
pz_ax = axes;
set(pz_ax,'title',text(0,0,'Pole-zero plot'),...
	'box','on',...
	'xlim' ,[-1 1]*1.2,...
	'ylim' ,[-1 1]*1.2,...
	'drawmode','fast',...
	'position',[.55 0.4 0.45 0.45])
circ = exp([0:100]/100 * 2 * pi * sqrt(-1));
lc = line(real(circ),imag(circ),'linestyle','--','erasemode','none');
axis('square')

old_Mag = ones(1,2^6+1);
old_ar = [];
old_br = [];
old_L = 1;

[old_Mag,old_ar,old_br,old_L]=...
	bupdate(Z,P,xo_init*pi,table,mag_ax,pz_ax,old_Mag,old_ar,old_br,old_L);





