summaryrefslogtreecommitdiff
path: root/map65/libm65/q65b.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/q65b.F90
parentd83a420f0517d7fc47aeb288a6a0cf4891784fcc (diff)
New upstream version 3.2.0~rc1+dfsgupstream/3.2.0_rc1+dfsg
Diffstat (limited to 'map65/libm65/q65b.F90')
-rw-r--r--map65/libm65/q65b.F90262
1 files changed, 218 insertions, 44 deletions
diff --git a/map65/libm65/q65b.F90 b/map65/libm65/q65b.F90
index 6dba967f..7f28f0f3 100644
--- a/map65/libm65/q65b.F90
+++ b/map65/libm65/q65b.F90
@@ -9,9 +9,31 @@
implicit none
contains
- subroutine q65b(nutc, nqd, nxant, fcenter, nfcal, nfsample, ikhz, mousedf, ntol, xpol, &
+ subroutine write_wav_header(unit, nsamp, fs)
+ integer, intent(in) :: unit, nsamp, fs
+ integer :: datasize, file_size
+
+ datasize = nsamp * 2 ! 16-bit samples
+ file_size = 36 + datasize
+
+ write(unit) 'RIFF'
+ write(unit) file_size
+ write(unit) 'WAVE'
+ write(unit) 'fmt '
+ write(unit) 16 ! PCM header size
+ write(unit) 1 ! PCM format
+ write(unit) 1 ! mono
+ write(unit) fs ! sample rate
+ write(unit) fs*2 ! byte rate
+ write(unit) 2 ! block align
+ write(unit) 16 ! bits per sample
+ write(unit) 'data'
+ write(unit) datasize
+ end subroutine write_wav_header
+
+ subroutine q65b(nutc, nqd, nxant, fcenter, nfcal, nfsample, ikhz, mousedf, ntol, xpol, configured_dphi_deg, &
mycall0, mygrid, hiscall0, hisgrid, mode_q65, f0, fqso, newdat, nagain, &
- max_drift, ndop00, idec)
+ max_drift, ndop00, idec, cursor_fallback)
use iso_c_binding
use q65_decode
@@ -25,6 +47,9 @@
use txpol_mod
use iso_fortran_env, only: real64, int16
use map65_mmdec_mod
+ use npar_ptrs_mod, only: nrate_active, nfft_big_active, nfft_active, t_start, abort_decode, &
+ manualDecodeFlag, ndepth
+ use sec_midn_mod, only: sec_midn
implicit none
@@ -39,6 +64,7 @@
integer, intent(in) :: mousedf
integer, intent(in) :: ntol
logical, intent(in) :: xpol
+ integer, intent(in) :: configured_dphi_deg
character(len=12), intent(in) :: mycall0
character(len=6), intent(in) :: mygrid
character(len=12), intent(in) :: hiscall0
@@ -51,10 +77,14 @@
integer, intent(in) :: max_drift
integer, intent(in) :: ndop00
integer, intent(out) :: idec
+ logical, optional, intent(in) :: cursor_fallback
!==== Local parameters ====================================================
- integer, parameter :: MAXFFT1 = 5376000
- integer, parameter :: MAXFFT2 = 336000
+ ! MAXFFT1/2 are *max* sizes; actual runtime sizes are derived below.
+ ! MAXFFT1 must be = nfft_big_active; MAXFFT2 must be = the
+ ! downsampled-FFT length at the highest supported rate.
+ integer, parameter :: MAXFFT1 = 56*192000
+ integer, parameter :: MAXFFT2 = 336000*2
integer, parameter :: NMAX = 60*12000
real(real64), parameter :: RAD = 57.2957795
@@ -62,12 +92,19 @@
integer(int16) :: iwave(300*12000)
complex :: cx(0:MAXFFT2-1), cy(0:MAXFFT2-1), cz(0:MAXFFT2)
integer :: ipk1(1)
+ ! Small, fixed bin radius for the automatic-candidate ipk search (see
+ ! the note below at "for a wideband candidate"). Deliberately
+ ! independent of the GUI's Ftol (ntol) -- this only needs to absorb
+ ! wb_sync's own low-SNR bin-selection noise, not accommodate arbitrary
+ ! user-configured search widths.
+ integer, parameter :: IPK_LOCAL_BINS = 5
integer :: i, ia, ib, ifreq, ikhz1, ipk, ipol
- integer :: j, ja, jb, k0, mhz, ndf, ndpth, nfft1, nfft2
+ integer :: j, ja, jb, k0, mhz, ndf, nfft1, nfft2
integer :: npol, nq65df, nsubmode, ntxpol, nutc00, nh
integer :: nfa, nfb
- real :: df, df3, f_ipk, f_mouse, fac
- real :: freq1_00, frx, fsked, poldeg, r, snr1
+ integer :: k0_click, mousedf_gate
+ real :: df, df3, dphi, f_ipk, f_mouse, fac
+ real :: combine_poldeg, freq1_00, frx, fsked, poldeg, r, snr1
real(real64) :: freq0, freq1
character(len=12) :: mycall, hiscall
character(len=4) :: grid4
@@ -76,6 +113,9 @@
character(len=2) :: cmode
character(len=256) :: linenew
+ integer :: t_now, t_rate
+ logical :: search_cursor
+
! From q65_decode / wideband_sync / globals:
! real :: xdt0
! integer :: nsnr0, nfreq0, nkhz_center
@@ -88,49 +128,149 @@
data msg00 /' '/
save
- call init_cacb(5376000)
+ idec = -1
+ nsnr0 = -99
+ msg0 = ' '
+ cq0 = ' '
+ xdt0 = 0.0
+ nfreq0 = 0
+ search_cursor = manualDecodeFlag .ne. 0
+ if (present(cursor_fallback)) search_cursor = search_cursor .or. cursor_fallback
+
+ call system_clock(t_now, t_rate)
+ if (real(t_now - t_start)/real(t_rate) > 40.0) then
+ abort_decode = .true.
+ return
+ endif
+
+ ! ca/cb storage is sized at MAXFFT1 in cacb_mod; the *active* big FFT
+ ! length is nfft_big_active, set from C++ via set_runtime_params_().
+ call init_cacb(MAXFFT1)
- call init_wideband_sync(NFFT)
+ ! wideband_sync expects the active symspec FFT length
+ call init_wideband_sync()
+
if (mycall0(1:1) .ne. ' ') mycall = mycall0
if (hiscall0(1:1) .ne. ' ') hiscall = hiscall0
if (hisgrid(1:4) .ne. ' ') grid4 = hisgrid(1:4)
! Find best frequency and ipol from sync_dat, the "orange sync curve".
- df3 = 96000.0/32768.0
+ ! df3 is the bin width of the symspec FFT (savg), based on the
+ ! active runtime rate and FFT length.
+ df3 = real(nrate_active)/real(nfft_active)
ifreq = nint((1000.0*f0)/df3)
- ia = nint(ifreq - ntol/df3)
- ib = nint(ifreq + ntol/df3)
- if (ia >= 1 .and. ia <= 32768 .and. ib >= 1 .and. ib <= 32768) then !if added qt6
- ipk1 = maxloc(sync(ia:ib)%ccfmax)
+ ! Candidate frequencies need only local bin refinement. Cursor
+ ! targets, including the no-candidate fallback, need the selected Ftol.
+ ! Bin-quantized weak-signal candidates can miss the true sync peak;
+ ! retain the local five-bin search rather than testing one exact bin.
+ if (search_cursor) then
+ ia = nint(ifreq - ntol/df3)
+ ib = nint(ifreq + ntol/df3)
+ if (ia >= 1 .and. ia <= nfft_active .and. ib >= 1 .and. ib <= nfft_active) then
+ ipk1 = maxloc(sync(ia:ib)%ccfmax)
+ else
+ go to 901
+ endif
+ ipk = ia + ipk1(1) - 1
else
- go to 901
+ ia = max(1, ifreq - IPK_LOCAL_BINS)
+ ib = min(nfft_active, ifreq + IPK_LOCAL_BINS)
+ if (ifreq >= 1 .and. ifreq <= nfft_active .and. ib >= ia) then
+ ipk1 = maxloc(sync(ia:ib)%ccfmax)
+ ipk = ia + ipk1(1) - 1
+ else
+ go to 901
+ endif
endif
+ snr1 = sync(ipk)%ccfmax
- ipk = ia + ipk1(1) - 1
- if (ldecoded(ipk)) go to 900
+ ! TEMP diagnostic 2026-09-09 for the two-close-Q65-signals investigation.
+ call dbg('q65b: ipk check at t=' // rtoa(sec_midn()) // &
+ ' nqd=' // itoa(nqd) // ' f0=' // rtoa(real(f0)) // ' ntol=' // itoa(ntol) // &
+ ' ifreq=' // itoa(ifreq) // ' ipk=' // itoa(ipk) // &
+ ' ldecoded(ipk)=' // itoa(merge(1,0,ldecoded(ipk))) // &
+ ' manualDecodeFlag=' // itoa(manualDecodeFlag) // ' nagain=' // itoa(nagain))
+ ! ldecoded(ipk) exists to keep the wideband scan from repeatedly
+ ! re-decoding a bin it already got a result from. A manual click is a
+ ! deliberate, targeted request to decode at this exact bin, so it must
+ ! not be skipped just because an earlier wideband pass happened to
+ ! visit the same bin first.
+ if (ldecoded(ipk) .and. manualDecodeFlag .eq. 0) then
+ call dbg('q65b: SKIPPED (ldecoded already set) at t=' // rtoa(sec_midn()) // &
+ ' ipk=' // itoa(ipk))
+ go to 900
+ endif
snr1 = sync(ipk)%ccfmax
! ipol was never declared and its value is never used
ipol = 1
if (xpol) ipol = sync(ipk)%ipol
- nfft1 = MAXFFT1
- nfft2 = MAXFFT2
- df = 96000.0/NFFT1
+ ! Runtime big-FFT length: nfft_big_active is 56 * nrate_active,
+ ! set from C++ at startup. Clamp to MAXFFT1 for safety.
+ nfft1 = min(nfft_big_active, MAXFFT1)
+
+ ! Use a fixed small-FFT length that matches the legacy 96 kHz
+ ! geometry and stays within MAXFFT2 at all supported rates.
+ nfft2 = min(336000, MAXFFT2)
+
+ ! Preserve the legacy 95238 special case; if you still use it,
+ ! override the derived sizes with the historical ones.
if (nfsample .eq. 95238) then
- nfft1 = 5120000
- nfft2 = 322560
- df = 96000.0/nfft1
+ nfft1 = min(5120000, MAXFFT1)
+ nfft2 = min(322560, MAXFFT2)
endif
+
+ ! Bin width of the big FFT: use the active runtime rate instead
+ ! of a hardcoded 96000. The 95238 special case is preserved.
+ df = real(nrate_active)/real(nfft1)
+ if (nfsample .eq. 95238) df = 96000.0/real(nfft1)
+
nh = nfft2/2
- f_mouse = 1000.0*(fqso + 48.0) + mousedf
+ f_mouse = 1000.0*(fqso + real(nrate_active/2000.0)) + mousedf
f_ipk = ipk*df3
- k0 = nint((ipk*df3 - 1000.0)/df)
- if (nagain .eq. 1) k0 = nint((f_mouse - 1000.0)/df)
-
- if (k0 .lt. nh .or. k0 .gt. MAXFFT1 - nfft2 + 1) go to 900
- if (snr1 .lt. 1.5) go to 900 !### Threshold needs work? ###
+ ! k0 used to come from f_ipk (ipk*df3), i.e. re-derived from the coarse,
+ ! JT65-oriented "orange sync curve" bin (df3 ~ 2.9 Hz resolution) rather
+ ! than from the candidate's own already-refined frequency. For a
+ ! wideband Q65 candidate, f0 (set by the caller from cand()%f, which
+ ! wb_sync already computed with sub-bin precision) is a strictly better
+ ! estimate of the true signal frequency than re-quantizing through ipk
+ ! -- and the difference matters here because the actual decode below
+ ! only searches a tight +/-10 Hz window (nfa=990..1010) around k0.
+ ! Use f0 directly, same as the manual-click/nagain path already does
+ ! via f_mouse below.
+ k0 = nint((1000.0*f0 - 1000.0)/df)
+ ! ipk (and therefore f_ipk) comes from maxloc'ing the JT65-oriented
+ ! "orange sync curve" over the whole +/-ntol window -- fine for a
+ ! wideband scan, but if a strong JT65 signal happens to sit anywhere
+ ! in that window it will always win, even over an equal-strength Q65
+ ! signal, because that curve doesn't score Q65 signals the same way.
+ ! A manual click already tells us exactly where to look, so center
+ ! the analysis window on the actual clicked frequency (f_mouse, which
+ ! properly includes mousedf) instead of trusting that curve.
+ ! nagain=1 (Decode button / Find Delta Phi repeat) runs the same
+ ! per-candidate loop over real, precise wb_sync candidates that
+ ! automatic decoding does -- f0 is not an imprecise mousefqso click
+ ! there, so forcing k0 to f_mouse would make every candidate in that
+ ! loop decode the same narrow window near the cursor instead of its
+ ! own frequency. The one nagain=1 case where f0 genuinely comes from
+ ! mousefqso (the "no candidate found" fallback in map65a.f90) already
+ ! sets f0 = mousefqso + mousedf directly, so the default k0 below
+ ! lands on the same point anyway.
+ if (manualDecodeFlag .ne. 0) k0 = nint((f_mouse - 1000.0)/df)
+
+ if (k0 .lt. nh .or. k0 .gt. nfft1 - nfft2 + 1) then
+ if (manualDecodeFlag .ne. 0) &
+ call dbg('q65b manual: NOT ATTEMPTED, k0=' // itoa(k0) // ' outside ' // &
+ itoa(nh) // '..' // itoa(nfft1 - nfft2 + 1))
+ go to 900
+ endif
+ ! Likewise, snr1 is the sync-curve strength at ipk, which for a manual
+ ! click may be the wrong bin entirely (see above) -- don't let it gate
+ ! a manual decode attempt. Let q65_decode's own Q65-native search
+ ! (below) determine success or failure instead.
+ if (snr1 .lt. 1.5 .and. manualDecodeFlag .eq. 0) go to 900 !### Threshold needs work? ###
fac = 1.0/nfft2
cx(0:nfft2 - 1) = ca(k0:k0 + nfft2 - 1)
@@ -138,6 +278,11 @@
if (xpol) then
cy(0:nfft2 - 1) = cb(k0:k0 + nfft2 - 1)
cy = fac*cy
+ if (configured_dphi_deg .ne. 0) then
+ dphi = configured_dphi_deg/RAD
+ ! The sync weights describe the phase-corrected basis from symspec.
+ cy(0:nfft2 - 1) = cmplx(cos(dphi), sin(dphi))*cy(0:nfft2 - 1)
+ endif
endif
! Here cx and cy (if xpol) are frequency-domain data around the selected
@@ -153,8 +298,12 @@
poldeg = 0.
if (xpol) then
+ ! NB: still uses the (possibly wrong-bin, see k0 above) ipk for
+ ! manual clicks in xpol mode -- not exercised by current testing,
+ ! but worth revisiting if xpol manual decode misbehaves similarly.
poldeg = sync(ipk)%pol
- cz(0:MAXFFT2 - 1) = cos(poldeg/RAD)*cx + sin(poldeg/RAD)*cy
+ combine_poldeg = sync(ipk)%combine_pol
+ cz(0:MAXFFT2 - 1) = cos(combine_poldeg/RAD)*cx + sin(combine_poldeg/RAD)*cy
else
cz(0:MAXFFT2 - 1) = cx
endif
@@ -177,34 +326,49 @@
iwave(2*i + 2) = int(max(-32768, min(32767, nint(real(cz(j))))), kind=2)
iwave(2*i + 1) = int(max(-32768, min(32767, nint(aimag(cz(j))))), kind=2)
enddo
- iwave(2*nfft2 + 1:) = 0
-! open(30,file='000000_0001.wav',status='unknown',access='stream')
-! h=default_header(12000,NMAX)
-! write(30) h,iwave
-! close(30)
+ iwave(2*nfft2 + 1:) = 0
nsubmode = mode_q65 - 1
nfa = 990 !Tight limits around ipk for the wideband decode
nfb = 1010
- if (nagain .eq. 1) then !For nagain=1, use limits of +/- ntol
+ if (search_cursor) then
nfa = max(100, 1000 - ntol)
nfb = min(2500, 1000 + ntol)
endif
- nsnr0 = -99 !Default snr for no decode
- ndpth = 3
! NB: Frequency of ipk is now shifted to 1000 Hz.
+
+ ! ndepth comes from npar_ptrs_mod, set from the GUI's Decode Depth
+ ! menu (see MainWindow::decode() -> setNdepth()). This used to be a
+ ! local variable "ndpth" hardwired to 3 here, which was never
+ ! connected to the GUI at all -- Q65 always ran at depth 3
+ ! regardless of what the user selected.
call map65_mmdec(nutc, iwave, nqd, 60, nsubmode, nfa, nfb, 1000, ntol, &
- newdat, nagain, max_drift, ndpth, mycall, hiscall0, hisgrid)
+ newdat, nagain, max_drift, ndepth, mycall, hiscall0, hisgrid)
MHz = fcenter
freq0 = MHz + 0.001d0*ikhz
+ call dbg('q65b: map65_mmdec result at t=' // rtoa(sec_midn()) // &
+ ' nqd=' // itoa(nqd) // ' f0=' // rtoa(real(f0)) // ' ipk=' // itoa(ipk) // &
+ ' nsnr0=' // itoa(nsnr0))
+
if (nsnr0 .gt. -99) then
ldecoded(ipk) = .true.
- nq65df = nint(1000*(0.001*k0*df + nkhz_center - 48.0 + 1.000 - 1.27046 - ikhz)) - nfcal
+ nq65df = nint(1000*( 0.001*k0*df + nkhz_center - real(nrate_active)/2000.0 + 1.000 - 1.27046 - ikhz )) - nfcal
nq65df = nq65df + nfreq0 - 1000
+ ! Gate comparison target, expressed in the same ikhz-relative frame
+ ! as nq65df above -- computed from f0 (the unambiguous target
+ ! frequency for this call, for both a manual click and a wideband
+ ! candidate) via the same k0/nq65df formula, rather than from raw
+ ! mousedf, which is relative to mousefqso and only matches ikhz's
+ ! frame when the target happens to already be sub-kHz. mousedf
+ ! itself is left untouched -- f_mouse/k0 above still need it in its
+ ! original, unmodified form.
+ k0_click = nint((1000.0*f0 - 1000.0)/df)
+ mousedf_gate = nint(1000*( 0.001*k0_click*df + nkhz_center - real(nrate_active)/2000.0 + &
+ 1.000 - 1.27046 - ikhz )) - nfcal
npol = nint(poldeg)
if (nxant .ne. 0) then
npol = npol - 45
@@ -216,11 +380,18 @@
if (ndf .gt. 500) ikhz1 = ikhz + (nq65df + 500)/1000
if (ndf .lt. -500) ikhz1 = ikhz + (nq65df - 500)/1000
ndf = nq65df - 1000*(ikhz1 - ikhz)
- if (nqd .eq. 1 .and. abs(nq65df - mousedf) .lt. ntol) then
+
+ if (manualDecodeFlag .ne. 0) &
+ call dbg('q65b manual: decoded nq65df=' // itoa(nq65df) // ' mousedf_gate=' // &
+ itoa(mousedf_gate) // ' ntol=' // itoa(ntol) // ' nqd=' // itoa(nqd) // &
+ ' display gate ' // merge('PASSED', 'FAILED', &
+ nqd .eq. 1 .and. abs(nq65df - mousedf_gate) .lt. ntol))
+
+ if (nqd .eq. 1 .and. abs(nq65df - mousedf_gate) .lt. ntol) then
+
write (linenew, '("!",I3.3,I5,I4,I6.4,F5.1,I5," : ",A28,A3,I4,1X,A1)') &
ikhz1, ndf, npol, nutc, xdt0, nsnr0, msg0(1:28), cq0, ntxpol, cp
call write_stdout(trim(linenew)//new_line('a'))
-
endif
! Write to lu 26, for Messages and Band Map windows
@@ -242,7 +413,7 @@
nutc00 = nutc
msg00 = msg0(1:28)
freq1_00 = freq1
- frx = 0.001*k0*df + nkhz_center - 48.0 + 1.0 - 0.001*nfcal
+ frx = 0.001*k0*df + nkhz_center - real(nrate_active/2000.0) + 1.0 - 0.001*nfcal
fsked = frx - 0.001*ndop00/2.0 - 1.5
write (12, 1120) nutc, fsked, xdt0, nsnr0, trim(msg0)
1120 format(i4.4, f9.3, f7.2, i5, 2x, a, i6)
@@ -252,7 +423,10 @@
900 close (13)
close (17)
idec = -1
- read (cq0(2:2), *) idec
+ ! cq0 is blank unless this call's own decode attempt set it (see the
+ ! reset at subroutine entry); only parse it when there's really a
+ ! digit there, otherwise there's nothing for READ to find.
+ if (cq0(2:2) .ne. ' ') read (cq0(2:2), *) idec
return
901 close (13)
close (17)