function rav=urlaz2(rev,UAV)
%URLAZ2  controller parameter conversion inside Laplace(s) and z-domain(z)
%        based on approximations, applicable to PID structures with
%        delayed derivative part
%        rav=urlaz2(rev(,UAV))
%        input  vector: rev=[T Ri 0 0 Fo x1 x2 x3 x4]
%        output vector: rav=[y1,y2,y3,y4]
%  UVA: optionally switch off disp commands: 1=off; any=on,  default: 0
%  T:  sampling time (precondition: same time unit for all time quantities)
%  Ri: direction of the conversion  (1)   s to z
%                                   (any) z to s
%  Fo: Formula == Choice of an approximation
%   (1) 1. AND 2. DIFF via 1. order approximation      
%      - OR -    INT using RF and positive error PLUS 1. DIFF via 1. order
%   (2) INT using RF AND negative error PLUS 1. DIFF via 1. order
%   (3) INT using TF PLUS 1. DIFF via 1. order
%   (4)==(4b) DIFF: 1. DIFF via 2. order, 2. DIFF via 1. order 
%   (5) Bilinear Transform
%   (6) TRANS of 0. order S&H PLUS controller
%   (7)==(4a) same as (4)==(4a), but: denominator with 1. orders only
%
%  x1,y1  x2,y2  x3,y3   x4,y4
%   Vpid    Tn     Tv     Tz    = Laplace-domain: Vpid(1+1/pTn+pTv/(1+pTz))  
%    Vr     d1     d2     b1    = Z-domain:                                   
%                         Vr(1+d1(z^-1)+d2(z^-2))/[(1-(z^-1))*(1+b1(z^-1))]
%
%  applied abbreviations:
%             DIFF   - approximation of differentiation
%             INT    - approximation of integration
%             RF     - rectangular formula
%             TF     - trapezoid formula
%             TRANS  - Z-transform
%             S&H    - sample & hold

narginchk(0,2); if nargin==2 if UVA~=1; UVA=0; end
else UVA=0; end                 %no prevention of disp commands
T=rev(1); Ri=rev(2); Fo=rev(5); % sampling time / direction / parameters

  if Ri==1         Vpid=rev(6); Tn=rev(7); Tv=rev(8); Tz=rev(9);
                   a=T+Tz;       b=Tv/T; c=Tz/Tn; d=(Tv+Tz)/T;
                   e=(Tn+Tz)/Tn; f=T/Tn; g=Tz/T;
     if     Fo==1  Kc=T/a; q=a/T+a/Tn+b; d1=1+c+2*d;     d2=d;    b1=Tz/a;
     elseif Fo==2  Kc=T/a; q=a/T+b;      d1=1-a/Tn+2*d;  d2=-c+d; b1=Tz/a;
     elseif Fo==3  Kc=T/a; q=e+f/2+d;    d1=1+c-f/2+2*d; d2=d;    b1=Tz/a;
     elseif Fo==4  % ==4b
                   Kc=T/((3/2)*T+Tz); q=((3/2)*T+Tz)/T+(T+(3/2)*Tz)/Tn+b;
                   d1=2+2*c+2*d;      d2=e/2+d; b1=(Tz+0.5*T)/(Tz+(3/2)*T);
     elseif Fo==5  Kc=T/((1/2)*T+Tz); q=e/2+f/4+d;
                   d1=-f/2+2*d;     d2=d+f/4-e/2; b1=(Tz-0.5*T)/(Tz+0.5*T);  
     elseif Fo==6  Kc=T/Tz;           q=d;          b1=exp(-T/Tz);
                   d1=(2*Tv+Tz)/T-c-g*(-b1); d2=b+(c-g)*(-b1);    %b1!!
     elseif Fo==7  % == 4a
                   Kc=T/a;            q=((3/2)*T+Tz)/T+(T+(3/2)*Tz)/Tn+b;
                   d1=2+2*c+2*d;      d2=e/2+d;                   b1=Tz/a;           
     end
     Vr=Vpid*Kc*q; d1=-d1/q; d2=d2/q; b1=-b1;
     if UVA~=1 disp('[Vr d1 d2 b1] = '); end
     rav=[Vr d1 d2 b1];
  else              % Q=1/q
                    Vr=rev(6); d1=rev(7); d2=rev(8); b1=rev(9);
                    Kc=1+b1;
     if     Fo==1   Tz=b1/Kc; Q=(b1-d1-d2*(2+b1))/Kc; Tv=d2/Q+Tz;
     elseif Fo==2   Tz=b1/Kc; Q=(1+2*b1+d1*b1-d2)/Kc;
                              Tv=(d2-d1*b1)/((1+2*b1)*Q)+Tz*b1/(1+2*b1);
     elseif Fo==3   Tz=b1/Kc; Q=(1+3*b1-d1*(1-b1)-d2*(3+b1))/2/Kc;
                              Tv=d2/Q+Tz;
     elseif Fo==4   % == 4b
                    Tz=(1+3*b1)/2/Kc; Q=(1+3*b1-d1*(1-b1)-d2*(3+b1))/2/Kc;
                              Tv=(d1+4*d2)/2/Q+Tz;
  elseif Fo==5 Tz=(b1-1)/(b1+1)/2; Q=(1+3*b1-d1*(1-b1)-d2*(3+b1))/2/(1+b1);
                              Tv=(d2-d1+1)/4/Q+Tz;
     elseif Fo==6   Tz=1/log(-b1); Q=(1+2*b1+d1*b1-d2)/Kc;
                             Tv=(d2-b1*(d1-b1))/(1+b1)/Q*(-Tz*T)/T; %Tz!!!
                             % OR Tv2=((1+d2-b1*d1)/Q-(1-b1))*(-Tz*T)/2/T;
                             % OR Tv3=((d2-b1*(1+d1))/Q+b1)*(-Tz*T)/T;
     elseif Fo==7   % == 4a
                    Tz=b1/Kc; Q=(b1-d1-d2*(2+b1))/Kc;
                              Tv=(d1+4*d2)/2/Q+Tz;
     end
     Vpid=Vr*Q/Kc; Tn=Q*T/(1+d1+d2); Tv=Tv*T; Tz=-Tz*T;
                                                     %Tv2=Tv2*T, Tv3=Tv3*T,
     if UVA~=1 disp('[Vpid Tn Tv Tz] = '); end
     rav=[Vpid Tn Tv Tz]; 
  end
  