C....*...1.........2.........3.........4.........5.........6.........7.*.......8
C     DBASIS   3/31/79
C
C     PURPOSE
C     COMPUTES AN N BY N MATRIX B WHOSE COLUMNS ARE ORTHOGONAL.
C
C     USAGE
C     CALL DBASIS(B,N)
C
C     ARGUMENTS
C     B - COMPUTED N BY N MATRIX STORED COLUMNWISE (STORAGE MODE OF 0)
C         REAL*8
C     N - ORDER OF B (TO BE SUPPLIED BY CALLING PROGRAM)
C
C     REMARKS
C     THE FINITE FOURIER TRANSFORM COEFFICIENTS OF A TIME SERIES OF
C     LENGTH N MAY BE OBTAINED BY REGRESSING THE TIME SERIES ON B.  THE
C     ORDER OF THE COEFFICIENTS THUS OBTAINED ARE:
C       A(0), A(1), B(1), ..., A((N-1)/2), B((N-1)/2)           N ODD
C       A(0), A(1), B(1), ..., A((N-2)/2), B((N-2)/2), A(N/2)   N EVEN
C
      SUBROUTINE DBASIS(B,N)
      IMPLICIT REAL*8 (A-H,O-Z)
      save
      REAL*8 B(N,N)
      DO 100 K = 1, N
  100 B(K,1) = 1.
      IF(N.EQ.1) RETURN
      N2 = N/2
      MODN2 = MOD(N,2)
      IF(MODN2 .EQ. 0)  N2 = N2 - 1
      DO 300 M = 1, N2
      MC = 2*M
      MS = 2*M + 1
      DO 200 K = 1, N
      TWOPI=8.0D0*DATAN(1.D0)
      ARG = TWOPI     * M * (K-1) / N
      B(K,MC) = DCOS(ARG)
      B(K,MS) = DSIN(ARG)
  200 CONTINUE
  300 CONTINUE
      IF(MODN2 .EQ.1)  RETURN
      DO 400 K = 1, N
      ARG = TWOPI     * (N2+1) * (K-1) / N
  400 B(K,N) = DCOS(ARG)
      RETURN
      END
