| 
									
										
										
										
											2016-03-18 23:29:10 +00:00
										 |  |  | subroutine sync65(ss,nfa,nfb,naggressive,ntol,nhsym,ca,ncand,nrobust,   &
 | 
					
						
							| 
									
										
										
										
											2016-10-24 20:48:24 +00:00
										 |  |  |      bVHF)
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   parameter (NSZ=3413,NFFT=8192,MAXCAND=300)
 | 
					
						
							|  |  |  |   real ss(322,NSZ)
 | 
					
						
							| 
									
										
										
										
											2015-11-25 16:40:22 +00:00
										 |  |  |   real ccfblue(-11:540)             !CCF with pseudorandom sequence
 | 
					
						
							| 
									
										
										
										
											2016-03-18 23:29:10 +00:00
										 |  |  |   real ccfred(NSZ)                  !Peak of ccfblue, as function of freq
 | 
					
						
							| 
									
										
										
										
											2016-10-24 20:48:24 +00:00
										 |  |  |   logical bVHF
 | 
					
						
							| 
									
										
										
										
											2016-03-18 23:29:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |   type candidate
 | 
					
						
							|  |  |  |      real freq
 | 
					
						
							|  |  |  |      real dt
 | 
					
						
							|  |  |  |      real sync
 | 
					
						
							| 
									
										
										
										
											2016-05-03 16:10:22 +00:00
										 |  |  |      real flip
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |   end type candidate
 | 
					
						
							|  |  |  |   type(candidate) ca(MAXCAND)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   common/steve/thresh0
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-10 14:18:10 +00:00
										 |  |  |   if(ntol.eq.-99) stop                       !Silence compiler warning
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |   call setup65
 | 
					
						
							| 
									
										
										
										
											2015-12-18 19:58:10 +00:00
										 |  |  |   df=12000.0/NFFT                            !df = 12000.0/8192 = 1.465 Hz
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |   ia=max(2,nint(nfa/df))
 | 
					
						
							|  |  |  |   ib=min(NSZ-1,nint(nfb/df))
 | 
					
						
							| 
									
										
										
										
											2015-11-25 16:40:22 +00:00
										 |  |  |   lag1=-11
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |   lag2=59
 | 
					
						
							|  |  |  |   nsym=126
 | 
					
						
							|  |  |  |   ncand=0
 | 
					
						
							|  |  |  |   fdot=0.
 | 
					
						
							|  |  |  |   ccfred=0.
 | 
					
						
							|  |  |  |   ccfblue=0.
 | 
					
						
							| 
									
										
										
										
											2015-12-18 19:58:10 +00:00
										 |  |  |   ccfmax=0.
 | 
					
						
							|  |  |  |   ipk=0
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   do i=ia,ib
 | 
					
						
							|  |  |  |      call xcor(ss,i,nhsym,nsym,lag1,lag2,ccfblue,ccf0,lagpk0,flip,fdot,nrobust)
 | 
					
						
							|  |  |  | ! Remove best-fit slope from ccfblue and normalize so baseline rms=1.0
 | 
					
						
							| 
									
										
										
										
											2016-10-24 20:48:24 +00:00
										 |  |  |      if(.not.bVHF) call slope(ccfblue(lag1),lag2-lag1+1,      &
 | 
					
						
							| 
									
										
										
										
											2016-03-18 23:29:10 +00:00
										 |  |  |           lagpk0-lag1+1.0)
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |      ccfred(i)=ccfblue(lagpk0)
 | 
					
						
							| 
									
										
										
										
											2015-12-18 19:58:10 +00:00
										 |  |  |      if(ccfred(i).gt.ccfmax) then
 | 
					
						
							|  |  |  |         ccfmax=ccfred(i)
 | 
					
						
							|  |  |  |         ipk=i
 | 
					
						
							|  |  |  |      endif
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |   enddo
 | 
					
						
							|  |  |  |   call pctile(ccfred(ia:ib),ib-ia+1,35,xmed)
 | 
					
						
							|  |  |  |   ccfred(ia:ib)=ccfred(ia:ib)-xmed
 | 
					
						
							|  |  |  |   ccfred(ia-1)=ccfred(ia)
 | 
					
						
							|  |  |  |   ccfred(ib+1)=ccfred(ib)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   do i=ia,ib
 | 
					
						
							|  |  |  |      freq=i*df
 | 
					
						
							|  |  |  |      itry=0
 | 
					
						
							| 
									
										
										
										
											2016-04-12 14:24:55 +00:00
										 |  |  | !     if(naggressive.gt.0 .and. ntol.lt.1000 .and. ccfmax.ge.thresh0) then
 | 
					
						
							|  |  |  |      if(naggressive.gt.0 .and. ccfmax.ge.thresh0) then
 | 
					
						
							| 
									
										
										
										
											2015-12-18 19:58:10 +00:00
										 |  |  |         if(i.ne.ipk) cycle
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |         itry=1
 | 
					
						
							|  |  |  |         ncand=ncand+1
 | 
					
						
							| 
									
										
										
										
											2015-12-18 19:58:10 +00:00
										 |  |  |      else
 | 
					
						
							|  |  |  |         if(ccfred(i).ge.thresh0 .and. ccfred(i).gt.ccfred(i-1) .and.       &
 | 
					
						
							|  |  |  |              ccfred(i).gt.ccfred(i+1)) then
 | 
					
						
							|  |  |  |            itry=1
 | 
					
						
							|  |  |  |            ncand=ncand+1
 | 
					
						
							|  |  |  |         endif
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |      endif
 | 
					
						
							|  |  |  |      if(itry.ne.0) then
 | 
					
						
							| 
									
										
										
										
											2016-03-18 23:29:10 +00:00
										 |  |  |         call xcor(ss,i,nhsym,nsym,lag1,lag2,ccfblue,ccf0,lagpk,flip,fdot,  &
 | 
					
						
							|  |  |  |              nrobust)
 | 
					
						
							| 
									
										
										
										
											2016-10-24 20:48:24 +00:00
										 |  |  |         if(.not.bVHF) call slope(ccfblue(lag1),lag2-lag1+1,       &
 | 
					
						
							| 
									
										
										
										
											2016-03-18 23:29:10 +00:00
										 |  |  |              lagpk-lag1+1.0)
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |         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
 | 
					
						
							|  |  |  |         dtx=xlag*2048.0/11025.0
 | 
					
						
							|  |  |  |         ccfblue(lag1)=0.
 | 
					
						
							|  |  |  |         ccfblue(lag2)=0.
 | 
					
						
							|  |  |  |         ca(ncand)%freq=freq
 | 
					
						
							|  |  |  |         ca(ncand)%dt=dtx
 | 
					
						
							| 
									
										
										
										
											2016-05-03 16:10:22 +00:00
										 |  |  |         ca(ncand)%flip=flip
 | 
					
						
							| 
									
										
										
										
											2016-10-24 20:48:24 +00:00
										 |  |  |         if(bVHF) then
 | 
					
						
							| 
									
										
										
										
											2016-03-18 23:29:10 +00:00
										 |  |  |            ca(ncand)%sync=db(ccfred(i)) - 16.0
 | 
					
						
							|  |  |  |         else
 | 
					
						
							|  |  |  |            ca(ncand)%sync=ccfred(i)
 | 
					
						
							|  |  |  |         endif
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |      endif
 | 
					
						
							| 
									
										
										
										
											2016-05-03 16:10:22 +00:00
										 |  |  |      if(ncand.eq.MAXCAND) exit
 | 
					
						
							| 
									
										
										
										
											2015-11-18 01:28:12 +00:00
										 |  |  |   enddo
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return
 | 
					
						
							|  |  |  | end subroutine sync65
 |