2016-06-08 22:02:50 -04:00
|
|
|
subroutine syncmsk144(cdat,npts,msgreceived,fest,nutc,t0) !nutc and t0 are for debug output
|
2016-05-26 17:19:09 -04:00
|
|
|
use iso_c_binding, only: c_loc,c_size_t
|
|
|
|
use packjt
|
|
|
|
use hashing
|
|
|
|
use timer_module, only: timer
|
|
|
|
|
2016-06-04 21:19:54 -04:00
|
|
|
parameter (NSPM=864)
|
2016-05-28 13:58:06 -04:00
|
|
|
character*22 msgreceived
|
2016-05-26 17:19:09 -04:00
|
|
|
character*85 pchk_file,gen_file
|
|
|
|
complex cdat(npts) !Analytic signal
|
2016-06-05 21:53:04 -04:00
|
|
|
complex cdat2(npts)
|
2016-05-28 13:58:06 -04:00
|
|
|
complex c(NSPM)
|
|
|
|
complex ctmp(6000)
|
|
|
|
complex cb(42) !Complex waveform for sync word
|
|
|
|
complex csum,cfac,cca,ccb
|
|
|
|
complex cc(npts)
|
|
|
|
complex cc1(npts)
|
|
|
|
complex cc2(npts)
|
2016-06-04 21:19:54 -04:00
|
|
|
complex bb(6)
|
2016-05-28 13:58:06 -04:00
|
|
|
integer s8(8),hardbits(144),hardword(128),unscrambledhardbits(128)
|
|
|
|
integer*1, target:: i1Dec8BitBytes(10)
|
|
|
|
integer, dimension(1) :: iloc
|
2016-05-28 16:53:35 -04:00
|
|
|
integer*4 i4Msg6BitWords(12) !72-bit message as 6-bit words
|
2016-05-28 13:58:06 -04:00
|
|
|
integer*4 i4Dec6BitWords(12)
|
|
|
|
integer*1 decoded(80)
|
|
|
|
integer*1, allocatable :: message(:)
|
2016-05-28 16:53:35 -04:00
|
|
|
integer*1 i1hashdec
|
2016-06-04 21:19:54 -04:00
|
|
|
integer ipeaks(10)
|
2016-05-28 13:58:06 -04:00
|
|
|
logical ismask(6000)
|
|
|
|
real cbi(42),cbq(42)
|
|
|
|
real tonespec(6000)
|
|
|
|
real rcw(12)
|
|
|
|
real dd(npts)
|
2016-05-26 17:19:09 -04:00
|
|
|
real pp(12) !Half-sine pulse shape
|
2016-06-05 12:02:26 -04:00
|
|
|
real*8 dt, df, fs, pi, twopi
|
2016-05-28 13:58:06 -04:00
|
|
|
real softbits(144)
|
|
|
|
real*8 unscrambledsoftbits(128)
|
|
|
|
real lratio(128)
|
2016-05-26 17:19:09 -04:00
|
|
|
logical first
|
|
|
|
data first/.true./
|
|
|
|
|
2016-05-28 13:58:06 -04:00
|
|
|
data s8/0,1,1,1,0,0,1,0/
|
2016-06-05 12:02:26 -04:00
|
|
|
save first,cb,pi,twopi,dt,f0,f1
|
2016-06-06 21:30:00 -04:00
|
|
|
|
2016-05-26 17:19:09 -04:00
|
|
|
if(first) then
|
2016-06-01 19:46:36 -04:00
|
|
|
! These files can be found in /lib/ldpc/jtmode_codes directory
|
2016-06-07 10:54:53 -04:00
|
|
|
pchk_file="./data/peg-128-80-reg3.pchk"
|
|
|
|
gen_file="./data/peg-128-80-reg3.gen"
|
2016-05-26 17:19:09 -04:00
|
|
|
call init_ldpc(trim(pchk_file)//char(0),trim(gen_file)//char(0))
|
2016-05-28 13:58:06 -04:00
|
|
|
! define half-sine pulse and raised-cosine edge window
|
|
|
|
pi=4d0*datan(1d0)
|
2016-05-28 16:53:35 -04:00
|
|
|
twopi=8d0*datan(1d0)
|
2016-05-28 13:58:06 -04:00
|
|
|
dt=1.0/12000.0
|
2016-05-26 17:19:09 -04:00
|
|
|
do i=1,12
|
|
|
|
angle=(i-1)*pi/12.0
|
|
|
|
pp(i)=sin(angle)
|
2016-05-28 13:58:06 -04:00
|
|
|
rcw(i)=(1-cos(angle))/2
|
2016-05-26 17:19:09 -04:00
|
|
|
enddo
|
|
|
|
|
2016-05-28 13:58:06 -04:00
|
|
|
! define the sync word waveform
|
|
|
|
s8=2*s8-1
|
|
|
|
cbq(1:6)=pp(7:12)*s8(1)
|
|
|
|
cbq(7:18)=pp*s8(3)
|
|
|
|
cbq(19:30)=pp*s8(5)
|
|
|
|
cbq(31:42)=pp*s8(7)
|
|
|
|
cbi(1:12)=pp*s8(2)
|
|
|
|
cbi(13:24)=pp*s8(4)
|
|
|
|
cbi(25:36)=pp*s8(6)
|
|
|
|
cbi(37:42)=pp(1:6)*s8(8)
|
|
|
|
cb=cmplx(cbi,cbq)
|
2016-05-31 21:58:46 -04:00
|
|
|
|
2016-05-26 17:19:09 -04:00
|
|
|
first=.false.
|
|
|
|
endif
|
|
|
|
|
2016-05-28 13:58:06 -04:00
|
|
|
! Coarse carrier frequency sync
|
2016-06-01 23:19:08 -04:00
|
|
|
! look for tones near 2k and 4k in the (analytic signal)**2 spectrum
|
2016-06-05 13:21:19 -04:00
|
|
|
! search range for coarse frequency error is +/- 100 Hz
|
2016-05-28 13:58:06 -04:00
|
|
|
fs=12000.0
|
|
|
|
nfft=6000 !using a zero-padded fft to get 2 Hz bins
|
|
|
|
df=fs/nfft
|
|
|
|
|
|
|
|
ctmp=cmplx(0.0,0.0)
|
|
|
|
ctmp(1:npts)=cdat**2
|
|
|
|
ctmp(1:12)=ctmp(1:12)*rcw
|
|
|
|
ctmp(npts-11:npts)=ctmp(npts-11:npts)*rcw(12:1:-1)
|
|
|
|
call four2a(ctmp,nfft,1,-1,1)
|
|
|
|
tonespec=abs(ctmp)**2
|
2016-06-01 19:46:36 -04:00
|
|
|
|
2016-05-28 13:58:06 -04:00
|
|
|
ismask=.false.
|
|
|
|
ismask(1901:2101)=.true. ! high tone search window
|
|
|
|
iloc=maxloc(tonespec,ismask)
|
|
|
|
ihpk=iloc(1)
|
|
|
|
ah=tonespec(ihpk)
|
|
|
|
ismask=.false.
|
|
|
|
ismask(901:1101)=.true. ! window for low tone
|
|
|
|
iloc=maxloc(tonespec,ismask)
|
|
|
|
ilpk=iloc(1)
|
|
|
|
al=tonespec(ilpk)
|
2016-06-01 23:19:08 -04:00
|
|
|
fdiff=(ihpk-ilpk)*df
|
2016-06-05 13:21:19 -04:00
|
|
|
ferrh=(ihpk-2001)*df/2.0
|
|
|
|
ferrl=(ilpk-1001)*df/2.0
|
2016-06-06 21:30:00 -04:00
|
|
|
if( abs(fdiff-2000) .le. 16.0 ) then
|
2016-06-05 13:21:19 -04:00
|
|
|
if( ah .ge. al ) then
|
|
|
|
ferr=ferrh
|
|
|
|
else
|
|
|
|
ferr=ferrl
|
|
|
|
endif
|
2016-06-06 21:30:00 -04:00
|
|
|
else
|
|
|
|
msgreceived=' '
|
2016-06-08 22:02:50 -04:00
|
|
|
phase0=-97 !-97 is failed carrier sync, -98 failure to decode, -99 decoded, bad hash
|
|
|
|
niterations=0
|
|
|
|
ndither=0
|
|
|
|
i1hashdec=0
|
2016-06-06 21:30:00 -04:00
|
|
|
goto 999
|
|
|
|
endif
|
|
|
|
|
2016-05-28 13:58:06 -04:00
|
|
|
! remove coarse freq error - should now be within a few Hz
|
|
|
|
call tweak1(cdat,npts,-(1500+ferr),cdat)
|
|
|
|
|
2016-06-01 23:19:08 -04:00
|
|
|
! attempt frame synchronization
|
|
|
|
! correlate with sync word waveforms - the resulting complex
|
|
|
|
! correlations provide all synch information.
|
2016-05-28 13:58:06 -04:00
|
|
|
cc=0
|
|
|
|
cc1=0
|
|
|
|
cc2=0
|
2016-06-04 21:19:54 -04:00
|
|
|
do i=1,npts-(56*6+41)
|
2016-05-28 13:58:06 -04:00
|
|
|
cc1(i)=sum(cdat(i:i+41)*conjg(cb))
|
|
|
|
cc2(i)=sum(cdat(i+56*6:i+56*6+41)*conjg(cb))
|
2016-05-26 17:19:09 -04:00
|
|
|
enddo
|
2016-05-28 13:58:06 -04:00
|
|
|
cc=cc1+cc2
|
|
|
|
dd=abs(cc1)*abs(cc2)
|
2016-06-04 21:19:54 -04:00
|
|
|
|
|
|
|
! Find 5 largest peaks
|
|
|
|
do ipk=1,5
|
|
|
|
iloc=maxloc(abs(cc))
|
|
|
|
ic1=iloc(1)
|
|
|
|
iloc=maxloc(dd)
|
|
|
|
ic2=iloc(1)
|
|
|
|
ipeaks(ipk)=ic2
|
|
|
|
dd(max(1,ic2-7):min(npts-56*6-41,ic2+7))=0.0
|
|
|
|
enddo
|
|
|
|
|
|
|
|
! See if we can find "closed brackets" - a pair of peaks that differ by 864, plus or minus
|
|
|
|
! This information is not yet used for anything
|
|
|
|
do ii=1,5
|
|
|
|
do jj=ii+1,5
|
|
|
|
if( (ii .ne. jj) .and. (abs( abs(ipeaks(ii)-ipeaks(jj))-864) .le. 5) ) then
|
2016-06-06 21:30:00 -04:00
|
|
|
! write(*,*) "closed brackets: ",ii,jj,ipeaks(ii),ipeaks(jj),abs(ipeaks(ii)-ipeaks(jj))
|
2016-06-04 21:19:54 -04:00
|
|
|
endif
|
|
|
|
enddo
|
|
|
|
enddo
|
|
|
|
|
2016-06-06 21:30:00 -04:00
|
|
|
do iav=1,6
|
2016-06-04 21:19:54 -04:00
|
|
|
do ipk=1,5
|
2016-06-05 12:02:26 -04:00
|
|
|
do id=1,3
|
|
|
|
if( id .eq. 1 ) is=0
|
|
|
|
if( id .eq. 2 ) is=-1
|
|
|
|
if( id .eq. 3 ) is=1
|
2016-06-05 21:53:04 -04:00
|
|
|
|
2016-06-04 21:19:54 -04:00
|
|
|
! we want ic to be the index of the first sample of the message
|
2016-06-05 21:53:04 -04:00
|
|
|
ic=ipeaks(ipk)
|
2016-06-04 21:19:54 -04:00
|
|
|
|
2016-06-05 22:48:06 -04:00
|
|
|
if( ic .le. 864 ) ic=ic+864
|
|
|
|
if( ic .gt. 2*864 ) ic=ic-864
|
2016-06-05 21:53:04 -04:00
|
|
|
! now do fine adjustment of sync index
|
2016-06-04 21:19:54 -04:00
|
|
|
! bb is used to place the sampling index at the center of the eye
|
|
|
|
do i=1,6
|
|
|
|
io=i-3
|
2016-06-05 22:48:06 -04:00
|
|
|
ill=max(1,ic+io)
|
|
|
|
iul=min(ic+io+6+864,npts)
|
|
|
|
bb(i) = sum( ( cdat(ic+io:iul-6:6) * conjg( cdat(ic+io+6:iul:6) ) )*2 )
|
2016-06-04 21:19:54 -04:00
|
|
|
enddo
|
|
|
|
iloc=maxloc(abs(bb))
|
|
|
|
ibb=iloc(1)
|
2016-06-06 21:30:00 -04:00
|
|
|
|
2016-06-04 21:19:54 -04:00
|
|
|
! Adjust frame index to place peak of bb at desired lag
|
2016-06-05 12:02:26 -04:00
|
|
|
ic=ic + ibb-2+is
|
2016-06-05 19:11:51 -04:00
|
|
|
if( ic .le. 864 ) ic=ic+864
|
|
|
|
if( ic .gt. 2*864 ) ic=ic-864
|
2016-06-04 21:19:54 -04:00
|
|
|
|
|
|
|
! Estimate fine frequency error.
|
2016-06-05 21:53:04 -04:00
|
|
|
! Should a larger separation be used when frames are averaged?
|
|
|
|
cca=sum(cdat(ic:ic+41)*conjg(cb))
|
|
|
|
ccb=sum(cdat(ic+56*6:ic+56*6+41)*conjg(cb))
|
2016-05-28 13:58:06 -04:00
|
|
|
cfac=ccb*conjg(cca)
|
|
|
|
ferr2=atan2(imag(cfac),real(cfac))/(twopi*56*6*dt)
|
2016-05-26 17:19:09 -04:00
|
|
|
|
2016-06-01 23:19:08 -04:00
|
|
|
! Final estimate of the carrier frequency - returned to the calling program
|
2016-05-28 16:53:35 -04:00
|
|
|
fest=1500+ferr+ferr2
|
2016-06-05 12:02:26 -04:00
|
|
|
! Remove fine frequency error
|
2016-06-05 21:53:04 -04:00
|
|
|
call tweak1(cdat,npts,-ferr2,cdat2)
|
|
|
|
|
|
|
|
! place the beginning of the central frame at NSPM+1
|
|
|
|
cdat2=cshift(cdat2,ic-865)
|
|
|
|
|
2016-06-06 21:30:00 -04:00
|
|
|
! Try each of the three frames individually, and then
|
|
|
|
! do frame averaging on passes 4 and 5
|
2016-06-05 21:53:04 -04:00
|
|
|
if( iav .eq. 1 ) then
|
|
|
|
c=cdat2(NSPM+1:2*NSPM)
|
|
|
|
elseif( iav .eq. 2 ) then
|
2016-06-06 21:30:00 -04:00
|
|
|
c=cdat2(2*NSPM+1:npts)
|
2016-06-05 21:53:04 -04:00
|
|
|
elseif( iav .eq. 3 ) then
|
2016-06-06 21:30:00 -04:00
|
|
|
c=cdat2(1:NSPM)
|
|
|
|
elseif( iav .eq. 4 ) then
|
|
|
|
c=cdat2(1:NSPM)+cdat2(NSPM+1:2*NSPM)
|
|
|
|
elseif( iav .eq. 5 ) then
|
|
|
|
c=cdat2(NSPM+1:2*NSPM)+cdat2(2*NSPM+1:npts)
|
|
|
|
elseif( iav .eq. 6 ) then
|
2016-06-05 21:53:04 -04:00
|
|
|
c=cdat2(1:NSPM)+cdat2(NSPM+1:2*NSPM)+cdat2(2*NSPM+1:npts)
|
|
|
|
endif
|
2016-05-28 13:58:06 -04:00
|
|
|
|
2016-06-04 21:19:54 -04:00
|
|
|
! Estimate final frequency error and carrier phase.
|
|
|
|
cca=sum(c(1:1+41)*conjg(cb))
|
|
|
|
ccb=sum(c(1+56*6:1+56*6+41)*conjg(cb))
|
2016-05-28 16:53:35 -04:00
|
|
|
cfac=ccb*conjg(cca)
|
|
|
|
ffin=atan2(imag(cfac),real(cfac))/(twopi*56*6*dt)
|
2016-05-28 13:58:06 -04:00
|
|
|
phase0=atan2(imag(cca+ccb),real(cca+ccb))
|
2016-05-28 16:53:35 -04:00
|
|
|
|
2016-06-01 23:19:08 -04:00
|
|
|
! Remove the static phase error from the data
|
2016-05-28 13:58:06 -04:00
|
|
|
cfac=cmplx(cos(phase0),sin(phase0))
|
2016-06-04 21:19:54 -04:00
|
|
|
c=c*conjg(cfac)
|
|
|
|
|
|
|
|
! sample to get softsamples
|
2016-05-28 13:58:06 -04:00
|
|
|
do i=1,72
|
|
|
|
softbits(2*i-1)=imag(c(1+(i-1)*12))
|
|
|
|
softbits(2*i)=real(c(7+(i-1)*12))
|
|
|
|
enddo
|
|
|
|
hardbits=0
|
|
|
|
do i=1,144
|
|
|
|
if( softbits(i) .ge. 0.0 ) then
|
|
|
|
hardbits(i)=1
|
|
|
|
endif
|
|
|
|
enddo
|
2016-06-01 19:46:36 -04:00
|
|
|
|
|
|
|
! calculated the number of sync-word bits that are incorrect
|
2016-06-08 22:02:50 -04:00
|
|
|
! don't present to the decoder if there are too many sync-word bit-errors
|
|
|
|
nbadsync1=(8-sum(s8*(2*hardbits(1:8)-1)))/2
|
|
|
|
nbadsync2=(8-sum(s8*(2*hardbits(57:57+7)-1)))/2
|
|
|
|
nbadsync=nbadsync1+nbadsync2
|
2016-06-05 21:53:04 -04:00
|
|
|
if( nbadsync .gt. 6 ) cycle
|
|
|
|
|
2016-06-04 21:19:54 -04:00
|
|
|
! this could be used to count the number of hard errors that were corrected
|
2016-05-28 13:58:06 -04:00
|
|
|
hardword(1:48)=hardbits(9:9+47)
|
|
|
|
hardword(49:128)=hardbits(65:65+80-1)
|
|
|
|
unscrambledhardbits(1:127:2)=hardword(1:64)
|
|
|
|
unscrambledhardbits(2:128:2)=hardword(65:128)
|
2016-06-04 21:19:54 -04:00
|
|
|
|
|
|
|
! normalize the softsymbols before submitting to decoder
|
2016-05-28 13:58:06 -04:00
|
|
|
sav=sum(softbits)/144
|
|
|
|
s2av=sum(softbits*softbits)/144
|
|
|
|
ssig=sqrt(s2av-sav*sav)
|
|
|
|
softbits=softbits/ssig
|
2016-05-26 17:19:09 -04:00
|
|
|
|
2016-06-08 22:02:50 -04:00
|
|
|
! sigma=0.65
|
|
|
|
sigma=0.70
|
2016-05-28 13:58:06 -04:00
|
|
|
lratio(1:48)=softbits(9:9+47)
|
|
|
|
lratio(49:128)=softbits(65:65+80-1)
|
|
|
|
lratio=exp(2.0*lratio/(sigma*sigma))
|
|
|
|
|
|
|
|
unscrambledsoftbits(1:127:2)=lratio(1:64)
|
|
|
|
unscrambledsoftbits(2:128:2)=lratio(65:128)
|
2016-05-26 17:19:09 -04:00
|
|
|
|
2016-06-05 21:53:04 -04:00
|
|
|
max_iterations=10
|
2016-06-08 22:02:50 -04:00
|
|
|
max_dither=50
|
2016-05-28 13:58:06 -04:00
|
|
|
call ldpc_decode(unscrambledsoftbits, decoded, max_iterations, niterations, max_dither, ndither)
|
2016-05-26 17:19:09 -04:00
|
|
|
|
2016-06-04 21:19:54 -04:00
|
|
|
if( niterations .ge. 0.0 ) then
|
|
|
|
goto 778
|
2016-05-28 16:53:35 -04:00
|
|
|
endif
|
2016-06-05 12:40:45 -04:00
|
|
|
|
2016-06-04 21:19:54 -04:00
|
|
|
enddo
|
|
|
|
enddo
|
2016-06-05 12:02:26 -04:00
|
|
|
enddo
|
2016-06-04 21:19:54 -04:00
|
|
|
|
|
|
|
msgreceived=' '
|
2016-06-08 22:02:50 -04:00
|
|
|
phase0=-98
|
|
|
|
i1hashdec=0
|
2016-06-04 21:19:54 -04:00
|
|
|
goto 999
|
2016-05-26 17:19:09 -04:00
|
|
|
|
2016-06-04 21:19:54 -04:00
|
|
|
778 continue
|
2016-05-28 16:53:35 -04:00
|
|
|
! The decoder found a codeword - compare decoded hash with calculated
|
|
|
|
! Collapse 80 decoded bits to 10 bytes. Bytes 1-9 are the message, byte 10 is the hash
|
2016-05-28 13:58:06 -04:00
|
|
|
do ibyte=1,10
|
|
|
|
itmp=0
|
|
|
|
do ibit=1,8
|
|
|
|
itmp=ishft(itmp,1)+iand(1,decoded((ibyte-1)*8+ibit))
|
|
|
|
enddo
|
|
|
|
i1Dec8BitBytes(ibyte)=itmp
|
|
|
|
enddo
|
2016-05-26 17:19:09 -04:00
|
|
|
|
2016-05-28 13:58:06 -04:00
|
|
|
! Calculate the hash using the first 9 bytes.
|
|
|
|
ihashdec=nhash(c_loc(i1Dec8BitBytes),int(9,c_size_t),146)
|
|
|
|
ihashdec=2*iand(ihashdec,255)
|
2016-05-26 17:19:09 -04:00
|
|
|
|
2016-05-28 13:58:06 -04:00
|
|
|
! Compare calculated hash with received byte 10 - if they agree, keep the message.
|
|
|
|
i1hashdec=ihashdec
|
2016-05-28 16:53:35 -04:00
|
|
|
|
|
|
|
if( i1hashdec .eq. i1Dec8BitBytes(10) ) then
|
|
|
|
! Good hash --- unpack 72-bit message
|
|
|
|
do ibyte=1,12
|
|
|
|
itmp=0
|
|
|
|
do ibit=1,6
|
|
|
|
itmp=ishft(itmp,1)+iand(1,decoded((ibyte-1)*6+ibit))
|
|
|
|
enddo
|
|
|
|
i4Dec6BitWords(ibyte)=itmp
|
|
|
|
enddo
|
|
|
|
call unpackmsg(i4Dec6BitWords,msgreceived)
|
2016-06-05 12:02:26 -04:00
|
|
|
else
|
|
|
|
msgreceived=' '
|
2016-06-08 22:02:50 -04:00
|
|
|
phase0=-99
|
2016-05-28 16:53:35 -04:00
|
|
|
endif
|
2016-06-01 23:19:08 -04:00
|
|
|
|
2016-06-08 22:02:50 -04:00
|
|
|
999 continue
|
|
|
|
write(78,1001) nutc,t0,iav,ipk,is,fdiff,fest,nbadsync1,nbadsync2, &
|
|
|
|
phase0,niterations,ndither,i1hashdec,i1Dec8BitBytes(10),msgreceived
|
|
|
|
1001 format(i6,f8.2,i4,i4,i4,f8.2,f8.2,i4,i4,f8.2,i4,i4,i4,i4,2x,a22)
|
2016-05-26 17:19:09 -04:00
|
|
|
|
2016-06-08 22:02:50 -04:00
|
|
|
return
|
2016-05-28 13:58:06 -04:00
|
|
|
end subroutine syncmsk144
|