Program Test_MLS

      Program Test_MLS
c
c This code demonstrates the generation and subsequent
c circular autocorrelation of Maximum Length Sequences
c
c (c) J.J.Bunn 1996, Julian.Bunn@caltech.edu
c
      parameter (maxpow=13,maxlen=2**maxpow)
      common /setup/ length
      integer length
      integer*1 mls(maxlen)
      integer*2 ss(maxlen)
      do i=2,10
         length = 2**i
         Call Get_Sequence(mls)
         do j=1,length
            ss(j) = mls(j)
         end do
         write(6,'(20i3)') (mls(j),j=1,length-1)
         Call MLS_Correlate(mls,ss)
         write(6,*) 'Correlation:'
         write(6,'(20i3)') (ss(j),j=1,length-1)
      end do
      end

Subroutine Get_Sequence

      Subroutine Get_Sequence(mls)
c
c MLS sequence generator for up to 4 taps in a 24 bit register
c
      parameter (maxpow=13,maxlen=2**maxpow)
      parameter (maxelements=24)
      common /setup/ length
      integer length
      integer*1 mls(*)
      integer bit(maxelements)
      integer tap(4,maxelements)
      integer ntap(maxelements)
      integer nelements
      character*2 ctemp
      save ntap,tap
      data ntap / 0, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 4,
     &            4, 4, 2, 4, 2, 2, 4, 2, 2, 2, 2, 4/
      data tap  / 0, 0, 0, 0,
     &            1, 2, 0, 0,
     &            2, 3, 0, 0,
     &            3, 4, 0, 0,
     &            3, 5, 0, 0,
     &            5, 6, 0, 0,
     &            6, 7, 0, 0,
     &            2, 3, 5, 8,
     &            5, 9, 0, 0,
     &            7,10, 0, 0,
     &            9,11, 0, 0,
     &            6, 8,11,12,
     &            9,10,12,13,
     &            4, 8,13,14,
     &           14,15, 0, 0,
     &            4,13,15,16,
     &           14,17, 0, 0,
     &           11,18, 0, 0,
     &           14,17,18,19,
     &           17,20, 0, 0,
     &           19,21, 0, 0,
     &           21,22, 0, 0,
     &           18,23, 0, 0,
     &           17,22,23,24/
      do i=2,maxpow
         if(length.eq.2**i) then
            nelements = i
            goto 1
         endif
      end do
      nelements = 7
      length = 2**nelements
    1 continue
      nelements = max(2,min(nelements,maxpow))
      write(ctemp,'(i2)') nelements
      write(6,*) 'MLS calculation for '//
     &               ctemp//' elements.'
c
c Initialise the register elements
c
      do i=1,nelements
         bit(i) = 1
      end do
      N = length-1
      do i=1,N
         isum = 0
c
c Sum the contributions from each tap
c
         do j=1,ntap(nelements)
            isum = isum + bit( tap(j,nelements) )
         end do
c
c Shift the register to the right
c
         do j=nelements,2,-1
            bit(j) = bit(j-1)
         end do
c
c replace the first bit with the modulo-2 tap sum
c
         bit(1) = mod(isum,2)
         mls(i) = bit(1)
      end do
      mls(length) = 0
      end

Subroutine mls_correlate

      Subroutine mls_correlate(mls,ss)
      parameter (maxpow=13,maxlen=2**maxpow)
      common /setup/ length
      integer length
      integer*1 mls(*)
      integer*2 ss(*)
      integer result(maxlen)
      o2length = 0.5/real(length-1)
      do n=1,length-1
         sum = 0.0
         do l=1,length-1
            npl = n + l - 1
            if(npl.gt.length-1) npl = npl - (length-1)
            a = mls(l)
            if(a.le.0.) a = -1.
            b = ss(npl)
            if(b.le.0.) b = -1.
            sum = sum + a*b
         end do
         result(n) = nint(sum*o2length)
      end do
      do i=1,length-1
         ss(i) = result(i)
      end do
      end