      SUBROUTINE HILBERT( X, N )

C     Forms the Hilbert transform of the function
C     Real( X( N ) ) is the input time series
C     N must be a power of 2 and <16384

      COMPLEX CI
      PARAMETER ( CI = ( 0.0, 1.0 ) )

      COMPLEX X( * )

C     Check N is a power of 2

      IF ( N .LE. 0 )
     &   STOP 'FATAL ERROR in HILBERT: N must be positive'

      NT = 2**( INT( LOG10(REAL(N)) / 0.30104 ) +1 )
      IF ( NT .NE. N )
     &   STOP 'FATAL ERROR in HILBERT: N must be a power of 2'

C     *** Fourier transform ***

      CALL CFFT( X, N, 1 )
      CALL CFFTSC( X, N )

C     *** Multiply by i*sgn( f ) ***

C     --- positive spectrum ( lower N/2 positions)
      DO I = 1, N/2-1
         X( I ) =  CI * X( I )
      END DO

      X( N / 2 ) = 0.0      ! d.c. component

C     --- negative spectrum (upper N/2 positions)
      DO I = N/2+1, N
         X( I ) = -CI * X( I )
      END DO

C     *** Inverse Fourier transform ***

      CALL CFFT( X, N, -1 )

      RETURN
      END
