summaryrefslogtreecommitdiff
path: root/lib/sync24.f90
blob: cf6bf5afe4a7abd82d8928f82c16ab5a7e6d559c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
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
174
175
176
177
178
179
subroutine sync24(dat,jz,DFTolerance,NFreeze,MouseDF,mode,mode4,    &
     dtx,dfx,snrx,snrsync,ccfblue,ccfred1,flip,width)

! Synchronizes JT4 data, finding the best-fit DT and DF.  

  parameter (NFFTMAX=2520)         !Max length of FFTs
  parameter (NHMAX=NFFTMAX/2)      !Max length of power spectra
  parameter (NSMAX=525)            !Max number of half-symbol steps
  integer DFTolerance              !Range of DF search
  real dat(jz)
  real psavg(NHMAX)                !Average spectrum of whole record
  real s2(NHMAX,NSMAX)             !2d spectrum, stepped by half-symbols
  real ccfblue(-5:540)             !CCF with pseudorandom sequence
  real ccfred(-450:450)            !Peak of ccfblue, as function of freq
  real ccfred1(-224:224)           !Peak of ccfblue, as function of freq
  real tmp(1260)
  save

! Do FFTs of twice symbol length, stepped by half symbols.  Note that 
! we have already downsampled the data by factor of 2.

  nsym=207
  nfft=2520
  nh=nfft/2
  nq=nfft/4
  nsteps=jz/nq - 1
  df=0.5*11025.0/nfft
  psavg(1:nh)=0.

  do j=1,nsteps                     !Compute spectrum for each step, get average
     k=(j-1)*nq + 1
     call ps24(dat(k),nfft,s2(1,j))
     psavg(1:nh)=psavg(1:nh) + s2(1:nh,j)
  enddo

  call flat1(psavg,s2,nh,nsteps,NHMAX,NSMAX)        !Flatten spectra

! Set freq and lag ranges
  famin=200.
  fbmax=2700.
  fa=famin
  fb=fbmax
  if(NFreeze.eq.1) then
     fa=max(famin,1270.46+MouseDF-DFTolerance)
     fb=min(fbmax,1270.46+MouseDF+DFTolerance)
  else
     fa=max(famin,1270.46+MouseDF-600)
     fb=min(fbmax,1270.46+MouseDF+600)
  endif
  ia=fa/df
  ib=fb/df
  if(mode.eq.7) then
     ia=ia - 3*mode4
     ib=ib - 3*mode4
  endif
  i0=nint(1270.46/df)
  lag1=-5
  lag2=59
  syncbest=-1.e30
  syncbest2=-1.e30
  ccfred=0.

  do i=ia,ib                                !Find best frequency channel for CCF

     call xcor24(s2,i,nsteps,nsym,lag1,lag2,mode4,ccfblue,ccf0,lagpk0,flip)
     j=i-i0
     if(mode.eq.7) j=j + 3*mode4
     if(j.ge.-372 .and. j.le.372) ccfred(j)=ccf0

! Find rms of the CCF, without main peak
     call slope(ccfblue(lag1),lag2-lag1+1,lagpk0-lag1+1.0)
     sync=abs(ccfblue(lagpk0))
     ppmax=psavg(i)-1.0

! Find best sync value
     if(sync.gt.syncbest2) then
        ipk2=i
        lagpk2=lagpk0
        syncbest2=sync
     endif

! We are most interested if snrx will be more than -30 dB.
     if(ppmax.gt.0.2938) then            !Corresponds to snrx.gt.-30.0
        if(sync.gt.syncbest) then
           ipk=i
           lagpk=lagpk0
           syncbest=sync
        endif
     endif
  enddo

! If we found nothing with snrx > -30 dB, take the best sync that *was* found.
  if(syncbest.lt.-10.) then
     ipk=ipk2
     lagpk=lagpk2
     syncbest=syncbest2
  endif

  dfx=(ipk-i0)*df
  if(mode.eq.7) dfx=dfx + 3*mode4*df

! Peak up in time, at best whole-channel frequency
  call xcor24(s2,ipk,nsteps,nsym,lag1,lag2,mode4,ccfblue,ccfmax,lagpk,flip)
  xlag=lagpk
  if(lagpk.gt.lag1 .and. lagpk.lt.lag2) then
     call peakup(ccfblue(lagpk-1),ccfmax,ccfblue(lagpk+1),dx2)
     xlag=lagpk+dx2
  endif

! Find rms of the CCF, without the main peak
  call slope(ccfblue(lag1),lag2-lag1+1,xlag-lag1+1.0)
  sq=0.
  nsq=0
  do lag=lag1,lag2
     if(abs(lag-xlag).gt.2.0) then
        sq=sq+ccfblue(lag)**2
        nsq=nsq+1
     endif
  enddo
  rms=sqrt(sq/nsq)
  snrsync=abs(ccfblue(lagpk))/rms - 1.1                       !Empirical

  dt=2.0/11025.0
  istart=xlag*nq
  dtx=istart*dt
  snrx=-99.0
  ppmax=psavg(ipk)-1.0

  if(ppmax.gt.0.0001) then
     snrx=db(ppmax*df/2500.0) + 7.5        !Empirical
     if(mode.eq.7) snrx=snrx + 3.0         !Empirical
  endif
  if(snrx.lt.-33.0) snrx=-33.0

! Compute width of sync tone to outermost -3 dB points
  i1=max(-450,ia-i0)
  i2=min(450,ib-i0)
  call pctile(ccfred(i1),i2-i1+1,45,base)

  jpk=ipk-i0
  if(abs(jpk).gt.450) then
     print*,'sync24 a:',jpk,ipk,i0
     snrsync=0.
     go to 999
  else
     stest=base + 0.5*(ccfred(jpk)-base) ! -3 dB
  endif
  do i=-10,0
     if(jpk+i.ge.-371) then 
        if(ccfred(jpk+i).gt.stest) go to 30
     endif
  enddo
  i=0
30 continue
  if(abs(jpk+i-1).gt.450 .or. abs(jpk+i).gt.450) then
     print*,'sync24 b:',jpk,i
  else
     x1=i-0.5
  endif

  do i=10,0,-1
     if(jpk+i.le.371) then
        if(ccfred(jpk+i).gt.stest) go to 32
     endif
  enddo
  i=0
32 x2=i+0.5
  width=x2-x1
  if(width.gt.1.2) width=sqrt(width**2 - 1.44)
  width=df*width
  width=max(0.0,min(99.0,width))

  do i=-224,224
     ccfred1(i)=ccfred(i)
  enddo

999 return
end subroutine sync24