    PROGRAM nag_tsa_spectral_ex03

      ! Example Program Text for nag_tsa_spectral
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_tsa_spectral, ONLY : nag_bivar_spectral_coh, &
       nag_bivar_spectral_lin_sys
      USE nag_examples_io, ONLY : nag_std_out, nag_std_in
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, l, n
      ! .. Local Arrays ..
      REAL (wp), ALLOCATABLE :: crf(:), f_x(:), f_y(:), sqrd_coh(:)
      REAL (wp) :: stats(4)
      COMPLEX (wp), ALLOCATABLE :: f_xy(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) &
       'Example Program Results for nag_tsa_spectral_ex03'

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) l, n

      ALLOCATE (f_x(l),f_y(l),f_xy(l),sqrd_coh(l),crf(l)) ! Allocate storage

      READ (nag_std_in,*) stats
      READ (nag_std_in,*) f_x
      READ (nag_std_in,*) f_y
      READ (nag_std_in,*) f_xy

      CALL nag_bivar_spectral_coh(f_x,f_y,f_xy,stats,sqrd_coh)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) &
       '  Squared coherency - using nag_bivar_spectral_coh'
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'((1X,4(I5,f10.4)))') (i-1,sqrd_coh(i),i=1,l)

      CALL nag_bivar_spectral_lin_sys(f_x,f_y,f_xy,stats,n,crf=crf)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '  Impulse causal response function &
       &- using nag_bivar_spectral_lin_sys'
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'((1X,4(I5,f10.4)))') (i-1,crf(i),i=1,l)

      DEALLOCATE (f_x,f_y,f_xy,sqrd_coh,crf) ! Deallocate storage

    END PROGRAM nag_tsa_spectral_ex03
