my_ctime = ctime_nproc[my_idx[0]]
     my_h1 = strain1_nproc[my_idx[0]]
     my_l1 = strain2_nproc[my_idx[0]]
     my_endtime = my_ctime[-1]
 
 
 
 ########################### data  massage 3  #################################
 # FILTERING/SIMULATING & FFTing THE DATA; PREPPING IT FOR  MAPPING
 
 print 'filtering, ffting & saving the strains...'
 
 # Fourier space objects: Nt optimal timestream length; freqs frequency array at chosen fs
 
 Nt = len(my_h1)
 Nt = lf.bestFFTlength(Nt)
 
 freqs = np.fft.rfftfreq(2*Nt, 1./fs)
 freqs = freqs[:Nt/2+1]
 
 
 # frequency mask
 
 mask = (freqs>low_f) & (freqs < high_f)
 
 
 # repackage the strains & copy them (fool-proof); create empty array for the filtered, FFTed, correlated data
 
 strains = (my_h1,my_l1)
 strains_copy = (my_h1.copy(),my_l1.copy()) #calcualte psds from these
 
Exemple #2
0
    def filter(self,strain_in,low_f,high_f, hf_psd, simulate = False):
        fs=self.fs        
        dt=1./fs
        
        '''WINDOWING & RFFTING.'''
        
        Nt = len(strain_in)
        Nt = lf.bestFFTlength(Nt)
        strain_in = strain_in[:Nt]
        strain_in_nowin = np.copy(strain_in)
        strain_in_nowin *= signal.tukey(Nt,alpha=0.05)
        strain_in *= np.blackman(Nt)
        freqs = np.fft.rfftfreq(2*Nt, dt)
        #print '=rfft='
        hf = np.fft.rfft(strain_in, n=2*Nt)#, norm = 'ortho') 
        hf_nowin = np.fft.rfft(strain_in_nowin, n=2*Nt)#, norm = 'ortho') 
        #print '++'
        
        hf = hf[:Nt/2+1]
        hf_nowin = hf_nowin[:Nt/2+1]
        freqs = freqs[:Nt/2+1]
                
        '''the PSD. '''
        #Pxx, frexx = mlab.psd(strain_in_nowin, Fs=fs, NFFT=2*fs,noverlap=fs/2,window=np.blackman(2*fs),scale_by_freq=True)
        #hf_psd = interp1d(frexx,Pxx)
        #hf_psd_data = abs(hf_nowin.copy()*np.conj(hf_nowin.copy())/(fs**2))
        
        #if sim: return simulated noise
        # strain_in = sim noise
        
        #Norm
        #mask = (freqs>low_f) & (freqs < high_f)
        #norm = np.mean(hf_psd_data[mask])/np.mean(hf_psd(freqs)[mask])
        
        #print norm
        
        #hf_psd=interp1d(frexx,Pxx*norm)
        
        
        '''NOTCHING. '''
        
        hf_in = hf.copy()
        notch_fs = np.array([14.0,34.70, 35.30, 35.90, 36.70, 37.30, 40.95, 60.00, 120.00, 179.99, 304.99, 331.49, 510.02, 1009.99])
        sigma_fs = np.array([.5,.5,.5,.5,.5,.5,.5,1.,1.,1.,1.,5.,5.,1.])
        #np.array([0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.5,0.3,0.2])
        
        samp_hz = fs**2*(len(hf))**(-1.)-6.68 #correction due to?
                
        pixels = np.arange(len(hf))

        #hf_psd = abs(hf_win.copy())**2
        #hf_psd_in = hf_psd.copy()
             
        i = 0
          
        while i < len(notch_fs):
            notch_pix = int(notch_fs[i]*samp_hz)
            hf_nowin = hf_nowin*(1.-self.gaussian(pixels,notch_pix,sigma_fs[i]*samp_hz))
            i+=1           
        
        #FITTING PSD. ffit(self,f,a,b,c,d,e)
        
        #cropping:
        #mask = np.ones(len(freqs),dtype=bool)
            
        #for (j,notch) in enumerate(notch_fs):      
        #    for (i,f) in enumerate(freqs):
        #        if abs(f-notch) < 1.: mask[i] = False
        
        #low_f = 50.
        #high_f = 350.
        
        # for (f,hfi,bo) in zip(freqs[low_f*int(samp_hz):high_f*int(samp_hz)],hf_psd[low_f*int(samp_hz):high_f*int(samp_hz)],mask[low_f*int(samp_hz):high_f*int(samp_hz)]):
        #      if bo == True:
        #          freqscut.append(f)
        #          hf_psdcut.append(hfi)

        #psd_params, psd_cov = curve_fit(self.ffit2,freqscut,hf_psdcut,p0=(40.,200.,3.21777e-44))
        
        #fitted = np.array(self.ffit2(freqs[1:],psd_params[0],psd_params[1],psd_params[2]))
        
        #BPING HF
        
        gauss_lo = self.halfgaussian(pixels,low_f*samp_hz,samp_hz)
        gauss_hi = self.halfgaussian(pixels,high_f*samp_hz,samp_hz)
        hf_nbped = hf_nowin*(1.-gauss_lo)*(gauss_hi)
              
        
        # whitening: transform to freq domain, divide by asd
        # remember: interp_psd is strain/rtHz
        # white_hf = hf_nbped/(np.sqrt(hf_psd(freqs)/2./dt))#hf_psd_in[1:]
        # white_hf[0] = 0.
        # white_hf[-1:] = 0.
        #white_hf_bp = white_hf*self.g_butt(freqs,3)
        
        #hf_inv = np.fft.irfft(white_hf, n=Nt)
        
        # index = [idx, dect]
        #
            
        return hf_nbped#, hf_psd
def PSD_params(strain1, low_f, high_f, run_name):

    Nt = len(strain1)
    Nt = lf.bestFFTlength(Nt)

    fs = 4096
    dt = 1. / fs

    freqs = np.fft.rfftfreq(Nt, 1. / fs)

    # frequency mask

    mask = (freqs > low_f) & (freqs < high_f)

    strain_in_1 = strain1
    '''WINDOWING & RFFTING.'''

    Nt = len(strain_in_1)
    Nt = lf.bestFFTlength(Nt)

    strain_in = strain_in_1[:Nt]
    strain_in_cp = np.copy(strain_in)

    strain_in_nowin = np.copy(strain_in)
    strain_in_nowin *= signal.tukey(Nt, alpha=0.05)
    strain_in_cp *= signal.tukey(Nt, alpha=0.05)

    freqs = np.fft.rfftfreq(Nt, dt)
    hf_nowin = np.fft.rfft(strain_in_nowin, n=Nt,
                           norm='ortho')  #####!HERE! 03/03/18 #####

    fstar = fs

    Psd_data = abs(hf_nowin.copy() * np.conj(hf_nowin.copy()))

    mask = (freqs > low_f) & (freqs < high_f)

    if high_f < 300. or low_f < 30.:
        masxx = (freqs > 30.) & (freqs < 300.)

    else:
        masxx = (freqs > low_f) & (freqs < high_f)

    freqs_cp = np.copy(freqs)
    Psd_cp = np.copy(Psd_data)
    freqs_cp = freqs_cp[masxx]
    Psd_cp = Psd_cp[masxx]

    freqs_notch, Psd_notch = Pdx_notcher(freqs_cp, Psd_cp, run_name)
    freqcp = np.copy(freqs_notch)
    Pscp = np.copy(Psd_notch)

    try:
        fit = curve_fit(PDX, freqcp,
                        Pscp)  #, bounds = ([0.,0.,0.],[2.,2.,2.]))
        psd_params = fit[0]

    except RuntimeError:
        print myid, "Error - curve_fit failed"
        psd_params = [10., 10., 10.]

    # plt.figure()
    #
    # plt.loglog(freqs[mask], np.abs(hf_nowin[mask])**2, label = 'nowin PSD')
    # plt.loglog(freqs[mask], hf_psd(freqs[mask])*1., label = 'mlab PSD')
    #
    # plt.loglog(frexcp, Pxcp, label = 'notchy PSD')
    #
    # plt.loglog(frexx[masxx],PDX(frexx,a,b,c)[masxx], label = 'notched pdx fit')
    # plt.legend()
    # plt.show()

    # plt.figure()
    #
    # plt.loglog(freqs[mask], np.abs(hf_nowin[mask])**2, label = 'nowin PSD')
    # plt.loglog(freqs[mask], hf_psd(freqs[mask])*1., label = 'mlab PSD')
    #
    # plt.loglog(frexcp, Pxcp, label = 'notchy PSD')
    #
    # plt.loglog(frexx[masxx],PDX(frexx,a,b,c)[masxx], label = 'notched pdx fit')
    # plt.legend()
    # plt.savefig('seg.png')

    return psd_params
Exemple #4
0
    def injector(self,strains_in,low_f,high_f, sim = False):
        fs=self.fs        
        dt=1./fs
        
        Nt = len(strains_in[0])
        Nt = lf.bestFFTlength(Nt)
        freqs = np.fft.rfftfreq(2*Nt, dt)
        freqs = freqs[:Nt/2+1]

        #print '+sim+'
    
        psds = []
        faketot = []
        
        if sim == True:     #simulates streams for all detectors called when T.scope was initialised
            fakestreams = self.sim_tstreams(freqs)
            
        for (idx_det,strain_in) in enumerate(strains_in):
        
            '''WINDOWING & RFFTING.'''
            
            strain_in = strain_in[:Nt]
            strain_in_nowin = np.copy(strain_in)
            strain_in_nowin *= signal.tukey(Nt,alpha=0.05)
            strain_in *= np.blackman(Nt)

            hf = np.fft.rfft(strain_in, n=2*Nt)#, norm = 'ortho') 
            hf_nowin = np.fft.rfft(strain_in_nowin, n=2*Nt)#, norm = 'ortho') 
    
            hf = hf[:Nt/2+1]
            hf_nowin = hf_nowin[:Nt/2+1]
            
            '''the PSD. '''
    
            Pxx, frexx = mlab.psd(strain_in_nowin, Fs=fs, NFFT=2*fs,noverlap=fs/2,window=np.blackman(2*fs),scale_by_freq=True)
            hf_psd = interp1d(frexx,Pxx)
            hf_psd_data = abs(hf_nowin.copy()*np.conj(hf_nowin.copy())/(fs**2))
    
    
            #Norm
            mask = (freqs>low_f) & (freqs < high_f)
            norm = np.mean(hf_psd_data[mask])/np.mean(hf_psd(freqs)[mask])
    
            #print norm
    
            hf_psd=interp1d(frexx,Pxx*norm)
            psds.append(hf_psd)
                
            #print frexx, Pxx, len(Pxx)
    
            #Pxx, frexx = mlab.psd(strain_in_win[:Nt], Fs = fs, NFFT = 4*fs, window = mlab.window_none)
            
            # plt.figure()
            # plt.loglog(freqs,np.sqrt(hf_psd_data), color = 'r')
            # plt.loglog(frexx,np.sqrt(Pxx), alpha = 0.5)
            # #plt.loglog(freqs,np.sqrt(hf_psd(freqs)), color = 'g') #(freqs)
            # #plt.ylim([-100.,100.])
            # plt.savefig('pxx.png' )

            if sim == True:
                rands = [np.random.normal(loc = 0., scale = 1. , size = len(hf_psd_data)),np.random.normal(loc = 0., scale = 1. , size = len(hf_psd_data))] 
                fakenoise = rands[0]+1.j*rands[1]
                fake_psd = hf_psd(freqs)*self.fs**2
                fakenoise = np.array(fakenoise*np.sqrt(fake_psd/2.))#np.sqrt(self.fs/2.)#part of the normalization

                fake = np.sum([fakenoise,fakestreams[idx_det]], axis=0)
                fake_inv = np.fft.irfft(fake , n=2*Nt)[:Nt]
                #print fakestreams[idx_det]
        
                faketot.append(fake_inv)
        
        #print '=\sim='
        
        return faketot, psds