summaryrefslogtreecommitdiff
path: root/map65/libm65/wideband_sync.f90
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2026-09-26 23:24:03 +0200
committerChristoph Berg <myon@debian.org>2026-09-26 23:24:03 +0200
commit059831534c7e884f0a607ed86b582fd00d821731 (patch)
tree76bcb67a78f8aa2654fd34c2ad801a62136b0170 /map65/libm65/wideband_sync.f90
parentd83a420f0517d7fc47aeb288a6a0cf4891784fcc (diff)
New upstream version 3.2.0~rc1+dfsgupstream/3.2.0_rc1+dfsg
Diffstat (limited to 'map65/libm65/wideband_sync.f90')
-rw-r--r--map65/libm65/wideband_sync.f90325
1 files changed, 221 insertions, 104 deletions
diff --git a/map65/libm65/wideband_sync.f90 b/map65/libm65/wideband_sync.f90
index a39b42f2..a8ae8a68 100644
--- a/map65/libm65/wideband_sync.f90
+++ b/map65/libm65/wideband_sync.f90
@@ -5,7 +5,7 @@ module wideband_sync
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.0 s
+ 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
@@ -15,30 +15,32 @@ module wideband_sync
real :: ccfmax
real :: xdt
real :: pol
+ real :: combine_pol
integer :: ipol
integer :: iflip
logical :: birdie
end type sync_dat
- integer, parameter :: NFFT = 32768
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
+ 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)
- real, intent(in) :: savg(4,NFFT)
+ 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
@@ -47,9 +49,11 @@ contains
! Search symbol spectra ss() over frequency range nfa to nfb (in kHz) for
-! JT65 and Q65 sync patterns. 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 the structure array cand().
+! 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)
@@ -60,15 +64,16 @@ contains
integer, allocatable :: indx(:)
logical skip
- call wb_sync(ss, savg, xpol, jz, nfa, nfb) !Output to sync() array
+ 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 = 96000.0/NFFT
+ df3 = real(nrate_active)/real(nfft_active)
ia = nint(1000*nfa/df3) + 1
ib = nint(1000*nfb/df3) + 1
- if (ia .lt. 1) ia = 1
- if (ib .gt. NFFT - 1) ib = NFFT - 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
@@ -83,7 +88,7 @@ contains
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
@@ -97,9 +102,10 @@ contains
if (sync(n)%birdie) cycle
! Test for signal outside of TxT range and set bw for this signal type
- j1 = int((sync(n)%xdt + 1.0)/tstep - 1.0)
- j2 = int((sync(n)%xdt + 52.0)/tstep + 1.0)
+ 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
@@ -108,13 +114,16 @@ contains
do j = j2, jz
pavg = pavg + ss(ipol, j, n - 20:n + 20)
enddo
- jsum = j1 + (jz - j2 + 1)
- pmax = maxval(pavg(-2:2)) !### Why not just pavg(0) ?
- base = (sum(pavg) - pmax)/jsum
- pmax = pmax/base
- if (pmax .gt. 5.0) cycle
+ 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
@@ -128,40 +137,52 @@ contains
cand(k)%pol = sync(n)%pol
cand(k)%ipol = sync(n)%ipol
cand(k)%iflip = nint(flip)
- cand(k)%indx = n
+ 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
+ ncand = k
return
end subroutine get_candidates
- subroutine wb_sync(ss, savg, xpol, jz, nfa, nfb)
+ 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)
- real, intent(in) :: savg(4,NFFT)
- logical, intent(in) :: xpol
+ 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, bw, ccf, ccfmax, df3, fac, flip, poldeg, spk, syncmin, tstep
- integer i, i0, ia, ib, ipolbest, j, ja, jb, k, lag, lagbest
- integer nbw, nguard, npol, ipol
- logical first
+ 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 ---
@@ -195,17 +216,48 @@ contains
first = .false.
endif
- df3 = 96000.0/NFFT
+ 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 - 1) ib = NFFT - 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)
@@ -217,77 +269,131 @@ contains
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
- ccf = 0.
- ccf4 = 0.
- do j = 1, 22 !Test for Q65 sync
- k = isync(j) + lag
- ccf4(1:npol) = ccf4(1:npol) + ss(1:npol, k, i + 1) + &
- ss(1:npol, k + 1, i + 1) + ss(1:npol, k + 2, i + 1)
- enddo
- ccf4(1:npol) = ccf4(1:npol) - savg(1:npol, i + 1)*3*22/float(jz)
- ccf = maxval(ccf4)
- ip = maxloc(ccf4)
- ipol = ip(1)
- if (ccf .gt. ccfmax) then
- ipolbest = ipol
- lagbest = lag
- ccfmax = ccf
- ccf4best = ccf4
- flip = 0.
+ 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
- ccf = 0.
- ccf4 = 0.
- do j = 1, 63 !Test for JT65 sync, std msg
- k = jsync0(j) + lag
- ccf4(1:npol) = ccf4(1:npol) + ss(1:npol, k, i + 1) + ss(1:npol, k + 1, i + 1)
- enddo
- ccf4(1:npol) = ccf4(1:npol) - savg(1:npol, i + 1)*2*63/float(jz)
- 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
+ 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
- ccf = 0.
- ccf4 = 0.
- do j = 1, 63 !Test for JT65 sync, OOO msg
- k = jsync1(j) + lag
- ccf4(1:npol) = ccf4(1:npol) + ss(1:npol, k, i + 1) + ss(1:npol, k + 1, i + 1)
- enddo
- ccf4(1:npol) = ccf4(1:npol) - savg(1:npol, i + 1)*2*63/float(jz)
- 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
+ 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
- poldeg = 0.
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
@@ -303,26 +409,28 @@ contains
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
- bw = 65*4*1.66666667 !Q65-60C bandwidth
- nbw = int(bw/df3 + 1) !Number of bins to blank
- syncmin = 2.0
- nguard = 10
- do i = ia, ib
- if (sync(i)%ccfmax .lt. syncmin) cycle
- if ((i .lt. 1) .or. (i .gt. (NFFT - nbw))) cycle !w3sz debug
- spk = maxval(sync(i:i + nbw)%ccfmax)
- ip = maxloc(sync(i:i + nbw)%ccfmax)
- i0 = ip(1) + i - 1
- ja = min(i, i0 - nguard)
- jb = i0 + nbw + nguard
- if (ja .lt. 1) cycle !ja = 1 !w3sz debug
- if (jb .lt. 1) cycle !jb = 1 !w3sz debug
- if (ja .gt. NFFT) cycle !ja = NFFT !w3sz debug
- if (jb .gt. NFFT) cycle !jb = NFFT !w3sz debug
- sync(ja:jb)%ccfmax = 0.
- sync(i0)%ccfmax = spk
- enddo
+ ! 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, &
@@ -333,10 +441,19 @@ contains
return
end subroutine wb_sync
- subroutine init_wideband_sync(n)
+ 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
- integer, intent(in) :: n
- if (.not. allocated(sync)) allocate (sync(n))
+ 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