| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  | program wspr5d
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-01 12:32:59 +00:00
										 |  |  | ! Decode WSPR-LF data read from *.c5 or *.wav files.
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-01 12:32:59 +00:00
										 |  |  | ! WSPR-LF is a potential WSPR-like mode intended for use at LF and MF.
 | 
					
						
							|  |  |  | ! It uses an LDPC (300,60) code, OQPSK modulation, and 5 minute T/R sequences.
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  | ! Reception and Demodulation algorithm:
 | 
					
						
							|  |  |  | !   1. Compute coarse spectrum; find fc1 = approx carrier freq
 | 
					
						
							|  |  |  | !   2. Mix from fc1 to 0; LPF at +/- 0.75*R
 | 
					
						
							|  |  |  | !   3. Square, FFT; find peaks near -R/2 and +R/2 to get fc2
 | 
					
						
							|  |  |  | !   4. Mix from fc2 to 0
 | 
					
						
							|  |  |  | !   5. Fit cb13 (central part of csync) to c -> lag, phase
 | 
					
						
							|  |  |  | !   6. Fit complex ploynomial for channel equalization
 | 
					
						
							|  |  |  | !   7. Get soft bits from equalized data
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-01 12:32:59 +00:00
										 |  |  | ! Still to do: find and decode more than one signal in the specified passband.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |   include 'wsprlf_params.f90'
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  |   parameter (NMAX=300*12000)
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |   character arg*8,message*22,cbits*50,infile*80,fname*16,datetime*11
 | 
					
						
							|  |  |  |   character*120 data_dir
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |   complex csync(0:NZ-1)                 !Sync symbols only, from cbb
 | 
					
						
							|  |  |  |   complex c(0:NZ-1)                     !Complex waveform
 | 
					
						
							|  |  |  |   complex c1(0:NZ-1)                    !Complex waveform
 | 
					
						
							|  |  |  |   complex zz(NS+ND)                     !Complex symbol values (intermediate)
 | 
					
						
							|  |  |  |   complex z
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  |   real*8 fMHz
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |   real rxdata(ND),llr(ND)               !Soft symbols
 | 
					
						
							|  |  |  |   real pp(2*NSPS)                       !Shaped pulse for OQPSK
 | 
					
						
							|  |  |  |   real a(5)                             !For twkfreq1
 | 
					
						
							|  |  |  |   real aa(20),bb(20)                    !Fitted polyco's
 | 
					
						
							| 
									
										
										
										
											2017-05-31 14:08:54 +00:00
										 |  |  |   real fpks(20)
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |   integer id(NS+ND)                     !NRZ values (+/-1) for Sync and Data
 | 
					
						
							|  |  |  |   integer ierror(NS+ND)
 | 
					
						
							|  |  |  |   integer isync(48)                     !Long sync vector
 | 
					
						
							|  |  |  |   integer ib13(13)                      !Barker 13 code
 | 
					
						
							| 
									
										
										
										
											2017-04-28 17:46:24 +00:00
										 |  |  |   integer ihdr(11)
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |   integer*8 n8
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  |   integer*2 iwave(NMAX)                 !Generated full-length waveform  
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |   integer*1 idat(7)
 | 
					
						
							|  |  |  |   integer*1 decoded(KK),apmask(ND),cw(ND)
 | 
					
						
							|  |  |  |   data ib13/1,1,1,1,1,-1,-1,1,1,-1,1,-1,1/
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |   nargs=iargc()
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  |   if(nargs.lt.2) then
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |      print*,'Usage:   wspr5d [-a <data_dir>] [-f fMHz] file1 [file2 ...]'
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |      go to 999
 | 
					
						
							|  |  |  |   endif
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |   iarg=1
 | 
					
						
							|  |  |  |   data_dir="."
 | 
					
						
							|  |  |  |   call getarg(iarg,arg)
 | 
					
						
							|  |  |  |   if(arg(1:2).eq.'-a') then
 | 
					
						
							|  |  |  |      call getarg(iarg+1,data_dir)
 | 
					
						
							|  |  |  |      iarg=iarg+2
 | 
					
						
							|  |  |  |   endif
 | 
					
						
							|  |  |  |   call getarg(iarg,arg)
 | 
					
						
							|  |  |  |   if(arg(1:2).eq.'-f') then
 | 
					
						
							|  |  |  |      call getarg(iarg+1,arg)
 | 
					
						
							|  |  |  |      read(arg,*) fMHz
 | 
					
						
							|  |  |  |      iarg=iarg+2
 | 
					
						
							|  |  |  |   endif
 | 
					
						
							|  |  |  |   
 | 
					
						
							|  |  |  |   open(13,file=trim(data_dir)//'/ALL_WSPR.TXT',status='unknown',   &
 | 
					
						
							|  |  |  |        position='append')
 | 
					
						
							| 
									
										
										
										
											2018-05-18 15:04:15 +00:00
										 |  |  | !  maxn=4                                 !Default value
 | 
					
						
							|  |  |  |   maxn=2
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |   twopi=8.0*atan(1.0)
 | 
					
						
							|  |  |  |   fs=NSPS*12000.0/NSPS0                  !Sample rate
 | 
					
						
							|  |  |  |   dt=1.0/fs                              !Sample interval (s)
 | 
					
						
							|  |  |  |   tt=NSPS*dt                             !Duration of "itone" symbols (s)
 | 
					
						
							|  |  |  |   ts=2*NSPS*dt                           !Duration of OQPSK symbols (s)
 | 
					
						
							|  |  |  |   baud=1.0/tt                            !Keying rate for "itone" symbols (baud)
 | 
					
						
							|  |  |  |   txt=NZ*dt                              !Transmission length (s)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   do i=1,N2                              !Half-sine pulse shape
 | 
					
						
							|  |  |  |      pp(i)=sin(0.5*(i-1)*twopi/(2*NSPS))
 | 
					
						
							|  |  |  |   enddo
 | 
					
						
							|  |  |  |   n8=z'cbf089223a51'
 | 
					
						
							|  |  |  |   do i=1,48
 | 
					
						
							|  |  |  |      isync(i)=-1
 | 
					
						
							|  |  |  |      if(iand(n8,1).eq.1) isync(i)=1
 | 
					
						
							|  |  |  |      n8=n8/2
 | 
					
						
							|  |  |  |   enddo
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 19:06:57 +00:00
										 |  |  | ! Define array id() for sync symbols
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |   id=0
 | 
					
						
							|  |  |  |   do j=1,48                             !First group of 48
 | 
					
						
							|  |  |  |      id(2*j-1)=2*isync(j)
 | 
					
						
							|  |  |  |   enddo
 | 
					
						
							|  |  |  |   do j=1,13                             !Barker 13 code
 | 
					
						
							|  |  |  |      id(j+96)=2*ib13(j)
 | 
					
						
							|  |  |  |   enddo
 | 
					
						
							|  |  |  |   do j=1,48                             !Second group of 48
 | 
					
						
							|  |  |  |      id(2*j+109)=2*isync(j)
 | 
					
						
							|  |  |  |   enddo
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 19:06:57 +00:00
										 |  |  |   csync=0.
 | 
					
						
							|  |  |  |   do j=1,205
 | 
					
						
							|  |  |  |      if(abs(id(j)).eq.2) then
 | 
					
						
							|  |  |  |         ia=nint((j-0.5)*N2)
 | 
					
						
							|  |  |  |         ib=ia+N2-1
 | 
					
						
							|  |  |  |         csync(ia:ib)=pp*id(j)/abs(id(j))
 | 
					
						
							|  |  |  |      endif
 | 
					
						
							|  |  |  |   enddo
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-18 15:04:15 +00:00
										 |  |  | write(*,*) 'iarg, nargs ',iarg,nargs
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |   do ifile=iarg,nargs
 | 
					
						
							|  |  |  |      call getarg(ifile,infile)
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  |      open(10,file=infile,status='old',access='stream')
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |      j1=index(infile,'.c5')
 | 
					
						
							|  |  |  |      j2=index(infile,'.wav')
 | 
					
						
							|  |  |  |      if(j1.gt.0) then
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  |         read(10,end=999) fname,ntrmin,fMHz,c
 | 
					
						
							|  |  |  |         read(fname(8:11),*) nutc
 | 
					
						
							| 
									
										
										
										
											2017-05-25 16:24:57 +00:00
										 |  |  |         write(datetime,'(i11)') nutc
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |      else if(j2.gt.0) then
 | 
					
						
							| 
									
										
										
										
											2017-04-28 17:46:24 +00:00
										 |  |  |         read(10,end=999) ihdr,iwave
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |         read(infile(j2-4:j2-1),*) nutc
 | 
					
						
							|  |  |  |         datetime=infile(j2-11:j2-1)
 | 
					
						
							| 
									
										
										
										
											2017-04-28 17:46:24 +00:00
										 |  |  |         call wspr5_downsample(iwave,c)
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  |      else
 | 
					
						
							|  |  |  |         print*,'Wrong file format?'
 | 
					
						
							|  |  |  |         go to 999
 | 
					
						
							|  |  |  |      endif
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |      close(10)
 | 
					
						
							| 
									
										
										
										
											2017-06-02 13:58:32 +00:00
										 |  |  |      fa=100.0
 | 
					
						
							| 
									
										
										
										
											2017-05-13 19:34:47 +00:00
										 |  |  |      fb=150.0
 | 
					
						
							|  |  |  |      call getfc1w(c,fs,fa,fb,fc1,xsnr)         !First approx for freq
 | 
					
						
							| 
									
										
										
										
											2017-06-02 13:58:32 +00:00
										 |  |  |      npeaks=20
 | 
					
						
							| 
									
										
										
										
											2017-05-31 14:08:54 +00:00
										 |  |  |      call getfc2w(c,csync,npeaks,fs,fc1,fpks)      !Refined freq
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-31 14:08:54 +00:00
										 |  |  |      a(1)=-fc1
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |      a(2:5)=0.
 | 
					
						
							|  |  |  |      call twkfreq1(c,NZ,fs,a,c)       !Mix c down by fc1+fc2
 | 
					
						
							| 
									
										
										
										
											2017-05-01 12:32:59 +00:00
										 |  |  | ! Find time offset xdt
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |      amax=0.
 | 
					
						
							|  |  |  |      jpk=0
 | 
					
						
							|  |  |  |      iaa=0
 | 
					
						
							|  |  |  |      ibb=NZ-1
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  |      jmax=1260
 | 
					
						
							|  |  |  |      do j=-jmax,jmax,NSPS/8
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |         ia=j
 | 
					
						
							|  |  |  |         ib=NZ-1+j
 | 
					
						
							|  |  |  |         if(ia.lt.0) then
 | 
					
						
							|  |  |  |            ia=0
 | 
					
						
							|  |  |  |            iaa=-j
 | 
					
						
							|  |  |  |         else
 | 
					
						
							|  |  |  |            iaa=0
 | 
					
						
							|  |  |  |         endif
 | 
					
						
							|  |  |  |         if(ib.gt.NZ-1) then
 | 
					
						
							|  |  |  |            ib=NZ-1
 | 
					
						
							|  |  |  |            ibb=NZ-1-j
 | 
					
						
							|  |  |  |         endif
 | 
					
						
							|  |  |  |         z=sum(c(ia:ib)*conjg(csync(iaa:ibb)))
 | 
					
						
							|  |  |  |         if(abs(z).gt.amax) then
 | 
					
						
							|  |  |  |            amax=abs(z)
 | 
					
						
							|  |  |  |            jpk=j
 | 
					
						
							|  |  |  |         endif
 | 
					
						
							|  |  |  |      enddo
 | 
					
						
							|  |  |  |      xdt=jpk/fs
 | 
					
						
							| 
									
										
										
										
											2017-05-31 14:08:54 +00:00
										 |  |  | xdt=1.0
 | 
					
						
							|  |  |  | jpk=fs*xdt
 | 
					
						
							| 
									
										
										
										
											2017-04-28 12:04:05 +00:00
										 |  |  |      do i=0,NZ-1
 | 
					
						
							|  |  |  |         j=i+jpk
 | 
					
						
							|  |  |  |         if(j.ge.0 .and. j.lt.NZ) c1(i)=c(j)
 | 
					
						
							|  |  |  |      enddo
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      nterms=maxn
 | 
					
						
							| 
									
										
										
										
											2017-05-31 14:08:54 +00:00
										 |  |  |      do itry=1,npeaks
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |         nhard0=0
 | 
					
						
							|  |  |  |         nhardsync0=0
 | 
					
						
							|  |  |  |         ifer=1
 | 
					
						
							| 
									
										
										
										
											2017-05-31 14:08:54 +00:00
										 |  |  |         a(1)=-fpks(itry)
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |         a(2:5)=0.
 | 
					
						
							|  |  |  |         call twkfreq1(c1,NZ,fs,a,c)       !Mix c1 into c
 | 
					
						
							|  |  |  |         call cpolyfitw(c,pp,id,maxn,aa,bb,zz,nhs)
 | 
					
						
							|  |  |  |         call msksoftsymw(zz,aa,bb,id,nterms,ierror,rxdata,nhard0,nhardsync0)
 | 
					
						
							|  |  |  |         if(nhardsync0.gt.35) cycle
 | 
					
						
							|  |  |  |         rxav=sum(rxdata)/ND
 | 
					
						
							|  |  |  |         rx2av=sum(rxdata*rxdata)/ND
 | 
					
						
							|  |  |  |         rxsig=sqrt(rx2av-rxav*rxav)
 | 
					
						
							|  |  |  |         rxdata=rxdata/rxsig
 | 
					
						
							|  |  |  |         ss=0.84
 | 
					
						
							|  |  |  |         llr=2.0*rxdata/(ss*ss)
 | 
					
						
							|  |  |  |         apmask=0
 | 
					
						
							|  |  |  |         max_iterations=40
 | 
					
						
							|  |  |  |         ifer=0
 | 
					
						
							|  |  |  |         call bpdecode300(llr,apmask,max_iterations,decoded,niterations,cw)
 | 
					
						
							| 
									
										
										
										
											2018-05-18 15:04:15 +00:00
										 |  |  |         nhardmin=0
 | 
					
						
							|  |  |  |         if(niterations.lt.0) call osd300(llr,apmask,5,decoded,cw,nhardmin,dmin)
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |         nbadcrc=0
 | 
					
						
							| 
									
										
										
										
											2018-05-18 15:04:15 +00:00
										 |  |  |         call chkcrc10(decoded,nbadcrc)
 | 
					
						
							|  |  |  |         if(nbadcrc.ne.0) ifer=1
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |         if(ifer.eq.0) exit
 | 
					
						
							|  |  |  |      enddo                                !Freq dither loop
 | 
					
						
							|  |  |  |      message='                      '
 | 
					
						
							|  |  |  |      if(ifer.eq.0) then
 | 
					
						
							|  |  |  |         write(cbits,1100) decoded(1:50)
 | 
					
						
							|  |  |  | 1100    format(50i1)
 | 
					
						
							|  |  |  |         read(cbits,1102) idat
 | 
					
						
							|  |  |  | 1102    format(6b8,b2)
 | 
					
						
							|  |  |  |         idat(7)=ishft(idat(7),6)
 | 
					
						
							|  |  |  |         call wqdecode(idat,message,itype)
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |         nsnr=nint(xsnr)
 | 
					
						
							| 
									
										
										
										
											2017-05-31 14:08:54 +00:00
										 |  |  | !        freq=fMHz + 1.d-6*(fc1+fc2)
 | 
					
						
							| 
									
										
										
										
											2018-01-01 17:03:55 +00:00
										 |  |  | !        freq=fMHz + 1.d-6*(fc1+fpks(itry))
 | 
					
						
							|  |  |  |         freq=fc1+fpks(itry)
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |         nfdot=0
 | 
					
						
							|  |  |  |         write(13,1110) datetime,0,nsnr,xdt,freq,message,nfdot
 | 
					
						
							|  |  |  | 1110    format(a11,2i4,f6.2,f12.7,2x,a22,i3)
 | 
					
						
							| 
									
										
										
										
											2018-05-18 15:04:15 +00:00
										 |  |  |         write(*,1112) datetime(8:11),nsnr,xdt,freq,nfdot,message,itry,nhardmin
 | 
					
						
							| 
									
										
										
										
											2018-01-01 17:03:55 +00:00
										 |  |  | !1112    format(a4,i4,f5.1,f11.6,i3,2x,a22,i4)
 | 
					
						
							| 
									
										
										
										
											2018-05-18 15:04:15 +00:00
										 |  |  | 1112    format(a4,i4,f8.3,f8.3,i3,2x,a22,i4,i4)
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  |      endif
 | 
					
						
							| 
									
										
										
										
											2017-04-28 16:49:09 +00:00
										 |  |  |   enddo                                   ! ifile loop
 | 
					
						
							| 
									
										
										
										
											2017-05-12 14:39:38 +00:00
										 |  |  |   write(*,1120)
 | 
					
						
							|  |  |  | 1120 format("<DecodeFinished>")
 | 
					
						
							| 
									
										
										
										
											2017-04-27 17:43:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 999 end program wspr5d
 |