summaryrefslogtreecommitdiff
path: root/lib/ft8var/sync8dvar.f90
blob: 84b6507e32ff30eb575dc18e768cd160321c7388 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
subroutine sync8dvar(cd0,i0,ctwk,itwk,sync,ipass,lastsync,iqso,lcq,lcallsstd,lcqcand)

! Compute sync power for a complex, downsampled FT8 signal.
  use ft8_mod1, only : csync,csynce,csyncsd,csyncsdcq,csynccq, &
       csyncsd_valid,csyncsdcq_valid

  complex, intent(in) :: cd0(-800:4000),ctwk(32)
  complex csync1(0:18,32),csync2(32),z1,z2,z3,z4,zt1(0:6),zt2(0:6),zt3(0:6),zt4(0:7),zt5(0:18), &
          z11(224),z22(224),z33(224)
  real sync1(0:20)!,sync2(0:20)
  integer, intent(in) :: i0,ipass,iqso
  logical(1), intent(in) :: lcq,lcallsstd,lcqcand
  logical(1) lastsync,lsync1_valid

  sync=0.; sync1=0.; zt1=0.; zt2=0.; zt3=0.; z4=0.; z11=0.; z22=0.; z33=0.!; sync2=0.
  lsync1_valid=.false.
  k=1
  do i=0,6 ! Sum over 7 Costas frequencies and three Costas arrays
    i1=i0+i*32; i2=i1+1152; i3=i1+2304 ! +36*32, +72*32
    csync2=csync(i,1:32)
    if(itwk.eq.1) csync2=ctwk*csync2      !Tweak the frequency
    z11(k:k+31)=cd0(i1:i1+31)*conjg(csync2);  z22(k:k+31)=cd0(i2:i2+31)*conjg(csync2)
    z33(k:k+31)=cd0(i3:i3+31)*conjg(csync2)
    zt1(i)=sum(z11(k:k+31)); zt2(i)=sum(z22(k:k+31)); zt3(i)=sum(z33(k:k+31))
    k=k+32
  enddo
  if(ipass.eq.1 .or. ipass.eq.5 .or. ipass.eq.9) then
    if(.not.lastsync) then
      do i=0,6
        sync1(i) = SQRT(real(zt1(i))**2 + aimag(zt1(i))**2)
        sync1(i+7) = SQRT(real(zt2(i))**2 + aimag(zt2(i))**2)
        sync1(i+14) = SQRT(real(zt3(i))**2 + aimag(zt3(i))**2)
      enddo
      sync=sum(sync1)
    else
      do i=0,6
        sync = sync + real(zt1(i))**2 + aimag(zt1(i))**2 + real(zt2(i))**2 + aimag(zt2(i))**2 + real(zt3(i))**2 + &
          aimag(zt3(i))**2
      enddo
    endif
  else if(ipass.eq.2 .or. ipass.eq.6 .or. ipass.eq.7) then
    if(.not.lastsync) then
      do i=0,6
        sync1(i) = real(zt1(i))**2 + aimag(zt1(i))**2
        sync1(i+7) = real(zt2(i))**2 + aimag(zt2(i))**2
        sync1(i+14) = real(zt3(i))**2 + aimag(zt3(i))**2
      enddo
      sync=sum(sync1)
    else
      do i=0,6
        sync = sync + real(zt1(i))**2 + aimag(zt1(i))**2 + real(zt2(i))**2 + aimag(zt2(i))**2 + real(zt3(i))**2 + &
          aimag(zt3(i))**2
      enddo
    endif
  else ! if(ipass.eq.3 .or. ipass.eq.4 .or. ipass.eq.8)  then
    do i=0,6
      if(i.lt.6) then
        z1=(zt1(i)+zt1(i+1))/2; z2=(zt2(i)+zt2(i+1))/2; z3=(zt3(i)+zt3(i+1))/2 ! use i=5 z values for i=6
      endif
      if(.not.lastsync) then
        sync = sync + abs(real(z1)) + abs(aimag(z1)) + abs(real(z2)) + abs(aimag(z2)) + abs(real(z3)) + abs(aimag(z3))
      else
        sync = sync + real(z1)**2 + aimag(z1)**2 + real(z2)**2 + aimag(z2)**2 + real(z3)**2 + aimag(z3)**2
      endif
    enddo
  endif

  if(itwk.eq.1 .and. lcqcand .and. iqso.eq.1) then
    zt4=0.
    do i=0,7
      csync2=csynccq(i,1:32); csync2=ctwk*csync2      !Tweak the frequency
      i4=i0+(i+7)*32; zt4(i)=sum(cd0(i4:i4+31)*conjg(csync2))
    enddo
    do i=0,7
      if(ipass.eq.3 .or. ipass.eq.4 .or. ipass.eq.8)  then
        if(i.lt.7) z4=(zt4(i)+zt4(i+1))/2 ! use i=6 z4 value for i=7
      else
       z4=zt4(i)
      endif
      if(ipass.eq.1 .or. ipass.eq.5 .or. ipass.eq.9) then; sync = sync + SQRT(real(z4)**2 + aimag(z4)**2)
      else if(ipass.eq.2 .or. ipass.eq.6 .or. ipass.eq.7) then; sync = sync + real(z4)**2 + aimag(z4)**2
      else if(ipass.eq.3 .or. ipass.eq.4 .or. ipass.eq.8) then; sync = sync + abs(real(z4)) + abs(aimag(z4))
      endif
    enddo
  endif

  if(.not.lastsync) then
    if(iqso.gt.1 .and. iqso.lt.4 .and. lcallsstd) then
      csync1=csynce
      lsync1_valid=.true.
    else if(iqso.eq.4 .and. .not.lcq .and. csyncsd_valid) then
      csync1=csyncsd
      lsync1_valid=.true.
    endif

    if(lsync1_valid) then
      zt5=0.
      do i=0,18
        csync2=csync1(i,1:32)
        if(itwk.eq.1) csync2=ctwk*csync2      !Tweak the frequency
        i4=i0+(i+7)*32; zt5(i)=sum(cd0(i4:i4+31)*conjg(csync2))
      enddo
      do i=0,18
        if(ipass.eq.2 .or. ipass.eq.6 .or. ipass.eq.7)  then
          if(i.lt.18) z4=(zt5(i)+zt5(i+1))/2 ! use i=17 z4 value for i=18
        else
         z4=zt5(i)
        endif
        if(ipass.eq.1 .or. ipass.eq.5 .or. ipass.eq.9) then; sync = sync + SQRT(real(z4)**2 + aimag(z4)**2)
        else if(ipass.eq.2 .or. ipass.eq.6 .or. ipass.eq.7) then; sync = sync + real(z4)**2 + aimag(z4)**2
        else if(ipass.eq.3 .or. ipass.eq.4 .or. ipass.eq.8) then; sync = sync + abs(real(z4)) + abs(aimag(z4))
        endif
      enddo
    endif

    if(iqso.eq.4 .and. lcq .and. csyncsdcq_valid) then
      do i=0,57
        csync2=csyncsdcq(i,1:32)
        if(itwk.eq.1) csync2=ctwk*csync2      !Tweak the frequency
        z4=0.; if(i.lt.29) then; k=i+7; else; k=i+14; endif
        i4=i0+k*32; z4=sum(cd0(i4:i4+31)*conjg(csync2))
        if(ipass.eq.1 .or. ipass.eq.5 .or. ipass.eq.9) then; sync = sync + SQRT(real(z4)**2 + aimag(z4)**2)
          else if(ipass.eq.2 .or. ipass.eq.6 .or. ipass.eq.7) then; sync = sync + real(z4)**2 + aimag(z4)**2
          else if(ipass.eq.3 .or. ipass.eq.4 .or. ipass.eq.8) then; sync = sync + abs(real(z4)) + abs(aimag(z4))
        endif
      enddo
    endif
  endif

  return
end subroutine sync8dvar