
function [old_Mag,old_ar,old_br,old_L] = bupdate(Z,P,wo,table,mag_ax,pz_ax,old_Mag,old_ar,old_br,old_L)

SN = 1e-7;

if wo == 0
	wo = 0.00001;
elseif wo > pi-0.00001;
	wo = 0.9999*pi;
end

k = max(find((table(:,4) < wo/pi+SN) & (table(:,5) > wo/pi-SN)));
%%%%%%%%%%%%%%%%%%%% error %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if length(k) == 0
        disp('    The half-magnitude frequency, wo, must lie in ')
        disp('    the appropriate interval. See the table. ')
	break
end

%%%%%%%%%%%%%%%%%%%% CALCULATE x-Domain function %%%%%%%%%%%%%%%%%%
L = table(k,1);
M = table(k,2);
N = table(k,3);
xo = (1-cos(wo))/2;
Fo = (1/2)^2;

if N == 0
	%%%%%%%%%%%% FIR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	k = M-1:-1:0;
	R = [0, choose(M-k-1,0).*choose(L+k-1,k)];
   	T = [choose(M-k-2,-1).*choose(L+k,k), 0];
	c = (Fo/(1-xo)^L - polyval(R,xo))/polyval(T,xo);
	S = R + c*T;
	Q = 1;
elseif M == 0
	%%%%%%%%%%%% ALL ZEROS at z = -1 %%%%%%%%%%%%%%%%%%%%%%%%%%
	k = min([L,N]):-1:0;
	Q = choose(L,k).*(-1).^k;
	if L < N
		Q = [zeros(1,N-L), Q];
	end
	c = ((1/Fo)*(1-xo)^L - polyval(Q,xo))/(xo^N);
	Q(1) = Q(1) + c;
	S = 1;
else % M > 0
	%%%%%%%%%%%% Some Zeros Contribute To Passband %%%%%%%%%%%%
	k = M-1:-1:0;
	R = [0, choose(M+N-k-1,N).*choose(L-N+k-1,k)];
	T = [choose(M+N-k-2,N-1).*choose(L-N+k,k), 0];
	k = N:-1:0;
	AGR = choose(M+N-k-1,M-1).*choose(M+L-1,k).*(-1).^k;
	AGT = choose(M+N-k-1,M-1).*choose(M+L-1,k-1).*(-1).^(k-1);
	c = (((1-xo)^L)*polyval(R,xo) - Fo*polyval(AGR,xo)) / ...
        	(Fo*polyval(AGT,xo) - ((1-xo)^L)*polyval(T,xo));
	S = R + c*T;
	Q = AGR + c*AGT;
end

%%%%%%%%%%%%%%%%%%%% convert to z-domain %%%%%%%%%%%%%%%%%%%%%%%%%%

tmp = 1 - 2*roots(S);
br1 = tmp+sqrt(tmp.^2-1);
br2 = tmp-sqrt(tmp.^2-1);
br = sort([br1; br2]+eps*sqrt(-1));     % sort by absolute value
br = br(1:M);                           % take roots INSIDE unit circle
b2 = real(poly(br));

tmp = 1 - 2*roots(Q);
ar1 = tmp+sqrt(tmp.^2-1);
ar2 = tmp-sqrt(tmp.^2-1);
ar = sort([ar1; ar2]+eps*sqrt(-1));     % sort by absolute value
ar = ar(1:N);                           % take roots INSIDE unit circle
a = real(poly(ar));

%%%%%%%%%%%%%%%%%%%% construct b1  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
b1 = choose(L,0:L);

%%%%%%%%%%%%%%%%%%%% normalize %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
b2 = b2*sum(a)/(sum(b1)*sum(b2));
b = conv(b1,b2);


%%%%%%%%%%%%%%%%%%%% DISPLAY RESULTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% if length(a)

	% ---- PLOT MAGNITUDE ----
	Mag = abs(fft(b,2^7)./fft(a,2^7));
	Mag = Mag(1:2^6+1);
	w = [0:2^6]'/(2^6);
        axes(mag_ax)
        l1 = line(w,old_Mag,'color','k','erasemode','none');
	l1 = line(w,Mag,'color','y','erasemode','none');
	old_Mag = Mag;

	% ---- PLOT POLE-ZERO DIAGRAM ----
	axes(pz_ax)
	l2 = line(real(old_ar),imag(old_ar),'linestyle','x','color','k','erasemode','none');
	l3 = line(real(old_br),imag(old_br),'linestyle','o','color','k','erasemode','none');
	I = sqrt(-1);
	ar = ar+SN*I;
	l2 = line(real(ar),imag(ar),'color','y','linestyle','x','erasemode','none');
	br = br+SN*I;
        l3 = line(real(br),imag(br),'color','y','linestyle','o','erasemode','none');
        l4 = line(-1,0,'linestyle','o','erasemode','none');
        old_ar = ar;
        old_br = br;

	if old_L ~= L
	 s1 = num2str(old_L);
	 t1 = text(-0.9,0.1,s1,'color','k','erasemode','none');
	 s1 = num2str(L);
	 t1 = text(-0.9,0.1,s1,'color','w','erasemode','none');
	 old_L = L;
	end

% end


