! using the points in the neighborhood of the peak, fit the tune
subroutine fit_peak(fft, fft_peak, n, fit_tune)
use bmad
implicit none

real(rp), allocatable :: fft(:)
real(rp) fit_tune,int_tune
real(rp) a,b,c,d
integer fft_peak 
integer n
 
  d= abs(fft(fft_peak))
  b=abs(fft(fft_peak+1))
  if(d /= 0. .or. b /= 0.)then
   c=cos(twopi/n)
   A= (-(d+b*c)*(d-b)+b*sqrt(c*c*(d+b)*(d+b)-2*d*b*(2*c*c-c-1)))/(d*d+b*b+2*d*b*c)
  endif
  int_tune = fft_peak/float(n)
  fit_tune= int_tune + (1/twopi)*asin(A*sin(twopi/float(n)))
return
end subroutine
