diff options
Diffstat (limited to 'map65/libm65/ccf65.f90')
| -rw-r--r-- | map65/libm65/ccf65.f90 | 123 |
1 files changed, 70 insertions, 53 deletions
diff --git a/map65/libm65/ccf65.f90 b/map65/libm65/ccf65.f90 index 27a41f2f..2aa5228a 100644 --- a/map65/libm65/ccf65.f90 +++ b/map65/libm65/ccf65.f90 @@ -1,29 +1,37 @@ !------------------------------------------------------------------------------ -! NOTE: This module intentionally preserves the original legacy WSJT/MAP65 -! ccf65 implementation. Modernizing this routine changes the numerical -! behavior of the JT65 correlation (FFT layout, half-spectrum handling, -! baseline statistics, and sync metrics), which in turn produces a -! different false-positive/false-negative profile. Extensive testing -! shows that the legacy algorithm yields the correct and expected -! decode behavior, so it is retained here without modification. +! NOTE: This routine preserves the legacy JT65 correlation core. Its boundary +! validation and output defaults make degenerate inputs deterministic +! without changing the normal signal-processing path. !------------------------------------------------------------------------------ module ccf65_legacy_mod + implicit none contains subroutine ccf65(ss_plane, nhsym, ssmax, sync1, ipol1, jpz, dt1, flipk, & syncshort, snr2, ipol2, dt2) - use four2a_legacy_wrap_mod, only: r2c_legacy, c2r_legacy + use four2a_legacy_wrap_mod, only: r2c_legacy, c2r_legacy, JT65_NFFT, JT65_NH use pctile_mod, only: pctile + use, intrinsic :: ieee_arithmetic, only: ieee_is_finite + + implicit none + + integer, parameter :: NFFT = JT65_NFFT + integer, parameter :: NH = JT65_NH + integer, parameter :: MAX_NHSYM = 322 + + integer, intent(in) :: nhsym, jpz + real, intent(in) :: ssmax + real, intent(out) :: sync1, dt1, flipk, syncshort, snr2, dt2 + integer, intent(out) :: ipol1, ipol2 - parameter (NFFT=512,NH=NFFT/2) ! Modern interface: ! ss_plane(4,322) is passed as a proper 2-D slice (ss(:,:,i)) - real, intent(in) :: ss_plane(4,322) + real, intent(in) :: ss_plane(4,MAX_NHSYM) ! Legacy expects: real ss(4,322) passed by reference from ss(1,1,i) - real :: ss(4,322) + real :: ss(4,MAX_NHSYM) ! time-domain real :: s(NFFT) ! CCF = ss*pr @@ -37,12 +45,11 @@ subroutine ccf65(ss_plane, nhsym, ssmax, sync1, ipol1, jpz, dt1, flipk, & complex :: cpr(0:NH) ! Complex FT of pr complex :: cpr2(0:NH) ! Complex FT of pr2 - real tmp1(322) + real tmp1(MAX_NHSYM) real ccf(-11:54,4) - logical first integer npr(126) - data first/.true./ - save s,s2,pr,pr2,cs,cs2,cpr,cpr2 + integer :: i, j, k, ip, lag, lagpk, lagpk2, npol + real :: fac, base, ccfbest, ccfbest2, ccf2, sumccf, sq, rms ! The JT65 pseudo-random sync pattern: data npr/ & @@ -54,40 +61,48 @@ subroutine ccf65(ss_plane, nhsym, ssmax, sync1, ipol1, jpz, dt1, flipk, & 0,1,0,1,0,0,1,1,0,0,1,0,0,1,0,0,0,0,1,1, & 1,1,1,1,1,1/ - logical :: debug - debug = .false. ! (nhsym .gt. 300) - - ss = ss_plane - - if(first) then - fac=1.0/NFFT - do i=1,NFFT - pr(i)=0. - pr2(i)=0. - k=2*mod((i-1)/8,2)-1 - if(i.le.NH) pr2(i)=fac*k - enddo - do i=1,126 - j=2*i - pr(j)=fac*(2*npr(i)-1) -! pr(j-1)=pr(j) - enddo + sync1 = -4.0 + syncshort = -4.0 + snr2 = 0.01 + dt1 = 0.0 + dt2 = 0.0 + flipk = 1.0 + ipol1 = 1 + ipol2 = 1 - call r2c_legacy(pr, cpr, NFFT) - call r2c_legacy(pr2, cpr2, NFFT) + if (nhsym < 2 .or. nhsym > MAX_NHSYM) return + if (jpz < 1 .or. jpz > 4) return + if (.not. ieee_is_finite(ssmax)) return - first=.false. - endif + npol = jpz + ss = ss_plane + if (.not. all(ieee_is_finite(ss(1:npol,1:nhsym)))) return + + fac = 1.0/NFFT + do i=1,NFFT + pr(i)=0. + pr2(i)=0. + k=2*mod((i-1)/8,2)-1 + if(i.le.NH) pr2(i)=fac*k + enddo + do i=1,126 + j=2*i + pr(j)=fac*(2*npr(i)-1) + enddo + + call r2c_legacy(pr, cpr) + call r2c_legacy(pr2, cpr2) - syncshort=0. - snr2=0. + ccf = 0.0 ! Look for JT65 sync pattern and shorthand square-wave pattern. ccfbest=0. ccfbest2=0. ipol1=1 ipol2=1 - do ip=1,jpz !Do jpz polarizations + lagpk=0 + lagpk2=0 + do ip=1,npol !Do npol polarizations do i=1,nhsym-1 ! s(i)=ss(ip,i)+ss(ip,i+1) s(i)=min(ssmax,ss(ip,i)+ss(ip,i+1)) @@ -97,17 +112,17 @@ subroutine ccf65(ss_plane, nhsym, ssmax, sync1, ipol1, jpz, dt1, flipk, & s(nhsym:NFFT)=0. ! === Forward FFT: real ? packed half-spectrum === - call r2c_legacy(s, cs, NFFT) + call r2c_legacy(s, cs) ! === Multiply by sync patterns in frequency domain === do i=0,NH - cs2(i) = cs(i)*conjg(cpr2(i)) - cs(i) = cs(i)*conjg(cpr(i)) - enddo + cs2(i) = cs(i) * conjg(cpr2(i)) + cs(i) = cs(i) * conjg(cpr(i)) + enddo ! === Inverse FFT: packed half-spectrum ? real === - call c2r_legacy(cs, s, NFFT) - call c2r_legacy(cs2, s2, NFFT) + call c2r_legacy(cs, s) + call c2r_legacy(cs2, s2) do lag=-11,54 !Check for best JT65 sync j=lag @@ -122,9 +137,6 @@ subroutine ccf65(ss_plane, nhsym, ssmax, sync1, ipol1, jpz, dt1, flipk, & endif enddo -!### Not sure why this is ever true??? - if(sum(ccf).eq.0.0) return -!### do lag=-11,54 !Check for best shorthand ccf2=s2(lag+28) if(ccf2.gt.ccfbest2) then @@ -136,6 +148,8 @@ subroutine ccf65(ss_plane, nhsym, ssmax, sync1, ipol1, jpz, dt1, flipk, & enddo + if (.not. ieee_is_finite(ccfbest) .or. ccfbest <= 0.0) return + ! Find rms level on baseline of "ccfblue", for normalization. sumccf=0. do lag=-11,54 @@ -147,8 +161,10 @@ subroutine ccf65(ss_plane, nhsym, ssmax, sync1, ipol1, jpz, dt1, flipk, & if(abs(lag-lagpk).gt.1) sq=sq + (ccf(lag,ipol1)-base)**2 enddo rms=sqrt(sq/49.0) - sync1=-4.0 - if(rms.gt.0.0) sync1=ccfbest/rms - 4.0 + if (ieee_is_finite(rms) .and. rms > 0.0) then + sync1=ccfbest/rms - 4.0 + syncshort=0.5*ccfbest2/rms - 4.0 + endif dt1=lagpk*(2048.0/11025.0) - 2.5 ! Find base level for normalizing snr2. @@ -158,10 +174,11 @@ subroutine ccf65(ss_plane, nhsym, ssmax, sync1, ipol1, jpz, dt1, flipk, & call pctile(tmp1,nhsym,40,base) snr2=0.01 if(base.gt.0.0) snr2=0.398107*ccfbest2/base !### empirical - syncshort=0.5*ccfbest2/rms - 4.0 !### better normalizer than rms? dt2=2.5 + lagpk2*(2048.0/11025.0) - if (debug) write(logunit,*) 'QT?', 'sync1=', sync1, ' lagpk=', lagpk, ' dt1=', dt1,' snr2=',snr2 + if (.not. ieee_is_finite(sync1)) sync1 = -4.0 + if (.not. ieee_is_finite(syncshort)) syncshort = -4.0 + if (.not. ieee_is_finite(snr2)) snr2 = 0.01 return end subroutine ccf65 |
