title: my power spectra are believable? a comparison between lomb-scargle and fft (scipy.signal and numpy.fft)
body: <p>Could anyone kindly point out why I get very different results? <br/>
  There are many peaks which should not appear.In fact,there should be only one peak.<br/>
  I  am a python newbie and all comments about my code below are welcome.</p>
  <p>The test data is here.<a href="https://clbin.com/YJkwr" rel="nofollow noreferrer">enter link description here</a><br/>
  You can directly  <code>wget  https://clbin.com/YJkwr</code> . <br/>
  Its first column are the arrival times for a series of photons.A light source emits photons randomly.
  The total time is 55736s and there are 67398 photons.
  I try to detect some kind of periodicity of light intensity. <br>
  We can bin the time and light intensity is proportional to the photon numbers in every time bin.</p>
  <p>I tried numpy.fft and lomb-scargle of scipy.signal to make its power spectrum,but got very different results.</p>
  <h1>fft</h1>
  <pre><code> import pylab as pl
   import numpy as np
  
   timepoints=np.loadtxt('timesequence',usecols=(0,),unpack=True,delimiter=&quot;,&quot;)
   binshu=50000
   interd=54425./binshu  
   t=np.histogram(timepoints,bins=binshu)[0]
   sp = np.fft.fft(t)
   freq = np.fft.fftfreq(len(t),d=interd)  
   freqnum = np.fft.fftfreq(len(t),d=interd).argsort()  
   pl.xlabel(&quot;frequency(Hz)&quot;)
   pl.plot(freq[freqnum],np.abs(sp)[freqnum])
  </code></pre>
  <p><img src="https://i.stack.imgur.com/xEYHM.png" alt="enter image description here" /></p>
  <h1>lomb-scargle</h1>
  <pre><code> timepoints=np.loadtxt('timesequence',usecols=(0,),unpack=True,delimiter=&quot;,&quot;)
   binshu=50000
   intensity=np.histogram(timepoints,bins=binshu)[0].astype('float64') 
   middletime=np.histogram(timepoints,bins=binshu)[1][1:binshu+1]-np.histogram(timepoints,bins=binshu)[1][3]*0.5
   freq1=1./(timepoints.max()-timepoints.min())
   freq2=freq1*len(timepoints)
   freqs=np.linspace(freq1,freq2,1000)
   pm1=spectral.lombscargle(middletime,intensity,freqs)
  
   pl.xlabel(&quot;frequency(Hz)&quot;)
   pl.plot(freqs,pm1)
   pl.xlim(0.5*freq1,2*freq2)
   pl.ylim(0,250)
   pl.show()
  </code></pre>
  <p><img src="https://i.stack.imgur.com/uPCtz.png" alt="enter image description here" /></p>
  <p>*********************************<br/>
  Thank you, Warren,I appreciate it.Thank you for your detailed reply.</p>
  <p>You are right,there is an integration time,here it is around 1.7s.<br/>
  There are many single exposures.Every exposure costs 1.7s<br/>
  In one single exposure,we can not tell its arrival time precisely.<br/></p>
  <p>If the time series are like:<br/>
  0 1.7 3.4  8.5 8.5 <br/></p>
  <p>The integration time of the last two photons are <code>1.7s</code>,not <code>(8.5-3.4)s</code>.So I will revise part of your code.<br></p>
  <p>HOWEVER my question still remains. You adjust several parameters to get the <code>0.024Hz</code> peak in lomb-scargle peak to some degree. And you use this to guide your parameters in fft.</p>
  <p>If you do not know the the number <code>0.024</code>, maybe you may use different parameters to get a different highest peak ?<br/></p>
  <p>How to guarantee every time we can get right the <code>num_ls_freqs</code> ? You can see if we choose different <code>num_ls_freqs</code>,the highest peak shifts.<br/></p>
  <p>If I have many timing series, every time I have to specify different parameters? And how to get them?<br/></p>