function [w1,w0,bw,pbb] = conv2lpp(ty,pb,sb,w3d)
% CONV2LPP Convert filter specifications to analog LP Prototype.
%
% [WPS,WN,BW,PBS] = CONV2LPP(TY,Fp,Fs,W3) Convert to LPP
% TY = 'lp, 'hp',' bp', 'bs',
% Fp, Fs, F3 contain the passband, stopband and 3dB edge(s)
% For 'bp' & 'bs' if F3 is scalar, it is taken as the center freq. F0
% WPS = [wp, ws, <w3dB>] = returns normalized band edges.
% WN = normalizing frequency for 'lp' and 'hp'
% WN = center frequency W0 for 'bp' or 'bs'
% BW = bandwidth for 'bp' and 'bs' only
% PBS = geometrically symmetric passband edges
% NOTE: Returned values have same units as input frequencies
%
% NOTE: For 'bp' and 'bs', bands are made geometrically symmetric using
% a FIXED W0 or W3 or Fp (if given as [Fp1 Fp2]) or Fs IN THAT ORDER
%
% CONV2LPP (with no input arguments) invokes the following example:
% % Find the LPP specifications corresponding to a BP filter with
% % passband edges = [20 30]Hz and stopband edges = [10 40]Hz
% % Note: Since Fp is given as [20 30], the passband is held fixed.
% >>[fps,f0,BW,pbs] = conv2lpp('bp',[20 30],[10 40])
% ADSP Toolbox: Version 2.0
% For use with "Analog and Digital Signal Processing", 2nd Ed.
% Published by PWS Publishing Co.
%
% Ashok Ambardar, EE Dept. MTU, Houghton, MI 49931, USA
% http://www.ee.mtu/faculty/akambard.html
% e-mail: akambard@mtu.edu
% Copyright (c) 1998
if nargin==0,help conv2lpp,disp('Strike a key to see results of the example')
pause,[fps,f0,BW,pbs]=conv2lpp('bp',[20 30],[10 40]),return,end
if nargin<4,w3d=0;end
w1=[];w0=[];bw=[];pbb=[]; %output arguments
%ERROR CHECK
ty=ty(1:2);
e=0;if ty=='lp',if pb>sb,e=1;end
elseif ty=='hp',if sb>pb,e=1;end
elseif ty=='bp',if length(pb)==2,if length(sb)==2
pp=abs(diff(pb));ss=abs(diff(sb));if pp>ss,e=1;end,end,end
elseif ty=='bs',if length(pb)==2,if length(sb)==2
pp=abs(diff(pb));ss=abs(diff(sb));if ss>pp,e=1;end,end,end
else,error('Unknown type. Use lp hp bp or bs in single quotes'),return,end
if e==1,error('Passband and stopband switched'),return,end
lwp=length(pb);lws=length(sb);if w3d==0,lw3=0;else,lw3=length(w3d);end
if lw3==1,w02=w3d*w3d;
elseif lw3==2,w02=w3d(1)*w3d(2);
elseif lwp==2,w02=pb(1)*pb(2);
elseif lws==2,w02=sb(1)*sb(2);
else,w02=pb*pb;end
w0=sqrt(w02);
if ty=='lp',w1=[pb(1) sb(1)];
if lw3==1,w1=[w1 w3d];end,
w1=w1/w0;pbb=pb(1);
elseif ty=='hp',
w1=[1/pb(1) 1/sb(1)];
if lw3==1,w1=[w1 1/w3d];end,
w1=w1*w0;pbb=pb(1);
else
if lws==1,sb(2)=w02/sb(1);end,
if lwp==1,pb(2)=w02/pb(1);end,
w=sort([pb sb]);
if w02>w(1)*w(4),w(1)=w02/w(4);else,w(4)=w02/w(1);end
if w02>w(2)*w(3),w(3)=w02/w(2);else,w(2)=w02/w(3);end
b1=w(3)-w(2);b2=w(4)-w(1);
if lw3==2,b3=abs(w3d(2)-w3d(1));end
if ty=='bp',bw=b1;pbb=[w(2) w(3)];else,bw=b2;pbb=[w(1) w(4)];end
if lw3==2,bw=b3;end
if ty=='bp',w1=[b1 b2];else,w1=[b2 b1];end
if lw3==2,w1=[w1 b3];end,
if ty=='bp',w1=w1/bw;else,w1=bw./w1;end
end
if w1(1)>w1(2),error('Cannot meet given specifications'),end