	SUBROUTINE DFT2 (XDATA,NPREV,N,NREM,ISGN)

C	DISCRETE FOURIER TRANSFORM OF LENGTH N.  IN-PLACE COOLEY-TUKEY
C	ALGORITHM, BIT-REVERSED TO NORMAL ORDER, SANDE-TUKEY PHASE SHIFTS.
C	DIMENSION XDATA(NPREV,N,NREM)
C	XDATA(J1,K4,J5) = SUM(XDATA(J1,J4,J5)*EXP(ISGN*2*PI*I*(J4-1)*
C	(K4-1)/N)), SUMMED OVER J4 = 1 TO N FOR ALL J1 FROM 1 TO NPREV,
C	K4 FROM 1 TO N AND J5 FROM 1 TO NREM.  N MUST BE A POWER OF TWO.
C	METHOD--LET IPREV TAKE THE VALUES 1, 2 OR 4, 4 OR 8, ..., N/16,
C	N/4, N.  THE CHOICE BETWEEN 2 OR 4, ETC., DEPENDS ON WHETHER N IS
C	A POWER OF FOUR.  DEFINE IFACT = 2 OR 4, THE NEXT FACTOR THAT
C	IPREV MUST TAKE, AND IREM = N/(IFACT*IPREV).  THEN--
C	DIMENSION XDATA(NPREV,IPREV,IFACT,IREM,NREM)
C	COMPLEX XDATA
C	XDATA(J1,J2,K3,J4,J5) = SUM(XDATA(J1,J2,J3,J4,J5)*EXP(ISGN*2*PI*I*
C	(K3-1)*((J3-1)/IFACT+(J2-1)/(IFACT*IPREV)))), SUMMED OVER J3 = 1
C	TO IFACT FOR ALL J1 FROM 1 TO NPREV, J2 FROM 1 TO IPREV, K3 FROM
C	1 TO IFACT, J4 FROM 1 TO IREM AND J5 FROM 1 TO NREM.  THIS IS
C	A PHASE-SHIFTED DISCRETE FOURIER TRANSFORM OF LENGTH IFACT.
C	FACTORING N BY FOURS SAVES ABOUT TWENTY FIVE PERCENT OVER FACTOR-
C	ING BY TWOS.  XDATA MUST BE BIT-REVERSED INITIALLY.

	DIMENSION XDATA(1)
	TWOPI=6.2831853071795865D0*DBLE(ISGN)
	IP0=2
	IP1=IP0*NPREV
	IP4=IP1*N
	IP5=IP4*NREM
	IP2=IP1
	NPART=N
10	IF (NPART-2) 70,30,20
20	NPART=NPART/4
	GO TO 10

C	DO A FOURIER TRANSFORM OF LENGTH TWO
30	IF (IP2-IP4) 40,170,170
40	IP3=IP2*2
C	IP2=IP1*IPROD
C	IP3=IP2*IFACT

	DO 50 I1=1,IP1,IP0
C	I1 = 1+(J1-1)*IP0

	DO 50 I5=I1,IP5,IP3
C	I5 = 1+(J1-1)*IP0+(J4-1)*IP3+(J5-1)*IP4
	I3A=I5
	I3B=I3A+IP2
C	I3 = 1+(J1-1)*IP0+(J2-1)*IP1+(J3-1)*IP2+(J4-1)*IP3+(J5-1)*IP4
	TEMPR=XDATA(I3B)
	TEMPI=XDATA(I3B+1)
	XDATA(I3B)=XDATA(I3A)-TEMPR
	XDATA(I3B+1)=XDATA(I3A+1)-TEMPI
	XDATA(I3A)=XDATA(I3A)+TEMPR
50	XDATA(I3A+1)=XDATA(I3A+1)+TEMPI

C	DO A FOURIER TRANSFORM OF LENGTH FOUR (FROM BIT REVERSED ORDER)
60	IP2=IP3
70	IF (IP2-IP4) 80,170,170
80	IP3=IP2*4
	THETA=TWOPI/FLOAT(IP3/IP1)
	SINTH=SIN(THETA/2.)
	WSTPR=-2.*SINTH*SINTH
C	COS(THETA)-1, FOR ACCURACY.
	WSTPI=SIN(THETA)
	WR=1.
	WI=0.

	DO 160 I2=1,IP2,IP1
C	I2 = 1+(J2-1)*IP1
	IF (I2-1) 100,100,90
90	W2R=WR*WR-WI*WI
	W2I=2.*WR*WI
	W3R=W2R*WR-W2I*WI
	W3I=W2R*WI+W2I*WR
100	I1MAX=I2+IP1-IP0

	DO 150 I1=I2,I1MAX,IP0
C	I1 = 1+(J1-1)*IP0

	DO 150 I5=I1,IP5,IP3
C	I5 = 1+(J1-1)*IP0+(J4-1)*IP3+(J5-1)*IP4
	I3A=I5
	I3B=I3A+IP2
	I3C=I3B+IP2
	I3D=I3C+IP2
C	I3 = 1+(J1-1)*IP0+(J2-1)*IP1+(J3-1)*IP2+(J4-1)*IP3+(J5-1)*IP4
	IF (I2-1) 120,120,110

C	APPLY THE PHASE SHIFT FACTORS
110	TEMPR=XDATA(I3B)
	XDATA(I3B)=W2R*TEMPR-W2I*XDATA(I3B+1)
	XDATA(I3B+1)=W2R*XDATA(I3B+1)+W2I*TEMPR
	TEMPR=XDATA(I3C)
	XDATA(I3C)=WR*TEMPR-WI*XDATA(I3C+1)
	XDATA(I3C+1)=WR*XDATA(I3C+1)+WI*TEMPR
	TEMPR=XDATA(I3D)
	XDATA(I3D)=W3R*TEMPR-W3I*XDATA(I3D+1)
	XDATA(I3D+1)=W3R*XDATA(I3D+1)+W3I*TEMPR
120	T0R=XDATA(I3A)+XDATA(I3B)
	T0I=XDATA(I3A+1)+XDATA(I3B+1)
	T1R=XDATA(I3A)-XDATA(I3B)
	T1I=XDATA(I3A+1)-XDATA(I3B+1)
	T2R=XDATA(I3C)+XDATA(I3D)
	T2I=XDATA(I3C+1)+XDATA(I3D+1)
	T3R=XDATA(I3C)-XDATA(I3D)
	T3I=XDATA(I3C+1)-XDATA(I3D+1)
	XDATA(I3A)=T0R+T2R
	XDATA(I3A+1)=T0I+T2I
	XDATA(I3C)=T0R-T2R
	XDATA(I3C+1)=T0I-T2I
	IF (ISGN) 130,130,140
130	T3R=-T3R
	T3I=-T3I
140	XDATA(I3B)=T1R-T3I
	XDATA(I3B+1)=T1I+T3R
	XDATA(I3D)=T1R+T3I
150	XDATA(I3D+1)=T1I-T3R

	TEMP=WR
	WR=WSTPR*WR-WSTPI*WI+WR
	WI=WSTPR*WI+WSTPI*TEMP+WI
C	OMIT THE NEXT THREE CARDS IF ROUNDED ARITHMETIC IS USED.
	FACTR=1.5-.5*(WR*WR+WI*WI)
	WR=WR*FACTR
160	WI=WI*FACTR
	GO TO 60

170	RETURN
	END
