summaryrefslogtreecommitdiff
path: root/lib/ft4
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ft4')
-rw-r--r--lib/ft4/clockit.f90111
-rw-r--r--lib/ft4/ft4_downsample.f9051
-rw-r--r--lib/ft4/ft4_params.f9016
-rw-r--r--lib/ft4/ft4b.f90489
-rw-r--r--lib/ft4/ft4d.f9083
-rw-r--r--lib/ft4/ft4sim.f90152
-rw-r--r--lib/ft4/ft4sim_mult.f90105
-rw-r--r--lib/ft4/gen_ft4wave.f9066
-rw-r--r--lib/ft4/genft4.f9090
-rw-r--r--lib/ft4/getcandidates4.f9086
-rw-r--r--lib/ft4/messages.txt162
-rw-r--r--lib/ft4/subtractft4.f9066
-rw-r--r--lib/ft4/sync4d.f9077
-rw-r--r--lib/ft4/syncft4.f90145
14 files changed, 1699 insertions, 0 deletions
diff --git a/lib/ft4/clockit.f90 b/lib/ft4/clockit.f90
new file mode 100644
index 00000000..eb3e7d8b
--- /dev/null
+++ b/lib/ft4/clockit.f90
@@ -0,0 +1,111 @@
+subroutine clockit(dname,k)
+
+! Times procedure number n between a call with k=0 (tstart) and with
+! k=1 (tstop). Accumulates sums of these times in array ut (user time).
+! Also traces all calls (for debugging purposes) if limtrace.gt.0
+
+ character*8 dname,name(50),space,ename
+ character*16 sname
+ character*512 data_dir,fname
+ logical first,on(50)
+ real ut(50),ut0(50),dut(50),tt(2)
+ integer ncall(50),nlevel(50),nparent(50)
+ integer onlevel(0:10)
+ data first/.true./,eps/0.000001/,ntrace/0/
+ data level/0/,nmax/0/,space/' '/
+ data limtrace/0/,lu/29/,ntimer/1/
+! data limtrace/1000000/,lu/29/,ntimer/1/
+ save
+
+ if(ntimer.eq.0) return
+ if(lu.lt.1) lu=6
+ if(k.gt.1) go to 40 !Check for "all done" (k>1)
+ onlevel(0)=0
+
+ do n=1,nmax !Check for existing name
+ if(name(n).eq.dname) go to 20
+ enddo
+
+ nmax=nmax+1 !This is a new one
+ n=nmax
+ ncall(n)=0
+ on(n)=.false.
+ ut(n)=eps
+ name(n)=dname
+
+20 if(k.eq.0) then !Get start times (k=0)
+ if(on(n)) print*,'Error in timer: ',dname,' already on.'
+ level=level+1 !Increment the level
+ on(n)=.true.
+ ut0(n)=etime(tt)
+ ncall(n)=ncall(n)+1
+ if(ncall(n).gt.1.and.nlevel(n).ne.level) then
+ nlevel(n)=-1
+ else
+ nlevel(n)=level
+ endif
+ nparent(n)=onlevel(level-1)
+ onlevel(level)=n
+
+ else if(k.eq.1) then !Get stop times and accumulate sums. (k=1)
+ if(on(n)) then
+ on(n)=.false.
+ ut1=etime(tt)
+ ut(n)=ut(n)+ut1-ut0(n)
+ endif
+ level=level-1
+ endif
+
+ ntrace=ntrace+1
+ if(ntrace.lt.limtrace) write(28,1020) ntrace,dname,k,level,nparent(n)
+1020 format(i8,': ',a8,3i5)
+ return
+
+! Write out the timer statistics
+
+40 open(lu,file=trim(fname),status='unknown')
+ write(lu,1040)
+1040 format(/' name time frac dtime', &
+ ' dfrac calls level parent'/73('-'))
+
+ if(k.gt.100) then
+ ndiv=k-100
+ do i=1,nmax
+ ncall(i)=ncall(i)/ndiv
+ ut(i)=ut(i)/ndiv
+ enddo
+ endif
+
+ total=ut(1)
+ sum=0.
+ sumf=0.
+ do i=1,nmax
+ dut(i)=ut(i)
+ do j=i,nmax
+ if(nparent(j).eq.i) dut(i)=dut(i)-ut(j)
+ enddo
+ utf=ut(i)/total
+ dutf=dut(i)/total
+ sum=sum+dut(i)
+ sumf=sumf+dutf
+ kk=nlevel(i)
+ sname=space(1:kk)//name(i)//space(1:8-kk)
+ ename=space
+ if(i.ge.2) ename=name(nparent(i))
+ write(lu,1060) float(i),sname,ut(i),utf,dut(i),dutf, &
+ ncall(i),nlevel(i),ename
+1060 format(f4.0,a16,2(f10.2,f6.2),i7,i5,2x,a8)
+ enddo
+
+ write(lu,1070) sum,sumf
+1070 format(/36x,f10.2,f6.2)
+ close(lu)
+ return
+
+ entry clockit2(data_dir)
+ l1=index(data_dir,char(0))-1
+ if(l1.ge.1) data_dir(l1+1:l1+1)='/'
+ fname=data_dir(1:l1+1)//'clockit.out'
+ return
+
+end subroutine clockit
diff --git a/lib/ft4/ft4_downsample.f90 b/lib/ft4/ft4_downsample.f90
new file mode 100644
index 00000000..819b10fb
--- /dev/null
+++ b/lib/ft4/ft4_downsample.f90
@@ -0,0 +1,51 @@
+subroutine ft4_downsample(dd,newdata,f0,c)
+
+! Input: real data in dd() at sample rate 12000 Hz
+! Output: Complex data in c(), sampled at 1200 Hz
+
+ include 'ft4_params.f90'
+ parameter (NFFT2=NMAX/16)
+ real dd(NMAX)
+ complex c(0:NMAX/NDOWN-1)
+ complex c1(0:NFFT2-1)
+ complex cx(0:NMAX/2)
+ real x(NMAX), window(0:NFFT2-1)
+ equivalence (x,cx)
+ logical first, newdata
+ data first/.true./
+ save first,window,x
+
+ df=12000.0/NMAX
+ baud=12000.0/NSPS
+ if(first) then
+ bw_transition = 0.5*baud
+ bw_flat = 4*baud
+ iwt = bw_transition / df
+ iwf = bw_flat / df
+ pi=4.0*atan(1.0)
+ window(0:iwt-1) = 0.5*(1+cos(pi*(/(i,i=iwt-1,0,-1)/)/iwt))
+ window(iwt:iwt+iwf-1)=1.0
+ window(iwt+iwf:2*iwt+iwf-1) = 0.5*(1+cos(pi*(/(i,i=0,iwt-1)/)/iwt))
+ window(2*iwt+iwf:)=0.0
+ iws = baud / df
+ window=cshift(window,iws)
+ first=.false.
+ endif
+
+ if(newdata) then
+ x=dd
+ call four2a(x,NMAX,1,-1,0) !r2c FFT to freq domain
+ endif
+ i0=nint(f0/df)
+ c1=0.
+ c1(0)=cx(i0)
+ do i=1,NFFT2/2
+ if(i0+i.le.NMAX/2) c1(i)=cx(i0+i)
+ if(i0-i.ge.0) c1(NFFT2-i)=cx(i0-i)
+ enddo
+ c1=c1*window/NFFT2
+ call four2a(c1,NFFT2,1,1,1) !c2c FFT back to time domain
+ c=c1(0:NMAX/NDOWN-1)
+
+ return
+end subroutine ft4_downsample
diff --git a/lib/ft4/ft4_params.f90 b/lib/ft4/ft4_params.f90
new file mode 100644
index 00000000..46630cc2
--- /dev/null
+++ b/lib/ft4/ft4_params.f90
@@ -0,0 +1,16 @@
+! FT4
+! LDPC(174,91) code, four 4x4 Costas arrays for Sync
+
+parameter (KK=91) !Information bits (77 + CRC14)
+parameter (ND=87) !Data symbols
+parameter (NS=16) !Sync symbols
+parameter (NN=NS+ND) !Sync and data symbols (103)
+parameter (NN2=NS+ND+2) !Total channel symbols (105)
+parameter (NSPS=512) !Samples per symbol at 12000 S/s
+parameter (NZ=NSPS*NN) !Sync and Data samples (52736)
+parameter (NZ2=NSPS*NN2) !Total samples in shaped waveform (53760)
+parameter (NMAX=18*3456) !Samples in iwave
+parameter (NFFT1=2048, NH1=NFFT1/2) !Length of FFTs for symbol spectra
+parameter (NSTEP=NSPS) !Coarse time-sync step size
+parameter (NHSYM=(NMAX-NFFT1)/NSTEP) !Number of symbol spectra (1/4-sym steps)
+parameter (NDOWN=16) !Downsample factor
diff --git a/lib/ft4/ft4b.f90 b/lib/ft4/ft4b.f90
new file mode 100644
index 00000000..d425277e
--- /dev/null
+++ b/lib/ft4/ft4b.f90
@@ -0,0 +1,489 @@
+subroutine ft4b(cdatetime0,tbuf,nfa,nfb,nQSOProgress,ncontest,nfqso, &
+ iwave,ndecodes,mycall,hiscall,cqstr,line,data_dir)
+
+ use packjt77
+ include 'ft4_params.f90'
+ parameter (NSS=NSPS/NDOWN)
+
+ character message*37,msgsent*37,msg0*37
+ character c77*77
+ character*61 line,linex(100)
+ character*37 decodes(100)
+ character*512 data_dir,fname
+ character*17 cdatetime0
+ character*12 mycall,hiscall
+ character*12 mycall0,hiscall0
+ character*6 hhmmss
+ character*4 cqstr,cqstr0
+
+ complex cd2(0:NMAX/NDOWN-1) !Complex waveform
+ complex cb(0:NMAX/NDOWN-1)
+ complex cd(0:NN*NSS-1) !Complex waveform
+ complex ctwk(2*NSS),ctwk2(2*NSS,-16:16)
+ complex csymb(NSS)
+ complex cs(0:3,NN)
+ real s4(0:3,NN)
+
+ real bmeta(2*NN),bmetb(2*NN),bmetc(2*NN)
+ real a(5)
+ real llr(2*ND),llra(2*ND),llrb(2*ND),llrc(2*ND),llrd(2*ND)
+ real s2(0:255)
+ real candidate(3,100)
+ real savg(NH1),sbase(NH1)
+
+ integer apbits(2*ND)
+ integer apmy_ru(28),aphis_fd(28)
+ integer icos4a(0:3),icos4b(0:3),icos4c(0:3),icos4d(0:3)
+ integer*2 iwave(NMAX) !Raw received data
+ integer*1 message77(77),rvec(77),apmask(2*ND),cw(2*ND)
+ integer*1 hbits(2*NN)
+ integer graymap(0:3)
+ integer ip(1)
+ integer nappasses(0:5) ! # of decoding passes for QSO States 0-5
+ integer naptypes(0:5,4) ! nQSOProgress, decoding pass
+ integer mcq(29)
+ integer mrrr(19),m73(19),mrr73(19)
+
+ logical nohiscall,unpk77_success
+ logical one(0:255,0:7) ! 256 4-symbol sequences, 8 bits
+ logical first, dobigfft
+
+ data icos4a/0,1,3,2/
+ data icos4b/1,0,2,3/
+ data icos4c/2,3,1,0/
+ data icos4d/3,2,0,1/
+ data graymap/0,1,3,2/
+ data msg0/' '/
+ data first/.true./
+ data mcq/0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0/
+ data mrrr/0,1,1,1,1,1,1,0,1,0,0,1,0,0,1,0,0,0,1/
+ data m73/0,1,1,1,1,1,1,0,1,0,0,1,0,1,0,0,0,0,1/
+ data mrr73/0,1,1,1,1,1,1,0,0,1,1,1,0,1,0,1,0,0,1/
+ data rvec/0,1,0,0,1,0,1,0,0,1,0,1,1,1,1,0,1,0,0,0,1,0,0,1,1,0,1,1,0, &
+ 1,0,0,1,0,1,1,0,0,0,0,1,0,0,0,1,0,1,0,0,1,1,1,1,0,0,1,0,1, &
+ 0,1,0,1,0,1,1,0,1,1,1,1,1,0,0,0,1,0,1/
+ save fs,dt,tt,txt,twopi,h,one,first,linex,apbits,nappasses,naptypes, &
+ mycall0,hiscall0,msg0,cqstr0,ctwk2
+
+ call clockit('ft4_deco',0)
+ hhmmss=cdatetime0(8:13)
+
+ if(first) then
+ fs=12000.0/NDOWN !Sample rate after downsampling
+ dt=1/fs !Sample interval after downsample (s)
+ tt=NSPS*dt !Duration of "itone" symbols (s)
+ txt=NZ*dt !Transmission length (s) without ramp up/down
+ twopi=8.0*atan(1.0)
+ h=1.0
+ one=.false.
+ do i=0,255
+ do j=0,7
+ if(iand(i,2**j).ne.0) one(i,j)=.true.
+ enddo
+ enddo
+
+ do idf=-16,16
+ a=0.
+ a(1)=real(idf)
+ ctwk=1.
+ call clockit('twkfreq1',0)
+ call twkfreq1(ctwk,2*NSS,fs/2.0,a,ctwk2(:,idf))
+ call clockit('twkfreq1',1)
+ enddo
+
+ mrrr=2*mod(mrrr+rvec(59:77),2)-1
+ m73=2*mod(m73+rvec(59:77),2)-1
+ mrr73=2*mod(mrr73+rvec(59:77),2)-1
+ nappasses(0)=2
+ nappasses(1)=2
+ nappasses(2)=2
+ nappasses(3)=2
+ nappasses(4)=2
+ nappasses(5)=3
+
+! iaptype
+!------------------------
+! 1 CQ ??? ??? (29 ap bits)
+! 2 MyCall ??? ??? (29 ap bits)
+! 3 MyCall DxCall ??? (58 ap bits)
+! 4 MyCall DxCall RRR (77 ap bits)
+! 5 MyCall DxCall 73 (77 ap bits)
+! 6 MyCall DxCall RR73 (77 ap bits)
+!********
+ naptypes(0,1:4)=(/1,2,0,0/) ! Tx6 selected (CQ)
+ naptypes(1,1:4)=(/2,3,0,0/) ! Tx1
+ naptypes(2,1:4)=(/2,3,0,0/) ! Tx2
+ naptypes(3,1:4)=(/3,6,0,0/) ! Tx3
+ naptypes(4,1:4)=(/3,6,0,0/) ! Tx4
+ naptypes(5,1:4)=(/3,1,2,0/) ! Tx5
+
+ mycall0=''
+ hiscall0=''
+ cqstr0=''
+ first=.false.
+ endif
+
+ if(cqstr.ne.cqstr0) then
+ i0=index(cqstr,' ')
+ if(i0.le.1) then
+ message='CQ A1AA AA01'
+ else
+ message='CQ '//cqstr(1:i0-1)//' A1AA AA01'
+ endif
+ i3=-1
+ n3=-1
+ call pack77(message,i3,n3,c77)
+ call unpack77(c77,1,msgsent,unpk77_success)
+ read(c77,'(29i1)') mcq
+ mcq=2*mod(mcq+rvec(1:29),2)-1
+ cqstr0=cqstr
+ endif
+
+ l1=index(mycall,char(0))
+ if(l1.ne.0) mycall(l1:)=" "
+ l1=index(hiscall,char(0))
+ if(l1.ne.0) hiscall(l1:)=" "
+ if(mycall.ne.mycall0 .or. hiscall.ne.hiscall0) then
+ apbits=0
+ apbits(1)=99
+ apbits(30)=99
+ apmy_ru=0
+ aphis_fd=0
+
+ if(len(trim(mycall)) .lt. 3) go to 10
+
+ nohiscall=.false.
+ hiscall0=hiscall
+ if(len(trim(hiscall0)).lt.3) then
+ hiscall0=mycall ! use mycall for dummy hiscall - mycall won't be hashed.
+ nohiscall=.true.
+ endif
+ message=trim(mycall)//' '//trim(hiscall0)//' RR73'
+ i3=-1
+ n3=-1
+ call pack77(message,i3,n3,c77)
+ call unpack77(c77,1,msgsent,unpk77_success)
+ if(i3.ne.1 .or. (message.ne.msgsent) .or. .not.unpk77_success) go to 10
+ read(c77,'(77i1)') message77
+ apmy_ru=2*mod(message77(1:28)+rvec(2:29),2)-1
+ aphis_fd=2*mod(message77(30:57)+rvec(29:56),2)-1
+ message77=mod(message77+rvec,2)
+ call encode174_91(message77,cw)
+ apbits=2*cw-1
+ if(nohiscall) apbits(30)=99
+
+10 continue
+ mycall0=mycall
+ hiscall0=hiscall
+ endif
+ candidate=0.0
+ ncand=0
+ syncmin=1.2
+ maxcand=100
+
+ fa=nfa
+ fb=nfb
+ call clockit('getcand4',0)
+ call getcandidates4(iwave,fa,fb,syncmin,nfqso,maxcand,savg,candidate, &
+ ncand,sbase)
+ call clockit('getcand4',1)
+
+ ndecodes=0
+ dobigfft=.true.
+ do icand=1,ncand
+ f0=candidate(1,icand)
+ snr=candidate(3,icand)-1.0
+ if( f0.le.10.0 .or. f0.ge.4990.0 ) cycle
+ call clockit('ft4_down',0)
+ call ft4_downsample(iwave,dobigfft,f0,cd2) !Downsample from 512 to 32 Sa/Symbol
+ if(dobigfft) dobigfft=.false.
+ call clockit('ft4_down',1)
+
+ sum2=sum(cd2*conjg(cd2))/(real(NMAX)/real(NDOWN))
+ if(sum2.gt.0.0) cd2=cd2/sqrt(sum2)
+! Sample rate is now 12000/16 = 750 samples/second
+ do isync=1,2
+ if(isync.eq.1) then
+ idfmin=-12
+ idfmax=12
+ idfstp=3
+ ibmin=0
+ ibmax=216 !Max DT = 216/750 = 0.288 s
+ ibstp=4
+ else
+ idfmin=idfbest-4
+ idfmax=idfbest+4
+ idfstp=1
+ ibmin=max(0,ibest-5)
+ ibmax=min(ibest+5,NMAX/NDOWN-1)
+ ibstp=1
+ endif
+ ibest=-1
+ smax=-99.
+ idfbest=0
+ do idf=idfmin,idfmax,idfstp
+
+ call clockit('sync4d ',0)
+ do istart=ibmin,ibmax,ibstp
+ call sync4d(cd2,istart,ctwk2(:,idf),1,sync) !Find sync power
+ if(sync.gt.smax) then
+ smax=sync
+ ibest=istart
+ idfbest=idf
+ endif
+ enddo
+ call clockit('sync4d ',1)
+
+ enddo
+ enddo
+ f0=f0+real(idfbest)
+ if( f0.le.10.0 .or. f0.ge.4990.0 ) cycle
+
+ call clockit('ft4down ',0)
+ call ft4_downsample(iwave,dobigfft,f0,cb) !Final downsample with corrected f0
+ call clockit('ft4down ',1)
+ sum2=sum(abs(cb)**2)/(real(NSS)*NN)
+ if(sum2.gt.0.0) cb=cb/sqrt(sum2)
+ cd=cb(ibest:ibest+NN*NSS-1)
+ call clockit('four2a ',0)
+ do k=1,NN
+ i1=(k-1)*NSS
+ csymb=cd(i1:i1+NSS-1)
+ call four2a(csymb,NSS,1,-1,1)
+ cs(0:3,k)=csymb(1:4)
+ s4(0:3,k)=abs(csymb(1:4))
+ enddo
+ call clockit('four2a ',1)
+
+! Sync quality check
+ is1=0
+ is2=0
+ is3=0
+ is4=0
+ do k=1,4
+ ip=maxloc(s4(:,k))
+ if(icos4a(k-1).eq.(ip(1)-1)) is1=is1+1
+ ip=maxloc(s4(:,k+33))
+ if(icos4b(k-1).eq.(ip(1)-1)) is2=is2+1
+ ip=maxloc(s4(:,k+66))
+ if(icos4c(k-1).eq.(ip(1)-1)) is3=is3+1
+ ip=maxloc(s4(:,k+99))
+ if(icos4d(k-1).eq.(ip(1)-1)) is4=is4+1
+ enddo
+ nsync=is1+is2+is3+is4 !Number of correct hard sync symbols, 0-16
+ if(smax .lt. 0.7 .or. nsync .lt. 8) cycle
+
+ do nseq=1,3 !Try coherent sequences of 1, 2, and 4 symbols
+ if(nseq.eq.1) nsym=1
+ if(nseq.eq.2) nsym=2
+ if(nseq.eq.3) nsym=4
+ nt=2**(2*nsym)
+ do ks=1,NN-nsym+1,nsym !87+16=103 symbols.
+ amax=-1.0
+ do i=0,nt-1
+ i1=i/64
+ i2=iand(i,63)/16
+ i3=iand(i,15)/4
+ i4=iand(i,3)
+ if(nsym.eq.1) then
+ s2(i)=abs(cs(graymap(i4),ks))
+ elseif(nsym.eq.2) then
+ s2(i)=abs(cs(graymap(i3),ks)+cs(graymap(i4),ks+1))
+ elseif(nsym.eq.4) then
+ s2(i)=abs(cs(graymap(i1),ks ) + &
+ cs(graymap(i2),ks+1) + &
+ cs(graymap(i3),ks+2) + &
+ cs(graymap(i4),ks+3) &
+ )
+ else
+ print*,"Error - nsym must be 1, 2, or 4."
+ endif
+ enddo
+ ipt=1+(ks-1)*2
+ if(nsym.eq.1) ibmax=1
+ if(nsym.eq.2) ibmax=3
+ if(nsym.eq.4) ibmax=7
+ do ib=0,ibmax
+ bm=maxval(s2(0:nt-1),one(0:nt-1,ibmax-ib)) - &
+ maxval(s2(0:nt-1),.not.one(0:nt-1,ibmax-ib))
+ if(ipt+ib.gt.2*NN) cycle
+ if(nsym.eq.1) then
+ bmeta(ipt+ib)=bm
+ elseif(nsym.eq.2) then
+ bmetb(ipt+ib)=bm
+ elseif(nsym.eq.4) then
+ bmetc(ipt+ib)=bm
+ endif
+ enddo
+ enddo
+ enddo
+
+ bmetb(205:206)=bmeta(205:206)
+ bmetc(201:204)=bmetb(201:204)
+ bmetc(205:206)=bmeta(205:206)
+
+ call clockit('normaliz',0)
+ call normalizebmet(bmeta,2*NN)
+ call normalizebmet(bmetb,2*NN)
+ call normalizebmet(bmetc,2*NN)
+ call clockit('normaliz',1)
+
+ hbits=0
+ where(bmeta.ge.0) hbits=1
+ ns1=count(hbits( 1: 8).eq.(/0,0,0,1,1,0,1,1/))
+ ns2=count(hbits( 67: 74).eq.(/0,1,0,0,1,1,1,0/))
+ ns3=count(hbits(133:140).eq.(/1,1,1,0,0,1,0,0/))
+ ns4=count(hbits(199:206).eq.(/1,0,1,1,0,0,0,1/))
+ nsync_qual=ns1+ns2+ns3+ns4
+ if(nsync_qual.lt. 20) cycle
+
+ scalefac=2.83
+ llra( 1: 58)=bmeta( 9: 66)
+ llra( 59:116)=bmeta( 75:132)
+ llra(117:174)=bmeta(141:198)
+ llra=scalefac*llra
+ llrb( 1: 58)=bmetb( 9: 66)
+ llrb( 59:116)=bmetb( 75:132)
+ llrb(117:174)=bmetb(141:198)
+ llrb=scalefac*llrb
+ llrc( 1: 58)=bmetc( 9: 66)
+ llrc( 59:116)=bmetc( 75:132)
+ llrc(117:174)=bmetc(141:198)
+ llrc=scalefac*llrc
+
+ apmag=maxval(abs(llra))*1.1
+ npasses=3+nappasses(nQSOProgress)
+ if(ncontest.ge.5) npasses=3 ! Don't support Fox and Hound
+ do ipass=1,npasses
+ if(ipass.eq.1) llr=llra
+ if(ipass.eq.2) llr=llrb
+ if(ipass.eq.3) llr=llrc
+ if(ipass.le.3) then
+ apmask=0
+ iaptype=0
+ endif
+
+ if(ipass .gt. 3) then
+ llrd=llrc
+ iaptype=naptypes(nQSOProgress,ipass-3)
+
+! ncontest=0 : NONE
+! 1 : NA_VHF
+! 2 : EU_VHF
+! 3 : FIELD DAY
+! 4 : RTTY
+! 5 : FOX
+! 6 : HOUND
+!
+! Conditions that cause us to bail out of AP decoding
+ napwid=50
+ if(ncontest.le.4 .and. iaptype.ge.3 .and. (abs(f0-nfqso).gt.napwid) ) cycle
+ if(iaptype.ge.2 .and. apbits(1).gt.1) cycle ! No, or nonstandard, mycall
+ if(iaptype.ge.3 .and. apbits(30).gt.1) cycle ! No, or nonstandard, dxcall
+
+ if(iaptype.eq.1) then ! CQ or CQ TEST or CQ FD or CQ RU or CQ SCC
+ apmask=0
+ apmask(1:29)=1
+ llrd(1:29)=apmag*mcq(1:29)
+ endif
+
+ if(iaptype.eq.2) then ! MyCall,???,???
+ apmask=0
+ if(ncontest.eq.0.or.ncontest.eq.1) then
+ apmask(1:29)=1
+ llrd(1:29)=apmag*apbits(1:29)
+ else if(ncontest.eq.2) then
+ apmask(1:28)=1
+ llrd(1:28)=apmag*apbits(1:28)
+ else if(ncontest.eq.3) then
+ apmask(1:28)=1
+ llrd(1:28)=apmag*apbits(1:28)
+ else if(ncontest.eq.4) then
+ apmask(2:29)=1
+ llrd(2:29)=apmag*apmy_ru(1:28)
+ endif
+ endif
+
+ if(iaptype.eq.3) then ! MyCall,DxCall,???
+ apmask=0
+ if(ncontest.eq.0.or.ncontest.eq.1.or.ncontest.eq.2) then
+ apmask(1:58)=1
+ llrd(1:58)=apmag*apbits(1:58)
+ else if(ncontest.eq.3) then ! Field Day
+ apmask(1:56)=1
+ llrd(1:28)=apmag*apbits(1:28)
+ llrd(29:56)=apmag*aphis_fd(1:28)
+ else if(ncontest.eq.4) then ! RTTY RU
+ apmask(2:57)=1
+ llrd(2:29)=apmag*apmy_ru(1:28)
+ llrd(30:57)=apmag*apbits(30:57)
+ endif
+ endif
+
+ if(iaptype.eq.4 .or. iaptype.eq.5 .or. iaptype.eq.6) then
+ apmask=0
+ if(ncontest.le.4) then
+ apmask(1:91)=1 ! mycall, hiscall, RRR|73|RR73
+ if(iaptype.eq.6) llrd(1:91)=apmag*apbits(1:91)
+ endif
+ endif
+
+ llr=llrd
+ endif
+ max_iterations=40
+ message77=0
+ call clockit('bpdecode',0)
+ call bpdecode174_91(llr,apmask,max_iterations,message77, &
+ cw,nharderror,niterations)
+ call clockit('bpdecode',1)
+ if(sum(message77).eq.0) cycle
+ if( nharderror.ge.0 ) then
+ message77=mod(message77+rvec,2) ! remove rvec scrambling
+ write(c77,'(77i1)') message77(1:77)
+ call unpack77(c77,1,message,unpk77_success)
+ idupe=0
+ do i=1,ndecodes
+ if(decodes(i).eq.message) idupe=1
+ enddo
+ if(ibest.le.10 .and. message.eq.msg0) idupe=1 !Already decoded
+ if(idupe.eq.1) exit
+ ndecodes=ndecodes+1
+ decodes(ndecodes)=message
+ if(snr.gt.0.0) then
+ xsnr=10*log10(snr)-14.0
+ else
+ xsnr=-20.0
+ endif
+ nsnr=nint(max(-20.0,xsnr))
+ freq=f0
+ tsig=mod(tbuf + ibest/750.0,100.0)
+
+ write(line,1000) hhmmss,nsnr,tsig,nint(freq),message
+1000 format(a6,i4,f5.1,i5,' + ',1x,a37)
+ l1=index(data_dir,char(0))-1
+ if(l1.ge.1) data_dir(l1+1:l1+1)="/"
+ fname=data_dir(1:l1+1)//'all_ft4.txt'
+ open(24,file=trim(fname),status='unknown',position='append')
+ write(24,1002) cdatetime0,nsnr,tsig,nint(freq),message, &
+ nharderror,nsync_qual,ipass,niterations,iaptype,nsync
+ if(hhmmss.eq.' ') write(*,1002) cdatetime0,nsnr, &
+ tsig,nint(freq),message,nharderror,nsync_qual,ipass, &
+ niterations,iaptype
+1002 format(a17,i4,f5.1,i5,' Rx ',a37,6i4)
+ close(24)
+ linex(ndecodes)=line
+ if(ibest.ge.ibmax-15) msg0=message !Possible dupe candidate
+ exit
+ endif
+ enddo !Sequence estimation
+ enddo !Candidate list
+ call clockit('ft4_deco',1)
+ call clockit2(data_dir)
+ call clockit('ft4_deco',101)
+ return
+
+ entry get_ft4msg(idecode,line)
+ line=linex(idecode)
+ return
+
+ end subroutine ft4b
diff --git a/lib/ft4/ft4d.f90 b/lib/ft4/ft4d.f90
new file mode 100644
index 00000000..a63e3e0a
--- /dev/null
+++ b/lib/ft4/ft4d.f90
@@ -0,0 +1,83 @@
+program ft4d
+
+ include 'ft4_params.f90'
+ character*8 arg
+ character*17 cdatetime
+ character*512 data_dir
+ character*12 mycall
+ character*12 hiscall
+ character*80 infile
+ character*61 line
+ character*4 cqstr
+ real*8 fMHz
+ integer ihdr(11)
+ integer*2 iwave(240000) !20*12000
+
+ fs=12000.0/NDOWN !Sample rate
+ dt=1/fs !Sample interval after downsample (s)
+ tt=NSPS*dt !Duration of "itone" symbols (s)
+ baud=1.0/tt !Keying rate for "itone" symbols (baud)
+ txt=NZ*dt !Transmission length (s)
+
+ nargs=iargc()
+ if(nargs.lt.1) then
+ print*,'Usage: ft4d [-a <data_dir>] [-f fMHz] [-n nQSOProgress] file1 [file2 ...]'
+ go to 999
+ endif
+ iarg=1
+ data_dir="."
+ call getarg(iarg,arg)
+ if(arg(1:2).eq.'-a') then
+ call getarg(iarg+1,data_dir)
+ iarg=iarg+2
+ endif
+ call getarg(iarg,arg)
+ if(arg(1:2).eq.'-f') then
+ call getarg(iarg+1,arg)
+ read(arg,*) fMHz
+ iarg=iarg+2
+ endif
+ nQSOProgress=0
+ if(arg(1:2).eq.'-n') then
+ call getarg(iarg+1,arg)
+ read(arg,*) nQSOProgress
+ iarg=iarg+2
+ endif
+ nfa=10
+ nfb=4990
+ ndecodes=0
+ nfqso=1500
+ mycall="K9AN"
+ hiscall="K1JT"
+ ncontest=4
+ cqstr="RU "
+
+ do ifile=iarg,nargs
+ call getarg(ifile,infile)
+ open(10,file=infile,status='old',access='stream')
+ read(10) ihdr
+ npts=min(ihdr(11)/2,180000)
+ read(10) iwave(1:npts)
+ close(10)
+ cdatetime=infile
+ j2=index(infile,'.wav')
+ if(j2.ge.14) cdatetime=infile(j2-13:j2)//'000'
+ istep=3456
+ nsteps=(npts-52800)/istep + 1
+ do n=1,nsteps
+ i0=(n-1)*istep + 1
+ tbuf=(i0-1)/12000.0
+ call ft4b(cdatetime,tbuf,nfa,nfb,nQSOProgress,ncontest, &
+ nfqso,iwave(i0),ndecodes,mycall,hiscall,cqstr,line,data_dir)
+ do idecode=1,ndecodes
+ call get_ft4msg(idecode,line)
+ write(*,'(a61)') line
+ enddo
+ enddo !steps
+ enddo !files
+
+ call four2a(xx,-1,1,-1,1) !Destroy FFTW plans to free their memory
+
+999 end program ft4d
+
+
diff --git a/lib/ft4/ft4sim.f90 b/lib/ft4/ft4sim.f90
new file mode 100644
index 00000000..94ab8218
--- /dev/null
+++ b/lib/ft4/ft4sim.f90
@@ -0,0 +1,152 @@
+program ft4sim
+
+! Generate simulated signals for experimental "FT4" mode
+
+ use wavhdr
+ use packjt77
+ include 'ft4_params.f90' !Set various constants
+ parameter (NWAVE=NN*NSPS)
+ parameter (NZZ=18*3456) !62208
+ type(hdr) h !Header for .wav file
+ character arg*12,fname*17
+ character msg37*37,msgsent37*37
+ character c77*77
+ complex c0(0:NZZ-1)
+ complex c(0:NZZ-1)
+ real wave(NZZ)
+ real dphi(0:NZZ-1)
+ real pulse(3*NSPS)
+ integer itone(NN)
+ integer*1 msgbits(77)
+ integer*2 iwave(NZZ) !Generated full-length waveform
+ integer icos4(4)
+ data icos4/0,1,3,2/
+
+! Get command-line argument(s)
+ nargs=iargc()
+ if(nargs.ne.7) then
+ print*,'Usage: ft4sim "message" f0 DT fdop del nfiles snr'
+ print*,'Examples: ft4sim "CQ W9XYZ EN37" 1500 0.0 0.1 1.0 10 -15'
+ print*,' ft4sim "K1ABC W9XYZ R 539 WI" 1500 0.0 0.1 1.0 10 -15'
+ go to 999
+ endif
+ call getarg(1,msg37) !Message to be transmitted
+ call getarg(2,arg)
+ read(arg,*) f0 !Frequency (only used for single-signal)
+ call getarg(3,arg)
+ read(arg,*) xdt !Time offset from nominal (s)
+ call getarg(4,arg)
+ read(arg,*) fspread !Watterson frequency spread (Hz)
+ call getarg(5,arg)
+ read(arg,*) delay !Watterson delay (ms)
+ call getarg(6,arg)
+ read(arg,*) nfiles !Number of files
+ call getarg(7,arg)
+ read(arg,*) snrdb !SNR_2500
+
+ nfiles=abs(nfiles)
+ twopi=8.0*atan(1.0)
+ fs=12000.0 !Sample rate (Hz)
+ dt=1.0/fs !Sample interval (s)
+ hmod=1.0 !Modulation index (0.5 is MSK, 1.0 is FSK)
+ tt=NSPS*dt !Duration of symbols (s)
+ baud=1.0/tt !Keying rate (baud)
+ txt=NZ*dt !Transmission length (s)
+
+ bandwidth_ratio=2500.0/(fs/2.0)
+ sig=sqrt(2*bandwidth_ratio) * 10.0**(0.05*snrdb)
+ if(snrdb.gt.90.0) sig=1.0
+ txt=NN*NSPS/12000.0
+
+ ! Source-encode, then get itone()
+ i3=-1
+ n3=-1
+ call pack77(msg37,i3,n3,c77)
+ read(c77,'(77i1)') msgbits
+ call genft4(msg37,0,msgsent37,msgbits,itone)
+ write(*,*)
+ write(*,'(a9,a37,3x,a7,i1,a1,i1)') 'Message: ',msgsent37,'i3.n3: ',i3,'.',n3
+ write(*,1000) f0,xdt,txt,snrdb
+1000 format('f0:',f9.3,' DT:',f6.2,' TxT:',f6.1,' SNR:',f6.1)
+ write(*,*)
+ if(i3.eq.1) then
+ write(*,*) ' mycall hiscall hisgrid'
+ write(*,'(28i1,1x,i1,1x,28i1,1x,i1,1x,i1,1x,15i1,1x,3i1)') msgbits(1:77)
+ else
+ write(*,'(a14)') 'Message bits: '
+ write(*,'(77i1)') msgbits
+ endif
+ write(*,*)
+ write(*,'(a17)') 'Channel symbols: '
+ write(*,'(76i1)') itone
+ write(*,*)
+
+ call sgran()
+
+! The filtered frequency pulse
+ do i=1,3*NSPS
+ tt=(i-1.5*NSPS)/real(NSPS)
+ pulse(i)=gfsk_pulse(1.0,tt)
+ enddo
+
+! Define the instantaneous frequency waveform
+ dphi_peak=twopi*hmod/real(NSPS)
+ dphi=0.0
+ do j=1,NN
+ ib=(j-1)*NSPS
+ ie=ib+3*NSPS-1
+ dphi(ib:ie)=dphi(ib:ie)+dphi_peak*pulse*itone(j)
+ enddo
+
+ phi=0.0
+ c0=0.0
+ dphi=dphi+twopi*f0*dt
+! do j=0,NMAX-1 !### ??? ###
+ do j=0,(NN+2)*NSPS-1
+ c0(j)=cmplx(cos(phi),sin(phi))
+ phi=mod(phi+dphi(j),twopi)
+ enddo
+
+ c0(0:NSPS-1)=c0(0:NSPS-1)*(1.0-cos(twopi*(/(i,i=0,NSPS-1)/)/(2.0*NSPS)) )/2.0
+ c0((NN+1)*NSPS:(NN+2)*NSPS-1)=c0((NN+1)*NSPS:(NN+2)*NSPS-1)*(1.0+cos(twopi*(/(i,i=0,NSPS-1)/)/(2.0*NSPS) ))/2.0
+ c0((NN+2)*NSPS:)=0.
+
+ k=nint((xdt+0.5)/dt)
+ c0=cshift(c0,-k)
+
+ do ifile=1,nfiles
+ c=c0
+ if(fspread.ne.0.0 .or. delay.ne.0.0) call watterson(c,NZZ,NWAVE,fs,delay,fspread)
+ c=sig*c
+ wave=real(c)
+ peak=maxval(abs(wave))
+ nslots=1
+
+ if(snrdb.lt.90) then
+ do i=1,NZZ !Add gaussian noise at specified SNR
+ xnoise=gran()
+ wave(i)=wave(i) + xnoise
+ enddo
+ endif
+
+ gain=100.0
+ if(snrdb.lt.90.0) then
+ wave=gain*wave
+ else
+ datpk=maxval(abs(wave))
+ fac=32766.9/datpk
+ wave=fac*wave
+ endif
+ if(any(abs(wave).gt.32767.0)) print*,"Warning - data will be clipped."
+ iwave=nint(wave)
+ h=default_header(12000,NZZ)
+ write(fname,1102) ifile
+1102 format('000000_',i6.6,'.wav')
+ open(10,file=fname,status='unknown',access='stream')
+ write(10) h,iwave !Save to *.wav file
+ close(10)
+ write(*,1110) ifile,xdt,f0,snrdb,fname
+1110 format(i4,f7.2,f8.2,f7.1,2x,a17)
+ enddo
+
+999 end program ft4sim
diff --git a/lib/ft4/ft4sim_mult.f90 b/lib/ft4/ft4sim_mult.f90
new file mode 100644
index 00000000..31309328
--- /dev/null
+++ b/lib/ft4/ft4sim_mult.f90
@@ -0,0 +1,105 @@
+program ft4sim_mult
+
+! Generate simulated signals for experimental "FT4" mode
+
+ use wavhdr
+ use packjt77
+ include 'ft4_params.f90' !FT4 protocol constants
+ parameter (NWAVE=NN*NSPS)
+ parameter (NZZ=65760) !Length of .wav file (4.48+1.0)*12000
+ type(hdr) h !Header for .wav file
+ character arg*12,fname*17,cjunk*4
+ character msg37*37,msgsent37*37,c77*77
+ complex cwave0((NN+2)*NSPS)
+ real wave0((NN+2)*NSPS)
+ real wave(NZZ)
+ real tmp(NZZ)
+ integer itone(NN)
+ integer*1 msgbits(77)
+ integer*2 iwave(NZZ) !Generated full-length waveform
+ integer icos4(4)
+ data icos4/0,1,3,2/
+
+! Get command-line argument(s)
+ nargs=iargc()
+ if(nargs.ne.2) then
+ print*,'Usage: ft4sim_mult nsigs nfiles'
+ print*,'Example: ft4sim_mult 20 8 '
+ go to 999
+ endif
+ call getarg(1,arg)
+ read(arg,*) nsigs !Number of signals
+ call getarg(2,arg)
+ read(arg,*) nfiles !Number of files
+
+ twopi=8.0*atan(1.0)
+ fs=12000.0 !Sample rate (Hz)
+ dt=1.0/fs !Sample interval (s)
+ hmod=1.0 !Modulation index (0.5 is MSK, 1.0 is FSK)
+ tt=NSPS*dt !Duration of unsmoothed symbols (s)
+ baud=1.0/tt !Keying rate (baud)
+ txt=NZ*dt !Transmission length (s) without ramp up/down
+ bandwidth_ratio=2500.0/(fs/2.0)
+ txt=NN*NSPS/12000.0
+ open(10,file='messages.txt',status='old',err=998)
+
+ do ifile=1,nfiles
+1 read(10,1001,end=999) cjunk,n
+1001 format(a4,i2)
+ if(cjunk.ne.'File' .or. n.ne.ifile) go to 1
+ wave=0.
+ write(fname,1002) ifile
+1002 format('000000_',i6.6,'.wav')
+
+ do isig=1,nsigs
+ read(10,1003,end=100) cjunk,isnr,xdt0,ifreq,msg37
+1003 format(a4,30x,i3,f5.1,i5,1x,a37)
+ if(cjunk.eq.'File') go to 100
+ if(isnr.lt.-16) isnr=-16
+ f0=ifreq*93.75/50.0
+ call random_number(r)
+ xdt=r-0.5
+! Source-encode, then get itone()
+ i3=-1
+ n3=-1
+ call pack77(msg37,i3,n3,c77)
+ call genft4(msg37,0,msgsent37,msgbits,itone)
+ nwave0=(NN+2)*NSPS
+ icmplx=0
+ call gen_ft4wave(itone,NN,NSPS,12000.0,f0,cwave0,wave0,icmplx,nwave0)
+
+ k0=nint((xdt+0.5)/dt)
+ if(k0.lt.1) k0=1
+ tmp(:k0-1)=0.0
+ tmp(k0:k0+nwave0-1)=wave0
+ tmp(k0+nwave0:)=0.0
+
+ ! Insert this signal into wave() array
+ sig=sqrt(2*bandwidth_ratio) * 10.0**(0.05*isnr)
+ wave=wave + sig*tmp
+ write(*,1100) fname(1:13),isig,isnr,xdt,nint(f0),msg37
+1100 format(a13,i4,i5,f5.1,i6,2x,a37)
+ enddo ! isig
+
+100 backspace 10
+
+ do i=1,NZZ !Add gaussian noise at specified SNR
+ xnoise=gran()
+ wave(i)=wave(i) + xnoise
+ enddo
+
+ gain=30.0
+ wave=gain*wave
+ if(any(abs(wave).gt.32767.0)) print*,"Warning - data will be clipped."
+ iwave=nint(wave)
+ h=default_header(12000,NZZ)
+ open(12,file=fname,status='unknown',access='stream')
+ write(12) h,iwave !Save to *.wav file
+ close(12)
+ print*,' '
+ enddo ! ifile
+ go to 999
+
+998 print*,'Cannot open file "messages.txt"'
+
+999 end program ft4sim_mult
diff --git a/lib/ft4/gen_ft4wave.f90 b/lib/ft4/gen_ft4wave.f90
new file mode 100644
index 00000000..410e8340
--- /dev/null
+++ b/lib/ft4/gen_ft4wave.f90
@@ -0,0 +1,66 @@
+subroutine gen_ft4wave(itone,nsym,nsps,fsample,f0,cwave,wave,icmplx,nwave)
+
+ real wave(nwave)
+ complex cwave(nwave)
+ real pulse(6144) !512*4*3
+ real dphi(0:240000-1)
+ integer itone(nsym)
+ logical first
+ data first/.true./
+ save pulse,first,twopi,dt,hmod
+
+ if(first) then
+ twopi=8.0*atan(1.0)
+ dt=1.0/fsample
+ hmod=1.0
+! Compute the frequency-smoothing pulse
+ do i=1,3*nsps
+ tt=(i-1.5*nsps)/real(nsps)
+ pulse(i)=gfsk_pulse(1.0,tt)
+ enddo
+ first=.false.
+ endif
+
+! Compute the smoothed frequency waveform.
+! Length = (nsym+2)*nsps samples, zero-padded
+ dphi_peak=twopi*hmod/real(nsps)
+ dphi=0.0
+ do j=1,nsym
+ ib=(j-1)*nsps
+ ie=ib+3*nsps-1
+ dphi(ib:ie) = dphi(ib:ie) + dphi_peak*pulse(1:3*nsps)*itone(j)
+ enddo
+
+! Calculate and insert the audio waveform
+ phi=0.0
+ dphi = dphi + twopi*f0*dt !Shift frequency up by f0
+ wave=0.
+ if(icmplx.eq.1) cwave=0.
+ k=0
+ do j=0,nwave-1
+ k=k+1
+ if(icmplx.eq.0) then
+ wave(k)=sin(phi)
+ else
+ cwave(k)=cmplx(cos(phi),sin(phi))
+ endif
+ phi=mod(phi+dphi(j),twopi)
+ enddo
+
+! Compute the ramp-up and ramp-down symbols
+ if(icmplx.eq.0) then
+ wave(1:nsps)=wave(1:nsps) * &
+ (1.0-cos(twopi*(/(i,i=0,nsps-1)/)/(2.0*nsps)))/2.0
+ k1=(nsym+1)*nsps+1
+ wave(k1:k1+nsps-1)=wave(k1:k1+nsps-1) * &
+ (1.0+cos(twopi*(/(i,i=0,nsps-1)/)/(2.0*nsps)))/2.0
+ else
+ cwave(1:nsps)=cwave(1:nsps) * &
+ (1.0-cos(twopi*(/(i,i=0,nsps-1)/)/(2.0*nsps)))/2.0
+ k1=(nsym+1)*nsps+1
+ cwave(k1:k1+nsps-1)=cwave(k1:k1+nsps-1) * &
+ (1.0+cos(twopi*(/(i,i=0,nsps-1)/)/(2.0*nsps)))/2.0
+ endif
+
+ return
+end subroutine gen_ft4wave
diff --git a/lib/ft4/genft4.f90 b/lib/ft4/genft4.f90
new file mode 100644
index 00000000..f57ca59a
--- /dev/null
+++ b/lib/ft4/genft4.f90
@@ -0,0 +1,90 @@
+subroutine genft4(msg0,ichk,msgsent,msgbits,i4tone)
+
+! Encode an FT4 message
+! Input:
+! - msg0 requested message to be transmitted
+! - ichk if ichk=1, return only msgsent
+! - msgsent message as it will be decoded
+! - i4tone array of audio tone values, {0,1,2,3}
+
+! Frame structure:
+! s16 + 87symbols + 2 ramp up/down = 105 total channel symbols
+! r1 + s4 + d29 + s4 + d29 + s4 + d29 + s4 + r1
+
+! Message duration: TxT = 105*512/12000 = 4.48 s
+
+! use iso_c_binding, only: c_loc,c_size_t
+
+ use packjt77
+ include 'ft4_params.f90'
+ character*37 msg0
+ character*37 message !Message to be generated
+ character*37 msgsent !Message as it will be received
+ character*77 c77
+ integer*4 i4tone(NN),itmp(ND)
+ integer*1 codeword(2*ND)
+ integer*1 msgbits(77),rvec(77)
+ integer icos4a(4),icos4b(4),icos4c(4),icos4d(4)
+ logical unpk77_success
+ data icos4a/0,1,3,2/
+ data icos4b/1,0,2,3/
+ data icos4c/2,3,1,0/
+ data icos4d/3,2,0,1/
+ data rvec/0,1,0,0,1,0,1,0,0,1,0,1,1,1,1,0,1,0,0,0,1,0,0,1,1,0,1,1,0, &
+ 1,0,0,1,0,1,1,0,0,0,0,1,0,0,0,1,0,1,0,0,1,1,1,1,0,0,1,0,1, &
+ 0,1,0,1,0,1,1,0,1,1,1,1,1,0,0,0,1,0,1/
+ message=msg0
+
+ do i=1, 37
+ if(ichar(message(i:i)).eq.0) then
+ message(i:37)=' '
+ exit
+ endif
+ enddo
+ do i=1,37 !Strip leading blanks
+ if(message(1:1).ne.' ') exit
+ message=message(i+1:)
+ enddo
+
+ i3=-1
+ n3=-1
+ call pack77(message,i3,n3,c77)
+ call unpack77(c77,0,msgsent,unpk77_success) !Unpack to get msgsent
+
+ if(ichk.eq.1) go to 999
+ read(c77,'(77i1)',err=1) msgbits
+ if(unpk77_success) go to 2
+1 msgbits=0
+ itone=0
+ msgsent='*** bad message *** '
+ go to 999
+
+entry get_ft4_tones_from_77bits(msgbits,i4tone)
+
+2 msgbits=mod(msgbits+rvec,2)
+ call encode174_91(msgbits,codeword)
+
+! Grayscale mapping:
+! bits tone
+! 00 0
+! 01 1
+! 11 2
+! 10 3
+
+ do i=1,ND
+ is=codeword(2*i)+2*codeword(2*i-1)
+ if(is.le.1) itmp(i)=is
+ if(is.eq.2) itmp(i)=3
+ if(is.eq.3) itmp(i)=2
+ enddo
+
+ i4tone(1:4)=icos4a
+ i4tone(5:33)=itmp(1:29)
+ i4tone(34:37)=icos4b
+ i4tone(38:66)=itmp(30:58)
+ i4tone(67:70)=icos4c
+ i4tone(71:99)=itmp(59:87)
+ i4tone(100:103)=icos4d
+
+999 return
+end subroutine genft4
diff --git a/lib/ft4/getcandidates4.f90 b/lib/ft4/getcandidates4.f90
new file mode 100644
index 00000000..5d42ca74
--- /dev/null
+++ b/lib/ft4/getcandidates4.f90
@@ -0,0 +1,86 @@
+subroutine getcandidates4(dd,fa,fb,syncmin,nfqso,maxcand,savg,candidate, &
+ ncand,sbase)
+
+ include 'ft4_params.f90'
+ real s(NH1,NHSYM)
+ real savg(NH1),savsm(NH1)
+ real sbase(NH1)
+ real x(NFFT1)
+ real window(NFFT1)
+ complex cx(0:NH1)
+ real candidate(3,maxcand)
+ real dd(NMAX)
+ integer indx(NH1)
+ integer ipk(1)
+ equivalence (x,cx)
+ logical first
+ data first/.true./
+ save first,window
+
+ if(first) then
+ first=.false.
+ pi=4.0*atan(1.)
+ window=0.
+ call nuttal_window(window,NFFT1)
+ endif
+
+! Compute symbol spectra, stepping by NSTEP steps.
+ savg=0.
+ tstep=NSTEP/12000.0
+ df=12000.0/NFFT1
+ fac=1.0/300.0
+ do j=1,NHSYM
+ ia=(j-1)*NSTEP + 1
+ ib=ia+NFFT1-1
+ if(ib.gt.NMAX) exit
+ x=fac*dd(ia:ib)*window
+ call four2a(x,NFFT1,1,-1,0) !r2c FFT
+ do i=1,NH1
+ s(i,j)=real(cx(i))**2 + aimag(cx(i))**2
+ enddo
+ savg=savg + s(1:NH1,j) !Average spectrum
+ enddo
+ savsm=0.
+ do i=8,NH1-7
+ savsm(i)=sum(savg(i-7:i+7))/15.
+ enddo
+ nfa=fa/df
+ if(nfa.lt.1) nfa=1
+ nfb=fb/df
+ if(nfb.gt.nint(5000.0/df)) nfb=nint(5000.0/df)
+ n300=300/df
+ n2500=2500/df
+! np=nfb-nfa+1
+ np=n2500-n300+1
+ indx=0
+ call indexx(savsm(n300:n2500),np,indx)
+ xn=savsm(n300+indx(nint(0.3*np)))
+ ncand=0
+ if(xn.le.1.e-8) return
+ savsm=savsm/xn
+! call ft4_baseline(savg,nfa,nfb,sbase)
+! savsm=savsm/sbase
+
+ f_offset = -1.5*12000/512
+ do i=nfa+1,nfb-1
+ if(savsm(i).ge.savsm(i-1) .and. savsm(i).ge.savsm(i+1) .and. &
+ savsm(i).ge.syncmin) then
+ den=savsm(i-1)-2*savsm(i)+savsm(i+1)
+ del=0.
+ if(den.ne.0.0) del=0.5*(savsm(i-1)-savsm(i+1))/den
+ fpeak=(i+del)*df+f_offset
+ speak=savsm(i) - 0.25*(savsm(i-1)-savsm(i+1))*del
+ ncand=ncand+1
+ if(ncand.gt.maxcand) then
+ ncand=maxcand
+ exit
+ endif
+ candidate(1,ncand)=fpeak
+ candidate(2,ncand)=-99.99
+ candidate(3,ncand)=speak
+ if(ncand.eq.maxcand) exit
+ endif
+ enddo
+
+return
+end subroutine getcandidates4
diff --git a/lib/ft4/messages.txt b/lib/ft4/messages.txt
new file mode 100644
index 00000000..16329fce
--- /dev/null
+++ b/lib/ft4/messages.txt
@@ -0,0 +1,162 @@
+File 1
+190106_000015 7.080 Rx FT8 -15 0.2 178 N1TRK N4FKH 569 VA
+190106_000015 7.080 Rx FT8 -13 -0.1 253 N1TRK KB7RUQ 539 UT
+190106_000015 7.080 Rx FT8 10 0.2 389 W0ZF N3LFC RR73
+190106_000015 7.080 Rx FT8 -10 0.1 450 PY4AZ KF7YED 559 MT
+190106_000015 7.080 Rx FT8 -3 0.2 507 N1TRK WA4DYD 559 GA
+190106_000015 7.080 Rx FT8 14 0.2 689 KB0VHA KA1YQC R 539 MA
+190106_000015 7.080 Rx FT8 -5 0.1 984 W9JA N9OY RR73
+190106_000015 7.080 Rx FT8 -12 0.1 1123 VA2CZ K8GNG RR73
+190106_000015 7.080 Rx FT8 2 0.1 1240 CQ RU K7RL CN88
+190106_000015 7.080 Rx FT8 -11 0.5 1293 K4ZMW K7VZ RR73
+190106_000015 7.080 Rx FT8 2 0.1 1387 WD9IGY KX1X 73
+190106_000015 7.080 Rx FT8 -10 0.1 1536 CQ RU W0FRC DM79
+190106_000015 7.080 Rx FT8 -2 0.1 1635 K4SQC VE3RX 549 ON
+190106_000030 7.080 Rx FT8 -11 0.2 1745 CQ RU K1LOG FN56
+190106_000030 7.080 Rx FT8 3 0.8 1798 WD5DAX WS4WW 73
+190106_000030 7.080 Rx FT8 12 0.1 1895 DJ6GI KG4W 73
+190106_000030 7.080 Rx FT8 -14 0.2 2119 KF7YED KF5ZNQ 549 TX
+190106_000030 7.080 Rx FT8 -5 0.1 336 CQ RU AB5XS EM12
+190106_000030 7.080 Rx FT8 0 0.0 1415 W3KIT AA8SW RR73
+190106_000030 7.080 Rx FT8 -13 0.3 1540 NI6G W7DRW 579 AZ
+190106_000030 7.080 Rx FT8 -10 0.1 312 SHIFT W9JA
+190106_000030 7.080 Rx FT8 -16 0.1 1447 W7BOB KJ7G 549 WA
+File 2
+190106_000045 7.080 Rx FT8 -9 0.1 178 N1TRK N4FKH 569 VA
+190106_000045 7.080 Rx FT8 -9 -0.1 253 N1TRK KB7RUQ RR73
+190106_000045 7.080 Rx FT8 -8 0.1 336 CQ RU AB5XS EM12
+190106_000045 7.080 Rx FT8 16 0.3 689 KB0VHA KA1YQC R 539 MA
+190106_000045 7.080 Rx FT8 -3 0.1 984 CQ RU N9OY EN43
+190106_000045 7.080 Rx FT8 -10 0.1 1146 K1JT WB4HXE 559 GA
+190106_000045 7.080 Rx FT8 6 0.1 1240 VE3LON K7RL R 549 WA
+190106_000045 7.080 Rx FT8 -1 0.1 1386 WD9IGY KX1X 73
+190106_000045 7.080 Rx FT8 -12 0.1 1536 CQ RU W0FRC DM79
+190106_000045 7.080 Rx FT8 3 0.1 1635 K4SQC VE3RX RR73
+190106_000045 7.080 Rx FT8 -5 0.1 1688 CQ RU W1QA FN32
+190106_000045 7.080 Rx FT8 1 0.8 1797 CQ RU WS4WW FM17
+190106_000045 7.080 Rx FT8 14 0.1 1895 HB9BUN KG4W R 549 VA
+190106_000100 7.080 Rx FT8 -7 0.1 2002 W9TO KN3ILZ 529 PA
+190106_000100 7.080 Rx FT8 -9 0.1 312 W9JA PY2APK RRR
+190106_000100 7.080 Rx FT8 -12 0.1 436 NZ7P WA7JAY 589 CA
+190106_000100 7.080 Rx FT8 -13 -0.1 1380 AC6BW KR9A R 559 WI
+190106_000100 7.080 Rx FT8 -17 0.1 1448 W7BOB KJ7G RR73
+190106_000100 7.080 Rx FT8 -7 0.3 1540 NI6G W7DRW 569 AZ
+File 3
+190106_000115 7.080 Rx FT8 -13 0.2 178 N1TRK N4FKH 569 VA
+190106_000115 7.080 Rx FT8 -14 -0.1 253 N1TRK KB7RUQ RR73
+190106_000115 7.080 Rx FT8 -5 0.1 336 CQ RU AB5XS EM12
+190106_000115 7.080 Rx FT8 -7 0.1 449 NI6G KF7YED 569 MT
+190106_000115 7.080 Rx FT8 14 0.2 689 KB0VHA KA1YQC 73
+190106_000115 7.080 Rx FT8 11 0.4 852 W9TO N4QWF 569 VA
+190106_000115 7.080 Rx FT8 -3 0.1 984 W4EMB N9OY R 529 WI
+190106_000115 7.080 Rx FT8 -14 0.1 1117 WB4FAY K7PDW 549 UT
+190106_000115 7.080 Rx FT8 3 0.2 1240 VE3LON K7RL R 559 WA
+190106_000115 7.080 Rx FT8 -18 0.1 1380 AC6BW KR9A R 559 WI
+190106_000115 7.080 Rx FT8 4 0.1 1475 K4KCL KB4S 559 VA
+190106_000130 7.080 Rx FT8 -5 0.1 1612 K1JT WB4HXE RR73
+190106_000115 7.080 Rx FT8 -1 0.1 1688 KW4RTR W1QA R 539 MA
+190106_000115 7.080 Rx FT8 -15 0.2 1745 CQ RU K1LOG FN56
+190106_000115 7.080 Rx FT8 3 0.8 1798 WA1T WS4WW R 579 VA
+190106_000115 7.080 Rx FT8 14 0.1 1895 HB9BUN KG4W R 549 VA
+190106_000115 7.080 Rx FT8 -9 0.2 2125 CQ RU NF3R FN20
+190106_000130 7.080 Rx FT8 -8 0.4 976 AG1T K7VZ 569 AZ
+190106_000130 7.080 Rx FT8 -7 0.1 1434 Z36W N4KMC 529 FL
+File 4
+190106_000145 7.080 Rx FT8 -8 0.1 335 CQ RU AB5XS EM12
+190106_000145 7.080 Rx FT8 -11 0.2 388 W0ZF WW4LL 559 ME
+190106_000145 7.080 Rx FT8 -10 0.1 449 NI6G KF7YED 569 MT
+190106_000145 7.080 Rx FT8 10 0.3 689 CQ RU KA1YQC FN42
+190106_000145 7.080 Rx FT8 12 0.4 852 W9TO N4QWF RR73
+190106_000145 7.080 Rx FT8 -2 0.1 984 W4EMB N9OY R 529 WI
+190106_000145 7.080 Rx FT8 -12 0.1 1117 WB4FAY K7PDW 549 UT
+190106_000145 7.080 Rx FT8 4 0.2 1240 VE3LON K7RL 73
+190106_000145 7.080 Rx FT8 -13 0.1 1348 WW5M KO9V 559 NC
+190106_000145 7.080 Rx FT8 -14 0.1 1380 AC6BW KR9A R 559 WI
+190106_000145 7.080 Rx FT8 2 0.1 1475 K4KCL KB4S RR73
+190106_000145 7.080 Rx FT8 -9 0.1 1536 KC1HTT W0FRC 73
+190106_000145 7.080 Rx FT8 1 0.1 1688 KW4RTR W1QA R 539 MA
+190106_000200 7.080 Rx FT8 1 0.8 1797 WA1T WS4WW R 569 VA
+190106_000200 7.080 Rx FT8 15 0.1 1895 CQ RU KG4W FM17
+190106_000200 7.080 Rx FT8 -6 0.2 2125 CQ RU NF3R FN20
+190106_000200 7.080 Rx FT8 -7 0.1 311 PD8DX PY2APK 539 0081
+190106_000200 7.080 Rx FT8 -3 0.5 976 AG1T K7VZ RR73
+190106_000200 7.080 Rx FT8 -5 0.1 1432 Z36W N4KMC 529 FL
+190106_000200 7.080 Rx FT8 -7 0.3 1540 N7BT W7DRW 559 AZ
+190106_000200 7.080 Rx FT8 -16 0.3 2124 N3KCR ON9COP 529 0053
+190106_000200 7.080 Rx FT8 -12 0.1 1448 K2DH KJ7G 549 WA
+File 5
+190106_000215 7.080 Rx FT8 11 0.1 435 K1JT WV8WA 569 WV
+190106_000215 7.080 Rx FT8 -4 0.1 335 CQ RU AB5XS EM12
+190106_000215 7.080 Rx FT8 10 0.5 689 CQ RU KA1YQC FN42
+190106_000215 7.080 Rx FT8 -10 0.1 843 CQ RU AE5JH EL07
+190106_000215 7.080 Rx FT8 -3 0.1 984 W4EMB N9OY R 529 WI
+190106_000215 7.080 Rx FT8 -10 0.1 1118 WB4FAY W5MO 539 TX
+190106_000215 7.080 Rx FT8 -16 0.1 1170 K1JT K8GNG 569 MI
+190106_000215 7.080 Rx FT8 6 0.1 1240 CQ RU K7RL CN88
+190106_000215 7.080 Rx FT8 -8 0.1 1348 WW5M KO9V RR73
+190106_000215 7.080 Rx FT8 -12 0.1 1432 Z36W N4KMC 529 FL
+190106_000215 7.080 Rx FT8 2 0.1 1490 KA6BIM KB4S 529 VA
+190106_000230 7.080 Rx FT8 -1 0.1 1688 CQ RU W1QA FN32
+190106_000230 7.080 Rx FT8 15 0.1 1895 DD5ZZ KG4W R 539 VA
+190106_000230 7.080 Rx FT8 -8 0.2 2125 CQ RU NF3R FN20
+190106_000230 7.080 Rx FT8 -13 0.1 1380 AC6BW KR9A R 559 WI
+190106_000230 7.080 Rx FT8 -4 0.1 1536 CQ RU W0FRC DM79
+190106_000230 7.080 Rx FT8 -8 0.3 1540 N7BT W7DRW 559 AZ
+File 6
+190106_000245 7.080 Rx FT8 6 0.1 635 K1JT WV8WA RR73
+190106_000245 7.080 Rx FT8 -12 0.1 335 VE3RX AB5XS R 589 TX
+190106_000245 7.080 Rx FT8 -16 0.1 423 WA1T PY2APK 529 0081
+190106_000245 7.080 Rx FT8 9 0.4 507 N1TRK N4QWF RR73
+190106_000245 7.080 Rx FT8 -10 0.1 869 W7BOB W3KIT 539 MD
+190106_000245 7.080 Rx FT8 -7 0.1 984 W4EMB N9OY R 529 WI
+190106_000245 7.080 Rx FT8 -13 0.1 1170 K1JT K8GNG 569 MI
+190106_000245 7.080 Rx FT8 5 0.1 1240 W6GMT K7RL R 569 WA
+190106_000245 7.080 Rx FT8 2 0.1 1490 KA6BIM KB4S 529 VA
+190106_000245 7.080 Rx FT8 -1 0.1 1688 CQ RU W1QA FN32
+190106_000245 7.080 Rx FT8 1 0.7 1798 N3KCR WS4WW R 599 VA
+190106_000245 7.080 Rx FT8 17 0.1 1895 DD5ZZ KG4W 73
+190106_000300 7.080 Rx FT8 -10 0.2 2125 CQ RU NF3R FN20
+190106_000300 7.080 Rx FT8 8 0.1 546 W7BOB W4DHE 539 KY
+190106_000300 7.080 Rx FT8 -9 0.2 844 CQ RU AE5JH EL07
+190106_000300 7.080 Rx FT8 -5 0.1 1536 W9WLX W0FRC R 549 CO
+File 7
+190106_000315 7.080 Rx FT8 -5 0.1 335 VE3RX AB5XS 73
+190106_000315 7.080 Rx FT8 -13 0.1 424 WA1T PY2APK 529 0081
+190106_000315 7.080 Rx FT8 6 0.1 545 W7BOB W4DHE 539 KY
+190106_000315 7.080 Rx FT8 -7 0.4 600 K2DH K7VZ 549 AZ
+190106_000315 7.080 Rx FT8 9 0.1 689 VE3LON KA1YQC 73
+190106_000315 7.080 Rx FT8 -8 0.1 869 W7BOB W3KIT 539 MD
+190106_000315 7.080 Rx FT8 -1 0.1 984 W4EMB N9OY R 529 WI
+190106_000315 7.080 Rx FT8 -15 -0.1 1117 WB4FAY KB7RUQ 529 UT
+190106_000315 7.080 Rx FT8 7 -0.2 1240 W6GMT K7RL 73
+190106_000315 7.080 Rx FT8 -5 0.2 1581 VA3WW WA4DYD 559 GA
+190106_000315 7.080 Rx FT8 -8 0.1 1688 DD5ZZ W1QA R 569 MA
+190106_000315 7.080 Rx FT8 4 0.7 1798 N3KCR WS4WW 73
+190106_000315 7.080 Rx FT8 16 0.1 1895 CQ RU KG4W FM17
+190106_000315 7.080 Rx FT8 -11 0.2 2125 CQ RU NF3R FN20
+190106_000330 7.080 Rx FT8 -8 0.1 1170 K1JT K8GNG RR73
+190106_000330 7.080 Rx FT8 -13 -0.1 454 NZ7P KM7S R 549 CA
+190106_000330 7.080 Rx FT8 1 0.1 941 KW4RTR KO9V 559 NC
+190106_000330 7.080 Rx FT8 -16 0.3 1034 W9JA W9OY 539 FL
+190106_000330 7.080 Rx FT8 -2 0.1 1240 K2DH KD2GXL 559 NY
+190106_000330 7.080 Rx FT8 -5 0.1 1536 W9WLX W0FRC 73
+190106_000330 7.080 Rx FT8 -2 -0.1 918 KF6JXM W5BT 529 TX
+File 8
+190106_000345 7.080 Rx FT8 -5 0.1 335 CQ RU AB5XS EM12
+190106_000345 7.080 Rx FT8 -13 0.1 423 WA1T PY2APK 529 0081
+190106_000345 7.080 Rx FT8 6 0.2 689 CQ RU KA1YQC FN42
+190106_000345 7.080 Rx FT8 -16 0.1 852 W9TO KM4LFT RR73
+190106_000345 7.080 Rx FT8 -4 -0.1 918 KF6JXM W5BT 529 TX
+190106_000345 7.080 Rx FT8 2 0.1 984 W4EMB N9OY R 529 WI
+190106_000345 7.080 Rx FT8 -15 0.3 1034 W9JA W9OY RR73
+190106_000345 7.080 Rx FT8 -7 0.1 1175 K2DH KD2GXL 559 NY
+190106_000345 7.080 Rx FT8 6 -0.1 1240 CQ RU K7RL CN88
+190106_000345 7.080 Rx FT8 -10 0.1 1536 CQ RU W0FRC DM79
+190106_000345 7.080 Rx FT8 -7 0.2 1581 VA3WW WA4DYD RR73
+190106_000345 7.080 Rx FT8 -9 0.1 1688 DD5ZZ W1QA 73
+190106_000345 7.080 Rx FT8 17 0.1 1895 CQ RU KG4W FM17
+190106_000345 7.080 Rx FT8 -9 0.1 2002 K2DH KN3ILZ 549 PA
+190106_000400 7.080 Rx FT8 -5 0.7 2052 CQ RU WS4WW FM17
+190106_000400 7.080 Rx FT8 -9 0.2 2125 NV4G NF3R R 549 PA
+190106_000400 7.080 Rx FT8 -5 0.0 941 KW4RTR KO9V RR73
+190106_000400 7.080 Rx FT8 -7 0.2 1744 KC1HTT K1LOG R 569 ME
diff --git a/lib/ft4/subtractft4.f90 b/lib/ft4/subtractft4.f90
new file mode 100644
index 00000000..83470d7e
--- /dev/null
+++ b/lib/ft4/subtractft4.f90
@@ -0,0 +1,66 @@
+subroutine subtractft4(dd,itone,f0,dt)
+
+! Subtract an ft4 signal
+!
+! Measured signal : dd(t) = a(t)cos(2*pi*f0*t+theta(t))
+! Reference signal : cref(t) = exp( j*(2*pi*f0*t+phi(t)) )
+! Complex amp : cfilt(t) = LPF[ dd(t)*CONJG(cref(t)) ]
+! Subtract : dd(t) = dd(t) - 2*REAL{cref*cfilt}
+
+ use timer_module, only: timer
+
+ parameter (NMAX=18*3456,NFRAME=(103+2)*512)
+ parameter (NFFT=NMAX,NFILT=1400)
+ real*4 dd(NMAX), window(-NFILT/2:NFILT/2), xjunk
+ complex cref,camp,cfilt,cw
+ integer itone(103)
+ logical first
+ data first/.true./
+ common/heap8/cref(NFRAME),camp(NMAX),cfilt(NMAX),cw(NMAX),xjunk(NFRAME)
+ save first
+
+ nstart=dt*12000+1-512
+ nsym=103
+ nsps=512
+ fs=12000.0
+ icmplx=1
+ bt=1.0
+ call gen_ft4wave(itone,nsym,nsps,fs,f0,cref,xjunk,icmplx,NFRAME)
+ camp=0.
+ do i=1,nframe
+ id=nstart-1+i
+ if(id.ge.1.and.id.le.NMAX) camp(i)=dd(id)*conjg(cref(i))
+ enddo
+
+ if(first) then
+! Create and normalize the filter
+ pi=4.0*atan(1.0)
+ fac=1.0/float(nfft)
+ sum=0.0
+ do j=-NFILT/2,NFILT/2
+ window(j)=cos(pi*j/NFILT)**2
+ sum=sum+window(j)
+ enddo
+ cw=0.
+ cw(1:NFILT+1)=window/sum
+ cw=cshift(cw,NFILT/2+1)
+ call four2a(cw,nfft,1,-1,1)
+ cw=cw*fac
+ first=.false.
+ endif
+
+ cfilt=0.0
+ cfilt(1:nframe)=camp(1:nframe)
+ call four2a(cfilt,nfft,1,-1,1)
+ cfilt(1:nfft)=cfilt(1:nfft)*cw(1:nfft)
+ call four2a(cfilt,nfft,1,1,1)
+
+! Subtract the reconstructed signal
+ do i=1,nframe
+ j=nstart+i-1
+ if(j.ge.1 .and. j.le.NMAX) dd(j)=dd(j)-2*REAL(cfilt(i)*cref(i))
+ enddo
+
+ return
+end subroutine subtractft4
+
diff --git a/lib/ft4/sync4d.f90 b/lib/ft4/sync4d.f90
new file mode 100644
index 00000000..bda7ed4a
--- /dev/null
+++ b/lib/ft4/sync4d.f90
@@ -0,0 +1,77 @@
+subroutine sync4d(cd0,i0,ctwk,itwk,sync)
+
+! Compute sync power for a complex, downsampled FT4 signal.
+
+ include 'ft4_params.f90'
+ parameter(NP=NMAX/NDOWN,NSS=NSPS/NDOWN)
+ complex cd0(0:NP-1)
+ complex csynca(2*NSS),csyncb(2*NSS),csyncc(2*NSS),csyncd(2*NSS)
+ complex csync2(2*NSS)
+ complex ctwk(2*NSS)
+ complex z1,z2,z3,z4
+ complex zz1,zz2,zz3,zz4
+ logical first
+ integer icos4a(0:3),icos4b(0:3),icos4c(0:3),icos4d(0:3)
+ data icos4a/0,1,3,2/
+ data icos4b/1,0,2,3/
+ data icos4c/2,3,1,0/
+ data icos4d/3,2,0,1/
+ data first/.true./
+ save first,twopi,csynca,csyncb,csyncc,csyncd,fac
+
+ p(z1)=real(z1*fac)**2 + aimag(z1*fac)**2 !Statement function for power
+
+ if( first ) then
+ twopi=8.0*atan(1.0)
+ k=1
+ phia=0.0
+ phib=0.0
+ phic=0.0
+ phid=0.0
+ do i=0,3
+ dphia=2*twopi*icos4a(i)/real(NSS)
+ dphib=2*twopi*icos4b(i)/real(NSS)
+ dphic=2*twopi*icos4c(i)/real(NSS)
+ dphid=2*twopi*icos4d(i)/real(NSS)
+ do j=1,NSS/2
+ csynca(k)=cmplx(cos(phia),sin(phia))
+ csyncb(k)=cmplx(cos(phib),sin(phib))
+ csyncc(k)=cmplx(cos(phic),sin(phic))
+ csyncd(k)=cmplx(cos(phid),sin(phid))
+ phia=mod(phia+dphia,twopi)
+ phib=mod(phib+dphib,twopi)
+ phic=mod(phic+dphic,twopi)
+ phid=mod(phid+dphid,twopi)
+ k=k+1
+ enddo
+ enddo
+ first=.false.
+ fac=1.0/(2.0*NSS)
+ endif
+
+ i1=i0 !four Costas arrays
+ i2=i0+33*NSS
+ i3=i0+66*NSS
+ i4=i0+99*NSS
+
+ z1=0.
+ z2=0.
+ z3=0.
+ z4=0.
+
+ if(itwk.eq.1) csync2=ctwk*csynca !Tweak the frequency
+ if(i1.ge.0 .and. i1+4*NSS-1.le.NP-1) z1=sum(cd0(i1:i1+4*NSS-1:2)*conjg(csync2))
+
+ if(itwk.eq.1) csync2=ctwk*csyncb !Tweak the frequency
+ if(i2.ge.0 .and. i2+4*NSS-1.le.NP-1) z2=sum(cd0(i2:i2+4*NSS-1:2)*conjg(csync2))
+
+ if(itwk.eq.1) csync2=ctwk*csyncc !Tweak the frequency
+ if(i3.ge.0 .and. i3+4*NSS-1.le.NP-1) z3=sum(cd0(i3:i3+4*NSS-1:2)*conjg(csync2))
+
+ if(itwk.eq.1) csync2=ctwk*csyncd !Tweak the frequency
+ if(i4.ge.0 .and. i4+4*NSS-1.le.NP-1) z4=sum(cd0(i4:i4+4*NSS-1:2)*conjg(csync2))
+
+ sync = p(z1) + p(z2) + p(z3) + p(z4)
+
+ return
+end subroutine sync4d
diff --git a/lib/ft4/syncft4.f90 b/lib/ft4/syncft4.f90
new file mode 100644
index 00000000..4841c560
--- /dev/null
+++ b/lib/ft4/syncft4.f90
@@ -0,0 +1,145 @@
+subroutine syncft4(iwave,nfa,nfb,syncmin,nfqso,maxcand,s,candidate, &
+ ncand,sbase)
+
+ include 'ft4_params.f90'
+! Search over +/- 2.5s relative to 0.5s TX start time.
+ parameter (JZ=20)
+ complex cx(0:NH1)
+ real s(NH1,NHSYM)
+ real savg(NH1)
+ real sbase(NH1)
+ real x(NFFT1)
+ real sync2d(NH1,-JZ:JZ)
+ real red(NH1)
+ real candidate0(3,maxcand)
+ real candidate(3,maxcand)
+ real dd(NMAX)
+ integer jpeak(NH1)
+ integer indx(NH1)
+ integer ii(1)
+ integer*2 iwave(NMAX)
+ integer icos4(0:3)
+ data icos4/0,1,3,2/ !Costas 4x4 tone pattern
+ equivalence (x,cx)
+
+ dd=iwave/1e3
+! Compute symbol spectra, stepping by NSTEP steps.
+ savg=0.
+ tstep=NSTEP/12000.0
+ df=12000.0/NFFT1
+ fac=1.0/300.0
+ do j=1,NHSYM
+ ia=(j-1)*NSTEP + 1
+ ib=ia+NSPS-1
+ x(1:NSPS)=fac*dd(ia:ib)
+ x(NSPS+1:)=0.
+ call four2a(x,NFFT1,1,-1,0) !r2c FFT
+ do i=1,NH1
+ s(i,j)=real(cx(i))**2 + aimag(cx(i))**2
+ enddo
+ savg=savg + s(1:NH1,j) !Average spectrum
+ enddo
+
+ call baseline(savg,nfa,nfb,sbase)
+
+ ia=max(1,nint(nfa/df))
+ ib=nint(nfb/df)
+ nssy=NSPS/NSTEP ! # steps per symbol
+ nfos=NFFT1/NSPS ! # frequency bin oversampling factor
+ jstrt=0.25/tstep
+ candidate0=0.
+ k=0
+
+ do i=ia,ib
+ do j=-JZ,+JZ
+ ta=0.
+ tb=0.
+ tc=0.
+ t0a=0.
+ t0b=0.
+ t0c=0.
+ do n=0,3
+ m=j+jstrt+nssy*n
+ if(m.ge.1.and.m.le.NHSYM) then
+ ta=ta + s(i+nfos*icos4(n),m)
+ t0a=t0a + sum(s(i:i+nfos*3:nfos,m))
+ endif
+ tb=tb + s(i+nfos*icos4(n),m+nssy*36)
+ t0b=t0b + sum(s(i:i+nfos*3:nfos,m+nssy*36))
+ if(m+nssy*72.le.NHSYM) then
+ tc=tc + s(i+nfos*icos4(n),m+nssy*72)
+ t0c=t0c + sum(s(i:i+nfos*3:nfos,m+nssy*72))
+ endif
+ enddo
+ t=ta+tb+tc
+ t0=t0a+t0b+t0c
+ t0=(t0-t)/3.0
+ sync_abc=t/t0
+ t=tb+tc
+ t0=t0b+t0c
+ t0=(t0-t)/3.0
+ sync_bc=t/t0
+ sync2d(i,j)=max(sync_abc,sync_bc)
+ enddo
+ enddo
+
+ red=0.
+ do i=ia,ib
+ ii=maxloc(sync2d(i,-JZ:JZ)) - 1 - JZ
+ j0=ii(1)
+ jpeak(i)=j0
+ red(i)=sync2d(i,j0)
+ enddo
+ iz=ib-ia+1
+ call indexx(red(ia:ib),iz,indx)
+ ibase=indx(nint(0.40*iz)) - 1 + ia
+ if(ibase.lt.1) ibase=1
+ if(ibase.gt.nh1) ibase=nh1
+ base=red(ibase)
+ red=red/base
+ do i=1,min(maxcand,iz)
+ n=ia + indx(iz+1-i) - 1
+ if(red(n).lt.syncmin.or.isnan(red(n)).or.k.eq.maxcand) exit
+ k=k+1
+! candidate0(1,k)=n*df+37.5*1.5
+ candidate0(1,k)=n*df
+ candidate0(2,k)=(jpeak(n)-1)*tstep
+ candidate0(3,k)=red(n)
+ enddo
+ ncand=k
+
+! Put nfqso at top of list, and save only the best of near-dupe freqs.
+ do i=1,ncand
+ if(abs(candidate0(1,i)-nfqso).lt.10.0) candidate0(1,i)=-candidate0(1,i)
+ if(i.ge.2) then
+ do j=1,i-1
+ fdiff=abs(candidate0(1,i))-abs(candidate0(1,j))
+ if(abs(fdiff).lt.4.0) then
+ if(candidate0(3,i).ge.candidate0(3,j)) candidate0(3,j)=0.
+ if(candidate0(3,i).lt.candidate0(3,j)) candidate0(3,i)=0.
+ endif
+ enddo
+ endif
+ enddo
+
+ fac=20.0/maxval(s)
+ s=fac*s
+
+! Sort by sync
+! call indexx(candidate0(3,1:ncand),ncand,indx)
+! Sort by frequency
+ call indexx(candidate0(1,1:ncand),ncand,indx)
+ k=1
+! do i=ncand,1,-1
+ do i=1,ncand
+ j=indx(i)
+! if( candidate0(3,j) .ge. syncmin .and. candidate0(2,j).ge.-1.5 ) then
+ if( candidate0(3,j) .ge. syncmin ) then
+ candidate(2:3,k)=candidate0(2:3,j)
+ candidate(1,k)=abs(candidate0(1,j))
+ k=k+1
+ endif
+ enddo
+ ncand=k-1
+ return
+end subroutine syncft4