summaryrefslogtreecommitdiff
path: root/lib/qra
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2026-10-09 14:12:35 +0200
committerChristoph Berg <myon@debian.org>2026-10-09 14:12:35 +0200
commitffc62c792c9a5196cb4ce7a353287dfe20db6524 (patch)
tree1269efa852935b183daae4bb33a58d1b4d2ffb53 /lib/qra
parent059831534c7e884f0a607ed86b582fd00d821731 (diff)
New upstream version 3.3.0~beta1+dfsgupstream/3.3.0_beta1+dfsg
Diffstat (limited to 'lib/qra')
-rw-r--r--lib/qra/q65/genq65.f9022
-rw-r--r--lib/qra/q65/q65.f90599
-rw-r--r--lib/qra/q65/q65.h5
-rw-r--r--lib/qra/q65/q65_callers.F90142
-rw-r--r--lib/qra/q65/q65_limits.h8
-rw-r--r--lib/qra/q65/q65_loops.f904
-rw-r--r--lib/qra/q65/q65_set_list2.f9017
-rw-r--r--lib/qra/q65/q65_subs.c223
-rw-r--r--lib/qra/q65/q65_workspace.f9042
9 files changed, 784 insertions, 278 deletions
diff --git a/lib/qra/q65/genq65.f90 b/lib/qra/q65/genq65.f90
index 1705ce82..4502a2b3 100644
--- a/lib/qra/q65/genq65.f90
+++ b/lib/qra/q65/genq65.f90
@@ -3,6 +3,8 @@ subroutine genq65(msg0,ichk,msgsent,itone,i3,n3,iflag)
! Encodes a Q65 message to yield itone(1:85)
use packjt77
+ use q65, only: q65_work
+ use q65_codec, only: q65_enc
character*37 msg0 !Message to be generated
character*37 msgsent !Message as it will be received
character*77 c77
@@ -27,11 +29,27 @@ subroutine genq65(msg0,ichk,msgsent,itone,i3,n3,iflag)
n3=-1
c77=' '
unpk77_success=.false.
- call pack77_legacy_truncating_fallback(msg0,i3,n3,c77)
+ if(associated(q65_work)) then
+ if(associated(q65_work%knowledge)) then
+ call pack77_legacy_truncating_fallback_for_state(q65_work%knowledge,msg0,i3,n3,c77)
+ else
+ call pack77_legacy_truncating_fallback(msg0,i3,n3,c77)
+ endif
+ else
+ call pack77_legacy_truncating_fallback(msg0,i3,n3,c77)
+ endif
if(i3.lt.0 .or. n3.lt.0) go to 998
read(c77(60:74),'(b15)',err=998) ng15
if(ng15.eq.32373) c77(60:74)='111111010010011' !Message is RR73
- call unpack77(c77,0,msgsent,unpk77_success) !Unpack to get msgsent
+ if(associated(q65_work)) then
+ if(associated(q65_work%knowledge)) then
+ call unpack77_for_state(q65_work%knowledge,c77,0,msgsent,unpk77_success)
+ else
+ call unpack77(c77,0,msgsent,unpk77_success)
+ endif
+ else
+ call unpack77(c77,0,msgsent,unpk77_success)
+ endif !Unpack to get msgsent
read(c77,1001,err=998) dgen
1001 format(12b6.6,b5.5)
dgen(13)=2*dgen(13)+iflag !Convert 77-bit to 78-bit payload; iflag is the spare 78th bit
diff --git a/lib/qra/q65/q65.f90 b/lib/qra/q65/q65.f90
index 5718243c..37c2e638 100644
--- a/lib/qra/q65/q65.f90
+++ b/lib/qra/q65/q65.f90
@@ -1,32 +1,311 @@
module q65
- parameter (NSTEP=8) !Number of time bins per symbol in s1, s1a, s1b
- parameter (PLOG_MIN=-242.0) !List decoding threshold
- integer iz0,jz0
-! integer listutc(10)
- integer apsym0(58),aph10(10)
- integer apmask1(78),apsymbols1(78)
- integer apmask(13),apsymbols(13)
- integer,dimension(22) :: isync = (/1,9,12,13,15,22,23,26,27,33,35, &
- 38,46,50,55,60,62,66,69,74,76,85/)
- integer codewords(63,411) !See MAX_NCW in q65_set_list2.f90
- integer ibwa,ibwb,ncw,nsps,mode_q65,nfa,nfb,nqd
- integer idfbest,idtbest,ibw,ndistbest,maxiters,max_drift
- integer istep,nsmo,lag1,lag2,npasses,iseq,ncand,nrc
- integer i0,j0
- integer navg(0:1)
- logical lnewdat
- real candidates(20,3) !snr, xdt, and f0 of top candidates
- real, allocatable :: s1(:,:) !Symbol spectra w/suppressed peaks
- real, allocatable :: s1w(:,:) !Symbol spectra w/suppressed peaks (W3SZ)
- real, allocatable,save :: s1a(:,:,:) !Cumulative symbol spectra
- real, allocatable,save :: ccf2(:) !Max CCF(freq) at any lag (orange curve)
- real, allocatable,save :: ccf2_avg(:) !Like ccf2, but for avg (red curve)
- real sync(85) !sync vector
- real df,dtstep,dtdec,f0dec,ftol,plog,drift
+ use q65_callers, only: Q65_MAX_CODEWORDS
+
+ use, intrinsic :: iso_c_binding
+ use fftw3
+ use packjt77, only: pack77_state,unpack77_for_state
+ use q65_codec, only: q65_enc,q65_intrinsics_ff,q65_dec,q65_dec_fullaplist
+
+ integer, parameter :: NSTEP=8
+ real, parameter :: PLOG_MIN=-242.0
+ integer, parameter :: isync(22) = [1,9,12,13,15,22,23,26,27,33,35, &
+ 38,46,50,55,60,62,66,69,74,76,85]
+ type :: q65_state
+ integer :: iz0=0
+ integer :: jz0=0
+ integer :: ibwa=0
+ integer :: ibwb=0
+ integer :: ncw=0
+ integer :: nsps=0
+ integer :: mode_q65=0
+ integer :: nfa=0
+ integer :: nfb=0
+ integer :: nqd=0
+ integer :: idfbest=0
+ integer :: idtbest=0
+ integer :: ibw=0
+ integer :: ndistbest=0
+ integer :: maxiters=0
+ integer :: max_drift=0
+ integer :: istep=0
+ integer :: nsmo=0
+ integer :: lag1=0
+ integer :: lag2=0
+ integer :: npasses=0
+ integer :: iseq=0
+ integer :: ncand=0
+ integer :: nrc=0
+ integer :: i0=0
+ integer :: j0=0
+ integer :: LL0=0
+ integer :: nhist=0,curve_average_count=0
+ real :: df=0.
+ real :: dtstep=0.
+ real :: dtdec=0.
+ real :: f0dec=0.
+ real :: ftol=0.
+ real :: plog=0.
+ real :: drift=0.
+ real :: curve_dt=0.
+ integer :: apsym0(58)
+ integer :: aph10(10)
+ integer :: apmask1(78)
+ integer :: apsymbols1(78)
+ integer :: apmask(13)
+ integer :: apsymbols(13)
+ integer :: codewords(63,Q65_MAX_CODEWORDS)
+ integer :: navg(0:1)
+ integer :: nf0(100)
+ real :: candidates(20,3)
+ real :: sync(85)
+ real, pointer :: s1(:,:)=>null()
+ real, pointer :: s1w(:,:)=>null()
+ real, pointer :: s1a(:,:,:)=>null()
+ real, pointer :: ccf2(:)=>null()
+ real, pointer :: ccf2_avg(:)=>null()
+ logical :: lnewdat=.false.,spectra_valid=.false.,analytic_valid=.false.,legacy_output=.true.
+ character(len=37) :: history(100)
+ complex, allocatable :: symbol_fft(:),analytic(:)
+ real, allocatable :: prepared(:,:),symbol_energies(:,:),correlation(:),list_correlation(:,:),best_list(:)
+ real, allocatable :: timing(:),spectrum_average(:),snr_spectrum(:)
+ integer, allocatable :: ordering(:),birdie_histogram(:)
+ type(pack77_state), pointer :: knowledge=>null()
+ integer(c_int64_t) :: symbol_plan=0,analytic_forward=0,analytic_inverse=0
+ integer :: symbol_length=0,analytic_length=0,planning=-1
+ end type
+ type(q65_state), pointer :: q65_work=>null()
+ type(q65_state), target, save :: legacy_work
+ integer, pointer :: iz0=>null()
+ integer, pointer :: jz0=>null()
+ integer, pointer :: ibwa=>null()
+ integer, pointer :: ibwb=>null()
+ integer, pointer :: ncw=>null()
+ integer, pointer :: nsps=>null()
+ integer, pointer :: mode_q65=>null()
+ integer, pointer :: nfa=>null()
+ integer, pointer :: nfb=>null()
+ integer, pointer :: nqd=>null()
+ integer, pointer :: idfbest=>null()
+ integer, pointer :: idtbest=>null()
+ integer, pointer :: ibw=>null()
+ integer, pointer :: ndistbest=>null()
+ integer, pointer :: maxiters=>null()
+ integer, pointer :: max_drift=>null()
+ integer, pointer :: istep=>null()
+ integer, pointer :: nsmo=>null()
+ integer, pointer :: lag1=>null()
+ integer, pointer :: lag2=>null()
+ integer, pointer :: npasses=>null()
+ integer, pointer :: iseq=>null()
+ integer, pointer :: ncand=>null()
+ integer, pointer :: nrc=>null()
+ integer, pointer :: i0=>null()
+ integer, pointer :: j0=>null()
+ integer, pointer :: LL0=>null()
+ integer, pointer :: nhist=>null()
+ real, pointer :: df=>null()
+ real, pointer :: dtstep=>null()
+ real, pointer :: dtdec=>null()
+ real, pointer :: f0dec=>null()
+ real, pointer :: ftol=>null()
+ real, pointer :: plog=>null()
+ real, pointer :: drift=>null()
+ real, pointer :: curve_dt=>null()
+ integer, pointer :: apsym0(:)=>null()
+ integer, pointer :: aph10(:)=>null()
+ integer, pointer :: apmask1(:)=>null()
+ integer, pointer :: apsymbols1(:)=>null()
+ integer, pointer :: apmask(:)=>null()
+ integer, pointer :: apsymbols(:)=>null()
+ integer, pointer :: codewords(:,:)=>null()
+ integer, pointer :: navg(:)=>null()
+ integer, pointer :: nf0(:)=>null()
+ real, pointer :: candidates(:,:)=>null()
+ real, pointer :: sync(:)=>null()
+ real, pointer :: s1(:,:)=>null()
+ real, pointer :: s1w(:,:)=>null()
+ real, pointer :: s1a(:,:,:)=>null()
+ real, pointer :: ccf2(:)=>null()
+ real, pointer :: ccf2_avg(:)=>null()
+ logical, pointer :: lnewdat=>null()
contains
+
+integer function q65_fft_flags() result(flags)
+ integer npatience,nthreads
+ common/patience/npatience,nthreads
+ flags=FFTW_ESTIMATE
+ if(npatience==1) flags=FFTW_ESTIMATE_PATIENT
+ if(npatience==2) flags=FFTW_MEASURE
+ if(npatience==3) flags=FFTW_PATIENT
+ if(npatience==4) flags=FFTW_EXHAUSTIVE
+end function
+
+subroutine q65_check_planning()
+ integer npatience,nthreads
+ common/patience/npatience,nthreads
+ if(q65_work%planning==npatience) return
+ !$omp critical(fftw)
+ if(q65_work%symbol_plan/=0) call sfftw_destroy_plan(q65_work%symbol_plan)
+ if(q65_work%analytic_forward/=0) call sfftw_destroy_plan(q65_work%analytic_forward)
+ if(q65_work%analytic_inverse/=0) call sfftw_destroy_plan(q65_work%analytic_inverse)
+ !$omp end critical(fftw)
+ q65_work%symbol_plan=0
+ q65_work%analytic_forward=0
+ q65_work%analytic_inverse=0
+ q65_work%planning=npatience
+ q65_work%spectra_valid=.false.
+ q65_work%analytic_valid=.false.
+end subroutine
+
+subroutine q65_analytic(iwave,npts)
+ integer, intent(in) :: npts
+ integer(c_int16_t), intent(in) :: iwave(npts)
+ integer nfft2,flags
+ real fac
+ call q65_check_planning()
+ if(q65_work%analytic_length/=npts) then
+ !$omp critical(fftw)
+ if(q65_work%analytic_forward/=0) call sfftw_destroy_plan(q65_work%analytic_forward)
+ if(q65_work%analytic_inverse/=0) call sfftw_destroy_plan(q65_work%analytic_inverse)
+ !$omp end critical(fftw)
+ q65_work%analytic_forward=0
+ q65_work%analytic_inverse=0
+ if(allocated(q65_work%analytic)) deallocate(q65_work%analytic)
+ allocate(q65_work%analytic(0:npts-1))
+ q65_work%analytic_length=npts
+ q65_work%analytic_valid=.false.
+ endif
+ if(q65_work%analytic_valid) return
+ nfft2=npts/2
+ flags=q65_fft_flags()
+ if(q65_work%analytic_forward==0) then
+ !$omp critical(fftw)
+ call sfftw_plan_dft_1d(q65_work%analytic_forward,npts,q65_work%analytic, &
+ q65_work%analytic,FFTW_FORWARD,flags)
+ call sfftw_plan_dft_1d(q65_work%analytic_inverse,nfft2,q65_work%analytic, &
+ q65_work%analytic,FFTW_BACKWARD,flags)
+ !$omp end critical(fftw)
+ endif
+ fac=2.0/(32767.0*npts)
+ q65_work%analytic=fac*iwave
+ call sfftw_execute(q65_work%analytic_forward)
+ q65_work%analytic(nfft2/2+1:nfft2-1)=0.
+ q65_work%analytic(0)=0.5*q65_work%analytic(0)
+ call sfftw_execute(q65_work%analytic_inverse)
+ q65_work%analytic_valid=.true.
+end subroutine
+
+subroutine q65_unpack(c77,nrx,decoded,success)
+ use packjt77, only: unpack77
+ character(len=77), intent(in) :: c77
+ integer, intent(in) :: nrx
+ character(len=37), intent(out) :: decoded
+ logical, intent(out) :: success
+ if(associated(q65_work)) then
+ if(associated(q65_work%knowledge)) then
+ call unpack77_for_state(q65_work%knowledge,c77,nrx,decoded,success)
+ return
+ endif
+ endif
+ call unpack77(c77,nrx,decoded,success)
+end subroutine
+
+subroutine q65_select(state)
+ type(q65_state), target, intent(inout) :: state
+ q65_work=>state
+ iz0=>state%iz0
+ jz0=>state%jz0
+ ibwa=>state%ibwa
+ ibwb=>state%ibwb
+ ncw=>state%ncw
+ nsps=>state%nsps
+ mode_q65=>state%mode_q65
+ nfa=>state%nfa
+ nfb=>state%nfb
+ nqd=>state%nqd
+ idfbest=>state%idfbest
+ idtbest=>state%idtbest
+ ibw=>state%ibw
+ ndistbest=>state%ndistbest
+ maxiters=>state%maxiters
+ max_drift=>state%max_drift
+ istep=>state%istep
+ nsmo=>state%nsmo
+ lag1=>state%lag1
+ lag2=>state%lag2
+ npasses=>state%npasses
+ iseq=>state%iseq
+ ncand=>state%ncand
+ nrc=>state%nrc
+ i0=>state%i0
+ j0=>state%j0
+ LL0=>state%LL0
+ nhist=>state%nhist
+ df=>state%df
+ dtstep=>state%dtstep
+ dtdec=>state%dtdec
+ f0dec=>state%f0dec
+ ftol=>state%ftol
+ plog=>state%plog
+ drift=>state%drift
+ curve_dt=>state%curve_dt
+ apsym0=>state%apsym0
+ aph10=>state%aph10
+ apmask1=>state%apmask1
+ apsymbols1=>state%apsymbols1
+ apmask=>state%apmask
+ apsymbols=>state%apsymbols
+ codewords=>state%codewords
+ navg=>state%navg
+ nf0=>state%nf0
+ candidates=>state%candidates
+ sync=>state%sync
+ s1=>state%s1
+ s1w=>state%s1w
+ s1a=>state%s1a
+ ccf2=>state%ccf2
+ ccf2_avg=>state%ccf2_avg
+ lnewdat=>state%lnewdat
+end subroutine
+
+subroutine q65_initialize_state(state)
+ type(q65_state), intent(inout) :: state
+ state%apsym0=0
+ state%aph10=0
+ state%apmask1=0
+ state%apsymbols1=0
+ state%apmask=0
+ state%apsymbols=0
+ state%codewords=0
+ state%navg=0
+ state%nf0=0
+ state%history=''
+ state%candidates=0.
+ state%sync=-22.0/63.0
+ state%sync(isync)=1.
+end subroutine
+
+subroutine q65_release_state(state)
+ type(q65_state), intent(inout) :: state
+ !$omp critical(fftw)
+ if(state%symbol_plan/=0) call sfftw_destroy_plan(state%symbol_plan)
+ if(state%analytic_forward/=0) call sfftw_destroy_plan(state%analytic_forward)
+ if(state%analytic_inverse/=0) call sfftw_destroy_plan(state%analytic_inverse)
+ !$omp end critical(fftw)
+ state%symbol_plan=0
+ state%analytic_forward=0
+ state%analytic_inverse=0
+ if(associated(state%s1)) deallocate(state%s1)
+ if(associated(state%s1w)) deallocate(state%s1w)
+ if(associated(state%s1a)) deallocate(state%s1a)
+ if(associated(state%ccf2)) deallocate(state%ccf2)
+ if(associated(state%ccf2_avg)) deallocate(state%ccf2_avg)
+end subroutine
+
+
subroutine q65_dec0(iavg,iwave,ntrperiod,nfqso,ntol,lclearave, &
emedelay,xdt,f0,snr1,width,dat4,snr2,idec,stageno)
@@ -62,11 +341,8 @@ subroutine q65_dec0(iavg,iwave,ntrperiod,nfqso,ntol,lclearave, &
integer*2 iwave(0:12000*ntrperiod-1) !Raw data
integer dat4(13)
character*37 decoded
- logical first,lclearave
- real, allocatable :: s3(:,:) !Data-symbol energies s3(LL,63)
- real, allocatable :: ccf1(:) !CCF(freq) at fixed lag (red)
- data first/.true./
- save first,LL0
+ logical lclearave
+ real, pointer :: s3(:,:),ccf1(:)
integer w3t
integer w3f
@@ -86,32 +362,34 @@ subroutine q65_dec0(iavg,iwave,ntrperiod,nfqso,ntol,lclearave, &
df=12000.0/nfft !Freq resolution = baud
istep=nsps/NSTEP
iz=5000.0/df !Uppermost frequency bin, at 5000 Hz
- txt=85.0*nsps/12000.0
- jz=(txt+1.0)*12000.0/istep !Number of symbol/NSTEP bins
- if(nsps.ge.6912) jz=(txt+2.0)*12000.0/istep !For TR 60 s and higher
+ ! Cover every complete symbol FFT window in the received period.
+ ! Keep jz odd: q65_symspec interpolates even columns between odd ones.
+ jz=(ntrperiod*12000-nsps)/istep + 1
ftol=ntol
ia=ntol/df
ia2=max(ia,10*mode_q65,nint(100.0/df))
! nsmo=int(0.7*mode_q65*mode_q65)
nsmo=int(0.5*mode_q65*mode_q65)
if(nsmo.lt.1) nsmo=1
- if(first) then !Generate the sync vector
- sync=-22.0/63.0 !Sync tone OFF
- do k=1,22
- sync(isync(k))=1.0 !Sync tone ON
- enddo
- endif
- allocate(s3(-64:LL-65,63))
- allocate(ccf1(-ia2:ia2))
+ if(allocated(q65_work%symbol_energies)) then
+ if(size(q65_work%symbol_energies,1)/=LL) deallocate(q65_work%symbol_energies)
+ endif
+ if(.not.allocated(q65_work%symbol_energies)) allocate(q65_work%symbol_energies(-64:LL-65,63))
+ if(allocated(q65_work%correlation)) then
+ if(size(q65_work%correlation)/=2*ia2+1) deallocate(q65_work%correlation)
+ endif
+ if(.not.allocated(q65_work%correlation)) allocate(q65_work%correlation(-ia2:ia2))
+ s3=>q65_work%symbol_energies
+ ccf1=>q65_work%correlation
if(LL.ne.LL0 .or. iz.ne.iz0 .or. jz.ne.jz0 .or. lclearave) then
- if(allocated(s1)) deallocate(s1)
+ if(associated(s1)) deallocate(s1)
allocate(s1(iz,jz))
- if(allocated(s1a)) deallocate(s1a)
+ if(associated(s1a)) deallocate(s1a)
allocate(s1a(iz,jz,0:1))
- if(allocated(ccf2)) deallocate(ccf2)
+ if(associated(ccf2)) deallocate(ccf2)
allocate(ccf2(iz))
- if(allocated(ccf2_avg)) deallocate(ccf2_avg)
+ if(associated(ccf2_avg)) deallocate(ccf2_avg)
allocate(ccf2_avg(iz))
s1=0.
s1a=0.
@@ -119,6 +397,13 @@ subroutine q65_dec0(iavg,iwave,ntrperiod,nfqso,ntol,lclearave, &
LL0=LL
iz0=iz
jz0=jz
+ q65_work%s1=>s1
+ q65_work%s1a=>s1a
+ q65_work%ccf2=>ccf2
+ q65_work%ccf2_avg=>ccf2_avg
+ ccf2=0.
+ ccf2_avg=0.
+ q65_work%spectra_valid=.false.
lclearave=.false.
endif
ccf1=0.
@@ -133,10 +418,21 @@ subroutine q65_dec0(iavg,iwave,ntrperiod,nfqso,ntol,lclearave, &
s3=0.
! if(iavg.eq.0 .and. lnewdat) then
+ call q65_check_planning()
if(iavg.eq.0) then
call timer('q65_syms',0)
! Compute symbol spectra with NSTEP time bins per symbol
- call q65_symspec(iwave,ntrperiod*12000,iz,jz,s1)
+ if(.not.q65_work%spectra_valid) then
+ call q65_symspec(iwave,ntrperiod*12000,iz,jz,s1)
+ if(allocated(q65_work%prepared)) then
+ if(any(shape(q65_work%prepared)/=shape(s1))) deallocate(q65_work%prepared)
+ endif
+ if(.not.allocated(q65_work%prepared)) allocate(q65_work%prepared(iz,jz))
+ q65_work%prepared=s1
+ q65_work%spectra_valid=.true.
+ else
+ s1=q65_work%prepared
+ endif
call timer('q65_syms',1)
! lnewdat=.false.
else
@@ -202,7 +498,11 @@ subroutine q65_dec0(iavg,iwave,ntrperiod,nfqso,ntol,lclearave, &
enddo
width=df*(i2-i1)
if(ncw.eq.0) ccf1=0.
- call q65_write_red(iz,xdt,ccf2_avg,ccf2) !### Need this call for WSJT-X
+ call q65_sync_curve(ccf2_avg,1,iz,rms1)
+ call q65_sync_curve(ccf2,1,iz,rms2)
+ curve_dt=xdt
+ q65_work%curve_average_count=navg(iseq)
+ if(q65_work%legacy_output) call q65_write_red(iz,xdt,ccf2_avg,ccf2)
if(idec.lt.0 .and. (iavg.eq.0 .or. iavg.eq.2)) then
call q65_dec_q012(s3,LL,snr2,dat4,idec,decoded)
@@ -210,13 +510,17 @@ subroutine q65_dec0(iavg,iwave,ntrperiod,nfqso,ntol,lclearave, &
if(idec.lt.0 .and. max_drift.eq.50 .and. stageno.eq.5) then
- if(allocated(s1w)) deallocate(s1w) ! w3sz
- allocate(s1w(iz,jz)) ! w3sz
+ if(associated(s1w)) then
+ if(any(shape(s1w)/=[iz,jz])) deallocate(s1w)
+ endif
+ if(.not.associated(s1w)) allocate(s1w(iz,jz))
+ q65_work%s1w=>s1w
s1w=s1
do w3t=1,jz
do w3f=1,iz
- mm=w3f + nint(drift*w3t/(jz*df))
+ ! Drift is measured over the 85-symbol transmission.
+ mm=w3f + nint(drift*w3t/(85*NSTEP*df))
if(mm.ge.1 .and. mm.le.iz) then
s1w(w3f,w3t)=s1(mm,w3t)
endif
@@ -256,7 +560,7 @@ subroutine q65_clravg
! Clear the averaging array to start a new average.
- if(allocated(s1a)) s1a(:,:,iseq)=0.
+ if(associated(s1a)) s1a(:,:,iseq)=0.
navg(iseq)=0
return
@@ -268,23 +572,36 @@ subroutine q65_symspec(iwave,nmax,iz,jz,s1)
integer*2 iwave(0:nmax-1) !Raw data
real s1(iz,jz)
- complex c0(0:41472) !Largest requirement, Q65-300x
- save c0
+ complex, pointer :: c0(:)
+ integer flags
nfft=nsps
+ if(.not.allocated(q65_work%symbol_fft)) allocate(q65_work%symbol_fft(0:41472))
+ c0=>q65_work%symbol_fft
+ call q65_check_planning()
+ if(q65_work%symbol_length/=nfft.or.q65_work%symbol_plan==0) then
+ !$omp critical(fftw)
+ if(q65_work%symbol_plan/=0) call sfftw_destroy_plan(q65_work%symbol_plan)
+ flags=q65_fft_flags()
+ call sfftw_plan_dft_r2c_1d(q65_work%symbol_plan,nfft,c0,c0,flags)
+ !$omp end critical(fftw)
+ q65_work%symbol_length=nfft
+ endif
fac=1/32767.0
do j=1,jz,2 !Compute symbol spectra at 2*step size
i1=(j-1)*istep
i2=i1+nsps-1
k=-1
do i=i1,i2,2 !Load iwave data into complex array c0, for r2c FFT
- xx=iwave(i)
- yy=iwave(i+1)
+ xx=0.
+ yy=0.
+ if(i<nmax) xx=iwave(i)
+ if(i+1<nmax) yy=iwave(i+1)
k=k+1
c0(k)=fac*cmplx(xx,yy)
enddo
c0(k+1:nfft-1)=0.
- call four2a(c0,nfft,1,-1,0) !r2c FFT
+ call sfftw_execute(q65_work%symbol_plan)
do i=1,iz
s1(i,j)=real(c0(i))**2 + aimag(c0(i))**2
enddo
@@ -400,14 +717,18 @@ subroutine q65_ccf_85(s1,iz,jz,nfqso,ia,ia2,ipk,jpk,f0,xdt,imsg_best, &
! attempt at q3 decoding. Return ccf1 for the "red sync curve".
real s1(iz,jz)
- real, allocatable :: ccf(:,:) !CCF(freq,lag)
- real, allocatable :: best(:) !best(imsg) -- for checking 2nd best
+ real, pointer :: ccf(:,:),best(:)
real ccf1(-ia2:ia2)
integer ijpk(2)
integer itone(85)
- allocate(ccf(-ia2:ia2,-53:214))
- allocate(best(ncw))
+ if(allocated(q65_work%list_correlation)) then
+ if(size(q65_work%list_correlation,1)/=2*ia2+1) deallocate(q65_work%list_correlation)
+ endif
+ if(.not.allocated(q65_work%list_correlation)) allocate(q65_work%list_correlation(-ia2:ia2,-53:214))
+ if(.not.allocated(q65_work%best_list)) allocate(q65_work%best_list(Q65_MAX_CODEWORDS))
+ ccf=>q65_work%list_correlation
+ best=>q65_work%best_list(:ncw)
ipk=0
jpk=0
ccf_best=0.
@@ -455,11 +776,10 @@ subroutine q65_ccf_85(s1,iz,jz,nfqso,ia,ia2,ipk,jpk,f0,xdt,imsg_best, &
best(imsg)=ccfmax
enddo ! imsg
- deallocate(ccf)
better=0.
if(imsg_best.gt.0) then
best(imsg_best)=0.
- better=ccf_best/maxval(best)
+ if(maxval(best)>0.) better=ccf_best/maxval(best)
endif
return
@@ -473,13 +793,18 @@ subroutine q65_ccf_22(s1,iz,jz,nfqso,ntol,ipk,jpk,f0,xdt,ccf2)
real s1(iz,jz)
real ccf2(iz) !Orange sync curve
real tmp(20,3)
- real, allocatable :: xdt2(:)
- real, allocatable :: s1avg(:)
- integer, allocatable :: indx(:)
+ real, pointer :: xdt2(:),s1avg(:)
+ integer, pointer :: indx(:)
- allocate(xdt2(iz))
- allocate(s1avg(iz))
- allocate(indx(iz))
+ if(allocated(q65_work%timing)) then
+ if(size(q65_work%timing)<iz) deallocate(q65_work%timing,q65_work%spectrum_average,q65_work%ordering)
+ endif
+ if(.not.allocated(q65_work%timing)) then
+ allocate(q65_work%timing(iz),q65_work%spectrum_average(iz),q65_work%ordering(iz))
+ endif
+ xdt2=>q65_work%timing(:iz)
+ s1avg=>q65_work%spectrum_average(:iz)
+ indx=>q65_work%ordering(:iz)
ia=max(nfa,100)/df
ib=min(nfb,4900)/df
@@ -487,7 +812,20 @@ subroutine q65_ccf_22(s1,iz,jz,nfqso,ntol,ipk,jpk,f0,xdt,ccf2)
ia=max(nint(100/df),nint((nfqso-ntol)/df))
ib=min(nint(4900/df),nint((nfqso+ntol)/df))
endif
- if(ia.ge.ib) ia=ib-ntol/df !Protect against wacky settings
+ if(ia.ge.ib) ia=ib-ntol/df
+ ia=max(1,ia)
+ ib=min(iz,ib)
+ if(ia>ib) then
+ ipk=0
+ jpk=0
+ f0=nfqso
+ xdt=0.
+ drift=0.
+ ncand=0
+ candidates=0.
+ ccf2=0.
+ return
+ endif
do i=ia,ib
s1avg(i)=sum(s1(i,1:jz))
@@ -551,6 +889,8 @@ subroutine q65_ccf_22(s1,iz,jz,nfqso,ntol,ipk,jpk,f0,xdt,ccf2)
call pctile(ccf2(ia:ib),jzz,84,base)
rms=base-ave
ncand=0
+ candidates=0.
+ if(rms<=0.) return
maxcand=20
do j=1,20
k=jzz-j+1
@@ -586,7 +926,7 @@ subroutine q65_dec1(s3,nsubmode,b90ts,esnodb,irc,dat4,decoded)
! Attmpt a full-AP list decode.
use packjt77
- real s3(1,1) !Silence compiler warning that wants to see a 2D array
+ real s3(64*(2+2**nsubmode),63)
real s3prob(0:63,63) !Symbol-value probabilities
integer dat4(13)
character c77*77,decoded*37
@@ -594,13 +934,19 @@ subroutine q65_dec1(s3,nsubmode,b90ts,esnodb,irc,dat4,decoded)
nFadingModel=1
decoded=' '
+ if(ncw<=0.or.maxval(s3)<=0.) then
+ dat4=0
+ esnodb=0.
+ irc=-1
+ return
+ endif
call q65_intrinsics_ff(s3,nsubmode,b90ts,nFadingModel,s3prob)
call q65_dec_fullaplist(s3,s3prob,codewords,ncw,esnodb,dat4,plog,irc)
if(sum(dat4).le.0) irc=-2
if(irc.ge.0 .and. plog.gt.PLOG_MIN) then
write(c77,1000) dat4(1:12),dat4(13)/2
1000 format(12b6.6,b5.5)
- call unpack77(c77,0,decoded,unpk77_success) !Unpack to get msgsent
+ call q65_unpack(c77,0,decoded,unpk77_success) !Unpack to get msgsent
else
irc=-1
endif
@@ -614,7 +960,7 @@ subroutine q65_dec2(s3,nsubmode,b90ts,esnodb,irc,dat4,decoded)
! Attempt a q0, q1, or q2 decode using spcified AP information.
use packjt77
- real s3(iz0,jz0) !Silence compiler warning that wants to see a 2D array
+ real s3(64*(2+2**nsubmode),63)
real s3prob(0:63,63) !Symbol-value probabilities
integer dat4(13)
character c77*77,decoded*37
@@ -622,6 +968,10 @@ subroutine q65_dec2(s3,nsubmode,b90ts,esnodb,irc,dat4,decoded)
nFadingModel=1
decoded=' '
+ dat4=0
+ esnodb=0.
+ irc=-1
+ if(maxval(s3)<=0.) return
call q65_intrinsics_ff(s3,nsubmode,b90ts,nFadingModel,s3prob)
call q65_dec(s3,s3prob,APmask,APsymbols,maxiters,esnodb,dat4,irc)
if(sum(dat4).le.0) irc=-2
@@ -629,7 +979,7 @@ subroutine q65_dec2(s3,nsubmode,b90ts,esnodb,irc,dat4,decoded)
if(irc.ge.0) then
write(c77,1000) dat4(1:12),dat4(13)/2
1000 format(12b6.6,b5.5)
- call unpack77(c77,0,decoded,unpk77_success) !Unpack to get msgsent
+ call q65_unpack(c77,0,decoded,unpk77_success) !Unpack to get msgsent
endif
return
@@ -669,9 +1019,6 @@ subroutine q65_write_red(iz,xdt,ccf2_avg,ccf2)
real ccf2_avg(iz)
real ccf2(iz)
- call q65_sync_curve(ccf2_avg,1,iz,rms1)
- call q65_sync_curve(ccf2,1,iz,rms2)
-
i1=max(1,nint(nfa/df))
i2=min(iz,int(nfb/df))
y0=minval(ccf2(i1:i2))
@@ -720,9 +1067,13 @@ subroutine q65_bzap(s3,LL)
parameter (NBZAP=15)
real s3(-64:LL-65,63)
integer ipk1(1)
- integer, allocatable :: hist(:)
+ integer, pointer :: hist(:)
- allocate(hist(-64:LL-65))
+ if(allocated(q65_work%birdie_histogram)) then
+ if(size(q65_work%birdie_histogram)<LL) deallocate(q65_work%birdie_histogram)
+ endif
+ if(.not.allocated(q65_work%birdie_histogram)) allocate(q65_work%birdie_histogram(-64:LL-65))
+ hist(-64:)=>q65_work%birdie_histogram(-64:LL-65)
hist=0
do j=1,63
ipk1=maxloc(s3(:,j))
@@ -746,9 +1097,13 @@ subroutine q65_snr(dat4,dtdec,f0dec,mode_q65,snr2)
integer dat4(13)
integer codeword(63)
integer itone(85)
- real, allocatable :: spec(:)
+ real, pointer :: spec(:)
- allocate(spec(iz0))
+ if(allocated(q65_work%snr_spectrum)) then
+ if(size(q65_work%snr_spectrum)<iz0) deallocate(q65_work%snr_spectrum)
+ endif
+ if(.not.allocated(q65_work%snr_spectrum)) allocate(q65_work%snr_spectrum(iz0))
+ spec=>q65_work%snr_spectrum(:iz0)
call q65_enc(dat4,codeword)
i=1
k=0
@@ -799,20 +1154,19 @@ subroutine q65_hist(if0,msg0,dxcall,dxgrid)
parameter (MAXHIST=100)
integer,intent(in) :: if0 !Audio freq of decode
character(len=37),intent(in),optional :: msg0 !Decoded message
- character(len=12),intent(out),optional :: dxcall !Second callsign in message
- character(len=6),intent(out),optional :: dxgrid !Third word in msg, if grid
+ character(len=12),intent(inout),optional :: dxcall !Second callsign in message
+ character(len=6),intent(inout),optional :: dxgrid !Third word in msg, if grid
character*6 g1
- character*37 msg(MAXHIST) !Saved messages
- integer nf0(MAXHIST) !Saved frequencies
+ character(len=37), pointer :: msg(:)
logical isgrid !Statement function
- data nhist/0/
- save nhist,nf0,msg
+
isgrid(g1)=g1(1:1).ge.'A' .and. g1(1:1).le.'R' .and. g1(2:2).ge.'A' .and. &
g1(2:2).le.'R' .and. g1(3:3).ge.'0' .and. g1(3:3).le.'9' .and. &
g1(4:4).ge.'0' .and. g1(4:4).le.'9' .and. g1(1:4).ne.'RR73'
+ msg=>q65_work%history
if(present(dxcall)) go to 100 !This is a lookup request
if(nhist.eq.MAXHIST) then
@@ -844,66 +1198,21 @@ subroutine q65_hist(if0,msg0,dxcall,dxgrid)
900 return
end subroutine q65_hist
-subroutine q65_hist2(nfreq,msg0,callers,nhist2)
-
- use types
- use prog_args
- parameter (MAX_CALLERS=50) !For multiple q3 decodes in NA VHF Contest mode
- character*37 msg0,msg
- type(q3list) callers(MAX_CALLERS)
- character*6 c6
- character*4 g4
- logical newcall,isgrid
-
- isgrid(g4)=g4(1:1).ge.'A' .and. g4(1:1).le.'R' .and. g4(2:2).ge.'A' .and. &
- g4(2:2).le.'R' .and. g4(3:3).ge.'0' .and. g4(3:3).le.'9' .and. &
- g4(4:4).ge.'0' .and. g4(4:4).le.'9' .and. g4(1:4).ne.'RR73'
-
- msg=msg0
- if(index(msg,'/').gt.0) goto 900 !Ignore messages with compound calls
- i0=index(msg,' R ')
- if(i0.ge.7) msg=msg(1:i0)//msg(i0+3:)
- i1=index(msg,' ')
- c6=' '
- g4=' '
- if(i1.ge.4 .and. i1.le.13) then
- i2=index(msg(i1+1:),' ') + i1
- c6=msg(i1+1:i2-1) !Extract DX call
- g4=msg(i2+1:i2+4) !Extract DX grid
- endif
-
- newcall=.true.
- do i=1,nhist2
- if(callers(i)%call .eq. c6) then
- newcall=.false.
- callers(i)%nsec=time()
- callers(i)%nfreq=nfreq
- exit
- endif
- enddo
-
- if(newcall .and. isgrid(g4)) then
- if(nhist2.eq.MAX_CALLERS) then
-! Purge the oldest caller
- callers(1:MAX_CALLERS-1)=callers(2:MAX_CALLERS)
- nhist2=nhist2-1
- endif
- nhist2=nhist2+1
- callers(nhist2)%call=c6
- callers(nhist2)%grid=g4
- callers(nhist2)%nsec=time()
- callers(nhist2)%nfreq=nfreq
- endif
-
- if(nhist2.ge.1 .and. nhist2.le.40) then
- open(24,file=trim(data_dir)//'/tsil.3q',status='unknown', &
- form='unformatted')
- write(24) nhist2
- write(24) callers(1:nhist2)
- close(24)
- endif
-
-900 return
+subroutine q65_hist2(nfreq,msg0,callers,nhist2,persist)
+
+ use types, only: q3list
+ use prog_args, only: data_dir
+ use q65_callers, only: Q65_MAX_CALLERS,q65_record_caller,q65_save_callers
+ implicit none
+ integer, intent(in) :: nfreq
+ character(len=*), intent(in) :: msg0
+ type(q3list), intent(inout) :: callers(Q65_MAX_CALLERS)
+ integer, intent(inout) :: nhist2
+ logical, intent(in) :: persist
+ integer :: status,time
+
+ call q65_record_caller(callers,nhist2,nfreq,msg0,time())
+ if(persist) call q65_save_callers(trim(data_dir)//'/tsil.3q',callers,nhist2,status)
end subroutine q65_hist2
end module q65
diff --git a/lib/qra/q65/q65.h b/lib/qra/q65/q65.h
index ab1b389d..06dd9b27 100644
--- a/lib/qra/q65/q65.h
+++ b/lib/qra/q65/q65.h
@@ -23,6 +23,7 @@
#define _q65_h
#include "qracodes.h"
+#include "q65_limits.h"
// Error codes returned by q65_decode(...)
#define Q65_DECODE_INVPARAMS -1
@@ -34,9 +35,7 @@
// Verify loglikelihood after successful decoding
#define Q65_CHECKLLH
// Max codeword list size in q65_decode_fullaplist
-// See MAX_NCW in q65_set_list2.f90 (not required to be a power of 2 --
-// this value is only ever used in a plain bounds check).
-#define Q65_FULLAPLIST_SIZE 411
+#define Q65_FULLAPLIST_SIZE Q65_AP_LIST_CAPACITY
// maximum number of weights for the fast-fading metric evaluation
#define Q65_FASTFADING_MAXWEIGTHS 65
diff --git a/lib/qra/q65/q65_callers.F90 b/lib/qra/q65/q65_callers.F90
new file mode 100644
index 00000000..224ca60f
--- /dev/null
+++ b/lib/qra/q65/q65_callers.F90
@@ -0,0 +1,142 @@
+#include "q65_limits.h"
+module q65_callers
+ use types, only: q3list
+ use, intrinsic :: iso_fortran_env, only: int64
+ implicit none
+ private
+ integer, parameter, public :: Q65_MAX_CALLERS=Q65_CALLER_CAPACITY
+ integer, parameter, public :: Q65_ROW_WIDTH=Q65_CALLER_ROW_WIDTH
+ integer, parameter, public :: Q65_MAX_CODEWORDS=Q65_AP_LIST_CAPACITY
+ public :: q65_load_callers,q65_save_callers,q65_expire_callers
+ public :: q65_record_caller,q65_remove_caller
+
+contains
+
+ subroutine q65_load_callers(filename,callers,count,status)
+ character(len=*), intent(in) :: filename
+ type(q3list), intent(out) :: callers(Q65_MAX_CALLERS)
+ integer, intent(out) :: count,status
+ type(q3list) :: loaded(Q65_MAX_CALLERS)
+ integer :: unit,n,close_status,file_size
+ logical :: exists
+
+ callers=q3list('','',0,0,0)
+ count=0
+ exists=.false.
+ file_size=-1
+ inquire(file=filename,exist=exists,size=file_size,iostat=status)
+ if(status.ne.0) return
+ if(.not.exists .or. file_size.eq.0) return
+ open(newunit=unit,file=filename,status='old',form='unformatted',action='read',iostat=status)
+ if(status.ne.0) return
+ loaded=q3list('','',0,0,0)
+ read(unit,iostat=status) n
+ if(status.eq.0) then
+ if(n.lt.0 .or. n.gt.Q65_MAX_CALLERS) then
+ status=1
+ else
+ read(unit,iostat=status) loaded(1:n)
+ endif
+ endif
+ close(unit,iostat=close_status)
+ if(status.eq.0) status=close_status
+ if(status.ne.0) return
+ callers=loaded
+ count=n
+ end subroutine q65_load_callers
+
+ subroutine q65_save_callers(filename,callers,count,status)
+ character(len=*), intent(in) :: filename
+ type(q3list), intent(in) :: callers(Q65_MAX_CALLERS)
+ integer, intent(in) :: count
+ integer, intent(out) :: status
+ integer :: unit,close_status
+
+ status=1
+ if(count.lt.0 .or. count.gt.Q65_MAX_CALLERS) return
+ open(newunit=unit,file=filename,status='replace',form='unformatted',action='write',iostat=status)
+ if(status.ne.0) return
+ write(unit,iostat=status) count
+ if(status.eq.0) write(unit,iostat=status) callers(1:count)
+ close(unit,iostat=close_status)
+ if(status.eq.0) status=close_status
+ end subroutine q65_save_callers
+
+ subroutine q65_expire_callers(callers,count,now)
+ type(q3list), intent(inout) :: callers(Q65_MAX_CALLERS)
+ integer, intent(inout) :: count
+ integer, intent(in) :: now
+ integer :: i,kept
+
+ kept=0
+ do i=1,count
+ if(int(now,int64)-int(callers(i)%nsec,int64).gt.86400_int64) cycle
+ kept=kept+1
+ callers(kept)=callers(i)
+ enddo
+ count=kept
+ callers(count+1:)=q3list('','',0,0,0)
+ end subroutine q65_expire_callers
+
+ subroutine q65_record_caller(callers,count,freq,msg,now)
+ type(q3list), intent(inout) :: callers(Q65_MAX_CALLERS)
+ integer, intent(inout) :: count
+ integer, intent(in) :: freq,now
+ character(len=*), intent(in) :: msg
+ character(len=37) :: text
+ character(len=6) :: call
+ character(len=4) :: grid
+ integer :: i,i0,i1,i2,oldest
+
+ call q65_expire_callers(callers,count,now)
+ text=msg
+ if(index(text,'/').gt.0) return
+ i0=index(text,' R ')
+ if(i0.ge.7) text=text(1:i0)//text(i0+3:)
+ i1=index(text,' ')
+ if(i1.lt.4 .or. i1.gt.13) return
+ i2=index(text(i1+1:),' ')+i1
+ if(i2.le.i1+1 .or. i2+4.gt.len(text)) return
+ call=text(i1+1:i2-1)
+ grid=text(i2+1:i2+4)
+ do i=1,count
+ if(callers(i)%call.ne.call) cycle
+ callers(i)%nsec=now
+ callers(i)%nfreq=freq
+ return
+ enddo
+ if(.not.valid_grid(grid)) return
+ if(count.eq.Q65_MAX_CALLERS) then
+ oldest=minloc(callers(1:count)%nsec,dim=1)
+ callers(oldest:count-1)=callers(oldest+1:count)
+ count=count-1
+ endif
+ count=count+1
+ callers(count)=q3list(call,grid,now,freq,0)
+ end subroutine q65_record_caller
+
+ subroutine q65_remove_caller(callers,count,call)
+ type(q3list), intent(inout) :: callers(Q65_MAX_CALLERS)
+ integer, intent(inout) :: count
+ character(len=*), intent(in) :: call
+ character(len=6) :: target
+ integer :: i
+
+ target=call
+ do i=1,count
+ if(callers(i)%call.ne.target) cycle
+ callers(i:count-1)=callers(i+1:count)
+ callers(count)=q3list('','',0,0,0)
+ count=count-1
+ return
+ enddo
+ end subroutine q65_remove_caller
+
+ logical function valid_grid(grid)
+ character(len=4), intent(in) :: grid
+ valid_grid=grid(1:1).ge.'A' .and. grid(1:1).le.'R' .and. &
+ grid(2:2).ge.'A' .and. grid(2:2).le.'R' .and. &
+ grid(3:3).ge.'0' .and. grid(3:3).le.'9' .and. &
+ grid(4:4).ge.'0' .and. grid(4:4).le.'9' .and. grid.ne.'RR73'
+ end function valid_grid
+end module q65_callers
diff --git a/lib/qra/q65/q65_limits.h b/lib/qra/q65/q65_limits.h
new file mode 100644
index 00000000..533a9399
--- /dev/null
+++ b/lib/qra/q65/q65_limits.h
@@ -0,0 +1,8 @@
+#ifndef WSJT_Q65_LIMITS_H
+#define WSJT_Q65_LIMITS_H
+
+#define Q65_CALLER_CAPACITY 50
+#define Q65_CALLER_ROW_WIDTH 37
+#define Q65_AP_LIST_CAPACITY (1 + 10 * (Q65_CALLER_CAPACITY + 1))
+
+#endif
diff --git a/lib/qra/q65/q65_loops.f90 b/lib/qra/q65/q65_loops.f90
index 4a5bc34f..b7e706f2 100644
--- a/lib/qra/q65/q65_loops.f90
+++ b/lib/qra/q65/q65_loops.f90
@@ -18,6 +18,7 @@ subroutine q65_loops(workspace,c00,npts2,nsps2,nsubmode,ndepth,jpk0, &
LL=64*(mode_q65+2)
call workspace%ensure(npts2,LL)
+ call workspace%prepare_fft(nsps2)
idec=-1
ircbest=9999
irc=-99
@@ -61,8 +62,9 @@ subroutine q65_loops(workspace,c00,npts2,nsps2,nsubmode,ndepth,jpk0, &
jpk=max(0,jpk)
jpk=min(29000,jpk)
call spec64(workspace%c0(0:npts2-1),workspace%cs,npts2,nsps2, &
- mode_q65,jpk,workspace%s3(1:LL*NN),LL,NN)
+ mode_q65,jpk,workspace%s3(1:LL*NN),LL,NN,workspace%plan)
call pctile(workspace%s3(1:LL*NN),LL*NN,40,base)
+ if(base<=0.) cycle
workspace%s3(1:LL*NN)=workspace%s3(1:LL*NN)/base
where(workspace%s3(1:LL*NN)>s3lim) workspace%s3(1:LL*NN)=s3lim
call q65_bzap(workspace%s3(1:LL*NN),LL) !Zap birdies
diff --git a/lib/qra/q65/q65_set_list2.f90 b/lib/qra/q65/q65_set_list2.f90
index 433802fb..7043dbad 100644
--- a/lib/qra/q65/q65_set_list2.f90
+++ b/lib/qra/q65/q65_set_list2.f90
@@ -1,17 +1,16 @@
subroutine q65_set_list2(mycall,hiscall,hisgrid,callers,nhist2,codewords,ncw)
use types
- parameter (MAX_NCW=411) !1 + 40 stored callers plus the current call, 5 kinds, 2 flags
- parameter (MAX_CALLERS=50) !For multiple q3 decodes in NA VHF Contest mode
+ use q65_callers, only: Q65_MAX_CALLERS,Q65_MAX_CODEWORDS
character*12 mycall,hiscall
character*6 hisgrid,c6
character*4 g4
character*37 msg,msgsent
logical std,isgrid
- integer codewords(63,MAX_NCW)
+ integer codewords(63,Q65_MAX_CODEWORDS)
integer itone(85)
integer isync(22)
- type(q3list) callers(MAX_CALLERS)
+ type(q3list) callers(Q65_MAX_CALLERS)
data isync/1,9,12,13,15,22,23,26,27,33,35,38,46,50,55,60,62,66,69,74,76,85/
@@ -19,10 +18,13 @@ subroutine q65_set_list2(mycall,hiscall,hisgrid,callers,nhist2,codewords,ncw)
g4(2:2).le.'R' .and. g4(3:3).ge.'0' .and. g4(3:3).le.'9' .and. &
g4(4:4).ge.'0' .and. g4(4:4).le.'9' .and. g4(1:4).ne.'RR73'
+ ncw=0
+ if(nhist2.lt.0 .or. nhist2.gt.Q65_MAX_CALLERS) return
+
call stdcall(hiscall,std)
jmax=nhist2
if(std .and. isgrid(hisgrid(1:4))) then
- jmax=min(MAX_CALLERS,nhist2+1)
+ jmax=nhist2+1
do j=1,nhist2
if(callers(j)%call .eq. hiscall(1:6)) then
jmax=nhist2
@@ -34,11 +36,12 @@ subroutine q65_set_list2(mycall,hiscall,hisgrid,callers,nhist2,codewords,ncw)
codewords(:,1)=0
i=1
do j=1,jmax
- c6=callers(j)%call
- g4=callers(j)%grid
if(j.eq.nhist2+1) then
c6=hiscall(1:6)
g4=hisgrid(1:4)
+ else
+ c6=callers(j)%call
+ g4=callers(j)%grid
endif
do k=1,5
msg=trim(mycall)//' '//trim(c6)
diff --git a/lib/qra/q65/q65_subs.c b/lib/qra/q65/q65_subs.c
index 4311ba39..8e9a8481 100644
--- a/lib/qra/q65/q65_subs.c
+++ b/lib/qra/q65/q65_subs.c
@@ -1,147 +1,130 @@
-// q65_subs.c
+/* Owned and legacy Fortran interfaces for the Q65 codec. */
-/* Fortran interface for Q65 codec
-
- To encode a Q65 message:
-
- integer x(13) !Message payload, 78 bits as 13 six-bit integers
- integer y(63) !Codeword, 63 six-bit integers
- call q65_enc(imsg,icodeword)
-
- To decode a Q65 message:
-
- parameter (LL=64,NN=63)
- real s3(LL,NN) !Received energies
- real s3prob(LL,NN) !Symbol-value probabilities
- integer APmask(13)
- integer APsymbols(13)
- real snr2500
- integer xdec(13) !Decoded 78-bit message as 13 six-bit integers
- integer irc !Return code from q65_decode()
-
- call q65_dec(s3,APmask,APsymbols,s3prob,snr2500,xdec,irc)
-*/
-
-#include "qra15_65_64_irr_e23.h" // QRA code used by Q65
+#include "qra15_65_64_irr_e23.h"
#include "q65.h"
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
-static q65_codec_ds codec;
-
-void q65_enc_(int x[], int y[])
+void *q65_codec_create(void)
{
-
- static int first=1;
- if (first) {
- // Set the QRA code, allocate memory, and initialize
- int rc = q65_init(&codec,&qra15_65_64_irr_e23);
- if (rc<0) {
- printf("error in q65_init()\n");
- exit(0);
- }
- first=0;
+ q65_codec_ds *codec = calloc(1, sizeof(*codec));
+ if (!codec) return NULL;
+ if (q65_init(codec, &qra15_65_64_irr_e23) < 0) {
+ free(codec);
+ return NULL;
}
- // Encode message x[13], producing codeword y[63]
- q65_encode(&codec,y,x);
+ return codec;
}
-void q65_intrinsics_ff_(float s3[], int* submode, float* B90Ts,
- int* fadingModel, float s3prob[])
+void q65_codec_destroy(void *handle)
{
+ if (!handle) return;
+ q65_free(handle);
+ free(handle);
+}
-/* Input: s3[LL,NN] Received energies
- * submode 0=A, 4=E
- * B90 Spread bandwidth, 90% fractional energy
- * fadingModel 0=Gaussian, 1=Lorentzian
- * Output: s3prob[LL,NN] Symbol-value intrinsic probabilities
- */
+int q65_codec_encode(void *handle, const int *x, int *y)
+{
+ memset(y, 0, 63 * sizeof(*y));
+ if (!handle) return -1;
+ return q65_encode(handle, y, x) < 0 ? -1 : 0;
+}
- int rc;
- static int first=1;
+int q65_codec_intrinsics(void *handle, const float *s3, int submode,
+ float b90ts, int fading_model, float *prob)
+{
+ memset(prob, 0, 64 * 63 * sizeof(*prob));
+ if (!handle) return -1;
+ return q65_intrinsics_fastfading(handle, prob, s3, submode, b90ts,
+ fading_model) < 0 ? -1 : 0;
+}
- if (first) {
- // Set the QRA code, allocate memory, and initialize
- int rc = q65_init(&codec,&qra15_65_64_irr_e23);
- if (rc<0) {
- printf("error in q65_init()\n");
- exit(0);
- }
- first=0;
- }
- rc = q65_intrinsics_fastfading(&codec,s3prob,s3,*submode,*B90Ts,*fadingModel);
- if(rc<0) {
- printf("error in q65_intrinsics()\n");
- exit(0);
+int q65_codec_decode(void *handle, const float *s3, const float *prob,
+ const int *mask, const int *symbols, int maxiters,
+ float *esnodb, int *xdec, int *decode_rc)
+{
+ int ydec[63];
+ *esnodb = 0;
+ *decode_rc = Q65_DECODE_INVPARAMS;
+ memset(xdec, 0, 13 * sizeof(*xdec));
+ if (!handle) return -1;
+ *decode_rc = q65_decode(handle, ydec, xdec, prob, mask, symbols, maxiters);
+ if (*decode_rc == Q65_DECODE_INVPARAMS) return -1;
+ if (*decode_rc < 0) return 0;
+ if (q65_esnodb_fastfading(handle, esnodb, ydec, s3) < 0) {
+ *decode_rc = Q65_DECODE_INVPARAMS;
+ *esnodb = 0;
+ return -1;
}
+ return 0;
}
-
-void q65_dec_(float s3[], float s3prob[], int APmask[], int APsymbols[],
- int* maxiters0, float* esnodb0, int xdec[], int* rc0)
-{
-/* Input: s3[LL,NN] Symbol spectra
- * s3prob[LL,NN] Symbol-value intrinsic probabilities
- * APmask[13] AP information to be used in decoding
- * APsymbols[13] Available AP informtion
- * Output:
- * esnodb0 Estimated Es/No (dB)
- * xdec[13] Decoded 78-bit message as 13 six-bit integers
- * rc0 Return code from q65_decode()
- */
-
- int rc;
+int q65_codec_decode_fullaplist(void *handle, const float *s3, const float *prob,
+ const int *codewords, int ncw, float *esnodb,
+ int *xdec, float *plog, int *decode_rc)
+{
int ydec[63];
- float esnodb;
- int maxiters=*maxiters0;
+ *esnodb = 0;
+ *plog = 0;
+ *decode_rc = Q65_DECODE_INVPARAMS;
+ memset(xdec, 0, 13 * sizeof(*xdec));
+ if (!handle) return -1;
+ *decode_rc = q65_decode_fullaplist(handle, ydec, xdec, prob, codewords, ncw);
+ if (*decode_rc == Q65_DECODE_INVPARAMS) return -1;
+ *plog = q65_llh;
+ if (*decode_rc < 0) return 0;
+ if (q65_esnodb_fastfading(handle, esnodb, ydec, s3) < 0) {
+ *decode_rc = Q65_DECODE_INVPARAMS;
+ *esnodb = 0;
+ return -1;
+ }
+ return 0;
+}
- rc = q65_decode(&codec,ydec,xdec,s3prob,APmask,APsymbols,maxiters);
- *rc0=rc;
- // rc = -1: Invalid params
- // rc = -2: Decode failed
- // rc = -3: CRC mismatch
- *esnodb0 = 0.0; //Default Es/No for a failed decode
- if(rc<0) return;
+static q65_codec_ds *legacy_codec(void)
+{
+ static q65_codec_ds *codec;
+ if (!codec) {
+ codec = q65_codec_create();
+ if (!codec) {
+ fputs("Unable to initialize Q65 codec\n", stderr);
+ exit(EXIT_FAILURE);
+ }
+ }
+ return codec;
+}
- rc = q65_esnodb_fastfading(&codec,&esnodb,ydec,s3);
- if(rc<0) {
- printf("error in q65_esnodb_fastfading()\n");
- exit(0);
+static void check_legacy_status(int status)
+{
+ if (status < 0) {
+ fputs("Q65 codec operation failed\n", stderr);
+ exit(EXIT_FAILURE);
}
- *esnodb0 = esnodb;
}
-void q65_dec_fullaplist_(float s3[], float s3prob[], int codewords[],
- int* ncw, float* esnodb0, int xdec[], float* plog, int* rc0)
+void q65_enc_(int x[], int y[])
{
-/* Input: s3[LL,NN] Symbol spectra
- * s3prob[LL,NN] Symbol-value intrinsic probabilities
- * codewords[63,ncw] Full codewords to search for
- * ncw Number of codewords
- * Output:
- * esnodb0 Estimated Es/No (dB)
- * xdec[13] Decoded 78-bit message as 13 six-bit integers
- * rc0 Return code from q65_decode()
- */
+ check_legacy_status(q65_codec_encode(legacy_codec(), x, y));
+}
- int rc;
- int ydec[63];
- float esnodb;
+void q65_intrinsics_ff_(float s3[], int *submode, float *b90ts,
+ int *fading_model, float prob[])
+{
+ check_legacy_status(q65_codec_intrinsics(legacy_codec(), s3, *submode,
+ *b90ts, *fading_model, prob));
+}
- rc = q65_decode_fullaplist(&codec,ydec,xdec,s3prob,codewords,*ncw);
- *plog=q65_llh;
- *rc0=rc;
-
- // rc = -1: Invalid params
- // rc = -2: Decode failed
- // rc = -3: CRC mismatch
- *esnodb0 = 0.0; //Default Es/No for a failed decode
- if(rc<0) return;
+void q65_dec_(float s3[], float prob[], int mask[], int symbols[],
+ int *maxiters, float *esnodb, int xdec[], int *rc)
+{
+ q65_codec_decode(legacy_codec(), s3, prob, mask, symbols,
+ *maxiters, esnodb, xdec, rc);
+}
- rc = q65_esnodb_fastfading(&codec,&esnodb,ydec,s3);
- if(rc<0) {
- printf("error in q65_esnodb_fastfading()\n");
- exit(0);
- }
- *esnodb0 = esnodb;
+void q65_dec_fullaplist_(float s3[], float prob[], int codewords[],
+ int *ncw, float *esnodb, int xdec[], float *plog, int *rc)
+{
+ q65_codec_decode_fullaplist(legacy_codec(), s3, prob, codewords,
+ *ncw, esnodb, xdec, plog, rc);
}
diff --git a/lib/qra/q65/q65_workspace.f90 b/lib/qra/q65/q65_workspace.f90
index 321bdfd4..00af36c0 100644
--- a/lib/qra/q65/q65_workspace.f90
+++ b/lib/qra/q65/q65_workspace.f90
@@ -1,5 +1,7 @@
module q65_workspace
+ use iso_c_binding, only: c_int64_t
+ use fftw3
implicit none
integer, parameter :: Q65_NN=63
@@ -9,8 +11,13 @@ module q65_workspace
complex, allocatable :: c0(:)
real, allocatable :: s3(:)
complex, allocatable :: cs(:)
+ integer(c_int64_t) :: plan=0
+ integer :: plan_length=0,planning=-1
contains
procedure :: ensure => ensure_q65_workspace
+ procedure :: prepare_fft
+ procedure :: destroy
+ final :: finalize
end type q65_workspace_type
contains
@@ -40,4 +47,39 @@ contains
if(.not.allocated(this%cs)) allocate(this%cs(0:Q65_MAXFFT-1))
end subroutine ensure_q65_workspace
+ subroutine prepare_fft(this,length)
+ class(q65_workspace_type), intent(inout) :: this
+ integer, intent(in) :: length
+ integer npatience,nthreads,flags
+ common/patience/npatience,nthreads
+ if(this%plan/=0.and.this%plan_length==length.and.this%planning==npatience) return
+ !$omp critical(fftw)
+ if(this%plan/=0) call sfftw_destroy_plan(this%plan)
+ flags=FFTW_ESTIMATE
+ if(npatience==1) flags=FFTW_ESTIMATE_PATIENT
+ if(npatience==2) flags=FFTW_MEASURE
+ if(npatience==3) flags=FFTW_PATIENT
+ if(npatience==4) flags=FFTW_EXHAUSTIVE
+ call sfftw_plan_dft_1d(this%plan,length,this%cs,this%cs,FFTW_FORWARD,flags)
+ !$omp end critical(fftw)
+ this%plan_length=length
+ this%planning=npatience
+ end subroutine
+
+ subroutine destroy(this)
+ class(q65_workspace_type), intent(inout) :: this
+ !$omp critical(fftw)
+ if(this%plan/=0) call sfftw_destroy_plan(this%plan)
+ !$omp end critical(fftw)
+ this%plan=0
+ if(allocated(this%c0)) deallocate(this%c0)
+ if(allocated(this%s3)) deallocate(this%s3)
+ if(allocated(this%cs)) deallocate(this%cs)
+ end subroutine
+
+ subroutine finalize(this)
+ type(q65_workspace_type), intent(inout) :: this
+ call this%destroy()
+ end subroutine
+
end module q65_workspace