
       Program radar
*------------------------------------------------------------------*
*                                                                  *
*  A First Course in Computational Physics,   EXERCISE 6.10        *
*                                                                  *
*  YOUR NAME and identifying information go here.                  *
*                                                                  *
*  This program evaluates the cross correlation function           *
*  between a reference signal and a (simulated) echo,              *
*  thus mimicing the operation of radar.                           *
*                                                                  *
*  As it currently exists, it reproduces FIGURES 6.8, 6.9, & 6.11  *
*  of the text.                                                    *
*                                                                  *
*                     last revised:  January 1, 1993    pld        *
*                 current revision:  today's date       abc        *
*                                                  (your initials) *
*                                                                  *
*------------------------------------------------------------------*
*
      double  precision  time, delta_t, reference(2001),echo(4001),
     +  cross_correlation(2001),sum
      double precision pi, Noise, smallest, biggest
      integer i, j, delay, shift
      parameter (pi = 3.14159 26535 89793 d0)
 
      delta_t = 2.d0*pi/50.d0   ! Sample every 1/50 cycle.

                        ! To simulate an echo, the recieved signal is
      delay = 840       ! delayed in time with respect to the reference
                        ! signal.  For this simulation, the delay is 
                        ! arbitrarily taken to be 840 time increments. 

*
* Initialize
*
      do i = 1, 2001
         reference(i) = 0.d0
      end do
      do i = 1, 4001
         echo(i)      = 0.d0
      end do
*
* Establish the reference signal, 10 cycles long:
*
      do i=1,501
         time = dble(i-1)*delta_t
         reference(i) = sin(time)
      end do
*
* Generate the echo by setting it equal to the reference, 
* but delayed in time:
*
      do i=1,501
         echo(i+delay) = reference(i)
      end do
*
* Add noise to the echo:
*
      do i=1,4001
         echo(i) = echo(i) + Noise()
      end do
*
*  Plot the reference signal:
*
      call ginit
      call window(1.d0, -3.5d0, 2001.d0 , 3.5d0)
      call viewport(.05d0, .5d0, .45d0, .9d0)
      do i = 2, 2001
         call line(dble(i-1),reference(i-1),dble(i),reference(i) )
      end do
      call cursor(14,1)
      write(*,1000)
1000  format(15x,'FIGURE 6.8')
*
*  Plot the echo:
*
      call window(1.d0, -3.50d0, 2001.d0 , 3.50d0)
      call viewport(.55d0, .5d0, .95d0, .9d0)
      do i=1,2001
         call line(dble(i),echo(i),dble(i),echo(i))
      end do	
      call cursor(16,1)
      write(*,1001)
1001  format(57x,'FIGURE 6.9')
*
*  Compute the cross-correlation for different 'lag' times:
*
      biggest  = -10.d6
      smallest = 10.d6
      do shift = 1, 2001
         sum = 0.d0
         do  j=1,501
            sum = sum + reference(j)*echo(j+shift-1)
         end do
         if(sum .gt.  biggest) biggest = sum
         if(sum .lt. smallest)smallest = sum
         cross_correlation(shift) = sum 
      end do
*
*  Plot the cross-correlation:
*
      call window(1.d0,smallest, 2001.d0 ,biggest)
      call viewport(.25d0, .05d0, .75d0, .5d0)
      do i = 2,2001
         call line(dble(i-1),cross_correlation(i-1),
     +             dble( i ),cross_correlation( I ) )
      end do
      call cursor(29,1)
      write(*,1002)
1002  format(33x,'FIGURE 6.11')

      call gend
      end



      double precision function Noise
*------------------------------------------------------------------*
*                                                                  *
* This function uses the Box-Muller method to generate             *
* a gaussian distribution of random numbers.  This                 *
* implementation is NOT particularly efficient.                    * 
*                                                                  *
*                                        1/1/93    pld             *
*                                                                  *
*------------------------------------------------------------------*
*
      real number
      double precision u, v, r

1     call random(number)            
      u = number + number - 1.d0
      call random(number)
      v = number + number - 1.d0
      r = u**2 + v**2
      if(r .ge. 1.d0)goto 1

      Noise = v * sqrt(-2.d0*log(r)/r)
      return
      end

