Lv2_ps_method.py
def manual(times,counts,xlims,vlines,toplot,oversampling):
    """
    Generating the power spectrum through the manual FFT method.

    times - array of binned times
    counts - array of counts from the binned data
    xlims - a list or array: first entry = True/False as to whether to impose an
    xlim; second and third entry correspond to the desired x-limits of the plot
    vlines - a list or array: first entry = True/False as to whether to draw
    a vertical line in the plot; second entry is the equation for the vertical line
    toplot - whether to show the plot or not
    oversampling - whether to perform oversampling. Array will consist of
    [True/False, oversampling factor]
    """
    if type(times) != list and type(times) != np.ndarray:
        raise TypeError("times should either be a list or an array!")
    if type(counts) != list and type(counts) != np.ndarray:
        raise TypeError("counts should either be a list or an array!")
    if type(xlims) != list and type(xlims) != np.ndarray:
        raise TypeError("xlims should either be a list or an array!")
    if type(vlines) != list and type(vlines) != np.ndarray:
        raise TypeError("vlines should either be a list or an array!")
    if toplot != True and toplot != False:
        raise ValueError("toplot should either be True or False!")
    if type(oversampling) != list and type(oversampling) != np.ndarray:
        raise TypeError("oversampling should either be a list or an array!")

    T = times[-1] - times[0]
    dt = T/len(times)
    print('Nyquist frequency: ' + str(1/(2*dt)))
    print('Frequency resolution: ' + str(1/T))

    padded_counts = padding(counts)
    mean_corrected = padded_counts-np.mean(counts)
    power_spec = 2.0/sum(counts)*np.abs(np.fft.fft(mean_corrected))**2 #sum(counts) = N_photons
    freqs = np.fft.fftfreq(padded_counts.size,dt)
    #print('Length of power spectrum from Lv2_ps_method is: ' + str(len(power_spec)))

    if oversampling[0] == True:
        os_counts = oversample(oversampling[1],counts)
        dt = T/len(times)

        padded_counts = padding(os_counts)
        mean_corrected = padded_counts-np.mean(counts)
        power_spec = 2.0/sum(counts)*np.abs(np.fft.fft(mean_corrected))**2 #sum(counts) = N_photons
        freqs = np.fft.fftfreq(padded_counts.size,dt)

    N = len(freqs)
#    checkmean = power_spec[(freqs>=10)&(freqs<=50)]
#    print(np.mean(checkmean))
    ####freqs = np.linspace(0,1/(2.0*dt),int(len(test_times)/2)) #correct method! Same as that of fftfreq
    if toplot == True:
        plt.semilogy(freqs[1:int(N/2)],power_spec[1:int(N/2)],'r-')#/np.mean(power_spec[1:int(N/2)]),'rx-')
        plt.xlabel('Hz')
        plt.ylabel('Normalized power spectrum')
        if xlims[0] == True:
            plt.xlim([xlims[1],xlims[2]])
        if vlines[0] == True:
            plt.axvline(x=vlines[1],color='k',alpha=0.5,lw=0.5)

    if sum(counts) == 0: #for Lv3_average_ps_segments, when a segment has no data!
        #Added on 5/11/19
        power_spec = np.ones(int(N/2)) + np.ones(int(N/2))

    return freqs[1:int(N/2)], power_spec[1:int(N/2)]

    Generating the power spectrum through the manual FFT method.

    times - array of binned times
    counts - array of counts from the binned data
    xlims - a list or array: first entry = True/False as to whether to impose an
    xlim; second and third entry correspond to the desired x-limits of the plot
    vlines - a list or array: first entry = True/False as to whether to draw
    a vertical line in the plot; second entry is the equation for the vertical line
    toplot - whether to show the plot or not
    oversampling - whether to perform oversampling. Array will consist of
    [True/False, oversampling factor]
    
Generating the power spectrum through the manual FFT method.
times - array of binned times
counts - array of counts from the binned data
xlims - a list or array: first entry = True/False as to whether to impose an
xlim; second and third entry correspond to the desired x-limits of the plot
vlines - a list or array: first entry = True/False as to whether to draw
a vertical line in the plot; second entry is the equation for the vertical line
toplot - whether to show the plot or not
oversampling - whether to perform oversampling. Array will consist of
[True/False, oversampling factor]
Generating the power spectrum through the manual FFT method . times - array of binned times counts - array of counts from the binned data xlims - a list or array : first entry = True / False as to whether to impose an xlim ; second and third entry correspond to the desired x - limits of the plot vlines - a list or array : first entry = True / False as to whether to draw a vertical line in the plot ; second entry is the equation for the vertical line toplot - whether to show the plot or not oversampling - whether to perform oversampling . Array will consist of [ True / False oversampling factor ]
def manual(times,counts,xlims,vlines,toplot,oversampling):
    if type(times) != list and type(times) != np.ndarray:
        raise TypeError("times should either be a list or an array!")
    if type(counts) != list and type(counts) != np.ndarray:
        raise TypeError("counts should either be a list or an array!")
    if type(xlims) != list and type(xlims) != np.ndarray:
        raise TypeError("xlims should either be a list or an array!")
    if type(vlines) != list and type(vlines) != np.ndarray:
        raise TypeError("vlines should either be a list or an array!")
    if toplot != True and toplot != False:
        raise ValueError("toplot should either be True or False!")
    if type(oversampling) != list and type(oversampling) != np.ndarray:
        raise TypeError("oversampling should either be a list or an array!")
    T = times[-1] - times[0]
    dt = T/len(times)
    print('Nyquist frequency: ' + str(1/(2*dt)))
    print('Frequency resolution: ' + str(1/T))
    padded_counts = padding(counts)
    mean_corrected = padded_counts-np.mean(counts)
    power_spec = 2.0/sum(counts)*np.abs(np.fft.fft(mean_corrected))**2 
    freqs = np.fft.fftfreq(padded_counts.size,dt)
    if oversampling[0] == True:
        os_counts = oversample(oversampling[1],counts)
        dt = T/len(times)
        padded_counts = padding(os_counts)
        mean_corrected = padded_counts-np.mean(counts)
        power_spec = 2.0/sum(counts)*np.abs(np.fft.fft(mean_corrected))**2 
        freqs = np.fft.fftfreq(padded_counts.size,dt)
    N = len(freqs)
    if toplot == True:
        plt.semilogy(freqs[1:int(N/2)],power_spec[1:int(N/2)],'r-')
        plt.xlabel('Hz')
        plt.ylabel('Normalized power spectrum')
        if xlims[0] == True:
            plt.xlim([xlims[1],xlims[2]])
        if vlines[0] == True:
            plt.axvline(x=vlines[1],color='k',alpha=0.5,lw=0.5)
    if sum(counts) == 0: 
        power_spec = np.ones(int(N/2)) + np.ones(int(N/2))
    return freqs[1:int(N/2)], power_spec[1:int(N/2)]
def manual ( times , counts , xlims , vlines , toplot , oversampling ) : if type ( times ) != list and type ( times ) != np . ndarray : raise TypeError ( "times should either be a list or an array!" ) if type ( counts ) != list and type ( counts ) != np . ndarray : raise TypeError ( "counts should either be a list or an array!" ) if type ( xlims ) != list and type ( xlims ) != np . ndarray : raise TypeError ( "xlims should either be a list or an array!" ) if type ( vlines ) != list and type ( vlines ) != np . ndarray : raise TypeError ( "vlines should either be a list or an array!" ) if toplot != True and toplot != False : raise ValueError ( "toplot should either be True or False!" ) if type ( oversampling ) != list and type ( oversampling ) != np . ndarray : raise TypeError ( "oversampling should either be a list or an array!" ) T = times [ - 1 ] - times [ 0 ] dt = T / len ( times ) print ( 'Nyquist frequency: ' + str ( 1 / ( 2 * dt ) ) ) print ( 'Frequency resolution: ' + str ( 1 / T ) ) padded_counts = padding ( counts ) mean_corrected = padded_counts - np . mean ( counts ) power_spec = 2.0 / sum ( counts ) * np . abs ( np . fft . fft ( mean_corrected ) ) ** 2 freqs = np . fft . fftfreq ( padded_counts . size , dt ) if oversampling [ 0 ] == True : os_counts = oversample ( oversampling [ 1 ] , counts ) dt = T / len ( times ) padded_counts = padding ( os_counts ) mean_corrected = padded_counts - np . mean ( counts ) power_spec = 2.0 / sum ( counts ) * np . abs ( np . fft . fft ( mean_corrected ) ) ** 2 freqs = np . fft . fftfreq ( padded_counts . size , dt ) N = len ( freqs ) if toplot == True : plt . semilogy ( freqs [ 1 : int ( N / 2 ) ] , power_spec [ 1 : int ( N / 2 ) ] , 'r-' ) plt . xlabel ( 'Hz' ) plt . ylabel ( 'Normalized power spectrum' ) if xlims [ 0 ] == True : plt . xlim ( [ xlims [ 1 ] , xlims [ 2 ] ] ) if vlines [ 0 ] == True : plt . axvline ( x = vlines [ 1 ] , color = 'k' , alpha = 0.5 , lw = 0.5 ) if sum ( counts ) == 0 : power_spec = np . ones ( int ( N / 2 ) ) + np . ones ( int ( N / 2 ) ) return freqs [ 1 : int ( N / 2 ) ] , power_spec [ 1 : int ( N / 2 ) ]
Generating the power spectrum through the manual FFT method.
Generating the power spectrum through the manual FFT method .
"""
    Generating the power spectrum through the manual FFT method.

    times - array of binned times
    counts - array of counts from the binned data
    xlims - a list or array: first entry = True/False as to whether to impose an
    xlim; second and third entry correspond to the desired x-limits of the plot
    vlines - a list or array: first entry = True/False as to whether to draw
    a vertical line in the plot; second entry is the equation for the vertical line
    toplot - whether to show the plot or not
    oversampling - whether to perform oversampling. Array will consist of
    [True/False, oversampling factor]
    """ #sum(counts) = N_photons #print('Length of power spectrum from Lv2_ps_method is: ' + str(len(power_spec))) #sum(counts) = N_photons #    checkmean = power_spec[(freqs>=10)&(freqs<=50)] #    print(np.mean(checkmean)) ####freqs = np.linspace(0,1/(2.0*dt),int(len(test_times)/2)) #correct method! Same as that of fftfreq #/np.mean(power_spec[1:int(N/2)]),'rx-') #for Lv3_average_ps_segments, when a segment has no data! #Added on 5/11/19
times
counts
xlims
vlines
toplot
oversampling