C
C	fft computation for a signal:      - forward fft
C	                                   - backward fft
C	                                   - complex envelope
C	(complex input/output)
C
C	Sven Ivansson
C
C
C
	SUBROUTINE CFFTAL (NSAMP,DDIN,CIN, ISID, ISN, IUTDIM, 
     $	ISAMP,DDUT,CUT)
C
C	input:
C	  NSAMP is the number of active data samples provided in cin
C	  DDIN is distance between in-samples    !!! REAL*8 !!!
C	  CIN(1:nsamp) is the in-signal (1 corresponds to 0.0)
C	  ISID is identifier for ismp to be used, 1<=isid<=10
C	  ISN is -1 for forward cfft (time to frequency)
C	         +1 for backward cfft (frequency to time)
C	          0 for complex envelope (time to time)
C	  IUTDIM is minimum dimension of cut (0 for unspecified)
C
C	output:
C	  ISAMP is the number of samples provided in cut
C	  DDUT is distance between ut-samples    !!! REAL*8 !!!
C	  CUT(1:isamp) is the ut-signal (1 corresponds to 0.0)
C	     may be chosen as cin which is then overwritten
C
	REAL*8 DDIN
	COMPLEX CIN(*)
C
	REAL*8 DDUT
	COMPLEX CUT(*)
C
	INTEGER NSOLD(10), ISMP(10),I2LG(10)
	REAL TABL(1+32768/4,10)
	SAVE NSOLD, ISMP,I2LG,TABL
	DATA NSOLD /10*-1/
C
	IF (ISID.LT.1 .OR. ISID.GT.10) STOP 13451
	IF (NSAMP.NE.NSOLD(ISID)) THEN
	 ISMP(ISID)=2
	 I2LG(ISID)=1
	 DO WHILE (ISMP(ISID).LT.NSAMP)
	  ISMP(ISID)=2*ISMP(ISID)
	  I2LG(ISID)=I2LG(ISID)+1
	 END DO
	 IF (ISMP(ISID).GT.32768) STOP 6730
	 CALL COSQT(I2LG(ISID),TABL(1,ISID))
	 NSOLD(ISID)=NSAMP
	END IF
	I2LOG=I2LG(ISID)
	ISAMP=ISMP(ISID)
	IF (IUTDIM.GT.0) THEN
	 IF (ISAMP.GT.IUTDIM) STOP 6731
	END IF
	DIN=DDIN
C
	DO J=1,NSAMP
	 CUT(J)=CIN(J)
	END DO
	DO J=NSAMP+1,ISAMP
	 CUT(J)=CMPLX(0.0,0.0)
	END DO
C
	IF (ISN.EQ.-1) THEN
C	 now cfft
	 CALL CFFT (CUT,TABL(1,ISID),I2LOG,I2LOG,-1)
	 DDUT=1.0/(ISAMP*DDIN)
	 DO J=1,ISAMP
	  CUT(J)=DIN*CUT(J)
	 END DO
	ELSE IF (ISN.EQ.+1) THEN
C	 now cfft
	 CALL CFFT (CUT,TABL(1,ISID),I2LOG,I2LOG,+1)
	 DDUT=1.0/(ISAMP*DDIN)
	 SLASK=DIN*ISAMP
	 DO J=1,ISAMP
	  CUT(J)=SLASK*CUT(J)
	 END DO
	ELSE IF (ISN.EQ.0) THEN
	 DDUT=DDIN
C	 now cfft
	 CALL CFFT (CUT,TABL(1,ISID),I2LOG,I2LOG,-1)
	 ND2=ISAMP/2
	 ND2P2=ND2+2
	 DO K=2,ND2
	  CUT(K)=2.0*CUT(K)
	 END DO
	 DO K=ND2P2,ISAMP
	  CUT(K)=CMPLX(0.0,0.0)
	 END DO
C	 now cfft
	 CALL CFFT (CUT,TABL(1,ISID),I2LOG,I2LOG,+1)
	END IF	
C
	RETURN
	END
