summaryrefslogtreecommitdiff
path: root/map65/libm65/ccf2.f90
blob: 521c7d6f16156f57da5bdfe75af75f164a36092e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module ccf2_mod
  implicit none
contains

subroutine ccf2(ss, nz, nflip, ccfbest, lagpk)
  implicit none

  !==== Arguments ============================================================
  integer, intent(in)  :: nz
  real,    intent(in)  :: ss(nz)
  integer, intent(in)  :: nflip
  real,    intent(out) :: ccfbest
  integer, intent(out) :: lagpk

  !==== Parameters ===========================================================
  integer, parameter :: LAGMAX = 200

  !==== Local variables ======================================================
  real    :: ccf(-LAGMAX:LAGMAX)
  real    :: s0, s1, x
  integer :: npr(126)
  integer :: i, j, lag, lag1, lag2
    
  !==== JT65 pseudo-random sync pattern =====================================
  data npr / &
    1,0,0,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1,0,0, &
    0,1,0,1,1,0,0,1,0,0,0,1,1,1,0,0,1,1,1,1, &
    0,1,1,0,1,1,1,1,0,0,0,1,1,0,1,0,1,0,1,1, &
    0,0,1,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1, &
    1,0,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1, &
    0,1,0,1,0,0,1,1,0,0,1,0,0,1,0,0,0,0,1,1, &
    1,1,1,1,1,1 /

  !==== Initialize outputs ===================================================
  ccfbest = 0.0
  lagpk   = 0

  !==== Main correlation loop ===============================================
  lag1 = -LAGMAX
  lag2 =  LAGMAX

  do lag = lag1, lag2
     s0 = 0.0
     s1 = 0.0

     do i = 1, 126
        j = 2*(8*i + 43) + lag
        if (j >= 1 .and. j <= nz-8) then
           x = ss(j) + ss(j+8)
           if (npr(i) == 0) then
              s0 = s0 + x
           else
              s1 = s1 + x
           end if
        end if
     end do

     ccf(lag) = nflip * (s1 - s0)

     if (ccf(lag) > ccfbest) then
        ccfbest = ccf(lag)
        lagpk   = lag
     end if
  end do

end subroutine ccf2

end module ccf2_mod