      SUBROUTINE CFFTF(N,AR,AI,IOPTC)                                           
C                                                                               
C  MODULE        CFFTF                                                          
C  VERSION       1.0                                                            
C  ROUTINE       2                                                              
C  LAST MODIFIED 19/10/81                                                       
C  AUTHOR        DAVE POWELL                                                    
C  ----------------------------------------------------------------             
C                                                                               
C  PARAMETERS                                                                   
C  **********                                                                   
C                                                                               
C  N      I*4  LENGTH OF SIGNAL MUST BE POWER OF 2                              
C  AR     R*4A REAL PART OF SIGNAL                                              
C  AI     R*4A IMAGINARY PART OF SIGNAL                                         
C  IOPTC  I*4  DEFINES OPTIONS REQUIRED                                         
C                                                                               
C  OPTIONS FOR A GIVEN IOPT                                                     
C  ************************                                                     
C                                                                               
C  IOPTC               OPTIONS                                                  
C  -----               -------                                                  
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 /FFT/MAXLEN,MAXN,C(1024)                                           
      REAL*4 C                                                                  
      REAL*4 AR(N),AI(N)                                                        
      INTEGER*4 N,IOPTC                                                         
C                                                                               
C  ---------------------------------------------------------------              
C                                                                               
C  LOCAL VARIABLES                                                              
C  ***************                                                              
C                                                                               
C  M      I*4  =LOG(N) BASE 2                                                   
C  MAXN   I*4  =MAXIMUM VALUE OF N ENCOUNTERED IN RUN                           
C  NV2    I*4  =N/2                                                             
C  NM1    I*4  =N-1                                                             
C  IS     I*4  REPRESENTS POWER OF TWO                                          
C  B      I*4A BIT REPRESENTATION                                               
C  LE     I*4  2**L                                                             
C  LE1    I*4  LE/2                                                             
C  IP     I*4  POINTER TO OTHER HALF OF BUTTERFLY                               
C  IOPT   I*4  COPY OF IOPTC                                                    
C  T,T1   R*4  DUMPS                                                            
C  I1-I4  I*4  DO LOOP COUNTERS                                                 
C  J2,J5  I*4       "                                                           
C  K5     I*4       "                                                           
C  L5     I*4       "                                                           
C  J3-J4  I*4  POINTERS                                                         
C  MN22   I*4  (MAXN/2)+2                                                       
C  MN4    I*4  MAXN/4                                                           
C  MN41   I*4  (MAXN/4)+1                                                       
C  FR     I*4  INDICATES DIRECTION OF FFT                                       
C  RTN    I*4  ROUTINE NUMBER (=2)                                              
C  IER    I*4  ERROR NUMBER                                                     
C                                                                               
      REAL*4 T,U1,U2,T1,W1,W2                                                   
      INTEGER*4 NV2,NM1,IS,IP,B,LE,LE1,MAXN,M,SLUP,CLUP,J2,MN41,MN4             
      INTEGER*4 MN22,TWOJ5,FR,IOPT,RTN,IER                                      
      DIMENSION B(3)                                                            
C                                                                               
      PARAMETER (RTN=2)                                                         
      DATA PI/3.141592653589793/                                                
C                                                                               
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                                                                               
C                                                                               
      DO 1002 I1=1,11                                                           
          M=I1                                                                  
          IF(2**M.EQ.N) GOTO 1003                                               
1002  CONTINUE                                                                  
      GOTO 9002                                                                 
1003  CONTINUE                                                                  
      NV2=N/2                                                                   
      NM1=N-1                                                                   
C                                                                               
C                                                                               
C  (2) OPTIONS REQUIRED & TAKE CONJUGATE IF INVERSE REQUIRED                    
C  *********************************************************                    
C                                                                               
      DO 2001 I2=1,3                                                            
          IS=4/2**(I2-1)                                                        
          IOPT=IOPT-IS                                                          
          IF(IOPT.GE.0) GOTO 2000                                               
          IOPT=IOPT+IS                                                          
          B(I2)=0                                                               
          GOTO 2001                                                             
2000      CONTINUE                                                              
          B(I2)=1                                                               
2001  CONTINUE                                                                  
C                                                                               
      IF (B(3).EQ.0) THEN                                                       
          DO 2002 J2=1,N                                                        
              AI(J2)=(-1)*AI(J2)                                                
2002      CONTINUE                                                              
      ENDIF                                                                     
C                                                                               
C                                                                               
C  (3) SET UP 'LOOK UP' TABLE                                                   
C  **************************                                                   
C                                                                               
      IF(N.GT.MAXN) THEN                                                        
          J3=(N/2)+2                                                            
          Y=2.0*PI/FLOAT(N)                                                     
          DO 3001 I3=1,N/4+1                                                    
              C(I3)=COS(Y*(I3-1))                                               
              C(J3-I3)=-C(I3)                                                   
3001      CONTINUE                                                              
          MAXN=N                                                                
      ENDIF                                                                     
      MN22=(MAXN/2)+2                                                           
      MN4=MAXN/4                                                                
      MN41=MN4+1                                                                
C                                                                               
C                                                                               
C  (4) BIT SHUFFLE IF REQUIRED                                                  
C  ***************************                                                  
C                                                                               
      IF(B(1).NE.1)GOTO 4004                                                    
      J4=1                                                                      
      DO 4003 I4=1,NM1                                                          
          IF(I4.GE.J4)GOTO 4000                                                 
          T=AR(J4)                                                              
          AR(J4)=AR(I4)                                                         
          AR(I4)=T                                                              
          T=AI(J4)                                                              
          AI(J4)=AI(I4)                                                         
          AI(I4)=T                                                              
4000      CONTINUE                                                              
          K=NV2                                                                 
4001      CONTINUE                                                              
          IF(K.GE.J4) GOTO 4002                                                 
          J4=J4-K                                                               
          K=K/2                                                                 
          GOTO 4001                                                             
4002      CONTINUE                                                              
          J4=J4+K                                                               
4003  CONTINUE                                                                  
4004  CONTINUE                                                                  
C                                                                               
C                                                                               
C  (5) IN PLACE FFT                                                             
C  ****************                                                             
C                                                                               
C   (a) DECIMATION IN TIME-I/P BIT SHUFFLED,0/P NATURAL ORDER                   
C                                                                               
C                                                                               
      IF(B(1).EQ.0.AND.B(3).EQ.1) GOTO 5100                                     
      LE=1                                                                      
      DO 5010 J5=1,M                                                            
          LE1=LE                                                                
          LE=2*LE                                                               
          MULT=MAXN/LE                                                          
          DO 5030 K5=1,LE1                                                      
              K5M=K5-1                                                          
              CLUP=1+(K5M*MULT)                                                 
              SLUP=CLUP                                                         
              IF(SLUP.LT.MN41)SLUP=MN22-SLUP                                    
              SLUP=SLUP-MN4                                                     
              DO 5020 L5=K5,N,LE                                                
                  IP=L5+LE1                                                     
                  T=AR(IP)*C(CLUP)+AI(IP)*C(SLUP)                               
                  T1=-AR(IP)*C(SLUP)+AI(IP)*C(CLUP)                             
                  AR(IP)=AR(L5)-T                                               
                  AI(IP)=AI(L5)-T1                                              
                  AR(L5)=AR(L5)+T                                               
                  AI(L5)=AI(L5)+T1                                              
5020          CONTINUE                                                          
5030      CONTINUE                                                              
5010  CONTINUE                                                                  
      GOTO 6000                                                                 
C                                                                               
C   (b) DECIMATION IN FREQ-I/P NATURAL ORDER,O/P BIT SHUFFLED                   
C                                                                               
C                                                                               
5100  CONTINUE                                                                  
      LE=2*N                                                                    
      TWOJ5=1                                                                   
      DO 5110 J5=1,M                                                            
          TWOJ5=2*TWOJ5                                                         
          CLUP1=(MAXN*TWOJ5)/(2*N)                                              
          LE=LE/2                                                               
          LE1=LE/2                                                              
          DO 5130 K5=1,LE1                                                      
              CLUP=1+CLUP1*(K5-1)                                               
              SLUP=CLUP                                                         
              IF(SLUP.LT.MN41)SLUP=MN22-SLUP                                    
              SLUP=SLUP-MN4                                                     
              DO 5120 L5=K5,N,LE                                                
                  IP=L5+LE1                                                     
                  T=AR(L5)-AR(IP)                                               
                  T1=AI(L5)-AI(IP)                                              
                  AR(L5)=AR(L5)+AR(IP)                                          
                  AI(L5)=AI(L5)+AI(IP)                                          
                  AR(IP)=T*C(CLUP)+T1*C(SLUP)                                   
                  AI(IP)=T1*C(CLUP)-T*C(SLUP)                                   
5120          CONTINUE                                                          
5130      CONTINUE                                                              
5110  CONTINUE                                                                  
C                                                                               
C                                                                               
C  (6) SCALE AND TAKE INVERSE AS REQUIRED                                       
C  **************************************                                       
C                                                                               
6000  CONTINUE                                                                  
      IF(B(2).EQ.0.AND.B(3).EQ.1) GOTO 6004                                     
      IF(B(2).EQ.0) GOTO 6001                                                   
      T=1.0/FLOAT(N)                                                            
      GOTO 6002                                                                 
6001  CONTINUE                                                                  
      T=1.00                                                                    
6002  CONTINUE                                                                  
      FR=(-1)**(B(3)+1)                                                         
      DO 6003 I6=1,N                                                            
          AR(I6)=AR(I6)*T                                                       
          AI(I6)=FR*AI(I6)*T                                                    
6003  CONTINUE                                                                  
6004  CONTINUE                                                                  
      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                                                                  
C                                                                               
      CALL UERERR('SIGPROC',RTN,IER,0,'E')                                      
      CALL UERREP('SIGREP',RTN,.FALSE.)                                         
C                                                                               
      RETURN                                                                    
      END                                                                       
