summaryrefslogtreecommitdiff
path: root/lib/fsk4hf
diff options
context:
space:
mode:
authorhibby <d@vehibberd.com>2018-02-16 15:17:14 +0000
committerhibby <d@vehibberd.com>2018-02-16 15:17:14 +0000
commit37f727ac0746fd85504aa46c1f023c09b3eff33a (patch)
treec8a7ce04545040a8f161ba279528730bd9a3e6ab /lib/fsk4hf
parentd63dd8d081a6fcd0715cd13f26df59fb831769d2 (diff)
New upstream version 1.8.0+repackupstream/1.8.0+repack
Diffstat (limited to 'lib/fsk4hf')
-rw-r--r--lib/fsk4hf/Makefile50
-rw-r--r--lib/fsk4hf/Makefile.win84
-rw-r--r--lib/fsk4hf/baseline.f9048
-rw-r--r--lib/fsk4hf/bpdecode120.f90306
-rw-r--r--lib/fsk4hf/bpdecode168.f90380
-rw-r--r--lib/fsk4hf/bpdecode174.f90401
-rw-r--r--lib/fsk4hf/bpdecode300.f90708
-rw-r--r--lib/fsk4hf/chkcrc10.f9027
-rw-r--r--lib/fsk4hf/chkcrc12.f9027
-rw-r--r--lib/fsk4hf/chkcrc12a.f9024
-rw-r--r--lib/fsk4hf/cpolyfit.f9076
-rw-r--r--lib/fsk4hf/cpolyfitw.f9068
-rw-r--r--lib/fsk4hf/dbpsksim.f90241
-rw-r--r--lib/fsk4hf/dopspread.f9062
-rw-r--r--lib/fsk4hf/encode120.f90116
-rw-r--r--lib/fsk4hf/encode168.f90141
-rw-r--r--lib/fsk4hf/encode174.f9050
-rw-r--r--lib/fsk4hf/encode300.f90308
-rw-r--r--lib/fsk4hf/extractmessage168.f9048
-rw-r--r--lib/fsk4hf/extractmessage174.f9048
-rw-r--r--lib/fsk4hf/fftw3.f9064
-rw-r--r--lib/fsk4hf/four2a.f90115
-rw-r--r--lib/fsk4hf/fsk4hf.f90145
-rw-r--r--lib/fsk4hf/fsk4sim.f90185
-rw-r--r--lib/fsk4hf/ft8_downsample.f9042
-rw-r--r--lib/fsk4hf/ft8_params.f9012
-rw-r--r--lib/fsk4hf/ft8apset.f9031
-rw-r--r--lib/fsk4hf/ft8b.f90437
-rw-r--r--lib/fsk4hf/ft8d.f9063
-rw-r--r--lib/fsk4hf/ft8sim.f90126
-rw-r--r--lib/fsk4hf/genbpsk.f9044
-rw-r--r--lib/fsk4hf/genfsk4.f9036
-rw-r--r--lib/fsk4hf/genfsk4hf.f9051
-rw-r--r--lib/fsk4hf/genft8.f9056
-rw-r--r--lib/fsk4hf/genft8refsig.f9022
-rw-r--r--lib/fsk4hf/genmskhf.f90126
-rw-r--r--lib/fsk4hf/genwspr5.f90107
-rw-r--r--lib/fsk4hf/genwspr_fsk8.f9045
-rw-r--r--lib/fsk4hf/genwsprlf.f90137
-rw-r--r--lib/fsk4hf/getfc1.f9058
-rw-r--r--lib/fsk4hf/getfc1w.f9047
-rw-r--r--lib/fsk4hf/getfc2.f9074
-rw-r--r--lib/fsk4hf/getfc2w.f9082
-rw-r--r--lib/fsk4hf/gran.c28
-rw-r--r--lib/fsk4hf/ldpc_174_87_params.f90102
-rw-r--r--lib/fsk4hf/ldpc_300_60_params.f90262
-rw-r--r--lib/fsk4hf/ldpcsim120.f90238
-rw-r--r--lib/fsk4hf/ldpcsim168.f90233
-rw-r--r--lib/fsk4hf/ldpcsim174.f90238
-rw-r--r--lib/fsk4hf/ldpcsim300.f90252
-rw-r--r--lib/fsk4hf/mskhfsim.f90194
-rw-r--r--lib/fsk4hf/msksoftsym.f9081
-rw-r--r--lib/fsk4hf/msksoftsymw.f9078
-rw-r--r--lib/fsk4hf/osd174.f90377
-rw-r--r--lib/fsk4hf/osd300.f90184
-rw-r--r--lib/fsk4hf/polyfit4.f90109
-rw-r--r--lib/fsk4hf/spec4.f9035
-rw-r--r--lib/fsk4hf/spec8.f9031
-rw-r--r--lib/fsk4hf/subtractft8.f9061
-rw-r--r--lib/fsk4hf/sync8.f90151
-rw-r--r--lib/fsk4hf/sync8d.f9054
-rw-r--r--lib/fsk4hf/tweak1.f9023
-rw-r--r--lib/fsk4hf/twkfreq1.f9026
-rw-r--r--lib/fsk4hf/watterson.f9062
-rw-r--r--lib/fsk4hf/wavhdr.f90110
-rw-r--r--lib/fsk4hf/wspr5_downsample.f9029
-rw-r--r--lib/fsk4hf/wspr5_wav.f9048
-rw-r--r--lib/fsk4hf/wspr5d.f90217
-rw-r--r--lib/fsk4hf/wspr5d_exp.f90499
-rw-r--r--lib/fsk4hf/wspr5sim.f90111
-rw-r--r--lib/fsk4hf/wspr_fsk8_downsample.f9027
-rw-r--r--lib/fsk4hf/wspr_fsk8_params.f9014
-rw-r--r--lib/fsk4hf/wspr_fsk8_sim.f90107
-rw-r--r--lib/fsk4hf/wspr_fsk8_wav.f9044
-rw-r--r--lib/fsk4hf/wspr_fsk8d.f90196
-rw-r--r--lib/fsk4hf/wsprlf.f90110
-rw-r--r--lib/fsk4hf/wsprlf_params.f9014
-rw-r--r--lib/fsk4hf/wsprlfsim.f90285
78 files changed, 9948 insertions, 0 deletions
diff --git a/lib/fsk4hf/Makefile b/lib/fsk4hf/Makefile
new file mode 100644
index 00000000..9372821b
--- /dev/null
+++ b/lib/fsk4hf/Makefile
@@ -0,0 +1,50 @@
+# Compilers
+CC = gcc
+CXX = g++
+FC = gfortran
+
+FFLAGS = -O2 -fbounds-check -Wall -Wno-conversion
+CFLAGS = -O2 -I.
+
+# Default rules
+%.o: %.c
+ ${CC} ${CFLAGS} -c $<
+%.o: %.f
+ ${FC} ${FFLAGS} -c $<
+%.o: %.F
+ ${FC} ${FFLAGS} -c $<
+%.o: %.f90
+ ${FC} ${FFLAGS} -c $<
+%.o: %.F90
+ ${FC} ${FFLAGS} -c $<
+
+all: wsprlf
+
+OBJS0 = testpsk.o four2a.o bpfilter.o nonlinear.o tweak1.o spectrum.o smo.o
+testpsk: $(OBJS0)
+ $(FC) -o testpsk $(OBJS0) -lfftw3f
+
+OBJS1 = gmsk8.o four2a.o gaussfilt.o
+gmsk8: $(OBJS1)
+ $(FC) -o gmsk8 $(OBJS1) -lfftw3f
+
+OBJS2 = testfsk.o four2a.o smo.o
+testfsk: $(OBJS2)
+ $(FC) -o testfsk $(OBJS2) -lfftw3f
+
+OBJS3 = fsk2sim.o four2a.o smo.o wavhdr.o gran.o
+fsk2sim: $(OBJS3)
+ $(FC) -o fsk2sim $(OBJS3) -lfftw3f
+
+OBJS4 = fsk4sim.o four2a.o wavhdr.o gran.o tweak1.o
+fsk4sim: $(OBJS4)
+ $(FC) -o fsk4sim $(OBJS4) -lfftw3f
+
+OBJS5 = wsprlf.o four2a.o
+wsprlf: $(OBJS5)
+ $(FC) -o wsprlf $(OBJS5) -lfftw3f
+
+.PHONY : clean
+
+clean:
+ $(RM) *.o testpsk testfsk fsk2sim fsk4sim wsprlf
diff --git a/lib/fsk4hf/Makefile.win b/lib/fsk4hf/Makefile.win
new file mode 100644
index 00000000..ab383557
--- /dev/null
+++ b/lib/fsk4hf/Makefile.win
@@ -0,0 +1,84 @@
+# Compilers
+CC = gcc
+CXX = g++
+FC = gfortran
+
+FFLAGS = -O2 -fbounds-check -Wall -Wno-conversion
+CFLAGS = -O2 -I.
+
+# Default rules
+%.o: %.c
+ ${CC} ${CFLAGS} -c $<
+%.o: %.f
+ ${FC} ${FFLAGS} -c $<
+%.o: %.F
+ ${FC} ${FFLAGS} -c $<
+%.o: %.f90
+ ${FC} ${FFLAGS} -c $<
+%.o: %.F90
+ ${FC} ${FFLAGS} -c $<
+
+all: dbpsksim.exe
+
+OBJS0 = testpsk.o four2a.o bpfilter.o nonlinear.o tweak1.o spectrum.o smo.o
+testpsk: $(OBJS0)
+ $(FC) -o testpsk $(OBJS0) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS1 = gmsk8.o four2a.o gaussfilt.o
+gmsk8: $(OBJS1)
+ $(FC) -o gmsk8 $(OBJS1) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS2 = testfsk.o four2a.o smo.o
+testfsk: $(OBJS2)
+ $(FC) -o testfsk $(OBJS2) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS3 = fsk2sim.o four2a.o smo.o wavhdr.o gran.o
+fsk2sim: $(OBJS3)
+ $(FC) -o fsk2sim $(OBJS3) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS4 = fsk4sim.o four2a.o gran.o genfsk4.o smo.o getsnr.o spec4.o \
+ watterson.o db.o snr2_wsprlf.o pctile.o shell.o snr_wsprlf.o
+fsk4sim.exe: $(OBJS4)
+ $(FC) -o fsk4sim.exe $(OBJS4) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS5 = wsprlf.o four2a.o downsample.o
+wsprlf.exe: $(OBJS5)
+ $(FC) -o wsprlf.exe $(OBJS5) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS6 = wspr_gmsk.o four2a.o gaussfilt.o
+wspr_gmsk.exe: $(OBJS6)
+ $(FC) -o wspr_gmsk.exe $(OBJS6) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS7 = wspr_msk.o four2a.o bpfilter.o
+wspr_msk.exe: $(OBJS7)
+ $(FC) -o wspr_msk.exe $(OBJS7) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS8 = dbpsksim.o four2a.o gran.o genbpsk.o watterson.o db.o \
+ encode120.o bpdecode120.o platanh.o
+dbpsksim.exe: $(OBJS8)
+ $(FC) -o dbpsksim.exe $(OBJS8) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS9 = fsk4a.o four2a.o gran.o genfsk4a.o spec4.o \
+ watterson.o db.o
+fsk4a.exe: $(OBJS9)
+ $(FC) -o fsk4a.exe $(OBJS9) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS10 = gmsk8.o gaussfilt.o four2a.o
+gmsk8.exe: $(OBJS10)
+ $(FC) -o gmsk8.exe $(OBJS10) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS11 = gmsksim.o four2a.o gran.o gengmsk.o genbpsk.o watterson.o db.o \
+ encode168.o bpdecode168.o platanh.o gaussfilt.o tweak1.o smo121.o
+gmsksim.exe: $(OBJS11)
+ $(FC) -o gmsksim.exe $(OBJS11) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+OBJS12 = mskhfsim.o four2a.o gran.o genmskhf.o watterson.o db.o \
+ encode168.o bpdecode168.o platanh.o twkfreq1.o smo121.o \
+ polyfit4.o
+mskhfsim.exe: $(OBJS12)
+ $(FC) -o mskhfsim.exe $(OBJS12) C:\JTSDK\fftw3f\libfftw3f-3.dll
+
+.PHONY : clean
+
+clean:
+ $(RM) *.o testpsk.exe testfsk.exe fsk2sim.exe fsk4sim.exe wsprlf.exe
diff --git a/lib/fsk4hf/baseline.f90 b/lib/fsk4hf/baseline.f90
new file mode 100644
index 00000000..035f9ab4
--- /dev/null
+++ b/lib/fsk4hf/baseline.f90
@@ -0,0 +1,48 @@
+subroutine baseline(s,nfa,nfb,sbase)
+
+! Fit baseline to spectrum (for FT8)
+! Input: s(npts) Linear scale in power
+! Output: sbase(npts) Baseline
+
+ implicit real*8 (a-h,o-z)
+ real*4 s(1920)
+ real*4 sbase(1920)
+ real*4 base
+ real*8 x(1000),y(1000),a(5)
+ data nseg/10/,npct/10/
+
+ df=12000.0/3840.0 !3.125 Hz
+ ia=max(1,nint(nfa/df))
+ ib=nint(nfb/df)
+ do i=ia,ib
+ s(i)=10.0*log10(s(i)) !Convert to dB scale
+ enddo
+
+ nterms=5
+ nlen=(ib-ia+1)/nseg !Length of test segment
+ i0=(ib-ia+1)/2 !Midpoint
+ k=0
+ do n=1,nseg !Loop over all segments
+ ja=ia + (n-1)*nlen
+ jb=ja+nlen-1
+ call pctile(s(ja),nlen,npct,base) !Find lowest npct of points
+ do i=ja,jb
+ if(s(i).le.base) then
+ if (k.lt.1000) k=k+1 !Save all "lower envelope" points
+ x(k)=i-i0
+ y(k)=s(i)
+ endif
+ enddo
+ enddo
+ kz=k
+ a=0.
+ call polyfit(x,y,y,kz,nterms,0,a,chisqr) !Fit a low-order polynomial
+ do i=ia,ib
+ t=i-i0
+ sbase(i)=a(1)+t*(a(2)+t*(a(3)+t*(a(4)+t*(a(5))))) + 0.65
+! write(51,3051) i*df,s(i),sbase(i)
+!3051 format(3f12.3)
+ enddo
+
+ return
+end subroutine baseline
diff --git a/lib/fsk4hf/bpdecode120.f90 b/lib/fsk4hf/bpdecode120.f90
new file mode 100644
index 00000000..c9b11ac3
--- /dev/null
+++ b/lib/fsk4hf/bpdecode120.f90
@@ -0,0 +1,306 @@
+subroutine bpdecode120(llr,apmask,maxiterations,decoded,niterations,cw)
+
+! A log-domain belief propagation decoder for the (120,60) code.
+
+integer, parameter:: N=120, K=60, M=N-K
+integer*1 codeword(N),cw(N),apmask(N)
+integer colorder(N)
+integer*1 decoded(K)
+integer Nm(7,M) ! 5, 6, or 7 bits per check
+integer Mn(3,N) ! 3 checks per bit
+integer synd(M)
+real tov(3,N)
+real toc(7,M)
+real tanhtoc(7,M)
+real zn(N)
+real llr(N)
+real Tmn
+integer nrw(M)
+
+data colorder/ &
+ 0,1,2,21,3,4,5,6,7,8,20,10,9,11,12,23,13,28,14,31, &
+ 15,16,22,26,17,30,18,29,25,32,41,34,19,33,27,36,38,43,42,24, &
+ 37,39,45,40,35,44,47,46,50,51,53,48,52,56,54,57,55,49,58,61, &
+ 60,59,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, &
+ 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99, &
+ 100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119/
+
+data Mn/ &
+ 1, 18, 48, &
+ 2, 4, 51, &
+ 3, 23, 47, &
+ 5, 36, 42, &
+ 6, 43, 49, &
+ 7, 24, 55, &
+ 8, 35, 60, &
+ 9, 26, 30, &
+ 10, 29, 45, &
+ 11, 13, 46, &
+ 12, 53, 54, &
+ 14, 20, 57, &
+ 15, 16, 58, &
+ 17, 39, 44, &
+ 19, 37, 41, &
+ 21, 28, 34, &
+ 22, 50, 59, &
+ 25, 31, 52, &
+ 27, 32, 38, &
+ 33, 40, 56, &
+ 1, 11, 47, &
+ 2, 10, 16, &
+ 3, 12, 27, &
+ 4, 24, 28, &
+ 5, 23, 60, &
+ 6, 29, 39, &
+ 7, 31, 54, &
+ 8, 50, 56, &
+ 9, 13, 14, &
+ 15, 22, 41, &
+ 17, 26, 40, &
+ 18, 25, 45, &
+ 19, 20, 55, &
+ 21, 30, 36, &
+ 32, 49, 59, &
+ 33, 53, 58, &
+ 34, 38, 46, &
+ 29, 35, 57, &
+ 37, 43, 48, &
+ 42, 51, 52, &
+ 7, 11, 44, &
+ 1, 42, 58, &
+ 2, 13, 49, &
+ 3, 20, 40, &
+ 4, 18, 56, &
+ 5, 45, 55, &
+ 6, 21, 31, &
+ 8, 46, 52, &
+ 9, 12, 48, &
+ 10, 37, 38, &
+ 14, 15, 25, &
+ 16, 17, 60, &
+ 19, 39, 53, &
+ 22, 44, 51, &
+ 23, 28, 41, &
+ 24, 32, 35, &
+ 26, 45, 59, &
+ 27, 33, 36, &
+ 30, 47, 54, &
+ 34, 50, 57, &
+ 33, 43, 55, &
+ 1, 41, 57, &
+ 2, 40, 54, &
+ 3, 6, 24, &
+ 4, 11, 59, &
+ 5, 13, 56, &
+ 7, 16, 34, &
+ 8, 19, 26, &
+ 9, 31, 58, &
+ 10, 21, 53, &
+ 12, 22, 60, &
+ 14, 38, 51, &
+ 15, 43, 46, &
+ 17, 48, 50, &
+ 18, 27, 39, &
+ 20, 28, 44, &
+ 23, 25, 49, &
+ 4, 29, 36, &
+ 30, 32, 52, &
+ 35, 37, 47, &
+ 39, 42, 59, &
+ 1, 21, 40, &
+ 2, 50, 55, &
+ 3, 8, 10, &
+ 5, 31, 37, &
+ 6, 14, 60, &
+ 7, 36, 49, &
+ 9, 34, 39, &
+ 11, 19, 25, &
+ 12, 52, 57, &
+ 13, 22, 29, &
+ 15, 30, 56, &
+ 16, 18, 20, &
+ 17, 24, 46, &
+ 23, 38, 58, &
+ 26, 28, 43, &
+ 2, 27, 41, &
+ 5, 32, 44, &
+ 33, 47, 51, &
+ 35, 48, 53, &
+ 42, 43, 54, &
+ 34, 45, 47, &
+ 1, 8, 49, &
+ 3, 14, 59, &
+ 4, 31, 46, &
+ 6, 20, 50, &
+ 7, 26, 53, &
+ 9, 10, 36, &
+ 11, 58, 60, &
+ 12, 21, 45, &
+ 13, 28, 33, &
+ 15, 17, 35, &
+ 16, 38, 52, &
+ 18, 41, 54, &
+ 19, 23, 32, &
+ 22, 40, 55, &
+ 24, 25, 42, &
+ 26, 27, 56, &
+ 29, 44, 54, &
+ 30, 37, 55/
+
+data Nm/ &
+ 1, 21, 42, 62, 82, 103, 0, &
+ 2, 22, 43, 63, 83, 97, 0, &
+ 3, 23, 44, 64, 84, 104, 0, &
+ 2, 24, 45, 65, 78, 105, 0, &
+ 4, 25, 46, 66, 85, 98, 0, &
+ 5, 26, 47, 64, 86, 106, 0, &
+ 6, 27, 41, 67, 87, 107, 0, &
+ 7, 28, 48, 68, 84, 103, 0, &
+ 8, 29, 49, 69, 88, 108, 0, &
+ 9, 22, 50, 70, 84, 108, 0, &
+ 10, 21, 41, 65, 89, 109, 0, &
+ 11, 23, 49, 71, 90, 110, 0, &
+ 10, 29, 43, 66, 91, 111, 0, &
+ 12, 29, 51, 72, 86, 104, 0, &
+ 13, 30, 51, 73, 92, 112, 0, &
+ 13, 22, 52, 67, 93, 113, 0, &
+ 14, 31, 52, 74, 94, 112, 0, &
+ 1, 32, 45, 75, 93, 114, 0, &
+ 15, 33, 53, 68, 89, 115, 0, &
+ 12, 33, 44, 76, 93, 106, 0, &
+ 16, 34, 47, 70, 82, 110, 0, &
+ 17, 30, 54, 71, 91, 116, 0, &
+ 3, 25, 55, 77, 95, 115, 0, &
+ 6, 24, 56, 64, 94, 117, 0, &
+ 18, 32, 51, 77, 89, 117, 0, &
+ 8, 31, 57, 68, 96, 107, 118, &
+ 19, 23, 58, 75, 97, 118, 0, &
+ 16, 24, 55, 76, 96, 111, 0, &
+ 9, 26, 38, 78, 91, 119, 0, &
+ 8, 34, 59, 79, 92, 120, 0, &
+ 18, 27, 47, 69, 85, 105, 0, &
+ 19, 35, 56, 79, 98, 115, 0, &
+ 20, 36, 58, 61, 99, 111, 0, &
+ 16, 37, 60, 67, 88, 102, 0, &
+ 7, 38, 56, 80, 100, 112, 0, &
+ 4, 34, 58, 78, 87, 108, 0, &
+ 15, 39, 50, 80, 85, 120, 0, &
+ 19, 37, 50, 72, 95, 113, 0, &
+ 14, 26, 53, 75, 81, 88, 0, &
+ 20, 31, 44, 63, 82, 116, 0, &
+ 15, 30, 55, 62, 97, 114, 0, &
+ 4, 40, 42, 81, 101, 117, 0, &
+ 5, 39, 61, 73, 96, 101, 0, &
+ 14, 41, 54, 76, 98, 119, 0, &
+ 9, 32, 46, 57, 102, 110, 0, &
+ 10, 37, 48, 73, 94, 105, 0, &
+ 3, 21, 59, 80, 99, 102, 0, &
+ 1, 39, 49, 74, 100, 0, 0, &
+ 5, 35, 43, 77, 87, 103, 0, &
+ 17, 28, 60, 74, 83, 106, 0, &
+ 2, 40, 54, 72, 99, 0, 0, &
+ 18, 40, 48, 79, 90, 113, 0, &
+ 11, 36, 53, 70, 100, 107, 0, &
+ 11, 27, 59, 63, 101, 114, 119, &
+ 6, 33, 46, 61, 83, 116, 120, &
+ 20, 28, 45, 66, 92, 118, 0, &
+ 12, 38, 60, 62, 90, 0, 0, &
+ 13, 36, 42, 69, 95, 109, 0, &
+ 17, 35, 57, 65, 81, 104, 0, &
+ 7, 25, 52, 71, 86, 109, 0/
+
+data nrw/ &
+6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, &
+6,6,6,6,6,7,6,6,6,6,6,6,6,6,6,6,6,6,6,6, &
+6,6,6,6,6,6,6,5,6,6,5,6,6,7,7,6,5,6,6,6/
+
+ncw=3
+
+toc=0
+tov=0
+tanhtoc=0
+!write(*,*) llr
+! initialize messages to checks
+do j=1,M
+ do i=1,nrw(j)
+ toc(i,j)=llr((Nm(i,j)))
+ enddo
+enddo
+
+ncnt=0
+
+do iter=0,maxiterations
+
+! Update bit log likelihood ratios (tov=0 in iteration 0).
+ do i=1,N
+ if( apmask(i) .ne. 1 ) then
+ zn(i)=llr(i)+sum(tov(1:ncw,i))
+ else
+ zn(i)=llr(i)
+ endif
+ enddo
+
+! Check to see if we have a codeword (check before we do any iteration).
+ cw=0
+ where( zn .gt. 0. ) cw=1
+ ncheck=0
+ do i=1,M
+ synd(i)=sum(cw(Nm(1:nrw(i),i)))
+ if( mod(synd(i),2) .ne. 0 ) ncheck=ncheck+1
+! if( mod(synd(i),2) .ne. 0 ) write(*,*) 'check ',i,' unsatisfied'
+ enddo
+!write(*,*) 'number of unsatisfied parity checks ',ncheck
+ if( ncheck .eq. 0 ) then ! we have a codeword - reorder the columns and return it
+ niterations=iter
+ codeword=cw(colorder+1)
+ decoded=codeword(M+1:N)
+ return
+ endif
+
+ if( iter.gt.0 ) then ! this code block implements an early stopping criterion
+ nd=ncheck-nclast
+ if( nd .lt. 0 ) then ! # of unsatisfied parity checks decreased
+ ncnt=0 ! reset counter
+ else
+ ncnt=ncnt+1
+ endif
+! write(*,*) iter,ncheck,nd,ncnt
+ if( ncnt .ge. 3 .and. iter .ge. 5 .and. ncheck .gt. 10) then
+ niterations=-1
+ return
+ endif
+ endif
+ nclast=ncheck
+
+! Send messages from bits to check nodes
+ do j=1,M
+ do i=1,nrw(j)
+ ibj=Nm(i,j)
+ toc(i,j)=zn(ibj)
+ do kk=1,ncw ! subtract off what the bit had received from the check
+ if( Mn(kk,ibj) .eq. j ) then
+ toc(i,j)=toc(i,j)-tov(kk,ibj)
+ endif
+ enddo
+ enddo
+ enddo
+
+! send messages from check nodes to variable nodes
+ do i=1,M
+ tanhtoc(1:7,i)=tanh(-toc(1:7,i)/2)
+ enddo
+
+ do j=1,N
+ do i=1,ncw
+ ichk=Mn(i,j) ! Mn(:,j) are the checks that include bit j
+ Tmn=product(tanhtoc(1:nrw(ichk),ichk),mask=Nm(1:nrw(ichk),ichk).ne.j)
+ call platanh(-Tmn,y)
+! y=atanh(-Tmn)
+ tov(i,j)=2*y
+ enddo
+ enddo
+
+enddo
+niterations=-1
+return
+end subroutine bpdecode120
diff --git a/lib/fsk4hf/bpdecode168.f90 b/lib/fsk4hf/bpdecode168.f90
new file mode 100644
index 00000000..d527f71d
--- /dev/null
+++ b/lib/fsk4hf/bpdecode168.f90
@@ -0,0 +1,380 @@
+subroutine bpdecode168(llr,apmask,maxiterations,decoded,niterations)
+!
+! A log-domain belief propagation decoder for the (168,84) code.
+!
+integer, parameter:: N=168, K=84, M=N-K
+integer*1 codeword(N),cw(N),apmask(N)
+integer colorder(N)
+integer*1 decoded(K)
+integer Nm(7,M) ! 5, 6, or 7 bits per check
+integer Mn(3,N) ! 3 checks per bit
+integer synd(M)
+real tov(3,N)
+real toc(7,M)
+real tanhtoc(7,M)
+real zn(N)
+real llr(N)
+real Tmn
+integer nrw(M)
+
+data colorder/0,1,2,3,28,4,5,6,7,8,9,10,11,34,12,32,13,14,15,16,17, &
+ 18,36,29,42,31,20,21,41,40,30,38,22,19,47,37,46,35,44,33,49,24, &
+ 43,51,25,26,27,50,52,57,69,54,55,45,59,58,56,61,60,53,48,23,62, &
+ 63,64,67,66,65,68,39,70,71,72,74,73,75,76,77,80,81,78,82,79,83, &
+ 84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104, &
+ 105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, &
+ 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146, &
+ 147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167/
+
+data Mn/ &
+ 1,24,67, &
+ 2,5,71, &
+ 3,31,66, &
+ 4,50,58, &
+ 6,60,65, &
+ 7,32,76, &
+ 8,49,83, &
+ 9,36,41, &
+ 10,40,63, &
+ 11,14,62, &
+ 12,72,75, &
+ 13,23,78, &
+ 15,16,80, &
+ 17,54,64, &
+ 18,51,59, &
+ 19,30,48, &
+ 20,68,81, &
+ 21,29,70, &
+ 22,25,43, &
+ 26,34,73, &
+ 27,35,37, &
+ 28,39,44, &
+ 33,53,55, &
+ 38,52,84, &
+ 42,56,57, &
+ 45,74,82, &
+ 46,69,79, &
+ 47,61,77, &
+ 1,4,5, &
+ 2,48,52, &
+ 3,47,82, &
+ 6,26,76, &
+ 7,9,16, &
+ 8,10,78, &
+ 11,36,56, &
+ 12,38,65, &
+ 13,43,81, &
+ 14,33,68, &
+ 15,18,44, &
+ 17,59,77, &
+ 19,27,69, &
+ 20,21,58, &
+ 22,45,79, &
+ 23,34,54, &
+ 24,28,40, &
+ 25,80,84, &
+ 29,37,51, &
+ 30,42,83, &
+ 31,63,72, &
+ 32,50,66, &
+ 35,67,73, &
+ 39,55,74, &
+ 41,61,71, &
+ 46,60,62, &
+ 49,70,74, &
+ 53,64,75, &
+ 25,57,67, &
+ 1,46,64, &
+ 2,51,63, &
+ 3,14,80, &
+ 4,15,78, &
+ 5,27,74, &
+ 6,13,70, &
+ 7,19,20, &
+ 8,38,77, &
+ 9,75,83, &
+ 10,36,69, &
+ 11,22,29, &
+ 12,58,82, &
+ 16,35,60, &
+ 17,32,43, &
+ 18,42,45, &
+ 21,53,84, &
+ 23,39,48, &
+ 24,52,68, &
+ 26,33,61, &
+ 28,56,76, &
+ 30,65,66, &
+ 31,34,49, &
+ 37,47,81, &
+ 16,40,54, &
+ 41,44,65, &
+ 50,73,79, &
+ 55,59,60, &
+ 54,57,71, &
+ 23,62,72, &
+ 1,36,47, &
+ 2,32,70, &
+ 3,28,69, &
+ 4,7,33, &
+ 5,20,26, &
+ 6,14,63, &
+ 8,22,68, &
+ 9,13,67, &
+ 10,55,71, &
+ 11,15,19, &
+ 12,51,56, &
+ 17,27,52, &
+ 18,34,46, &
+ 21,41,42, &
+ 24,50,80, &
+ 25,39,75, &
+ 29,54,76, &
+ 30,40,84, &
+ 31,35,58, &
+ 37,79,83, &
+ 38,43,73, &
+ 44,72,81, &
+ 7,45,62, &
+ 47,48,49, &
+ 53,57,78, &
+ 20,59,66, &
+ 28,61,64, &
+ 11,75,77, &
+ 33,54,82, &
+ 1,14,44, &
+ 2,62,73, &
+ 3,9,26, &
+ 4,37,84, &
+ 5,56,80, &
+ 6,45,71, &
+ 8,67,72, &
+ 10,76,81, &
+ 12,32,78, &
+ 13,59,82, &
+ 15,17,79, &
+ 16,42,69, &
+ 18,61,70, &
+ 19,31,64, &
+ 21,39,63, &
+ 22,30,58, &
+ 23,27,66, &
+ 24,41,49, &
+ 25,36,60, &
+ 29,65,67, &
+ 34,36,53, &
+ 35,48,76, &
+ 15,38,55, &
+ 40,43,74, &
+ 46,52,57, &
+ 50,63,77, &
+ 51,68,69, &
+ 2,44,83, &
+ 1,30,55, &
+ 3,29,78, &
+ 4,34,65, &
+ 5,31,38, &
+ 6,52,58, &
+ 7,25,51, &
+ 8,16,66, &
+ 9,46,74, &
+ 10,70,75, &
+ 11,32,84, &
+ 12,48,79, &
+ 13,50,64, &
+ 14,37,57, &
+ 17,42,72, &
+ 18,43,48, &
+ 19,24,60, &
+ 20,54,83, &
+ 21,47,62, &
+ 22,28,59, &
+ 23,61,80, &
+ 8,26,39, &
+ 27,44,53, &
+ 33,49,56, &
+ 35,68,71, &
+ 12,26,40/
+
+data Nm/ &
+ 1,29,58,87,116,144,0,&
+ 2,30,59,88,117,143,0,&
+ 3,31,60,89,118,145,0,&
+ 4,29,61,90,119,146,0,&
+ 2,29,62,91,120,147,0,&
+ 5,32,63,92,121,148,0,&
+ 6,33,64,90,109,149,0,&
+ 7,34,65,93,122,150,164,&
+ 8,33,66,94,118,151,0,&
+ 9,34,67,95,123,152,0,&
+ 10,35,68,96,114,153,0,&
+ 11,36,69,97,124,154,168,&
+ 12,37,63,94,125,155,0,&
+ 10,38,60,92,116,156,0,&
+ 13,39,61,96,126,138,0,&
+ 13,33,70,81,127,150,0,&
+ 14,40,71,98,126,157,0,&
+ 15,39,72,99,128,158,0,&
+ 16,41,64,96,129,159,0,&
+ 17,42,64,91,112,160,0,&
+ 18,42,73,100,130,161,0,&
+ 19,43,68,93,131,162,0,&
+ 12,44,74,86,132,163,0,&
+ 1,45,75,101,133,159,0,&
+ 19,46,57,102,134,149,0,&
+ 20,32,76,91,118,164,168,&
+ 21,41,62,98,132,165,0,&
+ 22,45,77,89,113,162,0,&
+ 18,47,68,103,135,145,0,&
+ 16,48,78,104,131,144,0,&
+ 3,49,79,105,129,147,0,&
+ 6,50,71,88,124,153,0,&
+ 23,38,76,90,115,166,0,&
+ 20,44,79,99,136,146,0,&
+ 21,51,70,105,137,167,0,&
+ 8,35,67,87,134,136,0,&
+ 21,47,80,106,119,156,0,&
+ 24,36,65,107,138,147,0,&
+ 22,52,74,102,130,164,0,&
+ 9,45,81,104,139,168,0,&
+ 8,53,82,100,133,0,0,&
+ 25,48,72,100,127,157,0,&
+ 19,37,71,107,139,158,0,&
+ 22,39,82,108,116,143,165,&
+ 26,43,72,109,121,0,0,&
+ 27,54,58,99,140,151,0,&
+ 28,31,80,87,110,161,0,&
+ 16,30,74,110,137,154,158,&
+ 7,55,79,110,133,166,0,&
+ 4,50,83,101,141,155,0,&
+ 15,47,59,97,142,149,0,&
+ 24,30,75,98,140,148,0,&
+ 23,56,73,111,136,165,0,&
+ 14,44,81,85,103,115,160,&
+ 23,52,84,95,138,144,0,&
+ 25,35,77,97,120,166,0,&
+ 25,57,85,111,140,156,0,&
+ 4,42,69,105,131,148,0,&
+ 15,40,84,112,125,162,0,&
+ 5,54,70,84,134,159,0,&
+ 28,53,76,113,128,163,0,&
+ 10,54,86,109,117,161,0,&
+ 9,49,59,92,130,141,0,&
+ 14,56,58,113,129,155,0,&
+ 5,36,78,82,135,146,0,&
+ 3,50,78,112,132,150,0,&
+ 1,51,57,94,122,135,0,&
+ 17,38,75,93,142,167,0,&
+ 27,41,67,89,127,142,0,&
+ 18,55,63,88,128,152,0,&
+ 2,53,85,95,121,167,0,&
+ 11,49,86,108,122,157,0,&
+ 20,51,83,107,117,0,0,&
+ 26,52,55,62,139,151,0,&
+ 11,56,66,102,114,152,0,&
+ 6,32,77,103,123,137,0,&
+ 28,40,65,114,141,0,0,&
+ 12,34,61,111,124,145,0,&
+ 27,43,83,106,126,154,0,&
+ 13,46,60,101,120,163,0,&
+ 17,37,80,108,123,0,0,&
+ 26,31,69,115,125,0,0,&
+ 7,48,66,106,143,160,0,&
+ 24,46,73,104,119,153,0/
+
+data nrw/ &
+6,6,6,6,6,6,6,7,6,6,6,7,6,6,6,6,6,6,6,6,6, &
+6,6,6,6,7,6,6,6,6,6,6,6,6,6,6,6,6,6,6,5,6, &
+6,7,5,6,6,7,6,6,6,6,6,7,6,6,6,6,6,6,6,6,6, &
+6,6,6,6,6,6,6,6,6,5,6,6,6,5,6,6,6,5,5,6,6/
+
+ncw=3
+
+toc=0
+tov=0
+tanhtoc=0
+!write(*,*) llr
+! initialize messages to checks
+do j=1,M
+ do i=1,nrw(j)
+ toc(i,j)=llr((Nm(i,j)))
+ enddo
+enddo
+
+ncnt=0
+
+do iter=0,maxiterations
+
+! Update bit log likelihood ratios (tov=0 in iteration 0).
+ do i=1,N
+ if( apmask(i) .ne. 1 ) then
+ zn(i)=llr(i)+sum(tov(1:ncw,i))
+ else
+ zn(i)=llr(i)
+ endif
+ enddo
+
+! Check to see if we have a codeword (check before we do any iteration).
+ cw=0
+ where( zn .gt. 0. ) cw=1
+ ncheck=0
+ do i=1,M
+ synd(i)=sum(cw(Nm(1:nrw(i),i)))
+ if( mod(synd(i),2) .ne. 0 ) ncheck=ncheck+1
+! if( mod(synd(i),2) .ne. 0 ) write(*,*) 'check ',i,' unsatisfied'
+ enddo
+!write(*,*) 'number of unsatisfied parity checks ',ncheck
+ if( ncheck .eq. 0 ) then ! we have a codeword - reorder the columns and return it
+ niterations=iter
+ codeword=cw(colorder+1)
+ decoded=codeword(M+1:N)
+ return
+ endif
+
+ if( iter.gt.0 ) then ! this code block implements an early stopping criterion
+ nd=ncheck-nclast
+ if( nd .lt. 0 ) then ! # of unsatisfied parity checks decreased
+ ncnt=0 ! reset counter
+ else
+ ncnt=ncnt+1
+ endif
+! write(*,*) iter,ncheck,nd,ncnt
+ if( ncnt .ge. 3 .and. iter .ge. 5 .and. ncheck .gt. 10) then
+ niterations=-1
+ return
+ endif
+ endif
+ nclast=ncheck
+
+! Send messages from bits to check nodes
+ do j=1,M
+ do i=1,nrw(j)
+ ibj=Nm(i,j)
+ toc(i,j)=zn(ibj)
+ do kk=1,ncw ! subtract off what the bit had received from the check
+ if( Mn(kk,ibj) .eq. j ) then
+ toc(i,j)=toc(i,j)-tov(kk,ibj)
+ endif
+ enddo
+ enddo
+ enddo
+
+! send messages from check nodes to variable nodes
+ do i=1,M
+ tanhtoc(1:7,i)=tanh(-toc(1:7,i)/2)
+ enddo
+
+ do j=1,N
+ do i=1,ncw
+ ichk=Mn(i,j) ! Mn(:,j) are the checks that include bit j
+ Tmn=product(tanhtoc(1:nrw(ichk),ichk),mask=Nm(1:nrw(ichk),ichk).ne.j)
+ call platanh(-Tmn,y)
+! y=atanh(-Tmn)
+ tov(i,j)=2*y
+ enddo
+ enddo
+
+enddo
+niterations=-1
+return
+end subroutine bpdecode168
diff --git a/lib/fsk4hf/bpdecode174.f90 b/lib/fsk4hf/bpdecode174.f90
new file mode 100644
index 00000000..9442cc4a
--- /dev/null
+++ b/lib/fsk4hf/bpdecode174.f90
@@ -0,0 +1,401 @@
+subroutine bpdecode174(llr,apmask,maxiterations,decoded,cw,nharderror,iter)
+!
+! A log-domain belief propagation decoder for the (174,87) code.
+!
+integer, parameter:: N=174, K=87, M=N-K
+integer*1 codeword(N),cw(N),apmask(N)
+integer colorder(N)
+integer*1 decoded(K)
+integer Nm(7,M) ! 5, 6, or 7 bits per check
+integer Mn(3,N) ! 3 checks per bit
+integer synd(M)
+real tov(3,N)
+real toc(7,M)
+real tanhtoc(7,M)
+real zn(N)
+real llr(N)
+real Tmn
+integer nrw(M)
+
+data colorder/ &
+ 0, 1, 2, 3, 30, 4, 5, 6, 7, 8, 9, 10, 11, 32, 12, 40, 13, 14, 15, 16,&
+ 17, 18, 37, 45, 29, 19, 20, 21, 41, 22, 42, 31, 33, 34, 44, 35, 47, 51, 50, 43,&
+ 36, 52, 63, 46, 25, 55, 27, 24, 23, 53, 39, 49, 59, 38, 48, 61, 60, 57, 28, 62,&
+ 56, 58, 65, 66, 26, 70, 64, 69, 68, 67, 74, 71, 54, 76, 72, 75, 78, 77, 80, 79,&
+ 73, 83, 84, 81, 82, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,&
+ 100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,&
+ 120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,&
+ 140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,&
+ 160,161,162,163,164,165,166,167,168,169,170,171,172,173/
+
+data Mn/ &
+ 1, 25, 69, &
+ 2, 5, 73, &
+ 3, 32, 68, &
+ 4, 51, 61, &
+ 6, 63, 70, &
+ 7, 33, 79, &
+ 8, 50, 86, &
+ 9, 37, 43, &
+ 10, 41, 65, &
+ 11, 14, 64, &
+ 12, 75, 77, &
+ 13, 23, 81, &
+ 15, 16, 82, &
+ 17, 56, 66, &
+ 18, 53, 60, &
+ 19, 31, 52, &
+ 20, 67, 84, &
+ 21, 29, 72, &
+ 22, 24, 44, &
+ 26, 35, 76, &
+ 27, 36, 38, &
+ 28, 40, 42, &
+ 30, 54, 55, &
+ 34, 49, 87, &
+ 39, 57, 58, &
+ 45, 74, 83, &
+ 46, 62, 80, &
+ 47, 48, 85, &
+ 59, 71, 78, &
+ 1, 50, 53, &
+ 2, 47, 84, &
+ 3, 25, 79, &
+ 4, 6, 14, &
+ 5, 7, 80, &
+ 8, 34, 55, &
+ 9, 36, 69, &
+ 10, 43, 83, &
+ 11, 23, 74, &
+ 12, 17, 44, &
+ 13, 57, 76, &
+ 15, 27, 56, &
+ 16, 28, 29, &
+ 18, 19, 59, &
+ 20, 40, 63, &
+ 21, 35, 52, &
+ 22, 54, 64, &
+ 24, 62, 78, &
+ 26, 32, 77, &
+ 30, 72, 85, &
+ 31, 65, 87, &
+ 33, 39, 51, &
+ 37, 48, 75, &
+ 38, 70, 71, &
+ 41, 42, 68, &
+ 45, 67, 86, &
+ 46, 81, 82, &
+ 49, 66, 73, &
+ 58, 60, 66, &
+ 61, 65, 85, &
+ 1, 14, 21, &
+ 2, 13, 59, &
+ 3, 67, 82, &
+ 4, 32, 73, &
+ 5, 36, 54, &
+ 6, 43, 46, &
+ 7, 28, 75, &
+ 8, 33, 71, &
+ 9, 49, 76, &
+ 10, 58, 64, &
+ 11, 48, 68, &
+ 12, 19, 45, &
+ 15, 50, 61, &
+ 16, 22, 26, &
+ 17, 72, 80, &
+ 18, 40, 55, &
+ 20, 35, 51, &
+ 23, 25, 34, &
+ 24, 63, 87, &
+ 27, 39, 74, &
+ 29, 78, 83, &
+ 30, 70, 77, &
+ 31, 69, 84, &
+ 22, 37, 86, &
+ 38, 41, 81, &
+ 42, 44, 57, &
+ 47, 53, 62, &
+ 52, 56, 79, &
+ 60, 75, 81, &
+ 1, 39, 77, &
+ 2, 16, 41, &
+ 3, 31, 54, &
+ 4, 36, 78, &
+ 5, 45, 65, &
+ 6, 57, 85, &
+ 7, 14, 49, &
+ 8, 21, 46, &
+ 9, 15, 72, &
+ 10, 20, 62, &
+ 11, 17, 71, &
+ 12, 34, 47, &
+ 13, 68, 86, &
+ 18, 23, 43, &
+ 19, 64, 73, &
+ 24, 48, 79, &
+ 25, 70, 83, &
+ 26, 80, 87, &
+ 27, 32, 40, &
+ 28, 56, 69, &
+ 29, 63, 66, &
+ 30, 42, 50, &
+ 33, 37, 82, &
+ 35, 60, 74, &
+ 38, 55, 84, &
+ 44, 52, 61, &
+ 51, 53, 72, &
+ 58, 59, 67, &
+ 47, 56, 76, &
+ 1, 19, 37, &
+ 2, 61, 75, &
+ 3, 8, 66, &
+ 4, 60, 84, &
+ 5, 34, 39, &
+ 6, 26, 53, &
+ 7, 32, 57, &
+ 9, 52, 67, &
+ 10, 12, 15, &
+ 11, 51, 69, &
+ 13, 14, 65, &
+ 16, 31, 43, &
+ 17, 20, 36, &
+ 18, 80, 86, &
+ 21, 48, 59, &
+ 22, 40, 46, &
+ 23, 33, 62, &
+ 24, 30, 74, &
+ 25, 42, 64, &
+ 27, 49, 85, &
+ 28, 38, 73, &
+ 29, 44, 81, &
+ 35, 68, 70, &
+ 41, 63, 76, &
+ 45, 49, 71, &
+ 50, 58, 87, &
+ 48, 54, 83, &
+ 13, 55, 79, &
+ 77, 78, 82, &
+ 1, 2, 24, &
+ 3, 6, 75, &
+ 4, 56, 87, &
+ 5, 44, 53, &
+ 7, 50, 83, &
+ 8, 10, 28, &
+ 9, 55, 62, &
+ 11, 29, 67, &
+ 12, 33, 40, &
+ 14, 16, 20, &
+ 15, 35, 73, &
+ 17, 31, 39, &
+ 18, 36, 57, &
+ 19, 46, 76, &
+ 21, 42, 84, &
+ 22, 34, 59, &
+ 23, 26, 61, &
+ 25, 60, 65, &
+ 27, 64, 80, &
+ 30, 37, 66, &
+ 32, 45, 72, &
+ 38, 51, 86, &
+ 41, 77, 79, &
+ 43, 56, 68, &
+ 47, 74, 82, &
+ 40, 52, 78, &
+ 54, 61, 71, &
+ 46, 58, 69/
+
+data Nm/ &
+ 1, 30, 60, 89, 118, 147, 0, &
+ 2, 31, 61, 90, 119, 147, 0, &
+ 3, 32, 62, 91, 120, 148, 0, &
+ 4, 33, 63, 92, 121, 149, 0, &
+ 2, 34, 64, 93, 122, 150, 0, &
+ 5, 33, 65, 94, 123, 148, 0, &
+ 6, 34, 66, 95, 124, 151, 0, &
+ 7, 35, 67, 96, 120, 152, 0, &
+ 8, 36, 68, 97, 125, 153, 0, &
+ 9, 37, 69, 98, 126, 152, 0, &
+ 10, 38, 70, 99, 127, 154, 0, &
+ 11, 39, 71, 100, 126, 155, 0, &
+ 12, 40, 61, 101, 128, 145, 0, &
+ 10, 33, 60, 95, 128, 156, 0, &
+ 13, 41, 72, 97, 126, 157, 0, &
+ 13, 42, 73, 90, 129, 156, 0, &
+ 14, 39, 74, 99, 130, 158, 0, &
+ 15, 43, 75, 102, 131, 159, 0, &
+ 16, 43, 71, 103, 118, 160, 0, &
+ 17, 44, 76, 98, 130, 156, 0, &
+ 18, 45, 60, 96, 132, 161, 0, &
+ 19, 46, 73, 83, 133, 162, 0, &
+ 12, 38, 77, 102, 134, 163, 0, &
+ 19, 47, 78, 104, 135, 147, 0, &
+ 1, 32, 77, 105, 136, 164, 0, &
+ 20, 48, 73, 106, 123, 163, 0, &
+ 21, 41, 79, 107, 137, 165, 0, &
+ 22, 42, 66, 108, 138, 152, 0, &
+ 18, 42, 80, 109, 139, 154, 0, &
+ 23, 49, 81, 110, 135, 166, 0, &
+ 16, 50, 82, 91, 129, 158, 0, &
+ 3, 48, 63, 107, 124, 167, 0, &
+ 6, 51, 67, 111, 134, 155, 0, &
+ 24, 35, 77, 100, 122, 162, 0, &
+ 20, 45, 76, 112, 140, 157, 0, &
+ 21, 36, 64, 92, 130, 159, 0, &
+ 8, 52, 83, 111, 118, 166, 0, &
+ 21, 53, 84, 113, 138, 168, 0, &
+ 25, 51, 79, 89, 122, 158, 0, &
+ 22, 44, 75, 107, 133, 155, 172, &
+ 9, 54, 84, 90, 141, 169, 0, &
+ 22, 54, 85, 110, 136, 161, 0, &
+ 8, 37, 65, 102, 129, 170, 0, &
+ 19, 39, 85, 114, 139, 150, 0, &
+ 26, 55, 71, 93, 142, 167, 0, &
+ 27, 56, 65, 96, 133, 160, 174, &
+ 28, 31, 86, 100, 117, 171, 0, &
+ 28, 52, 70, 104, 132, 144, 0, &
+ 24, 57, 68, 95, 137, 142, 0, &
+ 7, 30, 72, 110, 143, 151, 0, &
+ 4, 51, 76, 115, 127, 168, 0, &
+ 16, 45, 87, 114, 125, 172, 0, &
+ 15, 30, 86, 115, 123, 150, 0, &
+ 23, 46, 64, 91, 144, 173, 0, &
+ 23, 35, 75, 113, 145, 153, 0, &
+ 14, 41, 87, 108, 117, 149, 170, &
+ 25, 40, 85, 94, 124, 159, 0, &
+ 25, 58, 69, 116, 143, 174, 0, &
+ 29, 43, 61, 116, 132, 162, 0, &
+ 15, 58, 88, 112, 121, 164, 0, &
+ 4, 59, 72, 114, 119, 163, 173, &
+ 27, 47, 86, 98, 134, 153, 0, &
+ 5, 44, 78, 109, 141, 0, 0, &
+ 10, 46, 69, 103, 136, 165, 0, &
+ 9, 50, 59, 93, 128, 164, 0, &
+ 14, 57, 58, 109, 120, 166, 0, &
+ 17, 55, 62, 116, 125, 154, 0, &
+ 3, 54, 70, 101, 140, 170, 0, &
+ 1, 36, 82, 108, 127, 174, 0, &
+ 5, 53, 81, 105, 140, 0, 0, &
+ 29, 53, 67, 99, 142, 173, 0, &
+ 18, 49, 74, 97, 115, 167, 0, &
+ 2, 57, 63, 103, 138, 157, 0, &
+ 26, 38, 79, 112, 135, 171, 0, &
+ 11, 52, 66, 88, 119, 148, 0, &
+ 20, 40, 68, 117, 141, 160, 0, &
+ 11, 48, 81, 89, 146, 169, 0, &
+ 29, 47, 80, 92, 146, 172, 0, &
+ 6, 32, 87, 104, 145, 169, 0, &
+ 27, 34, 74, 106, 131, 165, 0, &
+ 12, 56, 84, 88, 139, 0, 0, &
+ 13, 56, 62, 111, 146, 171, 0, &
+ 26, 37, 80, 105, 144, 151, 0, &
+ 17, 31, 82, 113, 121, 161, 0, &
+ 28, 49, 59, 94, 137, 0, 0, &
+ 7, 55, 83, 101, 131, 168, 0, &
+ 24, 50, 78, 106, 143, 149, 0/
+
+data nrw/ &
+ 6,6,6,6,6,6,6,6,6,6, &
+ 6,6,6,6,6,6,6,6,6,6, &
+ 6,6,6,6,6,6,6,6,6,6, &
+ 6,6,6,6,6,6,6,6,6,7, &
+ 6,6,6,6,6,7,6,6,6,6, &
+ 6,6,6,6,6,7,6,6,6,6, &
+ 7,6,5,6,6,6,6,6,6,5, &
+ 6,6,6,6,6,6,6,6,6,6, &
+ 5,6,6,6,5,6,6/
+
+ncw=3
+
+decoded=0
+toc=0
+tov=0
+tanhtoc=0
+! initialize messages to checks
+do j=1,M
+ do i=1,nrw(j)
+ toc(i,j)=llr((Nm(i,j)))
+ enddo
+enddo
+
+ncnt=0
+
+do iter=0,maxiterations
+
+! Update bit log likelihood ratios (tov=0 in iteration 0).
+ do i=1,N
+ if( apmask(i) .ne. 1 ) then
+ zn(i)=llr(i)+sum(tov(1:ncw,i))
+ else
+ zn(i)=llr(i)
+ endif
+ enddo
+
+! Check to see if we have a codeword (check before we do any iteration).
+ cw=0
+ where( zn .gt. 0. ) cw=1
+ ncheck=0
+ do i=1,M
+ synd(i)=sum(cw(Nm(1:nrw(i),i)))
+ if( mod(synd(i),2) .ne. 0 ) ncheck=ncheck+1
+! if( mod(synd(i),2) .ne. 0 ) write(*,*) 'check ',i,' unsatisfied'
+ enddo
+! write(*,*) 'number of unsatisfied parity checks ',ncheck
+ if( ncheck .eq. 0 ) then ! we have a codeword - reorder the columns and return it
+ codeword=cw(colorder+1)
+ decoded=codeword(M+1:N)
+ nerr=0
+ do i=1,N
+ if( (2*cw(i)-1)*llr(i) .lt. 0.0 ) nerr=nerr+1
+ enddo
+ nharderror=nerr
+ return
+ endif
+
+ if( iter.gt.0 ) then ! this code block implements an early stopping criterion
+! if( iter.gt.10000 ) then ! this code block implements an early stopping criterion
+ nd=ncheck-nclast
+ if( nd .lt. 0 ) then ! # of unsatisfied parity checks decreased
+ ncnt=0 ! reset counter
+ else
+ ncnt=ncnt+1
+ endif
+! write(*,*) iter,ncheck,nd,ncnt
+ if( ncnt .ge. 5 .and. iter .ge. 10 .and. ncheck .gt. 15) then
+ nharderror=-1
+ return
+ endif
+ endif
+ nclast=ncheck
+
+! Send messages from bits to check nodes
+ do j=1,M
+ do i=1,nrw(j)
+ ibj=Nm(i,j)
+ toc(i,j)=zn(ibj)
+ do kk=1,ncw ! subtract off what the bit had received from the check
+ if( Mn(kk,ibj) .eq. j ) then
+ toc(i,j)=toc(i,j)-tov(kk,ibj)
+ endif
+ enddo
+ enddo
+ enddo
+
+! send messages from check nodes to variable nodes
+ do i=1,M
+ tanhtoc(1:7,i)=tanh(-toc(1:7,i)/2)
+ enddo
+
+ do j=1,N
+ do i=1,ncw
+ ichk=Mn(i,j) ! Mn(:,j) are the checks that include bit j
+ Tmn=product(tanhtoc(1:nrw(ichk),ichk),mask=Nm(1:nrw(ichk),ichk).ne.j)
+ call platanh(-Tmn,y)
+! y=atanh(-Tmn)
+ tov(i,j)=2*y
+ enddo
+ enddo
+
+enddo
+nharderror=-1
+return
+end subroutine bpdecode174
diff --git a/lib/fsk4hf/bpdecode300.f90 b/lib/fsk4hf/bpdecode300.f90
new file mode 100644
index 00000000..22f7f885
--- /dev/null
+++ b/lib/fsk4hf/bpdecode300.f90
@@ -0,0 +1,708 @@
+subroutine bpdecode300(llr,apmask,maxiterations,decoded,niterations,cw)
+
+! A log-domain belief propagation decoder for the (300,60) code.
+
+integer, parameter:: N=300, K=60, M=N-K
+integer*1 codeword(N),cw(N),apmask(N)
+integer colorder(N)
+integer*1 decoded(K)
+integer Nm(5,M) ! 4, or 5 bits per check
+integer Mn(7,N) ! 2, 3, or 7 checks per bit
+integer synd(M)
+real tov(7,N)
+real toc(5,M)
+real tanhtoc(5,M)
+real zn(N)
+real llr(N)
+real Tmn
+integer nrw(M)
+integer ncw(N)
+
+data colorder/ &
+0,1,2,3,4,5,6,7,8,9,10,11,123,12,13,14,15,16,17,18, &
+19,20,21,22,23,24,25,138,26,145,27,28,29,30,31,32,33,34,35,36, &
+37,154,38,39,40,41,42,43,44,144,46,47,48,49,50,51,52,53,143,54, &
+125,56,57,58,124,59,120,140,157,160,55,60,61,62,156,162,141,64,65,153, &
+181,183,66,170,67,68,69,130,70,164,71,72,73,74,75,63,76,77,135,78, &
+79,80,176,169,82,83,84,167,180,85,136,158,129,166,175,142,134,146,121,165, &
+88,89,192,90,45,91,92,93,182,189,94,95,96,173,81,97,98,178,122,126, &
+132,99,100,152,186,193,101,102,151,103,104,172,159,168,150,190,147,148,201,107, &
+205,177,108,198,197,174,127,109,185,110,202,87,199,171,179,187,139,137,106,131, &
+206,194,112,149,155,113,128,184,196,86,114,203,212,195,208,105,188,161,163,191, &
+200,209,214,204,115,218,133,111,207,117,213,216,211,217,116,215,219,220,210,221, &
+118,222,223,225,224,228,226,229,231,227,233,119,234,235,232,230,237,239,236,238, &
+240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259, &
+260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279, &
+280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299/
+
+data Mn/ &
+ 1, 67, 0, 0, 0, 0, 0, &
+ 2, 189, 0, 0, 0, 0, 0, &
+ 3, 201, 0, 0, 0, 0, 0, &
+ 4, 13, 0, 0, 0, 0, 0, &
+ 5, 84, 0, 0, 0, 0, 0, &
+ 6, 188, 0, 0, 0, 0, 0, &
+ 7, 140, 0, 0, 0, 0, 0, &
+ 8, 167, 0, 0, 0, 0, 0, &
+ 9, 187, 0, 0, 0, 0, 0, &
+ 10, 173, 0, 0, 0, 0, 0, &
+ 11, 88, 0, 0, 0, 0, 0, &
+ 12, 213, 0, 0, 0, 0, 0, &
+ 14, 141, 0, 0, 0, 0, 0, &
+ 15, 236, 0, 0, 0, 0, 0, &
+ 16, 117, 0, 0, 0, 0, 0, &
+ 17, 99, 0, 0, 0, 0, 0, &
+ 18, 111, 0, 0, 0, 0, 0, &
+ 19, 178, 0, 0, 0, 0, 0, &
+ 20, 28, 0, 0, 0, 0, 0, &
+ 21, 177, 0, 0, 0, 0, 0, &
+ 22, 199, 0, 0, 0, 0, 0, &
+ 23, 209, 0, 0, 0, 0, 0, &
+ 24, 220, 0, 0, 0, 0, 0, &
+ 25, 59, 0, 0, 0, 0, 0, &
+ 26, 224, 0, 0, 0, 0, 0, &
+ 27, 30, 0, 0, 0, 0, 0, &
+ 29, 157, 0, 0, 0, 0, 0, &
+ 31, 184, 0, 0, 0, 0, 0, &
+ 32, 179, 0, 0, 0, 0, 0, &
+ 33, 149, 0, 0, 0, 0, 0, &
+ 34, 144, 0, 0, 0, 0, 0, &
+ 35, 80, 0, 0, 0, 0, 0, &
+ 36, 228, 0, 0, 0, 0, 0, &
+ 37, 185, 0, 0, 0, 0, 0, &
+ 38, 197, 0, 0, 0, 0, 0, &
+ 39, 69, 0, 0, 0, 0, 0, &
+ 40, 42, 0, 0, 0, 0, 0, &
+ 41, 112, 0, 0, 0, 0, 0, &
+ 43, 70, 0, 0, 0, 0, 0, &
+ 44, 198, 0, 0, 0, 0, 0, &
+ 45, 76, 0, 0, 0, 0, 0, &
+ 46, 68, 0, 0, 0, 0, 0, &
+ 47, 90, 0, 0, 0, 0, 0, &
+ 48, 75, 0, 0, 0, 0, 0, &
+ 49, 118, 0, 0, 0, 0, 0, &
+ 50, 125, 0, 0, 0, 0, 0, &
+ 51, 114, 0, 0, 0, 0, 0, &
+ 52, 239, 0, 0, 0, 0, 0, &
+ 53, 108, 0, 0, 0, 0, 0, &
+ 54, 120, 0, 0, 0, 0, 0, &
+ 55, 162, 0, 0, 0, 0, 0, &
+ 56, 218, 0, 0, 0, 0, 0, &
+ 57, 138, 0, 0, 0, 0, 0, &
+ 58, 212, 0, 0, 0, 0, 0, &
+ 60, 207, 0, 0, 0, 0, 0, &
+ 61, 71, 0, 0, 0, 0, 0, &
+ 62, 65, 0, 0, 0, 0, 0, &
+ 63, 161, 0, 0, 0, 0, 0, &
+ 64, 166, 0, 0, 0, 0, 0, &
+ 66, 158, 0, 0, 0, 0, 0, &
+ 72, 235, 0, 0, 0, 0, 0, &
+ 73, 225, 0, 0, 0, 0, 0, &
+ 74, 116, 0, 0, 0, 0, 0, &
+ 77, 96, 0, 0, 0, 0, 0, &
+ 78, 81, 0, 0, 0, 0, 0, &
+ 79, 82, 0, 0, 0, 0, 0, &
+ 83, 229, 0, 0, 0, 0, 0, &
+ 85, 134, 0, 0, 0, 0, 0, &
+ 86, 176, 0, 0, 0, 0, 0, &
+ 87, 203, 0, 0, 0, 0, 0, &
+ 89, 145, 0, 0, 0, 0, 0, &
+ 91, 152, 0, 0, 0, 0, 0, &
+ 92, 237, 0, 0, 0, 0, 0, &
+ 93, 215, 0, 0, 0, 0, 0, &
+ 94, 130, 0, 0, 0, 0, 0, &
+ 95, 156, 0, 0, 0, 0, 0, &
+ 97, 104, 0, 0, 0, 0, 0, &
+ 98, 182, 0, 0, 0, 0, 0, &
+ 100, 222, 0, 0, 0, 0, 0, &
+ 101, 123, 0, 0, 0, 0, 0, &
+ 102, 181, 0, 0, 0, 0, 0, &
+ 103, 135, 0, 0, 0, 0, 0, &
+ 105, 146, 0, 0, 0, 0, 0, &
+ 106, 115, 0, 0, 0, 0, 0, &
+ 107, 109, 0, 0, 0, 0, 0, &
+ 110, 194, 0, 0, 0, 0, 0, &
+ 113, 164, 0, 0, 0, 0, 0, &
+ 119, 172, 0, 0, 0, 0, 0, &
+ 121, 190, 0, 0, 0, 0, 0, &
+ 122, 169, 0, 0, 0, 0, 0, &
+ 124, 211, 0, 0, 0, 0, 0, &
+ 126, 165, 0, 0, 0, 0, 0, &
+ 127, 139, 0, 0, 0, 0, 0, &
+ 128, 129, 0, 0, 0, 0, 0, &
+ 131, 205, 0, 0, 0, 0, 0, &
+ 132, 196, 0, 0, 0, 0, 0, &
+ 133, 193, 0, 0, 0, 0, 0, &
+ 136, 200, 0, 0, 0, 0, 0, &
+ 137, 159, 0, 0, 0, 0, 0, &
+ 142, 204, 0, 0, 0, 0, 0, &
+ 143, 154, 0, 0, 0, 0, 0, &
+ 147, 238, 0, 0, 0, 0, 0, &
+ 148, 175, 0, 0, 0, 0, 0, &
+ 150, 216, 0, 0, 0, 0, 0, &
+ 151, 171, 0, 0, 0, 0, 0, &
+ 153, 231, 0, 0, 0, 0, 0, &
+ 155, 208, 0, 0, 0, 0, 0, &
+ 160, 230, 0, 0, 0, 0, 0, &
+ 163, 223, 0, 0, 0, 0, 0, &
+ 168, 217, 0, 0, 0, 0, 0, &
+ 170, 180, 0, 0, 0, 0, 0, &
+ 174, 233, 0, 0, 0, 0, 0, &
+ 183, 202, 0, 0, 0, 0, 0, &
+ 186, 214, 0, 0, 0, 0, 0, &
+ 191, 206, 0, 0, 0, 0, 0, &
+ 192, 219, 0, 0, 0, 0, 0, &
+ 195, 227, 0, 0, 0, 0, 0, &
+ 210, 226, 0, 0, 0, 0, 0, &
+ 221, 234, 0, 0, 0, 0, 0, &
+ 232, 240, 0, 0, 0, 0, 0, &
+ 1, 106, 0, 0, 0, 0, 0, &
+ 2, 119, 0, 0, 0, 0, 0, &
+ 3, 139, 0, 0, 0, 0, 0, &
+ 4, 14, 0, 0, 0, 0, 0, &
+ 5, 65, 0, 0, 0, 0, 0, &
+ 6, 61, 0, 0, 0, 0, 0, &
+ 7, 223, 0, 0, 0, 0, 0, &
+ 8, 171, 0, 0, 0, 0, 0, &
+ 9, 136, 0, 0, 0, 0, 0, &
+ 10, 113, 0, 0, 0, 0, 0, &
+ 11, 104, 0, 0, 0, 0, 0, &
+ 12, 175, 0, 0, 0, 0, 0, &
+ 13, 203, 0, 0, 0, 0, 0, &
+ 15, 149, 0, 0, 0, 0, 0, &
+ 16, 226, 0, 0, 0, 0, 0, &
+ 17, 219, 0, 0, 0, 0, 0, &
+ 18, 98, 0, 0, 0, 0, 0, &
+ 19, 211, 0, 0, 0, 0, 0, &
+ 20, 49, 0, 0, 0, 0, 0, &
+ 21, 214, 0, 0, 0, 0, 0, &
+ 22, 68, 0, 0, 0, 0, 0, &
+ 23, 77, 0, 0, 0, 0, 0, &
+ 24, 116, 0, 0, 0, 0, 0, &
+ 25, 235, 0, 0, 0, 0, 0, &
+ 26, 50, 0, 0, 0, 0, 0, &
+ 27, 124, 0, 0, 0, 0, 0, &
+ 28, 229, 0, 0, 0, 0, 0, &
+ 29, 83, 0, 0, 0, 0, 0, &
+ 30, 158, 0, 0, 0, 0, 0, &
+ 31, 220, 0, 0, 0, 0, 0, &
+ 32, 155, 0, 0, 0, 0, 0, &
+ 33, 152, 0, 0, 0, 0, 0, &
+ 34, 231, 0, 0, 0, 0, 0, &
+ 35, 207, 0, 0, 0, 0, 0, &
+ 36, 40, 0, 0, 0, 0, 0, &
+ 37, 142, 0, 0, 0, 0, 0, &
+ 38, 75, 0, 0, 0, 0, 0, &
+ 39, 90, 167, 0, 0, 0, 0, &
+ 41, 55, 125, 0, 0, 0, 0, &
+ 42, 153, 196, 0, 0, 0, 0, &
+ 43, 72, 112, 0, 0, 0, 0, &
+ 44, 183, 233, 0, 0, 0, 0, &
+ 45, 81, 178, 0, 0, 0, 0, &
+ 46, 187, 230, 0, 0, 0, 0, &
+ 47, 133, 176, 0, 0, 0, 0, &
+ 48, 54, 186, 0, 0, 0, 0, &
+ 51, 150, 224, 0, 0, 0, 0, &
+ 52, 53, 190, 0, 0, 0, 0, &
+ 56, 143, 228, 0, 0, 0, 0, &
+ 57, 97, 197, 0, 0, 0, 0, &
+ 58, 62, 89, 0, 0, 0, 0, &
+ 59, 174, 194, 0, 0, 0, 0, &
+ 60, 91, 93, 0, 0, 0, 0, &
+ 63, 85, 96, 0, 0, 0, 0, &
+ 64, 92, 205, 0, 0, 0, 0, &
+ 66, 67, 164, 0, 0, 0, 0, &
+ 69, 103, 159, 0, 0, 0, 0, &
+ 70, 117, 122, 0, 0, 0, 0, &
+ 71, 88, 160, 0, 0, 0, 0, &
+ 73, 148, 180, 0, 0, 0, 0, &
+ 74, 108, 109, 0, 0, 0, 0, &
+ 76, 102, 151, 0, 0, 0, 0, &
+ 78, 128, 206, 0, 0, 0, 0, &
+ 79, 215, 239, 0, 0, 0, 0, &
+ 80, 138, 221, 0, 0, 0, 0, &
+ 82, 162, 195, 0, 0, 0, 0, &
+ 84, 161, 184, 0, 0, 0, 0, &
+ 86, 213, 218, 0, 0, 0, 0, &
+ 87, 120, 240, 0, 0, 0, 0, &
+ 94, 100, 157, 0, 0, 0, 0, &
+ 95, 202, 217, 0, 0, 0, 0, &
+ 99, 199, 201, 0, 0, 0, 0, &
+ 101, 127, 225, 0, 0, 0, 0, &
+ 105, 168, 185, 0, 0, 0, 0, &
+ 107, 182, 237, 0, 0, 0, 0, &
+ 110, 147, 208, 0, 0, 0, 0, &
+ 111, 118, 172, 0, 0, 0, 0, &
+ 114, 140, 165, 0, 0, 0, 0, &
+ 115, 130, 141, 0, 0, 0, 0, &
+ 121, 144, 173, 0, 0, 0, 0, &
+ 123, 204, 209, 0, 0, 0, 0, &
+ 126, 137, 188, 0, 0, 0, 0, &
+ 129, 179, 189, 0, 0, 0, 0, &
+ 131, 192, 210, 0, 0, 0, 0, &
+ 132, 200, 238, 0, 0, 0, 0, &
+ 134, 177, 191, 0, 0, 0, 0, &
+ 135, 145, 222, 0, 0, 0, 0, &
+ 146, 229, 236, 0, 0, 0, 0, &
+ 154, 169, 232, 0, 0, 0, 0, &
+ 124, 156, 163, 0, 0, 0, 0, &
+ 166, 223, 234, 0, 0, 0, 0, &
+ 1, 11, 170, 0, 0, 0, 0, &
+ 3, 181, 227, 0, 0, 0, 0, &
+ 193, 198, 220, 0, 0, 0, 0, &
+ 10, 16, 212, 0, 0, 0, 0, &
+ 42, 96, 216, 0, 0, 0, 0, &
+ 2, 6, 215, 0, 0, 0, 0, &
+ 4, 208, 219, 0, 0, 0, 0, &
+ 5, 22, 35, 0, 0, 0, 0, &
+ 7, 12, 20, 0, 0, 0, 0, &
+ 8, 15, 75, 0, 0, 0, 0, &
+ 9, 74, 83, 0, 0, 0, 0, &
+ 13, 37, 50, 0, 0, 0, 0, &
+ 14, 52, 86, 0, 0, 0, 0, &
+ 17, 30, 177, 0, 0, 0, 0, &
+ 18, 25, 97, 0, 0, 0, 0, &
+ 19, 72, 157, 0, 0, 0, 0, &
+ 21, 58, 116, 0, 0, 0, 0, &
+ 23, 111, 226, 0, 0, 0, 0, &
+ 24, 26, 180, 0, 0, 0, 0, &
+ 27, 34, 39, 0, 0, 0, 0, &
+ 28, 32, 161, 0, 0, 0, 0, &
+ 29, 36, 60, 0, 0, 0, 0, &
+ 31, 76, 154, 0, 0, 0, 0, &
+ 33, 101, 238, 0, 0, 0, 0, &
+ 38, 95, 162, 0, 0, 0, 0, &
+ 40, 164, 183, 0, 0, 0, 0, &
+ 41, 92, 196, 0, 0, 0, 0, &
+ 43, 48, 99, 165, 190, 198, 204, &
+ 44, 129, 138, 145, 160, 203, 237, &
+ 45, 65, 66, 98, 127, 137, 146, &
+ 46, 131, 149, 181, 211, 218, 224, &
+ 47, 49, 55, 191, 194, 207, 232, &
+ 51, 69, 106, 109, 119, 184, 217, &
+ 53, 62, 104, 155, 166, 206, 231, &
+ 54, 61, 63, 73, 118, 151, 163, &
+ 56, 94, 110, 117, 185, 189, 214, &
+ 57, 81, 91, 115, 173, 175, 227, &
+ 59, 79, 103, 136, 171, 201, 212, &
+ 24, 64, 77, 93, 202, 235, 236, &
+ 67, 132, 142, 150, 156, 176, 222, &
+ 68, 153, 159, 169, 170, 186, 221, &
+ 70, 84, 89, 113, 174, 197, 205, &
+ 71, 125, 130, 140, 158, 200, 210, &
+ 8, 78, 143, 182, 192, 193, 216, &
+ 23, 80, 82, 90, 108, 139, 228, &
+ 85, 122, 123, 128, 141, 187, 188, &
+ 25, 87, 100, 152, 209, 213, 234, &
+ 88, 134, 147, 167, 172, 178, 239, &
+ 18, 40, 102, 114, 133, 144, 179, &
+ 4, 105, 108, 112, 148, 230, 240, &
+ 29, 33, 50, 62, 107, 195, 199, &
+ 3, 83, 113, 120, 126, 177, 216, &
+ 11, 55, 116, 121, 135, 168, 225, &
+ 1, 27, 28, 76, 187, 226, 233, &
+ 2, 4, 7, 10, 22, 75, 222, &
+ 5, 30, 131, 152, 156, 168, 215, &
+ 6, 13, 19, 58, 196, 228, 229, &
+ 9, 26, 144, 147, 158, 223, 240, &
+ 12, 31, 66, 79, 92, 96, 155, &
+ 14, 54, 103, 173, 202, 232, 238, &
+ 15, 17, 37, 69, 129, 164, 209, &
+ 16, 72, 91, 114, 163, 169, 237, &
+ 20, 45, 89, 99, 143, 180, 208, &
+ 21, 39, 60, 141, 171, 198, 234, &
+ 21, 32, 52, 78, 95, 148, 199, &
+ 34, 73, 84, 157, 200, 221, 236, &
+ 35, 36, 63, 97, 105, 119, 220, &
+ 38, 46, 93, 111, 136, 191, 203, &
+ 41, 51, 151, 160, 213, 214, 231, &
+ 42, 57, 65, 161, 167, 194, 204, &
+ 43, 109, 162, 175, 189, 210, 212, &
+ 44, 74, 100, 149, 170, 188, 197, &
+ 47, 64, 88, 107, 122, 165, 211, &
+ 48, 139, 179, 184, 218, 233, 239, &
+ 49, 94, 106, 112, 138, 142, 205, &
+ 53, 59, 102, 115, 134, 182, 225, &
+ 56, 68, 101, 150, 166, 178, 207, &
+ 61, 117, 126, 154, 195, 219, 224, &
+ 67, 80, 118, 174, 185, 190, 235, &
+ 70, 77, 86, 125, 153, 172, 193, &
+ 32, 71, 87, 90, 98, 110, 135, &
+ 41, 75, 81, 85, 124, 133, 201, &
+ 82, 120, 128, 140, 159, 176, 183, &
+ 22, 72, 104, 130, 146, 181, 217, &
+ 25, 89, 96, 121, 132, 186, 230, &
+ 118, 123, 145, 192, 196, 227, 240, &
+ 1, 14, 35, 38, 114, 127, 192, &
+ 7, 23, 43, 63, 116, 137, 206, &
+ 2, 37, 52, 57, 64, 76, 120/
+
+data Nm/ &
+ 1, 121, 212, 265, 298, &
+ 2, 122, 217, 266, 300, &
+ 3, 123, 213, 263, 0, &
+ 4, 124, 218, 261, 266, &
+ 5, 125, 219, 267, 0, &
+ 6, 126, 217, 268, 0, &
+ 7, 127, 220, 266, 299, &
+ 8, 128, 221, 255, 0, &
+ 9, 129, 222, 269, 0, &
+ 10, 130, 215, 266, 0, &
+ 11, 131, 212, 264, 0, &
+ 12, 132, 220, 270, 0, &
+ 4, 133, 223, 268, 0, &
+ 13, 124, 224, 271, 298, &
+ 14, 134, 221, 272, 0, &
+ 15, 135, 215, 273, 0, &
+ 16, 136, 225, 272, 0, &
+ 17, 137, 226, 260, 0, &
+ 18, 138, 227, 268, 0, &
+ 19, 139, 220, 274, 0, &
+ 20, 140, 228, 275, 276, &
+ 21, 141, 219, 266, 295, &
+ 22, 142, 229, 256, 299, &
+ 23, 143, 230, 250, 0, &
+ 24, 144, 226, 258, 296, &
+ 25, 145, 230, 269, 0, &
+ 26, 146, 231, 265, 0, &
+ 19, 147, 232, 265, 0, &
+ 27, 148, 233, 262, 0, &
+ 26, 149, 225, 267, 0, &
+ 28, 150, 234, 270, 0, &
+ 29, 151, 232, 276, 292, &
+ 30, 152, 235, 262, 0, &
+ 31, 153, 231, 277, 0, &
+ 32, 154, 219, 278, 298, &
+ 33, 155, 233, 278, 0, &
+ 34, 156, 223, 272, 300, &
+ 35, 157, 236, 279, 298, &
+ 36, 158, 231, 275, 0, &
+ 37, 155, 237, 260, 0, &
+ 38, 159, 238, 280, 293, &
+ 37, 160, 216, 281, 0, &
+ 39, 161, 239, 282, 299, &
+ 40, 162, 240, 283, 0, &
+ 41, 163, 241, 274, 0, &
+ 42, 164, 242, 279, 0, &
+ 43, 165, 243, 284, 0, &
+ 44, 166, 239, 285, 0, &
+ 45, 139, 243, 286, 0, &
+ 46, 145, 223, 262, 0, &
+ 47, 167, 244, 280, 0, &
+ 48, 168, 224, 276, 300, &
+ 49, 168, 245, 287, 0, &
+ 50, 166, 246, 271, 0, &
+ 51, 159, 243, 264, 0, &
+ 52, 169, 247, 288, 0, &
+ 53, 170, 248, 281, 300, &
+ 54, 171, 228, 268, 0, &
+ 24, 172, 249, 287, 0, &
+ 55, 173, 233, 275, 0, &
+ 56, 126, 246, 289, 0, &
+ 57, 171, 245, 262, 0, &
+ 58, 174, 246, 278, 299, &
+ 59, 175, 250, 284, 300, &
+ 57, 125, 241, 281, 0, &
+ 60, 176, 241, 270, 0, &
+ 1, 176, 251, 290, 0, &
+ 42, 141, 252, 288, 0, &
+ 36, 177, 244, 272, 0, &
+ 39, 178, 253, 291, 0, &
+ 56, 179, 254, 292, 0, &
+ 61, 161, 227, 273, 295, &
+ 62, 180, 246, 277, 0, &
+ 63, 181, 222, 283, 0, &
+ 44, 157, 221, 266, 293, &
+ 41, 182, 234, 265, 300, &
+ 64, 142, 250, 291, 0, &
+ 65, 183, 255, 276, 0, &
+ 66, 184, 249, 270, 0, &
+ 32, 185, 256, 290, 0, &
+ 65, 163, 248, 293, 0, &
+ 66, 186, 256, 294, 0, &
+ 67, 148, 222, 263, 0, &
+ 5, 187, 253, 277, 0, &
+ 68, 174, 257, 293, 0, &
+ 69, 188, 224, 291, 0, &
+ 70, 189, 258, 292, 0, &
+ 11, 179, 259, 284, 0, &
+ 71, 171, 253, 274, 296, &
+ 43, 158, 256, 292, 0, &
+ 72, 173, 248, 273, 0, &
+ 73, 175, 238, 270, 0, &
+ 74, 173, 250, 279, 0, &
+ 75, 190, 247, 286, 0, &
+ 76, 191, 236, 276, 0, &
+ 64, 174, 216, 270, 296, &
+ 77, 170, 226, 278, 0, &
+ 78, 137, 241, 292, 0, &
+ 16, 192, 239, 274, 0, &
+ 79, 190, 258, 283, 0, &
+ 80, 193, 235, 288, 0, &
+ 81, 182, 260, 287, 0, &
+ 82, 177, 249, 271, 0, &
+ 77, 131, 245, 295, 0, &
+ 83, 194, 261, 278, 0, &
+ 84, 121, 244, 286, 0, &
+ 85, 195, 262, 284, 0, &
+ 49, 181, 256, 261, 0, &
+ 85, 181, 244, 282, 0, &
+ 86, 196, 247, 292, 0, &
+ 17, 197, 229, 279, 0, &
+ 38, 161, 261, 286, 0, &
+ 87, 130, 253, 263, 0, &
+ 47, 198, 260, 273, 298, &
+ 84, 199, 248, 287, 0, &
+ 63, 143, 228, 264, 299, &
+ 15, 178, 247, 289, 0, &
+ 45, 197, 246, 290, 297, &
+ 88, 122, 244, 278, 0, &
+ 50, 189, 263, 294, 300, &
+ 89, 200, 264, 296, 0, &
+ 90, 178, 257, 284, 0, &
+ 80, 201, 257, 297, 0, &
+ 91, 146, 210, 293, 0, &
+ 46, 159, 254, 291, 0, &
+ 92, 202, 263, 289, 0, &
+ 93, 193, 241, 298, 0, &
+ 94, 183, 257, 294, 0, &
+ 94, 203, 240, 272, 0, &
+ 75, 199, 254, 295, 0, &
+ 95, 204, 242, 267, 0, &
+ 96, 205, 251, 296, 0, &
+ 97, 165, 260, 293, 0, &
+ 68, 206, 259, 287, 0, &
+ 82, 207, 264, 292, 0, &
+ 98, 129, 249, 279, 0, &
+ 99, 202, 241, 299, 0, &
+ 53, 185, 240, 286, 0, &
+ 93, 123, 256, 285, 0, &
+ 7, 198, 254, 294, 0, &
+ 13, 199, 257, 275, 0, &
+ 100, 156, 251, 286, 0, &
+ 101, 169, 255, 274, 0, &
+ 31, 200, 260, 269, 0, &
+ 71, 207, 240, 297, 0, &
+ 83, 208, 241, 295, 0, &
+ 102, 196, 259, 269, 0, &
+ 103, 180, 261, 276, 0, &
+ 30, 134, 242, 283, 0, &
+ 104, 167, 251, 288, 0, &
+ 105, 182, 246, 280, 0, &
+ 72, 152, 258, 267, 0, &
+ 106, 160, 252, 291, 0, &
+ 101, 209, 234, 289, 0, &
+ 107, 151, 245, 270, 0, &
+ 76, 210, 251, 267, 0, &
+ 27, 190, 227, 277, 0, &
+ 60, 149, 254, 269, 0, &
+ 99, 177, 252, 294, 0, &
+ 108, 179, 240, 280, 0, &
+ 58, 187, 232, 281, 0, &
+ 51, 186, 236, 282, 0, &
+ 109, 210, 246, 273, 0, &
+ 87, 176, 237, 272, 0, &
+ 92, 198, 239, 284, 0, &
+ 59, 211, 245, 288, 0, &
+ 8, 158, 259, 281, 0, &
+ 110, 194, 264, 267, 0, &
+ 90, 209, 252, 273, 0, &
+ 111, 212, 252, 283, 0, &
+ 105, 128, 249, 275, 0, &
+ 88, 197, 259, 291, 0, &
+ 10, 200, 248, 271, 0, &
+ 112, 172, 253, 290, 0, &
+ 103, 132, 248, 282, 0, &
+ 69, 165, 251, 294, 0, &
+ 20, 206, 225, 263, 0, &
+ 18, 163, 259, 288, 0, &
+ 29, 203, 260, 285, 0, &
+ 111, 180, 230, 274, 0, &
+ 81, 213, 242, 295, 0, &
+ 78, 195, 255, 287, 0, &
+ 113, 162, 237, 294, 0, &
+ 28, 187, 244, 285, 0, &
+ 34, 194, 247, 290, 0, &
+ 114, 166, 252, 296, 0, &
+ 9, 164, 257, 265, 0, &
+ 6, 202, 257, 283, 0, &
+ 2, 203, 247, 282, 0, &
+ 89, 168, 239, 290, 0, &
+ 115, 206, 243, 279, 0, &
+ 116, 204, 255, 297, 298, &
+ 97, 214, 255, 291, 0, &
+ 86, 172, 243, 281, 0, &
+ 117, 186, 262, 289, 0, &
+ 96, 160, 238, 268, 297, &
+ 35, 170, 253, 283, 0, &
+ 40, 214, 239, 275, 0, &
+ 21, 192, 262, 276, 0, &
+ 98, 205, 254, 277, 0, &
+ 3, 192, 249, 293, 0, &
+ 113, 191, 250, 271, 0, &
+ 70, 133, 240, 279, 0, &
+ 100, 201, 239, 281, 0, &
+ 95, 175, 253, 286, 0, &
+ 115, 183, 245, 299, 0, &
+ 55, 154, 243, 288, 0, &
+ 107, 196, 218, 274, 0, &
+ 22, 201, 258, 272, 0, &
+ 118, 204, 254, 282, 0, &
+ 91, 138, 242, 284, 0, &
+ 54, 215, 249, 282, 0, &
+ 12, 188, 258, 280, 0, &
+ 114, 140, 247, 280, 0, &
+ 74, 184, 217, 267, 0, &
+ 104, 216, 255, 263, 0, &
+ 110, 191, 244, 295, 0, &
+ 52, 188, 242, 285, 0, &
+ 116, 136, 218, 289, 0, &
+ 23, 150, 214, 278, 0, &
+ 119, 185, 252, 277, 0, &
+ 79, 207, 251, 266, 0, &
+ 109, 127, 211, 269, 0, &
+ 25, 167, 242, 289, 0, &
+ 62, 193, 264, 287, 0, &
+ 118, 135, 229, 265, 0, &
+ 117, 213, 248, 297, 0, &
+ 33, 169, 256, 268, 0, &
+ 67, 147, 208, 268, 0, &
+ 108, 164, 261, 296, 0, &
+ 106, 153, 245, 280, 0, &
+ 120, 209, 243, 271, 0, &
+ 112, 162, 265, 285, 0, &
+ 119, 211, 258, 275, 0, &
+ 61, 144, 250, 290, 0, &
+ 14, 208, 250, 277, 0, &
+ 73, 195, 240, 273, 0, &
+ 102, 205, 235, 271, 0, &
+ 48, 184, 259, 285, 0, &
+ 120, 189, 261, 269, 297/
+
+data nrw/ &
+5,5,4,5,4,4,5,4,4,4,4,4,4,5,4,4,4,4,4,4, &
+5,5,5,4,5,4,4,4,4,4,4,5,4,4,5,4,5,5,4,4, &
+5,4,5,4,4,4,4,4,4,4,4,5,4,4,4,4,5,4,4,4, &
+4,4,5,5,4,4,4,4,4,4,4,5,4,4,5,5,4,4,4,4, &
+4,4,4,4,4,4,4,4,5,4,4,4,4,4,4,5,4,4,4,4, &
+4,4,4,4,4,4,4,4,4,4,4,4,4,5,4,5,4,5,4,5, &
+4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, &
+4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, &
+4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, &
+4,4,4,4,4,4,4,4,4,4,4,5,4,4,4,5,4,4,4,4, &
+4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, &
+4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5/
+
+data ncw/ &
+2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
+2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
+2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
+2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
+2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
+2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
+2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
+2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3, &
+3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, &
+3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, &
+3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, &
+3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,7,7, &
+7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, &
+7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, &
+7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7/
+
+!ncw=3
+
+toc=0
+tov=0
+tanhtoc=0
+!write(*,*) llr
+! initialize messages to checks
+do j=1,M
+ do i=1,nrw(j)
+ toc(i,j)=llr((Nm(i,j)))
+ enddo
+enddo
+
+ncnt=0
+
+do iter=0,maxiterations
+
+! Update bit log likelihood ratios (tov=0 in iteration 0).
+ do i=1,N
+ if( apmask(i) .ne. 1 ) then
+ zn(i)=llr(i)+sum(tov(1:ncw(i),i))
+ else
+ zn(i)=llr(i)
+ endif
+ enddo
+
+! Check to see if we have a codeword (check before we do any iteration).
+ cw=0
+ where( zn .gt. 0. ) cw=1
+ ncheck=0
+ do i=1,M
+ synd(i)=sum(cw(Nm(1:nrw(i),i)))
+ if( mod(synd(i),2) .ne. 0 ) ncheck=ncheck+1
+! if( mod(synd(i),2) .ne. 0 ) write(*,*) 'check ',i,' unsatisfied'
+ enddo
+!write(*,*) 'number of unsatisfied parity checks ',ncheck
+ if( ncheck .eq. 0 ) then ! we have a codeword - reorder the columns and return it
+! niterations=iter
+ codeword=cw(colorder+1)
+ decoded=codeword(M+1:N)
+ nerr=0
+ do i=1,N
+ if( (2*cw(i)-1)*llr(i) .lt. 0.0 ) nerr=nerr+1
+ enddo
+ niterations=nerr
+ return
+ endif
+
+ if( iter.gt.0 ) then ! this code block implements an early stopping criterion
+ nd=ncheck-nclast
+ if( nd .lt. 0 ) then ! # of unsatisfied parity checks decreased
+ ncnt=0 ! reset counter
+ else
+ ncnt=ncnt+1
+ endif
+! write(*,*) iter,ncheck,nd,ncnt
+ if( ncnt .ge. 5 .and. iter .ge. 15 .and. ncheck .gt. 50) then
+ niterations=-1
+ return
+ endif
+ endif
+ nclast=ncheck
+
+! Send messages from bits to check nodes
+ do j=1,M
+ do i=1,nrw(j)
+ ibj=Nm(i,j)
+ toc(i,j)=zn(ibj)
+! do kk=1,ncw(ibj) ! subtract off what the bit had received from the check
+ do kk=1,7 ! subtract off what the bit had received from the check
+ if( Mn(kk,ibj) .eq. j ) then
+ toc(i,j)=toc(i,j)-tov(kk,ibj)
+ endif
+ enddo
+ enddo
+ enddo
+
+! send messages from check nodes to variable nodes
+ do i=1,M
+ tanhtoc(1:5,i)=tanh(-toc(1:5,i)/2)
+ enddo
+
+ do j=1,N
+ do i=1,ncw(j)
+ ichk=Mn(i,j) ! Mn(:,j) are the checks that include bit j
+ Tmn=product(tanhtoc(1:nrw(ichk),ichk),mask=Nm(1:nrw(ichk),ichk).ne.j)
+ call platanh(-Tmn,y)
+! y=atanh(-Tmn)
+ tov(i,j)=2*y
+ enddo
+ enddo
+
+enddo
+niterations=-1
+return
+end subroutine bpdecode300
diff --git a/lib/fsk4hf/chkcrc10.f90 b/lib/fsk4hf/chkcrc10.f90
new file mode 100644
index 00000000..abe7bd79
--- /dev/null
+++ b/lib/fsk4hf/chkcrc10.f90
@@ -0,0 +1,27 @@
+subroutine chkcrc10(decoded,nbadcrc)
+
+ use crc
+ integer*1 decoded(60)
+ integer*1, target:: i1Dec8BitBytes(9)
+
+! Check the CRC
+ do ibyte=1,6
+ itmp=0
+ do ibit=1,8
+ itmp=ishft(itmp,1)+iand(1,decoded((ibyte-1)*8+ibit))
+ enddo
+ i1Dec8BitBytes(ibyte)=itmp
+ enddo
+ i1Dec8BitBytes(7)=decoded(49)*128+decoded(50)*64
+
+! Pack received CRC into bytes 8 and 9 for crc10_check
+ i1Dec8BitBytes(8)=decoded(51)*2+decoded(52)
+ i1Dec8BitBytes(9)=decoded(53)*128 + decoded(54)*64+decoded(55)*32 + &
+ decoded(56)*16
+ i1Dec8BitBytes(9)=i1Dec8BitBytes(9) + decoded(57)*8+decoded(58)*4 + &
+ decoded(59)*2+decoded(60)*1
+ nbadcrc=1
+ if(crc10_check(c_loc(i1Dec8BitBytes),9)) nbadcrc=0
+
+ return
+end subroutine chkcrc10
diff --git a/lib/fsk4hf/chkcrc12.f90 b/lib/fsk4hf/chkcrc12.f90
new file mode 100644
index 00000000..a177fa11
--- /dev/null
+++ b/lib/fsk4hf/chkcrc12.f90
@@ -0,0 +1,27 @@
+subroutine chkcrc12(decoded,nbadcrc)
+
+ use crc
+ integer*1 decoded(84)
+ integer*1, target:: i1Dec8BitBytes(11)
+
+! Check the CRC
+! Collapse 84 decoded bits to 11 bytes. Bytes 1-9 are the message,
+! byte 10 and first half of byte 11 is the crc
+ do ibyte=1,9
+ itmp=0
+ do ibit=1,8
+ itmp=ishft(itmp,1)+iand(1,decoded((ibyte-1)*8+ibit))
+ enddo
+ i1Dec8BitBytes(ibyte)=itmp
+ enddo
+
+! Pack the crc into bytes 10 and 11 for crc12_check
+ i1Dec8BitBytes(10)=decoded(73)*8 + decoded(74)*4 + decoded(75)*2 + decoded(76)
+ i1Dec8BitBytes(11)=decoded(77)*128 + decoded(78)*64 + &
+ decoded(79)*32 + decoded(80)*16 + decoded(81)*8 + decoded(82)*4 + &
+ decoded(83)*2 + decoded(84)
+ nbadcrc=1
+ if( crc12_check(c_loc (i1Dec8BitBytes), 11) ) nbadcrc=0
+
+ return
+end subroutine chkcrc12
diff --git a/lib/fsk4hf/chkcrc12a.f90 b/lib/fsk4hf/chkcrc12a.f90
new file mode 100644
index 00000000..fc429190
--- /dev/null
+++ b/lib/fsk4hf/chkcrc12a.f90
@@ -0,0 +1,24 @@
+subroutine chkcrc12a(decoded,nbadcrc)
+
+ use crc
+ integer*1 decoded(87)
+ integer*1, target:: i1Dec8BitBytes(11)
+ character*87 cbits
+
+! Write decoded bits into cbits: 75-bit message plus 12-bit CRC
+ write(cbits,1000) decoded
+1000 format(87i1)
+ read(cbits,1001) i1Dec8BitBytes
+1001 format(11b8)
+ read(cbits,1002) ncrc12 !Received CRC12
+1002 format(75x,b12)
+
+ i1Dec8BitBytes(10)=iand(i1Dec8BitBytes(10),128+64+32)
+ i1Dec8BitBytes(11)=0
+ icrc12=crc12(c_loc(i1Dec8BitBytes),11) !CRC12 computed from 75 msg bits
+
+ nbadcrc=1
+ if(ncrc12.eq.icrc12) nbadcrc=0
+
+ return
+end subroutine chkcrc12a
diff --git a/lib/fsk4hf/cpolyfit.f90 b/lib/fsk4hf/cpolyfit.f90
new file mode 100644
index 00000000..fd0d5987
--- /dev/null
+++ b/lib/fsk4hf/cpolyfit.f90
@@ -0,0 +1,76 @@
+subroutine cpolyfit(c,pp,id,maxn,aa,bb,zz,nhardsync)
+
+ parameter (KK=84) !Information bits (72 + CRC12)
+ parameter (ND=168) !Data symbols: LDPC (168,84), r=1/2
+ parameter (NS=65) !Sync symbols (2 x 26 + Barker 13)
+ parameter (NR=3) !Ramp up/down
+ parameter (NN=NR+NS+ND) !Total symbols (236)
+ parameter (NSPS=16) !Samples per MSK symbol (16)
+ parameter (N2=2*NSPS) !Samples per OQPSK symbol (32)
+ parameter (N13=13*N2) !Samples in central sync vector (416)
+ parameter (NZ=NSPS*NN) !Samples in baseband waveform (3760)
+ parameter (NFFT1=4*NSPS,NH1=NFFT1/2)
+
+ complex c(0:NZ-1) !Complex waveform
+ complex zz(NS+ND) !Complex symbol values (intermediate)
+ complex z,z0
+ real x(NS),yi(NS),yq(NS) !For complex polyfit
+ real pp(2*NSPS) !Shaped pulse for OQPSK
+ real aa(20),bb(20) !Fitted polyco's
+ integer id(NS+ND) !NRZ values (+/-1) for Sync and Data
+
+ ib=NSPS-1
+ ib2=N2-1
+ n=0
+ do j=1,117 !First-pass demodulation
+ ia=ib+1
+ ib=ia+N2-1
+ zz(j)=sum(pp*c(ia:ib))/NSPS
+ if(abs(id(j)).eq.2) then !Save all sync symbols
+ n=n+1
+ x(n)=float(ia+ib)/NZ - 1.0
+ yi(n)=real(zz(j))*0.5*id(j)
+ yq(n)=aimag(zz(j))*0.5*id(j)
+! write(54,1225) n,x(n),yi(n),yq(n)
+!1225 format(i5,3f12.4)
+ endif
+ if(j.le.116) then
+ zz(j+117)=sum(pp*c(ia+NSPS:ib+NSPS))/NSPS
+ endif
+ enddo
+
+ aa=0.
+ bb=0.
+ nterms=0
+ chisqa=0.
+ chisqb=0.
+ if(maxn.gt.0) then
+ npts=n
+ mode=0
+ nterms=maxn
+ call polyfit4(x,yi,yi,npts,nterms,mode,aa,chisqa)
+ call polyfit4(x,yq,yq,npts,nterms,mode,bb,chisqb)
+ endif
+
+ nhardsync=0
+ do j=1,117
+ if(abs(id(j)).ne.2) cycle
+ xx=j*2.0/117.0 - 1.0
+ yii=1.
+ yqq=0.
+ if(nterms.gt.0) then
+ yii=aa(1)
+ yqq=bb(1)
+ do i=2,nterms
+ yii=yii + aa(i)*xx**(i-1)
+ yqq=yqq + bb(i)*xx**(i-1)
+ enddo
+ endif
+ z0=cmplx(yii,yqq)
+ z=zz(j)*conjg(z0)
+ p=real(z)
+ if(p*id(j).lt.0) nhardsync=nhardsync+1
+ enddo
+
+ return
+end subroutine cpolyfit
diff --git a/lib/fsk4hf/cpolyfitw.f90 b/lib/fsk4hf/cpolyfitw.f90
new file mode 100644
index 00000000..561052f8
--- /dev/null
+++ b/lib/fsk4hf/cpolyfitw.f90
@@ -0,0 +1,68 @@
+subroutine cpolyfitw(c,pp,id,maxn,aa,bb,zz,nhardsync)
+
+ include 'wsprlf_params.f90'
+
+ complex c(0:NZ-1) !Complex waveform
+ complex zz(NS+ND) !Complex symbol values (intermediate)
+ complex z,z0
+ real x(NS),yi(NS),yq(NS) !For complex polyfit
+ real pp(2*NSPS) !Shaped pulse for OQPSK
+ real aa(20),bb(20) !Fitted polyco's
+ integer id(NS+ND) !NRZ values (+/-1) for Sync and Data
+
+ ib=NSPS-1
+ ib2=N2-1
+ n=0
+ jz=(NS+ND+1)/2
+ do j=1,jz !First-pass demodulation
+ ia=ib+1
+ ib=ia+N2-1
+ zz(j)=sum(pp*c(ia:ib))/NSPS
+ if(abs(id(j)).eq.2) then !Save all sync symbols
+ n=n+1
+ x(n)=float(ia+ib)/NZ - 1.0
+ yi(n)=real(zz(j))*0.5*id(j)
+ yq(n)=aimag(zz(j))*0.5*id(j)
+! write(54,1225) n,x(n),yi(n),yq(n)
+!1225 format(i5,3f12.4)
+ endif
+ if(j.lt.jz) then
+ zz(j+jz)=sum(pp*c(ia+NSPS:ib+NSPS))/NSPS
+ endif
+ enddo
+
+ aa=0.
+ bb=0.
+ nterms=0
+ chisqa=0.
+ chisqb=0.
+ if(maxn.gt.0) then
+ npts=n
+ mode=0
+ nterms=maxn
+ call polyfit4(x,yi,yi,npts,nterms,mode,aa,chisqa)
+ call polyfit4(x,yq,yq,npts,nterms,mode,bb,chisqb)
+ endif
+
+ nhardsync=0
+ do j=1,205
+ if(abs(id(j)).ne.2) cycle
+ xx=j*2.0/205.0 - 1.0
+ yii=1.
+ yqq=0.
+ if(nterms.gt.0) then
+ yii=aa(1)
+ yqq=bb(1)
+ do i=2,nterms
+ yii=yii + aa(i)*xx**(i-1)
+ yqq=yqq + bb(i)*xx**(i-1)
+ enddo
+ endif
+ z0=cmplx(yii,yqq)
+ z=zz(j)*conjg(z0)
+ p=real(z)
+ if(p*id(j).lt.0) nhardsync=nhardsync+1
+ enddo
+
+ return
+end subroutine cpolyfitw
diff --git a/lib/fsk4hf/dbpsksim.f90 b/lib/fsk4hf/dbpsksim.f90
new file mode 100644
index 00000000..8b9ea12c
--- /dev/null
+++ b/lib/fsk4hf/dbpsksim.f90
@@ -0,0 +1,241 @@
+program dbpsksim
+
+ parameter (ND=121) !Data symbols: LDPC (120,60), r=1/2
+ parameter (NN=ND) !Total symbols (121)
+ parameter (NSPS=28800) !Samples per symbol at 12000 sps
+ parameter (NZ=NSPS*NN) !Samples in waveform (3484800)
+ parameter (NFFT1=65536,NH1=NFFT1/2)
+ parameter (NFFT2=128,NH2=NFFT2/2)
+
+ character*8 arg
+ complex c(0:NZ-1) !Complex waveform
+ complex c2(0:NFFT1-1) !Short spectra
+ complex cr(0:NZ-1)
+ complex ct(0:NZ-1)
+ complex cz(0:NFFT2-1)
+ complex z0,z,zp
+ real s(-NH1+1:NH1)
+ real s2(-NH2+1:NH2)
+ real xnoise(0:NZ-1) !Generated random noise
+ real ynoise(0:NZ-1) !Generated random noise
+ real rxdata(120),llr(120)
+ integer id(NN) !Encoded NRZ data (values +/-1)
+ integer id1(NN) !Recovered data (1st pass)
+ integer id2(NN) !Recovered data (2nd pass)
+! integer icw(NN)
+ integer*1 msgbits(60),decoded(60),codeword(120),apmask(120),cw(120)
+ data msgbits/0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,&
+ 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0,0,1,0,1,1,0,1,0/
+
+ nnn=0
+ nargs=iargc()
+ if(nargs.ne.6) then
+ print*,'Usage: dbpsksim f0(Hz) delay(ms) fspread(Hz) ndiff iters snr(dB)'
+ print*,'Example: dbpsksim 1500 0 0 10 -35'
+ print*,'Set snr=0 to cycle through a range'
+ go to 999
+ endif
+ call getarg(1,arg)
+ read(arg,*) f0 !Low tone frequency
+ call getarg(2,arg)
+ read(arg,*) delay
+ call getarg(3,arg)
+ read(arg,*) fspread
+ call getarg(4,arg)
+ read(arg,*) ndiff
+ call getarg(5,arg)
+ read(arg,*) iters
+ call getarg(6,arg)
+ read(arg,*) snrdb
+
+ twopi=8.d0*atan(1.d0)
+ fs=12000.d0
+ dt=1.0/fs
+ ts=NSPS*dt
+ baud=1.d0/ts
+ txt=NZ*dt
+ bandwidth_ratio=2500.0/6000.0
+ write(*,1000) baud,5*baud,txt,delay,fspread,ndiff
+1000 format('Baud:',f6.3,' BW:',f4.1,' TxT:',f6.1,' Delay:',f5.2, &
+ ' fSpread:',f5.2,' ndiff:',i2/)
+
+ write(*,1004)
+1004 format(' SNR err ber fer fsigma'/35('-'))
+
+ call encode120(msgbits,codeword) !Encode the test message
+ isna=-28
+ isnb=-40
+ if(snrdb.ne.0.0) then
+ isna=nint(snrdb)
+ isnb=isna
+ endif
+ do isnr=isna,isnb,-1
+ snrdb=isnr
+ sig=sqrt(bandwidth_ratio) * 10.0**(0.05*snrdb)
+ if(snrdb.gt.90.0) sig=1.0
+ nhard=0
+ nhardc=0
+ nfe1=0
+ nfe2=0
+ sqf=0.
+ do iter=1,iters
+ nnn=nnn+1
+ id(1)=1 !First bit is always 1
+ id(2:NN)=2*codeword-1
+ call genbpsk(id,f0,ndiff,0,c) !Generate the 4-FSK waveform
+ if(delay.ne.0.0 .or. fspread.ne.0.0) call watterson(c,delay,fspread)
+ c=sig*c !Scale to requested SNR
+ if(snrdb.lt.90) then
+ do i=0,NZ-1 !Generate gaussian noise
+ xnoise(i)=gran()
+ ynoise(i)=gran()
+ enddo
+ c=c + cmplx(xnoise,ynoise) !Add noise to signal
+ endif
+
+! First attempt at finding carrier frequency fc: 64k FFTs ==> avg power spectra
+ nspec=NZ/NFFT1
+ df1=12000.0/NFFT1
+ s=0.
+ do k=1,nspec
+ ia=(k-1)*NSPS
+ ib=ia+NSPS-1
+ c2(0:NSPS-1)=c(ia:ib)
+ c2(NSPS:)=0.
+ call four2a(c2,NFFT1,1,-1,1)
+ do i=0,NFFT1-1
+ j=i
+ if(j.gt.NH1) j=j-NFFT1
+ s(j)=s(j) + real(c2(i))**2 + aimag(c2(i))**2
+ enddo
+ enddo
+ s=1.e-6*s
+ smax=0.
+ ipk=0
+ ia=(1400.0)/df1
+ ib=(1600.0)/df1
+ do i=ia,ib
+ f=i*df1
+ if(s(i).gt.smax) then
+ smax=s(i)
+ ipk=i
+ fc=f
+ endif
+ enddo
+ a=(s(ipk+1)-s(ipk-1))/2.0
+ b=(s(ipk+1)+s(ipk-1)-2.0*s(ipk))/2.0
+ dx=-a/(2.0*b)
+ fc=fc + df1*dx !Estimated carrier frequency
+ sqf=sqf + (fc-f0)**2
+
+! The following is for testing SNR calibration:
+! sp5n=(s(ipk-2)+s(ipk-1)+s(ipk)+s(ipk+1)+s(ipk+2)) !Sig + 5*noise
+! base=(sum(s)-sp5n)/(NFFT1-5.0) !Noise per bin
+! psig=sp5n-5*base !Sig only
+! pnoise=(2500.0/df1)*base !Noise in 2500 Hz
+! xsnrdb=db(psig/pnoise)
+
+ call genbpsk(id,fc,ndiff,1,cr) !Generate reference carrier
+ c=c*conjg(cr) !Mix signal to baseband
+
+ z0=1.0
+ do j=1,NN !Demodulate
+ ia=(j-1)*NSPS
+ ib=ia+NSPS-1
+ z=sum(c(ia:ib))
+ cz(j-1)=z
+ zp=z*conjg(z0)
+ p=1.e-4*real(zp)
+ id1(j)=-1
+ if(p.ge.0.0) id1(j)=1
+ if(j.ge.2) rxdata(j-1)=p
+ z0=z
+ enddo
+
+ rxav=sum(rxdata)/120
+ rx2av=sum(rxdata*rxdata)/120
+ rxsig=sqrt(rx2av-rxav*rxav)
+ rxdata=rxdata/rxsig
+ ss=0.84
+ llr=2.0*rxdata/(ss*ss)
+ apmask=0
+ max_iterations=10
+ call bpdecode120(llr,apmask,max_iterations,decoded,niterations,cw)
+
+! Count frame errors
+ if(niterations.lt.0 .or. count(msgbits.ne.decoded).gt.0) nfe1=nfe1+1
+
+! Find carrier frequency from squared cz array.
+ cz(121:)=0.
+ cz=cz*cz
+ call four2a(cz,NFFT2,1,-1,1)
+ s2max=0.
+ do i=0,NFFT2-1
+ j=i
+ if(i.gt.NH2) j=j-NFFT2
+ s2(j)=real(cz(i))**2 + aimag(cz(i))**2
+ if(s2(j).gt.s2max) then
+ s2max=s2(j)
+ jpk=j
+ endif
+! write(16,1200) j*baud/NFFT2,1.e-12*s2(j)
+!1200 format(2f12.3)
+ enddo
+ a=(s2(jpk+1)-s2(jpk-1))/2.0
+ b=(s2(jpk+1)+s2(jpk-1)-2.0*s2(jpk))/2.0
+ dx=-a/(2.0*b)
+ fc2=0.5*(jpk+dx)*baud/NFFT2
+
+ call genbpsk(id,fc2,ndiff,1,cr) !Generate new ref carrier at fc2
+ c=c*conjg(cr)
+ z0=1.0
+ do j=1,NN !Demodulate
+ ia=(j-1)*NSPS
+ ib=ia+NSPS-1
+ z=sum(c(ia:ib))
+ if(j.eq.1) z0=z
+ zp=z*conjg(z0)
+ p=1.e-4*real(zp)
+ id2(j)=-1
+ if(p.ge.0.0) id2(j)=1
+ if(j.ge.2) rxdata(j-1)=p
+ ierr=0
+ if(id2(j).ne.id(j)) ierr=1
+ id3=-1
+ if(real(z).ge.0.0) id3=1
+ if(j.ge.2 .and. id3.ne.id(j)) nhardc=nhardc+1
+ if(j.ge.2 .and. ndiff.eq.0) rxdata(j-1)=real(z)
+ z0=z
+ enddo
+ nhard=nhard + count(id2.ne.id) !Count hard errors
+
+ rxav=sum(rxdata)/120
+ rx2av=sum(rxdata*rxdata)/120
+ rxsig=sqrt(rx2av-rxav*rxav)
+ rxdata=rxdata/rxsig
+ ss=0.84
+ llr=2.0*rxdata/(ss*ss) !Soft symbols
+ apmask=0
+ max_iterations=10
+ decoded=0
+ call bpdecode120(llr,apmask,max_iterations,decoded,niterations,cw)
+! if(niterations.lt.0) then
+! llr=-llr
+! call bpdecode120(llr,apmask,max_iterations,decoded,niterations,cw)
+! if(niterations.ge.0) nhard=NN*iters-nhard
+! endif
+ if(niterations.ge.0) call chkcrc10(decoded,nbadcrc)
+ if(niterations.lt.0 .or. count(msgbits.ne.decoded).gt.0 .or. &
+ nbadcrc.ne.0) nfe2=nfe2+1
+ enddo
+
+ if(ndiff.eq.0) nhard=nhardc
+ fsigma=sqrt(sqf/iters)
+ ber=float(nhard)/(NN*iters)
+ fer=float(nfe2)/iters
+ write(*,1050) snrdb,nhard,ber,fer,fsigma
+ write(14,1050) snrdb,nhard,ber,fer,fsigma
+1050 format(f6.1,i5,f8.4,f7.3,f8.2)
+ enddo
+
+999 end program dbpsksim
diff --git a/lib/fsk4hf/dopspread.f90 b/lib/fsk4hf/dopspread.f90
new file mode 100644
index 00000000..cadfbecc
--- /dev/null
+++ b/lib/fsk4hf/dopspread.f90
@@ -0,0 +1,62 @@
+subroutine dopspread(c,fspread)
+
+ parameter (NFFT=268800,NH=NFFT/2)
+ complex c(0:NFFT-1)
+ complex cspread(0:NFFT-1)
+
+ df=12000.0/nfft
+ twopi=8*atan(1.0)
+ cspread(0)=1.0
+ cspread(NH)=0.
+ b=6.0 !Lorenzian 3/28 onward
+ do i=1,NH
+ f=i*df
+ x=b*f/fspread
+ z=0.
+ a=0.
+ if(x.lt.3.0) then !Cutoff beyond x=3
+ a=sqrt(1.111/(1.0+x*x)-0.1) !Lorentzian
+ call random_number(r1)
+ phi1=twopi*r1
+ z=a*cmplx(cos(phi1),sin(phi1))
+ endif
+ cspread(i)=z
+ z=0.
+ if(x.lt.50.0) then
+ call random_number(r2)
+ phi2=twopi*r2
+ z=a*cmplx(cos(phi2),sin(phi2))
+ endif
+ cspread(NFFT-i)=z
+ enddo
+
+ izh=fspread/df
+ do i=-izh,izh
+ f=i*df
+ j=i
+ if(j.lt.0) j=j+nfft
+ s=real(cspread(j))**2 + aimag(cspread(j))**2
+! write(23,3000) f,s,cspread(j)
+!3000 format(f10.3,3f12.6)
+ enddo
+
+ call four2a(cspread,NFFT,1,1,1) !Transform to time domain
+
+ sum=0.
+ do i=0,NFFT-1
+ p=real(cspread(i))**2 + aimag(cspread(i))**2
+ sum=sum+p
+ enddo
+ avep=sum/NFFT
+ fac=sqrt(1.0/avep)
+ cspread=fac*cspread !Normalize to constant avg power
+ c=cspread*c !Apply Rayleigh fading to c()
+
+ do i=0,NFFT-1
+ p=real(cspread(i))**2 + aimag(cspread(i))**2
+! write(24,3010) i,p,cspread(i)
+!3010 format(i8,3f12.6)
+ enddo
+
+ return
+end subroutine dopspread
diff --git a/lib/fsk4hf/encode120.f90 b/lib/fsk4hf/encode120.f90
new file mode 100644
index 00000000..577bbe86
--- /dev/null
+++ b/lib/fsk4hf/encode120.f90
@@ -0,0 +1,116 @@
+subroutine encode120(message,codeword)
+! Encode an 60-bit message and return a 120-bit codeword.
+! The generator matrix has dimensions (60,60).
+! The code is a (120,60) regular ldpc code with column weight 3.
+! The code was generated using the PEG algorithm.
+! After creating the codeword, the columns are re-ordered according to
+! "colorder" to make the codeword compatible with the parity-check matrix
+!
+character*15 g(60)
+integer*1 codeword(120)
+integer colorder(120)
+integer*1 gen(60,60)
+integer*1 itmp(120)
+integer*1 message(60)
+integer*1 pchecks(60)
+logical first
+data first/.true./
+data g/ &
+ "65541ad98feab6e",&
+ "27249940a5895a3",&
+ "c80eac7506bf794",&
+ "aa50393e3e18d3f",&
+ "28527e87d47dced",&
+ "5da0dcaf8db048c",&
+ "d6509a43ca9b01a",&
+ "9a7dadd9c94f1d4",&
+ "bb673d3ba07cf29",&
+ "65e190f2fbed447",&
+ "bc2062a4e520969",&
+ "9e357f3feed059b",&
+ "aa6b59212036a57",&
+ "f78a326722d6565",&
+ "416754bc34c6405",&
+ "f77000b3f04ff67",&
+ "d48fbd7d48c5ab9",&
+ "031ffb5db3a70cb",&
+ "125964e358c4df5",&
+ "bd02c32a5a241ea",&
+ "4c15ecdd8561abd",&
+ "7f0f1b352c7413e",&
+ "26edb94dfd0ae79",&
+ "ca1ba1ee0f8fb24",&
+ "49878a58cb4544c",&
+ "3dbcd0ff821b203",&
+ "c1f4440160d5345",&
+ "b5ea9dc7a5a70ab",&
+ "cebcf7d94976be4",&
+ "0968265f5977c88",&
+ "c5a36937faa78c3",&
+ "f0d4fef11e01c10",&
+ "e35fc0c779bebfe",&
+ "cf49c3eb41a31d5",&
+ "3f0b19352c7013e",&
+ "0e15eccd8521abd",&
+ "dda8dcaf9d3048c",&
+ "fee31438fba59ed",&
+ "ad74a27e939189c",&
+ "736ac01b439106e",&
+ "ab5d2729b29bfa1",&
+ "edf11fb02e5a426",&
+ "5f38be1c93ecc83",&
+ "1e4b3b8dc516b3e",&
+ "84443d8bee614c6",&
+ "d854d9f355ceac4",&
+ "a476b5ece51f0ea",&
+ "831c2b36c4c2f68",&
+ "f485c97a91615ae",&
+ "e9376d828ade9ba",&
+ "cac586f089d3185",&
+ "b8f8c67613dafe2",&
+ "1a3142b401b315d",&
+ "87dbedc43265d2e",&
+ "bb64ec6e652e7da",&
+ "e71bfd4c95dfd38",&
+ "31209af07ad4f75",&
+ "cff1a8ccc5f4978",&
+ "742eded1e1dfefd",&
+ "1cd7154a904dac4"/
+
+data colorder/ &
+ 0,1,2,21,3,4,5,6,7,8,20,10,9,11,12,23,13,28,14,31, &
+ 15,16,22,26,17,30,18,29,25,32,41,34,19,33,27,36,38,43,42,24, &
+ 37,39,45,40,35,44,47,46,50,51,53,48,52,56,54,57,55,49,58,61, &
+ 60,59,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, &
+ 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99, &
+ 100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119/
+
+save first,gen
+
+if( first ) then ! fill the generator matrix
+ gen=0
+ do i=1,60
+ do j=1,15
+ read(g(i)(j:j),"(Z1)") istr
+ do jj=1, 4
+ icol=(j-1)*4+jj
+ if( btest(istr,4-jj) ) gen(i,icol)=1
+ enddo
+ enddo
+ enddo
+first=.false.
+endif
+
+do i=1, 60
+ nsum=0
+ do j=1, 60
+ nsum=nsum+message(j)*gen(i,j)
+ enddo
+ pchecks(i)=mod(nsum,2)
+enddo
+itmp(1:60)=pchecks
+itmp(61:120)=message(1:60)
+codeword(colorder+1)=itmp(1:120)
+
+return
+end subroutine encode120
diff --git a/lib/fsk4hf/encode168.f90 b/lib/fsk4hf/encode168.f90
new file mode 100644
index 00000000..bf121215
--- /dev/null
+++ b/lib/fsk4hf/encode168.f90
@@ -0,0 +1,141 @@
+subroutine encode168(message,codeword)
+! Encode an 84-bit message and return a 168-bit codeword.
+! The generator matrix has dimensions (84,84).
+! The code is a (168,84) regular ldpc code with column weight 3.
+! The code was generated using the PEG algorithm.
+! After creating the codeword, the columns are re-ordered according to
+! "colorder" to make the codeword compatible with the parity-check matrix
+!
+character*21 g(84)
+integer*1 codeword(168)
+integer colorder(168)
+integer*1 gen(84,168)
+integer*1 itmp(168)
+integer*1 message(84)
+integer*1 pchecks(84)
+logical first
+data first/.true./
+data g/ & !parity generator matrix for (168,84) code
+ "25c5bf31ef6710fde9a5a", &
+ "18038ef7899cd97a77d96", &
+ "270dde504dad076c02b1f", &
+ "ed37fe12616565bd7d500", &
+ "12b99aa49b5367aff3838", &
+ "41cc27f2fac8b228aac21", &
+ "2265b233a3cff0b9cee24", &
+ "292760cd4f7f4a526a2f1", &
+ "2b3db4c8bd831911680cc", &
+ "cef2b24ce203bdc60b266", &
+ "5045a24f9340915d807ab", &
+ "3592b7fc60ba85139502e", &
+ "9318023145637bd798f0e", &
+ "ad796023c3d58d1e6509c", &
+ "3da5eab57f040e75d7413", &
+ "27466d1d2734d0ff64830", &
+ "2ed50bb1ce313bbfb1ab0", &
+ "9a616bda01b25b7e6eeaf", &
+ "a84c8c1e9df103169d10d", &
+ "a40da29b4aca9234a8942", &
+ "dd258d02d79a5f209d3d0", &
+ "bdfdc06713511997b5621", &
+ "25c58f12f4096cd8ead1a", &
+ "b2638a478f21e10fe97de", &
+ "4051020f43c605d458156", &
+ "f651aad14322a526dae35", &
+ "a1c147e31bcc9d87330bf", &
+ "7524b53d996d48284647b", &
+ "a72e7d25ce31b27282e56", &
+ "a97f53b019022350b7519", &
+ "56106c6340c0810790984", &
+ "c63b8e03a57208635992b", &
+ "43a3de2aa3a2b1afb65dc", &
+ "9baa64847ead03b77fecc", &
+ "251cbd1895c8839c46b0d", &
+ "2858107dde2d173e13530", &
+ "20096f6a870f636b704e7", &
+ "7f833ccbceec52dd6eb79", &
+ "a9108dd77b8015b75242a", &
+ "689666a79e5579c916236", &
+ "aa5dff46459787f69911f", &
+ "794558c13138d08171089", &
+ "c937042857b291cee8dfd", &
+ "6f0bf3248bb9a231366b8", &
+ "1c09e756ef1656c96f2d2", &
+ "073b875b6774e71fba549", &
+ "f7d840aafc037febd2d5c", &
+ "dcc0e7d0da5fe17c99ad3", &
+ "98238ef7819cd97a77d94", &
+ "177c2594743477421a262", &
+ "7d01a833c19374fbaaa6e", &
+ "7bb800216660482ffd1c4", &
+ "39a92e2dba0d4cfda98d2", &
+ "44b8d88622698816456a8", &
+ "791db2334d6d86639229b", &
+ "ba6004b086bd38559ea48", &
+ "f94558e13138d18170089", &
+ "08ba145302cfbed7845ae", &
+ "fb8e64b6da3602168ed38", &
+ "1045a2cf1340915d8072b", &
+ "7592b6fc64ba85139582e", &
+ "3eb238a11bc6654452bae", &
+ "b69d8d23b1ea170f70214", &
+ "0123dfae84fb20462a614", &
+ "4131066ad52a339b3c0d7", &
+ "fd2cc26850951c43ed737", &
+ "a644d4eb7e56c40f0d050", &
+ "0c3bd9d5dab7c9ee2c8fc", &
+ "4a198b37af56d7ceffb56", &
+ "b6e946c429294cf0eed8b", &
+ "98384d75e758774f5ff3b", &
+ "5c58e5d9a4d0531d37384", &
+ "7a0af02719afed521fd06", &
+ "8cd5b2e694e7854abbc70", &
+ "1a2f061912d0ea19702d3", &
+ "6ffbce557d8fa691a50e8", &
+ "d43438e2e2ed5d9f14011", &
+ "8d502106083b809adba00", &
+ "67e22f9b9983aa715964d", &
+ "b31f3a3f3c1f406b1fd58", &
+ "529f60ac291f827d97331", &
+ "476a815424f2e2cbe641f", &
+ "81c82c89bcc3feec42458", &
+ "2c882d0e281b178e80364"/
+
+data colorder/0,1,2,3,28,4,5,6,7,8,9,10,11,34,12,32,13,14,15,16,17, &
+ 18,36,29,42,31,20,21,41,40,30,38,22,19,47,37,46,35,44,33,49,24, &
+ 43,51,25,26,27,50,52,57,69,54,55,45,59,58,56,61,60,53,48,23,62, &
+ 63,64,67,66,65,68,39,70,71,72,74,73,75,76,77,80,81,78,82,79,83, &
+ 84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104, &
+ 105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, &
+ 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146, &
+ 147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167/
+
+save first,gen
+
+if( first ) then ! fill the generator matrix
+ gen=0
+ do i=1,84
+ do j=1,21
+ read(g(i)(j:j),"(Z1)") istr
+ do jj=1, 4
+ icol=(j-1)*4+jj
+ if( btest(istr,4-jj) ) gen(i,icol)=1
+ enddo
+ enddo
+ enddo
+first=.false.
+endif
+
+do i=1, 84
+ nsum=0
+ do j=1, 84
+ nsum=nsum+message(j)*gen(i,j)
+ enddo
+ pchecks(i)=mod(nsum,2)
+enddo
+itmp(1:84)=pchecks
+itmp(85:168)=message(1:84)
+codeword(colorder+1)=itmp(1:168)
+
+return
+end subroutine encode168
diff --git a/lib/fsk4hf/encode174.f90 b/lib/fsk4hf/encode174.f90
new file mode 100644
index 00000000..8ef463e8
--- /dev/null
+++ b/lib/fsk4hf/encode174.f90
@@ -0,0 +1,50 @@
+subroutine encode174(message,codeword)
+! Encode an 101-bit message and return a 174-bit codeword.
+! The generator matrix has dimensions (73,101).
+! The code is a (174,101) regular ldpc code with column weight 3.
+! The code was generated using the PEG algorithm.
+! After creating the codeword, the columns are re-ordered according to
+! "colorder" to make the codeword compatible with the parity-check matrix
+!
+
+include "ldpc_174_87_params.f90"
+
+integer*1 codeword(N)
+integer*1 gen(M,K)
+integer*1 itmp(N)
+integer*1 message(K)
+integer*1 pchecks(M)
+logical first
+data first/.true./
+
+save first,gen
+
+if( first ) then ! fill the generator matrix
+ gen=0
+ do i=1,M
+ do j=1,11
+ read(g(i)( (j-1)*2+1:(j-1)*2+2 ),"(Z2)") istr
+ do jj=1, 8
+ icol=(j-1)*8+jj
+ if( icol .le. 87 ) then
+ if( btest(istr,8-jj) ) gen(i,icol)=1
+ endif
+ enddo
+ enddo
+ enddo
+first=.false.
+endif
+
+do i=1,M
+ nsum=0
+ do j=1,K
+ nsum=nsum+message(j)*gen(i,j)
+ enddo
+ pchecks(i)=mod(nsum,2)
+enddo
+itmp(1:M)=pchecks
+itmp(M+1:N)=message(1:K)
+codeword(colorder+1)=itmp(1:N)
+
+return
+end subroutine encode174
diff --git a/lib/fsk4hf/encode300.f90 b/lib/fsk4hf/encode300.f90
new file mode 100644
index 00000000..f1d3bbae
--- /dev/null
+++ b/lib/fsk4hf/encode300.f90
@@ -0,0 +1,308 @@
+subroutine encode300(message,codeword)
+! Encode an 60-bit message and return a 300-bit codeword.
+! The generator matrix has dimensions (240,60).
+! The code is a (300,60) irregular ldpc code with column weights:
+! 52% column weight 2
+! 27% column weight 3
+! 21% column weight 7
+! The code was generated using the PEG algorithm.
+! After creating the codeword, the columns are re-ordered according to
+! "colorder" to make the codeword compatible with the parity-check matrix
+!
+character*15 g(240)
+integer*1 codeword(300)
+integer colorder(300)
+integer*1 gen(240,60)
+integer*1 itmp(300)
+integer*1 message(60)
+integer*1 pchecks(240)
+logical first
+data first/.true./
+data g/ &
+ "316fd3bb18bcefd", &
+ "a9c1c984f91244e", &
+ "9e04bd3d5d78d89", &
+ "f81617089621bd4", &
+ "12997ce2f44dbf4", &
+ "3ebddaf9b0fa1fc", &
+ "d0c114b0b0ef162", &
+ "f8c4f115f98bd92", &
+ "d0a79c0c5b8ca19", &
+ "477f6712f357b3b", &
+ "fa28b2444a7e66b", &
+ "bedcd4df8d95c64", &
+ "da30de73e57022c", &
+ "bc099bbb90fe09e", &
+ "cffc1e47e5708e8", &
+ "713d808563ca9a3", &
+ "70fcf1741d5d5d7", &
+ "32e80bc15112008", &
+ "804cef4df9b18ec", &
+ "3736881819d1033", &
+ "f4e37db7f9c5efe", &
+ "9e84b93d4d78d09", &
+ "2250c3518ec830a", &
+ "55a529a92e18021", &
+ "1cb80b14c9f6eae", &
+ "80c504b031ef926", &
+ "ece6636d0ac9c6d", &
+ "5d50a1690782cd0", &
+ "3d54a1fb30937a2", &
+ "ba8fe8006318041", &
+ "02917ce2fc45bf4", &
+ "abc1d984f95a44e", &
+ "fc05b4c4ab2d850", &
+ "467f7718f357b3b", &
+ "472cc094546c6b2", &
+ "fcdd94cf8c9cc64", &
+ "4dbc1647e970cc8", &
+ "6caa465c442aed1", &
+ "aead5af8b0da1be", &
+ "d8e1fa45a2e8431", &
+ "9d4dc4cc63abb7f", &
+ "9b2df6b48264637", &
+ "7335808563ca3a3", &
+ "36bf8d5cd93e6cc", &
+ "004ccf4db9b08ec", &
+ "90a71c8c598ca19", &
+ "f8c5d115f90bc92", &
+ "b95546c4e3f7934", &
+ "7d50a1690786cd0", &
+ "c90939921a0d7c6", &
+ "d0c504b030ef126", &
+ "ce3e6f9396fc542", &
+ "a0072a59f3707f5", &
+ "532d0a8fe3da1ea", &
+ "68b9e5cd7d142db", &
+ "fedc94df8c9dc64", &
+ "6da2465c448aed0", &
+ "3574aa19cb273c0", &
+ "1e54768c6bc6843", &
+ "691f65654498186", &
+ "fe2c92444a6ef6b", &
+ "9caad933e038cc4", &
+ "ad4e6f4defb28ec", &
+ "4f3d80947c6d2b2", &
+ "1caad933e0b8cc4", &
+ "b14fd3bf18bcafd", &
+ "ad091bbbb0f809e", &
+ "90b71c8c598da19", &
+ "f8c4d115f90bd92", &
+ "9d4dcccc63afb7f", &
+ "fa2c92444a6e76b", &
+ "1e14768c6bc6c43", &
+ "d1baf5aacb86087", &
+ "bdf762b92ee51c7", &
+ "caacec06ad8a90c", &
+ "804ccf4df9b08ec", &
+ "69e969f9da5cbd8", &
+ "814ccf4df9b086c", &
+ "cebe4f9796f4542", &
+ "491f65654499186", &
+ "8fbf5b9796f6d2a", &
+ "ce3e4f9396f4542", &
+ "47558560e7debc3", &
+ "94aadd33e038cc4", &
+ "a94eef4debb286e", &
+ "d8e5d115f91bcd2", &
+ "532d488fe3da0ab", &
+ "664e7bc4e23a80c", &
+ "94a2dd33a038cd4", &
+ "d8c5d115f91bc92", &
+ "0fef071eee60bd5", &
+ "9a89a09163c2b97", &
+ "0eaf071e6c60bd5", &
+ "bc0d1bbbb0fe0be", &
+ "f9babd3d12d0f31", &
+ "69a969f9da5c9d8", &
+ "6e4e7bc4e23a82c", &
+ "b0042659f3227f5", &
+ "2d51418f0f28347", &
+ "be0d5bbbb0da0be", &
+ "225003508ec8302", &
+ "8fbf4b9796f4d2a", &
+ "bead5af9b0da1be", &
+ "6ca2465c440aed1", &
+ "4fbc1e47ed708c8", &
+ "bd091bbbb0fc09e", &
+ "b0062259f3307f5", &
+ "a8072a59f3727f5", &
+ "a0062259f3707f5", &
+ "3c380b14c974eae", &
+ "30042659f3226f5", &
+ "48b9e4cd7d142db", &
+ "728bcd4b38308fb", &
+ "c0c504b031ef126", &
+ "314fd3bb18bcafd", &
+ "1c29148305faec1", &
+ "44c92a9c28ada63", &
+ "88e99b370aae32b", &
+ "695081690386ad8", &
+ "572d0a8de3da1ea", &
+ "467f6610f357b2b", &
+ "733d008563da1a3", &
+ "d1baf4aacb84087", &
+ "4315551d71c8ff0", &
+ "48bde4cd7d140db", &
+ "3ebd58f9b0da9fc", &
+ "51baf4aacb84083", &
+ "814e4f4de9b082c", &
+ "814ecf4de9b086c", &
+ "be0d1bbbb0fa0be", &
+ "4f7580947c792b3", &
+ "cdf2dce48c39c3b", &
+ "d8c5c115f91bc12", &
+ "a94e6f4debb28ee", &
+ "be2d5afbb0da1be", &
+ "cdd6dce48439c2b", &
+ "bebd5af9b0da1fe", &
+ "fa2892444a6e66b", &
+ "51bbf4aacb8c083", &
+ "baa73d81eebcd83", &
+ "79a2ce47f138cc9", &
+ "cc28cf198e6dbd4", &
+ "fcde94dfcc9cc64", &
+ "1016fcf59286717", &
+ "12917ce2fc4dbf4", &
+ "4fbc1647e9708c8", &
+ "3e382b1cc974fae", &
+ "d5bafdaad386087", &
+ "0fef473eee60bd5", &
+ "c0e504b031ee126", &
+ "8bbf5b9797f6d2a", &
+ "0eef071e6e60bd5", &
+ "1806fcf59386517", &
+ "fcdc94df8c9cc64", &
+ "141eca2bfa25656", &
+ "5fbc1767e9708e8", &
+ "5aa4c7803a6bdf1", &
+ "b14bd3b718bcafd", &
+ "3ebd5af9b0da1fc", &
+ "d0a7148c5b8ca09", &
+ "a94ecf4debb086e", &
+ "733d808563ca1a3", &
+ "fd9abd1d92d0f31", &
+ "bc091bbbb0fe09e", &
+ "d0c514b0b0ef122", &
+ "4f7d80947c7d2b3", &
+ "8b3f5b97b7f6d2a", &
+ "4fbc1767e9708c8", &
+ "cebf4f9796f4502", &
+ "9c76c880a864e67", &
+ "abc1c984f95244e", &
+ "795081690786ad8", &
+ "467f6710f357b3b", &
+ "1c380b14c9f4eae", &
+ "d5baf5aac386087", &
+ "bedc94df8c95c64", &
+ "553d0a8de2da1fa", &
+ "0315551d71d8ff0", &
+ "1c1eca2ffa25656", &
+ "d4bafdaad3c6087", &
+ "be2d5bfbb0da0be", &
+ "b0062659f3207f5", &
+ "5ffc1765e9708e8", &
+ "8d62e8bcd303e33", &
+ "cc08cf198e69bd4", &
+ "573d0a8de3da1fa", &
+ "cd56dce48639c2b", &
+ "472dc094546c2b2", &
+ "7950a16907868d8", &
+ "7283cf4b38308fb", &
+ "894ecf4de9b086e", &
+ "0f7580b47c792b3", &
+ "cfbf4b9796f4d0a", &
+ "3e380b14c974fae", &
+ "732d0085e3da1a3", &
+ "1816fcf59386717", &
+ "532d088fe3da1ab", &
+ "1c300b94c9fcaae", &
+ "d0a71c8c5b8ca19", &
+ "9e84bd3d5d78d09", &
+ "225083508ec830a", &
+ "f99abd1d12d0f31", &
+ "35f4aa19cb673c0", &
+ "cdd2dce48c39c2b", &
+ "0f7780b47c792bf", &
+ "0e33a5f114f5730", &
+ "bc05b4c4ab0d850", &
+ "1c300b14c9f4aae", &
+ "cfbc1e47ed708e8", &
+ "0f7180b47c392b3", &
+ "d8c7c115f91be12", &
+ "c09148adfa94e97", &
+ "9c66c880a844e67", &
+ "2226c13b73519f8", &
+ "cebf4b9796f4d02", &
+ "c0e706b031ee126", &
+ "6a6629715e53ce3", &
+ "73f9aa824e7d0b8", &
+ "473d80947c6c2b2", &
+ "1df140e0ddb5632", &
+ "473dc0945c6c2b2", &
+ "81b4d95f671971d", &
+ "663945ca758e2b6", &
+ "02ec3d98a2306fd", &
+ "5dadb0fa1275690", &
+ "4bb8aaa854948d0", &
+ "8359ba40886971c", &
+ "49cc3d2a2be2ee0", &
+ "bfdf13af137f318", &
+ "a1de773a2b1ff04", &
+ "8ff3945a2f465c7", &
+ "532d0087e3da1a3", &
+ "f3eaf7fa454d385", &
+ "a606aa5aeba07d9", &
+ "67f0627b0af8a53", &
+ "56698bed69d1c2c", &
+ "d5f420011fbf924", &
+ "2a8f86c810e2c62", &
+ "43cc1cf1208c206", &
+ "ee784c4900258de"/
+
+data colorder/ &
+0,1,2,3,4,5,6,7,8,9,10,11,123,12,13,14,15,16,17,18, &
+19,20,21,22,23,24,25,138,26,145,27,28,29,30,31,32,33,34,35,36, &
+37,154,38,39,40,41,42,43,44,144,46,47,48,49,50,51,52,53,143,54, &
+125,56,57,58,124,59,120,140,157,160,55,60,61,62,156,162,141,64,65,153, &
+181,183,66,170,67,68,69,130,70,164,71,72,73,74,75,63,76,77,135,78, &
+79,80,176,169,82,83,84,167,180,85,136,158,129,166,175,142,134,146,121,165, &
+88,89,192,90,45,91,92,93,182,189,94,95,96,173,81,97,98,178,122,126, &
+132,99,100,152,186,193,101,102,151,103,104,172,159,168,150,190,147,148,201,107, &
+205,177,108,198,197,174,127,109,185,110,202,87,199,171,179,187,139,137,106,131, &
+206,194,112,149,155,113,128,184,196,86,114,203,212,195,208,105,188,161,163,191, &
+200,209,214,204,115,218,133,111,207,117,213,216,211,217,116,215,219,220,210,221, &
+118,222,223,225,224,228,226,229,231,227,233,119,234,235,232,230,237,239,236,238, &
+240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259, &
+260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279, &
+280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299/
+
+save first,gen
+
+if( first ) then ! fill the generator matrix
+ gen=0
+ do i=1,240
+ do j=1,15
+ read(g(i)(j:j),"(Z1)") istr
+ do jj=1, 4
+ icol=(j-1)*4+jj
+ if( btest(istr,4-jj) ) gen(i,icol)=1
+ enddo
+ enddo
+ enddo
+first=.false.
+endif
+
+do i=1, 240
+ nsum=0
+ do j=1, 60
+ nsum=nsum+message(j)*gen(i,j)
+ enddo
+ pchecks(i)=mod(nsum,2)
+enddo
+itmp(1:240)=pchecks
+itmp(241:300)=message(1:60)
+codeword(colorder+1)=itmp(1:300)
+
+return
+end subroutine encode300
diff --git a/lib/fsk4hf/extractmessage168.f90 b/lib/fsk4hf/extractmessage168.f90
new file mode 100644
index 00000000..466d2a63
--- /dev/null
+++ b/lib/fsk4hf/extractmessage168.f90
@@ -0,0 +1,48 @@
+subroutine extractmessage168(decoded,msgreceived,ncrcflag,recent_calls,nrecent)
+ use iso_c_binding, only: c_loc,c_size_t
+ use crc
+ use packjt
+
+ character*22 msgreceived
+ character*12 call1,call2
+ character*12 recent_calls(nrecent)
+ integer*1 decoded(84)
+ integer*1, target:: i1Dec8BitBytes(11)
+ integer*4 i4Dec6BitWords(12)
+
+! Collapse 84 decoded bits to 11 bytes. Bytes 1-9 are the message, byte 10 and first half of byte 11 is the crc
+ do ibyte=1,9
+ itmp=0
+ do ibit=1,8
+ itmp=ishft(itmp,1)+iand(1,decoded((ibyte-1)*8+ibit))
+ enddo
+ i1Dec8BitBytes(ibyte)=itmp
+ enddo
+! Need to pack the crc into bytes 10 and 11 for crc12_check
+ i1Dec8BitBytes(10)=decoded(73)*8+decoded(74)*4+decoded(75)*2+decoded(76)
+ i1Dec8BitBytes(11)=decoded(77)*128+decoded(78)*64+decoded(79)*2*32+decoded(80)*16
+ i1Dec8BitBytes(11)=i1Dec8BitBytes(11)+decoded(81)*8+decoded(82)*4+decoded(83)*2+decoded(84)
+
+ if( crc12_check(c_loc (i1Dec8BitBytes), 11) ) then
+! CRC12 checks out --- 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 unpackmsg144(i4Dec6BitWords,msgreceived,call1,call2)
+ ncrcflag=1
+ if( call1(1:2) .ne. 'CQ' .and. call1(1:2) .ne. ' ' ) then
+ call update_recent_calls(call1,recent_calls,nrecent)
+ endif
+ if( call2(1:2) .ne. ' ' ) then
+ call update_recent_calls(call2,recent_calls,nrecent)
+ endif
+ else
+ msgreceived=' '
+ ncrcflag=-1
+ endif
+ return
+ end subroutine extractmessage168
diff --git a/lib/fsk4hf/extractmessage174.f90 b/lib/fsk4hf/extractmessage174.f90
new file mode 100644
index 00000000..ec4543d4
--- /dev/null
+++ b/lib/fsk4hf/extractmessage174.f90
@@ -0,0 +1,48 @@
+subroutine extractmessage174(decoded,msgreceived,ncrcflag,recent_calls,nrecent)
+ use iso_c_binding, only: c_loc,c_size_t
+ use crc
+ use packjt
+
+ character*22 msgreceived
+ character*12 call1,call2
+ character*12 recent_calls(nrecent)
+ character*87 cbits
+ integer*1 decoded(87)
+ integer*1, target:: i1Dec8BitBytes(11)
+ integer*4 i4Dec6BitWords(12)
+
+! Write decoded bits into cbits: 75-bit message plus 12-bit CRC
+ write(cbits,1000) decoded
+1000 format(87i1)
+ read(cbits,1001) i1Dec8BitBytes
+1001 format(11b8)
+ read(cbits,1002) ncrc12 !Received CRC12
+1002 format(75x,b12)
+
+ i1Dec8BitBytes(10)=iand(i1Dec8BitBytes(10),128+64+32)
+ i1Dec8BitBytes(11)=0
+ icrc12=crc12(c_loc(i1Dec8BitBytes),11) !CRC12 computed from 75 msg bits
+
+ if(ncrc12.eq.icrc12) then
+! CRC12 checks out --- 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 unpackmsg144(i4Dec6BitWords,msgreceived,call1,call2)
+ ncrcflag=1
+ if( call1(1:2) .ne. 'CQ' .and. call1(1:2) .ne. ' ' ) then
+ call update_recent_calls(call1,recent_calls,nrecent)
+ endif
+ if( call2(1:2) .ne. ' ' ) then
+ call update_recent_calls(call2,recent_calls,nrecent)
+ endif
+ else
+ msgreceived=' '
+ ncrcflag=-1
+ endif
+ return
+ end subroutine extractmessage174
diff --git a/lib/fsk4hf/fftw3.f90 b/lib/fsk4hf/fftw3.f90
new file mode 100644
index 00000000..440ccc28
--- /dev/null
+++ b/lib/fsk4hf/fftw3.f90
@@ -0,0 +1,64 @@
+ INTEGER FFTW_R2HC
+ PARAMETER (FFTW_R2HC=0)
+ INTEGER FFTW_HC2R
+ PARAMETER (FFTW_HC2R=1)
+ INTEGER FFTW_DHT
+ PARAMETER (FFTW_DHT=2)
+ INTEGER FFTW_REDFT00
+ PARAMETER (FFTW_REDFT00=3)
+ INTEGER FFTW_REDFT01
+ PARAMETER (FFTW_REDFT01=4)
+ INTEGER FFTW_REDFT10
+ PARAMETER (FFTW_REDFT10=5)
+ INTEGER FFTW_REDFT11
+ PARAMETER (FFTW_REDFT11=6)
+ INTEGER FFTW_RODFT00
+ PARAMETER (FFTW_RODFT00=7)
+ INTEGER FFTW_RODFT01
+ PARAMETER (FFTW_RODFT01=8)
+ INTEGER FFTW_RODFT10
+ PARAMETER (FFTW_RODFT10=9)
+ INTEGER FFTW_RODFT11
+ PARAMETER (FFTW_RODFT11=10)
+ INTEGER FFTW_FORWARD
+ PARAMETER (FFTW_FORWARD=-1)
+ INTEGER FFTW_BACKWARD
+ PARAMETER (FFTW_BACKWARD=+1)
+ INTEGER FFTW_MEASURE
+ PARAMETER (FFTW_MEASURE=0)
+ INTEGER FFTW_DESTROY_INPUT
+ PARAMETER (FFTW_DESTROY_INPUT=1)
+ INTEGER FFTW_UNALIGNED
+ PARAMETER (FFTW_UNALIGNED=2)
+ INTEGER FFTW_CONSERVE_MEMORY
+ PARAMETER (FFTW_CONSERVE_MEMORY=4)
+ INTEGER FFTW_EXHAUSTIVE
+ PARAMETER (FFTW_EXHAUSTIVE=8)
+ INTEGER FFTW_PRESERVE_INPUT
+ PARAMETER (FFTW_PRESERVE_INPUT=16)
+ INTEGER FFTW_PATIENT
+ PARAMETER (FFTW_PATIENT=32)
+ INTEGER FFTW_ESTIMATE
+ PARAMETER (FFTW_ESTIMATE=64)
+ INTEGER FFTW_ESTIMATE_PATIENT
+ PARAMETER (FFTW_ESTIMATE_PATIENT=128)
+ INTEGER FFTW_BELIEVE_PCOST
+ PARAMETER (FFTW_BELIEVE_PCOST=256)
+ INTEGER FFTW_DFT_R2HC_ICKY
+ PARAMETER (FFTW_DFT_R2HC_ICKY=512)
+ INTEGER FFTW_NONTHREADED_ICKY
+ PARAMETER (FFTW_NONTHREADED_ICKY=1024)
+ INTEGER FFTW_NO_BUFFERING
+ PARAMETER (FFTW_NO_BUFFERING=2048)
+ INTEGER FFTW_NO_INDIRECT_OP
+ PARAMETER (FFTW_NO_INDIRECT_OP=4096)
+ INTEGER FFTW_ALLOW_LARGE_GENERIC
+ PARAMETER (FFTW_ALLOW_LARGE_GENERIC=8192)
+ INTEGER FFTW_NO_RANK_SPLITS
+ PARAMETER (FFTW_NO_RANK_SPLITS=16384)
+ INTEGER FFTW_NO_VRANK_SPLITS
+ PARAMETER (FFTW_NO_VRANK_SPLITS=32768)
+ INTEGER FFTW_NO_VRECURSE
+ PARAMETER (FFTW_NO_VRECURSE=65536)
+ INTEGER FFTW_NO_SIMD
+ PARAMETER (FFTW_NO_SIMD=131072)
diff --git a/lib/fsk4hf/four2a.f90 b/lib/fsk4hf/four2a.f90
new file mode 100644
index 00000000..57c7239e
--- /dev/null
+++ b/lib/fsk4hf/four2a.f90
@@ -0,0 +1,115 @@
+subroutine four2a(a,nfft,ndim,isign,iform)
+
+! IFORM = 1, 0 or -1, as data is
+! complex, real, or the first half of a complex array. Transform
+! values are returned in array DATA. They are complex, real, or
+! the first half of a complex array, as IFORM = 1, -1 or 0.
+
+! The transform of a real array (IFORM = 0) dimensioned N(1) by N(2)
+! by ... will be returned in the same array, now considered to
+! be complex of dimensions N(1)/2+1 by N(2) by .... Note that if
+! IFORM = 0 or -1, N(1) must be even, and enough room must be
+! reserved. The missing values may be obtained by complex conjugation.
+
+! The reverse transformation of a half complex array dimensioned
+! N(1)/2+1 by N(2) by ..., is accomplished by setting IFORM
+! to -1. In the N array, N(1) must be the true N(1), not N(1)/2+1.
+! The transform will be real and returned to the input array.
+
+! This version of four2a makes calls to the FFTW library to do the
+! actual computations.
+
+ parameter (NPMAX=2100) !Max numberf of stored plans
+ parameter (NSMALL=16384) !Max size of "small" FFTs
+ complex a(nfft) !Array to be transformed
+ complex aa(NSMALL) !Local copy of "small" a()
+ integer nn(NPMAX),ns(NPMAX),nf(NPMAX) !Params of stored plans
+ integer*8 nl(NPMAX),nloc !More params of plans
+ integer*8 plan(NPMAX) !Pointers to stored plans
+ logical found_plan
+ data nplan/0/ !Number of stored plans
+ common/patience/npatience,nthreads !Patience and threads for FFTW plans
+ include 'fftw3.f90' !FFTW definitions
+ save plan,nplan,nn,ns,nf,nl
+
+ if(nfft.lt.0) go to 999
+
+ nloc=loc(a)
+
+ found_plan = .false.
+ !$omp critical(four2a_setup)
+ do i=1,nplan
+ if(nfft.eq.nn(i) .and. isign.eq.ns(i) .and. &
+ iform.eq.nf(i) .and. nloc.eq.nl(i)) then
+ found_plan = .true.
+ exit
+ end if
+ enddo
+
+ if(i.ge.NPMAX) stop 'Too many FFTW plans requested.'
+
+ if (.not. found_plan) then
+ nplan=nplan+1
+ i=nplan
+
+ nn(i)=nfft
+ ns(i)=isign
+ nf(i)=iform
+ nl(i)=nloc
+
+! Planning: FFTW_ESTIMATE, FFTW_ESTIMATE_PATIENT, FFTW_MEASURE,
+! FFTW_PATIENT, FFTW_EXHAUSTIVE
+ nflags=FFTW_ESTIMATE
+ if(npatience.eq.1) nflags=FFTW_ESTIMATE_PATIENT
+ if(npatience.eq.2) nflags=FFTW_MEASURE
+ if(npatience.eq.3) nflags=FFTW_PATIENT
+ if(npatience.eq.4) nflags=FFTW_EXHAUSTIVE
+
+ if(nfft.le.NSMALL) then
+ jz=nfft
+ if(iform.eq.0) jz=nfft/2
+ aa(1:jz)=a(1:jz)
+ endif
+
+ !$omp critical(fftw) ! serialize non thread-safe FFTW3 calls
+ if(isign.eq.-1 .and. iform.eq.1) then
+ call sfftw_plan_dft_1d(plan(i),nfft,a,a,FFTW_FORWARD,nflags)
+ else if(isign.eq.1 .and. iform.eq.1) then
+ call sfftw_plan_dft_1d(plan(i),nfft,a,a,FFTW_BACKWARD,nflags)
+ else if(isign.eq.-1 .and. iform.eq.0) then
+ call sfftw_plan_dft_r2c_1d(plan(i),nfft,a,a,nflags)
+ else if(isign.eq.1 .and. iform.eq.-1) then
+ call sfftw_plan_dft_c2r_1d(plan(i),nfft,a,a,nflags)
+ else
+ stop 'Unsupported request in four2a'
+ endif
+ !$omp end critical(fftw)
+
+ if(nfft.le.NSMALL) then
+ jz=nfft
+ if(iform.eq.0) jz=nfft/2
+ a(1:jz)=aa(1:jz)
+ endif
+ end if
+ !$omp end critical(four2a_setup)
+
+ call sfftw_execute(plan(i))
+ return
+
+999 continue
+
+ !$omp critical(four2a)
+ do i=1,nplan
+! The test is only to silence a compiler warning:
+ if(ndim.ne.-999) then
+ !$omp critical(fftw) ! serialize non thread-safe FFTW3 calls
+ call sfftw_destroy_plan(plan(i))
+ !$omp end critical(fftw)
+ end if
+ enddo
+
+ nplan=0
+ !$omp end critical(four2a)
+
+ return
+end subroutine four2a
diff --git a/lib/fsk4hf/fsk4hf.f90 b/lib/fsk4hf/fsk4hf.f90
new file mode 100644
index 00000000..191e05c3
--- /dev/null
+++ b/lib/fsk4hf/fsk4hf.f90
@@ -0,0 +1,145 @@
+program fsk4hf
+
+! Simulate characteristics of a potential mode using LDPC (168,84) code,
+! 4-FSK modulation, and 30 s T/R sequences.
+
+ parameter (KK=84) !Information bits (72 + CRC12)
+ parameter (ND=84) !Data symbols: LDPC (168,84), r=1/2
+ parameter (NS=12) !Sync symbols (3 @ 4x4 Costas arrays)
+ parameter (NR=2) !Ramp up/down
+ parameter (NN=NR+NS+ND) !Total symbols (98)
+ parameter (NSPS=2688/84) !Samples per symbol (32)
+ parameter (NZ=NSPS*NN) !Samples in baseband waveform (3760)
+
+ character*8 arg
+ complex c0(0:NZ-1) !Complex waveform
+ complex c(0:NZ-1) !Complex waveform
+ real xnoise(0:NZ-1) !Generated random noise
+ real ynoise(0:NZ-1) !Generated random noise
+ real rxdata(2*ND),llr(2*ND) !Soft symbols
+ real s(0:NSPS,NN)
+ real savg(0:NSPS)
+ real ps(0:3)
+ integer id(ND) !Symbol values (0-3), data only
+ integer id1(ND) !Recovered data values
+ integer*1 msgbits(KK),decoded(KK),apmask(ND),cw(ND)
+ data msgbits/0,0,1,0,0,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1, &
+ 1,1,1,0,1,1,1,1,1,1,1,0,0,1,0,0,1,1,0,1,0,1,1,1,0,1,1,0,1,1, &
+ 1,1,0,1,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,1,0/
+
+ nargs=iargc()
+ if(nargs.ne.5) then
+ print*,'Usage: fsk4hf f0(Hz) delay(ms) fspread(Hz) iters snr(dB)'
+ print*,'Example: fsk4hf 20 0 0 10 -20'
+ print*,'Set snr=0 to cycle through a range'
+ go to 999
+ endif
+ call getarg(1,arg)
+ read(arg,*) f0 !Generated carrier frequency
+ call getarg(2,arg)
+ read(arg,*) delay !Delta_t (ms) for Watterson model
+ call getarg(3,arg)
+ read(arg,*) fspread !Fspread (Hz) for Watterson model
+ call getarg(4,arg)
+ read(arg,*) iters !Iterations at each SNR
+ call getarg(5,arg)
+ read(arg,*) snrdb !Specified SNR_2500
+
+ twopi=8.0*atan(1.0)
+ fs=12000.0/84.0 !Sample rate = 142.857... Hz
+ dt=1.0/fs !Sample interval (s)
+ tt=NSPS*dt !Duration of "itone" symbols (s)
+ baud=1.0/tt !Keying rate for "itone" symbols (baud)
+ txt=NZ*dt !Transmission length (s)
+ bandwidth_ratio=2500.0/(fs/2.0)
+ write(*,1000) f0,delay,fspread,iters,baud,4*baud,txt
+1000 format('f0:',f5.1,' Delay:',f4.1,' fSpread:',f5.2, &
+ ' Iters:',i6/'Baud:',f7.3,' BW:',f5.1,' TxT:',f5.1,f5.2/)
+ write(*,1004)
+1004 format(/' SNR sym bit ser ber fer fsigma'/50('-'))
+
+ call genfsk4hf(msgbits,f0,id,c0) !Generate baseband waveform
+ isna=-10
+ isnb=-30
+ if(snrdb.ne.0.0) then
+ isna=nint(snrdb)
+ isnb=isna
+ endif
+ do isnr=isna,isnb,-1 !Loop over SNR range
+ snrdb=isnr
+ sig=sqrt(bandwidth_ratio) * 10.0**(0.05*snrdb)
+ if(snrdb.gt.90.0) sig=1.0
+ nhard=0
+ nbit=0
+ nfe=0
+ sqf=0.
+ do iter=1,iters !Loop over requested iterations
+ c=c0
+ if(delay.ne.0.0 .or. fspread.ne.0.0) then
+ call watterson(c,NZ,fs,delay,fspread)
+ endif
+ c=sig*c !Scale to requested SNR
+ if(snrdb.lt.90) then
+ do i=0,NZ-1 !Generate gaussian noise
+ xnoise(i)=gran()
+ ynoise(i)=gran()
+ enddo
+ c=c + cmplx(xnoise,ynoise) !Add AWGN noise
+ endif
+ df=fs/(2*NSPS)
+ i0=nint(f0/df)
+ call spec4(c,s,savg)
+ do i=0,NSPS
+ write(12,3001) i*df,savg(i),db(savg(i))
+3001 format(3f15.3)
+ enddo
+
+ do j=1,ND
+ nlo=0
+ nhi=0
+ k=j+5
+ if(j.ge.43) k=j+9
+ ps=s(i0:i0+6:2,k)
+ ps=sqrt(ps) !###
+ rlo=max(ps(1),ps(3))-max(ps(0),ps(2))
+ rhi=max(ps(2),ps(3))-max(ps(0),ps(1))
+ if(rlo.ge.0.0) nlo=1
+ if(rhi.ge.0.0) nhi=1
+ rxdata(2*j-1)=rhi
+ rxdata(2*j)=rlo
+ id1(j)=2*nhi+nlo
+ enddo
+! write(*,1001) id(1:70)
+! write(*,1001) id1(1:70)
+!1001 format(70i1)
+ nhard=nhard+count(id.ne.id1)
+ nbit=nbit + count(iand(id,1).ne.iand(id1,1)) + &
+ count(iand(id,2).ne.iand(id1,2))
+
+ 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 bpdecode168(llr,apmask,max_iterations,decoded,niterations,cw)
+ nbadcrc=0
+ if(niterations.ge.0) call chkcrc12(decoded,nbadcrc)
+ if(niterations.lt.0 .or. count(msgbits.ne.decoded).gt.0 .or. &
+ nbadcrc.ne.0) ifer=1
+ nfe=nfe+ifer
+ enddo
+
+ fsigma=sqrt(sqf/iters)
+ ser=float(nhard)/(ND*iters)
+ ber=float(nbit)/(2*ND*iters)
+ fer=float(nfe)/iters
+ write(*,1050) snrdb,nhard,nbit,ser,ber,fer,fsigma
+! write(60,1050) snrdb,nhard,ber,fer,fsigma
+1050 format(f6.1,2i6,2f8.4,f7.3,f8.2)
+ enddo
+
+999 end program fsk4hf
diff --git a/lib/fsk4hf/fsk4sim.f90 b/lib/fsk4hf/fsk4sim.f90
new file mode 100644
index 00000000..9800616e
--- /dev/null
+++ b/lib/fsk4hf/fsk4sim.f90
@@ -0,0 +1,185 @@
+program fsk4sim
+
+ parameter (ND=60) !Data symbols: LDPC (120,60), r=1/2
+ parameter (NN=ND) !Total symbols (60)
+ parameter (NSPS=57600) !Samples per symbol at 12000 sps
+ parameter (NZ=NSPS*NN) !Samples in waveform (3456000)
+
+ character*8 arg
+ complex c(0:NZ-1) !Complex waveform
+ complex cr(0:NZ-1)
+ complex cs(NSPS,NN)
+ complex cps(0:3)
+ complex ct(0:2*NN-1)
+ complex z,w,zsum
+ real r(0:NZ-1)
+ real s(NSPS,NN)
+ real savg(NSPS)
+ real tmp(NN) !For generating random data
+ real xnoise(0:NZ-1) !Generated random noise
+ real ps(0:3)
+ integer id(NN) !Encoded 2-bit data (values 0-3)
+ integer id2(NN) !Recovered data
+ equivalence (r,cr)
+
+ nnn=0
+ nargs=iargc()
+ if(nargs.ne.6) then
+ print*,'Usage: fsk8sim f0 delay(ms) fspread(Hz) nts iters snr(dB)'
+ go to 999
+ endif
+ call getarg(1,arg)
+ read(arg,*) f0 !Low tone frequency
+ call getarg(2,arg)
+ read(arg,*) delay
+ call getarg(3,arg)
+ read(arg,*) fspread
+ call getarg(4,arg)
+ read(arg,*) nts
+ call getarg(5,arg)
+ read(arg,*) iters
+ call getarg(6,arg)
+ read(arg,*) snrdb
+
+ twopi=8.d0*atan(1.d0)
+ fs=12000.d0
+ dt=1.0/fs
+ ts=NSPS*dt
+ baud=1.d0/ts
+ txt=NZ*dt
+ bandwidth_ratio=2500.0/6000.0
+ write(*,1000) baud,5*baud,txt,delay,fspread,nts
+1000 format('Baud:',f6.3,' BW:',f5.1,' TxT:',f5.1,' Delay:',f5.2, &
+ ' fSpread:',f5.2,' nts:',i3/)
+
+ write(*,1004)
+1004 format(' SNR Sym Bit SER BER Sym Bit SER BER'/59('-'))
+
+ isna=-25
+ isnb=-40
+ if(snrdb.ne.0.0) then
+ isna=nint(snrdb)
+ isnb=isna
+ endif
+ do isnr=isna,isnb,-1
+ snrdb=isnr
+ sig=sqrt(2*bandwidth_ratio) * 10.0**(0.05*snrdb)
+ if(snrdb.gt.90.0) sig=1.0
+ nhard1=0
+ nhard2=0
+ nbit1=0
+ nbit2=0
+ nh2=0
+ nb2=0
+ do iter=1,iters
+ nnn=nnn+1
+ id=0
+ call random_number(tmp)
+ where(tmp.ge.0.25 .and. tmp.lt.0.50) id=1
+ where(tmp.ge.0.50 .and. tmp.lt.0.75) id=2
+ where(tmp.ge.0.75) id=3
+
+ call genfsk4(id,f0,nts,c) !Generate the 4-FSK waveform
+ call watterson(c,delay,fspread)
+ if(sig.ne.1.0) c=sig*c !Scale to requested SNR
+ if(snrdb.lt.90) then
+ do i=0,NZ-1 !Generate gaussian noise
+ xnoise(i)=gran()
+ enddo
+ endif
+ r(0:NZ-1)=real(c(0:NZ-1)) + xnoise !Add noise to signal
+
+ call snr2_wsprlf(r,freq,snr2500,width,1)
+ write(*,3001) freq,snr2500,width
+3001 format(40x,3f10.3)
+
+ df=12000.0/(2*NSPS)
+! i0=nint(f0/df)
+! i0=nint((1500.0+freq)/df)
+ i0=nint((f0+freq)/df)
+ call spec4(r,cs,s,savg)
+
+ do j=1,NN
+ nlo=0
+ nhi=0
+ ps=s(i0:i0+6*nts:2*nts,j)
+ cps=cs(i0:i0+6*nts:2*nts,j)
+ if(max(ps(1),ps(3)).ge.max(ps(0),ps(2))) nlo=1
+ if(max(ps(2),ps(3)).ge.max(ps(0),ps(1))) nhi=1
+ id2(j)=2*nhi+nlo
+ z=cps(id2(j))
+ ct(j-1)=z
+ enddo
+ nh1=count(id.ne.id2)
+ nb1=count(iand(id,1).ne.iand(id2,1)) + count(iand(id,2).ne.iand(id2,2))
+
+ ct(NN:)=0.
+ call four2a(ct,2*NN,1,-1,1)
+ df2=baud/(2*NN)
+ ct=cshift(ct,NN)
+ ppmax=0.
+ dfpk=0.
+ do i=0,2*NN-1
+ f=(i-NN)*df2
+ pp=real(ct(i))**2 + aimag(ct(i))**2
+ if(pp.gt.ppmax) then
+ ppmax=pp
+ dfpk=f
+ endif
+ enddo
+
+ zsum=0.
+ do j=1,NN
+ phi=(j-1)*twopi*dfpk*ts
+ w=cmplx(cos(phi),sin(phi))
+ cps=cs(i0:i0+6*nts:2*nts,j)*conjg(w)
+ z=cps(id2(j))
+ ct(j)=z
+ zsum=zsum+z
+ write(12,1042) j,id(j),id2(j),20*ps,atan2(aimag(z),real(z)), &
+ atan2(aimag(zsum),real(zsum)),zsum
+1042 format(3i2,6f8.3,2f8.1)
+ enddo
+
+ phi0=atan2(aimag(zsum),real(zsum))
+ zsum=0.
+ do j=1,NN
+ phi=(j-1)*twopi*dfpk*ts + phi0
+ w=cmplx(cos(phi),sin(phi))
+ nlo=0
+ nhi=0
+ cps=cs(i0:i0+6*nts:2*nts,j)*conjg(w)
+ ps=real(cps)
+ if(max(ps(1),ps(3)).ge.max(ps(0),ps(2))) nlo=1
+ if(max(ps(2),ps(3)).ge.max(ps(0),ps(1))) nhi=1
+ id2(j)=2*nhi+nlo
+ z=cps(id2(j))
+ ct(j)=z
+ zsum=zsum+z
+ enddo
+
+ nh2=count(id.ne.id2)
+ nb2=count(iand(id,1).ne.iand(id2,1)) + count(iand(id,2).ne.iand(id2,2))
+ nhard1=nhard1+nh1
+ nhard2=nhard2+nh2
+ nbit1=nbit1+nb1
+ nbit2=nbit2+nb2
+
+ fdiff=1500.0+freq - f0
+ write(13,1040) snrdb,snr2500,f0,fdiff,width,nh1,nb1,nh2,nb2
+1040 format(2f7.1,f9.2,f7.2,f6.1,2(i8,i6))
+40 continue
+ enddo
+
+ ser1=float(nhard1)/(NN*iters)
+ ser2=float(nhard2)/(NN*iters)
+ ber1=float(nbit1)/(2*NN*iters)
+ ber2=float(nbit2)/(2*NN*iters)
+ write(*,1050) snrdb,nhard1,nbit1,ser1,ber1,nhard2,nbit2,ser2,ber2
+ write(14,1050) snrdb,nhard1,nbit1,ser1,ber1,nhard2,nbit2,ser2,ber2
+1050 format(f6.1,2(2i5,2f8.4))
+ enddo
+ write(*,1060) NN*iters,2*NN*iters
+1060 format(59('-')/'Max: ',2i5)
+
+999 end program fsk4sim
diff --git a/lib/fsk4hf/ft8_downsample.f90 b/lib/fsk4hf/ft8_downsample.f90
new file mode 100644
index 00000000..79191416
--- /dev/null
+++ b/lib/fsk4hf/ft8_downsample.f90
@@ -0,0 +1,42 @@
+subroutine ft8_downsample(dd,newdat,f0,c1)
+
+! Downconvert to complex data sampled at 200 Hz ==> 32 samples/symbol
+
+ parameter (NMAX=15*12000,NSPS=1920)
+ parameter (NFFT1=192000,NFFT2=3200) !192000/60 = 3200
+
+ logical newdat
+ complex c1(0:NFFT2-1)
+ complex cx(0:NFFT1/2)
+ real dd(NMAX),x(NFFT1)
+ equivalence (x,cx)
+ save cx
+
+ if(newdat) then
+! Data in dd have changed, recompute the long FFT
+ x(1:NMAX)=dd
+ x(NMAX+1:NFFT1)=0. !Zero-pad the x array
+ call four2a(cx,NFFT1,1,-1,0) !r2c FFT to freq domain
+ newdat=.false.
+ endif
+
+ df=12000.0/NFFT1
+ baud=12000.0/NSPS
+ i0=nint(f0/df)
+ ft=f0+8.0*baud
+ it=min(nint(ft/df),NFFT1/2)
+ fb=f0-1.0*baud
+ ib=max(1,nint(fb/df))
+ k=0
+ c1=0.
+ do i=ib,it
+ c1(k)=cx(i)
+ k=k+1
+ enddo
+ c1=cshift(c1,i0-ib)
+ call four2a(c1,NFFT2,1,1,1) !c2c FFT back to time domain
+ fac=1.0/sqrt(float(NFFT1)*NFFT2)
+ c1=fac*c1
+
+ return
+end subroutine ft8_downsample
diff --git a/lib/fsk4hf/ft8_params.f90 b/lib/fsk4hf/ft8_params.f90
new file mode 100644
index 00000000..858e61e9
--- /dev/null
+++ b/lib/fsk4hf/ft8_params.f90
@@ -0,0 +1,12 @@
+! LDPC (174,87) code
+parameter (KK=87) !Information bits (75 + CRC12)
+parameter (ND=58) !Data symbols
+parameter (NS=21) !Sync symbols (3 @ Costas 7x7)
+parameter (NN=NS+ND) !Total channel symbols (79)
+parameter (NSPS=1920) !Samples per symbol at 12000 S/s
+parameter (NZ=NSPS*NN) !Samples in full 15 s waveform (151,680)
+parameter (NMAX=15*12000) !Samples in iwave (180,000)
+parameter (NFFT1=2*NSPS, NH1=NFFT1/2) !Length of FFTs for symbol spectra
+parameter (NSTEP=NSPS/4) !Rough time-sync step size
+parameter (NHSYM=NMAX/NSTEP-3) !Number of symbol spectra (1/4-sym steps)
+parameter (NDOWN=60) !Downsample factor
diff --git a/lib/fsk4hf/ft8apset.f90 b/lib/fsk4hf/ft8apset.f90
new file mode 100644
index 00000000..ca5e5a6d
--- /dev/null
+++ b/lib/fsk4hf/ft8apset.f90
@@ -0,0 +1,31 @@
+subroutine ft8apset(mycall12,mygrid6,hiscall12,hisgrid6,bcontest,apsym,iaptype)
+ parameter(NAPM=4,KK=87)
+ character*12 mycall12,hiscall12
+ character*22 msg,msgsent
+ character*6 mycall,hiscall
+ character*6 mygrid6,hisgrid6
+ character*4 hisgrid
+ logical bcontest
+ integer apsym(KK)
+ integer*1 msgbits(KK)
+ integer itone(KK)
+
+ mycall=mycall12(1:6)
+ hiscall=hiscall12(1:6)
+ hisgrid=hisgrid6(1:4)
+ if(len_trim(hiscall).eq.0) then
+ iaptype=1
+ hiscall="K9AN"
+ else
+ iaptype=2
+ endif
+ hisgrid=hisgrid6(1:4)
+! if(len_trim(hisgrid).eq.0) hisgrid="EN50"
+ if(index(hisgrid," ").eq.0) hisgrid="EN50"
+ msg=mycall//' '//hiscall//' '//hisgrid
+ i3bit=0 ! ### TEMPORARY ??? ###
+ call genft8(msg,mygrid6,bcontest,i3bit,msgsent,msgbits,itone)
+ apsym=2*msgbits-1
+
+ return
+end subroutine ft8apset
diff --git a/lib/fsk4hf/ft8b.f90 b/lib/fsk4hf/ft8b.f90
new file mode 100644
index 00000000..3049dfd4
--- /dev/null
+++ b/lib/fsk4hf/ft8b.f90
@@ -0,0 +1,437 @@
+subroutine ft8b(dd0,newdat,nQSOProgress,nfqso,nftx,ndepth,lapon,napwid, &
+ lsubtract,nagain,iaptype,mygrid6,bcontest,sync0,f1,xdt,xbase,apsym, &
+ nharderrors,dmin,nbadcrc,ipass,iera,message,xsnr)
+
+ use timer_module, only: timer
+ include 'ft8_params.f90'
+ parameter(NRECENT=10,NP2=2812)
+ character message*22,msgsent*22
+ character*12 recent_calls(NRECENT)
+ character*6 mygrid6
+ logical bcontest
+ real a(5)
+ real s1(0:7,ND),s2(0:7,NN),s1sort(8*ND)
+ real ps(0:7),psl(0:7)
+ real bmeta(3*ND),bmetb(3*ND),bmetap(3*ND)
+ real llr(3*ND),llra(3*ND),llr0(3*ND),llr1(3*ND),llrap(3*ND) !Soft symbols
+ real dd0(15*12000)
+ integer*1 decoded(KK),apmask(3*ND),cw(3*ND)
+ integer*1 msgbits(KK)
+ integer apsym(KK)
+ integer mcq(28),mde(28),mrrr(16),m73(16),mrr73(16)
+ integer itone(NN)
+ integer indxs1(8*ND)
+ integer icos7(0:6),ip(1)
+ integer nappasses(0:5) ! the number of decoding passes to use for each QSO state
+ integer naptypes(0:5,4) ! (nQSOProgress, decoding pass) maximum of 4 passes for now
+ complex cd0(3200)
+ complex ctwk(32)
+ complex csymb(32)
+ logical first,newdat,lsubtract,lapon,nagain
+ equivalence (s1,s1sort)
+ data icos7/2,5,6,0,4,1,3/
+ data mcq/1,1,1,1,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,1,1,0,0,1/
+ data mrrr/0,1,1,1,1,1,1,0,1,1,0,0,1,1,1,1/
+ data m73/0,1,1,1,1,1,1,0,1,1,0,1,0,0,0,0/
+ data mde/1,1,1,1,1,1,1,1,0,1,1,0,0,1,0,0,0,0,0,1,1,1,0,1,0,0,0,1/
+ data mrr73/0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,1/
+ data first/.true./
+ save nappasses,naptypes
+
+ if(first) then
+ mcq=2*mcq-1
+ mde=2*mde-1
+ mrrr=2*mrrr-1
+ m73=2*m73-1
+ mrr73=2*mrr73-1
+ nappasses(0)=2
+ nappasses(1)=2
+ nappasses(2)=2
+ nappasses(3)=4
+ nappasses(4)=4
+ nappasses(5)=3
+
+! iaptype
+!------------------------
+! 1 CQ ??? ???
+! 2 MyCall ??? ???
+! 3 MyCall DxCall ???
+! 4 MyCall DxCall RRR
+! 5 MyCall DxCall 73
+! 6 MyCall DxCall RR73
+! 7 ??? DxCall ???
+
+ naptypes(0,1:4)=(/1,2,0,0/)
+ naptypes(1,1:4)=(/2,3,0,0/)
+ naptypes(2,1:4)=(/2,3,0,0/)
+ naptypes(3,1:4)=(/3,4,5,6/)
+ naptypes(4,1:4)=(/3,4,5,6/)
+ naptypes(5,1:4)=(/3,1,2,0/) !?
+ first=.false.
+ endif
+
+ max_iterations=30
+ nharderrors=-1
+ fs2=12000.0/NDOWN
+ dt2=1.0/fs2
+ twopi=8.0*atan(1.0)
+ delfbest=0.
+ ibest=0
+
+ call timer('ft8_down',0)
+ call ft8_downsample(dd0,newdat,f1,cd0) !Mix f1 to baseband and downsample
+ call timer('ft8_down',1)
+
+ i0=nint((xdt+0.5)*fs2) !Initial guess for start of signal
+ smax=0.0
+ do idt=i0-8,i0+8 !Search over +/- one quarter symbol
+ call sync8d(cd0,idt,ctwk,0,sync)
+ if(sync.gt.smax) then
+ smax=sync
+ ibest=idt
+ endif
+ enddo
+ xdt2=ibest*dt2 !Improved estimate for DT
+
+! Now peak up in frequency
+ i0=nint(xdt2*fs2)
+ smax=0.0
+ do ifr=-5,5 !Search over +/- 2.5 Hz
+ delf=ifr*0.5
+ dphi=twopi*delf*dt2
+ phi=0.0
+ do i=1,32
+ ctwk(i)=cmplx(cos(phi),sin(phi))
+ phi=mod(phi+dphi,twopi)
+ enddo
+ call sync8d(cd0,i0,ctwk,1,sync)
+ if( sync .gt. smax ) then
+ smax=sync
+ delfbest=delf
+ endif
+ enddo
+ a=0.0
+ a(1)=-delfbest
+ call twkfreq1(cd0,NP2,fs2,a,cd0)
+ xdt=xdt2
+ f1=f1+delfbest !Improved estimate of DF
+
+ call sync8d(cd0,i0,ctwk,2,sync)
+
+ j=0
+ do k=1,NN
+ i1=ibest+(k-1)*32
+ csymb=cmplx(0.0,0.0)
+ if( i1.ge.1 .and. i1+31 .le. NP2 ) csymb=cd0(i1:i1+31)
+ call four2a(csymb,32,1,-1,1)
+ s2(0:7,k)=abs(csymb(1:8))/1e3
+ enddo
+
+! sync quality check
+ is1=0
+ is2=0
+ is3=0
+ do k=1,7
+ ip=maxloc(s2(:,k))
+ if(icos7(k-1).eq.(ip(1)-1)) is1=is1+1
+ ip=maxloc(s2(:,k+36))
+ if(icos7(k-1).eq.(ip(1)-1)) is2=is2+1
+ ip=maxloc(s2(:,k+72))
+ if(icos7(k-1).eq.(ip(1)-1)) is3=is3+1
+ enddo
+! hard sync sum - max is 21
+ nsync=is1+is2+is3
+ if(nsync .le. 6) then ! bail out
+ nbadcrc=1
+ return
+ endif
+
+ j=0
+ do k=1,NN
+ if(k.le.7) cycle
+ if(k.ge.37 .and. k.le.43) cycle
+ if(k.gt.72) cycle
+ j=j+1
+ s1(0:7,j)=s2(0:7,k)
+ enddo
+
+ call indexx(s1sort,8*ND,indxs1)
+ xmeds1=s1sort(indxs1(nint(0.5*8*ND)))
+ s1=s1/xmeds1
+
+ do j=1,ND
+ i4=3*j-2
+ i2=3*j-1
+ i1=3*j
+! Max amplitude
+ ps=s1(0:7,j)
+ r1=max(ps(1),ps(3),ps(5),ps(7))-max(ps(0),ps(2),ps(4),ps(6))
+ r2=max(ps(2),ps(3),ps(6),ps(7))-max(ps(0),ps(1),ps(4),ps(5))
+ r4=max(ps(4),ps(5),ps(6),ps(7))-max(ps(0),ps(1),ps(2),ps(3))
+ bmeta(i4)=r4
+ bmeta(i2)=r2
+ bmeta(i1)=r1
+ bmetap(i4)=r4
+ bmetap(i2)=r2
+ bmetap(i1)=r1
+! Max log metric
+ psl=log(ps)
+ r1=max(psl(1),psl(3),psl(5),psl(7))-max(psl(0),psl(2),psl(4),psl(6))
+ r2=max(psl(2),psl(3),psl(6),psl(7))-max(psl(0),psl(1),psl(4),psl(5))
+ r4=max(psl(4),psl(5),psl(6),psl(7))-max(psl(0),psl(1),psl(2),psl(3))
+ bmetb(i4)=r4
+ bmetb(i2)=r2
+ bmetb(i1)=r1
+
+! Metric for Cauchy noise
+! r1=log(ps(1)**3+ps(3)**3+ps(5)**3+ps(7)**3)- &
+! log(ps(0)**3+ps(2)**3+ps(4)**3+ps(6)**3)
+! r2=log(ps(2)**3+ps(3)**3+ps(6)**3+ps(7)**3)- &
+! log(ps(0)**3+ps(1)**3+ps(4)**3+ps(5)**3)
+! r4=log(ps(4)**3+ps(5)**3+ps(6)**3+ps(7)**3)- &
+! log(ps(0)**3+ps(1)**3+ps(2)**3+ps(3)**3)
+! Metric for AWGN, no fading
+! bscale=2.5
+! b0=bessi0(bscale*ps(0))
+! b1=bessi0(bscale*ps(1))
+! b2=bessi0(bscale*ps(2))
+! b3=bessi0(bscale*ps(3))
+! b4=bessi0(bscale*ps(4))
+! b5=bessi0(bscale*ps(5))
+! b6=bessi0(bscale*ps(6))
+! b7=bessi0(bscale*ps(7))
+! r1=log(b1+b3+b5+b7)-log(b0+b2+b4+b6)
+! r2=log(b2+b3+b6+b7)-log(b0+b1+b4+b5)
+! r4=log(b4+b5+b6+b7)-log(b0+b1+b2+b3)
+
+ if(nQSOProgress .eq. 0 .or. nQSOProgress .eq. 5) then
+! When bits 88:115 are set as ap bits, bit 115 lives in symbol 39 along
+! with no-ap bits 116 and 117. Take care of metrics for bits 116 and 117.
+ if(j.eq.39) then ! take care of bits that live in symbol 39
+ if(apsym(28).lt.0) then
+ bmetap(i2)=max(ps(2),ps(3))-max(ps(0),ps(1))
+ bmetap(i1)=max(ps(1),ps(3))-max(ps(0),ps(2))
+ else
+ bmetap(i2)=max(ps(6),ps(7))-max(ps(4),ps(5))
+ bmetap(i1)=max(ps(5),ps(7))-max(ps(4),ps(6))
+ endif
+ endif
+ endif
+
+! When bits 116:143 are set as ap bits, bit 115 lives in symbol 39 along
+! with ap bits 116 and 117. Take care of metric for bit 115.
+! if(j.eq.39) then ! take care of bit 115
+! iii=2*(apsym(29)+1)/2 + (apsym(30)+1)/2 ! known values of bits 116 & 117
+! if(iii.eq.0) bmetap(i4)=ps(4)-ps(0)
+! if(iii.eq.1) bmetap(i4)=ps(5)-ps(1)
+! if(iii.eq.2) bmetap(i4)=ps(6)-ps(2)
+! if(iii.eq.3) bmetap(i4)=ps(7)-ps(3)
+! endif
+
+! bit 144 lives in symbol 48 and will be 1 if it is set as an ap bit.
+! take care of metrics for bits 142 and 143
+ if(j.eq.48) then ! bit 144 is always 1
+ bmetap(i4)=max(ps(5),ps(7))-max(ps(1),ps(3))
+ bmetap(i2)=max(ps(3),ps(7))-max(ps(1),ps(5))
+ endif
+
+! bit 154 lives in symbol 52 and will be 0 if it is set as an ap bit
+! take care of metrics for bits 155 and 156
+ if(j.eq.52) then ! bit 154 will be 0 if it is set as an ap bit.
+ bmetap(i2)=max(ps(2),ps(3))-max(ps(0),ps(1))
+ bmetap(i1)=max(ps(1),ps(3))-max(ps(0),ps(2))
+ endif
+
+ enddo
+
+ call normalizebmet(bmeta,3*ND)
+ call normalizebmet(bmetb,3*ND)
+ call normalizebmet(bmetap,3*ND)
+
+ scalefac=2.83
+ llr0=scalefac*bmeta
+ llr1=scalefac*bmetb
+ llra=scalefac*bmetap ! llr's for use with ap
+ apmag=scalefac*(maxval(abs(bmetap))*1.01)
+
+! pass #
+!------------------------------
+! 1 regular decoding
+! 2 erase 24
+! 3 erase 48
+! 4 ap pass 1
+! 5 ap pass 2
+! 6 ap pass 3
+! 7 ap pass 4, etc.
+
+ if(lapon) then
+ npasses=4+nappasses(nQSOProgress)
+ else
+ npasses=4
+ endif
+
+ do ipass=1,npasses
+
+ llr=llr0
+ if(ipass.eq.2) llr=llr1
+ if(ipass.eq.3) llr(1:24)=0.
+ if(ipass.eq.4) llr(1:48)=0.
+ if(ipass.le.4) then
+ apmask=0
+ llrap=llr
+ iaptype=0
+ endif
+
+ if(ipass .gt. 4) then
+ iaptype=naptypes(nQSOProgress,ipass-4)
+ if(iaptype.ge.3 .and. (abs(f1-nfqso).gt.napwid .and. abs(f1-nftx).gt.napwid) ) cycle
+ if(iaptype.eq.1 .or. iaptype.eq.2 ) then ! AP,???,???
+ apmask=0
+ apmask(88:115)=1 ! first 28 bits are AP
+ apmask(144)=1 ! not free text
+ llrap=llr
+ if(iaptype.eq.1) llrap(88:115)=apmag*mcq
+ if(iaptype.eq.2) llrap(88:115)=apmag*apsym(1:28)
+ llrap(116:117)=llra(116:117)
+ llrap(142:143)=llra(142:143)
+ llrap(144)=-apmag
+ endif
+ if(iaptype.eq.3) then ! mycall, dxcall, ???
+ apmask=0
+ apmask(88:115)=1 ! mycall
+ apmask(116:143)=1 ! hiscall
+ apmask(144)=1 ! not free text
+ llrap=llr
+ llrap(88:143)=apmag*apsym(1:56)
+ llrap(144)=-apmag
+ endif
+ if(iaptype.eq.4 .or. iaptype.eq.5 .or. iaptype.eq.6) then
+ apmask=0
+ apmask(88:115)=1 ! mycall
+ apmask(116:143)=1 ! hiscall
+ apmask(144:159)=1 ! RRR or 73 or RR73
+ llrap=llr
+ llrap(88:143)=apmag*apsym(1:56)
+ if(iaptype.eq.4) llrap(144:159)=apmag*mrrr
+ if(iaptype.eq.5) llrap(144:159)=apmag*m73
+ if(iaptype.eq.6) llrap(144:159)=apmag*mrr73
+ endif
+ if(iaptype.eq.7) then ! ???, dxcall, ???
+ apmask=0
+ apmask(116:143)=1 ! hiscall
+ apmask(144)=1 ! not free text
+ llrap=llr
+ llrap(115)=llra(115)
+ llrap(116:143)=apmag*apsym(29:56)
+ llrap(144)=-apmag
+ endif
+ endif
+
+ cw=0
+ call timer('bpd174 ',0)
+ call bpdecode174(llrap,apmask,max_iterations,decoded,cw,nharderrors, &
+ niterations)
+ call timer('bpd174 ',1)
+ dmin=0.0
+ if(ndepth.eq.3 .and. nharderrors.lt.0) then
+ ndeep=3
+ if(abs(nfqso-f1).le.napwid .or. abs(nftx-f1).le.napwid) then
+ if((ipass.eq.3 .or. ipass.eq.4) .and. .not.nagain) then
+ ndeep=3
+ else
+ ndeep=4
+ endif
+ endif
+ if(nagain) ndeep=5
+ call timer('osd174 ',0)
+ call osd174(llrap,apmask,ndeep,decoded,cw,nharderrors,dmin)
+ call timer('osd174 ',1)
+ endif
+ nbadcrc=1
+ message=' '
+ xsnr=-99.0
+ if(count(cw.eq.0).eq.174) cycle !Reject the all-zero codeword
+!### if(any(decoded(73:75).ne.0)) cycle !Reject if any of the 3 extra bits is nonzero
+ if(nharderrors.ge.0 .and. nharderrors+dmin.lt.60.0 .and. &
+ .not.(sync.lt.2.0 .and. nharderrors.gt.35) .and. &
+ .not.(ipass.gt.2 .and. nharderrors.gt.39) .and. &
+ .not.(ipass.eq.4 .and. nharderrors.gt.30) &
+ ) then
+ call chkcrc12a(decoded,nbadcrc)
+ else
+ nharderrors=-1
+ cycle
+ endif
+!###
+ i3bit=4*decoded(73) + 2*decoded(74) + decoded(75)
+ iFreeText=decoded(57)
+! if(nbadcrc.eq.0) write(*,3001) nharderrors,nbadcrc,i3bit
+!3001 format('A',3i5)
+!###
+ if(nbadcrc.eq.0) then
+ call extractmessage174(decoded,message,ncrcflag,recent_calls,nrecent)
+ call genft8(message,mygrid6,bcontest,i3bit,msgsent,msgbits,itone)
+ if(i3bit.eq.1 .and. iFreeText.eq.0) message(21:21)='1'
+ if(i3bit.eq.2 .and. iFreeText.eq.0) message(21:21)='2'
+ if(lsubtract) call subtractft8(dd0,itone,f1,xdt2)
+ xsig=0.0
+ xnoi=0.0
+ do i=1,79
+ xsig=xsig+s2(itone(i),i)**2
+ ios=mod(itone(i)+4,7)
+ xnoi=xnoi+s2(ios,i)**2
+ enddo
+ xsnr=0.001
+ if(xnoi.gt.0 .and. xnoi.lt.xsig) xsnr=xsig/xnoi-1.0
+ xsnr=10.0*log10(xsnr)-27.0
+ xsnr2=db(xsig/xbase - 1.0) - 32.0
+! write(52,3052) f1,xdt,xsig,xnoi,xbase,xsnr,xsnr2
+!3052 format(7f10.2)
+ if(.not.nagain) xsnr=xsnr2
+ if(xsnr .lt. -24.0) xsnr=-24.0
+ return
+ endif
+ enddo
+
+ return
+end subroutine ft8b
+
+subroutine normalizebmet(bmet,n)
+ real bmet(n)
+
+ bmetav=sum(bmet)/real(n)
+ bmet2av=sum(bmet*bmet)/real(n)
+ var=bmet2av-bmetav*bmetav
+ if( var .gt. 0.0 ) then
+ bmetsig=sqrt(var)
+ else
+ bmetsig=sqrt(bmet2av)
+ endif
+ bmet=bmet/bmetsig
+ return
+end subroutine normalizebmet
+
+
+function bessi0(x)
+! From Numerical Recipes
+ real bessi0,x
+ double precision p1,p2,p3,p4,p5,p6,p7,q1,q2,q3,q4,q5,q6,q7,q8,q9,y
+ save p1,p2,p3,p4,p5,p6,p7,q1,q2,q3,q4,q5,q6,q7,q8,q9
+ data p1,p2,p3,p4,p5,p6,p7/1.0d0,3.5156229d0,3.0899424d0,1.2067492d0, &
+ 0.2659732d0,0.360768d-1,0.45813d-2/
+ data q1,q2,q3,q4,q5,q6,q7,q8,q9/0.39894228d0,0.1328592d-1, &
+ 0.225319d-2,-0.157565d-2,0.916281d-2,-0.2057706d-1, &
+ 0.2635537d-1,-0.1647633d-1,0.392377d-2/
+
+ if (abs(x).lt.3.75) then
+ y=(x/3.75)**2
+ bessi0=p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7)))))
+ else
+ ax=abs(x)
+ y=3.75/ax
+ bessi0=(exp(ax)/sqrt(ax))*(q1+y*(q2+y*(q3+y*(q4 &
+ +y*(q5+y*(q6+y*(q7+y*(q8+y*q9))))))))
+ endif
+ return
+end function bessi0
+
diff --git a/lib/fsk4hf/ft8d.f90 b/lib/fsk4hf/ft8d.f90
new file mode 100644
index 00000000..daee7b2b
--- /dev/null
+++ b/lib/fsk4hf/ft8d.f90
@@ -0,0 +1,63 @@
+program ft8d
+
+! Decode FT8 data read from *.wav files.
+
+ include 'ft8_params.f90'
+ character*12 arg
+ character infile*80,datetime*13,message*22
+ real s(NH1,NHSYM)
+ real candidate(3,100)
+ integer ihdr(11)
+ integer*2 iwave(NMAX) !Generated full-length waveform
+ real dd(NMAX)
+
+ nargs=iargc()
+ if(nargs.lt.3) then
+ print*,'Usage: ft8d MaxIt Norder file1 [file2 ...]'
+ print*,'Example ft8d 40 2 *.wav'
+ go to 999
+ endif
+ call getarg(1,arg)
+ read(arg,*) max_iterations
+ call getarg(2,arg)
+ read(arg,*) norder
+ nfiles=nargs-2
+
+ twopi=8.0*atan(1.0)
+ fs=12000.0 !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 (baud)
+ txt=NZ*dt !Transmission length (s)
+ nfa=100.0
+ nfb=3000.0
+ nfqso=1500.0
+
+ do ifile=1,nfiles
+ call getarg(ifile+2,infile)
+ open(10,file=infile,status='old',access='stream')
+ read(10,end=999) ihdr,iwave
+ close(10)
+ j2=index(infile,'.wav')
+ read(infile(j2-6:j2-1),*) nutc
+ datetime=infile(j2-13:j2-1)
+ call sync8(iwave,nfa,nfb,nfqso,s,candidate,ncand)
+ syncmin=2.0
+ dd=iwave
+ do icand=1,ncand
+ sync=candidate(3,icand)
+ if( sync.lt.syncmin) cycle
+ f1=candidate(1,icand)
+ xdt=candidate(2,icand)
+ nsnr=min(99,nint(10.0*log10(sync)-25.5))
+ call ft8b(dd,nfqso,f1,xdt,nharderrors,dmin,nbadcrc,message,xsnr)
+ nsnr=xsnr
+ xdt=xdt-0.6
+ write(*,1110) datetime,0,nsnr,xdt,f1,message,nharderrors,dmin
+1110 format(a13,2i4,f6.2,f7.1,' ~ ',a22,i6,f7.1)
+ enddo
+ enddo ! ifile loop
+
+999 end program ft8d
+
diff --git a/lib/fsk4hf/ft8sim.f90 b/lib/fsk4hf/ft8sim.f90
new file mode 100644
index 00000000..70d67a81
--- /dev/null
+++ b/lib/fsk4hf/ft8sim.f90
@@ -0,0 +1,126 @@
+program ft8sim
+
+! Generate simulated data for a 15-second HF/6m mode using 8-FSK.
+! Output is saved to a *.wav file.
+
+ use wavhdr
+ include 'ft8_params.f90' !Set various constants
+ type(hdr) h !Header for .wav file
+ character arg*12,fname*17,sorm*1
+ character msg*22,msgsent*22
+ character*6 mygrid6
+ logical bcontest
+ complex c0(0:NMAX-1)
+ complex c(0:NMAX-1)
+ integer itone(NN)
+ integer*1 msgbits(KK)
+ integer*2 iwave(NMAX) !Generated full-length waveform
+ data mygrid6/'EM48 '/
+
+! Get command-line argument(s)
+ nargs=iargc()
+ if(nargs.ne.8) then
+ print*,'Usage: ft8sim "message" s|m f0 DT fdop del nfiles snr'
+ print*,'Example: ft8sim "K1ABC W9XYZ EN37" m 1500.0 0.0 0.1 1.0 10 -18'
+ print*,'s|m: "s" for single signal at 1500 Hz, "m" for 25 signals'
+ print*,'f0 is ignored when sorm = m'
+ print*,'Make nfiles negative to invoke 72-bit contest mode.'
+ go to 999
+ endif
+ call getarg(1,msg) !Message to be transmitted
+ call getarg(2,sorm) !s for single signal, m for multiple sigs
+ if(sorm.eq."s") then
+ print*,"Generating single signal at 1500 Hz."
+ nsig=1
+ elseif( sorm.eq."m") then
+ print*,"Generating 25 signals per file."
+ nsig=25
+ else
+ print*,"sorm parameter must be s (single) or m (multiple)."
+ goto 999
+ endif
+ call getarg(3,arg)
+ read(arg,*) f0 !Frequency (only used for single-signal)
+ call getarg(4,arg)
+ read(arg,*) xdt !Time offset from nominal (s)
+ call getarg(5,arg)
+ read(arg,*) fspread !Watterson frequency spread (Hz)
+ call getarg(6,arg)
+ read(arg,*) delay !Watterson delay (ms)
+ call getarg(7,arg)
+ read(arg,*) nfiles !Number of files
+ call getarg(8,arg)
+ read(arg,*) snrdb !SNR_2500
+
+ bcontest=nfiles.lt.0
+ nfiles=abs(nfiles)
+ twopi=8.0*atan(1.0)
+ fs=12000.0 !Sample rate (Hz)
+ dt=1.0/fs !Sample interval (s)
+ tt=NSPS*dt !Duration of symbols (s)
+ baud=1.0/tt !Keying rate (baud)
+ bw=8*baud !Occupied bandwidth (Hz)
+ txt=NZ*dt !Transmission length (s)
+ bandwidth_ratio=2500.0/(fs/2.0)
+ sig=sqrt(2*bandwidth_ratio) * 10.0**(0.05*snrdb)
+ if(snrdb.gt.90.0) sig=1.0
+ txt=NN*NSPS/12000.0
+ i3bit=0 ! ### TEMPORARY ??? ###
+
+! Source-encode, then get itone()
+ call genft8(msg,mygrid6,bcontest,i3bit,msgsent,msgbits,itone)
+ write(*,1000) f0,xdt,txt,snrdb,bw,msgsent
+1000 format('f0:',f9.3,' DT:',f6.2,' TxT:',f6.1,' SNR:',f6.1, &
+ ' BW:',f4.1,2x,a22)
+
+write(*,'(28i1,1x,28i1)') msgbits(1:56)
+write(*,'(16i1)') msgbits(57:72)
+write(*,'(3i1)') msgbits(73:75)
+write(*,'(12i1)') msgbits(76:87)
+
+! call sgran()
+ do ifile=1,nfiles
+ c=0.
+ do isig=1,nsig
+ c0=0.
+ if(nsig.eq.25) then
+ f0=(isig+2)*100.0
+ endif
+ k=-1 + nint((xdt+0.5+0.01*gran())/dt)
+! k=-1 + nint((xdt+0.5)/dt)
+ phi=0.0
+ do j=1,NN !Generate complex waveform
+ dphi=twopi*(f0+itone(j)*baud)*dt
+ do i=1,NSPS
+ k=k+1
+ phi=mod(phi+dphi,twopi)
+ if(k.ge.0 .and. k.lt.NMAX) c0(k)=cmplx(cos(phi),sin(phi))
+ enddo
+ enddo
+ if(fspread.ne.0.0 .or. delay.ne.0.0) call watterson(c0,NMAX,fs,delay,fspread)
+ c=c+sig*c0
+ enddo
+ if(snrdb.lt.90) then
+ do i=0,NMAX-1 !Add gaussian noise at specified SNR
+ xnoise=gran()
+ ynoise=gran()
+ c(i)=c(i) + cmplx(xnoise,ynoise)
+ enddo
+ endif
+
+ fac=32767.0
+ rms=100.0
+ if(snrdb.ge.90.0) iwave(1:NMAX)=nint(fac*real(c))
+ if(snrdb.lt.90.0) iwave(1:NMAX)=nint(rms*real(c))
+
+ h=default_header(12000,NMAX)
+ write(fname,1102) ifile
+1102 format('000000_',i6.6,'.wav')
+ open(10,file=fname,status='unknown',access='stream')
+ write(10) h,iwave !Save to *.wav file
+ close(10)
+ write(*,1110) ifile,xdt,f0,snrdb,fname
+1110 format(i4,f7.2,f8.2,f7.1,2x,a17)
+ enddo
+
+999 end program ft8sim
diff --git a/lib/fsk4hf/genbpsk.f90 b/lib/fsk4hf/genbpsk.f90
new file mode 100644
index 00000000..6c283e72
--- /dev/null
+++ b/lib/fsk4hf/genbpsk.f90
@@ -0,0 +1,44 @@
+subroutine genbpsk(id,f00,ndiff,nref,c)
+
+ parameter (ND=121) !Data symbols: LDPC (120,60), r=1/2
+ parameter (NN=ND) !Total symbols (121)
+ parameter (NSPS=28800) !Samples per symbol at 12000 sps
+ parameter (NZ=NSPS*NN) !Samples in waveform (3456000)
+
+ complex c(0:NZ-1) !Complex waveform
+ real*8 twopi,dt,fs,baud,f0,dphi,phi
+ integer id(NN) !Encoded NRZ data (values +/-1)
+ integer ie(NN) !Differentially encoded data
+
+ f0=f00
+ twopi=8.d0*atan(1.d0)
+ fs=12000.d0
+ dt=1.0/fs
+ baud=1.d0/(NSPS*dt)
+
+ if(ndiff.ne.0) then
+ ie(1)=1 !First bit is always 1
+ do i=2,NN !Differentially encode
+ ie(i)=id(i)*ie(i-1)
+ enddo
+ endif
+
+! Generate the BPSK waveform
+ phi=0.d0
+ k=-1
+ do j=1,NN
+ dphi=twopi*f0*dt
+ x=id(j)
+ if(ndiff.ne.0) x=ie(j) !Differential
+ if(nref.ne.0) x=1.0 !Generate reference carrier
+ do i=1,NSPS
+ k=k+1
+ phi=phi+dphi
+ if(phi.gt.twopi) phi=phi-twopi
+ xphi=phi
+ c(k)=x*cmplx(cos(xphi),sin(xphi))
+ enddo
+ enddo
+
+ return
+end subroutine genbpsk
diff --git a/lib/fsk4hf/genfsk4.f90 b/lib/fsk4hf/genfsk4.f90
new file mode 100644
index 00000000..d8b0394b
--- /dev/null
+++ b/lib/fsk4hf/genfsk4.f90
@@ -0,0 +1,36 @@
+subroutine genfsk4(id,f00,nts,c)
+
+ parameter (ND=60) !Data symbols: LDPC (120,60), r=1/2
+ parameter (NN=ND) !Total symbols (60)
+ parameter (NSPS=57600) !Samples per symbol at 12000 sps
+ parameter (NZ=NSPS*NN) !Samples in waveform (3456000)
+ parameter (NFFT=NZ) !Full length FFT
+
+ complex c(0:NFFT-1) !Complex waveform
+ real*8 twopi,dt,fs,baud,f0,dphi,phi
+ integer id(NN) !Encoded 2-bit data (values 0-3)
+
+ f0=f00
+ twopi=8.d0*atan(1.d0)
+ fs=12000.d0
+ dt=1.0/fs
+ baud=1.d0/(NSPS*dt)
+
+! Generate the 4-FSK waveform
+ x=0.
+ c=0.
+ phi=0.d0
+ k=-1
+ do j=1,NN
+ dphi=twopi*(f0 + nts*id(j)*baud)*dt
+ do i=1,NSPS
+ k=k+1
+ phi=phi+dphi
+ if(phi.gt.twopi) phi=phi-twopi
+ xphi=phi
+ c(k)=cmplx(cos(xphi),sin(xphi))
+ enddo
+ enddo
+
+ return
+end subroutine genfsk4
diff --git a/lib/fsk4hf/genfsk4hf.f90 b/lib/fsk4hf/genfsk4hf.f90
new file mode 100644
index 00000000..924e9793
--- /dev/null
+++ b/lib/fsk4hf/genfsk4hf.f90
@@ -0,0 +1,51 @@
+subroutine genfsk4hf(msgbits,f0,id,c)
+
+ parameter (KK=84) !Information bits (72 + CRC12)
+ parameter (ND=84) !Data symbols: LDPC (168,84), r=1/2
+ parameter (NS=12) !Sync symbols (3 @ 4x4 Costas arrays)
+ parameter (NR=2) !Ramp up/down
+ parameter (NN=NR+NS+ND) !Total symbols (98)
+ parameter (NSPS=2688/84) !Samples per symbol (32)
+ parameter (NZ=NSPS*NN) !Samples in baseband waveform (3136)
+
+ complex c(0:NZ-1) !Complex waveform
+ integer id0(NN) !2-bit data (values 0-3), all symbols
+ integer id(ND) !2-bit data (values 0-3), data only
+ integer*1 msgbits(KK),codeword(2*ND)
+ integer icos4(4) !4x4 Costas array
+ data icos4/0,1,3,2/
+
+ twopi=8.0*atan(1.0)
+ fs=12000.0/84.0
+ dt=1.0/fs
+ baud=1.0/(NSPS*dt)
+ call encode168(msgbits,codeword) !Encode the test message
+ id0(1)=0 !Ramp-up
+ id0(2:5)=icos4 !First Costas array
+ id0(48:51)=icos4 !Second
+ id0(94:97)=icos4 !Third
+ id0(98)=0 !Ramp down
+ j=5
+ do i=1,84 !Data symbols
+ id(i)=2*codeword(2*i-1) + codeword(2*i)
+ j=j+1
+ if(i.eq.43) j=j+4
+ id0(j)=id(i)
+ enddo
+
+! Generate the 4-FSK waveform, low tone at f=0
+ c=0.
+ phi=0.d0
+ k=-1
+ do j=1,NN
+ dphi=twopi*(f0+id0(j)*baud)*dt
+ do i=1,NSPS
+ k=k+1
+ phi=phi+dphi
+ if(phi.gt.twopi) phi=phi-twopi
+ c(k)=cmplx(cos(phi),sin(phi))
+ enddo
+ enddo
+
+ return
+end subroutine genfsk4hf
diff --git a/lib/fsk4hf/genft8.f90 b/lib/fsk4hf/genft8.f90
new file mode 100644
index 00000000..899dad3e
--- /dev/null
+++ b/lib/fsk4hf/genft8.f90
@@ -0,0 +1,56 @@
+subroutine genft8(msg,mygrid,bcontest,i3bit,msgsent,msgbits,itone)
+
+! Encode an FT8 message, producing array itone().
+
+ use crc
+ use packjt
+ include 'ft8_params.f90'
+ character*22 msg,msgsent
+ character*6 mygrid
+ character*87 cbits
+ logical bcontest
+ integer*4 i4Msg6BitWords(12) !72-bit message as 6-bit words
+ integer*1 msgbits(KK),codeword(3*ND)
+ integer*1, target:: i1Msg8BitBytes(11)
+ integer itone(NN)
+ integer icos7(0:6)
+ data icos7/2,5,6,0,4,1,3/ !Costas 7x7 tone pattern
+
+ call packmsg(msg,i4Msg6BitWords,itype,bcontest) !Pack into 12 6-bit bytes
+ call unpackmsg(i4Msg6BitWords,msgsent,bcontest,mygrid) !Unpack to get msgsent
+
+ write(cbits,1000) i4Msg6BitWords,32*i3bit
+1000 format(12b6.6,b8.8)
+ read(cbits,1001) i1Msg8BitBytes(1:10)
+1001 format(10b8)
+ i1Msg8BitBytes(10)=iand(i1Msg8BitBytes(10),128+64+32)
+ i1Msg8BitBytes(11)=0
+ icrc12=crc12(c_loc(i1Msg8BitBytes),11)
+
+! For reference, here's how to check the CRC
+! i1Msg8BitBytes(10)=icrc12/256
+! i1Msg8BitBytes(11)=iand (icrc12,255)
+! checksumok = crc12_check(c_loc (i1Msg8BitBytes), 11)
+! if( checksumok ) write(*,*) 'Good checksum'
+
+ write(cbits,1003) i4Msg6BitWords,i3bit,icrc12
+1003 format(12b6.6,b3.3,b12.12)
+ read(cbits,1004) msgbits
+1004 format(87i1)
+
+ call encode174(msgbits,codeword) !Encode the test message
+
+! Message structure: S7 D29 S7 D29 S7
+ itone(1:7)=icos7
+ itone(36+1:36+7)=icos7
+ itone(NN-6:NN)=icos7
+ k=7
+ do j=1,ND
+ i=3*j -2
+ k=k+1
+ if(j.eq.30) k=k+7
+ itone(k)=codeword(i)*4 + codeword(i+1)*2 + codeword(i+2)
+ enddo
+
+ return
+end subroutine genft8
diff --git a/lib/fsk4hf/genft8refsig.f90 b/lib/fsk4hf/genft8refsig.f90
new file mode 100644
index 00000000..5bf4885c
--- /dev/null
+++ b/lib/fsk4hf/genft8refsig.f90
@@ -0,0 +1,22 @@
+subroutine genft8refsig(itone,cref,f0)
+ complex cref(79*1920)
+ integer itone(79)
+ real*8 twopi,phi,dphi,dt,xnsps
+ data twopi/0.d0/
+ save twopi
+ if( twopi .lt. 0.1 ) twopi=8.d0*atan(1.d0)
+
+ xnsps=1920.d0
+ dt=1.d0/12000.d0
+ phi=0.d0
+ k=1
+ do i=1,79
+ dphi=twopi*(f0*dt+itone(i)/xnsps)
+ do is=1,1920
+ cref(k)=cmplx(cos(phi),sin(phi))
+ phi=mod(phi+dphi,twopi)
+ k=k+1
+ enddo
+ enddo
+ return
+end subroutine genft8refsig
diff --git a/lib/fsk4hf/genmskhf.f90 b/lib/fsk4hf/genmskhf.f90
new file mode 100644
index 00000000..7d4b0838
--- /dev/null
+++ b/lib/fsk4hf/genmskhf.f90
@@ -0,0 +1,126 @@
+subroutine genmskhf(msgbits,id,icw,cbb,csync)
+
+!Encode an MSK-HF message, produce baseband waveform and sync vector.
+
+ parameter (KK=84) !Information bits (72 + CRC12)
+ parameter (ND=168) !Data symbols: LDPC (168,84), r=1/2
+ parameter (NS=65) !Sync symbols (2 x 26 + Barker 13)
+ parameter (NR=3) !Ramp up/down
+ parameter (NN=NR+NS+ND) !Total symbols (236)
+ parameter (NSPS=16) !Samples per MSK symbol (16)
+ parameter (N2=2*NSPS) !Samples per OQPSK symbol (32)
+ parameter (NZ=NSPS*NN) !Samples in baseband waveform (3760)
+
+ complex cbb(0:NZ-1)
+ complex csync(0:NZ-1)
+ real x(0:NZ-1)
+ real y(0:NZ-1)
+ real pp(N2)
+ logical first
+ integer*1 msgbits(KK),codeword(ND)
+ integer icw(ND)
+ integer id(NS+ND)
+ integer isync(26) !Long sync vector
+ integer ib13(13) !Barker 13 code
+ data ib13/1,1,1,1,1,-1,-1,1,1,-1,1,-1,1/
+ data first/.true./
+ save first,isync,twopi,pp
+
+ if(first) then
+ n=z'2c1aeb1'
+ do i=1,26
+ isync(i)=-1
+ if(iand(n,1).eq.1) isync(i)=1
+ n=n/2
+ enddo
+
+ twopi=8.0*atan(1.0)
+ do i=1,N2 !Half-sine shaped pulse
+ pp(i)=sin(0.5*(i-1)*twopi/N2)
+ enddo
+ first=.false.
+ endif
+
+ call encode168(msgbits,codeword) !Encode the test message
+ icw=2*codeword - 1
+
+! Message structure: R1 26*(S1+D1) S13 26*(D1+S1) R1
+! Generate QPSK without any offset; then shift the y array to get OQPSK.
+
+! Do the I channel first: results in array x
+ n=0
+ k=0
+ ia=0
+ ib=NSPS-1
+ x(ia:ib)=0. !Ramp up (half-symbol; shape TBD)
+ do j=1,26 !Insert group of 26*(S1+D1)
+ ia=ib+1
+ ib=ia+N2-1
+ n=n+1
+ id(n)=2*isync(j)
+ x(ia:ib)=isync(j)*pp !Insert Sync bit
+ ia=ib+1
+ ib=ia+N2-1
+ k=k+1
+ n=n+1
+ id(n)=icw(k)
+ x(ia:ib)=id(n)*pp !Insert data bit
+ enddo
+
+ do j=1,13 !Insert Barker 13 code
+ ia=ib+1
+ ib=ia+N2-1
+ n=n+1
+ id(n)=2*ib13(j)
+ x(ia:ib)=ib13(j)*pp
+ enddo
+
+ do j=1,26 !Insert group of 26*(S1+D1)
+ ia=ib+1
+ ib=ia+N2-1
+ k=k+1
+ n=n+1
+ id(n)=icw(k)
+ x(ia:ib)=id(n)*pp !Insert data bit
+ ia=ib+1
+ ib=ia+N2-1
+ n=n+1
+ id(n)=2*isync(j)
+ x(ia:ib)=isync(j)*pp !Insert Sync bit
+ enddo
+ ia=ib+1
+ ib=ia+NSPS-1
+ x(ia:ib)=0. !Ramp down (half-symbol; shape TBD)
+
+! Now do the Q channel: results in array y
+ ia=0
+ ib=NSPS-1
+ y(ia:ib)=0. !Ramp up (half-symbol; shape TBD)
+ do j=1,116
+ ia=ib+1
+ ib=ia+N2-1
+ k=k+1
+ n=n+1
+ id(n)=icw(k)
+ y(ia:ib)=id(n)*pp
+ enddo
+ ia=ib+1
+ ib=ia+NSPS-1
+ y(ia:ib)=0. !Ramp down (half-symbol; shape TBD)
+ y=cshift(y,-NSPS) !Shift Q array to get OQPSK
+ cbb=cmplx(x,y) !Complex baseband waveform
+
+ ib=NSPS-1
+ ib2=NSPS-1+64*N2
+ do j=1,26 !Zero all data symbols in x
+ ia=ib+1+N2
+ ib=ia+N2-1
+ x(ia:ib)=0.
+ ia2=ib2+1+N2
+ ib2=ia2+N2-1
+ x(ia2:ib2)=0.
+ enddo
+ csync=x
+
+ return
+end subroutine genmskhf
diff --git a/lib/fsk4hf/genwspr5.f90 b/lib/fsk4hf/genwspr5.f90
new file mode 100644
index 00000000..9385a8e1
--- /dev/null
+++ b/lib/fsk4hf/genwspr5.f90
@@ -0,0 +1,107 @@
+subroutine genwspr5(msg,msgsent,itone)
+
+! Encode a WSPR-LF message, producing array itone().
+
+ use crc
+ include 'wsprlf_params.f90'
+
+ character*22 msg,msgsent
+ character*60 cbits
+ integer*1,target :: idat(9)
+ integer*1 msgbits(KK),codeword(ND)
+ logical first
+ integer icw(ND)
+ integer id(NS+ND)
+ integer jd(NS+ND)
+ integer isync(48) !Long sync vector
+ integer ib13(13) !Barker 13 code
+ integer itone(NN)
+ integer*8 n8
+ data ib13/1,1,1,1,1,-1,-1,1,1,-1,1,-1,1/
+ data first/.true./
+ save first,isync
+
+ if(first) then
+ n8=z'cbf089223a51'
+ do i=1,48
+ isync(i)=-1
+ if(iand(n8,1).eq.1) isync(i)=1
+ n8=n8/2
+ enddo
+ first=.false.
+ endif
+
+ idat=0
+ call wqencode(msg,ntype0,idat) !Source encoding
+ id7=idat(7)
+ if(id7.lt.0) id7=id7+256
+ id7=id7/64
+ icrc=crc10(c_loc(idat),9) !Compute the 10-bit CRC
+ idat(8)=icrc/256 !Insert CRC into idat(8:9)
+ idat(9)=iand(icrc,255)
+ call wqdecode(idat,msgsent,itype)
+
+ write(cbits,1004) idat(1:6),id7,icrc
+1004 format(6b8.8,b2.2,b10.10)
+ read(cbits,1006) msgbits
+1006 format(60i1)
+
+! call chkcrc10(msgbits,nbadcrc)
+! print*,msgsent,itype,crc10_check(c_loc(idat),9),nbadcrc
+
+ call encode300(msgbits,codeword) !Encode the test message
+ icw=2*codeword - 1 !NRZ codeword
+
+! Message structure:
+! I channel: R1 48*(S1+D1) S13 48*(D1+S1) R1
+! Q channel: R1 D109 R1
+! Generate QPSK with no offset, then shift the y array to get OQPSK.
+
+! I channel:
+ n=0
+ k=0
+ do j=1,48 !Insert group of 48*(S1+D1)
+ n=n+1
+ id(n)=2*isync(j)
+ k=k+1
+ n=n+1
+ id(n)=icw(k)
+ enddo
+
+ do j=1,13 !Insert Barker 13 code
+ n=n+1
+ id(n)=2*ib13(j)
+ enddo
+
+ do j=1,48 !Insert group of 48*(S1+D1)
+ k=k+1
+ n=n+1
+ id(n)=icw(k)
+ n=n+1
+ id(n)=2*isync(j)
+ enddo
+
+! Q channel
+ do j=1,204
+ k=k+1
+ n=n+1
+ id(n)=icw(k)
+ enddo
+
+! Map I and Q to tones.
+ n=0
+ jz=(NS+ND+1)/2
+ do j=1,jz-1
+ jd(2*j-1)=id(j)/abs(id(j))
+ jd(2*j)=id(j+jz)/abs(id(j+jz))
+ enddo
+ jd(NS+ND)=id(jz)/abs(id(jz))
+ itone=0
+ do j=1,jz-1
+ itone(2*j+1)=(jd(2*j)*jd(2*j-1)+1)/2;
+ itone(2*j+2)=-(jd(2*j)*jd(2*j+1)-1)/2;
+ enddo
+ itone(NS+ND+2)=jd(NS+ND) !### Is this correct ??? ###
+
+ return
+end subroutine genwspr5
diff --git a/lib/fsk4hf/genwspr_fsk8.f90 b/lib/fsk4hf/genwspr_fsk8.f90
new file mode 100644
index 00000000..2ef6ace3
--- /dev/null
+++ b/lib/fsk4hf/genwspr_fsk8.f90
@@ -0,0 +1,45 @@
+subroutine genwspr_fsk8(msg,msgsent,itone)
+
+! Encode a WSPR-LF 8-FSK message, producing array itone().
+
+ use crc
+ include 'wspr_fsk8_params.f90'
+
+ character*22 msg,msgsent
+ character*60 cbits
+ integer*1,target :: idat(9)
+ integer*1 msgbits(KK),codeword(3*ND)
+ integer itone(NN)
+ integer icos7(0:6)
+ data icos7/2,5,6,0,4,1,3/ !Costas 7x7 tone pattern
+
+ idat=0
+ call wqencode(msg,ntype0,idat) !Source encoding
+ id7=idat(7)
+ if(id7.lt.0) id7=id7+256
+ id7=id7/64
+ icrc=crc10(c_loc(idat),9) !Compute the 10-bit CRC
+ idat(8)=icrc/256 !Insert CRC into idat(8:9)
+ idat(9)=iand(icrc,255)
+ call wqdecode(idat,msgsent,itype)
+
+ write(cbits,1004) idat(1:6),id7,icrc
+1004 format(6b8.8,b2.2,b10.10)
+ read(cbits,1006) msgbits
+1006 format(60i1)
+
+! call chkcrc10(msgbits,nbadcrc)
+! print*,msgsent,itype,crc10_check(c_loc(idat),9),nbadcrc
+
+ call encode300(msgbits,codeword) !Encode the test message
+
+! Message structure: S7 D100 S7
+ itone(1:7)=icos7
+ itone(NN-6:NN)=icos7
+ do j=1,ND
+ i=3*j -2
+ itone(j+7)=codeword(i)*4 + codeword(i+1)*2 + codeword(i+2)
+ enddo
+
+ return
+end subroutine genwspr_fsk8
diff --git a/lib/fsk4hf/genwsprlf.f90 b/lib/fsk4hf/genwsprlf.f90
new file mode 100644
index 00000000..4c5b31cf
--- /dev/null
+++ b/lib/fsk4hf/genwsprlf.f90
@@ -0,0 +1,137 @@
+subroutine genwsprlf(msgbits,id,icw,cbb,csync,itone)
+
+!Encode a WSPR-LF message, produce baseband waveform and sync vector.
+
+ include 'wsprlf_params.f90'
+
+ complex cbb(0:NZ-1)
+ complex csync(0:NZ-1)
+ real x(0:NZ-1)
+ real y(0:NZ-1)
+ real pp(N2)
+ logical first
+ integer*1 msgbits(KK),codeword(ND)
+ integer icw(ND)
+ integer id(NS+ND)
+ integer jd(NS+ND)
+ integer isync(48) !Long sync vector
+ integer ib13(13) !Barker 13 code
+ integer itone(NN)
+ integer*8 n8
+ data ib13/1,1,1,1,1,-1,-1,1,1,-1,1,-1,1/
+ data first/.true./
+ save first,isync,twopi,pp
+
+ if(first) then
+ n8=z'cbf089223a51'
+ do i=1,48
+ isync(i)=-1
+ if(iand(n8,1).eq.1) isync(i)=1
+ n8=n8/2
+ enddo
+
+ twopi=8.0*atan(1.0)
+ do i=1,N2 !Half-sine shaped pulse
+ pp(i)=sin(0.5*(i-1)*twopi/N2)
+ enddo
+ first=.false.
+ endif
+
+ call encode300(msgbits,codeword) !Encode the test message
+ icw=2*codeword - 1
+
+! Message structure: R1 48*(S1+D1) S13 48*(D1+S1) R1
+! Generate QPSK without any offset; then shift the y array to get OQPSK.
+
+! Do the I channel first: results in array x
+ n=0
+ k=0
+ ia=0
+ ib=NSPS-1
+ x(ia:ib)=0. !Ramp up (half-symbol; shape TBD)
+ do j=1,48 !Insert group of 48*(S1+D1)
+ ia=ib+1
+ ib=ia+N2-1
+ n=n+1
+ id(n)=2*isync(j)
+ x(ia:ib)=isync(j)*pp !Insert Sync bit
+ ia=ib+1
+ ib=ia+N2-1
+ k=k+1
+ n=n+1
+ id(n)=icw(k)
+ x(ia:ib)=id(n)*pp !Insert data bit
+ enddo
+
+ do j=1,13 !Insert Barker 13 code
+ ia=ib+1
+ ib=ia+N2-1
+ n=n+1
+ id(n)=2*ib13(j)
+ x(ia:ib)=ib13(j)*pp
+ enddo
+
+ do j=1,48 !Insert group of 48*(S1+D1)
+ ia=ib+1
+ ib=ia+N2-1
+ k=k+1
+ n=n+1
+ id(n)=icw(k)
+ x(ia:ib)=id(n)*pp !Insert data bit
+ ia=ib+1
+ ib=ia+N2-1
+ n=n+1
+ id(n)=2*isync(j)
+ x(ia:ib)=isync(j)*pp !Insert Sync bit
+ enddo
+ ia=ib+1
+ ib=ia+NSPS-1
+ x(ia:ib)=0. !Ramp down (half-symbol; shape TBD)
+
+! Now do the Q channel: results in array y
+ ia=0
+ ib=NSPS-1
+ y(ia:ib)=0. !Ramp up (half-symbol; shape TBD)
+ do j=1,204
+ ia=ib+1
+ ib=ia+N2-1
+ k=k+1
+ n=n+1
+ id(n)=icw(k)
+ y(ia:ib)=id(n)*pp
+ enddo
+ ia=ib+1
+ ib=ia+NSPS-1
+ y(ia:ib)=0. !Ramp down (half-symbol; shape TBD)
+ y=cshift(y,-NSPS) !Shift Q array to get OQPSK
+ cbb=cmplx(x,y) !Complex baseband waveform
+
+ ib=NSPS-1
+ ib2=NSPS-1+64*N2
+ do j=1,48 !Zero all data symbols in x
+ ia=ib+1+N2
+ ib=ia+N2-1
+ x(ia:ib)=0.
+ ia2=ib2+1+N2
+ ib2=ia2+N2-1
+ x(ia2:ib2)=0.
+ enddo
+ csync=x
+
+! Map I and Q to tones.
+ n=0
+ jz=(NS+ND+1)/2
+ do j=1,jz-1
+ jd(2*j-1)=id(j)/abs(id(j))
+ jd(2*j)=id(j+jz)/abs(id(j+jz))
+ enddo
+ jd(NS+ND)=id(jz)/abs(id(jz))
+ itone=0
+ do j=1,jz-1
+ itone(2*j-1)=(jd(2*j)*jd(2*j-1)+1)/2;
+ itone(2*j)=-(jd(2*j)*jd(2*j+1)-1)/2;
+ enddo
+ itone(NS+ND)=jd(NS+ND) !### Is this correct ??? ###
+
+ return
+end subroutine genwsprlf
diff --git a/lib/fsk4hf/getfc1.f90 b/lib/fsk4hf/getfc1.f90
new file mode 100644
index 00000000..b82b0303
--- /dev/null
+++ b/lib/fsk4hf/getfc1.f90
@@ -0,0 +1,58 @@
+subroutine getfc1(c,fc1)
+
+ parameter (KK=84) !Information bits (72 + CRC12)
+ parameter (ND=168) !Data symbols: LDPC (168,84), r=1/2
+ parameter (NS=65) !Sync symbols (2 x 26 + Barker 13)
+ parameter (NR=3) !Ramp up/down
+ parameter (NN=NR+NS+ND) !Total symbols (236)
+ parameter (NSPS=16) !Samples per MSK symbol (16)
+ parameter (N2=2*NSPS) !Samples per OQPSK symbol (32)
+ parameter (N13=13*N2) !Samples in central sync vector (416)
+ parameter (NZ=NSPS*NN) !Samples in baseband waveform (3760)
+ parameter (NFFT1=4*NSPS,NH1=NFFT1/2)
+
+ complex c(0:NZ-1) !Complex waveform
+ complex c2(0:NFFT1-1) !Short spectra
+ real s(-NH1+1:NH1) !Coarse spectrum
+
+ nspec=NZ/NFFT1
+ fs=12000.0/72.0
+ df1=fs/NFFT1
+ s=0.
+ do k=1,nspec
+ ia=(k-1)*N2
+ ib=ia+N2-1
+ c2(0:N2-1)=c(ia:ib)
+ c2(N2:)=0.
+ call four2a(c2,NFFT1,1,-1,1)
+ do i=0,NFFT1-1
+ j=i
+ if(j.gt.NH1) j=j-NFFT1
+ s(j)=s(j) + real(c2(i))**2 + aimag(c2(i))**2
+ enddo
+ enddo
+! call smo121(s,NFFT1)
+ smax=0.
+ ipk=0
+ fc1=0.
+ ia=nint(40.0/df1)
+ do i=-ia,ia
+ f=i*df1
+ if(s(i).gt.smax) then
+ smax=s(i)
+ ipk=i
+ fc1=f
+ endif
+! write(51,3001) f,s(i),db(s(i))
+! 3001 format(f10.3,e12.3,f10.3)
+ enddo
+
+! The following is for testing SNR calibration:
+! sp3n=(s(ipk-1)+s(ipk)+s(ipk+1)) !Sig + 3*noise
+! base=(sum(s)-sp3n)/(NFFT1-3.0) !Noise per bin
+! psig=sp3n-3*base !Sig only
+! pnoise=(2500.0/df1)*base !Noise in 2500 Hz
+! xsnrdb=db(psig/pnoise)
+
+ return
+end subroutine getfc1
diff --git a/lib/fsk4hf/getfc1w.f90 b/lib/fsk4hf/getfc1w.f90
new file mode 100644
index 00000000..532b9837
--- /dev/null
+++ b/lib/fsk4hf/getfc1w.f90
@@ -0,0 +1,47 @@
+subroutine getfc1w(c,fs,fa,fb,fc1,xsnr)
+
+ include 'wsprlf_params.f90'
+ complex c(0:NZ-1) !Complex waveform
+ complex c2(0:NFFT1-1) !Short spectra
+ real s(-NH1+1:NH1) !Coarse spectrum
+ nspec=NZ/NFFT1
+ df1=fs/NFFT1
+ s=0.
+ do k=1,nspec
+ ia=(k-1)*N2
+ ib=ia+N2-1
+ c2(0:N2-1)=c(ia:ib)
+ c2(N2:)=0.
+ call four2a(c2,NFFT1,1,-1,1)
+ do i=0,NFFT1-1
+ j=i
+ if(j.gt.NH1) j=j-NFFT1
+ s(j)=s(j) + real(c2(i))**2 + aimag(c2(i))**2
+ enddo
+ enddo
+! call smo121(s,NFFT1)
+ smax=0.
+ ipk=0
+ fc1=0.
+ ia=nint(fa/df1)
+ ib=nint(fb/df1)
+ do i=ia,ib
+ f=i*df1
+ if(s(i).gt.smax) then
+ smax=s(i)
+ ipk=i
+ fc1=f
+ endif
+! write(51,3001) f,s(i),db(s(i))
+! 3001 format(f10.3,e12.3,f10.3)
+ enddo
+
+! The following is for testing SNR calibration:
+ sp3n=(s(ipk-1)+s(ipk)+s(ipk+1)) !Sig + 3*noise
+ base=(sum(s)-sp3n)/(NFFT1-3.0) !Noise per bin
+ psig=sp3n-3*base !Sig only
+ pnoise=(2500.0/df1)*base !Noise in 2500 Hz
+ xsnr=db(psig/pnoise)
+
+ return
+end subroutine getfc1w
diff --git a/lib/fsk4hf/getfc2.f90 b/lib/fsk4hf/getfc2.f90
new file mode 100644
index 00000000..9506168d
--- /dev/null
+++ b/lib/fsk4hf/getfc2.f90
@@ -0,0 +1,74 @@
+subroutine getfc2(c,csync,fc1,fc2,fc3)
+
+ parameter (KK=84) !Information bits (72 + CRC12)
+ parameter (ND=168) !Data symbols: LDPC (168,84), r=1/2
+ parameter (NS=65) !Sync symbols (2 x 26 + Barker 13)
+ parameter (NR=3) !Ramp up/down
+ parameter (NN=NR+NS+ND) !Total symbols (236)
+ parameter (NSPS=16) !Samples per MSK symbol (16)
+ parameter (N2=2*NSPS) !Samples per OQPSK symbol (32)
+ parameter (N13=13*N2) !Samples in central sync vector (416)
+ parameter (NZ=NSPS*NN) !Samples in baseband waveform (3760)
+ parameter (NFFT1=4*NSPS,NH1=NFFT1/2)
+
+ complex c(0:NZ-1) !Complex waveform
+ complex cs(0:NZ-1) !For computing spectrum
+ complex csync(0:NZ-1) !Sync symbols only, from cbb
+ real a(5)
+
+ fs=12000.0/72.0
+ df=fs/NZ
+ baud=fs/NSPS
+ a(1)=-fc1
+ a(2:5)=0.
+ call twkfreq1(c,NZ,fs,a,cs) !Mix down by fc1
+
+! Filter, square, then FFT to get refined carrier frequency fc2.
+ call four2a(cs,NZ,1,-1,1) !To freq domain
+ ia=nint(0.75*baud/df)
+ cs(ia:NZ-1-ia)=0. !Save only freqs around fc1
+ call four2a(cs,NZ,1,1,1) !Back to time domain
+ cs=cs/NZ
+ cs=cs*cs !Square the data
+ call four2a(cs,NZ,1,-1,1) !Compute squared spectrum
+
+! Find two peaks separated by baud
+ pmax=0.
+ fc2=0.
+ ic=nint(baud/df)
+ ja=nint(0.5*baud/df)
+ do j=-ja,ja
+ f2=j*df
+ ia=nint((f2-0.5*baud)/df)
+ if(ia.lt.0) ia=ia+NZ
+ ib=nint((f2+0.5*baud)/df)
+ p=real(cs(ia))**2 + aimag(cs(ia))**2 + &
+ real(cs(ib))**2 + aimag(cs(ib))**2
+ if(p.gt.pmax) then
+ pmax=p
+ fc2=0.5*f2
+ endif
+! write(52,1200) f2,p,db(p)
+!1200 format(f10.3,2f15.3)
+ enddo
+
+ a(1)=-fc1
+ a(2:5)=0.
+ call twkfreq1(c,NZ,fs,a,cs) !Mix down by fc1
+ cs=cs*conjg(csync)
+ call four2a(cs,NZ,1,-1,1) !To freq domain
+ pmax=0.
+ do i=0,NZ-1
+ f=i*df
+ if(i.gt.NZ/2) f=(i-NZ)*df
+ p=real(cs(i))**2 + aimag(cs(i))**2
+! write(51,3001) f,p,db(p)
+!3001 format(f10.3,e12.3,f10.3)
+ if(p.gt.pmax) then
+ pmax=p
+ fc3=f
+ endif
+ enddo
+
+ return
+end subroutine getfc2
diff --git a/lib/fsk4hf/getfc2w.f90 b/lib/fsk4hf/getfc2w.f90
new file mode 100644
index 00000000..0d100ee5
--- /dev/null
+++ b/lib/fsk4hf/getfc2w.f90
@@ -0,0 +1,82 @@
+subroutine getfc2w(c,csync,npeaks,fs,fc1,fpks)
+
+ include 'wsprlf_params.f90'
+
+ complex c(0:NZ-1) !Complex waveform
+ complex cs(0:NZ-1) !For computing spectrum
+ complex csync(0:NZ-1) !Sync symbols only, from cbb
+ real a(5)
+ real freqs(413),sp2(413),fpks(npeaks)
+ integer pkloc(1)
+
+ df=fs/NZ
+ baud=fs/NSPS
+ a(1)=-fc1
+ a(2:5)=0.
+ call twkfreq1(c,NZ,fs,a,cs) !Mix down by fc1
+
+! Filter, square, then FFT to get refined carrier frequency fc2.
+ call four2a(cs,NZ,1,-1,1) !To freq domain
+
+ ia=nint(0.75*baud/df)
+ cs(ia:NZ-1-ia)=0. !Save only freqs around fc1
+ call four2a(cs,NZ,1,1,1) !Back to time domain
+ cs=cs/NZ
+ cs=cs*cs !Square the data
+ call four2a(cs,NZ,1,-1,1) !Compute squared spectrum
+
+! Find two peaks separated by baud
+ pmax=0.
+ fc2=0.
+ ja=nint(0.3*baud/df)
+ k=1
+ do j=-ja,ja
+ f2=j*df
+ ia=nint((f2-0.5*baud)/df)
+ if(ia.lt.0) ia=ia+NZ
+ ib=nint((f2+0.5*baud)/df)
+ p=real(cs(ia))**2 + aimag(cs(ia))**2 + &
+ real(cs(ib))**2 + aimag(cs(ib))**2
+ if(p.gt.pmax) then
+ pmax=p
+ fc2=0.5*f2
+ endif
+ freqs(k)=0.5*f2
+ sp2(k)=p
+ k=k+1
+! write(52,1200) f2,p,db(p)
+!1200 format(f10.3,2f15.3)
+ enddo
+
+ do i=1,npeaks
+ pkloc=maxloc(sp2)
+ ipk=pkloc(1)
+ fpks(i)=freqs(ipk)
+ ipk0=max(1,ipk-1)
+ ipk1=min(413,ipk+1)
+! ipk0=ipk
+! ipk1=ipk
+ sp2(ipk0:ipk1)=0.0
+!write(*,*) i,fpks(i),fc2
+ enddo
+
+ a(1)=-fc1
+ a(2:5)=0.
+ call twkfreq1(c,NZ,fs,a,cs) !Mix down by fc1
+ cs=cs*conjg(csync)
+ call four2a(cs,NZ,1,-1,1) !To freq domain
+ pmax=0.
+ do i=0,NZ-1
+ f=i*df
+ if(i.gt.NZ/2) f=(i-NZ)*df
+ p=real(cs(i))**2 + aimag(cs(i))**2
+! write(51,3001) f,p,db(p)
+!3001 format(f10.3,e12.3,f10.3)
+ if(p.gt.pmax) then
+ pmax=p
+ fc3=f
+ endif
+ enddo
+
+ return
+end subroutine getfc2w
diff --git a/lib/fsk4hf/gran.c b/lib/fsk4hf/gran.c
new file mode 100644
index 00000000..24b98650
--- /dev/null
+++ b/lib/fsk4hf/gran.c
@@ -0,0 +1,28 @@
+#include <stdlib.h>
+#include <math.h>
+
+/* Generate gaussian random float with mean=0 and std_dev=1 */
+float gran_()
+{
+ float fac,rsq,v1,v2;
+ static float gset;
+ static int iset;
+
+ if(iset){
+ /* Already got one */
+ iset = 0;
+ return gset;
+ }
+ /* Generate two evenly distributed numbers between -1 and +1
+ * that are inside the unit circle
+ */
+ do {
+ v1 = 2.0 * (float)rand() / RAND_MAX - 1;
+ v2 = 2.0 * (float)rand() / RAND_MAX - 1;
+ rsq = v1*v1 + v2*v2;
+ } while(rsq >= 1.0 || rsq == 0.0);
+ fac = sqrt(-2.0*log(rsq)/rsq);
+ gset = v1*fac;
+ iset++;
+ return v2*fac;
+}
diff --git a/lib/fsk4hf/ldpc_174_87_params.f90 b/lib/fsk4hf/ldpc_174_87_params.f90
new file mode 100644
index 00000000..35af9b81
--- /dev/null
+++ b/lib/fsk4hf/ldpc_174_87_params.f90
@@ -0,0 +1,102 @@
+integer, parameter:: N=174, K=87, M=N-K
+character*22 g(87)
+integer colorder(N)
+data g/ & !parity generator matrix for (174,87) code
+"23bba830e23b6b6f50982e", &
+"1f8e55da218c5df3309052", &
+"ca7b3217cd92bd59a5ae20", &
+"56f78313537d0f4382964e", &
+"29c29dba9c545e267762fe", &
+"6be396b5e2e819e373340c", &
+"293548a138858328af4210", &
+"cb6c6afcdc28bb3f7c6e86", &
+"3f2a86f5c5bd225c961150", &
+"849dd2d63673481860f62c", &
+"56cdaec6e7ae14b43feeee", &
+"04ef5cfa3766ba778f45a4", &
+"c525ae4bd4f627320a3974", &
+"fe37802941d66dde02b99c", &
+"41fd9520b2e4abeb2f989c", &
+"40907b01280f03c0323946", &
+"7fb36c24085a34d8c1dbc4", &
+"40fc3e44bb7d2bb2756e44", &
+"d38ab0a1d2e52a8ec3bc76", &
+"3d0f929ef3949bd84d4734", &
+"45d3814f504064f80549ae", &
+"f14dbf263825d0bd04b05e", &
+"f08a91fb2e1f78290619a8", &
+"7a8dec79a51e8ac5388022", &
+"ca4186dd44c3121565cf5c", &
+"db714f8f64e8ac7af1a76e", &
+"8d0274de71e7c1a8055eb0", &
+"51f81573dd4049b082de14", &
+"d037db825175d851f3af00", &
+"d8f937f31822e57c562370", &
+"1bf1490607c54032660ede", &
+"1616d78018d0b4745ca0f2", &
+"a9fa8e50bcb032c85e3304", &
+"83f640f1a48a8ebc0443ea", &
+"eca9afa0f6b01d92305edc", &
+"3776af54ccfbae916afde6", &
+"6abb212d9739dfc02580f2", &
+"05209a0abb530b9e7e34b0", &
+"612f63acc025b6ab476f7c", &
+"0af7723161ec223080be86", &
+"a8fc906976c35669e79ce0", &
+"45b7ab6242b77474d9f11a", &
+"b274db8abd3c6f396ea356", &
+"9059dfa2bb20ef7ef73ad4", &
+"3d188ea477f6fa41317a4e", &
+"8d9071b7e7a6a2eed6965e", &
+"a377253773ea678367c3f6", &
+"ecbd7c73b9cd34c3720c8a", &
+"b6537f417e61d1a7085336", &
+"6c280d2a0523d9c4bc5946", &
+"d36d662a69ae24b74dcbd8", &
+"d747bfc5fd65ef70fbd9bc", &
+"a9fa2eefa6f8796a355772", &
+"cc9da55fe046d0cb3a770c", &
+"f6ad4824b87c80ebfce466", &
+"cc6de59755420925f90ed2", &
+"164cc861bdd803c547f2ac", &
+"c0fc3ec4fb7d2bb2756644", &
+"0dbd816fba1543f721dc72", &
+"a0c0033a52ab6299802fd2", &
+"bf4f56e073271f6ab4bf80", &
+"57da6d13cb96a7689b2790", &
+"81cfc6f18c35b1e1f17114", &
+"481a2a0df8a23583f82d6c", &
+"1ac4672b549cd6dba79bcc", &
+"c87af9a5d5206abca532a8", &
+"97d4169cb33e7435718d90", &
+"a6573f3dc8b16c9d19f746", &
+"2c4142bf42b01e71076acc", &
+"081c29a10d468ccdbcecb6", &
+"5b0f7742bca86b8012609a", &
+"012dee2198eba82b19a1da", &
+"f1627701a2d692fd9449e6", &
+"35ad3fb0faeb5f1b0c30dc", &
+"b1ca4ea2e3d173bad4379c", &
+"37d8e0af9258b9e8c5f9b2", &
+"cd921fdf59e882683763f6", &
+"6114e08483043fd3f38a8a", &
+"2e547dd7a05f6597aac516", &
+"95e45ecd0135aca9d6e6ae", &
+"b33ec97be83ce413f9acc8", &
+"c8b5dffc335095dcdcaf2a", &
+"3dd01a59d86310743ec752", &
+"14cd0f642fc0c5fe3a65ca", &
+"3a0a1dfd7eee29c2e827e0", &
+"8abdb889efbe39a510a118", &
+"3f231f212055371cf3e2a2"/
+data colorder/ &
+ 0, 1, 2, 3, 30, 4, 5, 6, 7, 8, 9, 10, 11, 32, 12, 40, 13, 14, 15, 16,&
+ 17, 18, 37, 45, 29, 19, 20, 21, 41, 22, 42, 31, 33, 34, 44, 35, 47, 51, 50, 43,&
+ 36, 52, 63, 46, 25, 55, 27, 24, 23, 53, 39, 49, 59, 38, 48, 61, 60, 57, 28, 62,&
+ 56, 58, 65, 66, 26, 70, 64, 69, 68, 67, 74, 71, 54, 76, 72, 75, 78, 77, 80, 79,&
+ 73, 83, 84, 81, 82, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,&
+ 100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,&
+ 120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,&
+ 140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,&
+ 160,161,162,163,164,165,166,167,168,169,170,171,172,173/
+
diff --git a/lib/fsk4hf/ldpc_300_60_params.f90 b/lib/fsk4hf/ldpc_300_60_params.f90
new file mode 100644
index 00000000..53ee994e
--- /dev/null
+++ b/lib/fsk4hf/ldpc_300_60_params.f90
@@ -0,0 +1,262 @@
+integer, parameter:: N=300, K=60, M=N-K
+character*15 g(M)
+integer colorder(N)
+data g/ &
+ "316fd3bb18bcefd", &
+ "a9c1c984f91244e", &
+ "9e04bd3d5d78d89", &
+ "f81617089621bd4", &
+ "12997ce2f44dbf4", &
+ "3ebddaf9b0fa1fc", &
+ "d0c114b0b0ef162", &
+ "f8c4f115f98bd92", &
+ "d0a79c0c5b8ca19", &
+ "477f6712f357b3b", &
+ "fa28b2444a7e66b", &
+ "bedcd4df8d95c64", &
+ "da30de73e57022c", &
+ "bc099bbb90fe09e", &
+ "cffc1e47e5708e8", &
+ "713d808563ca9a3", &
+ "70fcf1741d5d5d7", &
+ "32e80bc15112008", &
+ "804cef4df9b18ec", &
+ "3736881819d1033", &
+ "f4e37db7f9c5efe", &
+ "9e84b93d4d78d09", &
+ "2250c3518ec830a", &
+ "55a529a92e18021", &
+ "1cb80b14c9f6eae", &
+ "80c504b031ef926", &
+ "ece6636d0ac9c6d", &
+ "5d50a1690782cd0", &
+ "3d54a1fb30937a2", &
+ "ba8fe8006318041", &
+ "02917ce2fc45bf4", &
+ "abc1d984f95a44e", &
+ "fc05b4c4ab2d850", &
+ "467f7718f357b3b", &
+ "472cc094546c6b2", &
+ "fcdd94cf8c9cc64", &
+ "4dbc1647e970cc8", &
+ "6caa465c442aed1", &
+ "aead5af8b0da1be", &
+ "d8e1fa45a2e8431", &
+ "9d4dc4cc63abb7f", &
+ "9b2df6b48264637", &
+ "7335808563ca3a3", &
+ "36bf8d5cd93e6cc", &
+ "004ccf4db9b08ec", &
+ "90a71c8c598ca19", &
+ "f8c5d115f90bc92", &
+ "b95546c4e3f7934", &
+ "7d50a1690786cd0", &
+ "c90939921a0d7c6", &
+ "d0c504b030ef126", &
+ "ce3e6f9396fc542", &
+ "a0072a59f3707f5", &
+ "532d0a8fe3da1ea", &
+ "68b9e5cd7d142db", &
+ "fedc94df8c9dc64", &
+ "6da2465c448aed0", &
+ "3574aa19cb273c0", &
+ "1e54768c6bc6843", &
+ "691f65654498186", &
+ "fe2c92444a6ef6b", &
+ "9caad933e038cc4", &
+ "ad4e6f4defb28ec", &
+ "4f3d80947c6d2b2", &
+ "1caad933e0b8cc4", &
+ "b14fd3bf18bcafd", &
+ "ad091bbbb0f809e", &
+ "90b71c8c598da19", &
+ "f8c4d115f90bd92", &
+ "9d4dcccc63afb7f", &
+ "fa2c92444a6e76b", &
+ "1e14768c6bc6c43", &
+ "d1baf5aacb86087", &
+ "bdf762b92ee51c7", &
+ "caacec06ad8a90c", &
+ "804ccf4df9b08ec", &
+ "69e969f9da5cbd8", &
+ "814ccf4df9b086c", &
+ "cebe4f9796f4542", &
+ "491f65654499186", &
+ "8fbf5b9796f6d2a", &
+ "ce3e4f9396f4542", &
+ "47558560e7debc3", &
+ "94aadd33e038cc4", &
+ "a94eef4debb286e", &
+ "d8e5d115f91bcd2", &
+ "532d488fe3da0ab", &
+ "664e7bc4e23a80c", &
+ "94a2dd33a038cd4", &
+ "d8c5d115f91bc92", &
+ "0fef071eee60bd5", &
+ "9a89a09163c2b97", &
+ "0eaf071e6c60bd5", &
+ "bc0d1bbbb0fe0be", &
+ "f9babd3d12d0f31", &
+ "69a969f9da5c9d8", &
+ "6e4e7bc4e23a82c", &
+ "b0042659f3227f5", &
+ "2d51418f0f28347", &
+ "be0d5bbbb0da0be", &
+ "225003508ec8302", &
+ "8fbf4b9796f4d2a", &
+ "bead5af9b0da1be", &
+ "6ca2465c440aed1", &
+ "4fbc1e47ed708c8", &
+ "bd091bbbb0fc09e", &
+ "b0062259f3307f5", &
+ "a8072a59f3727f5", &
+ "a0062259f3707f5", &
+ "3c380b14c974eae", &
+ "30042659f3226f5", &
+ "48b9e4cd7d142db", &
+ "728bcd4b38308fb", &
+ "c0c504b031ef126", &
+ "314fd3bb18bcafd", &
+ "1c29148305faec1", &
+ "44c92a9c28ada63", &
+ "88e99b370aae32b", &
+ "695081690386ad8", &
+ "572d0a8de3da1ea", &
+ "467f6610f357b2b", &
+ "733d008563da1a3", &
+ "d1baf4aacb84087", &
+ "4315551d71c8ff0", &
+ "48bde4cd7d140db", &
+ "3ebd58f9b0da9fc", &
+ "51baf4aacb84083", &
+ "814e4f4de9b082c", &
+ "814ecf4de9b086c", &
+ "be0d1bbbb0fa0be", &
+ "4f7580947c792b3", &
+ "cdf2dce48c39c3b", &
+ "d8c5c115f91bc12", &
+ "a94e6f4debb28ee", &
+ "be2d5afbb0da1be", &
+ "cdd6dce48439c2b", &
+ "bebd5af9b0da1fe", &
+ "fa2892444a6e66b", &
+ "51bbf4aacb8c083", &
+ "baa73d81eebcd83", &
+ "79a2ce47f138cc9", &
+ "cc28cf198e6dbd4", &
+ "fcde94dfcc9cc64", &
+ "1016fcf59286717", &
+ "12917ce2fc4dbf4", &
+ "4fbc1647e9708c8", &
+ "3e382b1cc974fae", &
+ "d5bafdaad386087", &
+ "0fef473eee60bd5", &
+ "c0e504b031ee126", &
+ "8bbf5b9797f6d2a", &
+ "0eef071e6e60bd5", &
+ "1806fcf59386517", &
+ "fcdc94df8c9cc64", &
+ "141eca2bfa25656", &
+ "5fbc1767e9708e8", &
+ "5aa4c7803a6bdf1", &
+ "b14bd3b718bcafd", &
+ "3ebd5af9b0da1fc", &
+ "d0a7148c5b8ca09", &
+ "a94ecf4debb086e", &
+ "733d808563ca1a3", &
+ "fd9abd1d92d0f31", &
+ "bc091bbbb0fe09e", &
+ "d0c514b0b0ef122", &
+ "4f7d80947c7d2b3", &
+ "8b3f5b97b7f6d2a", &
+ "4fbc1767e9708c8", &
+ "cebf4f9796f4502", &
+ "9c76c880a864e67", &
+ "abc1c984f95244e", &
+ "795081690786ad8", &
+ "467f6710f357b3b", &
+ "1c380b14c9f4eae", &
+ "d5baf5aac386087", &
+ "bedc94df8c95c64", &
+ "553d0a8de2da1fa", &
+ "0315551d71d8ff0", &
+ "1c1eca2ffa25656", &
+ "d4bafdaad3c6087", &
+ "be2d5bfbb0da0be", &
+ "b0062659f3207f5", &
+ "5ffc1765e9708e8", &
+ "8d62e8bcd303e33", &
+ "cc08cf198e69bd4", &
+ "573d0a8de3da1fa", &
+ "cd56dce48639c2b", &
+ "472dc094546c2b2", &
+ "7950a16907868d8", &
+ "7283cf4b38308fb", &
+ "894ecf4de9b086e", &
+ "0f7580b47c792b3", &
+ "cfbf4b9796f4d0a", &
+ "3e380b14c974fae", &
+ "732d0085e3da1a3", &
+ "1816fcf59386717", &
+ "532d088fe3da1ab", &
+ "1c300b94c9fcaae", &
+ "d0a71c8c5b8ca19", &
+ "9e84bd3d5d78d09", &
+ "225083508ec830a", &
+ "f99abd1d12d0f31", &
+ "35f4aa19cb673c0", &
+ "cdd2dce48c39c2b", &
+ "0f7780b47c792bf", &
+ "0e33a5f114f5730", &
+ "bc05b4c4ab0d850", &
+ "1c300b14c9f4aae", &
+ "cfbc1e47ed708e8", &
+ "0f7180b47c392b3", &
+ "d8c7c115f91be12", &
+ "c09148adfa94e97", &
+ "9c66c880a844e67", &
+ "2226c13b73519f8", &
+ "cebf4b9796f4d02", &
+ "c0e706b031ee126", &
+ "6a6629715e53ce3", &
+ "73f9aa824e7d0b8", &
+ "473d80947c6c2b2", &
+ "1df140e0ddb5632", &
+ "473dc0945c6c2b2", &
+ "81b4d95f671971d", &
+ "663945ca758e2b6", &
+ "02ec3d98a2306fd", &
+ "5dadb0fa1275690", &
+ "4bb8aaa854948d0", &
+ "8359ba40886971c", &
+ "49cc3d2a2be2ee0", &
+ "bfdf13af137f318", &
+ "a1de773a2b1ff04", &
+ "8ff3945a2f465c7", &
+ "532d0087e3da1a3", &
+ "f3eaf7fa454d385", &
+ "a606aa5aeba07d9", &
+ "67f0627b0af8a53", &
+ "56698bed69d1c2c", &
+ "d5f420011fbf924", &
+ "2a8f86c810e2c62", &
+ "43cc1cf1208c206", &
+ "ee784c4900258de"/
+
+data colorder/ &
+0,1,2,3,4,5,6,7,8,9,10,11,123,12,13,14,15,16,17,18, &
+19,20,21,22,23,24,25,138,26,145,27,28,29,30,31,32,33,34,35,36, &
+37,154,38,39,40,41,42,43,44,144,46,47,48,49,50,51,52,53,143,54, &
+125,56,57,58,124,59,120,140,157,160,55,60,61,62,156,162,141,64,65,153, &
+181,183,66,170,67,68,69,130,70,164,71,72,73,74,75,63,76,77,135,78, &
+79,80,176,169,82,83,84,167,180,85,136,158,129,166,175,142,134,146,121,165, &
+88,89,192,90,45,91,92,93,182,189,94,95,96,173,81,97,98,178,122,126, &
+132,99,100,152,186,193,101,102,151,103,104,172,159,168,150,190,147,148,201,107, &
+205,177,108,198,197,174,127,109,185,110,202,87,199,171,179,187,139,137,106,131, &
+206,194,112,149,155,113,128,184,196,86,114,203,212,195,208,105,188,161,163,191, &
+200,209,214,204,115,218,133,111,207,117,213,216,211,217,116,215,219,220,210,221, &
+118,222,223,225,224,228,226,229,231,227,233,119,234,235,232,230,237,239,236,238, &
+240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259, &
+260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279, &
+280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299/
+
diff --git a/lib/fsk4hf/ldpcsim120.f90 b/lib/fsk4hf/ldpcsim120.f90
new file mode 100644
index 00000000..0b7d9286
--- /dev/null
+++ b/lib/fsk4hf/ldpcsim120.f90
@@ -0,0 +1,238 @@
+program ldpcsim120
+! End to end test of the (120,60)/crc10 encoder and decoder.
+use crc
+use packjt
+
+parameter(NRECENT=10)
+character*12 recent_calls(NRECENT)
+character*22 msg,msgsent,msgreceived
+character*8 arg
+integer*1, allocatable :: codeword(:), decoded(:), message(:)
+integer*1, target:: i1Msg8BitBytes(9)
+integer*1, target:: i1Dec8BitBytes(9)
+integer*1 msgbits(60)
+integer*1 apmask(120)
+integer*1 cw(120)
+integer*2 checksum
+integer colorder(120)
+integer nerrtot(120),nerrdec(120),nmpcbad(60)
+logical checksumok,fsk,bpsk
+real*8, allocatable :: rxdata(:)
+real, allocatable :: llr(:)
+real dllr(120),llrd(120)
+
+data colorder/ &
+ 0,1,2,21,3,4,5,6,7,8,20,10,9,11,12,23,13,28,14,31, &
+ 15,16,22,26,17,30,18,29,25,32,41,34,19,33,27,36,38,43,42,24, &
+ 37,39,45,40,35,44,47,46,50,51,53,48,52,56,54,57,55,49,58,61, &
+ 60,59,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, &
+ 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99, &
+ 100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119/
+
+do i=1,NRECENT
+ recent_calls(i)=' '
+enddo
+nerrtot=0
+nerrdec=0
+nmpcbad=0 ! Used to collect the number of errors in the message+crc part of the codeword
+
+nargs=iargc()
+if(nargs.ne.3) then
+ print*,'Usage: ldpcsim niter #trials s '
+ print*,'eg: ldpcsim 10 1000 0.84'
+ print*,'If s is negative, then value is ignored and sigma is calculated from SNR.'
+ return
+endif
+call getarg(1,arg)
+read(arg,*) max_iterations
+call getarg(2,arg)
+read(arg,*) ntrials
+call getarg(3,arg)
+read(arg,*) s
+
+fsk=.false.
+bpsk=.true.
+
+! don't count crc bits as data bits
+N=120
+K=60
+! scale Eb/No for a (120,50) code
+rate=real(50)/real(N)
+
+write(*,*) "rate: ",rate
+write(*,*) "niter= ",max_iterations," s= ",s
+
+allocate ( codeword(N), decoded(K), message(K) )
+allocate ( rxdata(N), llr(N) )
+
+! The message should be packed into the first 7 bytes
+ i1Msg8BitBytes(1:6)=85
+ i1Msg8BitBytes(7)=64
+! The CRC will be put into the last 2 bytes
+ i1Msg8BitBytes(8:9)=0
+ checksum = crc10 (c_loc (i1Msg8BitBytes), 9)
+! For reference, the next 3 lines show how to check the CRC
+ i1Msg8BitBytes(8)=checksum/256
+ i1Msg8BitBytes(9)=iand (checksum,255)
+ checksumok = crc10_check(c_loc (i1Msg8BitBytes), 9)
+ if( checksumok ) write(*,*) 'Good checksum'
+write(*,*) i1Msg8BitBytes(1:9)
+
+ mbit=0
+ do i=1, 7
+ i1=i1Msg8BitBytes(i)
+ do ibit=1,8
+ mbit=mbit+1
+ msgbits(mbit)=iand(1,ishft(i1,ibit-8))
+ enddo
+ enddo
+ i1=i1Msg8BitBytes(8) ! First 2 bits of crc10 are LSB of this byte
+ do ibit=1,2
+ msgbits(50+ibit)=iand(1,ishft(i1,ibit-2))
+ enddo
+ i1=i1Msg8BitBytes(9) ! Now shift in last 8 bits of the CRC
+ do ibit=1,8
+ msgbits(52+ibit)=iand(1,ishft(i1,ibit-8))
+ enddo
+
+ write(*,*) 'message'
+ write(*,'(9(8i1,1x))') msgbits
+
+ call encode120(msgbits,codeword)
+ call init_random_seed()
+ call sgran()
+
+ write(*,*) 'codeword'
+ write(*,'(15(8i1,1x))') codeword
+
+write(*,*) "Es/N0 SNR2500 ngood nundetected nbadcrc sigma"
+do idb = -10, 24
+ db=idb/2.0-1.0
+! sigma=1/sqrt( 2*rate*(10**(db/10.0)) ) ! to make db represent Eb/No
+ sigma=1/sqrt( 2*(10**(db/10.0)) ) ! db represents Es/No
+ ngood=0
+ nue=0
+ nbadcrc=0
+ nberr=0
+ do itrial=1, ntrials
+! Create a realization of a noisy received word
+ do i=1,N
+ if( bpsk ) then
+ rxdata(i) = 2.0*codeword(i)-1.0 + sigma*gran()
+ elseif( fsk ) then
+ if( codeword(i) .eq. 1 ) then
+ r1=(1.0 + sigma*gran())**2 + (sigma*gran())**2
+ r2=(sigma*gran())**2 + (sigma*gran())**2
+ elseif( codeword(i) .eq. 0 ) then
+ r2=(1.0 + sigma*gran())**2 + (sigma*gran())**2
+ r1=(sigma*gran())**2 + (sigma*gran())**2
+ endif
+ rxdata(i)=0.35*(sqrt(r1)-sqrt(r2))
+! rxdata(i)=0.35*(exp(r1)-exp(r2))
+! rxdata(i)=0.12*(log(r1)-log(r2))
+ endif
+ enddo
+ nerr=0
+ do i=1,N
+ if( rxdata(i)*(2*codeword(i)-1.0) .lt. 0 ) nerr=nerr+1
+ enddo
+ nerrtot(nerr)=nerrtot(nerr)+1
+ nberr=nberr+nerr
+
+! Correct signal normalization is important for this decoder.
+! rxav=sum(rxdata)/N
+! rx2av=sum(rxdata*rxdata)/N
+! rxsig=sqrt(rx2av-rxav*rxav)
+! rxdata=rxdata/rxsig
+! To match the metric to the channel, s should be set to the noise standard deviation.
+! For now, set s to the value that optimizes decode probability near threshold.
+! The s parameter can be tuned to trade a few tenth's dB of threshold for an order of
+! magnitude in UER
+ if( s .lt. 0 ) then
+ ss=sigma
+ else
+ ss=s
+ endif
+
+ llr=2.0*rxdata/(ss*ss)
+ apmask=0
+
+! max_iterations is max number of belief propagation iterations
+ call bpdecode120(llr, apmask, max_iterations, decoded, niterations, cw)
+ n2err=0
+ do i=1,N
+ if( cw(i)*(2*codeword(i)-1.0) .lt. 0 ) n2err=n2err+1
+ enddo
+!write(*,*) nerr,niterations,n2err
+ damp=0.75
+ ndither=0
+ if( niterations .lt. 0 ) then
+ do i=1, ndither
+ do in=1,N
+ dllr(in)=damp*gran()
+ enddo
+ llrd=llr+dllr
+ call bpdecode120(llrd, apmask, max_iterations, decoded, niterations, cw)
+ if( niterations .ge. 0 ) exit
+ enddo
+ endif
+
+! If the decoder finds a valid codeword, niterations will be .ge. 0.
+ if( niterations .ge. 0 ) then
+! Check the CRC
+ do ibyte=1,6
+ itmp=0
+ do ibit=1,8
+ itmp=ishft(itmp,1)+iand(1,decoded((ibyte-1)*8+ibit))
+ enddo
+ i1Dec8BitBytes(ibyte)=itmp
+ enddo
+ i1Dec8BitBytes(7)=decoded(49)*128+decoded(50)*64
+! Need to pack the received crc into bytes 8 and 9 for crc10_check
+ i1Dec8BitBytes(8)=decoded(51)*2+decoded(52)
+ i1Dec8BitBytes(9)=decoded(53)*128+decoded(54)*64+decoded(55)*32+decoded(56)*16
+ i1Dec8BitBytes(9)=i1Dec8BitBytes(9)+decoded(57)*8+decoded(58)*4+decoded(59)*2+decoded(60)*1
+ ncrcflag=0
+ if( crc10_check( c_loc( i1Dec8BitBytes ), 9 ) ) ncrcflag=1
+
+ if( ncrcflag .ne. 1 ) then
+ nbadcrc=nbadcrc+1
+ endif
+ nueflag=0
+
+ nerrmpc=0
+ do i=1,K ! find number of errors in message+crc part of codeword
+ if( msgbits(i) .ne. decoded(i) ) then
+ nueflag=1
+ nerrmpc=nerrmpc+1
+ endif
+ enddo
+ nmpcbad(nerrmpc)=nmpcbad(nerrmpc)+1 ! This histogram should inform our selection of CRC poly
+ if( ncrcflag .eq. 1 .and. nueflag .eq. 0 ) then
+ ngood=ngood+1
+ nerrdec(nerr)=nerrdec(nerr)+1
+ else if( ncrcflag .eq. 1 .and. nueflag .eq. 1 ) then
+ nue=nue+1;
+ endif
+ endif
+ enddo
+ snr2500=db+10*log10(0.4166/2500.0)
+ pberr=real(nberr)/(real(ntrials*N))
+ write(*,"(f4.1,4x,f5.1,1x,i8,1x,i8,1x,i8,8x,f5.2,8x,e10.3)") db,snr2500,ngood,nue,nbadcrc,ss,pberr
+
+enddo
+
+open(unit=23,file='nerrhisto.dat',status='unknown')
+do i=1,120
+ write(23,'(i4,2x,i10,i10,f10.2)') i,nerrdec(i),nerrtot(i),real(nerrdec(i))/real(nerrtot(i)+1e-10)
+enddo
+close(23)
+open(unit=25,file='nmpcbad.dat',status='unknown')
+do i=1,60
+ write(25,'(i4,2x,i10)') i,nmpcbad(i)
+enddo
+close(25)
+
+
+
+end program ldpcsim120
diff --git a/lib/fsk4hf/ldpcsim168.f90 b/lib/fsk4hf/ldpcsim168.f90
new file mode 100644
index 00000000..a1aee3eb
--- /dev/null
+++ b/lib/fsk4hf/ldpcsim168.f90
@@ -0,0 +1,233 @@
+program ldpcsim168
+! End to end test of the (168,84)/crc12 encoder and decoder.
+use crc
+use packjt
+
+parameter(NRECENT=10)
+character*12 recent_calls(NRECENT)
+character*22 msg,msgsent,msgreceived
+character*8 arg
+integer*1, allocatable :: codeword(:), decoded(:), message(:)
+integer*1, target:: i1Msg8BitBytes(11)
+integer*1 msgbits(84)
+integer*1 apmask(168), cw(168)
+integer*2 checksum
+integer*4 i4Msg6BitWords(13)
+integer colorder(168)
+integer nerrtot(168),nerrdec(168),nmpcbad(84)
+logical checksumok,fsk,bpsk
+real*8, allocatable :: rxdata(:)
+real, allocatable :: llr(:)
+
+data colorder/0,1,2,3,28,4,5,6,7,8,9,10,11,34,12,32,13,14,15,16,17, &
+ 18,36,29,42,31,20,21,41,40,30,38,22,19,47,37,46,35,44,33,49,24, &
+ 43,51,25,26,27,50,52,57,69,54,55,45,59,58,56,61,60,53,48,23,62, &
+ 63,64,67,66,65,68,39,70,71,72,74,73,75,76,77,80,81,78,82,79,83, &
+ 84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104, &
+ 105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, &
+ 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146, &
+ 147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167/
+
+do i=1,NRECENT
+ recent_calls(i)=' '
+enddo
+nerrtot=0
+nerrdec=0
+nmpcbad=0 ! Used to collect the number of errors in the message+crc part of the codeword
+
+nargs=iargc()
+if(nargs.ne.3) then
+ print*,'Usage: ldpcsim niter #trials s '
+ print*,'eg: ldpcsim 10 1000 0.84'
+ print*,'If s is negative, then value is ignored and sigma is calculated from SNR.'
+ return
+endif
+call getarg(1,arg)
+read(arg,*) max_iterations
+call getarg(2,arg)
+read(arg,*) ntrials
+call getarg(3,arg)
+read(arg,*) s
+
+fsk=.false.
+bpsk=.true.
+
+! don't count crc bits as data bits
+N=168
+K=84
+! scale Eb/No for a (168,72) code
+rate=real(72)/real(N)
+
+write(*,*) "rate: ",rate
+write(*,*) "niter= ",max_iterations," s= ",s
+
+allocate ( codeword(N), decoded(K), message(K) )
+allocate ( rxdata(N), llr(N) )
+
+! msg="K1JT K9AN EN50"
+ msg="G4WJS K9AN EN50"
+ call packmsg(msg,i4Msg6BitWords,itype,.false.) !Pack into 12 6-bit bytes
+ call unpackmsg(i4Msg6BitWords,msgsent,.false.,'') !Unpack to get msgsent
+ write(*,*) "message sent ",msgsent
+
+ i4=0
+ ik=0
+ im=0
+ do i=1,12
+ nn=i4Msg6BitWords(i)
+ do j=1, 6
+ ik=ik+1
+ i4=i4+i4+iand(1,ishft(nn,j-6))
+ i4=iand(i4,255)
+ if(ik.eq.8) then
+ im=im+1
+! if(i4.gt.127) i4=i4-256
+ i1Msg8BitBytes(im)=i4
+ ik=0
+ endif
+ enddo
+ enddo
+
+ i1Msg8BitBytes(10:11)=0
+ checksum = crc12 (c_loc (i1Msg8BitBytes), 11)
+! For reference, the next 3 lines show how to check the CRC
+ i1Msg8BitBytes(10)=checksum/256
+ i1Msg8BitBytes(11)=iand (checksum,255)
+ checksumok = crc12_check(c_loc (i1Msg8BitBytes), 11)
+ if( checksumok ) write(*,*) 'Good checksum'
+
+ mbit=0
+ do i=1, 9
+ i1=i1Msg8BitBytes(i)
+ do ibit=1,8
+ mbit=mbit+1
+ msgbits(mbit)=iand(1,ishft(i1,ibit-8))
+ enddo
+ enddo
+ i1=i1Msg8BitBytes(10) ! First 4 bits of crc12 are LSB of this byte
+ do ibit=1,4
+ msgbits(72+ibit)=iand(1,ishft(i1,ibit-4))
+ enddo
+ i1=i1Msg8BitBytes(11) ! Now shift in last 8 bits of the CRC
+ do ibit=1,8
+ msgbits(76+ibit)=iand(1,ishft(i1,ibit-8))
+ enddo
+
+ write(*,*) 'message'
+ write(*,'(11(8i1,1x))') msgbits
+
+ call encode168(msgbits,codeword)
+ call init_random_seed()
+ call sgran()
+
+ write(*,*) 'codeword'
+ write(*,'(21(8i1,1x))') codeword
+
+write(*,*) "Es/N0 SNR2500 ngood nundetected nbadcrc sigma"
+do idb = 6,-6,-1
+ db=idb/2.0-1.0
+ sigma=1/sqrt( 2*(10**(db/10.0)) )
+ ngood=0
+ nue=0
+ nbadcrc=0
+ nberr=0
+ do itrial=1, ntrials
+! Create a realization of a noisy received word
+ do i=1,N
+ if( bpsk ) then
+ rxdata(i) = 2.0*codeword(i)-1.0 + sigma*gran()
+ elseif( fsk ) then
+ if( codeword(i) .eq. 1 ) then
+ r1=(1.0 + sigma*gran())**2 + (sigma*gran())**2
+ r2=(sigma*gran())**2 + (sigma*gran())**2
+ elseif( codeword(i) .eq. 0 ) then
+ r2=(1.0 + sigma*gran())**2 + (sigma*gran())**2
+ r1=(sigma*gran())**2 + (sigma*gran())**2
+ endif
+ rxdata(i)=0.35*(sqrt(r1)-sqrt(r2))
+! rxdata(i)=0.35*(exp(r1)-exp(r2))
+! rxdata(i)=0.12*(log(r1)-log(r2))
+ endif
+ enddo
+ nerr=0
+ do i=1,N
+ if( rxdata(i)*(2*codeword(i)-1.0) .lt. 0 ) nerr=nerr+1
+ enddo
+ nerrtot(nerr)=nerrtot(nerr)+1
+ nberr=nberr+nerr
+
+! Correct signal normalization is important for this decoder.
+! rxav=sum(rxdata)/N
+! rx2av=sum(rxdata*rxdata)/N
+! rxsig=sqrt(rx2av-rxav*rxav)
+! rxdata=rxdata/rxsig
+! To match the metric to the channel, s should be set to the noise standard deviation.
+! For now, set s to the value that optimizes decode probability near threshold.
+! The s parameter can be tuned to trade a few tenth's dB of threshold for an order of
+! magnitude in UER
+ if( s .lt. 0 ) then
+ ss=sigma
+ else
+ ss=s
+ endif
+
+ llr=2.0*rxdata/(ss*ss)
+ nap=0 ! number of AP bits
+ llr(colorder(168-84+1:168-84+nap)+1)=5*(2.0*msgbits(1:nap)-1.0)
+ apmask=0
+ apmask(colorder(168-84+1:168-84+nap)+1)=1
+
+! max_iterations is max number of belief propagation iterations
+ call bpdecode168(llr, apmask, max_iterations, decoded, niterations)
+! if( niterations .eq. -1 ) then
+! norder=3
+! call osd168(llr, norder, decoded, niterations, cw)
+! endif
+! If the decoder finds a valid codeword, niterations will be .ge. 0.
+ if( niterations .ge. 0 ) then
+ call extractmessage168(decoded,msgreceived,ncrcflag,recent_calls,nrecent)
+ if( ncrcflag .ne. 1 ) then
+ nbadcrc=nbadcrc+1
+ endif
+
+ nueflag=0
+ nerrmpc=0
+ do i=1,K ! find number of errors in message+crc part of codeword
+ if( msgbits(i) .ne. decoded(i) ) then
+ nueflag=1
+ nerrmpc=nerrmpc+1
+ endif
+ enddo
+
+write(37,*) niterations, ncrcflag, nueflag
+ nmpcbad(nerrmpc)=nmpcbad(nerrmpc)+1
+ if( ncrcflag .eq. 1 ) then
+ if( nueflag .eq. 0 ) then
+ ngood=ngood+1
+ nerrdec(nerr)=nerrdec(nerr)+1
+ else if( nueflag .eq. 1 ) then
+ nue=nue+1;
+ endif
+ endif
+ endif
+ enddo
+ snr2500=db+10*log10(10.417/2500.0)
+ pberr=real(nberr)/(real(ntrials*N))
+ write(*,"(f4.1,4x,f5.1,1x,i8,1x,i8,1x,i8,8x,f5.2,8x,e10.3)") db,snr2500,ngood,nue,nbadcrc,ss,pberr
+
+enddo
+
+open(unit=23,file='nerrhisto.dat',status='unknown')
+do i=1,168
+ write(23,'(i4,2x,i10,i10,f10.2)') i,nerrdec(i),nerrtot(i),real(nerrdec(i))/real(nerrtot(i)+1e-10)
+enddo
+close(23)
+open(unit=25,file='nmpcbad.dat',status='unknown')
+do i=1,84
+ write(25,'(i4,2x,i10)') i,nmpcbad(i)
+enddo
+close(25)
+
+
+
+end program ldpcsim168
diff --git a/lib/fsk4hf/ldpcsim174.f90 b/lib/fsk4hf/ldpcsim174.f90
new file mode 100644
index 00000000..b8640f6d
--- /dev/null
+++ b/lib/fsk4hf/ldpcsim174.f90
@@ -0,0 +1,238 @@
+program ldpcsim174
+! End to end test of the (174,75)/crc12 encoder and decoder.
+use crc
+use packjt
+
+parameter(NRECENT=10)
+character*12 recent_calls(NRECENT)
+character*22 msg,msgsent,msgreceived
+character*8 arg
+integer*1, allocatable :: codeword(:), decoded(:), message(:)
+integer*1, target:: i1Msg8BitBytes(11)
+integer*1 msgbits(87)
+integer*1 apmask(174), cw(174)
+integer*2 checksum
+integer*4 i4Msg6BitWords(13)
+integer colorder(174)
+integer nerrtot(174),nerrdec(174),nmpcbad(87)
+logical checksumok,fsk,bpsk
+real*8, allocatable :: rxdata(:)
+real, allocatable :: llr(:)
+
+data colorder/ &
+ 0, 1, 2, 3, 30, 4, 5, 6, 7, 8, 9, 10, 11, 32, 12, 40, 13, 14, 15, 16,&
+ 17, 18, 37, 45, 29, 19, 20, 21, 41, 22, 42, 31, 33, 34, 44, 35, 47, 51, 50, 43,&
+ 36, 52, 63, 46, 25, 55, 27, 24, 23, 53, 39, 49, 59, 38, 48, 61, 60, 57, 28, 62,&
+ 56, 58, 65, 66, 26, 70, 64, 69, 68, 67, 74, 71, 54, 76, 72, 75, 78, 77, 80, 79,&
+ 73, 83, 84, 81, 82, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,&
+ 100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,&
+ 120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,&
+ 140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,&
+ 160,161,162,163,164,165,166,167,168,169,170,171,172,173/
+
+do i=1,NRECENT
+ recent_calls(i)=' '
+enddo
+nerrtot=0
+nerrdec=0
+nmpcbad=0 ! Used to collect the number of errors in the message+crc part of the codeword
+
+nargs=iargc()
+if(nargs.ne.4) then
+ print*,'Usage: ldpcsim niter ndepth #trials s '
+ print*,'eg: ldpcsim 10 2 1000 0.84'
+ print*,'belief propagation iterations: niter, ordered-statistics depth: ndepth'
+ print*,'If s is negative, then value is ignored and sigma is calculated from SNR.'
+ return
+endif
+call getarg(1,arg)
+read(arg,*) max_iterations
+call getarg(2,arg)
+read(arg,*) ndepth
+call getarg(3,arg)
+read(arg,*) ntrials
+call getarg(4,arg)
+read(arg,*) s
+
+fsk=.false.
+bpsk=.true.
+
+! don't count crc bits as data bits
+N=174
+K=87
+! scale Eb/No for a (174,87) code
+rate=real(K)/real(N)
+
+write(*,*) "rate: ",rate
+write(*,*) "niter= ",max_iterations," s= ",s
+
+allocate ( codeword(N), decoded(K), message(K) )
+allocate ( rxdata(N), llr(N) )
+
+ msg="K1JT K9AN EN50"
+! msg="G4WJS K9AN EN50"
+ call packmsg(msg,i4Msg6BitWords,itype,.false.) !Pack into 12 6-bit bytes
+ call unpackmsg(i4Msg6BitWords,msgsent,.false.,'') !Unpack to get msgsent
+ write(*,*) "message sent ",msgsent
+
+ i4=0
+ ik=0
+ im=0
+ do i=1,12
+ nn=i4Msg6BitWords(i)
+ do j=1, 6
+ ik=ik+1
+ i4=i4+i4+iand(1,ishft(nn,j-6))
+ i4=iand(i4,255)
+ if(ik.eq.8) then
+ im=im+1
+! if(i4.gt.127) i4=i4-256
+ i1Msg8BitBytes(im)=i4
+ ik=0
+ endif
+ enddo
+ enddo
+
+ i1Msg8BitBytes(10:11)=0
+ checksum = crc12 (c_loc (i1Msg8BitBytes), 11)
+! For reference, the next 3 lines show how to check the CRC
+ i1Msg8BitBytes(10)=checksum/256
+ i1Msg8BitBytes(11)=iand (checksum,255)
+ checksumok = crc12_check(c_loc (i1Msg8BitBytes), 11)
+ if( checksumok ) write(*,*) 'Good checksum'
+
+! K=87, For now:
+! msgbits(1:72) JT message bits
+! msgbits(73:75) 3 free message bits (set to 0)
+! msgbits(76:87) CRC12
+ mbit=0
+ do i=1, 9
+ i1=i1Msg8BitBytes(i)
+ do ibit=1,8
+ mbit=mbit+1
+ msgbits(mbit)=iand(1,ishft(i1,ibit-8))
+ enddo
+ enddo
+ msgbits(73:75)=0 ! the three extra message bits go here
+ i1=i1Msg8BitBytes(10) ! First 4 bits of crc12 are LSB of this byte
+ do ibit=1,4
+ msgbits(75+ibit)=iand(1,ishft(i1,ibit-4))
+ enddo
+ i1=i1Msg8BitBytes(11) ! Now shift in last 8 bits of the CRC
+ do ibit=1,8
+ msgbits(79+ibit)=iand(1,ishft(i1,ibit-8))
+ enddo
+
+ write(*,*) 'message'
+ write(*,'(11(8i1,1x))') msgbits
+
+ call encode174(msgbits,codeword)
+ call init_random_seed()
+! call sgran()
+
+ write(*,*) 'codeword'
+ write(*,'(22(8i1,1x))') codeword
+
+write(*,*) "Es/N0 SNR2500 ngood nundetected nbadcrc sigma"
+do idb = 20,-10,-1
+!do idb = -3,-3,-1
+ db=idb/2.0-1.0
+ sigma=1/sqrt( 2*(10**(db/10.0)) )
+ ngood=0
+ nue=0
+ nbadcrc=0
+ nberr=0
+ do itrial=1, ntrials
+! Create a realization of a noisy received word
+ do i=1,N
+ if( bpsk ) then
+ rxdata(i) = 2.0*codeword(i)-1.0 + sigma*gran()
+ elseif( fsk ) then
+ if( codeword(i) .eq. 1 ) then
+ r1=(1.0 + sigma*gran())**2 + (sigma*gran())**2
+ r2=(sigma*gran())**2 + (sigma*gran())**2
+ elseif( codeword(i) .eq. 0 ) then
+ r2=(1.0 + sigma*gran())**2 + (sigma*gran())**2
+ r1=(sigma*gran())**2 + (sigma*gran())**2
+ endif
+! rxdata(i)=0.35*(sqrt(r1)-sqrt(r2))
+! rxdata(i)=0.35*(exp(r1)-exp(r2))
+ rxdata(i)=0.12*(log(r1)-log(r2))
+ endif
+ enddo
+ nerr=0
+ do i=1,N
+ if( rxdata(i)*(2*codeword(i)-1.0) .lt. 0 ) nerr=nerr+1
+ enddo
+ nerrtot(nerr)=nerrtot(nerr)+1
+ nberr=nberr+nerr
+
+! Correct signal normalization is important for this decoder.
+ rxav=sum(rxdata)/N
+ rx2av=sum(rxdata*rxdata)/N
+ rxsig=sqrt(rx2av-rxav*rxav)
+ rxdata=rxdata/rxsig
+! To match the metric to the channel, s should be set to the noise standard deviation.
+! For now, set s to the value that optimizes decode probability near threshold.
+! The s parameter can be tuned to trade a few tenth's dB of threshold for an order of
+! magnitude in UER
+ if( s .lt. 0 ) then
+ ss=sigma
+ else
+ ss=s
+ endif
+
+ llr=2.0*rxdata/(ss*ss)
+ nap=0 ! number of AP bits
+ llr(colorder(174-87+1:174-87+nap)+1)=5*(2.0*msgbits(1:nap)-1.0)
+ apmask=0
+ apmask(colorder(174-87+1:174-87+nap)+1)=1
+
+! max_iterations is max number of belief propagation iterations
+ call bpdecode174(llr, apmask, max_iterations, decoded, cw, nharderrors,niterations)
+ if( ndepth .ge. 0 .and. nharderrors .lt. 0 ) call osd174(llr, apmask, ndepth, decoded, cw, nharderrors, dmin)
+! If the decoder finds a valid codeword, nharderrors will be .ge. 0.
+ if( nharderrors .ge. 0 ) then
+ call extractmessage174(decoded,msgreceived,ncrcflag,recent_calls,nrecent)
+ if( ncrcflag .ne. 1 ) then
+ nbadcrc=nbadcrc+1
+ endif
+
+ nueflag=0
+ nerrmpc=0
+ do i=1,K ! find number of errors in message+crc part of codeword
+ if( msgbits(i) .ne. decoded(i) ) then
+ nueflag=1
+ nerrmpc=nerrmpc+1
+ endif
+ enddo
+ nmpcbad(nerrmpc)=nmpcbad(nerrmpc)+1
+ if( ncrcflag .eq. 1 ) then
+ if( nueflag .eq. 0 ) then
+ ngood=ngood+1
+ nerrdec(nerr)=nerrdec(nerr)+1
+ else if( nueflag .eq. 1 ) then
+ nue=nue+1;
+ endif
+ endif
+ endif
+ enddo
+ baud=12000/1920
+ snr2500=db+10.0*log10((baud/2500.0))
+ pberr=real(nberr)/(real(ntrials*N))
+ write(*,"(f4.1,4x,f5.1,1x,i8,1x,i8,1x,i8,8x,f5.2,8x,e10.3)") db,snr2500,ngood,nue,nbadcrc,ss,pberr
+
+enddo
+
+open(unit=23,file='nerrhisto.dat',status='unknown')
+do i=1,174
+ write(23,'(i4,2x,i10,i10,f10.2)') i,nerrdec(i),nerrtot(i),real(nerrdec(i))/real(nerrtot(i)+1e-10)
+enddo
+close(23)
+open(unit=25,file='nmpcbad.dat',status='unknown')
+do i=1,87
+ write(25,'(i4,2x,i10)') i,nmpcbad(i)
+enddo
+close(25)
+
+end program ldpcsim174
diff --git a/lib/fsk4hf/ldpcsim300.f90 b/lib/fsk4hf/ldpcsim300.f90
new file mode 100644
index 00000000..d8a17759
--- /dev/null
+++ b/lib/fsk4hf/ldpcsim300.f90
@@ -0,0 +1,252 @@
+program ldpcsim300
+
+! End-to-end test of the (300,60)/crc10 encoder and decoders.
+
+use crc
+use packjt
+
+parameter(NRECENT=10)
+character*12 recent_calls(NRECENT)
+character*8 arg
+integer*1, allocatable :: codeword(:), decoded(:), message(:)
+integer*1, target:: i1Msg8BitBytes(9)
+integer*1, target:: i1Dec8BitBytes(9)
+integer*1 msgbits(60)
+integer*1 apmask(300)
+integer*1 cw(300)
+integer*2 checksum
+integer colorder(300)
+integer nerrtot(300),nerrdec(300),nmpcbad(60)
+logical checksumok,fsk,bpsk
+real*8, allocatable :: rxdata(:)
+real, allocatable :: llr(:)
+real dllr(300),llrd(300)
+
+data colorder/ &
+0,1,2,3,4,5,6,7,8,9,10,11,123,12,13,14,15,16,17,18, &
+19,20,21,22,23,24,25,138,26,145,27,28,29,30,31,32,33,34,35,36, &
+37,154,38,39,40,41,42,43,44,144,46,47,48,49,50,51,52,53,143,54, &
+125,56,57,58,124,59,120,140,157,160,55,60,61,62,156,162,141,64,65,153, &
+181,183,66,170,67,68,69,130,70,164,71,72,73,74,75,63,76,77,135,78, &
+79,80,176,169,82,83,84,167,180,85,136,158,129,166,175,142,134,146,121,165, &
+88,89,192,90,45,91,92,93,182,189,94,95,96,173,81,97,98,178,122,126, &
+132,99,100,152,186,193,101,102,151,103,104,172,159,168,150,190,147,148,201,107, &
+205,177,108,198,197,174,127,109,185,110,202,87,199,171,179,187,139,137,106,131, &
+206,194,112,149,155,113,128,184,196,86,114,203,212,195,208,105,188,161,163,191, &
+200,209,214,204,115,218,133,111,207,117,213,216,211,217,116,215,219,220,210,221, &
+118,222,223,225,224,228,226,229,231,227,233,119,234,235,232,230,237,239,236,238, &
+240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259, &
+260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279, &
+280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299/
+
+do i=1,NRECENT
+ recent_calls(i)=' '
+enddo
+nerrtot=0
+nerrdec=0
+nmpcbad=0 ! Used to collect the number of errors in the message+crc part of the codeword
+
+nargs=iargc()
+if(nargs.ne.3) then
+ print*,'Usage: ldpcsim niter #trials s '
+ print*,'eg: ldpcsim 100 1000 0.84'
+ print*,'If s is negative, then value is ignored and sigma is calculated from SNR.'
+ return
+endif
+call getarg(1,arg)
+read(arg,*) max_iterations
+call getarg(2,arg)
+read(arg,*) ntrials
+call getarg(3,arg)
+read(arg,*) s
+
+fsk=.false.
+bpsk=.true.
+
+! don't count crc bits as data bits
+N=300
+K=60
+! scale Eb/No for a (300,50) code
+rate=real(50)/real(N)
+
+write(*,*) "rate: ",rate
+write(*,*) "niter= ",max_iterations," s= ",s
+
+allocate ( codeword(N), decoded(K), message(K) )
+allocate ( rxdata(N), llr(N) )
+
+! The message should be packed into the first 7 bytes
+ i1Msg8BitBytes(1:6)=85
+ i1Msg8BitBytes(7)=64
+! The CRC will be put into the last 2 bytes
+ i1Msg8BitBytes(8:9)=0
+ checksum = crc10 (c_loc (i1Msg8BitBytes), 9)
+! For reference, the next 3 lines show how to check the CRC
+ i1Msg8BitBytes(8)=checksum/256
+ i1Msg8BitBytes(9)=iand (checksum,255)
+ checksumok = crc10_check(c_loc (i1Msg8BitBytes), 9)
+ if( checksumok ) write(*,*) 'Good checksum'
+write(*,*) i1Msg8BitBytes(1:9)
+
+ mbit=0
+ do i=1, 7
+ i1=i1Msg8BitBytes(i)
+ do ibit=1,8
+ mbit=mbit+1
+ msgbits(mbit)=iand(1,ishft(i1,ibit-8))
+ enddo
+ enddo
+ i1=i1Msg8BitBytes(8) ! First 2 bits of crc10 are LSB of this byte
+ do ibit=1,2
+ msgbits(50+ibit)=iand(1,ishft(i1,ibit-2))
+ enddo
+ i1=i1Msg8BitBytes(9) ! Now shift in last 8 bits of the CRC
+ do ibit=1,8
+ msgbits(52+ibit)=iand(1,ishft(i1,ibit-8))
+ enddo
+
+ write(*,*) 'message'
+ write(*,'(9(8i1,1x))') msgbits
+
+ call encode300(msgbits,codeword)
+ call init_random_seed()
+ call sgran()
+
+ write(*,*) 'codeword'
+ write(*,'(38(8i1,1x))') codeword
+
+write(*,*) "Es/N0 SNR2500 ngood nundetected nbadcrc sigma"
+do idb = 20,-16,-1
+!do idb = -16, -16, -1
+ db=idb/2.0-1.0
+! sigma=1/sqrt( 2*rate*(10**(db/10.0)) ) ! to make db represent Eb/No
+ sigma=1/sqrt( 2*(10**(db/10.0)) ) ! db represents Es/No
+ ngood=0
+ nue=0
+ nbadcrc=0
+ nberr=0
+ do itrial=1, ntrials
+! Create a realization of a noisy received word
+ do i=1,N
+ if( bpsk ) then
+ rxdata(i) = 2.0*codeword(i)-1.0 + sigma*gran()
+ elseif( fsk ) then
+ if( codeword(i) .eq. 1 ) then
+ r1=(1.0 + sigma*gran())**2 + (sigma*gran())**2
+ r2=(sigma*gran())**2 + (sigma*gran())**2
+ elseif( codeword(i) .eq. 0 ) then
+ r2=(1.0 + sigma*gran())**2 + (sigma*gran())**2
+ r1=(sigma*gran())**2 + (sigma*gran())**2
+ endif
+ rxdata(i)=0.35*(sqrt(r1)-sqrt(r2))
+! rxdata(i)=0.35*(exp(r1)-exp(r2))
+! rxdata(i)=0.12*(log(r1)-log(r2))
+ endif
+ enddo
+ nerr=0
+ do i=1,N
+ if( rxdata(i)*(2*codeword(i)-1.0) .lt. 0 ) nerr=nerr+1
+ enddo
+ nerrtot(nerr)=nerrtot(nerr)+1
+ nberr=nberr+nerr
+
+! Correct signal normalization is important for this decoder.
+! rxav=sum(rxdata)/N
+! rx2av=sum(rxdata*rxdata)/N
+! rxsig=sqrt(rx2av-rxav*rxav)
+! rxdata=rxdata/rxsig
+! To match the metric to the channel, s should be set to the noise standard deviation.
+! For now, set s to the value that optimizes decode probability near threshold.
+! The s parameter can be tuned to trade a few tenth's dB of threshold for an order of
+! magnitude in UER
+ if( s .lt. 0 ) then
+ ss=sigma
+ else
+ ss=s
+ endif
+
+ llr=2.0*rxdata/(ss*ss)
+ apmask=0
+! max_iterations is max number of belief propagation iterations
+ call bpdecode300(llr, apmask, max_iterations, decoded, niterations, cw)
+ if( niterations .lt. 0 ) then
+ norder=3
+ call osd300(llr, norder, decoded, niterations, cw)
+ endif
+ n2err=0
+ do i=1,N
+ if( cw(i)*(2*codeword(i)-1.0) .lt. 0 ) n2err=n2err+1
+ enddo
+!write(*,*) nerr,niterations,n2err
+ damp=0.75
+ ndither=0
+ if( niterations .lt. 0 ) then
+ do i=1, ndither
+ do in=1,N
+ dllr(in)=damp*gran()
+ enddo
+ llrd=llr+dllr
+ call bpdecode300(llrd, apmask, max_iterations, decoded, niterations, cw)
+ if( niterations .ge. 0 ) exit
+ enddo
+ endif
+
+! If the decoder finds a valid codeword, niterations will be .ge. 0.
+ if( niterations .ge. 0 ) then
+! Check the CRC
+ do ibyte=1,6
+ itmp=0
+ do ibit=1,8
+ itmp=ishft(itmp,1)+iand(1,decoded((ibyte-1)*8+ibit))
+ enddo
+ i1Dec8BitBytes(ibyte)=itmp
+ enddo
+ i1Dec8BitBytes(7)=decoded(49)*128+decoded(50)*64
+! Need to pack the received crc into bytes 8 and 9 for crc10_check
+ i1Dec8BitBytes(8)=decoded(51)*2+decoded(52)
+ i1Dec8BitBytes(9)=decoded(53)*128+decoded(54)*64+decoded(55)*32+decoded(56)*16
+ i1Dec8BitBytes(9)=i1Dec8BitBytes(9)+decoded(57)*8+decoded(58)*4+decoded(59)*2+decoded(60)*1
+ ncrcflag=0
+ if( crc10_check( c_loc( i1Dec8BitBytes ), 9 ) ) ncrcflag=1
+
+ if( ncrcflag .ne. 1 ) then
+ nbadcrc=nbadcrc+1
+ endif
+ nueflag=0
+
+ nerrmpc=0
+ do i=1,K ! find number of errors in message+crc part of codeword
+ if( msgbits(i) .ne. decoded(i) ) then
+ nueflag=1
+ nerrmpc=nerrmpc+1
+ endif
+ enddo
+ nmpcbad(nerrmpc)=nmpcbad(nerrmpc)+1 ! This histogram should inform our selection of CRC poly
+ if( ncrcflag .eq. 1 .and. nueflag .eq. 0 ) then
+ ngood=ngood+1
+ nerrdec(nerr)=nerrdec(nerr)+1
+ else if( ncrcflag .eq. 1 .and. nueflag .eq. 1 ) then
+ nue=nue+1;
+ endif
+ endif
+ enddo
+ snr2500=db+10*log10(1.389/2500.0)
+ pberr=real(nberr)/(real(ntrials*N))
+ write(*,"(f4.1,4x,f5.1,1x,i8,1x,i8,1x,i8,8x,f5.2,8x,e10.3)") db,snr2500,ngood,nue,nbadcrc,ss,pberr
+
+enddo
+
+open(unit=23,file='nerrhisto.dat',status='unknown')
+do i=1,120
+ write(23,'(i4,2x,i10,i10,f10.2)') i,nerrdec(i),nerrtot(i),real(nerrdec(i))/real(nerrtot(i)+1e-10)
+enddo
+close(23)
+open(unit=25,file='nmpcbad.dat',status='unknown')
+do i=1,60
+ write(25,'(i4,2x,i10)') i,nmpcbad(i)
+enddo
+close(25)
+
+
+
+end program ldpcsim300
diff --git a/lib/fsk4hf/mskhfsim.f90 b/lib/fsk4hf/mskhfsim.f90
new file mode 100644
index 00000000..86140e1c
--- /dev/null
+++ b/lib/fsk4hf/mskhfsim.f90
@@ -0,0 +1,194 @@
+program msksim
+
+! Simulate characteristics of a potential "MSK10" mode using LDPC (168,84)
+! code, OQPDK modulation, and 30 s T/R sequences.
+
+! 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
+
+ parameter (KK=84) !Information bits (72 + CRC12)
+ parameter (ND=168) !Data symbols: LDPC (168,84), r=1/2
+ parameter (NS=65) !Sync symbols (2 x 26 + Barker 13)
+ parameter (NR=3) !Ramp up/down
+ parameter (NN=NR+NS+ND) !Total symbols (236)
+ parameter (NSPS=1152/72) !Samples per MSK symbol (16)
+ parameter (N2=2*NSPS) !Samples per OQPSK symbol (32)
+ parameter (N13=13*N2) !Samples in central sync vector (416)
+ parameter (NZ=NSPS*NN) !Samples in baseband waveform (3776)
+ parameter (NFFT1=4*NSPS,NH1=NFFT1/2)
+
+ character*8 arg
+ complex cbb(0:NZ-1) !Complex baseband waveform
+ complex csync(0:NZ-1) !Sync symbols only, from cbb
+ complex cb13(0:N13-1) !Barker 13 waveform
+ complex c(0:NZ-1) !Complex waveform
+ complex c0(0:NZ-1) !Complex waveform
+ complex zz(NS+ND) !Complex symbol values (intermediate)
+ complex z
+ real xnoise(0:NZ-1) !Generated random noise
+ real ynoise(0:NZ-1) !Generated random noise
+ 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
+ integer id(NS+ND) !NRZ values (+/-1) for Sync and Data
+ integer ierror(NS+ND)
+ integer icw(NN)
+ integer*1 msgbits(KK),decoded(KK),apmask(ND),cw(ND)
+! integer*1 codeword(ND)
+ data msgbits/0,0,1,0,0,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1, &
+ 1,1,1,0,1,1,1,1,1,1,1,0,0,1,0,0,1,1,0,1,0,1,1,1,0,1,1,0,1,1, &
+ 1,1,0,1,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,1,0/
+
+ nargs=iargc()
+ if(nargs.ne.6) then
+ print*,'Usage: mskhfsim f0(Hz) delay(ms) fspread(Hz) maxn iters snr(dB)'
+ print*,'Example: mskhfsim 0 0 0 5 10 -20'
+ print*,'Set snr=0 to cycle through a range'
+ go to 999
+ endif
+ call getarg(1,arg)
+ read(arg,*) f0 !Generated carrier frequency
+ call getarg(2,arg)
+ read(arg,*) delay !Delta_t (ms) for Watterson model
+ call getarg(3,arg)
+ read(arg,*) fspread !Fspread (Hz) for Watterson model
+ call getarg(4,arg)
+ read(arg,*) maxn !Max nterms for polyfit
+ call getarg(5,arg)
+ read(arg,*) iters !Iterations at each SNR
+ call getarg(6,arg)
+ read(arg,*) snrdb !Specified SNR_2500
+
+ twopi=8.0*atan(1.0)
+ fs=12000.0/72.0 !Sample rate = 166.6666667 Hz
+ 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)
+ bandwidth_ratio=2500.0/(fs/2.0)
+ write(*,1000) f0,delay,fspread,maxn,iters,baud,3*baud,txt
+1000 format('f0:',f5.1,' Delay:',f4.1,' fSpread:',f5.2,' maxn:',i3, &
+ ' Iters:',i6/'Baud:',f7.3,' BW:',f5.1,' TxT:',f5.1,f5.2/)
+ write(*,1004)
+1004 format(/' SNR err ber fer fsigma'/37('-'))
+
+ do i=1,N2 !Half-sine pulse shape
+ pp(i)=sin(0.5*(i-1)*twopi/(2*NSPS))
+ enddo
+
+ call genmskhf(msgbits,id,icw,cbb,csync)!Generate baseband waveform and csync
+ cb13=csync(1680:2095) !Copy the Barker 13 waveform
+ a=0.
+ a(1)=f0
+ call twkfreq1(cbb,NZ,fs,a,cbb) !Mix to specified frequency
+
+ isna=-10
+ isnb=-30
+ if(snrdb.ne.0.0) then
+ isna=nint(snrdb)
+ isnb=isna
+ endif
+ do isnr=isna,isnb,-1 !Loop over SNR range
+ snrdb=isnr
+ sig=sqrt(bandwidth_ratio) * 10.0**(0.05*snrdb)
+ if(snrdb.gt.90.0) sig=1.0
+ nhard=0
+ nhardsync=0
+ nfe=0
+ sqf=0.
+ do iter=1,iters !Loop over requested iterations
+ c=cbb
+ if(delay.ne.0.0 .or. fspread.ne.0.0) then
+ call watterson(c,NZ,fs,delay,fspread)
+ endif
+ c=sig*c !Scale to requested SNR
+ if(snrdb.lt.90) then
+ do i=0,NZ-1 !Generate gaussian noise
+ xnoise(i)=gran()
+ ynoise(i)=gran()
+ enddo
+ c=c + cmplx(xnoise,ynoise) !Add AWGN noise
+ endif
+
+ call getfc1(c,fc1) !First approx for freq
+ call getfc2(c,csync,fc1,fc2,fc3) !Refined freq
+ sqf=sqf + (fc1+fc2-f0)**2
+
+!NB: Measured performance is about equally good using fc2 or fc3 here:
+ a(1)=-(fc1+fc2)
+ a(2:5)=0.
+ call twkfreq1(c,NZ,fs,a,c) !Mix c down by fc1+fc2
+
+! The following may not be necessary?
+! z=sum(c(1680:2095)*cb13)/208.0 !Get phase from Barker 13 vector
+! z0=z/abs(z)
+! c=c*conjg(z0)
+
+!---------------------------------------------------------------- DT
+! Not presently used:
+ amax=0.
+ jpk=0
+ do j=-20*NSPS,20*NSPS !Get jpk
+ z=sum(c(1680+j:2095+j)*cb13)/208.0
+ if(abs(z).gt.amax) then
+ amax=abs(z)
+ jpk=j
+ endif
+ enddo
+ xdt=jpk/fs
+
+ nterms=maxn
+ c0=c
+ do itry=1,10
+ idf=itry/2
+ if(mod(itry,2).eq.0) idf=-idf
+ nhard0=0
+ nhardsync0=0
+ ifer=1
+ a(1)=idf*0.01
+ a(2:5)=0.
+ call twkfreq1(c0,NZ,fs,a,c) !Mix c0 into c
+ call cpolyfit(c,pp,id,maxn,aa,bb,zz,nhs)
+ call msksoftsym(zz,aa,bb,id,nterms,ierror,rxdata,nhard0,nhardsync0)
+ if(nhardsync0.gt.12) 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 bpdecode168(llr,apmask,max_iterations,decoded,niterations,cw)
+ nbadcrc=0
+ if(niterations.ge.0) call chkcrc12(decoded,nbadcrc)
+ if(niterations.lt.0 .or. count(msgbits.ne.decoded).gt.0 .or. &
+ nbadcrc.ne.0) ifer=1
+! if(ifer.eq.0) write(67,1301) snrdb,itry,idf,niterations, &
+! nhardsync0,nhard0
+!1301 format(f6.1,5i6)
+ if(ifer.eq.0) exit
+ enddo !Freq dither loop
+ nhard=nhard+nhard0
+ nhardsync=nharsdync+nhardsync0
+ nfe=nfe+ifer
+ enddo
+
+ fsigma=sqrt(sqf/iters)
+ ber=float(nhard)/((NS+ND)*iters)
+ fer=float(nfe)/iters
+ write(*,1050) snrdb,nhard,ber,fer,fsigma
+! write(60,1050) snrdb,nhard,ber,fer,fsigma
+1050 format(f6.1,i7,f8.4,f7.3,f8.2)
+ enddo
+
+999 end program msksim
diff --git a/lib/fsk4hf/msksoftsym.f90 b/lib/fsk4hf/msksoftsym.f90
new file mode 100644
index 00000000..016b23e1
--- /dev/null
+++ b/lib/fsk4hf/msksoftsym.f90
@@ -0,0 +1,81 @@
+subroutine msksoftsym(zz,aa,bb,id,nterms,ierror,rxdata,nhard0,nhardsync0)
+
+ parameter (KK=84) !Information bits (72 + CRC12)
+ parameter (ND=168) !Data symbols: LDPC (168,84), r=1/2
+ parameter (NS=65) !Sync symbols (2 x 26 + Barker 13)
+ parameter (NR=3) !Ramp up/down
+ parameter (NN=NR+NS+ND) !Total symbols (236)
+ parameter (NSPS=16) !Samples per MSK symbol (16)
+ parameter (N2=2*NSPS) !Samples per OQPSK symbol (32)
+ parameter (N13=13*N2) !Samples in central sync vector (416)
+ parameter (NZ=NSPS*NN) !Samples in baseband waveform (3760)
+ parameter (NFFT1=4*NSPS,NH1=NFFT1/2)
+
+ complex zz(NS+ND) !Complex symbol values (intermediate)
+ complex z,z0
+ real rxdata(ND) !Soft symbols
+ real aa(20),bb(20) !Fitted polyco's
+ integer id(NS+ND) !NRZ values (+/-1) for Sync and Data
+ integer ierror(NS+ND)
+
+ n=0
+ ierror=0
+ do j=1,117
+ xx=j*2.0/117.0 - 1.0
+ yii=1.
+ yqq=0.
+ if(nterms.gt.0) then
+ yii=aa(1)
+ yqq=bb(1)
+ do i=2,nterms
+ yii=yii + aa(i)*xx**(i-1)
+ yqq=yqq + bb(i)*xx**(i-1)
+ enddo
+ endif
+ z0=cmplx(yii,yqq)
+ z=zz(j)*conjg(z0)
+ p=real(z)
+ if(abs(id(j)).eq.2) then
+ if(real(z)*id(j).lt.0) then !Sync bit
+ nhardsync0=nhardsync0+1
+ ierror(j)=2
+ endif
+ else
+ n=n+1 !Data bit
+ rxdata(n)=p
+ ierr=0
+ if(id(j)*p.lt.0) then
+ ierr=1
+ ierror(j)=1
+ endif
+ nhard0=nhard0+ierr
+ endif
+ enddo
+
+ do j=118,233
+ xx=(j-116.5)*2.0/117.0 - 1.0
+ yii=1.
+ yqq=0.
+ if(nterms.gt.0) then
+ yii=aa(1)
+ yqq=bb(1)
+ do i=2,nterms
+ yii=yii + aa(i)*xx**(i-1)
+ yqq=yqq + bb(i)*xx**(i-1)
+ enddo
+ endif
+ z0=cmplx(yii,yqq)
+ z=zz(j)*conjg(z0)
+ p=aimag(z)
+ n=n+1
+ rxdata(n)=p
+ ierr=0
+ if(id(j)*p.lt.0) then
+ ierr=1
+ ierror(j)=1
+ endif
+ nhard0=nhard0+ierr
+ enddo
+
+ return
+end subroutine msksoftsym
diff --git a/lib/fsk4hf/msksoftsymw.f90 b/lib/fsk4hf/msksoftsymw.f90
new file mode 100644
index 00000000..c7402ec4
--- /dev/null
+++ b/lib/fsk4hf/msksoftsymw.f90
@@ -0,0 +1,78 @@
+subroutine msksoftsymw(zz,aa,bb,id,nterms,ierror,rxdata,nhard0,nhardsync0)
+
+ include 'wsprlf_params.f90'
+
+ complex zz(NS+ND) !Complex symbol values (intermediate)
+ complex z,z0
+ real rxdata(ND) !Soft symbols
+ real aa(20),bb(20) !Fitted polyco's
+ integer id(NS+ND) !NRZ values (+/-1) for Sync and Data
+ integer ierror(NS+ND)
+
+ n=0
+ ierror=0
+ ierr=0
+ jz=(NS+ND+1)/2
+ do j=1,jz
+ xx=j*2.0/jz - 1.0
+ yii=1.
+ yqq=0.
+ if(nterms.gt.0) then
+ yii=aa(1)
+ yqq=bb(1)
+ do i=2,nterms
+ yii=yii + aa(i)*xx**(i-1)
+ yqq=yqq + bb(i)*xx**(i-1)
+ enddo
+ endif
+ z0=cmplx(yii,yqq)
+ z=zz(j)*conjg(z0)
+ p=real(z)
+ if(abs(id(j)).eq.2) then
+ if(real(z)*id(j).lt.0) then !Sync bit
+ nhardsync0=nhardsync0+1
+ ierror(j)=2
+ endif
+ else
+ n=n+1 !Data bit
+ rxdata(n)=p
+ ierr=0
+ if(id(j)*p.lt.0) then
+ ierr=1
+ ierror(j)=1
+ endif
+ nhard0=nhard0+ierr
+ endif
+! write(41,3301) j,id(j),ierror(j),ierr,n,p,p*id(j)
+!3301 format(5i6,2f10.3)
+ enddo
+
+ do j=jz+1,NS+ND
+ xx=(j-jz+0.5)*2.0/jz - 1.0
+ yii=1.
+ yqq=0.
+ if(nterms.gt.0) then
+ yii=aa(1)
+ yqq=bb(1)
+ do i=2,nterms
+ yii=yii + aa(i)*xx**(i-1)
+ yqq=yqq + bb(i)*xx**(i-1)
+ enddo
+ endif
+ z0=cmplx(yii,yqq)
+ z=zz(j)*conjg(z0)
+ p=aimag(z)
+ n=n+1
+ if(n.gt.ND) exit
+ rxdata(n)=p
+ ierr=0
+ if(id(j)*p.lt.0) then
+ ierr=1
+ ierror(j)=1
+ endif
+ nhard0=nhard0+ierr
+! write(41,3301) j,id(j),ierror(j),ierr,n,p,p*id(j)
+ enddo
+
+ return
+end subroutine msksoftsymw
diff --git a/lib/fsk4hf/osd174.f90 b/lib/fsk4hf/osd174.f90
new file mode 100644
index 00000000..f3d1403b
--- /dev/null
+++ b/lib/fsk4hf/osd174.f90
@@ -0,0 +1,377 @@
+subroutine osd174(llr,apmask,ndeep,decoded,cw,nhardmin,dmin)
+!
+! An ordered-statistics decoder for the (174,87) code.
+!
+include "ldpc_174_87_params.f90"
+
+integer*1 apmask(N),apmaskr(N)
+integer*1 gen(K,N)
+integer*1 genmrb(K,N),g2(N,K)
+integer*1 temp(K),m0(K),me(K),mi(K),misub(K),e2sub(N-K),e2(N-K),ui(N-K)
+integer*1 r2pat(N-K)
+integer indices(N),nxor(N)
+integer*1 cw(N),ce(N),c0(N),hdec(N)
+integer*1 decoded(K)
+integer indx(N)
+real llr(N),rx(N),absrx(N)
+logical first,reset
+data first/.true./
+save first,gen
+
+if( first ) then ! fill the generator matrix
+ gen=0
+ do i=1,M
+ do j=1,22
+ read(g(i)(j:j),"(Z1)") istr
+ do jj=1, 4
+ irow=(j-1)*4+jj
+ if( btest(istr,4-jj) ) gen(irow,i)=1
+ enddo
+ enddo
+ enddo
+ do irow=1,K
+ gen(irow,M+irow)=1
+ enddo
+first=.false.
+endif
+
+! Re-order received vector to place systematic msg bits at the end.
+rx=llr(colorder+1)
+apmaskr=apmask(colorder+1)
+
+! Hard decisions on the received word.
+hdec=0
+where(rx .ge. 0) hdec=1
+
+! Use magnitude of received symbols as a measure of reliability.
+absrx=abs(rx)
+call indexx(absrx,N,indx)
+
+! Re-order the columns of the generator matrix in order of decreasing reliability.
+do i=1,N
+ genmrb(1:K,i)=gen(1:K,indx(N+1-i))
+ indices(i)=indx(N+1-i)
+enddo
+
+! Do gaussian elimination to create a generator matrix with the most reliable
+! received bits in positions 1:K in order of decreasing reliability (more or less).
+do id=1,K ! diagonal element indices
+ do icol=id,K+20 ! The 20 is ad hoc - beware
+ iflag=0
+ if( genmrb(id,icol) .eq. 1 ) then
+ iflag=1
+ if( icol .ne. id ) then ! reorder column
+ temp(1:K)=genmrb(1:K,id)
+ genmrb(1:K,id)=genmrb(1:K,icol)
+ genmrb(1:K,icol)=temp(1:K)
+ itmp=indices(id)
+ indices(id)=indices(icol)
+ indices(icol)=itmp
+ endif
+ do ii=1,K
+ if( ii .ne. id .and. genmrb(ii,id) .eq. 1 ) then
+ genmrb(ii,1:N)=ieor(genmrb(ii,1:N),genmrb(id,1:N))
+ endif
+ enddo
+ exit
+ endif
+ enddo
+enddo
+
+g2=transpose(genmrb)
+
+! The hard decisions for the K MRB bits define the order 0 message, m0.
+! Encode m0 using the modified generator matrix to find the "order 0" codeword.
+! Flip various combinations of bits in m0 and re-encode to generate a list of
+! codewords. Return the member of the list that has the smallest Euclidean
+! distance to the received word.
+
+hdec=hdec(indices) ! hard decisions from received symbols
+m0=hdec(1:K) ! zero'th order message
+absrx=absrx(indices)
+rx=rx(indices)
+apmaskr=apmaskr(indices)
+
+call mrbencode(m0,c0,g2,N,K)
+nxor=ieor(c0,hdec)
+nhardmin=sum(nxor)
+dmin=sum(nxor*absrx)
+
+cw=c0
+ntotal=0
+nrejected=0
+
+if(ndeep.eq.0) goto 998 ! norder=0
+if(ndeep.gt.5) ndeep=5
+if( ndeep.eq. 1) then
+ nord=1
+ npre1=0
+ npre2=0
+ nt=40
+ ntheta=12
+elseif(ndeep.eq.2) then
+ nord=1
+ npre1=1
+ npre2=0
+ nt=40
+ ntheta=12
+elseif(ndeep.eq.3) then
+ nord=1
+ npre1=1
+ npre2=1
+ nt=40
+ ntheta=12
+ ntau=14
+elseif(ndeep.eq.4) then
+ nord=2
+ npre1=1
+ npre2=0
+ nt=40
+ ntheta=12
+ ntau=19
+elseif(ndeep.eq.5) then
+ nord=2
+ npre1=1
+ npre2=1
+ nt=40
+ ntheta=12
+ ntau=19
+endif
+
+do iorder=1,nord
+ if( iorder.eq. 1 ) then
+ misub(1:K-1)=0
+ misub(K)=1
+ iflag=K
+ elseif( iorder.eq. 2 ) then
+ misub(1:K-2)=0
+ misub(K-1:K)=1
+ iflag=K-1
+ endif
+ do while(iflag .ge.0)
+ if(iorder.eq.nord .and. npre1.eq.0) then
+ iend=iflag
+ else
+ iend=1
+ endif
+ do n1=iflag,iend,-1
+ mi=misub
+ mi(n1)=1
+ if(any(iand(apmaskr(1:K),mi).eq.1)) cycle
+ ntotal=ntotal+1
+ me=ieor(m0,mi)
+ if(n1.eq.iflag) then
+ call mrbencode(me,ce,g2,N,K)
+ e2sub=ieor(ce(K+1:N),hdec(K+1:N))
+ e2=e2sub
+ nd1Kpt=sum(e2sub(1:nt))+1
+ d1=sum(ieor(me(1:K),hdec(1:K))*absrx(1:K))
+ else
+ e2=ieor(e2sub,g2(K+1:N,n1))
+ nd1Kpt=sum(e2(1:nt))+2
+ endif
+ if(nd1Kpt .le. ntheta) then
+ call mrbencode(me,ce,g2,N,K)
+ nxor=ieor(ce,hdec)
+ if(n1.eq.iflag) then
+ dd=d1+sum(e2sub*absrx(K+1:N))
+ else
+ dd=d1+ieor(ce(n1),hdec(n1))*absrx(n1)+sum(e2*absrx(K+1:N))
+ endif
+ if( dd .lt. dmin ) then
+ dmin=dd
+ cw=ce
+ nhardmin=sum(nxor)
+ nd1Kptbest=nd1Kpt
+ endif
+ else
+ nrejected=nrejected+1
+ endif
+ enddo
+! Get the next test error pattern, iflag will go negative
+! when the last pattern with weight iorder has been generated.
+ call nextpat(misub,k,iorder,iflag)
+ enddo
+enddo
+
+if(npre2.eq.1) then
+ reset=.true.
+ ntotal=0
+ do i1=K,1,-1
+ do i2=i1-1,1,-1
+ ntotal=ntotal+1
+ mi(1:ntau)=ieor(g2(K+1:K+ntau,i1),g2(K+1:K+ntau,i2))
+ call boxit(reset,mi(1:ntau),ntau,ntotal,i1,i2)
+ enddo
+ enddo
+
+ ncount2=0
+ ntotal2=0
+ reset=.true.
+! Now run through again and do the second pre-processing rule
+ if(nord.eq.1) then
+ misub(1:K-1)=0
+ misub(K)=1
+ iflag=K
+ elseif(nord.eq.2) then
+ misub(1:K-1)=0
+ misub(K-1:K)=1
+ iflag=K-1
+ endif
+ do while(iflag .ge.0)
+ me=ieor(m0,misub)
+ call mrbencode(me,ce,g2,N,K)
+ e2sub=ieor(ce(K+1:N),hdec(K+1:N))
+ do i2=0,ntau
+ ntotal2=ntotal2+1
+ ui=0
+ if(i2.gt.0) ui(i2)=1
+ r2pat=ieor(e2sub,ui)
+778 continue
+ call fetchit(reset,r2pat(1:ntau),ntau,in1,in2)
+ if(in1.gt.0.and.in2.gt.0) then
+ ncount2=ncount2+1
+ mi=misub
+ mi(in1)=1
+ mi(in2)=1
+ if(sum(mi).lt.nord+npre1+npre2.or.any(iand(apmaskr(1:K),mi).eq.1)) cycle
+ me=ieor(m0,mi)
+ call mrbencode(me,ce,g2,N,K)
+ nxor=ieor(ce,hdec)
+ dd=sum(nxor*absrx)
+ if( dd .lt. dmin ) then
+ dmin=dd
+ cw=ce
+ nhardmin=sum(nxor)
+ endif
+ goto 778
+ endif
+ enddo
+ call nextpat(misub,K,nord,iflag)
+ enddo
+endif
+
+998 continue
+! Re-order the codeword to place message bits at the end.
+cw(indices)=cw
+hdec(indices)=hdec
+decoded=cw(K+1:N)
+cw(colorder+1)=cw ! put the codeword back into received-word order
+return
+end subroutine osd174
+
+subroutine mrbencode(me,codeword,g2,N,K)
+integer*1 me(K),codeword(N),g2(N,K)
+! fast encoding for low-weight test patterns
+ codeword=0
+ do i=1,K
+ if( me(i) .eq. 1 ) then
+ codeword=ieor(codeword,g2(1:N,i))
+ endif
+ enddo
+return
+end subroutine mrbencode
+
+subroutine nextpat(mi,k,iorder,iflag)
+ integer*1 mi(k),ms(k)
+! generate the next test error pattern
+ ind=-1
+ do i=1,k-1
+ if( mi(i).eq.0 .and. mi(i+1).eq.1) ind=i
+ enddo
+ if( ind .lt. 0 ) then ! no more patterns of this order
+ iflag=ind
+ return
+ endif
+ ms=0
+ ms(1:ind-1)=mi(1:ind-1)
+ ms(ind)=1
+ ms(ind+1)=0
+ if( ind+1 .lt. k ) then
+ nz=iorder-sum(ms)
+ ms(k-nz+1:k)=1
+ endif
+ mi=ms
+ do i=1,k ! iflag will point to the lowest-index 1 in mi
+ if(mi(i).eq.1) then
+ iflag=i
+ exit
+ endif
+ enddo
+ return
+end subroutine nextpat
+
+subroutine boxit(reset,e2,ntau,npindex,i1,i2)
+ integer*1 e2(1:ntau)
+ integer indexes(4000,2),fp(0:525000),np(4000)
+ logical reset
+ common/boxes/indexes,fp,np
+
+ if(reset) then
+ patterns=-1
+ fp=-1
+ np=-1
+ sc=-1
+ indexes=-1
+ reset=.false.
+ endif
+
+ indexes(npindex,1)=i1
+ indexes(npindex,2)=i2
+ ipat=0
+ do i=1,ntau
+ if(e2(i).eq.1) then
+ ipat=ipat+ishft(1,ntau-i)
+ endif
+ enddo
+
+ ip=fp(ipat) ! see what's currently stored in fp(ipat)
+ if(ip.eq.-1) then
+ fp(ipat)=npindex
+ else
+ do while (np(ip).ne.-1)
+ ip=np(ip)
+ enddo
+ np(ip)=npindex
+ endif
+ return
+end subroutine boxit
+
+subroutine fetchit(reset,e2,ntau,i1,i2)
+ integer indexes(4000,2),fp(0:525000),np(4000)
+ integer lastpat
+ integer*1 e2(ntau)
+ logical reset
+ common/boxes/indexes,fp,np
+ save lastpat,inext
+
+ if(reset) then
+ lastpat=-1
+ reset=.false.
+ endif
+
+ ipat=0
+ do i=1,ntau
+ if(e2(i).eq.1) then
+ ipat=ipat+ishft(1,ntau-i)
+ endif
+ enddo
+ index=fp(ipat)
+
+ if(lastpat.ne.ipat .and. index.gt.0) then ! return first set of indices
+ i1=indexes(index,1)
+ i2=indexes(index,2)
+ inext=np(index)
+ elseif(lastpat.eq.ipat .and. inext.gt.0) then
+ i1=indexes(inext,1)
+ i2=indexes(inext,2)
+ inext=np(inext)
+ else
+ i1=-1
+ i2=-1
+ inext=-1
+ endif
+ lastpat=ipat
+ return
+end subroutine fetchit
+
diff --git a/lib/fsk4hf/osd300.f90 b/lib/fsk4hf/osd300.f90
new file mode 100644
index 00000000..c4ec9a02
--- /dev/null
+++ b/lib/fsk4hf/osd300.f90
@@ -0,0 +1,184 @@
+subroutine osd300(llr,norder,decoded,niterations,cw)
+!
+! An ordered-statistics decoder for the (300,60) code.
+!
+include "ldpc_300_60_params.f90"
+
+integer*1 gen(K,N)
+integer*1 genmrb(K,N),g2(N,K)
+integer*1 temp(K),m0(K),me(K),mi(K)
+integer indices(N),nxor(N)
+integer*1 cw(N),ce(N),c0(N),hdec(N)
+integer*1 decoded(K)
+integer indx(N)
+real llr(N),rx(N),absrx(N)
+logical first
+data first/.true./
+
+save first,gen
+
+if( first ) then ! fill the generator matrix
+ gen=0
+ do i=1,M
+ do j=1,15
+ read(g(i)(j:j),"(Z1)") istr
+ do jj=1, 4
+ irow=(j-1)*4+jj
+ if( btest(istr,4-jj) ) gen(irow,i)=1
+ enddo
+ enddo
+ enddo
+ do irow=1,K
+ gen(irow,M+irow)=1
+ enddo
+first=.false.
+endif
+
+! re-order received vector to place systematic msg bits at the end
+rx=llr(colorder+1)
+
+! hard decode the received word
+hdec=0
+where(rx .ge. 0) hdec=1
+
+! use magnitude of received symbols as a measure of reliability.
+absrx=abs(rx)
+call indexx(absrx,N,indx)
+
+! re-order the columns of the generator matrix in order of decreasing reliability.
+do i=1,N
+ genmrb(1:K,i)=gen(1:K,indx(N+1-i))
+ indices(i)=indx(N+1-i)
+enddo
+
+! do gaussian elimination to create a generator matrix with the most reliable
+! received bits in positions 1:K in order of decreasing reliability (more or less).
+! reliability will not be strictly decreasing because column re-ordering is needed
+! to put the generator matrix in systematic form. the "indices" array tracks
+! column permutations caused by reliability sorting and gaussian elimination.
+do id=1,K ! diagonal element indices
+ do icol=id,K+20 ! The 20 is ad hoc - beware
+ iflag=0
+ if( genmrb(id,icol) .eq. 1 ) then
+ iflag=1
+ if( icol .ne. id ) then ! reorder column
+ temp(1:K)=genmrb(1:K,id)
+ genmrb(1:K,id)=genmrb(1:K,icol)
+ genmrb(1:K,icol)=temp(1:K)
+ itmp=indices(id)
+ indices(id)=indices(icol)
+ indices(icol)=itmp
+ endif
+ do ii=1,K
+ if( ii .ne. id .and. genmrb(ii,id) .eq. 1 ) then
+ genmrb(ii,1:N)=mod(genmrb(ii,1:N)+genmrb(id,1:N),2)
+ endif
+ enddo
+ exit
+ endif
+ enddo
+enddo
+
+g2=transpose(genmrb)
+
+! The hard decisions for the K MRB bits define the order 0 message, m0.
+! Encode m0 using the modified generator matrix to find the "order 0" codeword.
+! Flip various combinations of bits in m0 and re-encode to generate a list of
+! codewords. Test all such codewords against the received word to decide which
+! codeword is most likely to be correct.
+
+hdec=hdec(indices) ! hard decisions from received symbols
+m0=hdec(1:K) ! zero'th order message
+absrx=absrx(indices)
+rx=rx(indices)
+
+s1=sum(absrx(1:K))
+s2=sum(absrx(K+1:N))
+xlam=5.0
+rho=s1/(s1+xlam*s2)
+call mrbencode(m0,c0,g2,N,K)
+nxor=ieor(c0,hdec)
+nhardmin=sum(nxor)
+dmin=sum(nxor*absrx)
+thresh=rho*dmin
+
+cw=c0
+nt=0
+nrejected=0
+do iorder=1,norder
+ mi(1:K-iorder)=0
+ mi(K-iorder+1:K)=1
+ iflag=0
+ do while(iflag .ge. 0 )
+ dpat=sum(mi*absrx(1:K))
+ nt=nt+1
+ if( dpat .lt. thresh ) then ! reject unlikely error patterns
+ me=ieor(m0,mi)
+ call mrbencode(me,ce,g2,N,K)
+ nxor=ieor(ce,hdec)
+ dd=sum(nxor*absrx)
+ if( dd .lt. dmin ) then
+ dmin=dd
+ cw=ce
+ nhardmin=sum(nxor)
+ thresh=rho*dmin
+ endif
+ else
+ nrejected=nrejected+1
+ endif
+! get the next test error pattern, iflag will go negative
+! when the last pattern with weight iorder has been generated
+ call nextpat(mi,k,iorder,iflag)
+ enddo
+enddo
+
+!write(*,*) 'nhardmin ',nhardmin
+!write(*,*) 'total patterns ',nt,' number rejected ',nrejected
+
+! re-order the codeword to place message bits at the end
+cw(indices)=cw
+hdec(indices)=hdec
+decoded=cw(M+1:N)
+nerr=0
+do i=1,N
+ if( hdec(i) .ne. cw(i) ) nerr=nerr+1
+enddo
+niterations=nerr
+return
+end subroutine osd300
+
+subroutine mrbencode(me,codeword,g2,N,K)
+integer*1 me(K),codeword(N),g2(N,K)
+! fast encoding for low-weight test patterns
+ codeword=0
+ do i=1,K
+ if( me(i) .eq. 1 ) then
+ codeword=ieor(codeword,g2(1:N,i))
+ endif
+ enddo
+return
+end subroutine mrbencode
+
+subroutine nextpat(mi,k,iorder,iflag)
+ integer*1 mi(k),ms(k)
+! generate the next test error pattern
+ ind=-1
+ do i=1,k-1
+ if( mi(i).eq.0 .and. mi(i+1).eq.1) ind=i
+ enddo
+ if( ind .lt. 0 ) then ! no more patterns of this order
+ iflag=ind
+ return
+ endif
+ ms=0
+ ms(1:ind-1)=mi(1:ind-1)
+ ms(ind)=1
+ ms(ind+1)=0
+ if( ind+1 .lt. k ) then
+ nz=iorder-sum(ms)
+ ms(k-nz+1:k)=1
+ endif
+ mi=ms
+ iflag=ind
+ return
+end subroutine nextpat
diff --git a/lib/fsk4hf/polyfit4.f90 b/lib/fsk4hf/polyfit4.f90
new file mode 100644
index 00000000..892cd2cd
--- /dev/null
+++ b/lib/fsk4hf/polyfit4.f90
@@ -0,0 +1,109 @@
+subroutine polyfit4(x,y,sigmay,npts,nterms,mode,a,chisqr)
+
+ parameter (MAXN=20)
+ implicit real*8 (a-h,o-z)
+ real x(npts), y(npts), sigmay(npts), a(nterms),chisqr
+ real*8 sumx(2*MAXN-1), sumy(MAXN), array(MAXN,MAXN)
+
+! Accumulate weighted sums
+ nmax = 2*nterms-1
+ sumx=0.
+ sumy=0.
+ chisq=0.
+ do i=1,npts
+ xi=x(i)
+ yi=y(i)
+ if(mode.lt.0) then
+ weight=1./abs(yi)
+ else if(mode.eq.0) then
+ weight=1
+ else
+ weight=1./sigmay(i)**2
+ end if
+ xterm=weight
+ do n=1,nmax
+ sumx(n)=sumx(n)+xterm
+ xterm=xterm*xi
+ enddo
+ yterm=weight*yi
+ do n=1,nterms
+ sumy(n)=sumy(n)+yterm
+ yterm=yterm*xi
+ enddo
+ chisq=chisq+weight*yi**2
+ enddo
+
+! Construct matrices and calculate coefficients
+ do j=1,nterms
+ do k=1,nterms
+ n=j+k-1
+ array(j,k)=sumx(n)
+ enddo
+ enddo
+
+ delta=determ4(array,nterms)
+ if(delta.eq.0) then
+ chisqr=0.
+ a=0.
+ else
+ do l=1,nterms
+ do j=1,nterms
+ do k=1,nterms
+ n=j+k-1
+ array(j,k)=sumx(n)
+ enddo
+ array(j,l)=sumy(j)
+ enddo
+ a(l)=determ4(array,nterms)/delta
+ enddo
+
+! Calculate chi square
+
+ do j=1,nterms
+ chisq=chisq-2*a(j)*sumy(j)
+ do k=1,nterms
+ n=j+k-1
+ chisq=chisq+a(j)*a(k)*sumx(n)
+ enddo
+ enddo
+ free=npts-nterms
+ chisqr=chisq/free
+ end if
+
+ return
+end subroutine polyfit4
+
+real*8 function determ4(array,norder)
+
+ parameter (MAXN=20)
+ implicit real*8 (a-h,o-z)
+ real*8 array(MAXN,MAXN)
+
+ determ4=1.
+ do k=1,norder
+ if (array(k,k).ne.0) go to 41
+ do j=k,norder
+ if(array(k,j).ne.0) go to 31
+ enddo
+ determ4=0.
+ go to 60
+
+31 do i=k,norder
+ s8=array(i,j)
+ array(i,j)=array(i,k)
+ array(i,k)=s8
+ enddo
+ determ4=-1.*determ4
+41 determ4=determ4*array(k,k)
+ if(k.lt.norder) then
+ k1=k+1
+ do i=k1,norder
+ do j=k1,norder
+ array(i,j)=array(i,j)-array(i,k)*array(k,j)/array(k,k)
+ enddo
+ enddo
+ end if
+ enddo
+
+60 return
+end function determ4
diff --git a/lib/fsk4hf/spec4.f90 b/lib/fsk4hf/spec4.f90
new file mode 100644
index 00000000..e0588950
--- /dev/null
+++ b/lib/fsk4hf/spec4.f90
@@ -0,0 +1,35 @@
+subroutine spec4(c,s,savg)
+
+ parameter (KK=84) !Information bits (72 + CRC12)
+ parameter (ND=84) !Data symbols: LDPC (168,84), r=1/2
+ parameter (NS=12) !Sync symbols (3 @ 4x4 Costas arrays)
+ parameter (NR=2) !Ramp up/down
+ parameter (NN=NR+NS+ND) !Total symbols (98)
+ parameter (NSPS=2688/84) !Samples per symbol (32)
+ parameter (NZ=NSPS*NN) !Samples in baseband waveform (3760)
+ parameter (NFFT=2*NSPS,NH=NSPS)
+
+ complex c(0:NZ-1)
+ complex c1(0:NFFT-1)
+ real s(0:NH,NN)
+ real savg(0:NH)
+
+ fs=12000.0/84.0
+ df=fs/NFFT
+ savg=0.
+ do j=1,NN
+ ia=(j-1)*NSPS
+ ib=ia + NSPS-1
+ c1(0:NSPS-1)=c(ia:ib)
+ c1(NSPS:)=0.
+ call four2a(c1,NFFT,1,-1,1)
+ do k=1,NSPS
+ s(k,j)=real(c1(k))**2 + aimag(c1(k))**2
+ enddo
+ savg=savg+s(0:NH,j)
+ enddo
+ s=s/NZ
+ savg=savg/(NN*NZ)
+
+ return
+end subroutine spec4
diff --git a/lib/fsk4hf/spec8.f90 b/lib/fsk4hf/spec8.f90
new file mode 100644
index 00000000..43fbc7ee
--- /dev/null
+++ b/lib/fsk4hf/spec8.f90
@@ -0,0 +1,31 @@
+subroutine spec8(c,s,savg)
+
+ include 'wspr_fsk8_params.f90'
+ complex c(0:NMAXD-1)
+ complex c1(0:NSPS-1)
+ real s(0:NH2,NN)
+ real savg(0:NH2)
+
+ fs=12000.0/NDOWN
+ df=fs/NSPS
+ savg=0.
+ do j=1,NN
+ ia=(j-1)*NSPS
+ ib=ia + NSPS-1
+ c1(0:NSPS-1)=c(ia:ib)
+ c1(NSPS:)=0.
+ call four2a(c1,NSPS,1,-1,1)
+ do i=0,NH2
+ s(i,j)=real(c1(i))**2 + aimag(c1(i))**2
+ enddo
+ savg=savg+s(0:NH2,j)
+ enddo
+ s=s/NZ
+ savg=savg/(NN*NZ)
+! do i=0,NH2
+! write(31,3101) i*df,savg(i)
+!3101 format(f10.3,f12.3)
+! enddo
+
+ return
+end subroutine spec8
diff --git a/lib/fsk4hf/subtractft8.f90 b/lib/fsk4hf/subtractft8.f90
new file mode 100644
index 00000000..1031cf2b
--- /dev/null
+++ b/lib/fsk4hf/subtractft8.f90
@@ -0,0 +1,61 @@
+subroutine subtractft8(dd,itone,f0,dt)
+
+! Subtract an ft8 signal
+!
+! Measured signal : dd(t) = a(t)cos(2*pi*f0*t+theta(t))
+! Reference signal : cref(t) = exp( j*(2*pi*f0*t+phi(t)) )
+! Complex amp : cfilt(t) = LPF[ dd(t)*CONJG(cref(t)) ]
+! Subtract : dd(t) = dd(t) - 2*REAL{cref*cfilt}
+
+ use timer_module, only: timer
+
+ parameter (NMAX=15*12000,NFRAME=1920*79)
+ parameter (NFFT=NMAX,NFILT=1400)
+ real*4 dd(NMAX), window(-NFILT/2:NFILT/2)
+ complex cref,camp,cfilt,cw
+ integer itone(79)
+ logical first
+ data first/.true./
+ common/heap8/cref(NFRAME),camp(NMAX),cfilt(NMAX),cw(NMAX)
+ save first
+
+ nstart=dt*12000+1
+ call genft8refsig(itone,cref,f0)
+ camp=0.
+ do i=1,nframe
+ id=nstart-1+i
+ if(id.ge.1.and.id.le.NMAX) camp(i)=dd(id)*conjg(cref(i))
+ enddo
+
+ if(first) then
+! Create and normalize the filter
+ pi=4.0*atan(1.0)
+ fac=1.0/float(nfft)
+ sum=0.0
+ do j=-NFILT/2,NFILT/2
+ window(j)=cos(pi*j/NFILT)**2
+ sum=sum+window(j)
+ enddo
+ cw=0.
+ cw(1:NFILT+1)=window/sum
+ cw=cshift(cw,NFILT/2+1)
+ call four2a(cw,nfft,1,-1,1)
+ cw=cw*fac
+ first=.false.
+ endif
+
+ cfilt=0.0
+ cfilt(1:nframe)=camp(1:nframe)
+ call four2a(cfilt,nfft,1,-1,1)
+ cfilt(1:nfft)=cfilt(1:nfft)*cw(1:nfft)
+ call four2a(cfilt,nfft,1,1,1)
+
+! Subtract the reconstructed signal
+ do i=1,nframe
+ j=nstart+i-1
+ if(j.ge.1 .and. j.le.NMAX) dd(j)=dd(j)-2*REAL(cfilt(i)*cref(i))
+ enddo
+
+ return
+end subroutine subtractft8
+
diff --git a/lib/fsk4hf/sync8.f90 b/lib/fsk4hf/sync8.f90
new file mode 100644
index 00000000..30f6de17
--- /dev/null
+++ b/lib/fsk4hf/sync8.f90
@@ -0,0 +1,151 @@
+subroutine sync8(dd,nfa,nfb,syncmin,nfqso,s,candidate,ncand,sbase)
+
+ include 'ft8_params.f90'
+! Search over +/- 2.5s relative to 0.5s TX start time.
+ parameter (JZ=62)
+ complex cx(0:NH1)
+ real s(NH1,NHSYM)
+ real savg(NH1)
+ real sbase(NH1)
+ real x(NFFT1)
+ real sync2d(NH1,-JZ:JZ)
+ real red(NH1)
+ real candidate0(3,200)
+ real candidate(3,200)
+ real dd(NMAX)
+ integer jpeak(NH1)
+ integer indx(NH1)
+ integer ii(1)
+ integer icos7(0:6)
+ data icos7/2,5,6,0,4,1,3/ !Costas 7x7 tone pattern
+ equivalence (x,cx)
+
+! Compute symbol spectra, stepping by NSTEP steps.
+ savg=0.
+ tstep=NSTEP/12000.0
+ df=12000.0/NFFT1 !3.125 Hz
+ fac=1.0/300.0
+ do j=1,NHSYM
+ ia=(j-1)*NSTEP + 1
+ ib=ia+NSPS-1
+ x(1:NSPS)=fac*dd(ia:ib)
+ x(NSPS+1:)=0.
+ call four2a(x,NFFT1,1,-1,0) !r2c FFT
+ do i=1,NH1
+ s(i,j)=real(cx(i))**2 + aimag(cx(i))**2
+ enddo
+ savg=savg + s(1:NH1,j) !Average spectrum
+ enddo
+ call baseline(savg,nfa,nfb,sbase)
+! savg=savg/NHSYM
+! do i=1,NH1
+! write(51,3051) i*df,savg(i),db(savg(i))
+!3051 format(f10.3,e12.3,f12.3)
+! enddo
+
+ ia=max(1,nint(nfa/df))
+ ib=nint(nfb/df)
+ nssy=NSPS/NSTEP ! # steps per symbol
+ nfos=NFFT1/NSPS ! # frequency bin oversampling factor
+ jstrt=0.5/tstep
+
+ do i=ia,ib
+ do j=-JZ,+JZ
+ ta=0.
+ tb=0.
+ tc=0.
+ t0a=0.
+ t0b=0.
+ t0c=0.
+ do n=0,6
+ k=j+jstrt+nssy*n
+ if(k.ge.1.and.k.le.NHSYM) then
+ ta=ta + s(i+nfos*icos7(n),k)
+ t0a=t0a + sum(s(i:i+nfos*6:nfos,k))
+ endif
+ tb=tb + s(i+nfos*icos7(n),k+nssy*36)
+ t0b=t0b + sum(s(i:i+nfos*6:nfos,k+nssy*36))
+ if(k+nssy*72.le.NHSYM) then
+ tc=tc + s(i+nfos*icos7(n),k+nssy*72)
+ t0c=t0c + sum(s(i:i+nfos*6:nfos,k+nssy*72))
+ endif
+ enddo
+ t=ta+tb+tc
+ t0=t0a+t0b+t0c
+ t0=(t0-t)/6.0
+ sync_abc=t/t0
+
+ t=tb+tc
+ t0=t0b+t0c
+ t0=(t0-t)/6.0
+ sync_bc=t/t0
+ sync2d(i,j)=max(sync_abc,sync_bc)
+ enddo
+ enddo
+
+ red=0.
+ do i=ia,ib
+ ii=maxloc(sync2d(i,-JZ:JZ)) - 1 - JZ
+ j0=ii(1)
+ jpeak(i)=j0
+ red(i)=sync2d(i,j0)
+! write(52,3052) i*df,red(i),db(red(i))
+!3052 format(3f12.3)
+ enddo
+ iz=ib-ia+1
+ call indexx(red(ia:ib),iz,indx)
+ ibase=indx(nint(0.40*iz)) - 1 + ia
+ base=red(ibase)
+ red=red/base
+
+ candidate0=0.
+ k=0
+ do i=1,200
+ n=ia + indx(iz+1-i) - 1
+ if(red(n).lt.syncmin) exit
+ if(k.lt.200) k=k+1
+ candidate0(1,k)=n*df
+ candidate0(2,k)=(jpeak(n)-1)*tstep
+ candidate0(3,k)=red(n)
+ enddo
+ ncand=k
+
+! Put nfqso at top of list, and save only the best of near-dupe freqs.
+ do i=1,ncand
+ if(abs(candidate0(1,i)-nfqso).lt.10.0) candidate0(1,i)=-candidate0(1,i)
+ if(i.ge.2) then
+ do j=1,i-1
+ fdiff=abs(candidate0(1,i))-abs(candidate0(1,j))
+ if(abs(fdiff).lt.4.0) then
+ if(candidate0(3,i).ge.candidate0(3,j)) candidate0(3,j)=0.
+ if(candidate0(3,i).lt.candidate0(3,j)) candidate0(3,i)=0.
+ endif
+ enddo
+! write(*,3001) i,candidate0(1,i-1),candidate0(1,i),candidate0(3,i-1), &
+! candidate0(3,i)
+!3001 format(i2,4f8.1)
+ endif
+ enddo
+
+ fac=20.0/maxval(s)
+ s=fac*s
+
+! Sort by sync
+! call indexx(candidate0(3,1:ncand),ncand,indx)
+! Sort by frequency
+ call indexx(candidate0(1,1:ncand),ncand,indx)
+ k=1
+! do i=ncand,1,-1
+ do i=1,ncand
+ j=indx(i)
+! if( candidate0(3,j) .ge. syncmin .and. candidate0(2,j).ge.-1.5 ) then
+ if( candidate0(3,j) .ge. syncmin ) then
+ candidate(1,k)=abs(candidate0(1,j))
+ candidate(2,k)=candidate0(2,j)
+ candidate(3,k)=candidate0(3,j)
+ k=k+1
+ endif
+ enddo
+ ncand=k-1
+ return
+end subroutine sync8
diff --git a/lib/fsk4hf/sync8d.f90 b/lib/fsk4hf/sync8d.f90
new file mode 100644
index 00000000..d0d1c63b
--- /dev/null
+++ b/lib/fsk4hf/sync8d.f90
@@ -0,0 +1,54 @@
+subroutine sync8d(cd0,i0,ctwk,itwk,sync)
+
+! Compute sync power for a complex, downsampled FT8 signal.
+
+ parameter(NP2=2812,NDOWN=60)
+ complex cd0(3125)
+ complex csync(0:6,32)
+ complex csync2(32)
+ complex ctwk(32)
+ complex z1,z2,z3
+ logical first
+ integer icos7(0:6)
+ data icos7/2,5,6,0,4,1,3/
+ data first/.true./
+ save first,twopi,fs2,dt2,taus,baud,csync
+
+ p(z1)=real(z1)**2 + aimag(z1)**2 !Statement function for power
+
+! Set some constants and compute the csync array.
+ if( first ) then
+ twopi=8.0*atan(1.0)
+ fs2=12000.0/NDOWN !Sample rate after downsampling
+ dt2=1/fs2 !Corresponding sample interval
+ taus=32*dt2 !Symbol duration
+ baud=1.0/taus !Keying rate
+ do i=0,6
+ phi=0.0
+ dphi=twopi*icos7(i)*baud*dt2
+ do j=1,32
+ csync(i,j)=cmplx(cos(phi),sin(phi)) !Waveform for 7x7 Costas array
+ phi=mod(phi+dphi,twopi)
+ enddo
+ enddo
+ first=.false.
+ endif
+
+ sync=0
+ do i=0,6 !Sum over 7 Costas frequencies and
+ i1=i0+i*32 !three Costas arrays
+ i2=i1+36*32
+ i3=i1+72*32
+ csync2=csync(i,1:32)
+ if(itwk.eq.1) csync2=ctwk*csync2 !Tweak the frequency
+ z1=0.
+ z2=0.
+ z3=0.
+ if(i1.ge.1 .and. i1+31.le.NP2) z1=sum(cd0(i1:i1+31)*conjg(csync2))
+ if(i2.ge.1 .and. i2+31.le.NP2) z2=sum(cd0(i2:i2+31)*conjg(csync2))
+ if(i3.ge.1 .and. i3+31.le.NP2) z3=sum(cd0(i3:i3+31)*conjg(csync2))
+ sync = sync + p(z1) + p(z2) + p(z3)
+ enddo
+
+ return
+end subroutine sync8d
diff --git a/lib/fsk4hf/tweak1.f90 b/lib/fsk4hf/tweak1.f90
new file mode 100644
index 00000000..c62c4087
--- /dev/null
+++ b/lib/fsk4hf/tweak1.f90
@@ -0,0 +1,23 @@
+subroutine tweak1(ca,jz,f0,cb)
+
+! Shift frequency of analytic signal ca, with output to cb
+
+ complex ca(jz),cb(jz)
+ real*8 twopi
+ complex*16 w,wstep
+ complex w4
+ data twopi/0.d0/
+ save twopi
+
+ if(twopi.eq.0.d0) twopi=8.d0*atan(1.d0)
+ w=1.d0
+ dphi=twopi*f0/12000.d0
+ wstep=cmplx(cos(dphi),sin(dphi))
+ do i=1,jz
+ w=w*wstep
+ w4=w
+ cb(i)=w4*ca(i)
+ enddo
+
+ return
+end subroutine tweak1
diff --git a/lib/fsk4hf/twkfreq1.f90 b/lib/fsk4hf/twkfreq1.f90
new file mode 100644
index 00000000..e6aa8f8b
--- /dev/null
+++ b/lib/fsk4hf/twkfreq1.f90
@@ -0,0 +1,26 @@
+subroutine twkfreq1(ca,npts,fsample,a,cb)
+
+ complex ca(npts)
+ complex cb(npts)
+ complex w,wstep
+ real a(5)
+ data twopi/6.283185307/
+
+! Mix the complex signal
+ w=1.0
+ wstep=1.0
+ x0=0.5*(npts+1)
+ s=2.0/npts
+ do i=1,npts
+ x=s*(i-x0)
+ p2=1.5*x*x - 0.5
+ p3=2.5*(x**3) - 1.5*x
+ p4=4.375*(x**4) - 3.75*(x**2) + 0.375
+ dphi=(a(1) + x*a(2) + p2*a(3) + p3*a(4) + p4*a(5)) * (twopi/fsample)
+ wstep=cmplx(cos(dphi),sin(dphi))
+ w=w*wstep
+ cb(i)=w*ca(i)
+ enddo
+
+ return
+end subroutine twkfreq1
diff --git a/lib/fsk4hf/watterson.f90 b/lib/fsk4hf/watterson.f90
new file mode 100644
index 00000000..d047a26e
--- /dev/null
+++ b/lib/fsk4hf/watterson.f90
@@ -0,0 +1,62 @@
+subroutine watterson(c,npts,fs,delay,fspread)
+
+ complex c(0:npts-1)
+ complex c2(0:npts-1)
+ complex cs1(0:npts-1)
+ complex cs2(0:npts-1)
+
+ nonzero=0
+ df=fs/npts
+ if(fspread.gt.0.0) then
+ do i=0,npts-1
+ xx=gran()
+ yy=gran()
+ cs1(i)=0.707*cmplx(xx,yy)
+ xx=gran()
+ yy=gran()
+ cs2(i)=0.707*cmplx(xx,yy)
+ enddo
+ call four2a(cs1,npts,1,-1,1) !To freq domain
+ call four2a(cs2,npts,1,-1,1)
+ do i=0,npts-1
+ f=i*df
+ if(i.gt.npts/2) f=(i-npts)*df
+ x=(f/(0.5*fspread))**2
+ a=0.
+ if(x.le.50.0) then
+ a=exp(-x)
+ endif
+ cs1(i)=a*cs1(i)
+ cs2(i)=a*cs2(i)
+ if(abs(f).lt.10.0) then
+ p1=real(cs1(i))**2 + aimag(cs1(i))**2
+ p2=real(cs2(i))**2 + aimag(cs2(i))**2
+ if(p1.gt.0.0) nonzero=nonzero+1
+! write(62,3101) f,p1,p2,db(p1+1.e-12)-60,db(p2+1.e-12)-60
+!3101 format(f10.3,2f12.3,2f10.3)
+ endif
+ enddo
+ call four2a(cs1,npts,1,1,1) !Back to time domain
+ call four2a(cs2,npts,1,1,1)
+ cs1(0:npts-1)=cs1(0:npts-1)/npts
+ cs2(0:npts-1)=cs2(0:npts-1)/npts
+ endif
+
+ nshift=nint(0.001*delay*fs)
+ c2(0:npts-1)=cshift(c(0:npts-1),nshift)
+ sq=0.
+ do i=0,npts-1
+ if(nonzero.gt.1) then
+ c(i)=0.5*(cs1(i)*c(i) + cs2(i)*c2(i))
+ else
+ c(i)=0.5*(c(i) + c2(i))
+ endif
+ sq=sq + real(c(i))**2 + aimag(c(i))**2
+! write(61,3001) i/12000.0,c(i)
+!3001 format(3f12.6)
+ enddo
+ rms=sqrt(sq/npts)
+ c=c/rms
+
+ return
+end subroutine watterson
diff --git a/lib/fsk4hf/wavhdr.f90 b/lib/fsk4hf/wavhdr.f90
new file mode 100644
index 00000000..b54fba78
--- /dev/null
+++ b/lib/fsk4hf/wavhdr.f90
@@ -0,0 +1,110 @@
+module wavhdr
+ type hdr
+ character*4 ariff
+ integer*4 lenfile
+ character*4 awave
+ character*4 afmt
+ integer*4 lenfmt
+ integer*2 nfmt2
+ integer*2 nchan2
+ integer*4 nsamrate
+ integer*4 nbytesec
+ integer*2 nbytesam2
+ integer*2 nbitsam2
+ character*4 adata
+ integer*4 ndata
+ end type hdr
+
+ contains
+
+ function default_header(nsamrate,npts)
+ type(hdr) default_header,h
+ h%ariff='RIFF'
+ h%awave='WAVE'
+ h%afmt='fmt '
+ h%lenfmt=16
+ h%nfmt2=1
+ h%nchan2=1
+ h%nsamrate=nsamrate
+ h%nbitsam2=16
+ h%nbytesam2=h%nbitsam2 * h%nchan2 / 8
+ h%adata='data'
+ h%nbytesec=h%nsamrate * h%nbitsam2 * h%nchan2 / 8
+ h%ndata=2*npts
+ h%lenfile=h%ndata + 44 - 8
+ default_header=h
+ end function default_header
+
+ subroutine set_wsjtx_wav_params(fMHz,mode,nsubmode,ntrperiod,id2)
+
+ parameter (NBANDS=23,NMODES=11)
+ character*8 mode,modes(NMODES)
+ integer*2 id2(4)
+ integer iperiod(7)
+ real fband(NBANDS)
+ data fband/0.137,0.474,1.8,3.5,5.1,7.0,10.14,14.0,18.1,21.0,24.9, &
+ 28.0,50.0,144.0,222.0,432.0,902.0,1296.0,2304.0,3400.0, &
+ 5760.0,10368.0,24048.0/
+ data modes/'Echo','FSK441','ISCAT','JT4','JT65','JT6M','JT9', &
+ 'JT9+JT65','JTMS','JTMSK','WSPR'/
+ data iperiod/5,10,15,30,60,120,900/
+
+ dmin=1.e30
+ iband=0
+ do i=1,NBANDS
+ if(abs(fMHz-fband(i)).lt.dmin) then
+ dmin=abs(fMHz-fband(i))
+ iband=i
+ endif
+ enddo
+
+ imode=0
+ do i=1,NMODES
+ if(mode.eq.modes(i)) imode=i
+ enddo
+
+ ip=0
+ do i=1,7
+ if(ntrperiod.eq.iperiod(i)) ip=i
+ enddo
+
+ id2(1)=iband
+ id2(2)=imode
+ id2(3)=nsubmode
+ id2(4)=ip
+
+ return
+ end subroutine set_wsjtx_wav_params
+
+ subroutine get_wsjtx_wav_params(id2,band,mode,nsubmode,ntrperiod,ok)
+
+ parameter (NBANDS=23,NMODES=11)
+ character*8 mode,modes(NMODES)
+ character*6 band,bands(NBANDS)
+ integer*2 id2(4)
+ integer iperiod(7)
+ logical ok
+ data modes/'Echo','FSK441','ISCAT','JT4','JT65','JT6M','JT9', &
+ 'JT9+JT65','JTMS','JTMSK','WSPR'/
+ data iperiod/5,10,15,30,60,120,900/
+ data bands/'2190m','630m','160m','80m','60m','40m','30m','20m', &
+ '17m','15m','12m','10m','6m','2m','1.25m','70cm','33cm', &
+ '23cm','13cm','9cm','6cm','3cm','1.25cm'/
+
+ ok=.true.
+ if(id2(1).lt.1 .or. id2(1).gt.NBANDS) ok=.false.
+ if(id2(2).lt.1 .or. id2(2).gt.NMODES) ok=.false.
+ if(id2(3).lt.1 .or. id2(3).gt.8) ok=.false.
+ if(id2(4).lt.1 .or. id2(4).gt.7) ok=.false.
+
+ if(ok) then
+ band=bands(id2(1))
+ mode=modes(id2(2))
+ nsubmode=id2(3)
+ ntrperiod=iperiod(id2(4))
+ endif
+
+ return
+ end subroutine get_wsjtx_wav_params
+
+end module wavhdr
diff --git a/lib/fsk4hf/wspr5_downsample.f90 b/lib/fsk4hf/wspr5_downsample.f90
new file mode 100644
index 00000000..6fdb9021
--- /dev/null
+++ b/lib/fsk4hf/wspr5_downsample.f90
@@ -0,0 +1,29 @@
+subroutine wspr5_downsample(iwave,c)
+
+! Input: i*2 data in iwave() at sample rate 12000 Hz
+! Output: Complex data in c(), sampled at 400 Hz
+
+ include 'wsprlf_params.f90'
+ parameter (NMAX=300*12000,NFFT2=NMAX/30)
+ integer*2 iwave(NMAX)
+ complex c(0:NZ-1)
+ complex c1(0:NFFT2-1)
+ complex cx(0:NMAX/2)
+ real x(NMAX)
+ equivalence (x,cx)
+
+ df=12000.0/NMAX
+ x=iwave
+ call four2a(x,NMAX,1,-1,0) !r2c FFT to freq domain
+ i0=nint(1500.0/df)
+ c1(0)=cx(i0)
+ do i=1,NFFT2/2
+ c1(i)=cx(i0+i)
+ c1(NFFT2-i)=cx(i0-i)
+ enddo
+ c1=c1/NFFT2
+ call four2a(c1,NFFT2,1,1,1) !c2c FFT back to time domain
+ c=c1(0:NZ-1)
+
+ return
+end subroutine wspr5_downsample
diff --git a/lib/fsk4hf/wspr5_wav.f90 b/lib/fsk4hf/wspr5_wav.f90
new file mode 100644
index 00000000..0b09ccde
--- /dev/null
+++ b/lib/fsk4hf/wspr5_wav.f90
@@ -0,0 +1,48 @@
+subroutine wspr5_wav(baud,xdt,f0,itone,snrdb,iwave)
+
+! Generate iwave() from itone().
+
+ include 'wsprlf_params.f90'
+ parameter (NMAX=300*12000)
+ integer itone(NN)
+ integer*2 iwave(NMAX)
+ real*8 twopi,dt,dphi0,dphi1,dphi,phi
+ real dat(NMAX)
+
+ twopi=8.d0*atan(1.d0)
+ dt=1.d0/12000.d0
+
+ dat=0.
+ if(snrdb.lt.90) then
+ do i=1,NMAX
+ dat(i)=gran() !Generate gaussian noise
+ enddo
+ bandwidth_ratio=2500.0/6000.0
+ sig=sqrt(2*bandwidth_ratio)*10.0**(0.05*snrdb)
+ else
+ sig=1.0
+ endif
+
+ dphi0=twopi*(f0-0.25d0*baud)*dt
+ dphi1=twopi*(f0+0.25d0*baud)*dt
+ phi=0.d0
+ k=nint(xdt/dt)
+ do j=1,NN
+ dphi=dphi0
+ if(itone(j).eq.1) dphi=dphi1
+ if(k.eq.0) phi=-dphi
+ do i=1,NSPS0
+ k=k+1
+ phi=phi+dphi
+ if(phi.gt.twopi) phi=phi-twopi
+ xphi=phi
+ if(k.gt.0 .and. k.le.NMAX) dat(k)=dat(k) + sig*sin(xphi)
+ enddo
+ enddo
+ fac=32767.0
+ rms=100.0
+ if(snrdb.ge.90.0) iwave=nint(fac*dat)
+ if(snrdb.lt.90.0) iwave=nint(rms*dat)
+
+ return
+end subroutine wspr5_wav
diff --git a/lib/fsk4hf/wspr5d.f90 b/lib/fsk4hf/wspr5d.f90
new file mode 100644
index 00000000..7b50fff2
--- /dev/null
+++ b/lib/fsk4hf/wspr5d.f90
@@ -0,0 +1,217 @@
+program wspr5d
+
+! Decode WSPR-LF data read from *.c5 or *.wav files.
+
+! 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.
+
+! 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
+
+! Still to do: find and decode more than one signal in the specified passband.
+
+ include 'wsprlf_params.f90'
+ parameter (NMAX=300*12000)
+ character arg*8,message*22,cbits*50,infile*80,fname*16,datetime*11
+ character*120 data_dir
+ 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
+ real*8 fMHz
+ 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
+ real fpks(20)
+ 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
+ integer ihdr(11)
+ integer*8 n8
+ integer*2 iwave(NMAX) !Generated full-length waveform
+ 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/
+
+ nargs=iargc()
+ if(nargs.lt.2) then
+ print*,'Usage: wspr5d [-a <data_dir>] [-f fMHz] file1 [file2 ...]'
+ go to 999
+ endif
+ iarg=1
+ data_dir="."
+ call getarg(iarg,arg)
+ if(arg(1:2).eq.'-a') then
+ call getarg(iarg+1,data_dir)
+ iarg=iarg+2
+ endif
+ call getarg(iarg,arg)
+ if(arg(1:2).eq.'-f') then
+ call getarg(iarg+1,arg)
+ read(arg,*) fMHz
+ iarg=iarg+2
+ endif
+
+ open(13,file=trim(data_dir)//'/ALL_WSPR.TXT',status='unknown', &
+ position='append')
+ maxn=8 !Default value
+ 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
+
+! Define array id() for sync symbols
+ 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
+
+ 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
+
+ do ifile=iarg,nargs
+ call getarg(ifile,infile)
+ open(10,file=infile,status='old',access='stream')
+ j1=index(infile,'.c5')
+ j2=index(infile,'.wav')
+ if(j1.gt.0) then
+ read(10,end=999) fname,ntrmin,fMHz,c
+ read(fname(8:11),*) nutc
+ write(datetime,'(i11)') nutc
+ else if(j2.gt.0) then
+ read(10,end=999) ihdr,iwave
+ read(infile(j2-4:j2-1),*) nutc
+ datetime=infile(j2-11:j2-1)
+ call wspr5_downsample(iwave,c)
+ else
+ print*,'Wrong file format?'
+ go to 999
+ endif
+ close(10)
+ fa=100.0
+ fb=150.0
+ call getfc1w(c,fs,fa,fb,fc1,xsnr) !First approx for freq
+ npeaks=20
+ call getfc2w(c,csync,npeaks,fs,fc1,fpks) !Refined freq
+
+ a(1)=-fc1
+ a(2:5)=0.
+ call twkfreq1(c,NZ,fs,a,c) !Mix c down by fc1+fc2
+
+! Find time offset xdt
+ amax=0.
+ jpk=0
+ iaa=0
+ ibb=NZ-1
+ jmax=1260
+ do j=-jmax,jmax,NSPS/8
+ 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)))
+write(51,*) j/fs,real(z),imag(z)
+ if(abs(z).gt.amax) then
+ amax=abs(z)
+ jpk=j
+ endif
+ enddo
+ xdt=jpk/fs
+xdt=1.0
+jpk=fs*xdt
+ do i=0,NZ-1
+ j=i+jpk
+ if(j.ge.0 .and. j.lt.NZ) c1(i)=c(j)
+ enddo
+
+ nterms=maxn
+ do itry=1,npeaks
+ nhard0=0
+ nhardsync0=0
+ ifer=1
+ a(1)=-fpks(itry)
+ 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)
+ if(niterations.lt.0) call osd300(llr,4,decoded,niterations,cw)
+ nbadcrc=0
+ if(niterations.ge.0) call chkcrc10(decoded,nbadcrc)
+ if(niterations.lt.0 .or. nbadcrc.ne.0) ifer=1
+ 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)
+ nsnr=nint(xsnr)
+! freq=fMHz + 1.d-6*(fc1+fc2)
+ freq=fMHz + 1.d-6*(fc1+fpks(itry))
+ nfdot=0
+ write(13,1110) datetime,0,nsnr,xdt,freq,message,nfdot
+1110 format(a11,2i4,f6.2,f12.7,2x,a22,i3)
+ write(*,1112) datetime(8:11),nsnr,xdt,freq,nfdot,message,itry
+1112 format(a4,i4,f5.1,f11.6,i3,2x,a22,i4)
+ endif
+ enddo ! ifile loop
+ write(*,1120)
+1120 format("<DecodeFinished>")
+
+999 end program wspr5d
diff --git a/lib/fsk4hf/wspr5d_exp.f90 b/lib/fsk4hf/wspr5d_exp.f90
new file mode 100644
index 00000000..10193fa7
--- /dev/null
+++ b/lib/fsk4hf/wspr5d_exp.f90
@@ -0,0 +1,499 @@
+program wspr5d
+
+! Decode WSPR-LF data read from *.c5 or *.wav files.
+
+! 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.
+!
+! Still to do: find and decode more than one signal in the specified passband.
+
+! include 'wsprlf_params.f90'
+
+ parameter (NDOWN=30)
+ parameter (KK=60)
+ parameter (ND=300)
+ parameter (NS=109)
+ parameter (NR=3)
+ parameter (NN=NR+NS+ND)
+ parameter (NSPS0=8640)
+ parameter (NSPS=16)
+ parameter (N2=2*NSPS)
+ parameter (NZ=NSPS*NN)
+ parameter (NZ400=288*NN)
+ parameter (NMAX=300*12000)
+
+ character arg*8,message*22,cbits*50,infile*80,fname*16,datetime*11
+ character*120 data_dir
+ complex csync(0:NZ-1) !Sync symbols only, from cbb
+ complex c400(0:NZ400-1) !Complex waveform
+ complex c(0:NZ-1) !Complex waveform
+ complex cd(0:NZ-1) !Complex waveform
+ complex ca(0:NZ-1) !Complex waveform
+ complex zz
+ real*8 fMHz
+ real rxdata(ND),llr(ND) !Soft symbols
+ real pp(32) !Shaped pulse for OQPSK
+ real sbits(412),softbits(9)
+ real fpks(20)
+ integer id(NS+ND) !NRZ values (+/-1) for Sync and Data
+ integer isync(48) !Long sync vector
+ integer ib13(13) !Barker 13 code
+ integer ihdr(11)
+ integer*8 n8
+ integer*2 iwave(NMAX) !Generated full-length waveform
+ integer*1 idat(7)
+ integer*1 decoded(KK),apmask(ND),cw(ND)
+ integer*1 hbits(412),bits(13)
+ data ib13/1,1,1,1,1,-1,-1,1,1,-1,1,-1,1/
+
+ nargs=iargc()
+ if(nargs.lt.2) then
+ print*,'Usage: wspr5d [-a <data_dir>] [-f fMHz] file1 [file2 ...]'
+ go to 999
+ endif
+ iarg=1
+ data_dir="."
+ call getarg(iarg,arg)
+ if(arg(1:2).eq.'-a') then
+ call getarg(iarg+1,data_dir)
+ iarg=iarg+2
+ endif
+ call getarg(iarg,arg)
+ if(arg(1:2).eq.'-f') then
+ call getarg(iarg+1,arg)
+ read(arg,*) fMHz
+ iarg=iarg+2
+ endif
+
+ open(13,file=trim(data_dir)//'/ALL_WSPR.TXT',status='unknown', &
+ position='append')
+ maxn=8 !Default value
+ 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,32 !Half-sine pulse shape
+ pp(i)=sin(0.5*(i-1)*twopi/(32))
+ enddo
+ n8=z'cbf089223a51'
+ do i=1,48
+ isync(i)=-1
+ if(iand(n8,1).eq.1) isync(i)=1
+ n8=n8/2
+ enddo
+
+! Define array id() for sync symbols
+ 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
+
+ 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
+
+ do ifile=iarg,nargs
+ call getarg(ifile,infile)
+ open(10,file=infile,status='old',access='stream')
+ j1=index(infile,'.c5')
+ j2=index(infile,'.wav')
+ if(j1.gt.0) then
+ read(10,end=999) fname,ntrmin,fMHz,c400
+ read(fname(8:11),*) nutc
+ write(datetime,'(i11)') nutc
+ else if(j2.gt.0) then
+ read(10,end=999) ihdr,iwave
+ read(infile(j2-4:j2-1),*) nutc
+ datetime=infile(j2-11:j2-1)
+ call wspr5_downsample(iwave,c400)
+ else
+ print*,'Wrong file format?'
+ go to 999
+ endif
+ close(10)
+
+ fa=100.0
+ fb=150.0
+ fs400=400.0
+ call getfc1(c400,fs400,fa,fb,fc1,xsnr) !First approx for freq
+!write(*,*) datetime,'initial guess ',fc1
+ npeaks=5
+ call getfc2(c400,npeaks,fs400,fc1,fpks) !Refined freq
+
+ do idf=1,npeaks ! consider the top npeak peaks
+ fc2=fpks(idf)
+ call downsample(c400,fc1+fc2,cd)
+ s2=sum(cd*conjg(cd))/(16*412)
+ cd=cd/sqrt(s2)
+ do is=0,8 ! dt search range is narrow, to save time.
+ idt=is/2
+ if( mod(is,2).eq. 1 ) idt=-(is+1)/2
+ xdt=real(22+idt)/22.222 - 1.0
+ ca=cshift(cd,22+idt)
+ do iseq=1,3 ! try sequence estimation lengths of 3, 6, and 9 bits.
+ k=1-2*iseq
+ nseq=iseq*3
+ do i=1,408,iseq*4
+ k=k+iseq*2
+ j=(i+1)*16
+ call mskseqdet(nseq,ca(j),pp,id(k),softbits,1,phase)
+ hbits(i:i+iseq*4)=bits
+ sbits(i:i+iseq*4)=bits
+
+ sbits(i+1)=softbits(1)
+ sbits(i+2)=softbits(2)
+ if( id(k+1) .ne. 0 ) sbits(i+2)=id(k+1)*25
+ sbits(i+3)=softbits(3)
+
+ if( iseq .ge. 2 ) then
+ sbits(i+5)=softbits(4)
+ sbits(i+6)=softbits(5)
+ if( id(k+3) .ne. 0 ) sbits(i+6)=id(k+3)*25
+ sbits(i+7)=softbits(6)
+ if( iseq .eq. 3 ) then
+ sbits(i+9)=softbits(7)
+ sbits(i+10)=softbits(8)
+ if( id(k+5) .ne. 0 ) sbits(i+10)=id(k+5)*25
+ sbits(i+11)=softbits(9)
+ endif
+ endif
+ enddo
+ j=1
+ do i=1,205
+ if( abs(id(i)) .ne. 2 ) then
+ rxdata(j)=sbits(2*i-1)
+ j=j+1
+ endif
+ enddo
+ do i=1,204
+ rxdata(j)=sbits(2*i)
+ j=j+1
+ enddo
+ rxav=sum(rxdata)/ND
+ rx2av=sum(rxdata*rxdata)/ND
+ rxsig=sqrt(rx2av-rxav*rxav)
+ rxdata=rxdata/rxsig
+! sigma=0.84
+ sigma=1.20
+ llr=2*rxdata/(sigma*sigma)
+ apmask=0
+ max_iterations=40
+ ifer=0
+ nbadcrc=0
+ call bpdecode300(llr,apmask,max_iterations,decoded,niterations,cw)
+! niterations will be equal to the Hamming distance between hard received word and the codeword
+ if(niterations.lt.0) call osd300(llr,3,decoded,niterations,cw)
+ if(niterations.ge.0) call chkcrc10(decoded,nbadcrc)
+ if(niterations.lt.0 .or. nbadcrc.ne.0) ifer=1
+ if( ifer.eq.0 ) then
+ write(cbits,1200) decoded(1:50)
+1200 format(50i1)
+ read(cbits,1202) idat
+1202 format(6b8,b2)
+ idat(7)=ishft(idat(7),6)
+ call wqdecode(idat,message,itype)
+ nsnr=nint(xsnr)
+ freq=fMHz + 1.d-6*(fc1+fc2)
+ nfdot=0
+ write(13,1210) datetime,0,nsnr,xdt,freq,message,nfdot
+1210 format(a11,2i4,f6.2,f12.7,2x,a22,i3)
+ write(*,1212) datetime(8:11),nsnr,xdt,freq,nfdot,message,'*',idf,nseq,is,niterations
+1212 format(a4,i4,f5.1,f11.6,i3,2x,a22,a1,i3,i3,i3,i4)
+ goto 888
+ endif
+ enddo !iseq
+ enddo
+ enddo
+888 continue
+ enddo
+
+ write(*,1120)
+1120 format("<DecodeFinished>")
+
+999 end program wspr5d
+
+subroutine getmetric(ib,ps,xmet)
+ real ps(0:511)
+ xm1=0
+ xm0=0
+ do i=0,511
+ if( iand(i/ib,1) .eq. 1 .and. ps(i) .gt. xm1 ) xm1=ps(i)
+ if( iand(i/ib,1) .eq. 0 .and. ps(i) .gt. xm0 ) xm0=ps(i)
+ enddo
+ xmet=xm1-xm0
+ return
+end subroutine getmetric
+
+subroutine mskseqdet(ns,cdat,pp,bsync,softbits,ncoh,phase)
+!
+! Detect sequences of 3, 6, or 9 bits (ns).
+! Sync bits are assumed to be known.
+!
+complex cdat(16*12),cbest(16*12),cideal(16*12)
+complex cdf(16*12),cfac,zz
+real cm(0:511),cmbest(0:511)
+real pp(32),softbits(9)
+integer bit(13),bestbits(13),sgn(13)
+integer bsync(7)
+
+twopi=8.0*atan(1.0)
+dt=30.0*18.0/12000.0
+cmax=0;
+fbest=0.0;
+np=2**ns-1
+idfmax=40
+if( ncoh .eq. 1 ) idfmax=0
+do idf=0,idfmax
+ if( mod(idf,2).eq.1 ) deltaf=idf/2*0.02
+ if( mod(idf,2).eq.1 ) deltaf=-(idf+1)/2*0.02
+ dphi=twopi*deltaf*dt
+ cfac=cmplx(cos(dphi),sin(dphi))
+ cdf=1.0
+ do i=2,16*(ns-1)
+ cdf(i)=cdf(i-1)*cfac
+ enddo
+
+ cm=0
+ ibflag=0
+ do i=0,np
+ bit(1)=(bsync(1)+2)/4
+ bit(2)=iand(i/(2**(ns-1)),1)
+ bit(3)=iand(i/(2**(ns-2)),1)
+ if( bsync(2).ne.0 ) then ! force the barker bits
+ bit(3)=(bsync(2)+2)/4
+ endif
+ bit(4)=iand(i/(2**(ns-3)),1)
+ bit(5)=(bsync(3)+2)/4
+
+ if( ns .ge. 6 ) then
+ bit(6)=iand(i/(2**(ns-4)),1)
+ bit(7)=iand(i/(2**(ns-5)),1)
+ if( bsync(4).ne.0 ) then ! force the barker bits
+ bit(7)=(bsync(4)+2)/4
+ endif
+ bit(8)=iand(i/(2**(ns-6)),1)
+ bit(9)=(bsync(5)+2)/4
+ if( ns .eq. 9 ) then
+ bit(10)=iand(i/4,1)
+ bit(11)=iand(i/2,1)
+ if( bsync(6).ne.0 ) then ! force the barker bits
+ bit(11)=(bsync(6)+2)/4
+ endif
+ bit(12)=iand(i/1,1)
+ bit(13)=(bsync(7)+2)/4
+ endif
+ endif
+
+ sgn=2*bit-1
+ cideal(1:16) =cmplx(sgn(1)*pp(17:32),sgn(2)*pp(1:16))
+ cideal(17:32) =cmplx(sgn(3)*pp(1:16),sgn(2)*pp(17:32))
+ cideal(33:48) =cmplx(sgn(3)*pp(17:32),sgn(4)*pp(1:16))
+ cideal(49:64) =cmplx(sgn(5)*pp(1:16),sgn(4)*pp(17:32))
+ if( ns .ge. 6 ) then
+ cideal(65:80) =cmplx(sgn(5)*pp(17:32),sgn(6)*pp(1:16))
+ cideal(81:96) =cmplx(sgn(7)*pp(1:16),sgn(6)*pp(17:32))
+ cideal(97:112) =cmplx(sgn(7)*pp(17:32),sgn(8)*pp(1:16))
+ cideal(113:128)=cmplx(sgn(9)*pp(1:16),sgn(8)*pp(17:32))
+ if( ns .eq. 9 ) then
+ cideal(129:144) =cmplx(sgn(9)*pp(17:32),sgn(10)*pp(1:16))
+ cideal(145:160) =cmplx(sgn(11)*pp(1:16),sgn(10)*pp(17:32))
+ cideal(161:176) =cmplx(sgn(11)*pp(17:32),sgn(12)*pp(1:16))
+ cideal(177:192)=cmplx(sgn(13)*pp(1:16),sgn(12)*pp(17:32))
+ endif
+ endif
+ cideal=cideal*cdf
+ cm(i)=abs(sum(cdat(1:64*ns/3)*conjg(cideal(1:64*ns/3))))/1.e3
+ if( cm(i) .gt. cmax ) then
+ ibflag=1
+ cmax=cm(i)
+ bestbits=bit
+ cbest=cideal
+ fbest=deltaf
+ zz=sum(cdat*conjg(cbest))/1.e3
+ phase=atan2(imag(zz),real(zz))
+ endif
+ enddo
+ if( ibflag .eq. 1 ) then ! new best found
+ cmbest=cm
+ endif
+enddo
+softbits=0.0
+call getmetric(1,cmbest,softbits(ns))
+call getmetric(2,cmbest,softbits(ns-1))
+call getmetric(4,cmbest,softbits(ns-2))
+if( ns .ge. 6 ) then
+ call getmetric(8,cmbest,softbits(ns-3))
+ call getmetric(16,cmbest,softbits(ns-4))
+ call getmetric(32,cmbest,softbits(ns-5))
+ if( ns .eq. 9 ) then
+ call getmetric(64,cmbest,softbits(3))
+ call getmetric(128,cmbest,softbits(2))
+ call getmetric(256,cmbest,softbits(1))
+ endif
+endif
+end subroutine mskseqdet
+
+subroutine downsample(ci,f0,co)
+ parameter(NI=412*288,NO=NI/18)
+ complex ci(0:NI-1),ct(0:NI-1)
+ complex co(0:NO-1)
+
+ df=400.0/NI
+ ct=ci
+ call four2a(ct,NI,1,-1,1) !c2c FFT to freq domain
+ i0=nint(f0/df)
+ co=0.0
+ co(0)=ct(i0)
+ b=3.0
+ do i=1,NO/2
+ arg=(i*df/b)**2
+ filt=exp(-arg)
+ co(i)=ct(i0+i)*filt
+ co(NO-i)=ct(i0-i)*filt
+ enddo
+ co=co/NO
+ call four2a(co,NO,1,1,1) !c2c FFT back to time domain
+ return
+end subroutine downsample
+
+subroutine getfc1(c,fs,fa,fb,fc1,xsnr)
+
+! include 'wsprlf_params.f90'
+ parameter (NZ=288*412)
+ parameter (NSPS=288)
+ parameter (N2=2*NSPS)
+ parameter (NFFT1=16*NSPS)
+ parameter (NH1=NFFT1/2)
+
+ complex c(0:NZ-1) !Complex waveform
+ complex c2(0:NFFT1-1) !Short spectra
+ real s(-NH1+1:NH1) !Coarse spectrum
+ nspec=NZ/N2
+ df1=fs/NFFT1
+ s=0.
+ do k=1,nspec
+ ia=(k-1)*N2
+ ib=ia+N2-1
+ c2(0:N2-1)=c(ia:ib)
+ c2(N2:)=0.
+ call four2a(c2,NFFT1,1,-1,1)
+ do i=0,NFFT1-1
+ j=i
+ if(j.gt.NH1) j=j-NFFT1
+ s(j)=s(j) + real(c2(i))**2 + aimag(c2(i))**2
+ enddo
+ enddo
+! call smo121(s,NFFT1)
+ smax=0.
+ ipk=0
+ fc1=0.
+ ia=nint(fa/df1)
+ ib=nint(fb/df1)
+ do i=ia,ib
+ f=i*df1
+ if(s(i).gt.smax) then
+ smax=s(i)
+ ipk=i
+ fc1=f
+ endif
+! write(51,3001) f,s(i),db(s(i))
+! 3001 format(f10.3,e12.3,f10.3)
+ enddo
+
+! The following is for testing SNR calibration:
+ sp3n=(s(ipk-1)+s(ipk)+s(ipk+1)) !Sig + 3*noise
+ base=(sum(s)-sp3n)/(NFFT1-3.0) !Noise per bin
+ psig=sp3n-3*base !Sig only
+ pnoise=(2500.0/df1)*base !Noise in 2500 Hz
+ xsnr=db(psig/pnoise)
+ xsnr=xsnr+5.0
+ return
+end subroutine getfc1
+
+subroutine getfc2(c,npeaks,fs,fc1,fpks)
+
+! include 'wsprlf_params.f90'
+ parameter (NZ=288*412)
+ parameter (NSPS=288)
+ parameter (N2=2*NSPS)
+ parameter (NFFT1=16*NSPS)
+ parameter (NH1=NFFT1/2)
+
+ complex c(0:NZ-1) !Complex waveform
+ complex cs(0:NZ-1) !For computing spectrum
+ real a(5)
+ real freqs(413),sp2(413),fpks(npeaks)
+ integer pkloc(1)
+
+ df=fs/NZ
+ baud=fs/NSPS
+ a(1)=-fc1
+ a(2:5)=0.
+ call twkfreq1(c,NZ,fs,a,cs) !Mix down by fc1
+
+! Filter, square, then FFT to get refined carrier frequency fc2.
+ call four2a(cs,NZ,1,-1,1) !To freq domain
+
+ ia=nint(0.75*baud/df)
+ cs(ia:NZ-1-ia)=0. !Save only freqs around fc1
+! do i=1,NZ/2
+! filt=1/(1+((i*df)**2/(0.50*baud)**2)**8)
+! cs(i)=cs(i)*filt
+! cs(NZ+1-i)=cs(NZ+1-i)*filt
+! enddo
+ call four2a(cs,NZ,1,1,1) !Back to time domain
+ cs=cs/NZ
+ cs=cs*cs !Square the data
+ call four2a(cs,NZ,1,-1,1) !Compute squared spectrum
+! Find two peaks separated by baud
+ pmax=0.
+ fc2=0.
+! ja=nint(0.3*baud/df)
+ ja=nint(0.5*baud/df)
+ k=1
+ sp2=0.0
+ do j=-ja,ja
+ f2=j*df
+ ia=nint((f2-0.5*baud)/df)
+ if(ia.lt.0) ia=ia+NZ
+ ib=nint((f2+0.5*baud)/df)
+ p=real(cs(ia))**2 + aimag(cs(ia))**2 + &
+ real(cs(ib))**2 + aimag(cs(ib))**2
+ if(p.gt.pmax) then
+ pmax=p
+ fc2=0.5*f2
+ endif
+ freqs(k)=0.5*f2
+ sp2(k)=p
+ k=k+1
+! write(52,1200) f2,p,db(p)
+!1200 format(f10.3,2f15.3)
+ enddo
+
+ do i=1,npeaks
+ pkloc=maxloc(sp2)
+ ipk=pkloc(1)
+ fpks(i)=freqs(ipk)
+ ipk0=max(1,ipk-2)
+ ipk1=min(413,ipk+2)
+! ipk0=ipk
+! ipk1=ipk
+ sp2(ipk0:ipk1)=0.0
+ enddo
+ return
+end subroutine getfc2
diff --git a/lib/fsk4hf/wspr5sim.f90 b/lib/fsk4hf/wspr5sim.f90
new file mode 100644
index 00000000..5c2147b9
--- /dev/null
+++ b/lib/fsk4hf/wspr5sim.f90
@@ -0,0 +1,111 @@
+program wspr5sim
+
+! Generate simulated data for a 5-minute "WSPR-LF" mode. Output is saved
+! to a *.c5 or *.wav file.
+
+ use wavhdr
+ include 'wsprlf_params.f90' !Set various constants
+ parameter (NMAX=300*12000)
+ type(hdr) h !Header for .wav file
+ character arg*12,fname*16
+ character msg*22,msgsent*22
+ complex c0(0:NZ-1)
+ complex c(0:NZ-1)
+ real*8 fMHz
+ integer itone(NN)
+ integer*2 iwave(NMAX) !Generated full-length waveform
+
+! Get command-line argument(s)
+ nargs=iargc()
+ if(nargs.ne.8) then
+ print*,'Usage: wspr5sim "message" f0 DT fsp del nwav nfiles snr'
+ print*,'Example: wspr5sim "K1ABC FN42 30" 50 0.0 0.1 1.0 1 10 -33'
+ go to 999
+ endif
+ call getarg(1,msg) !Message to be transmitted
+ call getarg(2,arg)
+ read(arg,*) f0 !Freq relative to WSPR-band center (Hz)
+ call getarg(3,arg)
+ read(arg,*) xdt !Time offset from nominal (s)
+ call getarg(4,arg)
+ read(arg,*) fspread !Watterson frequency spread (Hz)
+ call getarg(5,arg)
+ read(arg,*) delay !Watterson delay (ms)
+ call getarg(6,arg)
+ read(arg,*) nwav !1 for *.wav file, 0 for *.c5 file
+ call getarg(7,arg)
+ read(arg,*) nfiles !Number of files
+ call getarg(8,arg)
+ read(arg,*) snrdb !SNR_2500
+
+ twopi=8.0*atan(1.0)
+ fs=12000.0/NDOWN !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)
+ bandwidth_ratio=2500.0/(fs/2.0)
+ sig=sqrt(bandwidth_ratio) * 10.0**(0.05*snrdb)
+ if(snrdb.gt.90.0) sig=1.0
+ txt=NN*NSPS0/12000.0
+
+ call genwspr5(msg,msgsent,itone) !Encode the message, get itone
+ write(*,1000) f0,xdt,txt,snrdb,fspread,delay,nfiles,msgsent
+1000 format('f0:',f9.3,' DT:',f6.2,' txt:',f6.1,' SNR:',f6.1, &
+ ' fspread:',f6.1,' delay:',f6.1,' nfiles:',i3,2x,a22)
+
+ dphi0=twopi*(f0-0.25*baud)*dt
+ dphi1=twopi*(f0+0.25*baud)*dt
+ phi=0.0
+ c0=0.
+ k=-1 + nint(xdt/dt)
+ do j=1,NN !Generate OQPSK waveform from itone
+ dphi=dphi0
+ if(itone(j).eq.1) dphi=dphi1
+ if(k.eq.0) phi=-dphi
+ do i=1,NSPS
+ k=k+1
+ phi=phi+dphi
+ if(phi.gt.twopi) phi=phi-twopi
+ xphi=phi
+ if(k.ge.0 .and. k.lt.NZ) c0(k)=cmplx(cos(xphi),sin(xphi))
+ enddo
+ enddo
+
+ call sgran()
+ do ifile=1,nfiles
+ c=c0
+ if(nwav.eq.0) then
+ if( fspread .ne. 0.0 .or. delay .ne. 0.0 ) then
+ call watterson(c,NZ,fs,delay,fspread)
+ endif
+ c=c*sig
+ if(snrdb.lt.90) then
+ do i=0,NZ-1 !Add gaussian noise at specified SNR
+ xnoise=gran()
+ ynoise=gran()
+ c(i)=c(i) + cmplx(xnoise,ynoise)
+ enddo
+ endif
+ write(fname,1100) ifile
+1100 format('000000_',i4.4,'.c5')
+ open(10,file=fname,status='unknown',access='stream')
+ fMHz=10.1387d0
+ nmin=5
+ write(10) fname,nmin,fMHz,c !Save to *.c5 file
+ close(10)
+ else
+ call wspr5_wav(baud,xdt,f0,itone,snrdb,iwave)
+ h=default_header(12000,NMAX)
+ write(fname,1102) ifile
+1102 format('000000_',i4.4,'.wav')
+ open(10,file=fname,status='unknown',access='stream')
+ write(10) h,iwave !Save to *.wav file
+ close(10)
+ endif
+ write(*,1110) ifile,xdt,f0,snrdb,fname
+1110 format(i4,f7.2,f8.2,f7.1,2x,a16)
+ enddo
+
+999 end program wspr5sim
diff --git a/lib/fsk4hf/wspr_fsk8_downsample.f90 b/lib/fsk4hf/wspr_fsk8_downsample.f90
new file mode 100644
index 00000000..9750e0cb
--- /dev/null
+++ b/lib/fsk4hf/wspr_fsk8_downsample.f90
@@ -0,0 +1,27 @@
+subroutine wspr_fsk8_downsample(iwave,c)
+
+! Input: i*2 data in iwave() at sample rate 12000 Hz
+! Output: Complex data in c(), sampled at 12000/24=500 Hz
+
+ include 'wspr_fsk8_params.f90'
+ integer*2 iwave(NMAX)
+ complex c(0:NMAXD-1)
+ complex c1(0:NMAXD-1)
+ complex cx(0:NMAX/2)
+ real x(NMAX)
+ equivalence (x,cx)
+
+ df=12000.0/NMAX
+ x=iwave
+ call four2a(x,NMAX,1,-1,0) !r2c FFT to freq domain
+ i0=nint(1500.0/df)
+ c1(0)=cx(i0)
+ do i=1,NMAXD/2
+ c1(i)=cx(i0+i)
+ c1(NMAXD-i)=cx(i0-i)
+ enddo
+ c=c1/NMAXD
+ call four2a(c,NMAXD,1,1,1) !c2c FFT back to time domain
+
+ return
+end subroutine wspr_fsk8_downsample
diff --git a/lib/fsk4hf/wspr_fsk8_params.f90 b/lib/fsk4hf/wspr_fsk8_params.f90
new file mode 100644
index 00000000..c080f029
--- /dev/null
+++ b/lib/fsk4hf/wspr_fsk8_params.f90
@@ -0,0 +1,14 @@
+! LDPC (300,60) code
+parameter (NDOWN=24) !Downsample factor
+parameter (KK=60) !Information bits (50 + CRC10)
+parameter (ND=100) !Data symbols
+parameter (NS=14) !Sync symbols (2 @ Costas 7x7)
+parameter (NN=NS+ND) !Total symbols (114)
+parameter (NSPS0=24576) !Samples per symbol at 12000 S/s
+parameter (NSPS=NSPS0/NDOWN) !Sam/sym, downsampled (1024)
+parameter (N7=7*NSPS) !Samples in Costas 7x7 array (7168)
+parameter (NZ=NSPS*NN) !Samples in downsampled waveform (116,736)
+parameter (NMAX=240*12000) !Samples in iwave()
+parameter (NMAXD=NMAX/24) !Samples in c(), after downsampling
+parameter (NFFT1=4*NSPS,NH1=NFFT1/2)
+parameter (NH2=NSPS/2)
diff --git a/lib/fsk4hf/wspr_fsk8_sim.f90 b/lib/fsk4hf/wspr_fsk8_sim.f90
new file mode 100644
index 00000000..40d808a0
--- /dev/null
+++ b/lib/fsk4hf/wspr_fsk8_sim.f90
@@ -0,0 +1,107 @@
+program wspr_fsk8_sim
+
+! Generate simulated data for a 4-minute "WSPR-LF" mode using 8-FSK.
+! Output is saved to a *.wav file.
+
+ use wavhdr
+ include 'wspr_fsk8_params.f90' !Set various constants
+ type(hdr) h !Header for .wav file
+ character arg*12,fname*16
+ character msg*22,msgsent*22
+ complex c0(0:NZ-1)
+ complex c(0:NZ-1)
+ real*8 fMHz
+ integer itone(NN)
+ integer*2 iwave(NMAX) !Generated full-length waveform
+
+! Get command-line argument(s)
+ nargs=iargc()
+ if(nargs.ne.8) then
+ print*,'Usage: wspr5sim "message" f0 DT fsp del nwav nfiles snr'
+ print*,'Example: wspr5sim "K1ABC FN42 30" 50 0.0 0.1 1.0 1 10 -33'
+ go to 999
+ endif
+ call getarg(1,msg) !Message to be transmitted
+ call getarg(2,arg)
+ read(arg,*) f0 !Freq relative to WSPR-band center (Hz)
+ call getarg(3,arg)
+ read(arg,*) xdt !Time offset from nominal (s)
+ call getarg(4,arg)
+ read(arg,*) fspread !Watterson frequency spread (Hz)
+ call getarg(5,arg)
+ read(arg,*) delay !Watterson delay (ms)
+ call getarg(6,arg)
+ read(arg,*) nwav !1 for *.wav file, 0 for *.c4 file
+ call getarg(7,arg)
+ read(arg,*) nfiles !Number of files
+ call getarg(8,arg)
+ read(arg,*) snrdb !SNR_2500
+
+ twopi=8.0*atan(1.0)
+ fs=12000.0/NDOWN !Sample rate after downsampling
+ dt=1.0/fs !Sample interval (s)
+ tt=NSPS*dt !Duration of symbols (s)
+ baud=1.0/tt !Keying rate
+ bw=8*baud
+ txt=NZ*dt !Transmission length (s)
+ bandwidth_ratio=2500.0/(fs/2.0)
+ sig=sqrt(bandwidth_ratio) * 10.0**(0.05*snrdb)
+ if(snrdb.gt.90.0) sig=1.0
+ txt=NN*NSPS0/12000.0
+
+ call genwspr_fsk8(msg,msgsent,itone) !Encode the message, get itone
+ write(*,1000) f0,xdt,txt,snrdb,bw,msgsent
+1000 format('f0:',f9.3,' DT:',f6.2,' TxT:',f6.1,' SNR:',f6.1, &
+ ' BW:',f4.1,2x,a22)
+
+
+ phi=0.0
+ c0=0.
+ k=-1 + nint(xdt/dt)
+ do j=1,NN !Generate OQPSK waveform from itone
+ dphi=twopi*(f0+itone(j)*baud)*dt
+ if(k.eq.0) phi=-dphi
+ do i=1,NSPS
+ k=k+1
+ phi=phi+dphi
+ if(phi.gt.twopi) phi=phi-twopi
+ xphi=phi
+ if(k.ge.0 .and. k.lt.NZ) c0(k)=cmplx(cos(xphi),sin(xphi))
+ enddo
+ enddo
+
+ call sgran()
+ do ifile=1,nfiles
+ if(nwav.eq.0) then
+ c=c0
+ if( fspread .ne. 0.0 .or. delay .ne. 0.0 ) then
+ call watterson(c,NZ,fs,delay,fspread)
+ endif
+ c=c*sig
+ if( snrdb.lt.90) then
+ do i=0,NZ-1
+ xnoise=gran()
+ ynoise=gran()
+ c(i)=c(i)+cmplx(xnoise,ynoise)
+ enddo
+ endif
+ write(fname,1100) ifile
+1100 format('000000_',i4.4,'.c4')
+ open(10,file=fname,status='unknown',access='stream')
+ fMHz=1.866d0
+ nmin=4
+ write(10) fname,nmin,fMHz,c
+ else
+ call wspr_fsk8_wav(baud,xdt,f0,itone,snrdb,iwave)
+ h=default_header(12000,NMAX)
+ write(fname,1102) ifile
+1102 format('000000_',i4.4,'.wav')
+ open(10,file=fname,status='unknown',access='stream')
+ write(10) h,iwave !Save to *.wav file
+ close(10)
+ endif
+ write(*,1110) ifile,xdt,f0,snrdb,fname
+1110 format(i4,f7.2,f8.2,f7.1,2x,a16)
+ enddo
+
+999 end program wspr_fsk8_sim
diff --git a/lib/fsk4hf/wspr_fsk8_wav.f90 b/lib/fsk4hf/wspr_fsk8_wav.f90
new file mode 100644
index 00000000..3b0a0ff3
--- /dev/null
+++ b/lib/fsk4hf/wspr_fsk8_wav.f90
@@ -0,0 +1,44 @@
+subroutine wspr_fsk8_wav(baud,xdt,f0,itone,snrdb,iwave)
+
+! Generate iwave() from itone().
+
+ include 'wspr_fsk8_params.f90'
+ integer itone(NN)
+ integer*2 iwave(NMAX)
+ real*8 twopi,dt,dphi,phi
+ real dat(NMAX)
+
+ twopi=8.d0*atan(1.d0)
+ dt=1.d0/12000.d0
+
+ dat=0.
+ if(snrdb.lt.90) then
+ do i=1,NMAX
+ dat(i)=gran() !Generate gaussian noise, rms = 1.0
+ enddo
+ bandwidth_ratio=2500.0/6000.0
+ sig=sqrt(2*bandwidth_ratio)*10.0**(0.05*snrdb)
+ else
+ sig=1.0
+ endif
+
+ phi=0.d0
+ k=nint((xdt+1.0)/dt)
+ do j=1,NN
+ dphi=twopi*(f0+ itone(j)*baud)*dt
+ if(k.eq.0) phi=-dphi
+ do i=1,NSPS0
+ k=k+1
+ phi=phi+dphi
+ if(phi.gt.twopi) phi=phi-twopi
+ xphi=phi
+ if(k.gt.0 .and. k.le.NMAX) dat(k)=dat(k) + sig*sin(xphi)
+ enddo
+ enddo
+ fac=32767.0
+ rms=100.0
+ if(snrdb.ge.90.0) iwave=nint(fac*dat)
+ if(snrdb.lt.90.0) iwave=nint(rms*dat)
+
+ return
+end subroutine wspr_fsk8_wav
diff --git a/lib/fsk4hf/wspr_fsk8d.f90 b/lib/fsk4hf/wspr_fsk8d.f90
new file mode 100644
index 00000000..c107f848
--- /dev/null
+++ b/lib/fsk4hf/wspr_fsk8d.f90
@@ -0,0 +1,196 @@
+program wspr_fsk8d
+
+! WSPR-LF is a potential WSPR-like mode intended for use at LF and MF.
+! This version uses 4-minute T/R sequences, an LDPC (300,60) code,
+! 8-FSK modulation, and noncoherent demodulation. This decoder reads
+! data from *.wav files.
+
+! 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. Find two 7x7 Costas arrays to get xdt and fc2
+! 4. Mix from fc2 to 0, compute aligned symbol spectra
+! 5. Get soft bits from symbol spectra
+
+! Still to do: find and decode more than one signal in the specified passband.
+
+ include 'wspr_fsk8_params.f90'
+ character arg*8,message*22,cbits*50,infile*80,fname*16,datetime*11
+ character*120 data_dir
+ complex csync(0:N7-1) !Sync symbols for Costas 7x7 array
+ complex c1(0:2*N7-1)
+ complex c2(0:2*N7-1)
+ complex c(0:NMAXD-1) !Complex waveform
+ real*8 fMHz
+ real rxdata(3*ND),llr(3*ND) !Soft symbols
+ real a(5) !For twkfreq1
+ real s(0:NH2,NN)
+ real savg(0:NH2)
+ real ss(0:N7)
+ real ss0(0:N7)
+ real ps(0:7)
+ integer ihdr(11)
+ integer*2 iwave(NMAX) !Generated full-length waveform
+ integer*1 idat(7)
+ integer*1 decoded(KK),apmask(3*ND),cw(3*ND)
+ integer icos7(0:6)
+ data icos7/2,5,6,0,4,1,3/ !Costas 7x7 tone pattern
+
+ nargs=iargc()
+ if(nargs.lt.7) then
+ print*,'Usage: wspr_fsk8d -d db -f fMHz -a data_dir file1 [file2 ...]'
+ go to 999
+ endif
+ call getarg(1,arg)
+ if(arg(1:3).ne.'-d ') go to 999
+ call getarg(2,arg)
+ read(arg,*) degrade
+ rxbw=3000.
+
+ call getarg(3,arg)
+ if(arg(1:3).ne.'-f ') go to 999
+ call getarg(4,arg)
+ read(arg,*) fMHz
+
+ call getarg(5,arg)
+ if(arg(1:3).ne.'-a ') go to 999
+ call getarg(6,data_dir)
+
+ open(13,file=trim(data_dir)//'/ALL_WSPR.TXT',status='unknown', &
+ position='append')
+
+ twopi=8.0*atan(1.0)
+ fs=NSPS*12000.0/NSPS0 !Sample rate after downsampling (Hz)
+ dt=1.0/fs !Sample interval (s)
+ ts=NSPS*dt !Symbol duration (s)
+ baud=1.0/ts !Keying rate (Hz)
+ txt=NZ*dt !Transmission length (s)
+
+ phi=0.
+ k=-1
+ do j=0,6
+ dphi=twopi*baud*icos7(j)*dt
+ do i=1,NSPS
+ phi=phi+dphi
+ if(phi.gt.twopi) phi=phi-twopi
+ k=k+1
+ csync(k)=cmplx(cos(phi),sin(phi))
+ enddo
+ enddo
+
+ do ifile=7,nargs
+ call getarg(ifile,infile)
+ open(10,file=infile,status='old',access='stream')
+ j1=index(infile,'.c4')
+ j2=index(infile,'.wav')
+ if(j1.gt.0) then
+ read(10,end=999) fname,ntrmin,fMHz,c(0:NZ-1)
+ read(fname(8:11),*) nutc
+ write(datetime,'(i11)') nutc
+ else if(j2.gt.0) then
+ read(10,end=999) ihdr,iwave
+ read(infile(j2-4:j2-1),*) nutc
+ datetime=infile(j2-11:j2-1)
+ if(degrade.gt.0.0) call degrade_snr(iwave,NMAX,degrade,rxbw)
+ call wspr_fsk8_downsample(iwave,c)
+ else
+ print*,'Wrong file format?'
+ go to 999
+ endif
+ close(10)
+ pmax=0.
+ df1=fs/(2*N7)
+ ia=nint(100.0/df1)
+ ib=nint(150.0/df1)
+ ipk=0
+ jpk=0
+! Get xdt and f0 from the sync arrays
+ do j0=0,1000,50
+ j0b=j0+107*NSPS
+ c1(0:N7-1)=c(j0:j0+N7-1)*conjg(csync)
+ c1(N7:)=0.
+ c2(0:N7-1)=c(j0b:j0b+N7-1)*conjg(csync)
+ c2(N7:)=0.
+ call four2a(c1,2*N7,1,-1,1)
+ call four2a(c2,2*N7,1,-1,1)
+ do i=0,N7
+ p=1.e-9*(real(c1(i))**2 + aimag(c1(i))**2 + &
+ real(c2(i))**2 + aimag(c2(i))**2)
+ ss(i)=p
+ enddo
+ do i=ia,ib
+ p=ss(i)
+ if(p.gt.pmax) then
+ pmax=p
+ ipk=i
+ jpk=j0
+ endif
+ enddo
+ if(jpk.eq.j0) ss0=ss
+ enddo
+ f0=ipk*df1
+ xdt=jpk*dt - 1.0
+
+ sp3n=(ss0(ipk-1)+ss0(ipk)+ss0(ipk+1)) !Sig + 3*noise
+ call pctile(ss0,N7,45,base)
+ psig=sp3n-3*base !Sig only
+ pnoise=(2500.0/df1)*base !Noise in 2500 Hz
+ xsnr=db(psig/pnoise) !SNR from sync tones
+
+ if(jpk.ge.0) c(0:NMAXD-1-jpk)=c(jpk:NMAXD-1)
+
+ a(1)=-f0
+ a(2:5)=0.
+ call twkfreq1(c,NZ,fs,a,c) !Mix from f0 down to 0
+ call spec8(c,s,savg) !Get symbol spectra
+
+ do j=1,ND
+ k=j+7
+ ps=s(0:7,k)
+ ! ps=sqrt(ps) !### ??? ###
+ ps=log(ps)
+ r1=max(ps(1),ps(3),ps(5),ps(7))-max(ps(0),ps(2),ps(4),ps(6))
+ r2=max(ps(2),ps(3),ps(6),ps(7))-max(ps(0),ps(1),ps(4),ps(5))
+ r4=max(ps(4),ps(5),ps(6),ps(7))-max(ps(0),ps(1),ps(2),ps(3))
+ rxdata(3*j-2)=r4
+ rxdata(3*j-1)=r2
+ rxdata(3*j)=r1
+ enddo
+
+ rxav=sum(rxdata)/ND
+ rx2av=sum(rxdata*rxdata)/ND
+ rxsig=sqrt(rx2av-rxav*rxav)
+ rxdata=rxdata/rxsig
+ s0=0.84
+ llr=2.0*rxdata/(s0*s0)
+ apmask=0
+ max_iterations=40
+ ifer=0
+ call bpdecode300(llr,apmask,max_iterations,decoded,niterations,cw)
+ if(niterations.lt.0) call osd300(llr,4,decoded,niterations,cw)
+ nbadcrc=0
+ if(niterations.ge.0) call chkcrc10(decoded,nbadcrc)
+ if(niterations.lt.0 .or. nbadcrc.ne.0) ifer=1
+ nsnr=nint(xsnr)
+! freq=fMHz + 1.d-6*f0
+ freq=1.d-6*(f0+1500)
+ nfdot=0
+ 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)
+ write(*,1112) datetime(8:11),nsnr,xdt,freq,nfdot,message
+1112 format(a4,i4,f5.1,f11.6,i3,2x,a22)
+ endif
+ write(13,1110) datetime,0,nsnr,xdt,freq,message,nfdot
+1110 format(a11,2i4,f6.2,f12.7,2x,a22,i3)
+ enddo ! ifile loop
+ write(*,1120)
+1120 format("<DecodeFinished>")
+
+999 end program wspr_fsk8d
+
diff --git a/lib/fsk4hf/wsprlf.f90 b/lib/fsk4hf/wsprlf.f90
new file mode 100644
index 00000000..efc3e846
--- /dev/null
+++ b/lib/fsk4hf/wsprlf.f90
@@ -0,0 +1,110 @@
+program wsprlf
+
+ parameter (NN=121) !Total symbols
+ parameter (NSPS=28800) !Samples per symbol @ fs=12000 Hz
+ parameter (NZ=NSPS*NN) !Samples in waveform
+
+ character*8 arg
+ complex c(0:NZ-1)
+ real*8 twopi,fs,f0,dt,phi,dphi
+ real x(0:NZ-1)
+ real p(0:NZ/2)
+ real h0(0:NSPS/2) !Pulse shape, rising edge
+ real h1(0:NSPS/2) !Pulse shape, trailing edge
+ real tmp(NN)
+ integer id(NN) !Generated data
+ integer ie(NN) !Differentially encoded data
+ data fs/12000.d0/
+
+ nargs=iargc()
+ if(nargs.ne.3) then
+ print*,'Usage: wsprlf f0 t1 snr'
+ goto 999
+ endif
+ call getarg(1,arg)
+ read(arg,*) f0
+ call getarg(2,arg)
+ read(arg,*) t1
+ call getarg(3,arg)
+ read(arg,*) snrdb
+
+ call random_number(tmp) !Generate random bipolar data
+ id=1
+ where(tmp.lt.0.5) id=-1
+ ie(1)=1
+ do i=2,NN !Differentially encode
+ ie(i)=id(i)*ie(i-1)
+ enddo
+
+ n1=nint(t1*NSPS)
+ twopi=8.d0*atan(1.d0)
+
+ do i=0,2*n1-1 !Define the shape functions
+ if(i.le.n1-1) then
+ h0(i)=0.5*(1.0-cos(0.5*i*twopi/n1))
+ else
+ h1(i-n1)=0.5*(1.0-cos(0.5*i*twopi/n1))
+ endif
+ enddo
+ if(t1.eq.0.0) h0=1
+ if(t1.eq.0.0) h1=1
+
+! Shape the channel pulses
+ x=1.
+ x(0:n1-1)=h0(0:n1-1) !Leading edge of 1st pulse
+ do j=2,NN !Leading edges
+ if(ie(j).ne.ie(j-1)) then
+ ia=(j-1)*NSPS + 1
+ ib=ia+n1-1
+ x(ia:ib)=h0(0:n1-1)
+ endif
+ enddo
+ do j=1,NN-1 !Trailing edges
+ if(ie(j+1).ne.ie(j)) then
+ ib=j*NSPS
+ ia=ib-n1+1
+ x(ia:ib)=h1(0:n1-1)
+ endif
+ enddo
+ ib=NN*NSPS-1
+ ia=ib-n1+1
+ x(ia:ib)=h1(0:n1-1) !Trailing edge of last pulse
+
+ dt=1.d0/fs
+ ts=dt*NSPS
+ baud=fs/NSPS
+ write(*,1000) baud,ts
+1000 format('Baud:',f6.3,' Tsym:',f6.3)
+
+ dphi=twopi*f0*dt
+ phi=0.d0
+ i=-1
+ do j=1,NN !Generate the baseband waveform
+ a=ie(j)
+ do k=1,NSPS
+ i=i+1
+ x(i)=a*x(i)
+ phi=phi+dphi
+ if(phi.gt.twopi) phi=phi-twopi
+ xphi=phi
+ c(i)=x(i)*cmplx(cos(xphi),sin(xphi))
+ sym=i*dt/ts
+ if(j.le.20) write(13,1010) sym,x(i),c(i)
+1010 format(4f12.6)
+ enddo
+ enddo
+
+ call four2a(c,NZ,1,-1,1) !To freq domain
+ df=fs/NZ
+ nh=NZ/2
+ do i=0,nh
+ f=i*df
+ p(i)=real(c(i))**2 + aimag(c(i))**2
+ enddo
+ p=p/maxval(p)
+ do i=0,nh !Save spectrum for plotting
+ write(14,1020) i*df,p(i),10.0*log10(p(i)+1.e-8)
+1020 format(f10.3,2e12.3)
+ enddo
+
+999 end program wsprlf
diff --git a/lib/fsk4hf/wsprlf_params.f90 b/lib/fsk4hf/wsprlf_params.f90
new file mode 100644
index 00000000..d0c3f5c7
--- /dev/null
+++ b/lib/fsk4hf/wsprlf_params.f90
@@ -0,0 +1,14 @@
+!parameter (NDOWN=540) !Downsample factor (default 540)
+parameter (NDOWN=30) !Downsample factor (default 540)
+parameter (KK=60) !Information bits (50 + CRC10)
+parameter (ND=300) !Data symbols: LDPC (300,60), r=1/5
+parameter (NS=109) !Sync symbols (2 x 48 + Barker 13)
+parameter (NR=3) !Ramp up/down
+parameter (NN=NR+NS+ND) !Total symbols (412)
+parameter (NSPS0=8640) !Samples per symbol at 12000 S/s
+parameter (NSPS=NSPS0/NDOWN) !Samples per MSK symbol (16)
+parameter (N2=2*NSPS) !Samples per OQPSK symbol (32)
+parameter (N13=13*N2) !Samples in central sync vector (416)
+parameter (NZ=NSPS*NN) !Samples in baseband waveform (6592)
+parameter (NZMAX=NSPS0*NN)
+parameter (NFFT1=4*NSPS,NH1=NFFT1/2)
diff --git a/lib/fsk4hf/wsprlfsim.f90 b/lib/fsk4hf/wsprlfsim.f90
new file mode 100644
index 00000000..542d4816
--- /dev/null
+++ b/lib/fsk4hf/wsprlfsim.f90
@@ -0,0 +1,285 @@
+program wsprlfsim
+
+! Simulate characteristics of a potential "WSPR-LF" mode using LDPC (300,60)
+! code, OQPSK modulation, and 5 minute T/R sequences.
+
+! 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
+
+ include 'wsprlf_params.f90'
+
+! Q: Would it be better for central Sync array to use both I and Q channels?
+
+ character*8 arg
+ complex cbb(0:NZ-1) !Complex baseband waveform
+ complex csync(0:NZ-1) !Sync symbols only, from cbb
+ complex c(0:NZ-1) !Complex waveform
+ complex c0(0:NZ-1) !Complex waveform
+ complex c1(0:NZ-1) !Complex waveform
+ complex zz(NS+ND) !Complex symbol values (intermediate)
+ complex z
+ real xnoise(0:NZ-1) !Generated random noise
+ real ynoise(0:NZ-1) !Generated random noise
+ 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
+ real t(11)
+ character*12 label(11)
+ integer*8 count0,count1,count2,count3,clkfreq
+ integer nc(11)
+ integer id(NS+ND) !NRZ values (+/-1) for Sync and Data
+ integer ierror(NS+ND)
+ integer icw(NN)
+ integer itone(NN)
+ integer*1 msgbits(KK),decoded(KK),apmask(ND),cw(ND)
+! integer*1 codeword(ND)
+ data msgbits/0,0,1,0,0,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1, &
+ 1,1,1,0,1,1,1,1,1,1,1,0,0,1,0,0,1,1,0,1,1,0,1,0,1,1,0,0,1,1/
+ data label/'genwsprlf','twkfreq1 a','watterson','noise gen','getfc1w', &
+ 'getfc2w','twkfreq1 b','xdt loop','cpolyfitw','msksoftsym', &
+ 'bpdecode300'/
+
+ nargs=iargc()
+ if(nargs.ne.6) then
+ print*,'Usage: wsprlfsim f0(Hz) delay(ms) fspread(Hz) maxn iters snr(dB)'
+ print*,'Example: wsprlfsim 0 0 0 5 10 -20'
+ print*,'Set snr=0 to cycle through a range'
+ go to 999
+ endif
+ call getarg(1,arg)
+ read(arg,*) f0 !Generated carrier frequency
+ call getarg(2,arg)
+ read(arg,*) delay !Delta_t (ms) for Watterson model
+ call getarg(3,arg)
+ read(arg,*) fspread !Fspread (Hz) for Watterson model
+ call getarg(4,arg)
+ read(arg,*) maxn !Max nterms for polyfit
+ call getarg(5,arg)
+ read(arg,*) iters !Iterations at each SNR
+ call getarg(6,arg)
+ read(arg,*) snrdb !Specified SNR_2500
+
+ nc=0
+ twopi=8.0*atan(1.0)
+ fs=NSPS*12000.0/NSPS0 !Sample rate = 22.2222... Hz
+ 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)
+ bandwidth_ratio=2500.0/(fs/2.0)
+ write(*,1000) fs,f0,delay,fspread,maxn,baud,3*baud,txt,iters
+1000 format('fs:',f10.3,' f0:',f5.1,' Delay:',f4.1,' fSpread:',f5.2, &
+ ' maxn:',i3,/'Baud:',f8.3,' BW:',f5.1,' TxT:',f6.1,' iters:',i4/)
+ write(*,1004)
+1004 format(/' SNR sync data ser ber fer fsigma tsigma', &
+ ' tsec'/68('-'))
+
+ do i=1,N2 !Half-sine pulse shape
+ pp(i)=sin(0.5*(i-1)*twopi/(2*NSPS))
+ enddo
+
+ t=0.
+ call system_clock(count0,clkfreq)
+ call genwsprlf(msgbits,id,icw,cbb,csync,itone)!Generate baseband waveform
+ call system_clock(count1,clkfreq)
+ t(1)=float(count1-count0)/float(clkfreq)
+ nc(1)=nc(1)+1
+ do i=0,NZ-1
+ write(40,4001) i,cbb(i),csync(i)
+4001 format(i8,4f12.6)
+ enddo
+
+ call system_clock(count0,clkfreq)
+ a=0.
+ a(1)=f0
+ call twkfreq1(cbb,NZ,fs,a,c0) !Mix baseband to specified frequency
+ call system_clock(count1,clkfreq)
+ t(2)=float(count1-count0)/float(clkfreq)
+ nc(2)=nc(2)+1
+
+ isna=-20
+ isnb=-40
+ if(snrdb.ne.0.0) then
+ isna=nint(snrdb)
+ isnb=isna
+ endif
+ do isnr=isna,isnb,-1 !Loop over SNR range
+ if(isna.ne.isnb) snrdb=isnr
+ sig=sqrt(bandwidth_ratio) * 10.0**(0.05*snrdb)
+ if(snrdb.gt.90.0) sig=1.0
+ nhard=0
+ nhardsync=0
+ nfe=0
+ sqf=0.
+ sqt=0.
+
+ call system_clock(count2,clkfreq)
+ do iter=1,iters !Loop over requested iterations
+ c=c0
+
+ call system_clock(count0,clkfreq)
+ if(delay.ne.0.0 .or. fspread.ne.0.0) then
+ call watterson(c,NZ,fs,delay,fspread)
+ endif
+ call system_clock(count1,clkfreq)
+ t(3)=t(3)+float(count1-count0)/float(clkfreq)
+ nc(3)=nc(3)+1
+
+ call system_clock(count0,clkfreq)
+ c=sig*c !Scale to requested SNR
+ if(snrdb.lt.90) then
+ do i=0,NZ-1 !Generate gaussian noise
+ xnoise(i)=gran()
+ ynoise(i)=gran()
+ enddo
+ c=c + cmplx(xnoise,ynoise) !Add AWGN noise
+ endif
+ call system_clock(count1,clkfreq)
+ t(4)=t(4)+float(count1-count0)/float(clkfreq)
+ nc(4)=nc(4)+1
+
+ call system_clock(count0,clkfreq)
+ call getfc1w(c,fs,fc1) !First approx for freq
+ call system_clock(count1,clkfreq)
+ t(5)=t(5)+float(count1-count0)/float(clkfreq)
+ nc(5)=nc(5)+1
+
+ call system_clock(count0,clkfreq)
+ call getfc2w(c,csync,fs,fc1,fc2,fc3) !Refined freq
+ call system_clock(count1,clkfreq)
+ t(6)=t(6)+float(count1-count0)/float(clkfreq)
+ nc(6)=nc(6)+1
+ sqf=sqf + (fc1+fc2-f0)**2
+
+ call system_clock(count0,clkfreq)
+!NB: Measured performance is about equally good using fc2 or fc3 here:
+ a(1)=-(fc1+fc2)
+ a(2:5)=0.
+ call twkfreq1(c,NZ,fs,a,c) !Mix c down by fc1+fc2
+ call system_clock(count1,clkfreq)
+ t(7)=t(7)+float(count1-count0)/float(clkfreq)
+ nc(7)=nc(7)+1
+
+! The following may not be necessary?
+! z=sum(c(3088:3503)*cb13)/208.0 !Get phase from Barker 13 vector
+! z0=z/abs(z)
+! c=c*conjg(z0)
+
+ call system_clock(count0,clkfreq)
+!---------------------------------------------------------------- DT
+! Not presently used:
+ amax=0.
+ jpk=0
+ iaa=0
+ ibb=NZ-1
+ do j=-20*NSPS,20*NSPS,NSPS/8
+ 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
+ sqt=sqt + xdt**2
+ call system_clock(count1,clkfreq)
+ t(8)=t(8)+float(count1-count0)/float(clkfreq)
+ nc(8)=nc(8)+1
+
+!-----------------------------------------------------------------
+
+ nterms=maxn
+ c1=c
+ do itry=1,20
+ idf=itry/2
+ if(mod(itry,2).eq.0) idf=-idf
+ nhard0=0
+ nhardsync0=0
+ ifer=1
+ a(1)=idf*0.00085
+ a(2:5)=0.
+ call system_clock(count0,clkfreq)
+ call twkfreq1(c1,NZ,fs,a,c) !Mix c1 into c
+ call cpolyfitw(c,pp,id,maxn,aa,bb,zz,nhs)
+ call system_clock(count1,clkfreq)
+ t(9)=t(9)+float(count1-count0)/float(clkfreq)
+ nc(9)=nc(9)+1
+
+ call system_clock(count0,clkfreq)
+ call msksoftsymw(zz,aa,bb,id,nterms,ierror,rxdata,nhard0,nhardsync0)
+ call system_clock(count1,clkfreq)
+ t(10)=t(10)+float(count1-count0)/float(clkfreq)
+ nc(10)=nc(10)+1
+
+ 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 system_clock(count0,clkfreq)
+ call bpdecode300(llr,apmask,max_iterations,decoded,niterations,cw)
+ call system_clock(count1,clkfreq)
+ t(11)=t(11)+float(count1-count0)/float(clkfreq)
+ nc(11)=nc(11)+1
+ nbadcrc=0
+ if(niterations.ge.0) call chkcrc10(decoded,nbadcrc)
+ if(niterations.lt.0 .or. count(msgbits.ne.decoded).gt.0 .or. &
+ nbadcrc.ne.0) ifer=1
+ if(ifer.eq.0) exit
+ enddo !Freq dither loop
+ nhard=nhard+nhard0
+ nhardsync=nhardsync+nhardsync0
+ nfe=nfe+ifer
+ if(nhardsync0+nhard0+niterations+ifer.gt.0) write(42,1045) snrdb, &
+ nhardsync0,nhard0,niterations,ifer,xdt
+1045 format(f6.1,4i6,f8.2)
+ enddo
+ call system_clock(count3,clkfreq)
+ tsec=float(count3-count2)/float(clkfreq)
+
+ fsigma=sqrt(sqf/iters)
+ tsigma=sqrt(sqt/iters)
+ ser=float(nhardsync)/(NS*iters)
+ ber=float(nhard)/(ND*iters)
+ fer=float(nfe)/iters
+ write(*,1050) snrdb,nhardsync,nhard,ser,ber,fer,fsigma,tsigma,tsec
+1050 format(f6.1,2i7,2f8.4,f7.3,2f8.2f8.3)
+ enddo
+
+ write(*,1060) NS*iters,ND*iters
+1060 format(68('-')/6x,2i7)
+
+ write(*,1065)
+1065 format(/'Timing sec frac calls'/39('-'))
+ do i=1,11
+ write(*,1070) label(i),t(i),t(i)/sum(t),nc(i)
+1070 format(a12,2f9.3,i8)
+ enddo
+ write(*,1072) sum(t),1.0
+1072 format(39('-')/12x,2f10.3)
+
+999 end program wsprlfsim