summaryrefslogtreecommitdiff
path: root/map65/libm65/wideband_sync.f90
blob: a8ae8a68d6524c51b24ee6a40f867562d53db52d (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
module wideband_sync

   use iso_fortran_env, only: real32

   type candidate
      real :: snr          !Relative S/N of sync detection
      real :: f            !Freq of sync tone, 0 to 96000 Hz
      real :: xdt          !DT of matching sync pattern, -1.0 to +4.6 s
      real :: pol          !Polarization angle, degrees
      integer :: ipol      !Polarization angle, 1 to 4 ==> 0, 45, 90, 135 deg
      integer :: iflip     !Sync type: JT65 = +/- 1, Q65 = 0
      integer :: indx
   end type candidate
   type sync_dat
      real :: ccfmax
      real :: xdt
      real :: pol
      real :: combine_pol
      integer :: ipol
      integer :: iflip
      logical :: birdie
   end type sync_dat

   integer, parameter :: MAX_CANDIDATES = 50
   real(real32), parameter :: SNR1_THRESHOLD = 4.5
   type(sync_dat), allocatable :: sync(:)  !NFFT
   type(sync_dat), allocatable :: q65_generalized_sync(:)
   integer nkhz_center

contains

   subroutine get_candidates(ss, savg, xpol, jz, nfa, nfb, nts_jt65, nts_q65, cand, ncand)
      use iso_c_binding
      use debug_log, only: dbg, itoa, rtoa
      use indexx_mod, only: indexx
      use iso_fortran_env, only: real64
      use npar_ptrs_mod, only: nrate_active, nfft_active
      
      implicit none

      !==== Dummy arguments =====================================================
      real,    intent(in)    :: ss(4,322,nfft_active)
      real,    intent(in)    :: savg(4,nfft_active)
      logical, intent(in)    :: xpol
      integer, intent(in)    :: jz, nfa, nfb
      integer, intent(in)    :: nts_jt65, nts_q65
      type(candidate), intent(out) :: cand(MAX_CANDIDATES)
      integer, intent(out)   :: ncand


! Search symbol spectra ss() over frequency range nfa to nfb (in kHz) for
! sync candidates. A valid generalized Q65 score replaces the legacy mixed-mode
! surface, so that path returns Q65 candidates only; MAP65 discovers JT65
! independently through ccf65(). Legacy paths may retain JT65 candidates.
! The nts_* variables are the submode tone spacings: 1 2 4 8 16 for A B C D E.
! Birdies are detected and excised. Candidates are returned in cand().

      integer, parameter :: MAX_PEAKS = 100
      real pavg(-20:20)
      real base, bw, diffhz, df3, flip, flip_top, pmax, snr1, snr_top, tstep
      real(real64) f0
      integer i, ia, ib, ipol, iz, j
      integer j1, j2, k, n, n_top, jsum, m
      integer, allocatable :: indx(:)
      logical skip

      call wb_sync(ss, savg, xpol, nts_q65.ne.0, jz, nfa, nfb) !Output to sync() array

      tstep = 2048.0/11025.0        !0.185760 s: 0.5*tsym_jt65, 0.3096*tsym_q65
      df3 = real(nrate_active)/real(nfft_active)
      ia = nint(1000*nfa/df3) + 1
      ib = nint(1000*nfb/df3) + 1
      if (ia .lt. 21) ia = 21
      if (ib .gt. nfft_active - 20) ib = nfft_active - 20
      iz = ib - ia + 1
                  
      allocate (indx(iz))

      ! w3sz this if block should not be necessary but it is
      if (iz <= 0) then
!         print *, 'GET_CAND: iz <= 0, ia=', ia, ' ib=', ib
         flush (6)
         return
      endif

      call indexx(sync(ia:ib)%ccfmax, iz, indx)   !Sort by relative snr

      n_top = indx(iz) + ia - 1
      snr_top = sync(n_top)%ccfmax
      flip_top = sync(n_top)%iflip
            
      k = 0
      do i = 1, MAX_PEAKS
         if ((iz + 1 - i) .lt. 1) cycle !w3sz debug
         n = indx(iz + 1 - i) + ia - 1
         f0 = 0.001*(n - 1)*df3
         snr1 = sync(n)%ccfmax
         if (snr1 .lt. SNR1_THRESHOLD) exit
         flip = sync(n)%iflip
         if (flip .ne. 0.0 .and. nts_jt65 .eq. 0) cycle
         if (flip .eq. 0.0 .and. nts_q65 .eq. 0) cycle
         if (sync(n)%birdie) cycle

! Test for signal outside of TxT range and set bw for this signal type
         j1 = max(0,min(jz,int((sync(n)%xdt + 1.0)/tstep - 1.0)))
         j2 = max(1,int((sync(n)%xdt + 52.0)/tstep + 1.0))
         if (flip .ne. 0) j2 = int((sync(n)%xdt + 47.811)/tstep + 1.0)
         j2 = max(1,j2)
         ipol = sync(n)%ipol
         pavg = 0.
         do j = 1, j1
            pavg = pavg + ss(ipol, j, n - 20:n + 20)
         enddo
         do j = j2, jz
            pavg = pavg + ss(ipol, j, n - 20:n + 20)
         enddo
         jsum = outside_row_count(j1,j2,jz)
         if (jsum .gt. 0) then
            pmax = maxval(pavg(-2:2))              !### Why not just pavg(0) ?
            base = (sum(pavg) - pmax)/jsum
            pmax = pmax/base
            if (pmax .gt. 5.0) cycle
         endif
         skip = .false.
         do m = 1, k                              !Skip false syncs within signal bw
            if (cand(m)%iflip .ne. nint(flip)) cycle   !only dedupe within the same type
            diffhz = 1000.0*(f0 - cand(m)%f)
            bw = nts_q65*110.0
            if (cand(m)%iflip .ne. 0) bw = nts_jt65*178.0
            if (diffhz .gt. -0.03*bw .and. diffhz .lt. 1.03*bw) skip = .true.
         enddo
         if (skip) cycle
         k = k + 1
         cand(k)%snr = snr1
         cand(k)%f = f0
         cand(k)%xdt = sync(n)%xdt
         cand(k)%pol = sync(n)%pol
         cand(k)%ipol = sync(n)%ipol
         cand(k)%iflip = nint(flip)
         cand(k)%indx = n            
         
!     write(50,3050) i,k,m,f0+32.0,diffhz,bw,snr1,db(snr1)
!3050 format(3i5,f8.3,2f8.0,2f8.2)
         if (k .ge. MAX_CANDIDATES) exit
      enddo
      ncand = k
      return
   end subroutine get_candidates

   subroutine wb_sync(ss, savg, xpol, q65_enabled, jz, nfa, nfb)
      use iso_c_binding
      use iso_fortran_env, only: real64
      use debug_log
      use indexx_mod
      use map65_polarization_score_mod, only: generalized_polarization_score,polarization_matrix
      use txpol_mod
      use trimlist_mod
      use pctile_mod
      use polfit_mod
      use npar_ptrs_mod, only: nrate_active, nfft_active
      implicit none

      !==== Dummy arguments =====================================================
      real,    intent(in)    :: ss(4,322,nfft_active)
      real,    intent(in)    :: savg(4,nfft_active)
      logical, intent(in)    :: xpol,q65_enabled
      integer, intent(in)    :: jz, nfa, nfb

      integer, parameter :: LAGMAX = 30
      integer, parameter :: Q65_SYNC_ROWS = 3*22, JT65_SYNC_ROWS = 2*63
      integer, parameter :: Q65_MIN_ROWS = Q65_SYNC_ROWS - 3
      integer, parameter :: JT65_MIN_ROWS = JT65_SYNC_ROWS - 2
      real(c_float) :: savg_med(4)
      real ccf4(4), ccf4best(4), a(3)
      real base, ccf, ccfmax, combine_poldeg, df3, fac, flip, poldeg, row_scale, tstep
      real(real64) :: combine_angle,g00,g01,g11,n00,n01,n11,physical_angle
      real(real64) :: projections(4),whitened_score
      integer i, ia, ib, ipolbest, j, k, lag, lagbest, nrows
      integer npol, ipol
      logical first,generalized_available,score_valid
      integer isync(22)
      integer jsync0(63), jsync1(63)
      integer q65_available(22,0:LAGMAX), q65_row_count(0:LAGMAX)
      integer jt65_0_available(63,0:LAGMAX), jt65_0_row_count(0:LAGMAX)
      integer jt65_1_available(63,0:LAGMAX), jt65_1_row_count(0:LAGMAX)
      integer ip(1)
      
        ! --- JT65 wideband debugging probe ---
        ! real :: freq_hz

! Q65 sync symbols
      data isync/1, 9, 12, 13, 15, 22, 23, 26, 27, 33, 35, 38, 46, 50, 55, 60, 62, 66, 69, 74, 76, 85/
      data jsync0/ &
         1, 4, 5, 9, 10, 11, 12, 13, 14, 16, 18, 22, 24, 25, 28, 32, &
         33, 34, 37, 38, 39, 40, 42, 43, 45, 46, 47, 48, 52, 53, 55, 57, &
         59, 60, 63, 64, 66, 68, 70, 73, 80, 81, 89, 90, 92, 95, 97, 98, &
         100, 102, 104, 107, 108, 111, 114, 119, 120, 121, 122, 123, 124, 125, 126/
      data jsync1/ &
         2, 3, 6, 7, 8, 15, 17, 19, 20, 21, 23, 26, 27, 29, 30, 31, &
         35, 36, 41, 44, 49, 50, 51, 54, 56, 58, 61, 62, 65, 67, 69, 71, &
         72, 74, 75, 76, 77, 78, 79, 82, 83, 84, 85, 86, 87, 88, 91, 93, &
         94, 96, 99, 101, 103, 105, 106, 109, 110, 112, 113, 115, 116, 117, 118/
      data first/.true./
      save first, isync, jsync0, jsync1

      tstep = 2048.0/11025.0        !0.185760 s: 0.5*tsym_jt65, 0.3096*tsym_q65
      if (first) then
         fac = 0.6/tstep
         do i = 1, 22                                !Expand the Q65 sync stride
            isync(i) = nint((isync(i) - 1)*fac) + 1
         enddo
         do i = 1, 63
            jsync0(i) = 2*(jsync0(i) - 1) + 1
            jsync1(i) = 2*(jsync1(i) - 1) + 1
         enddo
         first = .false.
      endif

      df3 = real(nrate_active)/real(nfft_active)
      ia = nint(1000*nfa/df3) + 1          !Flat frequency range for WSE converters
      ib = nint(1000*nfb/df3) + 1
      if (ia .lt. 1) ia = 1
      if (ib .gt. nfft_active - 1) ib = nfft_active - 1
      npol = 1
      if (xpol) npol = 4

      q65_row_count = 0
      jt65_0_row_count = 0
      jt65_1_row_count = 0
      do lag = 0, LAGMAX
         do j = 1, 22
            k = isync(j) + lag
            q65_available(j,lag) = min(3,max(0,jz-k+1))
            q65_row_count(lag) = q65_row_count(lag) + q65_available(j,lag)
         enddo
         do j = 1, 63
            k = jsync0(j) + lag
            jt65_0_available(j,lag) = min(2,max(0,jz-k+1))
            jt65_0_row_count(lag) = jt65_0_row_count(lag) + jt65_0_available(j,lag)

            k = jsync1(j) + lag
            jt65_1_available(j,lag) = min(2,max(0,jz-k+1))
            jt65_1_row_count(lag) = jt65_1_row_count(lag) + jt65_1_available(j,lag)
         enddo
      enddo

      do i = 1, npol
         call pctile(savg(i, ia:ib), ib - ia + 1, 50, savg_med(i))
      enddo
      n00=real(savg_med(1),real64)/real(jz,real64)
      n11=n00
      n01=0.0_real64
      generalized_available=.false.
      if (xpol .and. q65_enabled) then
         n11=real(savg_med(3),real64)/real(jz,real64)
         call pctile(0.5*(savg(2,ia:ib)-savg(4,ia:ib)),ib-ia+1,50,base)
         n01=real(base,real64)/real(jz,real64)
         call generalized_polarization_score(0.0_real64,0.0_real64,0.0_real64, &
              n00,n11,n01,whitened_score,physical_angle,combine_angle,generalized_available)
      endif
!  do i=ia,ib
!     write(14,3014) 0.001*(i-1)*df3,savg(1:npol,i)
!3014 format(5f10.3)
!  enddo

      lagbest = 0
      ipolbest = 1
      flip = 0.

      do i = ia, ib
         ccfmax = 0.
         poldeg = 0.
         combine_poldeg = 0.
         q65_generalized_sync(i)%ccfmax=0.
         q65_generalized_sync(i)%xdt=0.
         q65_generalized_sync(i)%pol=0.
         q65_generalized_sync(i)%combine_pol=0.
         q65_generalized_sync(i)%ipol=1
         q65_generalized_sync(i)%iflip=0
         q65_generalized_sync(i)%birdie=.false.
         do lag = 0, LAGMAX

            nrows = q65_row_count(lag)
            if (nrows .ge. Q65_MIN_ROWS) then
               ccf = 0.
               ccf4 = 0.
               do j = 1, 22                        !Test for Q65 sync
                  k = isync(j) + lag
                  if (q65_available(j,lag) .ge. 1) &
                     ccf4(1:npol) = ccf4(1:npol) + ss(1:npol,k,i)
                  if (q65_available(j,lag) .ge. 2) &
                     ccf4(1:npol) = ccf4(1:npol) + ss(1:npol,k+1,i)
                  if (q65_available(j,lag) .ge. 3) &
                     ccf4(1:npol) = ccf4(1:npol) + ss(1:npol,k+2,i)
               enddo
               ccf4(1:npol) = ccf4(1:npol) - savg(1:npol,i)*real(nrows)/real(jz)
               row_scale = sqrt(real(Q65_SYNC_ROWS)/real(nrows))
               ccf4(1:npol) = row_scale*ccf4(1:npol)
               ccf = maxval(ccf4)
               ip = maxloc(ccf4)
               ipol = ip(1)
               if (generalized_available) then
                  projections=real(ccf4,real64)
                  call polarization_matrix(projections,g00,g11,g01)
                  call generalized_polarization_score(g00,g11,g01,n00,n11,n01, &
                       whitened_score,physical_angle,combine_angle,score_valid)
                  if (score_valid .and. whitened_score > &
                       real(q65_generalized_sync(i)%ccfmax,real64)) then
                     q65_generalized_sync(i)%ccfmax=real(whitened_score)
                     q65_generalized_sync(i)%xdt=lag*tstep-1.0
                     q65_generalized_sync(i)%pol=real(physical_angle)
                     q65_generalized_sync(i)%combine_pol=real(combine_angle)
                     q65_generalized_sync(i)%ipol=ipol
                  endif
               endif
               if (ccf .gt. ccfmax) then
                  ipolbest = ipol
                  lagbest = lag
                  ccfmax = ccf
                  ccf4best = ccf4
                  flip = 0.
               endif
            endif

            nrows = jt65_0_row_count(lag)
            if (nrows .ge. JT65_MIN_ROWS) then
               ccf = 0.
               ccf4 = 0.
               do j = 1, 63                       !Test for JT65 sync, std msg
                  k = jsync0(j) + lag
                  if (jt65_0_available(j,lag) .ge. 1) &
                     ccf4(1:npol) = ccf4(1:npol) + ss(1:npol,k,i)
                  if (jt65_0_available(j,lag) .ge. 2) &
                     ccf4(1:npol) = ccf4(1:npol) + ss(1:npol,k+1,i)
               enddo
               ccf4(1:npol) = ccf4(1:npol) - savg(1:npol,i)*real(nrows)/real(jz)
               row_scale = sqrt(real(JT65_SYNC_ROWS)/real(nrows))
               ccf4(1:npol) = row_scale*ccf4(1:npol)
               ccf = maxval(ccf4)
               ip = maxloc(ccf4)
               ipol = ip(1)
               if (ccf .gt. ccfmax) then
                  ipolbest = ipol
                  lagbest = lag
                  ccfmax = ccf
                  ccf4best = ccf4
                  flip = 1.0
               endif
            endif

            nrows = jt65_1_row_count(lag)
            if (nrows .ge. JT65_MIN_ROWS) then
               ccf = 0.
               ccf4 = 0.
               do j = 1, 63                       !Test for JT65 sync, OOO msg
                  k = jsync1(j) + lag
                  if (jt65_1_available(j,lag) .ge. 1) &
                     ccf4(1:npol) = ccf4(1:npol) + ss(1:npol,k,i)
                  if (jt65_1_available(j,lag) .ge. 2) &
                     ccf4(1:npol) = ccf4(1:npol) + ss(1:npol,k+1,i)
               enddo
               ccf4(1:npol) = ccf4(1:npol) - savg(1:npol,i)*real(nrows)/real(jz)
               row_scale = sqrt(real(JT65_SYNC_ROWS)/real(nrows))
               ccf4(1:npol) = row_scale*ccf4(1:npol)
               ccf = maxval(ccf4)
               ip = maxloc(ccf4)
               ipol = ip(1)
               if (ccf .gt. ccfmax) then
                  ipolbest = ipol
                  lagbest = lag
                  ccfmax = ccf
                  ccf4best = ccf4
                  flip = -1.0
               endif
            endif

         enddo  ! lag

         if (xpol .and. ccfmax .ge. SNR1_THRESHOLD) then
            call polfit(ccf4best, 4, a)
            poldeg = a(3)
         endif
         combine_poldeg=poldeg
        sync(i)%ccfmax = ccfmax
        sync(i)%xdt    = lagbest*tstep - 1.0
        sync(i)%pol    = poldeg
        sync(i)%combine_pol = combine_poldeg
        sync(i)%ipol   = ipolbest
        sync(i)%iflip  = int(flip)
        sync(i)%birdie = .false.
        if (ccfmax/(savg(ipolbest, i)/savg_med(ipolbest)) .lt. 3.0) sync(i)%birdie = .true.
        if (generalized_available) then
           ipol=q65_generalized_sync(i)%ipol
           if (q65_generalized_sync(i)%ccfmax/(savg(ipol,i)/savg_med(ipol)) .lt. 3.0) &
                q65_generalized_sync(i)%birdie=.true.
        endif

        ! --- JT65 wideband probe (new) ---
!        freq_hz = 0.001 * (i - 1) * df3     ! convert FFT bin index to kHz

!        if (abs(freq_hz - 11.305d0) .lt. 0.02d0) then
!           write(*,*) 'WBSYNC BIN: freq=', freq_hz, &
!                      ' ccfmax=', sync(i)%ccfmax, &
!                      ' birdie=', sync(i)%birdie, &
!                      ' iflip=', sync(i)%iflip
!        endif

        enddo   ! i (frequency bin)

      call pctile(sync(ia:ib)%ccfmax, ib - ia + 1, 50, base)
      sync(ia:ib)%ccfmax = sync(ia:ib)%ccfmax/base
      if (generalized_available) then
         call pctile(q65_generalized_sync(ia:ib)%ccfmax,ib-ia+1,50,base)
         if (base > tiny(base)) then
            q65_generalized_sync(ia:ib)%ccfmax=q65_generalized_sync(ia:ib)%ccfmax/base
            sync(ia:ib)=q65_generalized_sync(ia:ib)
         endif
      endif

      ! A local-peak "collapse to single survivor" pass used to run here,
      ! blanking a wide (~450 Hz) same-type swath around whatever bin its
      ! own forward-only search happened to land on. Because that search
      ! and blanking radius were not centered on the true global peak, it
      ! could -- and, for closely-spaced real Q65 signals, did -- wipe out
      ! a much stronger genuine peak in favor of a weaker one found earlier
      ! in the frequency sweep, corrupting the candidate frequency reported
      ! for that signal (confirmed via w3sz debug logging: a real Q65
      ! signal's candidate frequency was off by ~150-200 Hz from its true
      ! peak whenever this pass ran, and exactly correct with it removed).
      ! get_candidates() below already does its own correct, strength-
      ! ordered, same-type-only deduplication (skip a candidate within its
      ! own signal bandwidth of an already-accepted stronger one), which
      ! does not have this failure mode, so this pass is not needed.

!  do i=ia,ib
!     write(15,3015) 0.001*(i-1)*df3+32.0,sync(i)%ccfmax,sync(i)%xdt,  &
!          sync(i)%ipol,sync(i)%iflip,sync(i)%birdie
!3015 format(3f10.3,2i6,L5)
!  enddo

      return
   end subroutine wb_sync

   pure integer function outside_row_count(j1,j2,jz)
      implicit none
      integer, intent(in) :: j1,j2,jz

      outside_row_count = max(0,min(j1,jz)) + max(0,jz-max(1,j2)+1)
   end function outside_row_count

   subroutine init_wideband_sync()
      use npar_ptrs_mod, only: nfft_active
      implicit none
      if (.not. allocated(sync)) allocate (sync(nfft_active))
      if (.not. allocated(q65_generalized_sync)) &
           allocate (q65_generalized_sync(nfft_active))
   end subroutine init_wideband_sync

end module wideband_sync