diff options
| author | Christoph Berg <myon@debian.org> | 2018-12-19 23:01:50 +0100 |
|---|---|---|
| committer | Christoph Berg <myon@debian.org> | 2018-12-19 23:01:50 +0100 |
| commit | 5c60c77adff9a54fd5f94f9915b22ccd8c4c5a3b (patch) | |
| tree | 410fa358a5f73260a579b4d669730f319a4d2609 /lib/wsprd | |
| parent | 6c82050de806dcdafaed0077400a6c3396a4a159 (diff) | |
| parent | e0737c56c1e384e05064a0c6e1ac46979371041b (diff) | |
Update upstream source from tag 'upstream/2.0.0+repack'
Update to upstream version '2.0.0+repack'
with Debian dir 55dbb3a6a7eb189e187dd79f732623ca0b56116d
Diffstat (limited to 'lib/wsprd')
| -rwxr-xr-x | lib/wsprd/README | 20 | ||||
| -rw-r--r-- | lib/wsprd/osdwspr.f90 | 359 | ||||
| -rw-r--r-- | lib/wsprd/wsprd.c | 444 | ||||
| -rw-r--r-- | lib/wsprd/wsprsim.c | 12 | ||||
| -rw-r--r-- | lib/wsprd/wsprsim_utils.c | 75 | ||||
| -rw-r--r-- | lib/wsprd/wsprsim_utils.h | 12 |
6 files changed, 746 insertions, 176 deletions
diff --git a/lib/wsprd/README b/lib/wsprd/README index 4579d2d3..141c2fb2 100755 --- a/lib/wsprd/README +++ b/lib/wsprd/README @@ -9,16 +9,21 @@ USAGE: OPTIONS: -a <path> path to writeable data files, default="." + -B disable block demodulation - use single-symbol noncoherent demod -c write .c2 file at the end of the first pass + -C maximum number of decoder cycles per bit, default 10000 + -d deeper search. Slower, a few more decodes -e x (x is transceiver dial frequency error in Hz) -f x (x is transceiver dial frequency in MHz) -H do not use (or update) the hash table + -J use the stack decoder instead of Fano decoder -m decode wspr-15 .wav file + -o n (0<=n<=5), decoding depth for OSD, default is disabled -q quick mode - doesn't dig deep for weak signals -s single pass mode, no subtraction (same as original wsprd) -v verbose mode (shows dupes) -w wideband mode - decode signals within +/- 150 Hz of center - -z x (x is fano metric table bias, default is 0.42) + -z x (x is fano metric table bias, default is 0.45) infile can be either .wav or .c2 @@ -35,8 +40,19 @@ subband center frequency. The wideband option (-w) extends this to +/- 150 Hz. wsprd maintains a hashtable and will decode all three types of wspr messages. An option (-H) is available to turn off use of the hashtable. +Decoding is attempted using soft symbols derived using single-symbol +noncoherent detection. If decoding fails, then soft symbols derived using +block demodulation over 2- and 3-symbol blocks are used in subsequent +decoding attempts. For slower computers, block demodulation can be +disabled with the -B command line option. + The symbols are decoded using Phil Karn's sequential decoder routine, -fano.c. +fano.c. The -J option replaces the Fano algorithm with the Jelinek +"stack-bucket" algorithm. If the -o command line option is enabled, then +an ordered-statistics decoder (OSD) is invoked if the Fano (or Jelinek) algorithm +fails. The OSD is a complete decoder, meaning that it always returns a codeword. +A returned codeword is considered valid only if the unpacked decode contains +a callsign that is already in the hashtable. NOTES: This program attempts to maximize the number of successful decodes per transmit diff --git a/lib/wsprd/osdwspr.f90 b/lib/wsprd/osdwspr.f90 new file mode 100644 index 00000000..42b8e949 --- /dev/null +++ b/lib/wsprd/osdwspr.f90 @@ -0,0 +1,359 @@ +subroutine osdwspr(ss,apmask,ndeep,cw,nhardmin,dmin) +! +use iso_c_binding +parameter (N=162, K=50, L=32) + +!integer*1 p1(L),p2(L),p3(L),p4(L) +integer*1 gg(64) + +real ss(N) +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 indx(N),ndeep,nhardmin +real rx(N),absrx(N),dmin +logical first,reset +data first/.true./ +data gg/1,1,0,1,0,1,0,0,1,0,0,0,1,1,0,0,1,0,1,0,0,1,0,1,1,1,0,1,1,0,0,0, & + 0,1,0,0,0,0,0,0,1,0,0,1,1,1,1,0,0,0,1,0,0,1,0,0,1,0,1,1,1,1,1,1/ + +save first,gen + +if( first ) then ! fill the generator matrix + gen=0 + gen(1,1:2*L)=gg(1:2*L) + do i=2,K + gen(i,:)=cshift(gen(i-1,:),-2) + enddo + first=.false. +endif + +rx=ss/127.0 +apmaskr=apmask + +! 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.le.0) goto 998 ! norder=0 +if(ndeep.gt.5) ndeep=5 +if( ndeep.eq. 1) then + nord=1 + npre1=0 + npre2=0 + nt=66 + ntheta=16 +elseif(ndeep.eq.2) then + nord=1 + npre1=1 + npre2=0 + nt=66 + ntheta=16 +elseif(ndeep.eq.3) then + nord=2 + npre1=1 + npre2=0 + nt=66 + ntheta=22 + ntau=16 +elseif(ndeep.eq.4) then + nord=2 + npre1=1 + npre2=1 + nt=66 + ntheta=22 + ntau=16 +elseif(ndeep.eq.5) then + nord=3 + npre1=1 + npre2=0 + nt=66 + ntheta=22 + ntau=20 +endif + +do iorder=1,nord + misub(1:K-iorder)=0 + misub(K-iorder+1:K)=1 + iflag=K-iorder+1 + 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 + misub(1:K-nord)=0 + misub(K-nord+1:K)=1 + iflag=K-nord+1 + 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 as-received order. +cw(indices)=cw +hdec(indices)=hdec +return +end subroutine osdwspr + +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/wsprd/wsprd.c b/lib/wsprd/wsprd.c index 09cd599b..2e4c3c23 100644 --- a/lib/wsprd/wsprd.c +++ b/lib/wsprd/wsprd.c @@ -4,12 +4,12 @@ File name: wsprd.c - Copyright 2001-2015, Joe Taylor, K1JT + Copyright 2001-2018, Joe Taylor, K1JT Much of the present code is based on work by Steven Franke, K9AN, which in turn was based on earlier work by K1JT. - Copyright 2014-2015, Steven Franke, K9AN + Copyright 2014-2018, Steven Franke, K9AN License: GNU GPL v3 @@ -43,6 +43,9 @@ #include "wsprsim_utils.h" #define max(x,y) ((x) > (y) ? (x) : (y)) + +extern void osdwspr_ (float [], unsigned char [], int *, unsigned char [], int *, float *); + // Possible PATIENCE options: FFTW_ESTIMATE, FFTW_ESTIMATE_PATIENT, // FFTW_MEASURE, FFTW_PATIENT, FFTW_EXHAUSTIVE #define PATIENCE FFTW_ESTIMATE @@ -59,8 +62,6 @@ unsigned char pr3[162]= 0,0,0,0,0,0,0,1,1,0,1,0,1,1,0,0,0,1,1,0, 0,0}; -unsigned long nr; - int printdata=0; //*************************************************************************** @@ -73,19 +74,18 @@ unsigned long readc2file(char *ptr_to_infile, float *idat, float *qdat, char *c2file[15]; FILE* fp; - buffer=malloc(sizeof(float)*2*65536); - memset(buffer,0,sizeof(float)*2*65536); - fp = fopen(ptr_to_infile,"rb"); if (fp == NULL) { fprintf(stderr, "Cannot open data file '%s'\n", ptr_to_infile); return 1; } - unsigned long nread=fread(c2file,sizeof(char),14,fp); - nread=fread(&ntrmin,sizeof(int),1,fp); - nread=fread(&dfreq,sizeof(double),1,fp); + fread(c2file,sizeof(char),14,fp); + fread(&ntrmin,sizeof(int),1,fp); + fread(&dfreq,sizeof(double),1,fp); *freq=dfreq; - nread=fread(buffer,sizeof(float),2*45000,fp); + + buffer=calloc(2*65536,sizeof(float)); + unsigned long nread=fread(buffer,sizeof(float),2*45000,fp); fclose(fp); *wspr_type=ntrmin; @@ -94,13 +94,13 @@ unsigned long readc2file(char *ptr_to_infile, float *idat, float *qdat, idat[i]=buffer[2*i]; qdat[i]=-buffer[2*i+1]; } + free(buffer); if( nread == 2*45000 ) { return nread/2; } else { return 1; } - free(buffer); } //*************************************************************************** @@ -133,19 +133,20 @@ unsigned long readwavfile(char *ptr_to_infile, int ntrmin, float *idat, float *q FILE *fp; short int *buf2; - buf2 = malloc(npoints*sizeof(short int)); fp = fopen(ptr_to_infile,"rb"); if (fp == NULL) { fprintf(stderr, "Cannot open data file '%s'\n", ptr_to_infile); return 1; } - nr=fread(buf2,2,22,fp); //Read and ignore header - nr=fread(buf2,2,npoints,fp); //Read raw data + + buf2 = calloc(npoints,sizeof(short int)); + fread(buf2,2,22,fp); //Read and ignore header + fread(buf2,2,npoints,fp); //Read raw data fclose(fp); realin=(float*) fftwf_malloc(sizeof(float)*nfft1); - fftout=(fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex)*nfft1); + fftout=(fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex)*(nfft1/2+1)); PLAN1 = fftwf_plan_dft_r2c_1d(nfft1, realin, fftout, PATIENCE); for (i=0; i<npoints; i++) { @@ -155,8 +156,8 @@ unsigned long readwavfile(char *ptr_to_infile, int ntrmin, float *idat, float *q for (i=npoints; i<(size_t)nfft1; i++) { realin[i]=0.0; } - free(buf2); + fftwf_execute(PLAN1); fftwf_free(realin); @@ -331,6 +332,146 @@ void sync_and_demodulate(float *id, float *qd, long np, } return; } + +void noncoherent_sequence_detection(float *id, float *qd, long np, + unsigned char *symbols, float *f1, int *shift1, + float *drift1, int symfac, int *nblocksize) +{ + /************************************************************************ + * Noncoherent sequence detection for wspr. * + * Allowed block lengths are nblock=1,2,3,6, or 9 symbols. * + * Longer block lengths require longer channel coherence time. * + * The whole block is estimated at once. * + * nblock=1 corresponds to noncoherent detection of individual symbols * + * like the original wsprd symbol demodulator. * + ************************************************************************/ + static float fplast=-10000.0; + static float dt=1.0/375.0, df=375.0/256.0; + static float pi=3.14159265358979323846; + float twopidt, df15=df*1.5, df05=df*0.5; + + int i, j, k, lag, itone, ib, b, nblock, nseq, imask; + float xi[512],xq[512]; + float is[4][162],qs[4][162],cf[4][162],sf[4][162],cm,sm,cmp,smp; + float p[512],fac,xm1,xm0; + float c0[257],s0[257],c1[257],s1[257],c2[257],s2[257],c3[257],s3[257]; + float dphi0, cdphi0, sdphi0, dphi1, cdphi1, sdphi1, dphi2, cdphi2, sdphi2, + dphi3, cdphi3, sdphi3; + float f0, fp, fsum=0.0, f2sum=0.0, fsymb[162]; + + twopidt=2*pi*dt; + f0=*f1; + lag=*shift1; + nblock=*nblocksize; + nseq=1<<nblock; + + for (i=0; i<162; i++) { + fp = f0 + (*drift1/2.0)*((float)i-81.0)/81.0; + if( i==0 || (fp != fplast) ) { // only calculate sin/cos if necessary + dphi0=twopidt*(fp-df15); + cdphi0=cos(dphi0); + sdphi0=sin(dphi0); + + dphi1=twopidt*(fp-df05); + cdphi1=cos(dphi1); + sdphi1=sin(dphi1); + + dphi2=twopidt*(fp+df05); + cdphi2=cos(dphi2); + sdphi2=sin(dphi2); + + dphi3=twopidt*(fp+df15); + cdphi3=cos(dphi3); + sdphi3=sin(dphi3); + + c0[0]=1; s0[0]=0; + c1[0]=1; s1[0]=0; + c2[0]=1; s2[0]=0; + c3[0]=1; s3[0]=0; + + for (j=1; j<257; j++) { + c0[j]=c0[j-1]*cdphi0 - s0[j-1]*sdphi0; + s0[j]=c0[j-1]*sdphi0 + s0[j-1]*cdphi0; + c1[j]=c1[j-1]*cdphi1 - s1[j-1]*sdphi1; + s1[j]=c1[j-1]*sdphi1 + s1[j-1]*cdphi1; + c2[j]=c2[j-1]*cdphi2 - s2[j-1]*sdphi2; + s2[j]=c2[j-1]*sdphi2 + s2[j-1]*cdphi2; + c3[j]=c3[j-1]*cdphi3 - s3[j-1]*sdphi3; + s3[j]=c3[j-1]*sdphi3 + s3[j-1]*cdphi3; + } + + fplast = fp; + } + + cf[0][i]=c0[256]; sf[0][i]=s0[256]; + cf[1][i]=c1[256]; sf[1][i]=s1[256]; + cf[2][i]=c2[256]; sf[2][i]=s2[256]; + cf[3][i]=c3[256]; sf[3][i]=s3[256]; + + is[0][i]=0.0; qs[0][i]=0.0; + is[1][i]=0.0; qs[1][i]=0.0; + is[2][i]=0.0; qs[2][i]=0.0; + is[3][i]=0.0; qs[3][i]=0.0; + + for (j=0; j<256; j++) { + k=lag+i*256+j; + if( (k>0) && (k<np) ) { + is[0][i]=is[0][i] + id[k]*c0[j] + qd[k]*s0[j]; + qs[0][i]=qs[0][i] - id[k]*s0[j] + qd[k]*c0[j]; + is[1][i]=is[1][i] + id[k]*c1[j] + qd[k]*s1[j]; + qs[1][i]=qs[1][i] - id[k]*s1[j] + qd[k]*c1[j]; + is[2][i]=is[2][i] + id[k]*c2[j] + qd[k]*s2[j]; + qs[2][i]=qs[2][i] - id[k]*s2[j] + qd[k]*c2[j]; + is[3][i]=is[3][i] + id[k]*c3[j] + qd[k]*s3[j]; + qs[3][i]=qs[3][i] - id[k]*s3[j] + qd[k]*c3[j]; + } + } + } + + for (i=0; i<162; i=i+nblock) { + for (j=0;j<nseq;j++) { + xi[j]=0.0; xq[j]=0.0; + cm=1; sm=0; + for (ib=0; ib<nblock; ib++) { + b=(j&(1<<(nblock-1-ib)))>>(nblock-1-ib); + itone=pr3[i+ib]+2*b; + xi[j]=xi[j]+is[itone][i+ib]*cm + qs[itone][i+ib]*sm; + xq[j]=xq[j]+qs[itone][i+ib]*cm - is[itone][i+ib]*sm; + cmp=cf[itone][i+ib]*cm - sf[itone][i+ib]*sm; + smp=sf[itone][i+ib]*cm + cf[itone][i+ib]*sm; + cm=cmp; sm=smp; + } + p[j]=xi[j]*xi[j]+xq[j]*xq[j]; + p[j]=sqrt(p[j]); + } + for (ib=0; ib<nblock; ib++) { + imask=1<<(nblock-1-ib); + xm1=0.0; xm0=0.0; + for (j=0; j<nseq; j++) { + if((j & imask)!=0) { + if(p[j] > xm1) xm1=p[j]; + } + if((j & imask)==0) { + if(p[j]>xm0) xm0=p[j]; + } + } + fsymb[i+ib]=xm1-xm0; + } + } + for (i=0; i<162; i++) { //Normalize the soft symbols + fsum=fsum+fsymb[i]/162.0; + f2sum=f2sum+fsymb[i]*fsymb[i]/162.0; + } + fac=sqrt(f2sum-fsum*fsum); + for (i=0; i<162; i++) { + fsymb[i]=symfac*fsymb[i]/fac; + if( fsymb[i] > 127) fsymb[i]=127.0; + if( fsymb[i] < -128 ) fsymb[i]=-128.0; + symbols[i]=fsymb[i] + 128; + } + return; +} + /*************************************************************************** symbol-by-symbol signal subtraction ****************************************************************************/ @@ -401,20 +542,13 @@ void subtract_signal2(float *id, float *qd, long np, float *refi, *refq, *ci, *cq, *cfi, *cfq; - refi=malloc(sizeof(float)*nc2); - refq=malloc(sizeof(float)*nc2); - ci=malloc(sizeof(float)*nc2); - cq=malloc(sizeof(float)*nc2); - cfi=malloc(sizeof(float)*nc2); - cfq=malloc(sizeof(float)*nc2); - - memset(refi,0,sizeof(float)*nc2); - memset(refq,0,sizeof(float)*nc2); - memset(ci,0,sizeof(float)*nc2); - memset(cq,0,sizeof(float)*nc2); - memset(cfi,0,sizeof(float)*nc2); - memset(cfq,0,sizeof(float)*nc2); - + refi=calloc(nc2,sizeof(float)); + refq=calloc(nc2,sizeof(float)); + ci=calloc(nc2,sizeof(float)); + cq=calloc(nc2,sizeof(float)); + cfi=calloc(nc2,sizeof(float)); + cfq=calloc(nc2,sizeof(float)); + twopidt=2.0*pi*dt; /****************************************************************************** @@ -460,7 +594,7 @@ void subtract_signal2(float *id, float *qd, long np, //lowpass filter and remove startup transient float w[nfilt], norm=0, partialsum[nfilt]; - memset(partialsum,0,sizeof(float)*nfilt); + for (i=0; i<nfilt; i++) partialsum[i]=0.0; for (i=0; i<nfilt; i++) { w[i]=sin(pi*(float)i/(float)(nfilt-1)); norm=norm+w[i]; @@ -516,31 +650,27 @@ unsigned long writec2file(char *c2filename, int trmin, double freq { int i; float *buffer; - buffer=malloc(sizeof(float)*2*45000); - memset(buffer,0,sizeof(float)*2*45000); - FILE *fp; fp = fopen(c2filename,"wb"); if( fp == NULL ) { fprintf(stderr, "Could not open c2 file '%s'\n", c2filename); - free(buffer); return 0; } unsigned long nwrite = fwrite(c2filename,sizeof(char),14,fp); nwrite = fwrite(&trmin, sizeof(int), 1, fp); nwrite = fwrite(&freq, sizeof(double), 1, fp); + buffer=calloc(2*45000,sizeof(float)); for(i=0; i<45000; i++) { buffer[2*i]=idat[i]; buffer[2*i+1]=-qdat[i]; } - nwrite = fwrite(buffer, sizeof(float), 2*45000, fp); + free(buffer); if( nwrite == 2*45000 ) { return nwrite; } else { - free(buffer); return 0; } } @@ -553,6 +683,7 @@ void usage(void) printf("\n"); printf("Options:\n"); printf(" -a <path> path to writeable data files, default=\".\"\n"); + printf(" -B disable block demodulation - use single-symbol noncoherent demod\n"); printf(" -c write .c2 file at the end of the first pass\n"); printf(" -C maximum number of decoder cycles per bit, default 10000\n"); printf(" -d deeper search. Slower, a few more decodes\n"); @@ -561,6 +692,7 @@ void usage(void) printf(" -H do not use (or update) the hash table\n"); printf(" -J use the stack decoder instead of Fano decoder\n"); printf(" -m decode wspr-15 .wav file\n"); + printf(" -o n (0<=n<=5), decoding depth for OSD, default is disabled\n"); printf(" -q quick mode - doesn't dig deep for weak signals\n"); printf(" -s single pass mode, no subtraction (same as original wsprd)\n"); printf(" -v verbose mode (shows dupes)\n"); @@ -571,12 +703,12 @@ void usage(void) //*************************************************************************** int main(int argc, char *argv[]) { - char cr[] = "(C) 2016, Steven Franke - K9AN"; + char cr[] = "(C) 2018, Steven Franke - K9AN"; (void)cr; extern char *optarg; extern int optind; int i,j,k; - unsigned char *symbols, *decdata, *channel_symbols; + unsigned char *symbols, *decdata, *channel_symbols, *apmask, *cw; signed char message[]={-9,13,-35,123,57,-39,64,0,0,0,0}; char *callsign, *call_loc_pow; char *ptr_to_infile,*ptr_to_infile_suffix; @@ -585,19 +717,22 @@ int main(int argc, char *argv[]) char timer_fname[200],hash_fname[200]; char uttime[5],date[7]; int c,delta,maxpts=65536,verbose=0,quickmode=0,more_candidates=0, stackdecoder=0; - int writenoise=0,usehashtable=1,wspr_type=2, ipass; - int writec2=0, npasses=2, subtraction=1; + int writenoise=0,usehashtable=1,wspr_type=2, ipass, nblocksize; + int nhardmin,ihash; + int writec2=0,maxdrift; int shift1, lagmin, lagmax, lagstep, ifmin, ifmax, worth_a_try, not_decoded; unsigned int nbits=81, stacksize=200000; unsigned int npoints, metric, cycles, maxnp; float df=375.0/256.0/2; float freq0[200],snr0[200],drift0[200],sync0[200]; + float fsymbs[162]; int shift0[200]; float dt=1.0/375.0, dt_print; double dialfreq_cmdline=0.0, dialfreq, freq_print; double dialfreq_error=0.0; float fmin=-110, fmax=110; float f1, fstep, sync1, drift1; + float dmin; float psavg[512]; float *idat, *qdat; clock_t t0,t00; @@ -606,22 +741,23 @@ int main(int argc, char *argv[]) struct result { char date[7]; char time[5]; float sync; float snr; float dt; double freq; char message[23]; float drift; - unsigned int cycles; int jitter; }; + unsigned int cycles; int jitter; int blocksize; unsigned int metric; + unsigned char osd_decode; }; struct result decodes[50]; char *hashtab; - hashtab=malloc(sizeof(char)*32768*13); - memset(hashtab,0,sizeof(char)*32768*13); + hashtab=calloc(32768*13,sizeof(char)); int nh; - symbols=malloc(sizeof(char)*nbits*2); - decdata=malloc(sizeof(char)*11); - channel_symbols=malloc(sizeof(char)*nbits*2); - - callsign=malloc(sizeof(char)*13); - call_loc_pow=malloc(sizeof(char)*23); + symbols=calloc(nbits*2,sizeof(unsigned char)); + apmask=calloc(162,sizeof(unsigned char)); + cw=calloc(162,sizeof(unsigned char)); + decdata=calloc(11,sizeof(unsigned char)); + channel_symbols=calloc(nbits*2,sizeof(unsigned char)); + callsign=calloc(13,sizeof(char)); + call_loc_pow=calloc(23,sizeof(char)); float allfreqs[100]; char allcalls[100][13]; - memset(allfreqs,0,sizeof(float)*100); + for (i=0; i<100; i++) allfreqs[i]=0.0; memset(allcalls,0,sizeof(char)*100*13); int uniques=0, noprint=0, ndecodes_pass=0; @@ -632,7 +768,11 @@ int main(int argc, char *argv[]) float minsync2=0.12; //Second sync limit int iifac=8; //Step size in final DT peakup int symfac=50; //Soft-symbol normalizing factor - int maxdrift=4; //Maximum (+/-) drift + int block_demod=1; //Default is to use block demod on pass 2 + int subtraction=1; + int npasses=2; + int ndepth=-1; //Depth for OSD + float minrms=52.0 * (symfac/64.0); //Final test for plausible decoding delta=60; //Fano threshold step float bias=0.45; //Fano metric bias (used for both Fano and stack algorithms) @@ -643,14 +783,17 @@ int main(int argc, char *argv[]) int mettab[2][256]; - idat=malloc(sizeof(float)*maxpts); - qdat=malloc(sizeof(float)*maxpts); + idat=calloc(maxpts,sizeof(float)); + qdat=calloc(maxpts,sizeof(float)); - while ( (c = getopt(argc, argv, "a:cC:de:f:HJmqstwvz:")) !=-1 ) { + while ( (c = getopt(argc, argv, "a:BcC:de:f:HJmo:qstwvz:")) !=-1 ) { switch (c) { case 'a': data_dir = optarg; break; + case 'B': + block_demod=0; + break; case 'c': writec2=1; break; @@ -676,10 +819,13 @@ int main(int argc, char *argv[]) case 'm': //15-minute wspr mode wspr_type = 15; break; + case 'o': //use ordered-statistics-decoder + ndepth=(int) strtol(optarg,NULL,10); + break; case 'q': //no shift jittering quickmode = 1; break; - case 's': //single pass mode (same as original wsprd) + case 's': //single pass mode subtraction = 0; npasses = 1; break; @@ -700,7 +846,7 @@ int main(int argc, char *argv[]) } if( stackdecoder ) { - stack=malloc(stacksize*sizeof(struct snode)); + stack=calloc(stacksize,sizeof(struct snode)); } if( optind+1 > argc) { @@ -745,10 +891,10 @@ int main(int argc, char *argv[]) // fdiag=fopen("wsprd_diag","a"); if((ftimer=fopen(timer_fname,"r"))) { - //Accumulate timing data - nr=fscanf(ftimer,"%f %f %f %f %f %f %f", - &treadwav,&tcandidates,&tsync0,&tsync1,&tsync2,&tfano,&ttotal); - fclose(ftimer); + //Accumulate timing data + fscanf(ftimer,"%f %f %f %f %f %f %f", + &treadwav,&tcandidates,&tsync0,&tsync1,&tsync2,&tfano,&ttotal); + fclose(ftimer); } ftimer=fopen(timer_fname,"w"); @@ -809,11 +955,24 @@ int main(int argc, char *argv[]) //*************** main loop starts here ***************** for (ipass=0; ipass<npasses; ipass++) { - - if( ipass > 0 && ndecodes_pass == 0 ) break; - ndecodes_pass=0; + if(ipass == 0) { + nblocksize=1; + maxdrift=4; + minsync2=0.12; + } + if(ipass == 1 ) { + if(block_demod == 1) { + nblocksize=3; // try all blocksizes up to 3 + maxdrift=0; // no drift for smaller frequency estimator variance + minsync2=0.10; + } else { // if called with -B, revert to "classic" wspr params + nblocksize=1; + maxdrift=4; + minsync2=0.12; + } + } + ndecodes_pass=0; // still needed? - memset(ps,0.0, sizeof(float)*512*nffts); for (i=0; i<nffts; i++) { for(j=0; j<512; j++ ) { k=i*128+j; @@ -830,7 +989,7 @@ int main(int argc, char *argv[]) } // Compute average spectrum - memset(psavg,0.0, sizeof(float)*512); + for (i=0; i<512; i++) psavg[i]=0.0; for (i=0; i<nffts; i++) { for (j=0; j<512; j++) { psavg[j]=psavg[j]+ps[j][i]; @@ -864,7 +1023,8 @@ int main(int argc, char *argv[]) * The corresponding threshold is -42.3 dB in 2500 Hz bandwidth for WSPR-15. */ float min_snr, snr_scaling_factor; - min_snr = pow(10.0,-7.0/10.0); //this is min snr in wspr bw +// min_snr = pow(10.0,-7.0/10.0); //this is min snr in wspr bw + min_snr = pow(10.0,-8.0/10.0); //this is min snr in wspr bw if( wspr_type == 2 ) { snr_scaling_factor=26.3; } else { @@ -1031,7 +1191,6 @@ int main(int argc, char *argv[]) shift1=shift0[j]; sync1=sync0[j]; - // coarse-grid lag and freq search, then if sync>minsync1 continue fstep=0.0; ifmin=0; ifmax=0; lagmin=shift1-128; @@ -1047,25 +1206,26 @@ int main(int argc, char *argv[]) sync_and_demodulate(idat, qdat, npoints, symbols, &f1, ifmin, ifmax, fstep, &shift1, lagmin, lagmax, lagstep, &drift1, symfac, &sync1, 1); - // refine drift estimate - fstep=0.0; ifmin=0; ifmax=0; - float driftp,driftm,syncp,syncm; - driftp=drift1+0.5; - sync_and_demodulate(idat, qdat, npoints, symbols, &f1, ifmin, ifmax, fstep, &shift1, + if(ipass == 0) { + // refine drift estimate + fstep=0.0; ifmin=0; ifmax=0; + float driftp,driftm,syncp,syncm; + driftp=drift1+0.5; + sync_and_demodulate(idat, qdat, npoints, symbols, &f1, ifmin, ifmax, fstep, &shift1, lagmin, lagmax, lagstep, &driftp, symfac, &syncp, 1); - driftm=drift1-0.5; - sync_and_demodulate(idat, qdat, npoints, symbols, &f1, ifmin, ifmax, fstep, &shift1, + driftm=drift1-0.5; + sync_and_demodulate(idat, qdat, npoints, symbols, &f1, ifmin, ifmax, fstep, &shift1, lagmin, lagmax, lagstep, &driftm, symfac, &syncm, 1); - if(syncp>sync1) { - drift1=driftp; - sync1=syncp; - } else if (syncm>sync1) { - drift1=driftm; - sync1=syncm; + if(syncp>sync1) { + drift1=driftp; + sync1=syncp; + } else if (syncm>sync1) { + drift1=driftm; + sync1=syncm; + } } - tsync1 += (float)(clock()-t0)/CLOCKS_PER_SEC; // fine-grid lag and freq search @@ -1089,49 +1249,83 @@ int main(int argc, char *argv[]) worth_a_try = 0; } - int idt=0, ii=0, jiggered_shift; + int idt, ii, jittered_shift; float y,sq,rms; not_decoded=1; - - while ( worth_a_try && not_decoded && idt<=(128/iifac)) { - ii=(idt+1)/2; - if( idt%2 == 1 ) ii=-ii; - ii=iifac*ii; - jiggered_shift=shift1+ii; - - // Use mode 2 to get soft-decision symbols - t0 = clock(); - sync_and_demodulate(idat, qdat, npoints, symbols, &f1, ifmin, ifmax, fstep, - &jiggered_shift, lagmin, lagmax, lagstep, &drift1, symfac, - &sync1, 2); - tsync2 += (float)(clock()-t0)/CLOCKS_PER_SEC; + int osd_decode=0; + int ib=1, blocksize; + int n1,n2; + while( ib <= nblocksize && not_decoded ) { + blocksize=ib; + idt=0; ii=0; + while ( worth_a_try && not_decoded && idt<=(128/iifac)) { + ii=(idt+1)/2; + if( idt%2 == 1 ) ii=-ii; + ii=iifac*ii; + jittered_shift=shift1+ii; - sq=0.0; - for(i=0; i<162; i++) { - y=(float)symbols[i] - 128.0; - sq += y*y; - } - rms=sqrt(sq/162.0); - - if((sync1 > minsync2) && (rms > minrms)) { - deinterleave(symbols); + // Use mode 2 to get soft-decision symbols t0 = clock(); + noncoherent_sequence_detection(idat, qdat, npoints, symbols, &f1, + &jittered_shift, &drift1, symfac, &blocksize); + tsync2 += (float)(clock()-t0)/CLOCKS_PER_SEC; + + sq=0.0; + for(i=0; i<162; i++) { + y=(float)symbols[i] - 128.0; + sq += y*y; + } + rms=sqrt(sq/162.0); + + if((sync1 > minsync2) && (rms > minrms)) { + deinterleave(symbols); + t0 = clock(); - if ( stackdecoder ) { - not_decoded = jelinek(&metric, &cycles, decdata, symbols, nbits, + if ( stackdecoder ) { + not_decoded = jelinek(&metric, &cycles, decdata, symbols, nbits, stacksize, stack, mettab,maxcycles); - } else { - not_decoded = fano(&metric,&cycles,&maxnp,decdata,symbols,nbits, + } else { + not_decoded = fano(&metric,&cycles,&maxnp,decdata,symbols,nbits, mettab,delta,maxcycles); - } + } + + tfano += (float)(clock()-t0)/CLOCKS_PER_SEC; - tfano += (float)(clock()-t0)/CLOCKS_PER_SEC; + if( (ndepth >= 0) && not_decoded ) { + for(i=0; i<162; i++) { + fsymbs[i]=symbols[i]-127; + } + osdwspr_(fsymbs,apmask,&ndepth,cw,&nhardmin,&dmin); + for(i=0; i<162; i++) { + symbols[i]=255*cw[i]; + } + fano(&metric,&cycles,&maxnp,decdata,symbols,nbits, + mettab,delta,maxcycles); + for(i=0; i<11; i++) { + if( decdata[i]>127 ) { + message[i]=decdata[i]-256; + } else { + message[i]=decdata[i]; + } + } + unpack50(message,&n1,&n2); + if( !unpackcall(n1,callsign) ) break; + callsign[12]=0; + ihash=nhash(callsign,strlen(callsign),(uint32_t)146); + if(strncmp(hashtab+ihash*13,callsign,13)==0) { + not_decoded=0; + osd_decode =1; + break; + } + } + } + idt++; + if( quickmode ) break; } - idt++; - if( quickmode ) break; - } - + ib++; + } + if( worth_a_try && !not_decoded ) { ndecodes_pass++; @@ -1157,7 +1351,6 @@ int main(int argc, char *argv[]) } else { break; } - } // Remove dupes (same callsign and freq within 3 Hz) @@ -1192,6 +1385,9 @@ int main(int argc, char *argv[]) decodes[uniques-1].drift=drift1; decodes[uniques-1].cycles=cycles; decodes[uniques-1].jitter=ii; + decodes[uniques-1].blocksize=blocksize; + decodes[uniques-1].metric=metric; + decodes[uniques-1].osd_decode=osd_decode; } } } @@ -1223,11 +1419,11 @@ int main(int argc, char *argv[]) decodes[i].time, decodes[i].snr,decodes[i].dt, decodes[i].freq, (int)decodes[i].drift, decodes[i].message); fprintf(fall_wspr, - "%6s %4s %3d %3.0f %5.2f %11.7f %-22s %2d %5u %4d\n", + "%6s %4s %3d %3.0f %5.2f %11.7f %-22s %2d %5u %4d %4d %4d %2u\n", decodes[i].date, decodes[i].time, (int)(10*decodes[i].sync), decodes[i].snr, decodes[i].dt, decodes[i].freq, decodes[i].message, (int)decodes[i].drift, decodes[i].cycles/81, - decodes[i].jitter); + decodes[i].jitter,decodes[i].blocksize,decodes[i].metric,decodes[i].osd_decode); fprintf(fwsprd, "%6s %4s %3d %3.0f %4.1f %10.6f %-22s %2d %5u %4d\n", decodes[i].date, decodes[i].time, (int)(10*decodes[i].sync), @@ -1280,7 +1476,15 @@ int main(int argc, char *argv[]) } fclose(fhash); } - + + free(hashtab); + free(symbols); + free(decdata); + free(channel_symbols); + free(callsign); + free(call_loc_pow); + free(idat); + free(qdat); if( stackdecoder ) { free(stack); } diff --git a/lib/wsprd/wsprsim.c b/lib/wsprd/wsprsim.c index f368b209..b3af9980 100644 --- a/lib/wsprd/wsprsim.c +++ b/lib/wsprd/wsprsim.c @@ -20,6 +20,7 @@ void usage() { printf("Options:\n"); printf(" -c (print channel symbols)\n"); printf(" -d (print packed data with zero tail - 11 bytes)\n"); + printf(" -f x (-100 Hz < f < 100 Hz)\n"); printf(" -o filename (write a c2 file with this name)\n"); printf(" -s x (x is snr of signal that is written to .c2 file)\n"); printf("\n"); @@ -126,6 +127,7 @@ int main(int argc, char *argv[]) extern int optind; int i, c, printchannel=0, writec2=0; float snr=50.0; + float f0=0.0, t0=1.0; char *message, *c2filename, *hashtab; c2filename=malloc(sizeof(char)*15); hashtab=malloc(sizeof(char)*32768*13); @@ -138,7 +140,7 @@ int main(int argc, char *argv[]) srand(getpid()); - while ( (c = getopt(argc, argv, "cdo:s:")) !=-1 ) { + while ( (c = getopt(argc, argv, "cdf:o:s:")) !=-1 ) { switch (c) { case 'c': printchannel=1; @@ -146,12 +148,15 @@ int main(int argc, char *argv[]) case 'd': printdata=1; break; + case 'f': + f0 = atof(optarg); case 'o': c2filename = optarg; writec2=1; break; case 's': - snr = (float)atoi(optarg); +// snr = (float)atoi(optarg); + snr = atof(optarg); break; } } @@ -192,9 +197,6 @@ int main(int argc, char *argv[]) snr=1.0; } - float f0, t0; - f0=0.0; - t0=1.0; add_signal_vector(f0, t0, snr, channel_symbols, isig, qsig); if( writec2) { // write a .c2 file diff --git a/lib/wsprd/wsprsim_utils.c b/lib/wsprd/wsprsim_utils.c index d4a53c8b..f65efba4 100644 --- a/lib/wsprd/wsprsim_utils.c +++ b/lib/wsprd/wsprsim_utils.c @@ -6,6 +6,13 @@ #include "nhash.h" #include "fano.h" +static char get_locator_character_code(char ch); +static char get_callsign_character_code(char ch); +static long unsigned int pack_grid4_power(char const *grid4, int power); +static long unsigned int pack_call(char const *callsign); +static void pack_prefix(char *callsign, int32_t *n, int32_t *m, int32_t *nadd ); +static void interleave(unsigned char *sym); + char get_locator_character_code(char ch) { if( ch >=48 && ch <=57 ) { //0-9 return ch-48; @@ -32,7 +39,7 @@ char get_callsign_character_code(char ch) { return -1; } -long unsigned int pack_grid4_power(char *grid4, int power) { +long unsigned int pack_grid4_power(char const *grid4, int power) { long unsigned int m; m=(179-10*grid4[0]-grid4[2])*180+10*grid4[1]+grid4[3]; @@ -40,32 +47,23 @@ long unsigned int pack_grid4_power(char *grid4, int power) { return m; } -long unsigned int pack_call(char *callsign) { +long unsigned int pack_call(char const *callsign) { unsigned int i; long unsigned int n; char call6[6]; - memset(call6,32,sizeof(char)*6); + memset(call6,' ',sizeof(call6)); // callsign is 6 characters in length. Exactly. size_t call_len = strlen(callsign); if( call_len > 6 ) { return 0; } - if( isdigit(*(callsign+2)) ) { + if( isdigit(callsign[2]) ) { for (i=0; i<call_len; i++) { - if( callsign[i] == 0 ) { - call6[i]=32; - } else { - call6[i]=*(callsign+i); - } + call6[i]=callsign[i]; } - } else if( isdigit(*(callsign+1)) ) { - call6[0]=32; + } else if( isdigit(callsign[1]) ) { for (i=1; i<call_len+1; i++) { - if( callsign[i-1]==0 ) { - call6[i]=32; - } else { - call6[i]=*(callsign+i-1); - } + call6[i]=callsign[i-1]; } } for (i=0; i<6; i++) { @@ -82,9 +80,7 @@ long unsigned int pack_call(char *callsign) { void pack_prefix(char *callsign, int32_t *n, int32_t *m, int32_t *nadd ) { size_t i; - char *call6; - call6=malloc(sizeof(char)*6); - memset(call6,32,sizeof(char)*6); + char * call6 = calloc(7,sizeof (char)); size_t i1=strcspn(callsign,"/"); if( callsign[i1+2] == 0 ) { @@ -92,6 +88,7 @@ void pack_prefix(char *callsign, int32_t *n, int32_t *m, int32_t *nadd ) { for (i=0; i<i1; i++) { call6[i]=callsign[i]; } + call6[i] = '\0'; *n=pack_call(call6); *nadd=1; int nc = callsign[i1+1]; @@ -113,10 +110,10 @@ void pack_prefix(char *callsign, int32_t *n, int32_t *m, int32_t *nadd ) { *m=10*(callsign[i1+1]-48)+(callsign[i1+2]-48); *m=60000 + 26 + *m; } else { - char* pfx=strtok(callsign,"/"); - call6=strtok(NULL," "); - *n=pack_call(call6); - size_t plen=strlen(pfx); + char const * pfx = strtok (callsign,"/"); + char const * call = strtok(NULL," "); + *n = pack_call (call); + size_t plen=strlen (pfx); if( plen ==1 ) { *m=36; *m=37*(*m)+36; @@ -142,6 +139,7 @@ void pack_prefix(char *callsign, int32_t *n, int32_t *m, int32_t *nadd ) { *nadd=1; } } + free (call6); } void interleave(unsigned char *sym) @@ -165,7 +163,8 @@ void interleave(unsigned char *sym) } int get_wspr_channel_symbols(char* rawmessage, char* hashtab, unsigned char* symbols) { - int m=0, n=0, ntype=0; + int m=0, ntype=0; + long unsigned int n=0; int i, j, ihash; unsigned char pr3[162]= {1,1,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,1,0, @@ -197,7 +196,7 @@ int get_wspr_channel_symbols(char* rawmessage, char* hashtab, unsigned char* sym // Use the presence and/or absence of "<" and "/" to decide what // type of message. No sanity checks! Beware! - if( (i1>3) & (i1<7) & (i2==mlen) & (i3==mlen) ) { + if( i1 > 3 && i1 < 7 && i2 == mlen && i3 == mlen ) { // Type 1 message: K9AN EN50 33 // xxnxxxx xxnn nn callsign = strtok(message," "); @@ -227,20 +226,20 @@ int get_wspr_channel_symbols(char* rawmessage, char* hashtab, unsigned char* sym ihash=nhash(callsign,strlen(callsign),(uint32_t)146); m=128*ihash + ntype + 64; - char grid6[6]; - memset(grid6,32,sizeof(char)*6); + char grid6[7]; + memset(grid6,0,sizeof(char)*7); j=strlen(grid); for(i=0; i<j-1; i++) { grid6[i]=grid[i+1]; } grid6[5]=grid[0]; - n=pack_call(grid6); + n = pack_call(grid6); } else if ( i2 < mlen ) { // just looks for a right slash // Type 2: PJ4/K1ABC 37 - callsign=strtok(message," "); - if( strlen(callsign) < i2 ) return 0; //guards against pathological case - powstr=strtok(NULL," "); - int power = atoi(powstr); + callsign = strtok (message," "); + if( i2==0 || i2>strlen(callsign) ) return 0; //guards against pathological case + powstr = strtok (NULL," "); + int power = atoi (powstr); if( power < 0 ) power=0; if( power > 60 ) power=60; power=power+nu[power%10]; @@ -252,7 +251,7 @@ int get_wspr_channel_symbols(char* rawmessage, char* hashtab, unsigned char* sym } else { return 0; } - + // pack 50 bits + 31 (0) tail bits into 11 bytes unsigned char it, data[11]; memset(data,0,sizeof(char)*11); @@ -292,20 +291,22 @@ int get_wspr_channel_symbols(char* rawmessage, char* hashtab, unsigned char* sym check_callsign=malloc(sizeof(char)*13); signed char check_data[11]; memcpy(check_data,data,sizeof(char)*11); + unpk_(check_data,hashtab,check_call_loc_pow,check_callsign); // printf("Will decode as: %s\n",check_call_loc_pow); - + unsigned int nbytes=11; // The message with tail is packed into almost 11 bytes. unsigned char channelbits[nbytes*8*2]; /* 162 rounded up */ - memset(channelbits,0,sizeof channelbits); + memset(channelbits,0,sizeof(char)*nbytes*8*2); encode(channelbits,data,nbytes); interleave(channelbits); - + for (i=0; i<162; i++) { symbols[i]=2*channelbits[i]+pr3[i]; } - + free(check_call_loc_pow); + free(check_callsign); return 1; } diff --git a/lib/wsprd/wsprsim_utils.h b/lib/wsprd/wsprsim_utils.h index 2f4151a5..acffdee9 100644 --- a/lib/wsprd/wsprsim_utils.h +++ b/lib/wsprd/wsprsim_utils.h @@ -11,18 +11,6 @@ extern int printdata; -char get_locator_character_code(char ch); - -char get_callsign_character_code(char ch); - -long unsigned int pack_grid4_power(char *grid4, int power); - -long unsigned int pack_call(char *callsign); - -void pack_prefix(char *callsign, int32_t *n, int32_t *m, int32_t *nadd ); - -void interleave(unsigned char *sym); - int get_wspr_channel_symbols(char* message, char* hashtab, unsigned char* symbols); #endif |
