      SUBROUTINE CFFTC(N,COMSIG,IOPTC)                                          
C                                                                               
C                                                                               
C  MODULE:         CFFTC                                                        
C  VERSION:        1.0                                                          
C  LAST MODIFIED:  22/01/82                                                     
C  AUTHOR:         DAVE POWELL                                                  
C                                                                               
C  PARAMETERS                                                                   
C  **********                                                                   
C                                                                               
C  N      I*4  LENGTH OF SIGNAL-MUST BE POWER OF 2                              
C  COMSIG R*4A ARRAY CONTAINING COMPLEX SIGNAL                                  
C  IOPTC  I*4  INTEGER(BETWEEN 0 & 7) DETERMINING OPTIONS-CARRIED OVER          
C                                                                               
C  OPTIONS FOR A GIVEN IOPT                                                     
C  ************************                                                     
C                                                                               
C  IOPT                    OPTIONS                                              
C  ----                    -------                                              
C   0     REVERSE,NO SCALING,NO BIT SHUFFLE  (D.I.T.)                           
C   1     FORWARD,    "     ,      "         (D.I.F.)                           
C   2     REVERSE,SCALING   ,      "         (D.I.T.)                           
C   3     FORWARD,    "     ,      "         (D.I.F.)                           
C   4     REVERSE,NO SCALING,BIT SHUFFLE     (D.I.T.)                           
C   5     FORWARD,    "     ,      "         (D.I.T.)                           
C   6     REVERSE,SCALING   ,      "         (D.I.T.)                           
C   7     FORWARD,    "     ,      "         (D.I.T.)                           
C                                                                               
C                                                                               
      COMMON /CFFT/MAXLEN,MAXN,W(1024)                                          
      COMPLEX COMSIG(N)                                                         
      INTEGER*4 IOPTC,N                                                         
C                                                                               
C   LOCAL VARIABLES                                                             
C   ***************                                                             
C                                                                               
C   Y     C*8   =-j2.0*PI/N                                                     
C   W     C*8   =EXP(-j*2.0*PI/N)                                               
C   CT    C*8   TEMPORARY COMPLEX STORE                                         
C   PI    R*4   3.14159.....                                                    
C   T     R*4   =1.0(NO SCALING);1.0/N(SCALING)                                 
C   M     I*4   LOG(BASE2)N                                                     
C   NM1   I*4   N-1                                                             
C   ELUP  I*4   POINTER AT LOOKUP TABLE                                         
C   B(3)  I*4A  INFORMATION ABOUT OPTIONS                                       
C   RTN   I*4   ROUTINE NUMBER (=1)                                             
C   IOPT  I*4   CODE CARRYING OPTIONS REQUIRED                                  
C   I1,I5,J2,I3J4,I4 I*4 COUNTERS                                               
C                                                                               
      COMPLEX Y,W,CT                                                            
      REAL*4 PI,T                                                               
      INTEGER*4 M,I1,I5,B(3),J2,I3,J4,I4,NM1,ELUP,IER,RTN,IOPT                  
C                                                                               
      DATA PI/3.141592653589793/                                                
      PARAMETER (RTN=1)                                                         
C                                                                               
C                                                                               
C  (1) CHECK PARAMETERS & SET M=LOG(N) BASE 2                                   
C  ******************************************                                   
C                                                                               
      IOPT=IOPTC                                                                
      IF(N.GT.MAXLEN.OR.N.LT.4) GOTO 9003                                       
      IF(IOPT.LT.0.OR.IOPT.GT.7) GOTO 9001                                      
C                                                                               
      M=1                                                                       
C     FORTRAN DO LOOP                                                           
      DO 1002 I1=1,11                                                           
          M=2*M                                                                 
          IF(M.EQ.N) GOTO 1003                                                  
C     ENDDO                                                                     
1002  CONTINUE                                                                  
      GOTO 9002                                                                 
1003  CONTINUE                                                                  
      M=I1                                                                      
      NV2=N/2                                                                   
      NM1=N-1                                                                   
C                                                                               
C                                                                               
C  (2) FIND OPTIONS REQUIRED & TAKE COMPLEX CONJUGATE AS REQUIRED               
C  **************************************************************               
C                                                                               
      IS=8                                                                      
C     FORTRAN DO LOOP                                                           
      DO 2001 I2=1,3                                                            
          IS=IS/2                                                               
          IOPT=IOPT-IS                                                          
          IF (IOPT.GE.0) GOTO 2000                                              
          IOPT=IOPT+IS                                                          
          B(I2)=0                                                               
          GOTO 2001                                                             
2000      CONTINUE                                                              
          B(I2)=1                                                               
C     ENDDO                                                                     
2001  CONTINUE                                                                  
C                                                                               
      IF(B(3).EQ.0) THEN                                                        
C         FORTRAN DO LOOP                                                       
          DO 2002 J2=1,N                                                        
              COMSIG(J2)=CONJG(COMSIG(J2))                                      
C         ENDDO                                                                 
2002      CONTINUE                                                              
      ENDIF                                                                     
C                                                                               
C                                                                               
C  (3) SET UP 'LOOK UP' TABLE                                                   
C  **************************                                                   
C                                                                               
      IF (N.GT.MAXN) THEN                                                       
          Y=(2.0*PI/FLOAT(N))*(0.0,-1.0)                                        
C         FORTRAN DO LOOP                                                       
          DO 3001 I3=1,N/2                                                      
              W(I3)=CEXP(Y*(I3-1))                                              
C         ENDDO                                                                 
3001      CONTINUE                                                              
          MAXN=N                                                                
      ENDIF                                                                     
C                                                                               
C                                                                               
C  (4) BIT SHUFFLE IF REQUIRED                                                  
C  ***************************                                                  
C                                                                               
      IF (B(1).EQ.1) THEN                                                       
C         BIT SHUFFLE                                                           
          J4=1                                                                  
C         FORTRAN DO LOOP                                                       
          DO 4003 I4=1,NM1                                                      
              IF (I4.LT.J4) THEN                                                
                  CT=COMSIG(J4)                                                 
                  COMSIG(J4)=COMSIG(I4)                                         
                  COMSIG(I4)=CT                                                 
              ENDIF                                                             
              K=NV2                                                             
4001          CONTINUE                                                          
              IF (K.GE.J4) GOTO 4002                                            
              J4=J4-K                                                           
              K=K/2                                                             
              GOTO 4001                                                         
4002          CONTINUE                                                          
              J4=J4+K                                                           
C         ENDDO                                                                 
4003      CONTINUE                                                              
      ENDIF                                                                     
C                                                                               
C                                                                               
C  (5) IN PLACE FFT                                                             
C  ****************                                                             
C                                                                               
C  (a) DECIMATION IN TIME-I/P BIT SHUFFLED,O/P NATURAL ORDER                    
C                                                                               
      IF (B(1).EQ.0.AND.B(3).EQ.1) GOTO 5100                                    
      LE=1                                                                      
C     FORTRAN DO LOOP                                                           
      DO 5010 J5=1,M                                                            
          LE1=LE                                                                
          LE=2*LE                                                               
          MULT=MAXN/LE                                                          
C         FORTRAN DO LOOP                                                       
          DO 5030 K5=1,LE1                                                      
              K5M=K5-1                                                          
              ELUP=1+(K5M*MULT)                                                 
C             FORTRAN DO LOOP                                                   
              DO 5020 L5=K5,N,LE                                                
                  IP=L5+LE1                                                     
                  CT=W(ELUP)*COMSIG(IP)                                         
                  COMSIG(IP)=COMSIG(L5)-CT                                      
                  COMSIG(L5)=COMSIG(L5)+CT                                      
C             ENDDO                                                             
5020          CONTINUE                                                          
C         ENDDO                                                                 
5030      CONTINUE                                                              
C     ENDDO                                                                     
5010  CONTINUE                                                                  
      GOTO 6000                                                                 
C                                                                               
C                                                                               
C  (b) DECIMATION IN FREQUENCY-I/P NATURAL ORDER,O/P BIT SHUFFLED               
C                                                                               
5100  CONTINUE                                                                  
      LE=2*N                                                                    
      TWOJ5=1                                                                   
C     FORTRAN DO LOOP                                                           
      DO 5110 J5=1,M                                                            
          TWOJ5=2*TWOJ5                                                         
          ELUP1=(MAXN*TWOJ5)/(2*N)                                              
          LE=LE/2                                                               
          LE1=LE/2                                                              
C         FORTRAN DO LOOP                                                       
          DO 5130 K5=1,LE1                                                      
              ELUP=1+ELUP1*(K5-1)                                               
C             FORTRAN DO LOOP                                                   
              DO 5120 L5=K5,N,LE                                                
                  IP=L5+LE1                                                     
                  CT=COMSIG(L5)-COMSIG(IP)                                      
                  COMSIG(L5)=COMSIG(L5)+COMSIG(IP)                              
                  COMSIG(IP)=CT*W(ELUP)                                         
C             ENDDO                                                             
5120          CONTINUE                                                          
C         ENDDO                                                                 
5130      CONTINUE                                                              
C     ENDDO                                                                     
5110  CONTINUE                                                                  
C                                                                               
C                                                                               
C  (6) SCALE AND TAKE INVERSE AS REQUIRED                                       
C  **************************************                                       
C                                                                               
6000  CONTINUE                                                                  
      IF(B(3).EQ.0.OR.B(2).EQ.1) THEN                                           
          IF(B(2).EQ.1) THEN                                                    
              T=1.0/FLOAT(N)                                                    
          ELSE                                                                  
              T=1.0                                                             
          ENDIF                                                                 
          IF (B(3).EQ.0) THEN                                                   
              DO 6001 I6=1,N                                                    
                  COMSIG(I6)=T*CONJG(COMSIG(I6))                                
6001      CONTINUE                                                              
          ELSE                                                                  
              DO 6002 J6=1,N                                                    
                  COMSIG(J6)=T*COMSIG(J6)                                       
6002      CONTINUE                                                              
          ENDIF                                                                 
      ENDIF                                                                     
      RETURN                                                                    
C                                                                               
C   (7) END AND CALL OF ERROR ROUTINES                                          
C   **********************************                                          
C                                                                               
9001  CONTINUE                                                                  
      IER=1                                                                     
      GOTO 9999                                                                 
C                                                                               
9002  CONTINUE                                                                  
      IER=2                                                                     
      GOTO 9999                                                                 
C                                                                               
9003  CONTINUE                                                                  
      IER=3                                                                     
      GOTO 9999                                                                 
C                                                                               
9999  CONTINUE                                                                  
      CALL UERERR('SIGPROC',RTN,IER,0,'E')                                      
      CALL UERREP('SIGREP',RTN,.FALSE.)                                         
      RETURN                                                                    
      END                                                                       
