summaryrefslogtreecommitdiff
path: root/lib/qra/q65/q65_loops.f90
blob: b7e706f2b7ac12d366dc72f27ce7667ceb077c4c (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
subroutine q65_loops(workspace,c00,npts2,nsps2,nsubmode,ndepth,jpk0,    &
     xdt0,f0,iaptype,xdt1,f1,snr2,dat4,idec)

  use packjt77
  use timer_module, only: timer
  use q65
  use q65_workspace, only: q65_workspace_type
  
  parameter (NN=63)
  parameter (LN=2176*63)           !LN=LL*NN; LL=64*(mode_q65+2), NN=63
  type(q65_workspace_type), intent(inout) :: workspace
  complex c00(0:npts2-1)           !Analytic representation of dd(), 6000 Hz
  character decoded*37
  real a(3)                        !twkfreq params f,f1,f2
  integer dat4(13)                 !Decoded message (as 13 six-bit integers)
  integer nap(0:11)                !AP return codes
  data nap/0,2,3,2,3,4,2,3,6,4,6,6/

  LL=64*(mode_q65+2)
  call workspace%ensure(npts2,LL)
  call workspace%prepare_fft(nsps2)
  idec=-1
  ircbest=9999
  irc=-99
  s3lim=20.
  baud=6000.0/nsps2

  idfmax=1
  idtmax=1
  maxdist=4
  ibw0=(ibwa+ibwb)/2
  if(iand(ndepth,3).eq.2) then
     idfmax=3
     idtmax=3
     maxdist=5
  endif
  if(iand(ndepth,3).eq.3) then
     idfmax=5
     idtmax=5
     maxdist=5
  endif

  napmin=99
  xdt1=xdt0
  f1=f0
  idfbest=0
  idtbest=0
  ndistbest=0

  do idf=1,idfmax
     ndf=idf/2
     if(mod(idf,2).eq.0) ndf=-ndf
     a=0.
     a(1)=-(f0+0.5*baud*ndf)
! Variable 'drift' is frequency increase over full TxT.  Therefore we want:
     a(2)=-0.5*drift
     call twkfreq(c00,workspace%c0(0:npts2-1),npts2,6000.0,a)
     do idt=1,idtmax
        ndt=idt/2
        if(mod(idt,2).eq.0) ndt=-ndt
        jpk=jpk0 + nsps2*ndt/16              !tsym/16
        jpk=max(0,jpk)
        jpk=min(29000,jpk)
        call spec64(workspace%c0(0:npts2-1),workspace%cs,npts2,nsps2,  &
             mode_q65,jpk,workspace%s3(1:LL*NN),LL,NN,workspace%plan)
        call pctile(workspace%s3(1:LL*NN),LL*NN,40,base)
        if(base<=0.) cycle
        workspace%s3(1:LL*NN)=workspace%s3(1:LL*NN)/base
        where(workspace%s3(1:LL*NN)>s3lim) workspace%s3(1:LL*NN)=s3lim
        call q65_bzap(workspace%s3(1:LL*NN),LL) !Zap birdies
        do ibw=ibwa,ibwb
           ndist=ndf**2 + ndt**2 + (ibw-ibw0)**2
           if(ndist.gt.maxdist) cycle
           b90=1.72**ibw
           if(b90.gt.345.0) cycle
           b90ts = b90/baud
           call timer('dec2    ',0)
           call q65_dec2(workspace%s3(1:LL*NN),nsubmode,b90ts,esnodb,irc,dat4,decoded)
           call timer('dec2    ',1)
              ! irc > 0 ==> number of iterations required to decode
              !  -1 = invalid params
              !  -2 = decode failed
              !  -3 = CRC mismatch
           if(irc.ge.0) then
              idfbest=idf
              idtbest=idt
              ndistbest=ndist
              nrc=irc
              go to 100
           endif
        enddo  ! ibw (b90 loop)
     enddo  ! idt (DT loop)
  enddo  ! idf (f0 loop)

100 if(irc.ge.0) then
     idec=iaptype
     snr2=esnodb - db(2500.0/baud)
     xdt1=xdt0 +  nsps2*ndt/(16.0*6000.0)
     f1=f0 + 0.5*baud*ndf
  endif

  return
end subroutine q65_loops