summaryrefslogtreecommitdiff
path: root/lib/ft8/ft8_a8d.f90
blob: ba3800e7a81ed0371f7b7a44f4985bd69ec9e224 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
subroutine ft8_a8d(dd,mycall,dxcall,dxgrid,f1a,xdt,fbest,xsnr,plog,msgbest, &
     progress_generation,knowledge,evidence)

! List decoding for FT8, activated only at nfqso (Rx Freq) +/- 10 Hz and when
! DxCall and DxGrid are populated. Returns xdt, fbest, and msgbest.

  use packjt77
  use ft8_codec_context, only: get_ft8_codec_state,genft8_for_state
  use decoder_engine_types, only: ft8_signal_evidence,engine_payload_hypothesis
  use ft8_a7
  use timer_module, only: timer
  use decode_completion_module, only: write_decode_progress
  include 'ft8_params.f90'           !Set various constants
  parameter (NWAVE=NN*NSPS/NDOWN)    !Length of generated cwave = 2528
  parameter (NZZ=3200)               !Length of downsampled arrays
  parameter (NFFT=NZZ,NH=NFFT/2)     !FFT length
  parameter (NMSGS=206)              !Number of messages to be tried
  parameter (PLOG_MIN=-160.0)        !Minimum log probability
  character mycall*12,dxcall*12,dxgrid*6
  character*37 msg,msgsent,msgbest
  character*77 c77
  integer*1 msgbits(77)
  integer*1 msgbits_best(77)
  real xjunk(NZZ)
  real s(-NH:NH)                     !Power spectrum of cd0
!  real s00(-NH:NH)                   !Raw spectrum of downsampled data
  real s0(-NH:NH)                    !Best-fit re-centered spectrum for this msg
  real s1(-NH:NH)                    !Overall best-fit re-centered spectrum
  real s8(0:7)                       !Spectrum of a single symbol
  real dd(NMAX)                      !Floating point copy of iwave
  real a(5)
  complex cwave(0:NZZ-1)             !Complex waveform corresponding to msg
  complex cd(0:NZZ-1)                !Data from dd, now complex and sampled at 200 Hz
  complex cd0(0:NZZ-1)               !All tones in cd nominally shifted to 0 Hz
  complex csymb(0:31)
  integer itone(NN)
  integer itone_best(NN)
  integer, intent(in) :: progress_generation
  type(pack77_state), target, optional, intent(inout) :: knowledge
  type(pack77_state), pointer :: codec
  type(ft8_signal_evidence), optional, intent(out) :: evidence
  integer ipk(1)
  logical newdat

  codec => get_ft8_codec_state()
  if(present(knowledge)) codec => knowledge
  if(present(evidence)) evidence=ft8_signal_evidence()
  f1=f1a
  newdat=.true.
! Mix from f1 to baseband and downsample from dd into cd (complex, 200 Hz sampling)
  call ft8_downsample(dd,newdat,f1,cd)
  fac=1.e-6

  fsd=200.0                              !Downsampled rate (Hz)
  bt=2.0                                 !Gaussian smoothing parameter
  dt=1.0/fsd
  df=200.0/NFFT
  s=0.
  ss=0.
  sq=0.

! Find the message that fits the received waveform best
  sbest=0.
  tbest=0.
  fbest=0.
  msgbest=''
  itone_best=0
  msgbits_best=0
  do imsg=1,NMSGS
     call write_decode_progress(progress_generation)
     call getmsg(imsg,mycall,dxcall,dxgrid,msg)

! Source-encode the message, get itone(), and generate complex FT8 waveform.
     i3=-1
     n3=-1
     call pack77_for_state(codec,msg,i3,n3,c77)
     call genft8_for_state(codec,msg,i3,n3,msgsent,msgbits,itone)
     call gen_ft8wave(itone,NN,32,bt,fsd,0.0,cwave,xjunk,1,NWAVE)
     cwave(NWAVE:)=0.

! For this message, find the best lag
     spk=0.
     fpk=0.
     tpk=0.
     lagpk=0
     lag1=-200
     lag2=200
     lagstep=4
     do iter=1,2
        if(iter.eq.2) then
           lag1=lagpk-8
           lag2=lagpk+8
           lagstep=1
        endif
        do lag=lag1,lag2,lagstep
           do i=0,NWAVE-1
              j=i+lag + 100
              cd0(i)=0.
              if(j.ge.0 .and. j.le.NWAVE-1) cd0(i)=cd(j)*conjg(cwave(i))
           enddo

           cd0(NWAVE:)=0.
           call four2a(cd0,NFFT,1,-1,1)               !Forward c2c FFT

           do i=0,NFFT-1
              j=i
              if(i.gt.NH) j=i-NFFT
              s(j)=fac*(real(cd0(i))**2 + aimag(cd0(i))**2)
           enddo

           call smo121(s,NFFT+1)

           smax=0.
           do j=-NH,NH
              smax=max(s(j),smax)
              if(s(j).gt.spk) then
                 spk=s(j)
                 fpk=j*df + f1
                 lagpk=lag
                 tpk=lag*dt
                 s0=s                       !Save best-fit spectrum for this msg
              endif
           enddo  ! j
        enddo  ! lag
     enddo  ! iter

     if(spk.gt.sbest) then
        sbest=spk
        fbest=fpk
        tbest=tpk
        msgbest=msg
        itone_best=itone
        msgbits_best=msgbits
        s1=s0                                !Save overall best-fit spectrum
     endif
  enddo  ! imsg

  a=0
  a(1)=f1-fbest
!  if(abs(a(1)+5.0).gt.10.0) then             !Effective Ftol = 10 Hz
  if(abs(a(1)).gt.5.0) then                   !Effective Ftol = 5 Hz
     msgbest=''
     return
  endif
  call twkfreq1(cd,NZZ,200.0,a,cd)           !Re-center the spectrum
  
  xdt=tbest
  ave=(sum(s1(-200:-100)) + sum(s1(100:200)))/202.0
  s1=s1/ave - 1.0
  s1pk=maxval(s1(-32:32))
  sig=0.
  nsig=0
  do i=-32,32
     if(s1(i).lt.0.5*s1pk) cycle
     sig=sig+s1(i)
     nsig=nsig+1
  enddo
  sig=sig/nsig
  xsnr=db(sig)-35.0
  if(xsnr.lt.-30) xsnr=-30                   !Set min SNR for 'a8' decodes

  if(msgbest.ne.'') then
! Compute probability of successful decode.
     plog=0.0
     nhard=0
     nsum=0
     sum_sync=0.
     sum_sig=0.
     sum_big=0.
     do k=1,NN
        i0=32*(k-1) + nint((tbest+0.5)/0.005)
        csymb=0.
        do i=0,31
           if(i0+i.ge.0 .and. i0+i.le.NZZ-1) csymb(i)=cd(i0+i)
        enddo
        call four2a(csymb,32,1,-1,1)           !c2c
        do i=0,7
           s8(i)=real(csymb(i))**2 + aimag(csymb(i))**2
        enddo
        s8sum=sum(s8)
        if(s8sum.gt.0.0) then
           p=s8(itone_best(k))/s8sum
           plog=plog + log(p)
           nsum=nsum+1
        endif
        ipk=maxloc(s8)-1
        if(ipk(1).ne.itone_best(k)) nhard=nhard+1
        if(k.le.7 .or. (k.ge.37 .and. k.le.43) .or. k.ge.73) then
           sum_sync=sum_sync + s8(itone_best(k))
        else
           sum_sig=sum_sig + s8(itone_best(k))
        endif
        sum_big=sum_big + s8(ipk(1))
     enddo
     if(nsum.lt.NN) plog=plog + (NN-nsum)*log(0.125)
     sigobig=(sum_sync+sum_sig)/sum_big
!     write(60,3060) nsum,xdt,fbest,xsnr,plog,nhard,sigosync,sigobig,trim(msgbest)
!3060 format(i2,f8.3,3f8.1,i5,2f7.2,1x,a)
     if(nhard.gt.54 .or. plog.lt.-159.0 .or. sigobig.lt.0.71) msgbest=''
  endif
  if(present(evidence) .and. msgbest.ne.'') then
     evidence%payload77=msgbits_best
     evidence%tones=int(itone_best,kind=1)
     evidence%payload_origin=engine_payload_hypothesis
     evidence%has_tones=1
     evidence%has_waveform_start=1
     evidence%waveform_start_seconds=xdt+0.5
     evidence%method=8
  endif
  
  return
end subroutine ft8_a8d