summaryrefslogtreecommitdiff
path: root/map65/libm65/ccf65.f90
diff options
context:
space:
mode:
Diffstat (limited to 'map65/libm65/ccf65.f90')
-rw-r--r--map65/libm65/ccf65.f90159
1 files changed, 108 insertions, 51 deletions
diff --git a/map65/libm65/ccf65.f90 b/map65/libm65/ccf65.f90
index 592fe95e..2aa5228a 100644
--- a/map65/libm65/ccf65.f90
+++ b/map65/libm65/ccf65.f90
@@ -1,23 +1,55 @@
-subroutine ccf65(ss,nhsym,ssmax,sync1,ipol1,jpz,dt1,flipk, &
- syncshort,snr2,ipol2,dt2)
-
- parameter (NFFT=512,NH=NFFT/2)
- real ss(4,322) !Input: half-symbol powers, 4 pol'ns
- real s(NFFT) !CCF = ss*pr
- complex cs(0:NH) !Complex FT of s
- real s2(NFFT) !CCF = ss*pr2
- complex cs2(0:NH) !Complex FT of s2
- real pr(NFFT) !JT65 pseudo-random sync pattern
- complex cpr(0:NH) !Complex FT of pr
- real pr2(NFFT) !JT65 shorthand pattern
- complex cpr2(0:NH) !Complex FT of pr2
- real tmp1(322)
+!------------------------------------------------------------------------------
+! 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, 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
+
+ ! Modern interface:
+ ! ss_plane(4,322) is passed as a proper 2-D slice (ss(:,:,i))
+ 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,MAX_NHSYM)
+
+ ! time-domain
+ real :: s(NFFT) ! CCF = ss*pr
+ real :: s2(NFFT) ! CCF = ss*pr2
+ real :: pr(NFFT) ! JT65 pseudo-random sync pattern
+ real :: pr2(NFFT) ! JT65 shorthand pattern
+
+ ! frequency-domain: half-spectrum, as in legacy
+ complex :: cs(0:NH) ! Complex FT of s
+ complex :: cs2(0:NH) ! Complex FT of s2
+ complex :: cpr(0:NH) ! Complex FT of pr
+ complex :: cpr2(0:NH) ! Complex FT of pr2
+
+ real tmp1(MAX_NHSYM)
real ccf(-11:54,4)
- logical first
integer npr(126)
- data first/.true./
- equivalence (s,cs),(pr,cpr),(s2,cs2),(pr2,cpr2)
- save
+ 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/ &
@@ -29,34 +61,48 @@ subroutine ccf65(ss,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/
- if(first) then
-! Initialize pr, pr2; compute cpr, cpr2.
- 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)
-! Not sure why, but it works significantly better without the following line:
-! pr(j-1)=pr(j)
- enddo
- call four2a(cpr,NFFT,1,-1,0)
- call four2a(cpr2,NFFT,1,-1,0)
- first=.false.
- endif
- syncshort=0.
- snr2=0.
+ sync1 = -4.0
+ syncshort = -4.0
+ snr2 = 0.01
+ dt1 = 0.0
+ dt2 = 0.0
+ flipk = 1.0
+ ipol1 = 1
+ ipol2 = 1
+
+ if (nhsym < 2 .or. nhsym > MAX_NHSYM) return
+ if (jpz < 1 .or. jpz > 4) return
+ if (.not. ieee_is_finite(ssmax)) return
+
+ 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)
+
+ 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))
@@ -64,13 +110,19 @@ subroutine ccf65(ss,nhsym,ssmax,sync1,ipol1,jpz,dt1,flipk, &
call pctile(s,nhsym-1,50,base)
s(1:nhsym-1)=s(1:nhsym-1)-base
s(nhsym:NFFT)=0.
- call four2a(cs,NFFT,1,-1,0) !Real-to-complex FFT
+
+ ! === Forward FFT: real ? packed half-spectrum ===
+ call r2c_legacy(s, cs)
+
+ ! === Multiply by sync patterns in frequency domain ===
do i=0,NH
- cs2(i)=cs(i)*conjg(cpr2(i)) !Mult by complex FFT of pr2
- cs(i)=cs(i)*conjg(cpr(i)) !Mult by complex FFT of pr
+ cs2(i) = cs(i) * conjg(cpr2(i))
+ cs(i) = cs(i) * conjg(cpr(i))
enddo
- call four2a(cs,NFFT,1,1,-1) !Complex-to-real inv-FFT
- call four2a(cs2,NFFT,1,1,-1) !Complex-to-real inv-FFT
+
+ ! === Inverse FFT: packed half-spectrum ? real ===
+ call c2r_legacy(cs, s)
+ call c2r_legacy(cs2, s2)
do lag=-11,54 !Check for best JT65 sync
j=lag
@@ -85,9 +137,6 @@ subroutine ccf65(ss,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
@@ -99,6 +148,8 @@ subroutine ccf65(ss,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
@@ -110,8 +161,10 @@ subroutine ccf65(ss,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.
@@ -121,8 +174,12 @@ subroutine ccf65(ss,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 (.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
+end module ccf65_legacy_mod