summaryrefslogtreecommitdiff
path: root/lib/jt9_decode.f90
blob: 9970e45ff0a1525f1d430a1955aeee9091075b44 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
module jt9_decode
  use, intrinsic :: iso_c_binding, only: c_short, c_ptr, c_float, c_float_complex, c_null_ptr, c_associated
  use fftw3, only: fftwf_plan_dft_r2c_1d, fftwf_execute_dft_r2c, fftwf_destroy_plan, FFTW_ESTIMATE
  use jt9_downsample, only: jt9_downsample_workspace
  use jt9_soft_symbols, only: softsym
  private
  public :: jt9_decoder, jt9_spectrum_workspace

  type :: jt9_spectrum_workspace
     private
     real(c_float), allocatable :: input(:)
     complex(c_float_complex), allocatable :: output(:)
     type(c_ptr) :: plan=c_null_ptr
   contains
     procedure :: initialize => initialize_spectrum
     procedure :: prepare_spectra
     procedure :: clear => clear_spectrum
     final :: finalize_spectrum
  end type jt9_spectrum_workspace

  type :: jt9_decoder
     procedure(jt9_decode_callback), pointer :: callback=>null()
     type(jt9_downsample_workspace), private :: downsample
     type(jt9_spectrum_workspace), private :: spectrum
     real, allocatable, private :: spectra(:,:), ccfred(:), red2(:)
     logical, allocatable, private :: ccfok(:), done(:)
   contains
     procedure :: initialize
     procedure :: decode
     procedure :: decode_pcm
     procedure :: release_input
     procedure :: reset
     final :: finalize_decoder
  end type jt9_decoder

  abstract interface
     subroutine jt9_decode_callback (this, sync, snr, dt, freq, drift, &
          decoded)
       import jt9_decoder
       implicit none
       class(jt9_decoder), intent(inout) :: this
       real, intent(in) :: sync
       integer, intent(in) :: snr
       real, intent(in) :: dt
       real, intent(in) :: freq
       integer, intent(in) :: drift
       character(len=22), intent(in) :: decoded
     end subroutine jt9_decode_callback
  end interface

contains

  subroutine initialize(this)
    class(jt9_decoder), intent(inout) :: this
    if(.not.allocated(this%ccfred)) then
       allocate(this%ccfred(6827),this%red2(6827),this%ccfok(6827),this%done(6827))
    endif
  end subroutine initialize

  subroutine decode_pcm(this,callback,samples,sample_count,receive_frequency, &
       search_low,search_high,tolerance,depth,submode,repeat)
    class(jt9_decoder), intent(inout) :: this
    procedure(jt9_decode_callback) :: callback
    ! The engine owns the full analysis buffer and pads beyond sample_count.
    integer(c_short), intent(in) :: samples(60*12000)
    integer, intent(in) :: sample_count,receive_frequency,search_low,search_high,tolerance,depth,submode
    logical, intent(in) :: repeat
    integer :: count,half_symbols

    count=max(0,min(sample_count,60*12000))
    half_symbols=min(181,max(0,(count-2048)/3456))
    call this%release_input()
    if(half_symbols.lt.2) return
    if(.not.any(samples(1:count).ne.0)) return
    if(.not.allocated(this%spectra)) allocate(this%spectra(184,6827))
    call this%spectrum%prepare_spectra(samples,half_symbols,this%spectra)
    call this%decode(callback,this%spectra,samples,receive_frequency,.true.,count/8, &
         search_low,search_low,search_high,tolerance,half_symbols,repeat,depth,9,submode,0)
  end subroutine decode_pcm

  subroutine initialize_spectrum(this)
    class(jt9_spectrum_workspace), intent(inout) :: this
    if(c_associated(this%plan)) return
    allocate(this%input(16384),this%output(0:8192))
    !$omp critical(fftw)
    this%plan=fftwf_plan_dft_r2c_1d(16384,this%input,this%output,FFTW_ESTIMATE)
    !$omp end critical(fftw)
  end subroutine initialize_spectrum

  subroutine prepare_spectra(this,samples,half_symbols,spectra)
    class(jt9_spectrum_workspace), intent(inout) :: this
    integer(c_short), intent(in) :: samples(:)
    integer, intent(in) :: half_symbols
    real, intent(out) :: spectra(184,6827)
    integer :: row,first,last,source_first
    real, parameter :: scale=(1.0/16384)**2

    call this%initialize()
    spectra=0.
    do row=1,half_symbols
       last=row*3456
       first=last-16384+1
       source_first=max(1,first)
       this%input=0.
       this%input(source_first-first+1:16384)=0.1*samples(source_first:last)
       call fftwf_execute_dft_r2c(this%plan,this%input,this%output)
       spectra(row,:)=scale*(real(this%output(0:6826))**2+aimag(this%output(0:6826))**2)
    enddo
  end subroutine prepare_spectra

  subroutine clear_spectrum(this)
    class(jt9_spectrum_workspace), intent(inout) :: this
    !$omp critical(fftw)
    if(c_associated(this%plan)) call fftwf_destroy_plan(this%plan)
    !$omp end critical(fftw)
    this%plan=c_null_ptr
    if(allocated(this%input)) deallocate(this%input,this%output)
  end subroutine clear_spectrum

  subroutine finalize_spectrum(this)
    type(jt9_spectrum_workspace), intent(inout) :: this
    call this%clear()
  end subroutine finalize_spectrum

  subroutine release_input(this)
    class(jt9_decoder), intent(inout) :: this
    call this%downsample%invalidate()
    nullify(this%callback)
  end subroutine release_input

  subroutine reset(this)
    class(jt9_decoder), intent(inout) :: this
    call this%release_input()
  end subroutine reset

  subroutine finalize_decoder(this)
    type(jt9_decoder), intent(inout) :: this
    call this%downsample%clear()
    call this%spectrum%clear()
    if(allocated(this%spectra)) deallocate(this%spectra)
    if(allocated(this%ccfred)) deallocate(this%ccfred,this%red2,this%ccfok,this%done)
    nullify(this%callback)
  end subroutine finalize_decoder

  subroutine decode(this,callback,ss,id2,nfqso,newdat,npts8,nfa,    &
       nfsplit,nfb,ntol,nzhsym,nagain,ndepth,nmode,nsubmode,nexp_decode)
    use timer_module, only: timer

    include 'constants.f90'
    class(jt9_decoder), intent(inout) :: this
    procedure(jt9_decode_callback) :: callback
    real ss(184,NSMAX)
    logical, intent(in) :: newdat, nagain
    character*22 msg
    integer*2, intent(in) :: id2(*)
    integer*1 i1SoftSymbols(207)
    common/decstats/ntry65a,ntry65b,n65a,n65b,num9,numfano

    if(nexp_decode.eq.-99) stop     !Silence compiler warning
    call this%initialize()
    associate(ccfred=>this%ccfred,red2=>this%red2,ccfok=>this%ccfok,done=>this%done)
    this%callback => callback
    if(newdat) call this%downsample%invalidate()
    if(nmode.eq.9 .and. nsubmode.ge.1) then
       call decode9w(nfqso,ntol,nsubmode,ss,nzhsym,id2,sync,nsnr,xdt,freq,msg)
       if (associated(this%callback).and.len_trim(msg)>0) then
          ndrift=0
          call this%callback(sync,nsnr,xdt,freq,ndrift,msg)
       end if
       go to 999
    endif

    nsynced=0
    ndecoded=0
    nsps=6912                                   !Params for JT9-1
    df3=1500.0/2048.0

    tstep=0.5*nsps/12000.0                      !Half-symbol step (seconds)
    done=.false.

    nf0=0
    nf1=nfa
    if(nmode.eq.65+9) nf1=nfsplit
    ia=max(1,nint((nf1-nf0)/df3))
    ib=min(NSMAX,nint((nfb-nf0)/df3))
    if(ib.le.ia) go to 999
    lag1=-int(2.5/tstep + 0.9999)
    lag2=int(5.0/tstep + 0.9999)
    call timer('sync9   ',0)
    call sync9(ss,nzhsym,lag1,lag2,ia,ib,ccfred,red2,ipk)
    call timer('sync9   ',1)

    nsps8=nsps/8
    df8=1500.0/nsps8
    dblim=db(864.0/nsps8) - 26.2

    ia1=1                         !quel compiler gripe
    ib1=1                         !quel compiler gripe
    do nqd=1,0,-1
       limit=5000
       ccflim=3.0
       red2lim=1.6
       schklim=2.2
       if(iand(ndepth,7).eq.2) then
          limit=10000
          ccflim=2.7
       endif
       if(iand(ndepth,7).eq.3 .or. nqd.eq.1) then
          limit=30000
          ccflim=2.5
          schklim=2.0
       endif
       if(nagain) then
          limit=100000
          ccflim=2.4
          schklim=1.8
       endif
       ccfok=.false.

       if(nqd.eq.1) then
          nfa1=nfqso-ntol
          nfb1=nfqso+ntol
          ia=max(1,nint((nfa1-nf0)/df3))
          ib=min(NSMAX,nint((nfb1-nf0)/df3))
          ccfok(ia:ib)=(ccfred(ia:ib).gt.(ccflim-2.0)) .and.               &
               (red2(ia:ib).gt.(red2lim-1.0))
          ia1=ia
          ib1=ib
       else
          nfa1=nf1
          nfb1=nfb
          ia=max(1,nint((nfa1-nf0)/df3))
          ib=min(NSMAX,nint((nfb1-nf0)/df3))
          do i=ia,ib
             ccfok(i)=ccfred(i).gt.ccflim .and. red2(i).gt.red2lim
          enddo
          ccfok(ia1:ib1)=.false.
       endif

       fgood=0.
       do i=ia,ib
          if(done(i) .or. (.not.ccfok(i))) cycle
          f=(i-1)*df3
          if(nqd.eq.1 .or.                                                   &
               (ccfred(i).ge.ccflim .and. abs(f-fgood).gt.10.0*df8)) then

             call timer('softsym ',0)
             fpk=nf0 + df3*(i-1)
             call softsym(this%downsample,id2,npts8,nsps8,fpk,syncpk,snrdb,xdt,    &
                  freq,drift,a3,schk,i1SoftSymbols)
             call timer('softsym ',1)

             sync=(syncpk+1)/4.0
             if(nqd.eq.1 .and. ((sync.lt.0.5) .or. (schk.lt.1.0))) cycle
             if(nqd.ne.1 .and. ((sync.lt.1.0) .or. (schk.lt.1.5))) cycle

             call timer('jt9fano ',0)
             call jt9fano(i1SoftSymbols,limit,nlim,msg)
             call timer('jt9fano ',1)

             if(sync.lt.0.0 .or. snrdb.lt.dblim-2.0) sync=0.0
             nsync=int(sync)
             if(nsync.gt.10) nsync=10
             nsnr=nint(snrdb)
             ndrift=nint(drift/df3)
             num9=num9+1

             if(msg.ne.'                      ') then
                numfano=numfano+1
                if (associated(this%callback)) then
                   call this%callback(sync,nsnr,xdt,freq,ndrift,msg)
                end if
                iaa=max(1,i-1)
                ibb=min(NSMAX,i+22)
                fgood=f
                nsynced=1
                ndecoded=1
                ccfok(iaa:ibb)=.false.
                done(iaa:ibb)=.true.
             endif
          endif
       enddo
       if(nagain) exit
    enddo

999 nullify(this%callback)
    end associate
    return
  end subroutine decode
end module jt9_decode