      SUBROUTINE ZTBILI(SAMP,SSPEC,ZSPEC)                                       
C                                                                               
C MODULE         ZTBILI                                                         
C VERSION        1.0                                                            
C PACKAGE        SIGPRO                                                         
C ROUTINE NO     30                                                             
C LAST MODIFIED  09/12/81                                                       
C AUTHOR         N C SEDGWICK                                                   
C                                                                               
C MAPS A SET OF SINGULARITIES IN THE S-PLANE TO SINGULARITIES                   
C IN THE Z-PLANE USING THE BILINEAR Z TRANSFORM.                                
C                                                                               
C PARAMETERS                                                                    
C **********                                                                    
C                                                                               
C SAMP    R*4  SAMPLING FREQUENCY                                               
C SSPEC   R*4A SPECIFICATION OF S-PLANE SINGULARITIES                           
C ZSPEC   R*4A SPECIFICATION OF Z-PLANE SINGULARITIES                           
C                                                                               
C LENGTH OF SSPEC AND ZSPEC ARE INDETERMINATE                                   
      REAL SAMP,SSPEC(9999),ZSPEC(9999)                                         
C                                                                               
C GLOBAL VARIABLES                                                              
C ****************                                                              
C                                                                               
$INCLUDE SIGPRO10.CMN (NLIST)                                                   
$INCLUDE SPLANE10.CMN (NLIST)                                                   
$INCLUDE ZPLANE10.CMN (NLIST)                                                   
C                                                                               
C LOCAL VARIABLES                                                               
C ***************                                                               
C                                                                               
C IS      I*4  LOOP COUNTER OVER SINGULARITIES                                  
C IPS     I*4  POINTER TO START OF CURRENT S-PLANE SINGULARITY                  
C IPZ     I*4  POINTER TO START OF CURRENT Z-PLANE SINGULARITY                  
C IER     I*4  ERROR NUMBER                                                     
C INFNO   I*4  COUNT OF NO OF ZEROS AT Z=(-1.0,0.0).  -VE COUNT=POLES           
C ITS     I*4  TYPE OF S-PLANE SINGULARITY                                      
C ITZ     I*4  TYPE OF Z-PLANE SINGULARITY                                      
C NS      I*4  NUMBER OF S-PLANE SINGULARITIES                                  
C NZ      I*4  NUMBER OF Z-PLANE SINGULARITIES                                  
C IZ      I*4  COUNTER OF CURRENT Z-PLANE SINGULARITY                           
C T       R*4  1/SAMP=INTERSAMPLE PERIOD                                        
C SPOS    C*8  POSITION OF S-PLANE SINGULARITY                                  
C ZPOS    C*8  POSITION OF Z-PLANE SINGULARITY                                  
C SGAIN   C*8  GAIN OF S-PLANE SINGULARITY                                      
C ZGAIN   C*8  GAIN OF Z-PLANE COMPLEX ZERO AT GIVEN POSITION                   
C RTN     P    ROUTINE NUMBER WITHIN PACKAGE                                    
C                                                                               
      INTEGER IS,IPS,IPZ,IER,RTN                                                
      INTEGER ITS,ITZ,NS,NZ,IZ                                                  
      REAL T                                                                    
      COMPLEX SPOS,ZPOS,SGAIN,ZGAIN                                             
      PARAMETER (RTN=30)                                                        
C                                                                               
C===================================================================            
C                                                                               
C CHECK PARAMETERS                                                              
C ****************                                                              
C                                                                               
      IF(SSPEC(PSX).NE.PSV) GOTO 9001                                           
      IF(SAMP .LE. 0.0) GOTO 9002                                               
C                                                                               
C INITIALISE                                                                    
C **********                                                                    
C                                                                               
      NS=INT(SSPEC(PSN))                                                        
      IF(NS.LT.0) GOTO 9003                                                     
      T=1.0/SAMP                                                                
C                                                                               
C COUNT NO OF ZEROS (-VE=POLES) AT Z=(-1,0)                                     
C -----------------------------------------                                     
C                                                                               
      INFNO=0                                                                   
      IF(NS.LE.0) GOTO 1100                                                     
      DO 1000 IS=1,NS                                                           
      IPS=PSH+1+PSE*(IS-1)                                                      
C CHECK SEQUENCE NO OF SINGULARITY                                              
      IF(INT(SSPEC(IPS+PSM)).NE.IS) GOTO 9004                                   
C INCREMENT COUNTER ACCORDING TO S-PLANE SINGULARITY TYPE.                      
C ONLY ONE OF THE FOLLOWING CONDITIONS WILL BE TRUE                             
      ITS=INT(SSPEC(IPS+PST))                                                   
C REAL ZERO OR COMPLEX ZERO                                                     
      IF(ITS.EQ.PSTRZ .OR. ITS.EQ.PSTCZ) INFNO=INFNO-1                          
C REAL POLE OR COMPLEX POLE                                                     
      IF(ITS.EQ.PSTRP .OR. ITS.EQ.PSTCP) INFNO=INFNO+1                          
C COMPLEX CONJUGATE ZERO PAIR                                                   
      IF(ITS.EQ.PST2Z) INFNO=INFNO-2                                            
C COMPLEX CONJUGATE POLE PAIR                                                   
      IF(ITS.EQ.PST2P) INFNO=INFNO+2                                            
1000  CONTINUE                                                                  
1100  CONTINUE                                                                  
C                                                                               
C CHECK LENGTH OF ZSPEC AND INITIALISE HEADER                                   
C -------------------------------------------                                   
C                                                                               
C CALC NO OF Z-PLANE SINGULARITIES                                              
      NZ=NS+(IABS(INFNO)+1)/2                                                   
C CHECK ZSPEC IS LONG ENOUGH                                                    
      IF(PZH+PZE*NZ .GT. INT(ZSPEC(PZL))) GOTO 9005                             
C INITIALISE                                                                    
      IF(PZH+PZE*NZ .GT. INT(ZSPEC(PZU))) ZSPEC(PZU)=PZH+PZE*NZ                 
      ZSPEC(PZX)=PZV                                                            
      ZSPEC(PZN)=NZ                                                             
C                                                                               
C CALC AND WRITE Z-PLANE SINGULARITY SPEC FOR EACH S-PLANE SINGULARITY          
      IZ=0                                                                      
C                                                                               
C ********************************************************************          
      IF(NS.LE.0) GOTO 2100                                                     
      DO 2000 IS=1,NS                                                           
C                                                                               
C TYPE INDEPENDENT PART                                                         
C ---------------------                                                         
C                                                                               
C GET LOCAL COPY OF S-PLANE VALUES                                              
      IPS=PSH+1+PSE*(IS-1)                                                      
      SPOS=CMPLX(SSPEC(IPS+PSR),SSPEC(IPS+PSI))                                 
      SGAIN=CMPLX(SSPEC(IPS+PSG),SSPEC(IPS+PSJ))                                
      ITS=INT(SSPEC(IPS+PST))                                                   
C CALC COMMON Z-PLANE VALUES                                                    
      ZPOS=(2.0+T*SPOS)/(2.0-T*SPOS)                                            
      ZGAIN=(2.0-T*SPOS)/T                                                      
      IZ=IZ+1                                                                   
      IPZ=PZH+1+PZE*(IZ-1)                                                      
      ZSPEC(IPZ+PZM)=IZ                                                         
C                                                                               
C SWITCH ON S-PLANE SINGULARITY TYPE                                            
C ----------------------------------                                            
C                                                                               
C REAL ZERO                                                                     
      IF(ITS.EQ.PSTRZ) THEN                                                     
      ZSPEC(IPZ+PZT)=PZTRZ                                                      
      ZSPEC(IPZ+PZG)=REAL(SGAIN)*REAL(ZGAIN)                                    
      ZSPEC(IPZ+PZJ)=AIMAG(SGAIN)*REAL(ZGAIN)                                   
      ZSPEC(IPZ+PZR)=REAL(ZPOS)                                                 
      ZSPEC(IPZ+PZI)=0.0                                                        
C                                                                               
C REAL POLE                                                                     
      ELSEIF(ITS.EQ.PSTRP) THEN                                                 
      ZSPEC(IPZ+PZT)=PZTRP                                                      
      ZSPEC(IPZ+PZG)=REAL(SGAIN)/REAL(ZGAIN)                                    
      ZSPEC(IPZ+PZJ)=0.0                                                        
      ZSPEC(IPZ+PZR)=REAL(ZPOS)                                                 
      ZSPEC(IPZ+PZI)=0.0                                                        
C                                                                               
C COMPLEX ZERO                                                                  
      ELSEIF(ITS.EQ.PSTCZ) THEN                                                 
      ZSPEC(IPZ+PZT)=PZTCZ                                                      
      ZSPEC(IPZ+PZG)=REAL(SGAIN*ZGAIN)                                          
      ZSPEC(IPZ+PZJ)=AIMAG(SGAIN*ZGAIN)                                         
      ZSPEC(IPZ+PZR)=REAL(ZPOS)                                                 
      ZSPEC(IPZ+PZI)=IMAG(ZPOS)                                                 
C                                                                               
C COMPLEX POLE                                                                  
      ELSEIF(ITS.EQ.PSTCP) THEN                                                 
      ZSPEC(IPZ+PZT)=PZTCP                                                      
      ZSPEC(IPZ+PZG)=REAL(SGAIN/ZGAIN)                                          
      ZSPEC(IPZ+PZJ)=AIMAG(SGAIN/ZGAIN)                                         
      ZSPEC(IPZ+PZR)=REAL(ZPOS)                                                 
      ZSPEC(IPZ+PZI)=IMAG(ZPOS)                                                 
C                                                                               
C COMPLEX CONJUGATE ZERO PAIR                                                   
      ELSEIF(ITS.EQ.PST2Z) THEN                                                 
      ZSPEC(IPZ+PZT)=PZT2Z                                                      
      ZSPEC(IPZ+PZG)=REAL(SGAIN)*(CABS(ZPOS)**2)                                
      ZSPEC(IPZ+PZJ)=AIMAG(SGAIN)*CABS(ZGAIN)**2                                
      ZSPEC(IPZ+PZR)=REAL(ZPOS)                                                 
      ZSPEC(IPZ+PZI)=IMAG(ZPOS)                                                 
C                                                                               
C COMPLEX CONJUGATE POLE PAIR                                                   
      ELSEIF(ITS.EQ.PST2P) THEN                                                 
      ZSPEC(IPZ+PZT)=PZT2P                                                      
      ZSPEC(IPZ+PZG)=REAL(SGAIN)/(CABS(ZPOS)**2)                                
      ZSPEC(IPZ+PZJ)=AIMAG(SGAIN)/CABS(ZGAIN)**2                                
      ZSPEC(IPZ+PZR)=REAL(ZPOS)                                                 
      ZSPEC(IPZ+PZI)=IMAG(ZPOS)                                                 
C                                                                               
C ILLEGAL TYPE OF S-PLANE SINGULARITY                                           
      ELSE                                                                      
      GOTO 9006                                                                 
      ENDIF                                                                     
2000  CONTINUE                                                                  
2100  CONTINUE                                                                  
C                                                                               
C APPEND APPROPRIATE NUMBER OF ZEROS(POLES) AT Z=(-1,0)                         
C *****************************************************                         
C                                                                               
C APPEND PAIRS OF ZEROS (OR POLES)                                              
C --------------------------------                                              
C                                                                               
      IF(IABS(INFNO)/2 .LE. 0) GOTO 3100                                        
      DO 3000 IS=1,IABS(INFNO)/2                                                
      IZ=IZ+1                                                                   
      IPZ=PZH+1+PZE*(IZ-1)                                                      
      ZSPEC(IPZ+PZM)=IZ                                                         
      IF(INFNO.GT.0) THEN                                                       
      ZSPEC(IPZ+PZT)=PZT2Z                                                      
      ELSE                                                                      
      ZSPEC(IPZ+PZT)=PZT2P                                                      
      ENDIF                                                                     
      ZSPEC(IPZ+PZG)=1.0                                                        
      ZSPEC(IPZ+PZJ)=0.0                                                        
      ZSPEC(IPZ+PZR)=-1.0                                                       
      ZSPEC(IPZ+PZI)=0.0                                                        
3000  CONTINUE                                                                  
3100  CONTINUE                                                                  
C                                                                               
C APPEND SINGLE REMAINING ZERO (OR POLE) IF ANY                                 
C ---------------------------------------------                                 
C                                                                               
      IF((INFNO/2)*2 .EQ. INFNO) GOTO 3200                                      
      IZ=IZ+1                                                                   
      IPZ=PZH+1+PZE*(IZ-1)                                                      
      ZSPEC(IPZ+PZM)=IZ                                                         
      IF(INFNO.GT.0) THEN                                                       
      ZSPEC(IPZ+PZT)=PZTRZ                                                      
      ELSE                                                                      
      ZSPEC(IPZ+PZT)=PZTRP                                                      
      ENDIF                                                                     
      ZSPEC(IPZ+PZG)=1.0                                                        
      ZSPEC(IPZ+PZJ)=0.0                                                        
      ZSPEC(IPZ+PZR)=-1.0                                                       
      ZSPEC(IPZ+PZI)=0.0                                                        
3200  CONTINUE                                                                  
C                                                                               
C RETURN                                                                        
C ******                                                                        
C                                                                               
      RETURN                                                                    
C                                                                               
C===================================================================            
C                                                                               
C ERRORS                                                                        
C ******                                                                        
C                                                                               
C SSPEC FORMAT NOT S-PLANE                                                      
9001  CONTINUE                                                                  
      IER=1                                                                     
      GOTO 9999                                                                 
C                                                                               
C SSPEC(PSN) IS NEGATIVE                                                        
9002  CONTINUE                                                                  
      IER=2                                                                     
      GOTO 9999                                                                 
C                                                                               
C PARAMETER SAMP OUT OF RANGE                                                   
9003  CONTINUE                                                                  
      IER=3                                                                     
      GOTO 9999                                                                 
C                                                                               
C ERROR IN SEQUENCE NOS OF SSPEC                                                
9004  CONTINUE                                                                  
      IER=4                                                                     
      GOTO 9999                                                                 
C ZSPEC NOT LONG ENOUGH                                                         
9005  CONTINUE                                                                  
      IER=5                                                                     
      GOTO 9999                                                                 
C                                                                               
C S-PLANE SINGULARITY TYPE IS NOT KNOWN                                         
9006  CONTINUE                                                                  
      IER=6                                                                     
      GOTO 9999                                                                 
C                                                                               
C GENERAL ERROR EXIT                                                            
C ------------------                                                            
C                                                                               
9999  CONTINUE                                                                  
      WRITE(7,'(X,A,I6)') 'ERROR IN ZTBILI=',IER                                
C     CALL UERERR(PKG,RTN,IER,0,'E')                                            
C     CALL UERREP(PKG,RTN,.TRUE.)                                               
      RETURN                                                                    
      END                                                                       
