diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/decode4.f90 | 112 | ||||
| -rw-r--r-- | lib/decoder.f90 | 40 | ||||
| -rw-r--r-- | lib/decoder_callbacks.f90 | 107 | ||||
| -rw-r--r-- | lib/decoder_engine.f90 | 240 | ||||
| -rw-r--r-- | lib/decoder_engine_types.f90 | 18 | ||||
| -rw-r--r-- | lib/deep4.f90 | 169 | ||||
| -rw-r--r-- | lib/extract4.f90 | 18 | ||||
| -rw-r--r-- | lib/jt4.f90 | 26 | ||||
| -rw-r--r-- | lib/jt4_decode.f90 | 1071 | ||||
| -rw-r--r-- | lib/jt4_host_support.f90 | 112 | ||||
| -rw-r--r-- | lib/jt9.f90 | 3 | ||||
| -rw-r--r-- | lib/jt9_params_init.f90 | 6 | ||||
| -rw-r--r-- | lib/jt9a.f90 | 5 | ||||
| -rw-r--r-- | lib/lpf1.f90 | 29 | ||||
| -rw-r--r-- | lib/ps4.f90 | 27 | ||||
| -rw-r--r-- | lib/streaming_apply.f90 | 7 | ||||
| -rw-r--r-- | lib/streaming_control.f90 | 3 | ||||
| -rw-r--r-- | lib/streaming_emit.f90 | 27 | ||||
| -rw-r--r-- | lib/streaming_io.f90 | 5 | ||||
| -rw-r--r-- | lib/sync4.f90 | 146 | ||||
| -rw-r--r-- | lib/xcor4.f90 | 30 |
21 files changed, 1305 insertions, 896 deletions
diff --git a/lib/decode4.f90 b/lib/decode4.f90 deleted file mode 100644 index e0596ed1..00000000 --- a/lib/decode4.f90 +++ /dev/null @@ -1,112 +0,0 @@ -subroutine decode4(dat,npts,dtx,nfreq,flip,mode4,ndepth,neme,minw, & - mycall,hiscall,hisgrid,decoded,nfano,deepbest,qbest,ichbest) - -! Decodes JT4 data, assuming that DT and DF have already been determined. -! Input dat(npts) has already been downsampled by 2: rate = 11025/2. -! ### NB: this initial downsampling should be removed in WSJT-X, since -! it restricts the useful bandwidth to < 2.7 kHz. - - use jt4 - real dat(npts) !Raw data - character decoded*22,deepmsg*22,deepbest*22 - character*12 mycall,hiscall - character*6 hisgrid - real*8 dt,df,phi,f0,dphi,twopi,phi1,dphi1 - complex*16 cz,cz1,c0,c1 - real*4 sym(207) - - twopi=8*atan(1.d0) - dt=2.d0/11025 !Sample interval (2x downsampled data) - df=11025.d0/2520.d0 !Tone separation for JT4A mode - nsym=206 - amp=15.0 - istart=nint((dtx+0.8)/dt) !Start index for synced FFTs - if(istart.lt.0) istart=0 - nchips=0 - qbest=0. - qtop=0. - deepmsg=' ' - ichbest=-1 - c0=0. - k=istart - phi=0.d0 - phi1=0.d0 - - ich1=minw+1 - do ich=1,7 - if(nch(ich).le.mode4) ich2=ich - enddo - - do ich=ich1,ich2 - nchips=min(nch(ich),70) - nspchip=1260/nchips - k=istart - phi=0.d0 - phi1=0.d0 - fac2=1.e-8 * sqrt(float(mode4)) - do j=1,nsym+1 - if(flip.gt.0.0) then - f0=nfreq + (npr(j))*mode4*df - f1=nfreq + (2+npr(j))*mode4*df - else - f0=nfreq + (1-npr(j))*mode4*df - f1=nfreq + (3-npr(j))*mode4*df - endif - dphi=twopi*dt*f0 - dphi1=twopi*dt*f1 - sq0=0. - sq1=0. - do nc=1,nchips - phi=0.d0 - phi1=0.d0 - c0=0. - c1=0. - do i=1,nspchip - k=k+1 - phi=phi+dphi - phi1=phi1+dphi1 - cz=dcmplx(cos(phi),-sin(phi)) - cz1=dcmplx(cos(phi1),-sin(phi1)) - if(k.le.npts) then - c0=c0 + dat(k)*cz - c1=c1 + dat(k)*cz1 - endif - enddo - sq0=sq0 + real(c0)**2 + aimag(c0)**2 - sq1=sq1 + real(c1)**2 + aimag(c1)**2 - enddo - sq0=fac2*sq0 - sq1=fac2*sq1 - rsym=amp*(sq1-sq0) - if(j.ge.1) then - rsymbol(j,ich)=rsym - sym(j)=rsym - endif - enddo - - call extract4(sym,ncount,decoded) !Do the convolutional decode - nfano=0 - if(ncount.ge.0) then - nfano=1 - ichbest=ich - exit - endif - - qual=0. !Now try deep search -! if(ndepth.ge.1) then - if(iand(ndepth,32).eq.32) then - call deep4(sym(2),neme,flip,mycall,hiscall,hisgrid,deepmsg,qual) - if(qual.gt.qbest) then - qbest=qual - deepbest=deepmsg - ichbest=ich - endif - endif - enddo - if(qbest.gt.qtop) then - qtop=qbest - endif - qual=qbest - - return -end subroutine decode4 diff --git a/lib/decoder.f90 b/lib/decoder.f90 index 4bcbe87f..6f6b9075 100644 --- a/lib/decoder.f90 +++ b/lib/decoder.f90 @@ -3,14 +3,12 @@ subroutine multimode_decoder_core(ss,id2,params,nfsample,completion,progress_gen !$ use omp_lib use prog_args use timer_module, only: timer - use jt4_decode use jt65_decode use jt65_host_support, only: load_jt65_calls use jt9_decode use decoder_callbacks, only: decoder_callback_context, & - counting_jt4_decoder, counting_jt65_decoder, counting_jt9_decoder, & - jt4_decoded, & - jt4_average, jt65_decoded, jt9_decoded + counting_jt65_decoder, counting_jt9_decoder, & + jt65_decoded, jt9_decoded use streaming_emit, only: streaming_emit_enabled, & streaming_emit_decode use decode_completion_module, only: decode_completion_result, & @@ -48,15 +46,16 @@ subroutine multimode_decoder_core(ss,id2,params,nfsample,completion,progress_gen character(len=4), allocatable :: jt65_grids(:) data ntr0/-1/ save - type(counting_jt4_decoder) :: my_jt4 type(counting_jt65_decoder) :: my_jt65 type(counting_jt9_decoder) :: my_jt9 - if(params%nmode.eq.8.or.params%nmode.eq.5.or.params%nmode.eq.9.or.params%nmode.eq.65.or. & + if(params%nmode.eq.4.or.params%nmode.eq.8.or.params%nmode.eq.5.or.params%nmode.eq.9.or. & + params%nmode.eq.65.or. & params%nmode.eq.66.or.params%nmode.eq.240.or.params%nmode.eq.241.or.params%nmode.eq.242) then call run_decoder_engine(id2,params,nfsample,completion,progress_generation, & 0_c_int64_t,0_c_int64_t,0_c_int, & - merge(params%kin,0_c_int,params%nmode==5.or.params%nmode==9.or.params%nmode==65.or.params%nmode==66.or. & + merge(params%kin,0_c_int,params%nmode==4.or.params%nmode==5.or.params%nmode==9.or. & + params%nmode==65.or.params%nmode==66.or. & params%nmode==240.or.params%nmode==241.or.params%nmode==242)) return endif @@ -64,7 +63,6 @@ subroutine multimode_decoder_core(ss,id2,params,nfsample,completion,progress_gen call reset_decode_completion(completion) active_progress_generation=progress_generation - my_jt4%decoded = 0 my_jt65%decoded = 0 my_jt9%decoded = 0 nsynced=0 @@ -125,7 +123,6 @@ subroutine multimode_decoder_core(ss,id2,params,nfsample,completion,progress_gen callback_context%bVHF = bVHF callback_context%b_superfox = params%b_superfox callback_context%mycall = mycall - my_jt4%context = callback_context my_jt65%context = callback_context my_jt9%context = callback_context @@ -151,25 +148,6 @@ subroutine multimode_decoder_core(ss,id2,params,nfsample,completion,progress_gen ! id2(1:nz)=0 ! temporarily disabled as it can breaak the JT9 decoder, maybe others endif - if(params%nmode.eq.4) open(14,file=trim(temp_dir)// & - '/avemsg.txt',status='unknown') - - if(params%nmode.eq.4) then - jz=52*nfsample - if(params%newdat) then - if(nfsample.eq.12000) call wav11(id2,jz,dd) - if(nfsample.eq.11025) dd(1:jz)=id2(1:jz) - else - jz=52*11025 - endif - call my_jt4%decode(jt4_decoded,dd,jz,params%nutc,params%nfqso, & - params%ntol,params%emedelay,params%dttol,logical(params%nagain), & - params%ndepth,logical(params%nclearave),params%minsync, & - params%minw,params%nsubmode,mycall,hiscall, & - hisgrid,params%nlist,params%listutc,jt4_average) - go to 800 - endif - npts65=52*12000 if(baddata(id2,npts65)) then nsynced=0 @@ -239,11 +217,10 @@ subroutine multimode_decoder_core(ss,id2,params,nfsample,completion,progress_gen ! JT65 is not yet producing info for nsynced, ndecoded. -800 ndecoded = my_jt4%decoded + my_jt65%decoded + my_jt9%decoded +800 ndecoded = my_jt65%decoded + my_jt9%decoded call set_decode_completion(completion,nsynced,ndecoded,navg0) call write_decode_progress(active_progress_generation) close(13) - if(params%nmode.eq.4) close(14) return end subroutine multimode_decoder_core @@ -265,7 +242,8 @@ subroutine multimode_decoder(ss,id2,params,nfsample) if (.not. completion%available) return if (streaming_emit_enabled()) then - call streaming_emit_decode_finished(params%nutc,params%nmode==66.or.params%nmode==240.or. & + call streaming_emit_decode_finished(params%nutc,params%nmode==4.or.params%nmode==66.or. & + params%nmode==240.or. & params%nmode==241.or.params%nmode==242) else if (.not. lquiet) then call write_decode_completion(completion) diff --git a/lib/decoder_callbacks.f90 b/lib/decoder_callbacks.f90 index 186290c7..3c5821de 100644 --- a/lib/decoder_callbacks.f90 +++ b/lib/decoder_callbacks.f90 @@ -99,69 +99,79 @@ contains logical, intent(in) :: is_average integer, intent(in) :: ave - character*22 decoded - character*3 cflags - integer context_nutc + character(len=22) :: decoded + character(len=3) :: cflags + type(decoder_callback_context) :: context + type(engine_observation) :: observation + integer :: i,display_utc select type (typed_this => this) type is (counting_jt4_decoder) - context_nutc = typed_this%context%nutc + context=typed_this%context class default return end select - if(ich.eq.-99) stop - if (have_sync) then - decoded=decoded0 - cflags=' ' - if(decoded.ne.' ') then - cflags='f ' - if(is_deep) then - cflags='d ' - write(cflags(2:2),'(i1)') min(int(qual),9) - if(qual.ge.10.0) cflags(2:2)='*' - if(qual.lt.3.0) decoded(22:22)='?' + observation=engine_observation() + observation%mode=engine_mode_jt4 + observation%variant=context%submode + observation%snr_db=snr + observation%frequency_hz=real(freq) + observation%dt_seconds=dt + observation%sync=this%sync_strength + observation%quality=qual + if(have_sync) then + observation%jt4%kind=1 + observation%jt4%sync_polarity=merge(-1,1,sync=='#') + if(len_trim(decoded0)>0) then + observation%jt4%kind=2 + observation%jt4%method=merge(2,1,is_deep) + observation%jt4%width_profile=max(0,ich-1) + if(is_average) observation%jt4%average_count=ave + do i=1,len_trim(decoded0) + observation%message(i)=decoded0(i:i) + enddo + endif + endif + if(associated(context%sink)) call context%sink(context%sink_user,observation) + + display_utc=context%nutc + if(context%utc_is_hhmmss) display_utc=display_utc/100 + if(context%render_legacy) then + if(have_sync) then + decoded=decoded0 + cflags=' ' + if(len_trim(decoded)>0) then + cflags='f ' + if(is_deep) then + cflags='d ' + write(cflags(2:2),'(i1)') min(int(qual),9) + if(qual>=10.0) cflags(2:2)='*' + if(qual<3.0) decoded(22:22)='?' + endif + if(is_average) then + write(cflags(3:3),'(i1)') min(ave,9) + if(ave>=10) cflags(3:3)='*' + if(cflags(1:1)=='f') cflags=cflags(1:1)//cflags(3:3)//' ' + endif endif - if(is_average) then - write(cflags(3:3),'(i1)') min(ave,9) - if(ave.ge.10) cflags(3:3)='*' - if(cflags(1:1).eq.'f') cflags=cflags(1:1)//cflags(3:3)//' ' + if(streaming_emit_enabled()) then + call streaming_emit_decode('JT4',context%nutc,snr,dt,freq,decoded,context%utc_is_hhmmss) + else + write(*,1000) display_utc,snr,dt,freq,sync,decoded,cflags endif - endif - if (streaming_emit_enabled()) then - call streaming_emit_decode("JT4", context_nutc, snr, dt, freq, decoded) else - write(*,1000) context_nutc,snr,dt,freq,sync,decoded,cflags - end if - else - if (.not. streaming_emit_enabled()) write(*,1000) context_nutc,snr,dt,freq - end if -1000 format(i4.4,i4,f5.1,i5,1x,'$',a1,1x,a22,1x,a3) + if(.not.streaming_emit_enabled()) write(*,1000) display_utc,snr,dt,freq + endif +1000 format(i4.4,i4,f5.1,i5,1x,'$',a1,1x,a22,1x,a3) + endif select type (typed_this => this) type is (counting_jt4_decoder) - typed_this%decoded = typed_this%decoded + 1 + if(observation%jt4%kind==2) typed_this%decoded=typed_this%decoded+1 end select end subroutine jt4_decoded - subroutine jt4_average (this, used, utc, sync, dt, freq, flip) - implicit none - class(jt4_decoder), intent(inout) :: this - logical, intent(in) :: used - integer, intent(in) :: utc - real, intent(in) :: sync - real, intent(in) :: dt - integer, intent(in) :: freq - logical, intent(in) :: flip - character(len=1) :: cused, csync - - cused = '.' - csync = '*' - if (used) cused = '$' - if (flip) csync = '$' - write(14,1000) cused,utc,sync,dt,freq,csync -1000 format(a1,i5.4,f6.1,f6.2,i6,1x,a1) - end subroutine jt4_average subroutine jt65_decoded(this,sync,snr,dt,freq,drift,nflip,width, & decoded0,ft,qual,nsmo,nsum,minsync) @@ -788,7 +798,8 @@ contains endif if (streaming_emit_enabled()) then - call streaming_emit_decode("FST4", nutc, nsnr, dt, nint(freq), decoded0,context%utc_is_hhmmss) + call streaming_emit_decode(merge('FST4W','FST4 ',context%mode==engine_mode_fst4w), & + nutc, nsnr, dt, nint(freq), decoded0,context%utc_is_hhmmss) else write(*,1005) line end if diff --git a/lib/decoder_engine.f90 b/lib/decoder_engine.f90 index eee39e74..fd49af0d 100644 --- a/lib/decoder_engine.f90 +++ b/lib/decoder_engine.f90 @@ -6,6 +6,7 @@ module decoder_engine use ft8_engine_kernel, only: ft8_kernel_state,params_block,run_ft8_kernel,reset_ft8_kernel,release_ft8_input use decoder_callbacks, only: decoder_callback_context,counting_ft4_decoder,ft4_decoded, & counting_jt9_decoder,jt9_decoded,counting_jt65_decoder,jt65_decoded, & + counting_jt4_decoder,jt4_decoded, & counting_q65_decoder,q65_decoded,counting_fst4_decoder,fst4_decoded use q65_decode, only: q65_options use fst4_decode, only: fst4_options @@ -13,11 +14,15 @@ module decoder_engine use q65_host_support, only: load_q65_history,save_q65_history,write_q65_curves,write_q65_diagnostic use q65_callers, only: Q65_MAX_CALLERS use types, only: q3list + use jt4_decode, only: jt4_options,jt4_call,jt4_average_entry + use jt4_host_support, only: convert_jt4_host_options,load_jt4_calls,write_jt4_averages, & + prepare_jt4_host,write_jt4_diagnostic use jt65_decode, only: jt65_options,jt65_average_entry use jt65_host_support, only: load_jt65_calls,write_jt65_averages use decode_completion_module, only: decode_completion_result,set_decode_completion,write_decode_progress use prog_args, only: temp_dir use timer_module, only: timer + use streaming_emit, only: streaming_emit_enabled implicit none private public :: engine_host_decode @@ -141,6 +146,13 @@ module decoder_engine type, bind(C) :: engine_fst4w_call character(c_char) :: call_grid(20) end type + type, bind(C) :: engine_jt4_call + character(c_char) :: call(12),grid(4) + end type + type, bind(C) :: engine_jt4_average_entry + integer(c_int) :: utc,frequency_hz,polarity,used + real(c_float) :: sync,dt_seconds + end type type, bind(C) :: attempt_request integer(c_int64_t) :: input_id=0,analysis_id=0 integer(c_int) :: attempt_no=0,mode=8,phase=2,source=0 @@ -150,6 +162,7 @@ module decoder_engine type(engine_jt65_options) :: jt65 type(engine_q65_options) :: q65 type(engine_fst4_options) :: fst4 + type(engine_jt4_options) :: jt4 end type type, bind(C) :: audio_view type(c_ptr) :: samples @@ -171,6 +184,7 @@ module decoder_engine type(counting_ft4_decoder) :: ft4 ! GCC 13 can finalize an uninitialized temporary for an embedded finalizable extension. type(counting_jt9_decoder), allocatable :: jt9 + type(counting_jt4_decoder), allocatable :: jt4 type(counting_jt65_decoder), allocatable :: jt65 type(counting_q65_decoder), allocatable :: q65 type(counting_fst4_decoder), allocatable :: fst4 @@ -178,7 +192,7 @@ module decoder_engine type(engine_observation), allocatable :: evidence(:) integer(c_short), allocatable :: audio(:) integer(c_int64_t) :: input_id=0,analysis_id=0 - integer :: input_mode=0,input_sample_count=0 + integer :: input_mode=0,input_sample_count=0,input_sample_rate=0 integer :: last_attempt=0,retained=0,dropped=0,emitted=0 logical :: running=.false.,render_legacy=.false. type(c_funptr) :: callback=c_null_funptr @@ -193,14 +207,17 @@ module decoder_engine contains - pure integer function analysis_extent(mode,period) result(samples) + pure integer function analysis_extent(mode,period,sample_rate) result(samples) integer, intent(in) :: mode - integer, optional, intent(in) :: period + integer, optional, intent(in) :: period,sample_rate select case(mode) case(engine_mode_ft4) samples=72576 case(engine_mode_ft8) samples=180000 + case(engine_mode_jt4) + samples=720000 + if(present(sample_rate)) samples=60*sample_rate case(engine_mode_jt9,engine_mode_jt65) samples=720000 case(engine_mode_q65) @@ -221,6 +238,7 @@ contains subroutine initialize_session(state) type(engine_session), intent(inout) :: state allocate(state%jt9) + allocate(state%jt4) allocate(state%jt65) allocate(state%q65) allocate(state%fst4) @@ -256,7 +274,7 @@ contains caps=engine_capabilities(engine_abi, & ior(ior(engine_support_ft8,engine_support_ft4), & ior(ior(ior(engine_support_jt9,engine_support_jt65),engine_support_q65), & - ior(engine_support_fst4,engine_support_fst4w))), & + ior(ior(engine_support_fst4,engine_support_fst4w),engine_support_jt4))), & 0,0,evidence_capacity) status=invalid if(valid_handle(handle)) status=ok @@ -281,7 +299,8 @@ contains logical, intent(in) :: render integer, intent(in) :: progress integer(c_short), pointer :: samples(:) - integer :: analysis_samples + integer :: analysis_samples,allocation_status + integer(c_short), allocatable :: grown_audio(:) type(params_block) :: params type(decoder_callback_context) :: context outcome=attempt_outcome() @@ -290,21 +309,28 @@ contains status=busy if(session%running) go to 900 status=unsupported - if(request%mode/=engine_mode_ft8.and.request%mode/=engine_mode_ft4.and. & + if(request%mode/=engine_mode_jt4.and.request%mode/=engine_mode_ft8.and.request%mode/=engine_mode_ft4.and. & request%mode/=engine_mode_jt9.and.request%mode/=engine_mode_jt65.and.request%mode/=engine_mode_q65.and. & request%mode/=engine_mode_fst4.and.request%mode/=engine_mode_fst4w) go to 900 - if(audio%sample_rate_hz/=12000) go to 900 + if(request%mode==engine_mode_jt4) then + if(audio%sample_rate_hz/=11025.and.audio%sample_rate_hz/=12000) go to 900 + else + if(audio%sample_rate_hz/=12000) go to 900 + endif status=invalid if(request%input_id<=0.or.request%analysis_id<=0.or.request%attempt_no<=0) go to 900 if(request%phase<1.or.request%phase>3.or.request%source<0.or.request%source>1) go to 900 if(request%mode==engine_mode_fst4.or.request%mode==engine_mode_fst4w) then analysis_samples=analysis_extent(request%mode,request%fst4%period_seconds) else - analysis_samples=analysis_extent(request%mode,request%q65%period_seconds) + analysis_samples=analysis_extent(request%mode,request%q65%period_seconds,audio%sample_rate_hz) endif if(audio%sample_count<1.or.audio%sample_count>analysis_samples) go to 900 if(.not.c_associated(audio%samples)) go to 900 - if(request%mode==engine_mode_ft4) then + if(request%mode==engine_mode_jt4) then + if(request%phase==1) go to 900 + if(.not.valid_jt4_options(request%jt4)) go to 900 + else if(request%mode==engine_mode_ft4) then if(request%phase==1) go to 900 if(request%ft4%search_low_hz<0.or.request%ft4%search_high_hz>6000.or. & request%ft4%search_low_hz>request%ft4%search_high_hz) go to 900 @@ -338,6 +364,9 @@ contains endif if(session%input_id/=0.and.session%input_id/=request%input_id) go to 900 if(session%input_id/=0.and.session%input_mode/=request%mode) go to 900 + if(request%mode==engine_mode_jt4.and.session%input_id/=0) then + if(audio%sample_count/=session%input_sample_count.or.audio%sample_rate_hz/=session%input_sample_rate) go to 900 + endif if(request%mode==engine_mode_q65.and.session%input_id/=0) then if(audio%sample_count/=session%input_sample_count) go to 900 endif @@ -348,14 +377,19 @@ contains if(session%analysis_id==request%analysis_id.and.request%attempt_no<=session%last_attempt) go to 900 if(size(session%audio)<analysis_samples) then - deallocate(session%audio) - allocate(session%audio(analysis_samples)) + allocate(grown_audio(analysis_samples),stat=allocation_status) + if(allocation_status/=0) then + status=internal_error + go to 900 + endif + call move_alloc(grown_audio,session%audio) endif session%running=.true. session%request=request session%input_id=request%input_id session%input_mode=request%mode session%input_sample_count=audio%sample_count + session%input_sample_rate=audio%sample_rate_hz session%analysis_id=request%analysis_id session%last_attempt=request%attempt_no session%callback=callback @@ -370,7 +404,9 @@ contains context%superfox_sink=>collect_superfox context%sink_user=handle context%render_legacy=render - if(request%mode==engine_mode_ft4) then + if(request%mode==engine_mode_jt4) then + call run_jt4_attempt(request%jt4,request%phase,audio%sample_count,audio%sample_rate_hz,context,progress,status) + else if(request%mode==engine_mode_ft4) then call run_ft4_attempt(request%ft4,request%phase,context,progress) else if(request%mode==engine_mode_jt9) then call run_jt9_attempt(request%jt9,request%phase,audio%sample_count,context,progress) @@ -464,6 +500,151 @@ contains call write_decode_progress(progress) end subroutine + pure logical function valid_hhmmss(utc) result(valid) + integer, intent(in) :: utc + valid=utc>=0.and.utc<=235959.and.mod(utc/100,100)<=59.and.mod(utc,100)<=59 + end function + + logical function valid_jt4_options(options) result(valid) + type(engine_jt4_options), intent(in) :: options + valid=.false. + if(.not.valid_hhmmss(options%utc)) return + if(options%submode<0.or.options%submode>6) return + if(options%minimum_width_profile<0.or.options%minimum_width_profile>options%submode) return + if(options%depth<1.or.options%depth>3) return + if(options%receive_frequency_hz<0.or.options%receive_frequency_hz>5000) return + if(options%tolerance_hz<0.or.options%tolerance_hz>5000) return + if(any([options%averaging,options%deep_search]<0).or.any([options%averaging,options%deep_search]>1)) return + valid=.true. + end function + + subroutine run_jt4_attempt(options,phase,sample_count,sample_rate,callback_context,progress,status) + type(engine_jt4_options), intent(in) :: options + integer, intent(in) :: phase,sample_count,sample_rate,progress + type(decoder_callback_context), intent(in) :: callback_context + integer(c_int), intent(out) :: status + type(decoder_callback_context) :: context + type(jt4_options) :: kernel_options + integer :: kernel_status + + context=callback_context + context%nutc=options%utc + context%utc_is_hhmmss=.true. + context%nfqso=options%receive_frequency_hz + context%submode=options%submode + context%ios13=-1 + context%ios14=-1 + if(context%render_legacy) then + call open_legacy_output(phase,context%ios13) + open(14,file=trim(temp_dir)//'/avemsg.txt',status='unknown',iostat=context%ios14) + call prepare_jt4_host(session%jt4) + endif + session%jt4%context=context + session%jt4%decoded=0 + kernel_options%utc=options%utc + kernel_options%submode=options%submode + kernel_options%receive_frequency_hz=options%receive_frequency_hz + kernel_options%tolerance_hz=options%tolerance_hz + kernel_options%depth=options%depth + kernel_options%min_sync=options%min_sync + kernel_options%minimum_width_profile=options%minimum_width_profile + kernel_options%averaging=options%averaging/=0 + kernel_options%deep_search=options%deep_search/=0 + kernel_options%mycall=transfer(options%mycall,kernel_options%mycall) + kernel_options%hiscall=transfer(options%hiscall,kernel_options%hiscall) + kernel_options%hisgrid=transfer(options%hisgrid,kernel_options%hisgrid) + call timer('decjt4 ',0) + if(context%render_legacy) then + call session%jt4%decode_pcm(kernel_options,session%audio(:sample_count),sample_rate,jt4_decoded, & + kernel_status,write_jt4_diagnostic) + else + call session%jt4%decode_pcm(kernel_options,session%audio(:sample_count),sample_rate,jt4_decoded,kernel_status) + endif + call timer('decjt4 ',1) + if(context%ios14==0) then + call write_jt4_averages(session%jt4,14) + close(14) + endif + if(context%ios13==0) close(13) + call set_decode_completion(session%completion,0,session%jt4%decoded,0) + call write_decode_progress(progress) + status=ok + if(kernel_status/=0) status=internal_error + end subroutine + + integer(c_int) function engine_set_jt4_calls(handle,entries,count) & + bind(C,name='decoder_engine_set_jt4_calls') result(status) + type(c_ptr), value :: handle,entries + integer(c_int), value :: count + type(engine_jt4_call), pointer :: records(:) + type(jt4_call), allocatable :: calls(:) + integer :: i,allocation_status,kernel_status + status=invalid + if(.not.valid_handle(handle)) return + status=busy + if(session%running) return + status=invalid + if(count<0.or.count>6999) return + if(count>0.and..not.c_associated(entries)) return + allocate(calls(count),stat=allocation_status) + status=internal_error + if(allocation_status/=0) return + if(count>0) then + call c_f_pointer(entries,records,[count]) + do i=1,count + calls(i)%call=transfer(records(i)%call,calls(i)%call) + calls(i)%grid=transfer(records(i)%grid,calls(i)%grid) + enddo + endif + call session%jt4%set_calls(calls,kernel_status) + if(kernel_status/=0) return + status=ok + end function + + integer(c_int) function engine_get_jt4_averages(handle,entries,entry_capacity,count) & + bind(C,name='decoder_engine_get_jt4_averages') result(status) + type(c_ptr), value :: handle,entries + integer(c_int), value :: entry_capacity + integer(c_int), intent(out) :: count + type(engine_jt4_average_entry), pointer :: records(:) + type(jt4_average_entry) :: averages(64) + integer :: i,n + count=0 + status=invalid + if(.not.valid_handle(handle)) return + status=busy + if(session%running) return + status=invalid + if(entry_capacity<0) return + if(entry_capacity>0.and..not.c_associated(entries)) return + call session%jt4%get_averages(averages,n) + count=n + if(entry_capacity>0) then + call c_f_pointer(entries,records,[entry_capacity]) + do i=1,min(entry_capacity,n) + records(i)%utc=averages(i)%utc + records(i)%frequency_hz=averages(i)%frequency_hz + records(i)%polarity=averages(i)%polarity + records(i)%used=merge(1,0,averages(i)%used) + records(i)%sync=averages(i)%sync + records(i)%dt_seconds=averages(i)%dt_seconds + enddo + endif + status=ok + if(entry_capacity>0.and.entry_capacity<n) status=capacity + end function + + integer(c_int) function engine_clear_jt4_averages(handle) & + bind(C,name='decoder_engine_clear_jt4_averages') result(status) + type(c_ptr), value :: handle + status=invalid + if(.not.valid_handle(handle)) return + status=busy + if(session%running) return + call session%jt4%clear_averages() + status=ok + end function + logical function valid_jt65_options(options) result(valid) type(engine_jt65_options), intent(in) :: options valid=.false. @@ -613,7 +794,6 @@ contains logical function valid_fst4_options(options) result(valid) type(engine_fst4_options), intent(in) :: options - integer :: hours,minutes,seconds valid=.false. if(analysis_extent(engine_mode_fst4,options%period_seconds)==0) return if(options%depth<1.or.options%depth>3) return @@ -628,10 +808,7 @@ contains if(options%eme_delay_seconds<0.or.options%eme_delay_seconds>real(options%period_seconds)) return if(any([options%single_decode,options%ap_cq_only,options%measure_doppler]<0)) return if(any([options%single_decode,options%ap_cq_only,options%measure_doppler]>1)) return - hours=options%utc/10000 - minutes=mod(options%utc/100,100) - seconds=mod(options%utc,100) - if(options%utc<0.or.hours>23.or.minutes>59.or.seconds>59) return + if(.not.valid_hhmmss(options%utc)) return valid=.true. end function @@ -742,7 +919,6 @@ contains logical function valid_q65_options(options) result(valid) type(engine_q65_options), intent(in) :: options - integer :: hours,minutes,seconds valid=.false. if(.not.any(options%period_seconds==[15,30,60,120,300])) return if(options%submode<0.or.options%submode>5) return @@ -758,10 +934,7 @@ contains options%pileup,options%ap_cq_only]<0)) return if(any([options%averaging,options%auto_clear,options%single_decode,options%extended_eme_search, & options%pileup,options%ap_cq_only]>1)) return - hours=options%utc/10000 - minutes=mod(options%utc/100,100) - seconds=mod(options%utc,100) - if(options%utc<0.or.hours>23.or.minutes>59.or.seconds>59) return + if(.not.valid_hhmmss(options%utc)) return valid=.true. end function @@ -982,11 +1155,13 @@ contains if(input_id<=0.or.session%input_id/=input_id) return call release_ft8_input(session%kernel) call session%jt9%release_input() + call session%jt4%release_input() call session%jt65%release_input() call session%q65%release_input() session%input_id=0 session%input_mode=0 session%input_sample_count=0 + session%input_sample_rate=0 session%analysis_id=0 session%last_attempt=0 session%retained=0 @@ -1003,6 +1178,7 @@ contains call reset_ft8_kernel(session%kernel) call session%ft4%reset() call session%jt9%reset() + call session%jt4%reset() call session%jt65%reset() call session%q65%reset() call session%fst4%reset() @@ -1010,6 +1186,7 @@ contains session%input_id=0 session%input_mode=0 session%input_sample_count=0 + session%input_sample_rate=0 session%analysis_id=0 session%last_attempt=0 session%retained=0 @@ -1105,11 +1282,18 @@ contains type(attempt_outcome) :: outcome type(engine_options) :: options integer(c_int) :: status - integer :: time,history_status,blanker,period_samples + integer :: time,history_status,blanker,period_samples,gate_samples character(len=12), allocatable :: calls(:) character(len=4), allocatable :: grids(:) options%abi_version=engine_abi completion=decode_completion_result(.true.,0,0,0) + if(params%nmode==engine_mode_jt4) then + if(params%nclearave.and.valid_handle(host_handle)) call session%jt4%clear_averages() + gate_samples=min(180000,max(0,valid_samples)) + if(gate_samples>0) then + if(sqrt(sum(real(id2(1:gate_samples))**2)/180000.0)<0.5) return + endif + endif if(params%nmode==engine_mode_ft4) then ! The legacy host's low-signal gate includes its zero-padded 15-second window. if(sqrt(sum(real(id2(1:180000))**2)/180000.0)<0.5) return @@ -1128,7 +1312,7 @@ contains endif if(input_id<=0.or.analysis_id<=0) then if(session%input_id==0.or.params%nutc/=fallback_utc.or.session%input_mode/=request%mode.or. & - ((params%nmode==engine_mode_ft4.or.params%nmode==engine_mode_jt9.or. & + ((params%nmode==engine_mode_jt4.or.params%nmode==engine_mode_ft4.or.params%nmode==engine_mode_jt9.or. & params%nmode==engine_mode_jt65.or.params%nmode==engine_mode_q65.or. & request%mode==engine_mode_fst4.or.request%mode==engine_mode_fst4w).and..not.params%nagain).or. & (params%nzhsym==41.and..not.params%nagain)) then @@ -1153,7 +1337,10 @@ contains if(params%nmode==engine_mode_ft8.and.params%nzhsym<50) request%phase=1 if(params%nagain) request%phase=3 request%source=merge(1,0,params%ndiskdat) - if(request%mode==engine_mode_ft4) then + if(request%mode==engine_mode_jt4) then + call convert_jt4_host_options(params,request%jt4,streaming_emit_enabled()) + if(request%jt4%deep_search/=0) call load_jt4_calls(session%jt4) + else if(request%mode==engine_mode_ft4) then request%ft4%utc=params%nutc request%ft4%qso_progress=params%nQSOProgress request%ft4%receive_frequency_hz=params%nfqso @@ -1304,13 +1491,14 @@ contains request%ft8%hisgrid=params%hisgrid endif audio%samples=c_loc(id2(1)) - period_samples=analysis_extent(request%mode,request%q65%period_seconds) + period_samples=analysis_extent(request%mode,request%q65%period_seconds,nfsample) if(request%mode==engine_mode_fst4.or.request%mode==engine_mode_fst4w) & period_samples=analysis_extent(request%mode,request%fst4%period_seconds) audio%sample_count=min(period_samples,valid_samples) if(valid_samples<=0.and.input_id<=0) then audio%sample_count=period_samples if(request%mode==engine_mode_jt9.or.request%mode==engine_mode_jt65) audio%sample_count=624000 + if(request%mode==engine_mode_jt4) audio%sample_count=52*nfsample endif if(request%mode==engine_mode_ft8) then if(params%lmultift8.and..not.(iand(params%nexp_decode,7)==7.and. & diff --git a/lib/decoder_engine_types.f90 b/lib/decoder_engine_types.f90 index 5c378646..dbf0f2f8 100644 --- a/lib/decoder_engine_types.f90 +++ b/lib/decoder_engine_types.f90 @@ -3,14 +3,14 @@ module decoder_engine_types implicit none private public :: ft8_signal_evidence, ft4_signal_evidence, superfox_evidence, superfox_observation - public :: jt9_result,jt65_result,q65_result,fst4_result + public :: jt9_result,jt65_result,q65_result,fst4_result,jt4_result,engine_jt4_options public :: engine_payload_none,engine_payload_decoded,engine_payload_hypothesis public :: sf_kind_cq,sf_kind_exchange,sf_kind_free_text,sf_kind_verification public :: engine_observation, engine_observation_sink, engine_superfox_sink public :: engine_abi,engine_mode_ft8,engine_mode_ft4,engine_mode_jt9,engine_mode_jt65,engine_mode_q65 public :: engine_support_ft8,engine_support_ft4,engine_support_jt9,engine_support_jt65,engine_support_q65 - integer(c_int), parameter :: engine_abi=7,engine_mode_ft8=8,engine_mode_ft4=5,engine_mode_jt9=9,engine_mode_jt65=65 + integer(c_int), parameter :: engine_abi=8,engine_mode_ft8=8,engine_mode_ft4=5,engine_mode_jt9=9,engine_mode_jt65=65 integer(c_int), parameter :: engine_support_ft8=1,engine_support_ft4=2,engine_support_jt9=4,engine_support_jt65=8 integer(c_int), parameter :: engine_mode_q65=66,engine_support_q65=16 @@ -19,6 +19,19 @@ module decoder_engine_types integer(c_int), parameter :: engine_mode_fst4=240,engine_mode_fst4w=241 integer(c_int), parameter :: engine_support_fst4=32,engine_support_fst4w=64 + public :: engine_mode_jt4,engine_support_jt4 + integer(c_int), parameter :: engine_mode_jt4=4,engine_support_jt4=128 + + type, bind(C) :: engine_jt4_options + integer(c_int) :: utc=0,submode=0,receive_frequency_hz=1500,tolerance_hz=100,depth=1,min_sync=0 + integer(c_int) :: minimum_width_profile=0,averaging=0,deep_search=0 + character(c_char) :: mycall(12)=' ',hiscall(12)=' ',hisgrid(6)=' ' + end type + + type, bind(C) :: jt4_result + integer(c_int) :: kind=0,method=0,average_count=0,sync_polarity=0,width_profile=0 + end type + integer, parameter :: engine_payload_none=0,engine_payload_decoded=1,engine_payload_hypothesis=2 integer, parameter :: sf_kind_cq=1,sf_kind_exchange=2,sf_kind_free_text=3,sf_kind_verification=4 @@ -82,6 +95,7 @@ module decoder_engine_types type(jt65_result) :: jt65 type(q65_result) :: q65 type(fst4_result) :: fst4 + type(jt4_result) :: jt4 end type abstract interface diff --git a/lib/deep4.f90 b/lib/deep4.f90 deleted file mode 100644 index 05f6732d..00000000 --- a/lib/deep4.f90 +++ /dev/null @@ -1,169 +0,0 @@ -subroutine deep4(sym0,neme,flip,mycall,hiscall,hisgrid,decoded,qual) - -! Deep search routine for JT4 - - use prog_args - parameter (MAXCALLS=7000,MAXRPT=63) - real*4 sym0(206),sym(206) - character callsign*12,grid*4,message*22,hisgrid*6,ceme*3 - character*12 mycall,hiscall - character mycall0*12,hiscall0*12,hisgrid0*6 - character*22 decoded - character*22 testmsg(2*MAXCALLS + 2 + MAXRPT) - character*15 callgrid(MAXCALLS) - character*180 line - character*4 rpt(MAXRPT) - integer ncode(206) - real*4 code(206,2*MAXCALLS + 2 + MAXRPT) - real pp(2*MAXCALLS + 2 + MAXRPT) - data neme0/-99/ - data rpt/'-01','-02','-03','-04','-05', & - '-06','-07','-08','-09','-10', & - '-11','-12','-13','-14','-15', & - '-16','-17','-18','-19','-20', & - '-21','-22','-23','-24','-25', & - '-26','-27','-28','-29','-30', & - 'R-01','R-02','R-03','R-04','R-05', & - 'R-06','R-07','R-08','R-09','R-10', & - 'R-11','R-12','R-13','R-14','R-15', & - 'R-16','R-17','R-18','R-19','R-20', & - 'R-21','R-22','R-23','R-24','R-25', & - 'R-26','R-27','R-28','R-29','R-30', & - 'RO','RRR','73'/ - save mycall0,hiscall0,hisgrid0,neme0,ntot,code,testmsg - - sym=sym0 - if(mycall.eq.mycall0 .and. hiscall.eq.hiscall0 .and. & - hisgrid.eq.hisgrid0 .and. neme.eq.neme0) go to 30 - - open(23,file=trim(data_dir)//'/CALL3.TXT',status='unknown') - k=0 - icall=0 - do n=1,MAXCALLS - if(n.eq.1) then - callsign=hiscall - do i=4,12 - if(ichar(callsign(i:i)).eq.0) callsign(i:i)=' ' - enddo - grid=hisgrid(1:4) - if(ichar(grid(3:3)).eq.0) grid(3:3)=' ' - if(ichar(grid(4:4)).eq.0) grid(4:4)=' ' - else - read(23,1002,end=20) line -1002 format (A80) - if(line(1:4).eq.'ZZZZ') go to 20 - if(line(1:2).eq.'//') go to 10 - i1=index(line,',') - if(i1.lt.4) go to 10 - i2=index(line(i1+1:),',') - if(i2.lt.5) go to 10 - i2=i2+i1 - i3=index(line(i2+1:),',') - if(i3.lt.1) i3=index(line(i2+1:),' ') - i3=i2+i3 - callsign=line(1:i1-1) - grid=line(i1+1:i2-1) - ceme=line(i2+1:i3-1) - if(neme.eq.1 .and. ceme.ne.'EME') go to 10 - endif - - icall=icall+1 - j1=index(mycall,' ') - 1 - if(j1.le.-1) j1=12 - if(j1.lt.3) j1=6 - j2=index(callsign,' ') - 1 - if(j2.le.-1) j2=12 - if(j2.lt.3) j2=6 - j3=index(mycall,'/') ! j3>0 means compound mycall - j4=index(callsign,'/') ! j4>0 means compound hiscall - callgrid(icall)=callsign(1:j2) - - mz=1 -! Allow MyCall + HisCall + rpt (?) - if(n.eq.1 .and. j3.lt.1 .and. j4.lt.1 .and. callsign(1:6).ne.' ') & - mz=MAXRPT+1 - do m=1,mz - if(m.gt.1) grid=rpt(m-1) - if(j3.lt.1 .and.j4.lt.1) callgrid(icall)=callsign(1:j2)//' '//grid - message=mycall(1:j1)//' '//callgrid(icall) - k=k+1 - testmsg(k)=message - call encode4(message,ncode) - code(1:206,k)=2*ncode(1:206)-1 - if(n.ge.2) then -! Insert CQ message - if(j4.lt.1) callgrid(icall)=callsign(1:j2)//' '//grid - message='CQ '//callgrid(icall) - k=k+1 - testmsg(k)=message - call encode4(message,ncode) - code(1:206,k)=2*ncode(1:206)-1 - endif - enddo -10 continue - enddo - -20 continue - close(23) - ntot=k - -30 mycall0=mycall - hiscall0=hiscall - hisgrid0=hisgrid - neme0=neme - - sq=0. - do j=1,206 - sq=sq + sym(j)**2 - enddo - rms=sqrt(sq/206.0) - sym=sym/rms - - p1=-1.e30 - p2=-1.e30 - do k=1,ntot - pp(k)=0. -! Should re-instate the following: -! if(k.ge.2 .and. k.le.64 .and. flip.gt.0.0) cycle -! Test all messages if flip=+1; skip the CQ messages if flip=-1. - if(flip.gt.0.0 .or. testmsg(k)(1:3).ne.'CQ ') then - p=0. - do j=1,206 - p=p + code(j,k)*sym(j) - enddo - pp(k)=p - if(p.gt.p1) then - p1=p - ip1=k - endif - endif - enddo - - do i=1,ntot - if(pp(i).gt.p2 .and. testmsg(i).ne.testmsg(ip1)) p2=pp(i) - enddo - - qual=p1-max(1.15*p2,70.0) - -! ### DO NOT REMOVE ### - rewind 77 - if(ip1.ge.1 .and. ip1.le.2*MAXCALLS+2+MAXRPT) write(77,1001) p1,p2,ntot, & - rms,qual,ip1,testmsg(ip1) -1001 format(2f8.2,i8,2f8.2,i6,2x,a22) - call flush(77) - - if(qual.gt.1.0) then - decoded=testmsg(ip1) - else - decoded=' ' - qual=0. - endif - -! Make sure everything is upper case. - do i=1,22 - if(decoded(i:i).ge.'a' .and. decoded(i:i).le.'z') & - decoded(i:i)=char(ichar(decoded(i:i))-32) - enddo - - return -end subroutine deep4 diff --git a/lib/extract4.f90 b/lib/extract4.f90 index ce8fb86a..b10d9da9 100644 --- a/lib/extract4.f90 +++ b/lib/extract4.f90 @@ -14,21 +14,23 @@ subroutine extract4(sym0,ncount,decoded) data first/.true./ save first,mettab,ndelta + ncount=-1 + decoded=' ' + ave0=sum(sym0)/207.0 + sym=sym0-ave0 + sq=dot_product(sym,sym) + rms0=sqrt(sq/206.0) + if(rms0<=tiny(rms0)) return + sym=sym/rms0 + if(first) then call getmet4(mettab,ndelta) first=.false. endif -!### Optimize these params: ... amp=30.0 limit=10000 - ave0=sum(sym0)/207.0 - sym=sym0-ave0 - sq=dot_product(sym,sym) - rms0=sqrt(sq/206.0) - sym=sym/rms0 - do j=1,207 n=nint(amp*sym(j)) if(n.lt.-127) n=-127 @@ -38,8 +40,6 @@ subroutine extract4(sym0,ncount,decoded) nbits=72 ncycles=0 - ncount=-1 - decoded=' ' call interleave4(symbol(2),-1) !Remove the interleaving call fano232(symbol(2),nbits+31,mettab,ndelta,limit,data1, & ncycles,metric,ncount) diff --git a/lib/jt4.f90 b/lib/jt4.f90 index 42a2d16b..b88f0242 100644 --- a/lib/jt4.f90 +++ b/lib/jt4.f90 @@ -1,29 +1,13 @@ module jt4 - parameter (MAXAVE=64) - integer iutc(MAXAVE) - integer nfsave(MAXAVE) - integer listutc(10) - real ppsave(207,7,MAXAVE) !Accumulated data for message averaging - real rsymbol(207,7) - real dtsave(MAXAVE) - real syncsave(MAXAVE) - real flipsave(MAXAVE) - real zz(1260,65,7) - - integer nsave,nlist,ich1,ich2 - integer nch(7) - integer npr(207) - data rsymbol/1449*0.0/ - data nch/1,2,4,9,18,36,72/ - data npr/ & + implicit none + integer, parameter :: MAXAVE=64 + integer, parameter :: nch(7)=[1,2,4,9,18,36,72] + integer, parameter :: npr(207)=[ & 0,0,0,0,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,0,0,0,0,0,0,1,1,0,0, & 0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,0,0, & 1,0,0,1,0,0,1,1,1,1,1,0,0,0,1,0,1,0,0,0,1,1,1,1,0,1,1,0,0,1, & 0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1,0,1,0,1, & 0,1,1,1,0,0,1,0,1,1,0,1,1,1,1,0,0,0,0,1,1,0,1,1,0,0,0,1,1,1, & 0,1,1,1,0,1,1,1,0,0,1,0,0,0,1,1,0,1,1,0,0,1,0,0,0,1,1,1,1,1, & - 1,0,0,1,1,0,0,0,0,1,1,0,0,0,1,0,1,1,0,1,1,1,1,0,1,0,1/ - - save ppsave,zz - + 1,0,0,1,1,0,0,0,0,1,1,0,0,0,1,0,1,1,0,1,1,1,1,0,1,0,1 ] end module jt4 diff --git a/lib/jt4_decode.f90 b/lib/jt4_decode.f90 index 8ab48bda..60c2886a 100644 --- a/lib/jt4_decode.f90 +++ b/lib/jt4_decode.f90 @@ -1,13 +1,60 @@ module jt4_decode + use iso_c_binding, only: c_ptr,c_null_ptr,c_associated,c_f_pointer,c_int16_t,c_size_t + use fftw3 + use jt4, only: nch,npr,MAXAVE + + type :: jt4_options + integer :: utc=0,submode=0,receive_frequency_hz=1200,tolerance_hz=100,depth=1 + integer :: min_sync=0,minimum_width_profile=0 + logical :: averaging=.false.,deep_search=.false. + character(len=12) :: mycall='',hiscall='' + character(len=6) :: hisgrid='' + end type + type :: jt4_call + character(len=12) :: call='' + character(len=4) :: grid='' + end type + type :: jt4_average_entry + integer :: utc=0,frequency_hz=0,polarity=1 + logical :: used=.false. + real :: sync=0.,dt_seconds=0. + end type + type, private :: jt4_fft_buffer + type(c_ptr) :: storage=c_null_ptr,forward=c_null_ptr,backward=c_null_ptr + real, pointer :: x(:)=>null() + complex, pointer :: cx(:)=>null() + end type type :: jt4_decoder procedure(jt4_decode_callback), pointer :: decode_callback => null () - procedure(jt4_average_callback), pointer :: average_callback => null () + procedure(jt4_diagnostic_callback), pointer, nopass :: diagnostic_callback => null () + integer :: iutc(MAXAVE)=-1,nfsave(MAXAVE)=0,nsave=0,average_count=0 + real :: dtsave(MAXAVE)=0.,syncsave(MAXAVE)=0.,flipsave(MAXAVE)=0. + logical :: average_used(MAXAVE)=.false. + real, allocatable :: ppsave(:,:,:),rsymbol(:,:) + real, allocatable, private :: dat(:),pcm(:),s2(:,:) + integer, private :: ich1=1,ich2=1,current_submode=-1,current_minimum_profile=-1 + integer, private :: fft_flags=FFTW_ESTIMATE + real :: sync_strength=0. + type(jt4_fft_buffer), private :: transform(4) + type(jt4_call), allocatable :: calls(:) + real, allocatable, private :: code(:,:) + character(len=22), allocatable, private :: testmsg(:) + character(len=12), private :: mycall0='',hiscall0='' + character(len=6), private :: hisgrid0='' + logical, private :: templates_valid=.false.,prepared=.false. + integer, private :: ntot=0,kernel_status=0 contains - procedure :: decode - procedure, private :: wsjt4, avg4 + procedure :: decode_pcm,set_calls,get_averages,clear_averages,release_input,reset,set_fft_flags + procedure :: save_average,select_average + procedure, private :: wsjt4,avg4,decode4,deep4,sync4,lpf1,ps4,getsnr,prepare_workspace,destroy_plans + final :: finalize_jt4 end type jt4_decoder -! Callback function to be called with each decode + abstract interface + subroutine jt4_diagnostic_callback(line) + character(len=*),intent(in) :: line + end subroutine + end interface abstract interface subroutine jt4_decode_callback (this, snr, dt, freq, have_sync, & sync, is_deep, decoded, qual, ich, is_average, ave) @@ -28,136 +75,57 @@ module jt4_decode end subroutine jt4_decode_callback end interface -! Callback function to be called with each average result - abstract interface - subroutine jt4_average_callback (this, used, utc, sync, dt, freq, flip) - import jt4_decoder - implicit none - class(jt4_decoder), intent(inout) :: this - logical, intent(in) :: used - integer, intent(in) :: utc - real, intent(in) :: sync - real, intent(in) :: dt - integer, intent(in) :: freq - logical, intent(in) :: flip - end subroutine jt4_average_callback - end interface contains - subroutine decode(this,decode_callback,dd,jz,nutc,nfqso,ntol0,emedelay, & - dttol,nagain,ndepth,nclearave,minsync,minw,nsubmode,mycall,hiscall, & - hisgrid,nlist0,listutc0,average_callback) + subroutine wsjt4(this,dat,npts,nutc,minsync,ntol,mode4,minw,mycall,hiscall,hisgrid,nfqso,ndepth) - use jt4 - use timer_module, only: timer +! JT4 searches for one signal within the selected frequency tolerance. - class(jt4_decoder), intent(inout) :: this - procedure(jt4_decode_callback) :: decode_callback - integer, intent(in) :: jz,nutc,nfqso,ntol0,ndepth,minsync,minw,nsubmode, & - nlist0,listutc0(10) - real, intent(in) :: dd(jz),emedelay,dttol - logical, intent(in) :: nagain, nclearave - character(len=12), intent(in) :: mycall,hiscall - character(len=6), intent(in) :: hisgrid - procedure(jt4_average_callback), optional :: average_callback - real*4 dat(30*11025) - character*6 cfile6 - save dat - - this%decode_callback => decode_callback - if (present (average_callback)) then - this%average_callback => average_callback - end if - mode4=nch(nsubmode+1) - ntol=ntol0 - neme=0 - lumsg=6 !### temp ? ### - ndiag=1 - nlist=nlist0 - listutc=listutc0 - - ! Lowpass filter and decimate by 2 - call timer('lpf1 ',0) - call lpf1(dd,jz,dat,jz2) - call timer('lpf1 ',1) - - write(cfile6(1:4),1000) nutc -1000 format(i4.4) - cfile6(5:6)=' ' - - call timer('wsjt4 ',0) - call this%wsjt4(dat,jz2,nutc,NClearAve,minsync,ntol,emedelay,dttol,mode4, & - minw,mycall,hiscall,hisgrid,nfqso,NAgain,ndepth,neme) - call timer('wsjt4 ',1) - - return - end subroutine decode - - subroutine wsjt4(this,dat,npts,nutc,NClearAve,minsync,ntol,emedelay,dttol, & - mode4,minw,mycall,hiscall,hisgrid,nfqso,NAgain,ndepth,neme) - -! Orchestrates the process of decoding JT4 messages. Note that JT4 -! always operates as if in "Single Decode" mode; it looks for only one -! decodable signal in the FTol range. - - use jt4 use timer_module, only: timer class(jt4_decoder), intent(inout) :: this integer, intent(in) :: npts,nutc,minsync,ntol,mode4,minw, & - nfqso,ndepth,neme - logical, intent(in) :: NAgain,NClearAve + nfqso,ndepth character(len=12), intent(in) :: mycall,hiscall character(len=6), intent(in) :: hisgrid real, intent(in) :: dat(npts) !Raw data - logical first,prtavg - character decoded*22,special*5 + logical prtavg,have_candidate + character decoded*22 character*22 avemsg,deepmsg,deepave,blank,deepmsg0,deepave1 character csync*1 - data first/.true./,nutc0/-999/,nfreq0/-999999/ - save - - if(first) then - nsave=0 - first=.false. - blank=' ' -! Silence compiler warnings - if(dttol.eq.-99.0 .and. emedelay.eq.-99.0 .and. nagain) stop - endif - zz=0. -! syncmin=3.0 + minsync + blank=' ' + decoded=blank + deepmsg=blank + deepmsg0=blank + deepave1=blank + avemsg=blank + csync=' ' + ich=0 + ich0=-1 + aveich=-1 + nfanoave=0 + ndeepave=0 + dtx0=0. + dtx1=0. + nfreq0=nfqso + nfreq1=nfqso syncmin=1.0+minsync - naggressive=0 - if(iand(ndepth,7).ge.2) naggressive=1 nq1=3 - nq2=6 - if(naggressive.eq.1) nq1=1 - if(NClearAve) then - nsave=0 - iutc=-1 - nfsave=0. - listutc=0 - ppsave=0. - rsymbol=0. - dtsave=0. - syncsave=0. - nfanoave=0 - ndeepave=0 - endif + if(iand(ndepth,7).ge.2) nq1=1 ! Attempt to synchronize: look for sync pattern, get DF and DT. call timer('sync4 ',0) - call sync4(dat,npts,ntol,nfqso,4,mode4,minw+1,dtx,dfx, & - snrx,snrsync,flip,width) + call this%sync4(dat,npts,ntol,nfqso,mode4,minw+1,dtx,dfx,snrsync,flip,have_candidate) sync=snrsync + this%sync_strength=sync dtxz=dtx-0.8 nfreqz=nint(dfx) call timer('sync4 ',1) nsnr=-26 - if(sync.lt.syncmin) then + if(.not.have_candidate.or.sync.lt.syncmin) then if (associated (this%decode_callback)) then call this%decode_callback(nsnr,dtxz,nfreqz,.false.,csync, & .false.,decoded,0.,ich,.false.,0) @@ -169,9 +137,6 @@ contains nsnr=nint(snrsync - 22.9) decoded=blank deepmsg=blank - special=' ' - nsync=sync - nsnrlim=-33 csync='*' if(flip.lt.0.0) csync='#' qbest=0. @@ -185,12 +150,12 @@ contains ! Attempt a single-sequence decode, including deep4 if Fano fails. call timer('decode4 ',0) - call decode4(dat,npts,dtx,nfreq,flip,mode4,ndepth,neme,minw, & + call this%decode4(dat,npts,dtx,nfreq,flip,mode4,ndepth,minw, & mycall,hiscall,hisgrid,decoded,nfano,deepmsg,qual,ich) call timer('decode4 ',1) if(nfano.gt.0) then - call getsnr(dat,npts,mode4,dtx,decoded,nsnr) + call this%getsnr(dat,npts,mode4,dtx,decoded,nsnr) ! Fano succeeded: report the message and return !Fano OK if (associated (this%decode_callback)) then call this%decode_callback(nsnr,dtx,nfreq,.true.,csync, & @@ -213,18 +178,12 @@ contains qave=0. ! If we're doing averaging, call avg4 if(iand(ndepth,16).eq.16 .and. (.not.prtavg)) then - if(nutc.ne.nutc0 .or. abs(nfreq-nfreq0).gt.ntol) then -! This is a new minute or a new frequency, so call avg4. - nutc0=nutc !Try decoding average - nfreq0=nfreq - nsave=nsave+1 - nsave=mod(nsave-1,64)+1 - call timer('avg4 ',0) - call this%avg4(nutc,sync,dtx,flip,nfreq,mode4,ntol,ndepth,neme, & - mycall,hiscall,hisgrid,nfanoave,avemsg,qave,deepave,ich, & - ndeepave) - call timer('avg4 ',1) - endif + call this%save_average(nutc,sync,dtx,flip,nfreq,ntol) + if(this%kernel_status/=0) return + call timer('avg4 ',0) + call this%avg4(nutc,dtx,flip,nfreq,mode4,ntol,ndepth, & + mycall,hiscall,hisgrid,nfanoave,avemsg,qave,deepave,ich,ndeepave) + call timer('avg4 ',1) if(nfanoave.gt.0) then ! Fano succeeded: report the message AVG FANO OK @@ -239,7 +198,7 @@ contains dtx1=dtx nfreq1=nfreq deepave1=deepave - ich1=ich + aveich=ich qabest=qave endif endif @@ -253,7 +212,7 @@ contains qual=qbest if (associated (this%decode_callback)) then - if(qual.gt.0.0) call getsnr(dat,npts,mode4,dtx,deepmsg,nsnr) + if(qual.gt.0.0) call this%getsnr(dat,npts,mode4,dtx,deepmsg,nsnr) if(int(qual).ge.nq1) then call this%decode_callback(nsnr,dtx,nfreqz,.true.,csync,.true., & deepmsg,qual,ich,.false.,0) @@ -266,7 +225,7 @@ contains dtx=dtx1 nfreq=nfreq1 deepave=deepave1 - ich=ich1 + ich=aveich qave=qabest if (associated (this%decode_callback) .and. ndeepave.ge.2) then @@ -279,103 +238,27 @@ contains 990 return end subroutine wsjt4 - subroutine avg4(this,nutc,snrsync,dtxx,flip,nfreq,mode4,ntol,ndepth,neme, & + subroutine avg4(this,nutc,dtxx,flip,nfreq,mode4,ntol,ndepth, & mycall,hiscall,hisgrid,nfanoave,avemsg,qave,deepave,ichbest,ndeepave) ! Decodes averaged JT4 data - use jt4 class(jt4_decoder), intent(inout) :: this character*22 avemsg,deepave,deepbest character mycall*12,hiscall*12,hisgrid*6 - character*1 csync,cused(64) real sym(207,7) - integer iused(64) - logical first - data first/.true./ - save - - if(first) then - iutc=-1 - nfsave=0 - dtdiff=0.2 - first=.false. - nsave=1 ! ### Should this be here? ### - endif - - do i=1,64 - if(nutc.eq.iutc(i) .and. abs(nfreq-nfsave(i)).le.ntol) go to 10 - enddo - -! Save data for message averaging - iutc(nsave)=nutc - syncsave(nsave)=snrsync - dtsave(nsave)=dtxx - nfsave(nsave)=nfreq - flipsave(nsave)=flip - ppsave(1:207,1:7,nsave)=rsymbol(1:207,1:7) - -10 sym=0. - syncsum=0. - dtsum=0. - nfsum=0 - nsum=0 - - do i=1,64 - cused(i)='.' - if(iutc(i).lt.0) cycle - if(mod(iutc(i),2).ne.mod(nutc,2)) cycle !Use only same sequence - if(abs(dtxx-dtsave(i)).gt.dtdiff) cycle !DT must match - if(abs(nfreq-nfsave(i)).gt.ntol) cycle !Freq must match - if(flip.ne.flipsave(i)) cycle !Sync (*/#) must match - sym(1:207,1:7)=sym(1:207,1:7) + ppsave(1:207,1:7,i) - syncsum=syncsum + syncsave(i) - dtsum=dtsum + dtsave(i) - nfsum=nfsum + nfsave(i) - cused(i)='$' - nsum=nsum+1 - iused(nsum)=i - enddo - if(nsum.lt.64) iused(nsum+1)=0 - - syncave=0. - dtave=0. - fave=0. - if(nsum.gt.0) then - sym=sym/nsum - syncave=syncsum/nsum - dtave=dtsum/nsum - fave=float(nfsum)/nsum - endif - - do i=1,nsave - csync='*' - if(flipsave(i).lt.0.0) csync='#' - if (associated (this%average_callback)) then - call this%average_callback(cused(i) .eq. '$',iutc(i), & - syncsave(i),dtsave(i),nfsave(i),flipsave(i).lt.0.) - end if - enddo - - sqt=0. - sqf=0. - do j=1,64 - i=iused(j) - if(i.eq.0) exit - csync='*' - if(flipsave(i).lt.0.0) csync='#' - sqt=sqt + (dtsave(i)-dtave)**2 - sqf=sqf + (nfsave(i)-fave)**2 - enddo - rmst=0. - rmsf=0. - if(nsum.ge.2) then - rmst=sqrt(sqt/(nsum-1)) - rmsf=sqrt(sqf/(nsum-1)) - endif - kbest=ich1 - do k=ich1,ich2 + call this%select_average(nutc,dtxx,flip,nfreq,ntol,sym,nsum) + nfanoave=0 + ndeepave=0 + avemsg=' ' + deepave=' ' + deepbest=' ' + qave=0. + ichbest=-1 + if(nsum==0) return + kbest=this%ich1 + do k=this%ich1,this%ich2 call extract4(sym(1,k),ncount,avemsg) !Do the Fano decode nfanoave=0 if(ncount.ge.0) then @@ -389,12 +272,11 @@ contains deepave=' ' qave=0. -! Possibly should pass nadd=nused, also ? if(iand(ndepth,32).eq.32) then flipx=1.0 !Normal flip not relevant for ave msg qbest=0. - do k=ich1,ich2 - call deep4(sym(2,k),neme,flipx,mycall,hiscall,hisgrid,deepave,qave) + do k=this%ich1,this%ich2 + call this%deep4(sym(2,k),flipx,mycall,hiscall,hisgrid,deepave,qave) if(qave.gt.qbest) then qbest=qave deepbest=deepave @@ -411,35 +293,35 @@ contains 900 return end subroutine avg4 -end module jt4_decode - -subroutine getsnr(dat,npts,mode4,dtx,decoded,nsnr) +subroutine getsnr(this,dat,npts,mode4,dtx,decoded,nsnr) ! Compute SNR using raw data, T, DF, and decoded message. - + + class(jt4_decoder),intent(inout) :: this parameter(NSPS=1260,NH=NSPS/2) real dat(npts) - real x(NSPS) + real,pointer :: x(:) real s(NH),savg(NH) - complex cx(0:NSPS/2) + complex,pointer :: cx(:) integer itone(206) integer ipk(1) character*22 decoded,msgsent - equivalence (x,cx) + x=>this%transform(4)%x + cx=>this%transform(4)%cx call gen4(decoded,0,msgsent,itone,itype) !Get itone values for this message dt=1.0/5512.5 df=5512.5/NSPS istart=(dtx+1.08)/dt - + savg=0. do j=1,206 !Loop over all symbols cx=0. do i=1,NSPS k=(j-1)*NSPS + i + istart if(k.ge.1 .and. k.le.npts) x(i)=dat(k) - enddo - call four2a(cx,NSPS,1,-1,0) !r2c FFT + enddo + call fftwf_execute_dft_r2c(this%transform(4)%forward,x,cx) !r2c FFT do i=1,nh s(i)=real(cx(i))**2 + aimag(cx(i))**2 enddo @@ -449,35 +331,744 @@ subroutine getsnr(dat,npts,mode4,dtx,decoded,nsnr) nskip=mode4/2 call averms(savg,nh,nskip,ave,rms) + if(rms<=tiny(rms)) then + nsnr=-33 + return + endif savg=(savg-ave)/rms do i=1,mode4/4 call smo121(savg,nh) enddo - + ipk=maxloc(savg) psig=0. do i=ipk(1),1,-1 if(savg(i).lt.0) exit psig=psig + savg(i) enddo - i1=i+1 do i=ipk(1),nh if(savg(i).lt.0) exit psig=psig + savg(i) enddo - i2=i-1 pnoise=2500.0*sqrt(206.0)/df snrdb=db(psig/pnoise) nsnr=nint(snrdb) - width=(i2-i1)*df -! rewind(62) -! do i=1,nh -! write(62,1010) i*df,savg(i) -!1010 format(2f10.3) -! enddo -! flush(62) - return end subroutine getsnr + +subroutine decode4(this,dat,npts,dtx,nfreq,flip,mode4,ndepth,minw, & + mycall,hiscall,hisgrid,decoded,nfano,deepbest,qbest,ichbest) + +! Decodes JT4 data, assuming that DT and DF have already been determined. +! Input dat(npts) has already been downsampled by 2: rate = 11025/2. + + class(jt4_decoder),intent(inout) :: this + real dat(npts) !Raw data + character decoded*22,deepmsg*22,deepbest*22 + character*12 mycall,hiscall + character*6 hisgrid + real*8 dt,df,phi,f0,dphi,twopi,phi1,dphi1 + complex*16 cz,cz1,c0,c1 + real*4 sym(207) + + twopi=8*atan(1.d0) + dt=2.d0/11025 !Sample interval (2x downsampled data) + df=11025.d0/2520.d0 !Tone separation for JT4A mode + nsym=206 + amp=15.0 + istart=nint((dtx+0.8)/dt) !Start index for synced FFTs + if(istart.lt.0) istart=0 + nchips=0 + nfano=0 + decoded=' ' + deepbest=' ' + qbest=0. + deepmsg=' ' + ichbest=-1 + c0=0. + k=istart + phi=0.d0 + phi1=0.d0 + + this%ich1=minw+1 + do ich=1,7 + if(nch(ich).le.mode4) this%ich2=ich + enddo + + do ich=this%ich1,this%ich2 + nchips=min(nch(ich),70) + nspchip=1260/nchips + k=istart + phi=0.d0 + phi1=0.d0 + fac2=1.e-8 * sqrt(float(mode4)) + do j=1,nsym+1 + if(flip.gt.0.0) then + f0=nfreq + (npr(j))*mode4*df + f1=nfreq + (2+npr(j))*mode4*df + else + f0=nfreq + (1-npr(j))*mode4*df + f1=nfreq + (3-npr(j))*mode4*df + endif + dphi=twopi*dt*f0 + dphi1=twopi*dt*f1 + sq0=0. + sq1=0. + do nc=1,nchips + phi=0.d0 + phi1=0.d0 + c0=0. + c1=0. + do i=1,nspchip + k=k+1 + phi=phi+dphi + phi1=phi1+dphi1 + cz=dcmplx(cos(phi),-sin(phi)) + cz1=dcmplx(cos(phi1),-sin(phi1)) + if(k.le.npts) then + c0=c0 + dat(k)*cz + c1=c1 + dat(k)*cz1 + endif + enddo + sq0=sq0 + real(c0)**2 + aimag(c0)**2 + sq1=sq1 + real(c1)**2 + aimag(c1)**2 + enddo + sq0=fac2*sq0 + sq1=fac2*sq1 + rsym=amp*(sq1-sq0) + this%rsymbol(j,ich)=rsym + sym(j)=rsym + enddo + + call extract4(sym,ncount,decoded) !Do the convolutional decode + nfano=0 + if(ncount.ge.0) then + nfano=1 + ichbest=ich + exit + endif + + qual=0. !Now try deep search + if(iand(ndepth,32).eq.32) then + call this%deep4(sym(2),flip,mycall,hiscall,hisgrid,deepmsg,qual) + if(qual.gt.qbest) then + qbest=qual + deepbest=deepmsg + ichbest=ich + endif + endif + enddo + +end subroutine decode4 + +subroutine deep4(this,sym0,flip,mycall,hiscall,hisgrid,decoded,qual) + +! Deep search routine for JT4 + + class(jt4_decoder),intent(inout) :: this + parameter (MAXCALLS=7000,MAXRPT=63) + real*4 sym0(206),sym(206) + character callsign*12,grid*4,message*22,hisgrid*6 + character*12 mycall,hiscall + character*22 decoded + character(len=15) :: callgrid + character*180 line + character*4 rpt(MAXRPT) + integer ncode(206) + real :: pp(2*MAXCALLS + 2 + MAXRPT) + integer :: allocation_status + data rpt/'-01','-02','-03','-04','-05', & + '-06','-07','-08','-09','-10', & + '-11','-12','-13','-14','-15', & + '-16','-17','-18','-19','-20', & + '-21','-22','-23','-24','-25', & + '-26','-27','-28','-29','-30', & + 'R-01','R-02','R-03','R-04','R-05', & + 'R-06','R-07','R-08','R-09','R-10', & + 'R-11','R-12','R-13','R-14','R-15', & + 'R-16','R-17','R-18','R-19','R-20', & + 'R-21','R-22','R-23','R-24','R-25', & + 'R-26','R-27','R-28','R-29','R-30', & + 'RO','RRR','73'/ + + sym=sym0 + if(mycall.eq.this%mycall0 .and. hiscall.eq.this%hiscall0 .and. & + hisgrid.eq.this%hisgrid0 .and. this%templates_valid) go to 30 + + if(.not.allocated(this%code)) then + allocate(this%code(206,64+2*size(this%calls)),this%testmsg(64+2*size(this%calls)),stat=allocation_status) + if(allocation_status/=0) then + this%kernel_status=-1 + if(allocated(this%code)) deallocate(this%code) + if(allocated(this%testmsg)) deallocate(this%testmsg) + decoded=' ' + qual=0. + return + endif + endif + k=0 + do n=1,1+size(this%calls) + if(n.eq.1) then + callsign=hiscall + do i=4,12 + if(ichar(callsign(i:i)).eq.0) callsign(i:i)=' ' + enddo + grid=hisgrid(1:4) + if(ichar(grid(3:3)).eq.0) grid(3:3)=' ' + if(ichar(grid(4:4)).eq.0) grid(4:4)=' ' + else + callsign=this%calls(n-1)%call + grid=this%calls(n-1)%grid + endif + + j1=index(mycall,' ') - 1 + if(j1.le.-1) j1=12 + if(j1.lt.3) j1=6 + j2=index(callsign,' ') - 1 + if(j2.le.-1) j2=12 + if(j2.lt.3) j2=6 + j3=index(mycall,'/') ! j3>0 means compound mycall + j4=index(callsign,'/') ! j4>0 means compound hiscall + callgrid=callsign(1:j2) + + mz=1 +! The current contact also supplies report and acknowledgement templates. + if(n.eq.1 .and. j3.lt.1 .and. j4.lt.1 .and. callsign(1:6).ne.' ') & + mz=MAXRPT+1 + do m=1,mz + if(m.gt.1) grid=rpt(m-1) + if(j3.lt.1 .and.j4.lt.1) callgrid=callsign(1:j2)//' '//grid + message=mycall(1:j1)//' '//callgrid + k=k+1 + this%testmsg(k)=message + call encode4(message,ncode) + this%code(1:206,k)=2*ncode(1:206)-1 + if(n.ge.2) then +! Insert CQ message + if(j4.lt.1) callgrid=callsign(1:j2)//' '//grid + message='CQ '//callgrid + k=k+1 + this%testmsg(k)=message + call encode4(message,ncode) + this%code(1:206,k)=2*ncode(1:206)-1 + endif + enddo + enddo + + this%ntot=k + +30 this%mycall0=mycall + this%hiscall0=hiscall + this%hisgrid0=hisgrid + this%templates_valid=.true. + + sq=0. + do j=1,206 + sq=sq + sym(j)**2 + enddo + rms=sqrt(sq/206.0) + decoded=' ' + qual=0. + if(rms<=tiny(rms)) return + sym=sym/rms + ip1=1 + + p1=-1.e30 + p2=-1.e30 + do k=1,this%ntot + pp(k)=0. +! Test all messages if flip=+1; skip the CQ messages if flip=-1. + if(flip.gt.0.0 .or. this%testmsg(k)(1:3).ne.'CQ ') then + p=0. + do j=1,206 + p=p + this%code(j,k)*sym(j) + enddo + pp(k)=p + if(p.gt.p1) then + p1=p + ip1=k + endif + endif + enddo + + do i=1,this%ntot + if(pp(i).gt.p2 .and. this%testmsg(i).ne.this%testmsg(ip1)) p2=pp(i) + enddo + + qual=p1-max(1.15*p2,70.0) + + if(associated(this%diagnostic_callback)) then + write(line,1001) p1,p2,this%ntot,rms,qual,ip1,this%testmsg(ip1) +1001 format(2f8.2,i8,2f8.2,i6,2x,a22) + call this%diagnostic_callback(trim(line)) + endif + + if(qual.gt.1.0) then + decoded=this%testmsg(ip1) + else + decoded=' ' + qual=0. + endif + +! Make sure everything is upper case. + do i=1,22 + if(decoded(i:i).ge.'a' .and. decoded(i:i).le.'z') & + decoded(i:i)=char(ichar(decoded(i:i))-32) + enddo + +end subroutine deep4 + +subroutine sync4(this,dat,jz,ntol,nfqso,mode4,minwidth,dtx,dfx,snrsync,flip,have_candidate) + +! Synchronizes JT4 data, finding the best-fit DT and DF. + + class(jt4_decoder),intent(inout) :: this + parameter (NFFTMAX=2520) !Max length of FFTs + parameter (NHMAX=NFFTMAX/2) !Max length of power spectra + parameter (NSMAX=525) !Max number of half-symbol steps + integer ntol !Range of DF search + real dat(jz) + real ccfblue(-5:540) !CCF with pseudorandom sequence + logical,intent(out) :: have_candidate + +! Do FFTs of twice symbol length, stepped by half symbols. Note that +! we have already downsampled the data by factor of 2. + dtx=0. + dfx=real(nfqso) + snrsync=0. + have_candidate=.false. + flip=1. + nsym=207 + nfft=2520 + nq=nfft/4 + nsteps=jz/nq - 1 + df=0.5*11025.0/nfft + ftop=nfqso + 7*mode4*df + if(ftop.gt.11025.0/4.0) then + go to 900 + endif + + + do j=1,nsteps !Compute spectrum for each step, get average + k=(j-1)*nq + 1 + call this%ps4(dat(k),nfft,this%s2(1,j)) + enddo + +! Set freq and lag ranges + ia=(nfqso-ntol)/df !Index of lowest tone, bottom of search range + ib=(nfqso+ntol)/df !Index of lowest tone, top of search range + iamin=nint(100.0/df) + if(ia.lt.iamin) ia=iamin + ibmax=nint(2700.0/df) - 6*mode4 + if(ib.gt.ibmax) ib=ibmax + + lag1=-5 + lag2=59 + syncbest=-1.e30 + + do ich=minwidth,7 !Find best width + kz=nch(ich)/2 + iaa=ia+kz + ibb=ib-kz + do i=iaa,ibb !Find best frequency channel for CCF + call xcor4(this%s2,i,nsteps,nsym,lag1,lag2,ich,mode4,ccfblue,ccf0, & + lagpk0,flip) + +! Find rms of the CCF, without main peak + call slope(ccfblue(lag1),lag2-lag1+1,lagpk0-lag1+1.0) + sync=abs(ccfblue(lagpk0)) + +! Find best sync value + if(sync.gt.syncbest*1.03) then + ipk=i + ichpk=ich + syncbest=sync + endif + enddo + enddo + if(syncbest<=0.) go to 900 + + dfx=ipk*df + +! Peak up in time, at best whole-channel frequency + call xcor4(this%s2,ipk,nsteps,nsym,lag1,lag2,ichpk,mode4,ccfblue,ccfmax, & + lagpk,flip) + xlag=lagpk + if(lagpk.gt.lag1 .and. lagpk.lt.lag2) then + call peakup(ccfblue(lagpk-1),ccfmax,ccfblue(lagpk+1),dx2) + xlag=lagpk+dx2 + endif + +! Find rms of the CCF, without the main peak + call slope(ccfblue(lag1),lag2-lag1+1,xlag-lag1+1.0) + sq=0. + nsq=0 + do lag=lag1,lag2 + if(abs(lag-xlag).gt.2.0) then + sq=sq+ccfblue(lag)**2 + nsq=nsq+1 + endif + enddo + rms=sqrt(sq/nsq) + if(rms<=tiny(rms)) return + snrsync=max(0.0,db(abs(ccfblue(lagpk)/rms - 1.0)) - 4.5) + dt=2.0/11025.0 + istart=xlag*nq + dtx=istart*dt + have_candidate=.true. + +900 return +end subroutine sync4 + +subroutine lpf1(this,dd,jz,dat,jz2) + + class(jt4_decoder),intent(inout) :: this + parameter (NFFT1=64*11025,NFFT2=32*11025) + real dd(jz) + real dat(jz/2) + real,pointer :: x(:) + complex,pointer :: cx(:) + x=>this%transform(2)%x + cx=>this%transform(2)%cx + + fac=1.0/float(NFFT1) + x(1:jz)=fac*dd(1:jz) + x(jz+1:NFFT1)=0.0 + call fftwf_execute_dft_r2c(this%transform(2)%forward,x,cx) !Forward FFT, r2c + cx(NFFT2/2:)=0.0 + + + call fftwf_execute_dft_c2r(this%transform(2)%backward,cx,x) !Inverse FFT, c2r + jz2=jz/2 + dat(1:jz2)=x(1:jz2) + +end subroutine lpf1 + +subroutine ps4(this,dat,nfft,s) + + class(jt4_decoder),intent(inout) :: this + parameter (NMAX=2520+2) + parameter (NHMAX=NMAX/2-1) + real dat(nfft) + real,pointer :: dat2(:) + real s(NHMAX) + complex,pointer :: c(:) + dat2=>this%transform(3)%x + c=>this%transform(3)%cx + + nh=nfft/2 + do i=1,nh + dat2(i)=dat(i)/128.0 !### Why 128 ?? + enddo + do i=nh+1,nfft + dat2(i)=0. + enddo + + call fftwf_execute_dft_r2c(this%transform(3)%forward,dat2,c) + + fac=1.0/nfft + do i=1,nh + s(i)=fac*(real(c(i))**2 + aimag(c(i))**2) + enddo + +end subroutine ps4 + + subroutine decode_pcm(this,options,input,sample_rate_hz,decode_callback,status,diagnostic_callback) + class(jt4_decoder),intent(inout) :: this + type(jt4_options),intent(in) :: options + integer(c_int16_t),intent(in) :: input(:) + integer,intent(in) :: sample_rate_hz + procedure(jt4_decode_callback) :: decode_callback + integer,intent(out) :: status + procedure(jt4_diagnostic_callback),optional :: diagnostic_callback + integer :: jz,jz2,depth,n + real,pointer :: x(:) + complex,pointer :: cx(:) + + status=-1 + if(sample_rate_hz/=11025.and.sample_rate_hz/=12000) return + if(size(input)<1.or.size(input)>60*sample_rate_hz) return + if(options%submode<0.or.options%submode>6) return + if(options%minimum_width_profile<0.or.options%minimum_width_profile>options%submode) return + call this%prepare_workspace(status,sample_rate_hz==12000) + if(status/=0) return + this%decode_callback=>decode_callback + nullify(this%diagnostic_callback) + if(present(diagnostic_callback)) this%diagnostic_callback=>diagnostic_callback + if(this%current_submode/=options%submode.or.this%current_minimum_profile/=options%minimum_width_profile) & + call this%clear_averages() + this%current_submode=options%submode + this%current_minimum_profile=options%minimum_width_profile + if(.not.this%prepared) then + this%pcm=0. + jz=min(size(input),52*sample_rate_hz) + if(sample_rate_hz==11025) then + this%pcm(1:jz)=real(input(1:jz)) + else + x=>this%transform(1)%x + cx=>this%transform(1)%cx + x=0. + x(1:jz)=real(input(1:jz)) + call fftwf_execute_dft_r2c(this%transform(1)%forward,x,cx) + cx(320000:)=0. + call fftwf_execute_dft_c2r(this%transform(1)%backward,cx,x) + n=int(jz*11025./12000.) + this%pcm(1:n)=1.e-6*x(1:n) + endif + call this%lpf1(this%pcm,52*11025,this%dat,jz2) + this%prepared=.true. + endif + depth=options%depth + if(options%averaging) depth=ior(depth,16) + if(options%deep_search) depth=ior(depth,32) + this%kernel_status=0 + call this%wsjt4(this%dat,26*11025,options%utc,options%min_sync,options%tolerance_hz, & + nch(options%submode+1),options%minimum_width_profile,options%mycall,options%hiscall, & + options%hisgrid,options%receive_frequency_hz,depth) + status=this%kernel_status + nullify(this%decode_callback,this%diagnostic_callback) + end subroutine + + subroutine prepare_workspace(this,status,include_conversion) + class(jt4_decoder),intent(inout) :: this + integer,intent(out) :: status + logical,intent(in),optional :: include_conversion + integer,parameter :: lengths(4)=[64*12000,64*11025,2520,1260] + integer,parameter :: inverse_lengths(4)=[64*11025,32*11025,0,0] + integer :: i,n,allocation_status,first + status=-1 + if(.not.allocated(this%pcm)) then + allocate(this%pcm(52*11025),stat=allocation_status) + if(allocation_status/=0) return + endif + if(.not.allocated(this%dat)) then + allocate(this%dat(30*11025),stat=allocation_status) + if(allocation_status/=0) return + endif + if(.not.allocated(this%s2)) then + allocate(this%s2(1260,525),stat=allocation_status) + if(allocation_status/=0) return + endif + if(.not.allocated(this%ppsave)) then + allocate(this%ppsave(207,7,MAXAVE),stat=allocation_status) + if(allocation_status/=0) return + this%ppsave=0. + endif + if(.not.allocated(this%rsymbol)) then + allocate(this%rsymbol(207,7),stat=allocation_status) + if(allocation_status/=0) return + this%rsymbol=0. + endif + if(.not.allocated(this%calls)) then + allocate(this%calls(0),stat=allocation_status) + if(allocation_status/=0) return + endif + first=2 + if(present(include_conversion)) then + if(include_conversion) first=1 + endif + do i=first,4 + n=lengths(i) + if(.not.c_associated(this%transform(i)%storage)) then + this%transform(i)%storage=fftwf_alloc_complex(int(n/2+1,c_size_t)) + if(.not.c_associated(this%transform(i)%storage)) return + call c_f_pointer(this%transform(i)%storage,this%transform(i)%x,[n+2]) + call c_f_pointer(this%transform(i)%storage,this%transform(i)%cx,[n/2+1]) + this%transform(i)%cx(0:n/2)=>this%transform(i)%cx + endif + if(.not.c_associated(this%transform(i)%forward)) then +!$omp critical(fftw) + this%transform(i)%forward=fftwf_plan_dft_r2c_1d(n,this%transform(i)%x, & + this%transform(i)%cx,this%fft_flags) +!$omp end critical(fftw) + endif + if(.not.c_associated(this%transform(i)%forward)) return + if(inverse_lengths(i)>0.and..not.c_associated(this%transform(i)%backward)) then +!$omp critical(fftw) + this%transform(i)%backward=fftwf_plan_dft_c2r_1d(inverse_lengths(i),this%transform(i)%cx, & + this%transform(i)%x,this%fft_flags) +!$omp end critical(fftw) + endif + if(inverse_lengths(i)>0.and..not.c_associated(this%transform(i)%backward)) return + enddo + status=0 + end subroutine + + subroutine set_fft_flags(this,flags) + class(jt4_decoder),intent(inout) :: this + integer,intent(in) :: flags + if(flags==this%fft_flags) return + call this%destroy_plans() + this%fft_flags=flags + this%prepared=.false. + end subroutine + + subroutine destroy_plans(this) + class(jt4_decoder),intent(inout) :: this + integer :: i +!$omp critical(fftw) + do i=1,4 + if(c_associated(this%transform(i)%forward)) call fftwf_destroy_plan(this%transform(i)%forward) + if(c_associated(this%transform(i)%backward)) call fftwf_destroy_plan(this%transform(i)%backward) + this%transform(i)%forward=c_null_ptr + this%transform(i)%backward=c_null_ptr + enddo +!$omp end critical(fftw) + end subroutine + + subroutine finalize_jt4(this) + type(jt4_decoder),intent(inout) :: this + integer :: i + call this%destroy_plans() + do i=1,4 + if(c_associated(this%transform(i)%storage)) call fftwf_free(this%transform(i)%storage) + this%transform(i)%storage=c_null_ptr + nullify(this%transform(i)%x,this%transform(i)%cx) + enddo + end subroutine + + subroutine release_input(this) + class(jt4_decoder),intent(inout) :: this + this%prepared=.false. + this%sync_strength=0. + nullify(this%decode_callback,this%diagnostic_callback) + end subroutine + + subroutine reset(this) + class(jt4_decoder),intent(inout) :: this + call this%release_input() + call this%clear_averages() + if(allocated(this%calls)) deallocate(this%calls) + if(allocated(this%code)) deallocate(this%code) + if(allocated(this%testmsg)) deallocate(this%testmsg) + this%templates_valid=.false. + this%ntot=0 + this%current_submode=-1 + this%current_minimum_profile=-1 + end subroutine + + subroutine set_calls(this,calls,status) + class(jt4_decoder),intent(inout) :: this + type(jt4_call),intent(in) :: calls(:) + integer,intent(out),optional :: status + type(jt4_call),allocatable :: replacement(:) + integer :: i,count,allocation_status + if(present(status)) status=0 + count=min(size(calls),6999) + if(allocated(this%calls)) then + if(size(this%calls)==count) then + do i=1,count + if(this%calls(i)%call/=calls(i)%call.or.this%calls(i)%grid/=calls(i)%grid) exit + enddo + if(i>count) return + endif + endif + allocate(replacement(count),stat=allocation_status) + if(allocation_status/=0) then + this%kernel_status=-1 + if(present(status)) status=-1 + return + endif + replacement=calls(1:count) + call move_alloc(replacement,this%calls) + if(allocated(this%code)) deallocate(this%code) + if(allocated(this%testmsg)) deallocate(this%testmsg) + this%templates_valid=.false. + end subroutine + + subroutine clear_averages(this) + class(jt4_decoder),intent(inout) :: this + this%iutc=-1 + this%nfsave=0 + this%dtsave=0. + this%syncsave=0. + this%flipsave=0. + this%average_used=.false. + this%nsave=0 + this%average_count=0 + if(allocated(this%ppsave)) this%ppsave=0. + if(allocated(this%rsymbol)) this%rsymbol=0. + end subroutine + + subroutine save_average(this,utc,sync,dt,polarity,freq,tolerance) + class(jt4_decoder),intent(inout) :: this + integer,intent(in) :: utc,freq,tolerance + real,intent(in) :: sync,dt,polarity + integer :: i,allocation_status + if(.not.allocated(this%ppsave)) then + allocate(this%ppsave(207,7,MAXAVE),stat=allocation_status) + if(allocation_status/=0) then + this%kernel_status=-1 + return + endif + this%ppsave=0. + endif + if(.not.allocated(this%rsymbol)) then + allocate(this%rsymbol(207,7),stat=allocation_status) + if(allocation_status/=0) then + this%kernel_status=-1 + return + endif + this%rsymbol=0. + endif + do i=1,MAXAVE + if(this%iutc(i)/100==utc/100.and.this%iutc(i)>=0.and.abs(this%nfsave(i)-freq)<=tolerance) return + enddo + this%nsave=mod(this%nsave,MAXAVE)+1 + this%average_count=min(this%average_count+1,MAXAVE) + i=this%nsave + this%iutc(i)=utc + this%nfsave(i)=freq + this%syncsave(i)=sync + this%dtsave(i)=dt + this%flipsave(i)=polarity + this%ppsave(:,:,i)=this%rsymbol + this%average_used(i)=.false. + end subroutine + + subroutine select_average(this,utc,dt,polarity,freq,tolerance,sym,nsum) + class(jt4_decoder),intent(inout) :: this + integer,intent(in) :: utc,freq,tolerance + real,intent(in) :: dt,polarity + real,intent(out) :: sym(207,7) + integer,intent(out) :: nsum + integer :: i + sym=0. + nsum=0 + this%average_used=.false. + if(.not.allocated(this%ppsave)) return + do i=1,MAXAVE + if(this%iutc(i)<0) cycle + if(mod(this%iutc(i)/100,2)/=mod(utc/100,2)) cycle + if(abs(dt-this%dtsave(i))>0.2) cycle + if(abs(freq-this%nfsave(i))>tolerance) cycle + if(polarity/=this%flipsave(i)) cycle + sym=sym+this%ppsave(:,:,i) + nsum=nsum+1 + this%average_used(i)=.true. + enddo + if(nsum>0) sym=sym/nsum + end subroutine + + subroutine get_averages(this,entries,count) + class(jt4_decoder),intent(in) :: this + type(jt4_average_entry),intent(out) :: entries(:) + integer,intent(out) :: count + integer :: i + count=0 + do i=1,MAXAVE + if(this%iutc(i)<0) cycle + count=count+1 + if(count>size(entries)) cycle + entries(count)%utc=this%iutc(i) + entries(count)%frequency_hz=this%nfsave(i) + entries(count)%polarity=1 + if(this%flipsave(i)<0.) entries(count)%polarity=-1 + entries(count)%used=this%average_used(i) + entries(count)%sync=this%syncsave(i) + entries(count)%dt_seconds=this%dtsave(i) + enddo + end subroutine +end module jt4_decode diff --git a/lib/jt4_host_support.f90 b/lib/jt4_host_support.f90 new file mode 100644 index 00000000..b84e9629 --- /dev/null +++ b/lib/jt4_host_support.f90 @@ -0,0 +1,112 @@ +module jt4_host_support + use jt4_decode, only: jt4_decoder,jt4_call,jt4_average_entry + use decoder_engine_types, only: engine_jt4_options + use prog_args, only: data_dir + use fftw3, only: FFTW_ESTIMATE,FFTW_ESTIMATE_PATIENT,FFTW_MEASURE,FFTW_PATIENT,FFTW_EXHAUSTIVE + include 'jt9com.f90' + private + public :: convert_jt4_host_options,prepare_jt4_host,load_jt4_calls,write_jt4_averages,write_jt4_diagnostic + +contains + + subroutine convert_jt4_host_options(params,options,utc_is_hhmmss) + type(params_block), intent(in) :: params + type(engine_jt4_options), intent(out) :: options + logical, optional, intent(in) :: utc_is_hhmmss + + options=engine_jt4_options() + options%utc=100*params%nutc + if(present(utc_is_hhmmss)) then + if(utc_is_hhmmss) options%utc=params%nutc + endif + options%submode=params%nsubmode + options%receive_frequency_hz=params%nfqso + options%tolerance_hz=params%ntol + options%depth=iand(params%ndepth,7) + options%min_sync=params%minsync + options%minimum_width_profile=params%minw + options%averaging=merge(1_c_int,0_c_int,iand(params%ndepth,16)/=0) + options%deep_search=merge(1_c_int,0_c_int,iand(params%ndepth,32)/=0) + options%mycall=params%mycall + options%hiscall=params%hiscall + options%hisgrid=params%hisgrid + end subroutine convert_jt4_host_options + + subroutine prepare_jt4_host(decoder) + class(jt4_decoder), intent(inout) :: decoder + integer :: flags,npatience,nthreads + common/patience/npatience,nthreads + + flags=FFTW_ESTIMATE + select case(npatience) + case(1) + flags=FFTW_ESTIMATE_PATIENT + case(2) + flags=FFTW_MEASURE + case(3) + flags=FFTW_PATIENT + case(4) + flags=FFTW_EXHAUSTIVE + end select + call decoder%set_fft_flags(flags) + end subroutine prepare_jt4_host + + subroutine load_jt4_calls(decoder) + class(jt4_decoder), intent(inout) :: decoder + type(jt4_call), allocatable :: calls(:) + character(len=180) :: line + integer :: unit,ios,count,i,first,second + + allocate(calls(6999)) + count=0 + open(newunit=unit,file=trim(data_dir)//'/CALL3.TXT',status='old',action='read',iostat=ios) + if(ios==0) then + do i=1,size(calls) + read(unit,'(a80)',iostat=ios) line + if(ios/=0) exit + if(line(:4)=='ZZZZ') exit + if(line(:2)=='//') cycle + first=index(line,',') + if(first<4) cycle + second=index(line(first+1:),',') + if(second<5) cycle + count=count+1 + calls(count)%call=line(:first-1) + calls(count)%grid=line(first+1:first+4) + enddo + close(unit) + endif + call decoder%set_calls(calls(:count)) + end subroutine load_jt4_calls + + subroutine write_jt4_averages(decoder,unit) + class(jt4_decoder), intent(in) :: decoder + integer, intent(in) :: unit + type(jt4_average_entry) :: entries(64) + integer :: count,i + character :: used,polarity + + call decoder%get_averages(entries,count) + rewind(unit) + do i=1,count + used='.' + if(entries(i)%used) used='$' + polarity='*' + if(entries(i)%polarity<0) polarity='$' + write(unit,'(a1,i5.4,f6.1,f6.2,i6,1x,a1)') used,entries(i)%utc/100, & + entries(i)%sync,entries(i)%dt_seconds,entries(i)%frequency_hz,polarity + enddo + endfile(unit) + flush(unit) + end subroutine write_jt4_averages + + subroutine write_jt4_diagnostic(line) + character(len=*), intent(in) :: line + integer :: unit,ios + + open(newunit=unit,file='fort.77',status='replace',iostat=ios) + if(ios/=0) return + write(unit,'(a)') line + close(unit) + end subroutine write_jt4_diagnostic +end module jt4_host_support diff --git a/lib/jt9.f90 b/lib/jt9.f90 index 1e59bd8e..13e0dfb0 100644 --- a/lib/jt9.f90 +++ b/lib/jt9.f90 @@ -394,6 +394,7 @@ program jt9 stop 2 end if call parse_wav_filename_nutc(infile, nutc, wav_utc_has_seconds) + if(mode.eq.4.and.wav_utc_has_seconds) nutc=nutc/100 if((mode.eq.66.or.mode.eq.240.or.mode.eq.241.or.mode.eq.242).and..not.wav_utc_has_seconds) & nutc=100*nutc nsps=6912 @@ -574,7 +575,7 @@ program jt9 endif ! Normal decoding pass - if(mode.eq.8.or.mode.eq.9.or.mode.eq.65.or.mode.eq.66.or. & + if(mode.eq.4.or.mode.eq.8.or.mode.eq.9.or.mode.eq.65.or.mode.eq.66.or. & mode.eq.240.or.mode.eq.241.or.mode.eq.242) then call run_engine_wav(shared_data%id2,engine_valid_samples) else if(mode.eq.5) then diff --git a/lib/jt9_params_init.f90 b/lib/jt9_params_init.f90 index aba14b66..f95c3a58 100644 --- a/lib/jt9_params_init.f90 +++ b/lib/jt9_params_init.f90 @@ -150,6 +150,11 @@ contains ! FST4 60s period uses a larger sample window (jt9.f90:485 in WAV path). if (mode .eq. 240) params%kin = 720000 + if(mode==4) then + params%kin=624000 + params%minw=0 + params%minsync=0 + endif ! JT9 with explicit -S<HZ>: remap nfa to fsplit (jt9.f90:386, :560). if (mode .eq. 9 .and. args%fsplit .ne. 2700) params%nfa = args%fsplit @@ -185,6 +190,7 @@ contains params%nftx = args%nrxfreq params%minw = 1 + if(params%nmode==4.and.params%nsubmode==0) params%minw=0 params%minsync = 0 params%nlist = 0 params%max_drift = 0 diff --git a/lib/jt9a.f90 b/lib/jt9a.f90 index c1a5a20a..85a02a8e 100644 --- a/lib/jt9a.f90 +++ b/lib/jt9a.f90 @@ -171,11 +171,12 @@ subroutine jt9a() ! MSK144 call decode_msk144_core(shared_memory%payload%id2, local_params, data_dir, & completion) - elseif(local_params%nmode.eq.8.or.local_params%nmode.eq.5.or.local_params%nmode.eq.9.or. & + elseif(local_params%nmode.eq.4.or.local_params%nmode.eq.8.or.local_params%nmode.eq.5.or. & + local_params%nmode.eq.9.or. & local_params%nmode.eq.65.or.local_params%nmode.eq.66.or. & local_params%nmode.eq.240.or.local_params%nmode.eq.241.or.local_params%nmode.eq.242) then pass_valid_samples=min(shared_memory%metadata%valid_samples,180000) - if(local_params%nmode.eq.9.or.local_params%nmode.eq.65) & + if(local_params%nmode.eq.4.or.local_params%nmode.eq.9.or.local_params%nmode.eq.65) & pass_valid_samples=min(shared_memory%metadata%valid_samples,720000) if(local_params%nmode.eq.66.or.local_params%nmode.eq.240.or. & local_params%nmode.eq.241.or.local_params%nmode.eq.242) & diff --git a/lib/lpf1.f90 b/lib/lpf1.f90 deleted file mode 100644 index a620e9a5..00000000 --- a/lib/lpf1.f90 +++ /dev/null @@ -1,29 +0,0 @@ -subroutine lpf1(dd,jz,dat,jz2) - - parameter (NFFT1=64*11025,NFFT2=32*11025) - real dd(jz) - real dat(jz) - real x(NFFT1) - complex cx(0:NFFT1/2) - equivalence (x,cx) - save x,cx - - fac=1.0/float(NFFT1) - x(1:jz)=fac*dd(1:jz) - x(jz+1:NFFT1)=0.0 - call four2a(cx,NFFT1,1,-1,0) !Forwarxd FFT, r2c - cx(NFFT2/2:)=0.0 - -! df=11025.0/NFFT1 -! do i=1,NFFT1/2 -! sx=real(cx(i))**2 + aimag(cx(i))**2 -! write(50,3000) i*df,sx -!3000 format(f15.6,e12.3) -! enddo - - call four2a(cx,NFFT2,1,1,-1) !Inverse FFT, c2r - jz2=jz/2 - dat(1:jz2)=x(1:jz2) - - return -end subroutine lpf1 diff --git a/lib/ps4.f90 b/lib/ps4.f90 deleted file mode 100644 index 782ab50d..00000000 --- a/lib/ps4.f90 +++ /dev/null @@ -1,27 +0,0 @@ -subroutine ps4(dat,nfft,s) - - parameter (NMAX=2520+2) - parameter (NHMAX=NMAX/2-1) - real dat(nfft) - real dat2(NMAX) - real s(NHMAX) - complex c(0:NMAX) - equivalence(dat2,c) - - nh=nfft/2 - do i=1,nh - dat2(i)=dat(i)/128.0 !### Why 128 ?? - enddo - do i=nh+1,nfft - dat2(i)=0. - enddo - - call four2a(c,nfft,1,-1,0) - - fac=1.0/nfft - do i=1,nh - s(i)=fac*(real(c(i))**2 + aimag(c(i))**2) - enddo - - return -end subroutine ps4 diff --git a/lib/streaming_apply.f90 b/lib/streaming_apply.f90 index 0a507481..98573ba4 100644 --- a/lib/streaming_apply.f90 +++ b/lib/streaming_apply.f90 @@ -68,6 +68,9 @@ contains end if if (in_cfg%depth_set) params%ndepth = in_cfg%depth if (in_cfg%submode_set) params%nsubmode = in_cfg%submode + if ((in_cfg%mode_set.or.in_cfg%submode_set).and..not.in_cfg%min_width_set) then + if(params%nmode==4.and.params%nsubmode==0) params%minw=0 + endif if (in_cfg%rxfreq_set) then params%nfqso = in_cfg%rxfreq params%nftx = in_cfg%rxfreq @@ -287,8 +290,8 @@ contains if ((io_mode.eq.240.or.io_mode.eq.241.or.io_mode.eq.242).and.io_TRperiod.ge.60.and. & params%nutc.le.2359) & params%nutc=100*params%nutc - ! Legacy numeric Q65 times may be HHMM. - if (io_mode.eq.66.and.io_TRperiod.ge.60.and.params%nutc.le.2359) & + ! Legacy numeric JT4 and Q65 times may be HHMM. + if ((io_mode.eq.4.or.io_mode.eq.66).and.io_TRperiod.ge.60.and.params%nutc.le.2359) & params%nutc=100*params%nutc endif ! Phase 8 (RFC v1 §6.3/§4.1): utc (ISO "HH:MM:SS") is a SECOND route to nutc, diff --git a/lib/streaming_control.f90 b/lib/streaming_control.f90 index a65915bd..2b02dbac 100644 --- a/lib/streaming_control.f90 +++ b/lib/streaming_control.f90 @@ -61,7 +61,8 @@ module streaming_control integer :: nfa = 0 logical :: nfb_set = .false. integer :: nfb = 0 - ! Per-period UTC as HHMMSS integer. Threaded into + ! Per-period UTC as HHMMSS integer; in periods of a minute or more a + ! value up to 2359 is read as HHMM. Threaded into ! params%nutc so streaming_emit_decode can format the time field ! correctly. Producer should send a fresh nutc in each period's ! configure frame (mirrors WAV-path filename-derived per-invocation diff --git a/lib/streaming_emit.f90 b/lib/streaming_emit.f90 index b3346926..0b6e4590 100644 --- a/lib/streaming_emit.f90 +++ b/lib/streaming_emit.f90 @@ -26,8 +26,11 @@ module streaming_emit private logical, save :: enabled_ = .false. + logical, save :: utc_from_iso_ = .false. + real(8), save :: trperiod_ = 60.d0 public :: streaming_emit_set_enabled + public :: streaming_emit_set_time_form public :: streaming_emit_enabled public :: streaming_emit_ready public :: streaming_emit_decode @@ -44,6 +47,20 @@ contains enabled_ = flag end subroutine streaming_emit_set_enabled + ! Called after each applied configure. An ISO utc is HHMMSS; a numeric nutc + ! is HHMMSS when the period is under a minute or the value exceeds 2359, + ! and HHMM otherwise. + subroutine streaming_emit_set_time_form(utc_set, nutc_set, trperiod) + logical, intent(in) :: utc_set, nutc_set + real(8), intent(in) :: trperiod + if (utc_set) then + utc_from_iso_ = .true. + else if (nutc_set) then + utc_from_iso_ = .false. + end if + trperiod_ = trperiod + end subroutine streaming_emit_set_time_form + pure function streaming_emit_enabled() result(v) logical :: v v = enabled_ @@ -193,15 +210,17 @@ contains logical, optional, intent(in) :: utc_is_hhmmss integer :: hh, mm, ss logical :: full_time - full_time=.false. - if(present(utc_is_hhmmss)) full_time=utc_is_hhmmss + full_time=utc_from_iso_.or.trperiod_.lt.60.d0.or.nutc.gt.2359 + if(present(utc_is_hhmmss)) full_time=full_time.or.utc_is_hhmmss str = '000000' - if (full_time.or.nutc.ge.100000) then + if (nutc .lt. 0) then + return + else if (full_time) then hh = nutc / 10000 mm = mod(nutc/100, 100) ss = mod(nutc, 100) write(str, '(i2.2,i2.2,i2.2)') hh, mm, ss - else if (nutc .ge. 0) then + else hh = nutc / 100 mm = mod(nutc, 100) write(str, '(i2.2,i2.2,a2)') hh, mm, '00' diff --git a/lib/streaming_io.f90 b/lib/streaming_io.f90 index a2c9fa28..ed1ac923 100644 --- a/lib/streaming_io.f90 +++ b/lib/streaming_io.f90 @@ -52,7 +52,7 @@ subroutine jt9_stream(shared_data, mode, TRperiod) use streaming_emit, only: streaming_emit_ready, & streaming_emit_decode_finished, streaming_emit_error, & streaming_emit_error_code, streaming_emit_error_version, & - streaming_emit_error_type + streaming_emit_error_type, streaming_emit_set_time_form use streaming_control, only: parse_control_frame, configure_fields, & control_type_error, CTRL_CONFIGURE, CTRL_HALT, CTRL_PARSE_ERR use streaming_apply, only: apply_configure_fields @@ -327,6 +327,7 @@ subroutine jt9_stream(shared_data, mode, TRperiod) prev_TRperiod = TRperiod call apply_configure_fields(cfg, mode, TRperiod, & shared_data%params, baseline_nfa, baseline_nfb) + call streaming_emit_set_time_form(cfg%utc_set, cfg%nutc_set, TRperiod) ! A configure that changes the applied mode or period starts a ! new period; what was in flight is discarded. Repeating the ! current configuration leaves the period alone. @@ -381,7 +382,7 @@ contains shared_data%params%nzhsym = 181 end if shared_data%params%kin = 64800 - if (mode .eq. 5 .or. mode .eq. 9 .or. mode .eq. 65 .or. mode .eq. 66 .or. & + if (mode .eq. 4 .or. mode .eq. 5 .or. mode .eq. 9 .or. mode .eq. 65 .or. mode .eq. 66 .or. & mode .eq. 240 .or. mode .eq. 241 .or. mode .eq. 242) & shared_data%params%kin = period%k diff --git a/lib/sync4.f90 b/lib/sync4.f90 deleted file mode 100644 index 9f7ceefc..00000000 --- a/lib/sync4.f90 +++ /dev/null @@ -1,146 +0,0 @@ -subroutine sync4(dat,jz,ntol,nfqso,mode,mode4,minwidth,dtx,dfx,snrx, & - snrsync,flip,width) - -! Synchronizes JT4 data, finding the best-fit DT and DF. - - parameter (NFFTMAX=2520) !Max length of FFTs - parameter (NHMAX=NFFTMAX/2) !Max length of power spectra - parameter (NSMAX=525) !Max number of half-symbol steps - integer ntol !Range of DF search - real dat(jz) - real s2(NHMAX,NSMAX) !2d spectrum, stepped by half-symbols - real ccfblue(-5:540) !CCF with pseudorandom sequence - real ccfred(NHMAX) !Peak of ccfblue, as function of freq - real red(NHMAX) !Peak of ccfblue, as function of freq - integer ipk1(1) - integer nch(7) - logical savered - equivalence (ipk1,ipk1a) - data nch/1,2,4,9,18,36,72/ - save - -! Do FFTs of twice symbol length, stepped by half symbols. Note that -! we have already downsampled the data by factor of 2. - nsym=207 - nfft=2520 - nh=nfft/2 - nq=nfft/4 - nsteps=jz/nq - 1 - df=0.5*11025.0/nfft - ftop=nfqso + 7*mode4*df - if(ftop.gt.11025.0/4.0) then - print*,'*** Rx Freq is set too high for this submode ***' - go to 900 - endif - - if(mode.eq.-999) width=0. !Silence compiler warning - - do j=1,nsteps !Compute spectrum for each step, get average - k=(j-1)*nq + 1 - call ps4(dat(k),nfft,s2(1,j)) - enddo - -! Set freq and lag ranges - ia=(nfqso-ntol)/df !Index of lowest tone, bottom of search range - ib=(nfqso+ntol)/df !Index of lowest tone, top of search range - iamin=nint(100.0/df) - if(ia.lt.iamin) ia=iamin - ibmax=nint(2700.0/df) - 6*mode4 - if(ib.gt.ibmax) ib=ibmax - - lag1=-5 - lag2=59 - syncbest=-1.e30 - snrx=-26.0 - ccfred=0. - red=0. - i0=nint(nfqso/df) - - do ich=minwidth,7 !Find best width - kz=nch(ich)/2 - savered=.false. - iaa=ia+kz - ibb=ib-kz - do i=iaa,ibb !Find best frequency channel for CCF - call xcor4(s2,i,nsteps,nsym,lag1,lag2,ich,mode4,ccfblue,ccf0, & - lagpk0,flip) - ccfred(i)=ccf0 - -! Find rms of the CCF, without main peak - call slope(ccfblue(lag1),lag2-lag1+1,lagpk0-lag1+1.0) - sync=abs(ccfblue(lagpk0)) -! write(*,3000) ich,i,i*df,ccf0,sync,syncbest -!3000 format(2i5,4f12.3) - -! Find best sync value - if(sync.gt.syncbest*1.03) then - ipk=i - lagpk=lagpk0 - ichpk=ich - syncbest=sync - savered=.true. - endif - enddo - if(savered) red=ccfred - enddo - if(syncbest.lt.-1.e29) go to 900 - ccfred=red - call pctile(ccfred(iaa:ibb),ibb-iaa+1,45,base) - ccfred=ccfred-base - - dfx=ipk*df - -! Peak up in time, at best whole-channel frequency - call xcor4(s2,ipk,nsteps,nsym,lag1,lag2,ichpk,mode4,ccfblue,ccfmax, & - lagpk,flip) - xlag=lagpk - if(lagpk.gt.lag1 .and. lagpk.lt.lag2) then - call peakup(ccfblue(lagpk-1),ccfmax,ccfblue(lagpk+1),dx2) - xlag=lagpk+dx2 - endif - -! Find rms of the CCF, without the main peak - call slope(ccfblue(lag1),lag2-lag1+1,xlag-lag1+1.0) - sq=0. - nsq=0 - do lag=lag1,lag2 - if(abs(lag-xlag).gt.2.0) then - sq=sq+ccfblue(lag)**2 - nsq=nsq+1 - endif - enddo - rms=sqrt(sq/nsq) - snrsync=max(0.0,db(abs(ccfblue(lagpk)/rms - 1.0)) - 4.5) - dt=2.0/11025.0 - istart=xlag*nq - dtx=istart*dt - - ipk1=maxloc(ccfred) - ccf10=0.5*maxval(ccfred) - do i=ipk1a,ia,-1 - if(ccfred(i).le.ccf10) exit - enddo - i1=i - do i=ipk1a,ib - if(ccfred(i).le.ccf10) exit - enddo - nw=i-i1 - width=nw*df - - sq=0. - ns=0 - iaa=max(ipk1a-10*nw,ia) - ibb=min(ipk1a+10*nw,ib) - jmax=2*mode4/3 - do i=iaa,ibb - j=abs(i-ipk1a) - if(j.gt.nw .and. j.lt.jmax) then - sq=sq + ccfred(j)*ccfred(j) - ns=ns+1 - endif - enddo - rms=sqrt(sq/ns) - snrx=10.0*log10(ccfred(ipk1a)/rms) - 41.2 - -900 return -end subroutine sync4 diff --git a/lib/xcor4.f90 b/lib/xcor4.f90 index fa9c2012..2dc2deb9 100644 --- a/lib/xcor4.f90 +++ b/lib/xcor4.f90 @@ -1,40 +1,22 @@ subroutine xcor4(s2,ipk,nsteps,nsym,lag1,lag2,ich,mode4,ccf,ccf0, & lagpk,flip) -! Computes ccf of the 4_FSK spectral array s2 and the pseudo-random -! array pr2. Returns peak of CCF and the lag at which peak occurs. +! Computes ccf of the 4_FSK spectral array s2 and the pseudo-random +! array pr2. Returns peak of CCF and the lag at which peak occurs. ! The CCF peak may be either positive or negative, with negative ! implying the "OOO" message. + use jt4, only: nch,npr parameter (NHMAX=1260) !Max length of power spectra parameter (NSMAX=525) !Max number of half-symbol steps real s2(NHMAX,NSMAX) !2d spectrum, stepped by half-symbols real a(NSMAX) real ccf(-5:540) - integer nch(7) - integer npr2(207) real pr2(207) - logical first - data lagmin/0/ !Silence compiler warning - data first/.true./ - data npr2/ & - 0,0,0,0,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,0,0,0,0,0,0,1,1,0,0, & - 0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,0,0, & - 1,0,0,1,0,0,1,1,1,1,1,0,0,0,1,0,1,0,0,0,1,1,1,1,0,1,1,0,0,1, & - 0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1,0,1,0,1, & - 0,1,1,1,0,0,1,0,1,1,0,1,1,1,1,0,0,0,0,1,1,0,1,1,0,0,0,1,1,1, & - 0,1,1,1,0,1,1,1,0,0,1,0,0,0,1,1,0,1,1,0,0,1,0,0,0,1,1,1,1,1, & - 1,0,0,1,1,0,0,0,0,1,1,0,0,0,1,0,1,1,0,1,1,1,1,0,1,0,1/ - data nch/1,2,4,9,18,36,72/ - save - - if(first) then - do i=1,207 - pr2(i)=2*npr2(i)-1 - enddo - first=.false. - endif + pr2=2*npr-1 + lagpk=lag1 + lagmin=lag1 ccfmax=0. ccfmin=0. nw=nch(min(max(ich,1),7)) |
