def csd(self,xstr,ystr,nfft=256,noverlap=0,coarsable=False): """Return CSD(cross spectral density) of the time-series. Positional parameters: xstr -- string naming one observable, either 'A' or 'E' ystr -- string naming one observable, either 'A' or 'E' Keyword parameters: nfft -- length of fast fourier transform(fft) (default 256) noverlap -- number of data points overlapping between segments (default 0) coarsable -- return the CSD as a Coarsable object (default False) OUTPUT: f -- frequencies P -- CSD Note: The CPSD is calculated using pl.csd() with an additional factor of dt in the normalisation. If xstr='A' and ystr='E', then the CSD between A and E is returned. """ x = getattr(self,xstr) y = getattr(self,ystr) fs = 1. / x.Cadence1 P , f = pl.csd( x.data , y.data , nfft , fs , noverlap=noverlap ) P = P / fs if coarsable: scale = {'Offset1' : f[0] , 'Cadence1': f[1] - f[0] } f = ASI.Coarsable( f , **scale ) P = ASI.Coarsable( P , **scale ) return f , P
def csd(self, xstr, ystr, nfft=256, noverlap=0, coarsable=False): """Return CSD(cross spectral density) of the time-series. Positional parameters: xstr -- string naming one observable, either 'A' or 'E' ystr -- string naming one observable, either 'A' or 'E' Keyword parameters: nfft -- length of fast fourier transform(fft) (default 256) noverlap -- number of data points overlapping between segments (default 0) coarsable -- return the CSD as a Coarsable object (default False) OUTPUT: f -- frequencies P -- CSD Note: The CPSD is calculated using pl.csd() with an additional factor of dt in the normalisation. If xstr='A' and ystr='E', then the CSD between A and E is returned. """ x = getattr(self, xstr) y = getattr(self, ystr) fs = 1. / x.Cadence1 P, f = pl.csd(x.data, y.data, nfft, fs, noverlap=noverlap) P = P / fs if coarsable: scale = {'Offset1': f[0], 'Cadence1': f[1] - f[0]} f = ASI.Coarsable(f, **scale) P = ASI.Coarsable(P, **scale) return f, P
power_max = 3 Nsegs = 10**np.arange( power_max + 1 ) P12s = [] ; P12s_legend = [] ; C12s = [] ; C12s_legend = [] P13s = [] ; P13s_legend = [] ; C13s = [] ; C13s_legend = [] P23s = [] ; P23s_legend = [] ; C23s = [] ; C23s_legend = [] for i , Nseg in enumerate( Nsegs ) : print "Taking %d averages to esimate spectral densities ... " % ( 2*Nseg ) nfft = int( np.round( N / Nseg ) ) noverlap = nfft / 2 P11 , f = pl.psd( n1 , nfft , fs , noverlap = noverlap ) P22 , f = pl.psd( n2 , nfft , fs , noverlap = noverlap ) P33 , f = pl.psd( n3 , nfft , fs , noverlap = noverlap ) P12 , f = pl.csd( n1 , n2 , nfft , fs , noverlap = noverlap ) P23 , f = pl.csd( n2 , n3 , nfft , fs , noverlap = noverlap ) P13 , f = pl.csd( n1 , n3 , nfft , fs , noverlap = noverlap ) C12 = np.abs( P12 )**2 / ( P11*P22 ) C23 = np.abs( P23 )**2 / ( P22*P33 ) C13 = np.abs( P13 )**2 / ( P11*P33 ) for k in range( C12.shape[0] ) : if C12[k] < 0 : print 'C12[%d] < 0' % k P12s += [ f , np.abs( P12 ) ] ; C12s += [ f , C12 ] ; C12s_legend += [ '%d' % Nseg ] P23s += [ f , np.abs( P23 ) ] ; C23s += [ f , C23 ] ; C23s_legend += [ '%d' % Nseg ] P13s += [ f , np.abs( P13 ) ] ; C13s += [ f , C13 ] ; C13s_legend += [ '%d' % Nseg ] print 'done' print 'C12'
f = csddict['f'].data P12 = csddict['AE'].data P13 = csddict['AT'].data P23 = csddict['ET'].data print 'Estimating power spectral densities from some simulated time-series...' tsdirz = '/gpfs1/JC0311443/workhere/stochasGW/Mapp/runs/EccenctricInclined_eta0_0_xi0_0_sw_1_t0_0/output/signal/point_sources/lon_263_lat_-35_plus_P00_GWslope_0/TDI/Michelson/G2/AET/noise/TDI/Michelson/G2/AET/simulation/by_me/stime_1.0_N_86400_for_days/data/' tspathz = tsdirz + 'd%03d.pkl' % day file = open( tspathz , 'rb' ) ; tsdictz = cpkl.load( file ) ; file.close() tz = tsdictz['t'].data s1z , s2z , s3z = tsdictz['1'].data , tsdictz['2'].data , tsdictz['3'].data stimez = tz[1] - tz[0] ; inittimez = tz[0] ; Nz = tz.shape[0] nfftz = int( np.round( Nz / 100 ) ) noverlapz = nfftz / 2 fsz = 1. / stimez P12z , fz = pl.csd( s1z , s2z , nfftz , fsz , noverlap=noverlapz ) P13z , fz = pl.csd( s1z , s3z , nfftz , fsz , noverlap=noverlapz ) P23z , fz = pl.csd( s2z , s3z , nfftz , fsz , noverlap=noverlapz ) print 'done' fig = plt.figure() fig.suptitle( 'Cross Spectral Densities' ) ax1 = fig.add_subplot(311) ax1.plot( fz , np.real( P12z ) , fz , np.imag( P12z ) , f , np.real( P12 ) , f , np.imag( P12 ) , 'y' ) # ax1.plot( f , np.real( P12 ) , f , np.imag( P12 ) , 'y' ) # ax1.plot( fz , np.real( P12z ) , fz , np.imag( P12z ) ) ax1.legend( ( 'real' , 'imag' ) ) ax1.set_ylabel( 'P12' ) ax2 = fig.add_subplot(312) ax2.plot( fz , np.real( P13z ) , fz , np.imag( P13z ) , f , np.real( P13 ) , f , np.imag( P13 ) , 'y' ) # ax2.plot( f , np.real( P13 ) , f , np.imag( P13 ) , 'y' )
tspath = tsdir + 'd%03d.pkl' % day file = open( tspath , 'rb' ) tsdict = cpkl.load( file ) ; file.close() t = tsdict['t'].data n1 , n2 , n3 = tsdict['1'].data , tsdict['2'].data , tsdict['3'].data stime = t[1] - t[0] ; inittime = t[0] ; N = t.shape[0] print "Taking %d averages to estimate spectral densities ... " % ( 2*Nseg ) nfft = int( np.round( N / Nseg ) ) noverlap = nfft / 2 fs = 1. / stime P12 , f = pl.csd( n1 , n2 , nfft , fs , noverlap = noverlap ) P13 , f = pl.csd( n1 , n3 , nfft , fs , noverlap = noverlap ) P23 , f = pl.csd( n2 , n3 , nfft , fs , noverlap = noverlap ) fig = plt.figure() ax = fig.add_subplot( 311 ) ax.plot( f , np.real( P12 ) , f , np.imag( P12 ) ) ax.legend( ( 'real' , 'imag' ) ) ax.set_ylabel( 'P12' ) ax.set_title( 'Day %d' % day ) ax = fig.add_subplot( 312 ) ax.plot( f , np.real( P13 ) , f , np.imag( P13 ) ) ax.legend( ( 'real' , 'imag' ) ) ax.set_ylabel( 'P13' ) ax = fig.add_subplot( 313 ) ax.plot( f , np.real( P23 ) , f , np.imag( P23 ) )