Exemple #1
0
def dxy(dtab, xytab, dout):

    mytb = taskinit.tbtool()

    os.system('cp -r ' + dtab + ' ' + dout)

    # How many spws
    mytb.open(dtab + '/SPECTRAL_WINDOW')
    nspw = mytb.nrows()
    mytb.close()

    for ispw in range(nspw):
        mytb.open(xytab)
        st = mytb.query('SPECTRAL_WINDOW_ID==' + str(ispw))
        x = st.getcol('CPARAM')
        st.close()
        mytb.close()

        mytb.open(dout, nomodify=False)
        st = mytb.query('SPECTRAL_WINDOW_ID==' + str(ispw))
        d = st.getcol('CPARAM')

        # the following assumes all antennas and chans same in both tables.

        # Xinv.D.X:
        d[0, :, :] *= pl.conj(x[0, :, :])
        d[1, :, :] *= x[0, :, :]

        st.putcol('CPARAM', d)
        st.close()
        mytb.close()
def dense_gauss_kernel(sigma, x, y=None):
    xf = pylab.fft2(x)  # x in Fourier domain
    x_flat = x.flatten()
    xx = pylab.dot(x_flat.transpose(), x_flat)  # squared norm of x

    if y is not None:
        yf = pylab.fft2(y)
        y_flat = y.flatten()
        yy = pylab.dot(y_flat.transpose(), y_flat)
    else:
        yf = xf
        yy = xx

    xyf = pylab.multiply(xf, pylab.conj(yf))

    xyf_ifft = pylab.ifft2(xyf)
    row_shift, col_shift = pylab.floor(pylab.array(x.shape) / 2).astype(int)
    xy_complex = pylab.roll(xyf_ifft, row_shift, axis=0)
    xy_complex = pylab.roll(xy_complex, col_shift, axis=1)
    xy = pylab.real(xy_complex)

    scaling = -1 / (sigma**2)
    xx_yy = xx + yy
    xx_yy_2xy = xx_yy - 2 * xy
    k = pylab.exp(scaling * pylab.maximum(0, xx_yy_2xy / x.size))

    return k
Exemple #3
0
def dxy(dtab,xytab,dout):

    mytb=taskinit.tbtool()

    os.system('cp -r '+dtab+' '+dout)

    # How many spws
    mytb.open(dtab+'/SPECTRAL_WINDOW')
    nspw=mytb.nrows()
    mytb.close()


    for ispw in range(nspw):
        mytb.open(xytab)
        st=mytb.query('SPECTRAL_WINDOW_ID=='+str(ispw))
        x=st.getcol('CPARAM')
        st.close()
        mytb.close()

        mytb.open(dout,nomodify=False)
        st=mytb.query('SPECTRAL_WINDOW_ID=='+str(ispw))
        d=st.getcol('CPARAM')

        # the following assumes all antennas and chans same in both tables.

        # Xinv.D.X:
        d[0,:,:]*=pl.conj(x[0,:,:])
        d[1,:,:]*=x[0,:,:]

        st.putcol('CPARAM',d)
        st.close()
        mytb.close()
Exemple #4
0
def pwcausalr(x,Nr,Nl,porder,fs,freq=0): # Note: freq determines whether the frequency points are calculated or chosen
    from pylab import size, shape, real, log, conj, zeros, arange, disp, array
    from numpy import linalg; det=linalg.det
    import numpy as np # Just for "sum"; can't remember what's wrong with pylab's sum
    [L,N] = shape(x); #L is the number of channels, N is the total points in every channel 
     
    if freq==0: F=timefreq(x[0,:],fs) # Define the frequency points
    else: F=array(range(0,freq+1)) # Or just pick them
    npts=size(F,0)
    # Initialize arrays
    maxindex=np.sum(arange(1,L))
    pp=zeros((L,npts))
    # Had these all defined on one line, and stupidly they STAY linked!!
    cohe=zeros((maxindex,npts))
    Fy2x=zeros((maxindex,npts))
    Fx2y=zeros((maxindex,npts))
    Fxy=zeros((maxindex,npts))
    index = 0;

    for i in range(1,L):
        for j in range(i+1,L+1):
            y=zeros((2,N)) # Initialize y
            index = index + 1; 
            y[0,:] = x[i-1,:]; 
            y[1,:] = x[j-1,:];   
            A2,Z2,tmp = armorf(y,Nr,Nl,porder); #fitting a model on every possible pair 
            eyx = Z2[1,1] - Z2[0,1]**2/Z2[0,0]; #corrected covariance 
            exy = Z2[0,0] - Z2[1,0]**2/Z2[1,1]; 
            f_ind = 0; 
            for f in F:
                f_ind = f_ind + 1; 
                S2,H2 = spectrum_AR(A2,Z2,porder,f,fs); 
                pp[i-1,f_ind-1] = abs(S2[0,0]*2);      # revised 
                if (i==L-1) & (j==L):
                    pp[j-1,f_ind-1] = abs(S2[1,1]*2);  # revised 
                cohe[index-1,f_ind-1] = real(abs(S2[0,1])**2 / S2[0,0]/S2[1,1]);   
                Fy2x[index-1,f_ind-1] = log(abs(S2[0,0])/abs(S2[0,0]-(H2[0,1]*eyx*conj(H2[0,1]))/fs)); #Geweke's original measure 
                Fx2y[index-1,f_ind-1] = log(abs(S2[1,1])/abs(S2[1,1]-(H2[1,0]*exy*conj(H2[1,0]))/fs)); 
                Fxy[index-1,f_ind-1] = log(abs(S2[0,0]-(H2[0,1]*eyx*conj(H2[0,1]))/fs)*abs(S2[1,1]-(H2[1,0]*exy*conj(H2[1,0]))/fs)/abs(det(S2))); 
                
    return F,pp,cohe,Fx2y,Fy2x,Fxy
Exemple #5
0
def dense_gauss_kernel(sigma, x, y=None):
    """
    Gaussian Kernel with dense sampling.
    Evaluates a gaussian kernel with bandwidth SIGMA for all displacements
    between input images X and Y, which must both be MxN. They must also
    be periodic (ie., pre-processed with a cosine window). The result is
    an MxN map of responses.

    If X and Y are the same, omit the third parameter to re-use some
    values, which is faster.
    """

    xf = pylab.fft2(x)  # x in Fourier domain
    x_flat = x.flatten()
    xx = pylab.dot(x_flat.transpose(), x_flat)  # squared norm of x

    if y is not None:
        # general case, x and y are different
        yf = pylab.fft2(y)
        y_flat = y.flatten()
        yy = pylab.dot(y_flat.transpose(), y_flat)
    else:
        # auto-correlation of x, avoid repeating a few operations
        yf = xf
        yy = xx

    # cross-correlation term in Fourier domain
    xyf = pylab.multiply(xf, pylab.conj(yf))

    # to spatial domain
    xyf_ifft = pylab.ifft2(xyf)
    #xy_complex = circshift(xyf_ifft, floor(x.shape/2))
    row_shift, col_shift = pylab.floor(pylab.array(x.shape) / 2).astype(int)
    xy_complex = pylab.roll(xyf_ifft, row_shift, axis=0)
    xy_complex = pylab.roll(xy_complex, col_shift, axis=1)
    xy = pylab.real(xy_complex)

    # calculate gaussian response for all positions
    scaling = -1 / (sigma**2)
    xx_yy = xx + yy
    xx_yy_2xy = xx_yy - 2 * xy
    k = pylab.exp(scaling * pylab.maximum(0, xx_yy_2xy / x.size))

    #print("dense_gauss_kernel x.shape ==", x.shape)
    #print("dense_gauss_kernel k.shape ==", k.shape)

    return k
def dense_gauss_kernel(sigma, x, y=None):
    """
    Gaussian Kernel with dense sampling.
    Evaluates a gaussian kernel with bandwidth SIGMA for all displacements
    between input images X and Y, which must both be MxN. They must also
    be periodic (ie., pre-processed with a cosine window). The result is
    an MxN map of responses.

    If X and Y are the same, ommit the third parameter to re-use some
    values, which is faster.
    """

    xf = pylab.fft2(x)  # x in Fourier domain
    x_flat = x.flatten()
    xx = pylab.dot(x_flat.transpose(), x_flat)  # squared norm of x

    if y is not None:
        # general case, x and y are different
        yf = pylab.fft2(y)
        y_flat = y.flatten()
        yy = pylab.dot(y_flat.transpose(), y_flat)
    else:
        # auto-correlation of x, avoid repeating a few operations
        yf = xf
        yy = xx

    # cross-correlation term in Fourier domain
    xyf = pylab.multiply(xf, pylab.conj(yf))

    # to spatial domain
    xyf_ifft = pylab.ifft2(xyf)
    #xy_complex = circshift(xyf_ifft, floor(x.shape/2))
    row_shift, col_shift = pylab.floor(pylab.array(x.shape)/2).astype(int)
    xy_complex = pylab.roll(xyf_ifft, row_shift, axis=0)
    xy_complex = pylab.roll(xy_complex, col_shift, axis=1)
    xy = pylab.real(xy_complex)

    # calculate gaussian response for all positions
    scaling = -1 / (sigma**2)
    xx_yy = xx + yy
    xx_yy_2xy = xx_yy - 2 * xy
    k = pylab.exp(scaling * pylab.maximum(0, xx_yy_2xy / x.size))

    #print("dense_gauss_kernel x.shape ==", x.shape)
    #print("dense_gauss_kernel k.shape ==", k.shape)

    return k
Exemple #7
0
def dense_gauss_kernel(sigma, x, y=None):
    """
    通过高斯核计算余弦子窗口图像块的响应图
    利用带宽是 sigma 的高斯核估计两个图像块 X (MxN) 和 Y (MxN) 的关系。X, Y 是循环的、经余弦窗处理的。输出结果是
    响应图矩阵 MxN. 如果 X = Y, 则函数调用时取消 y,则加快计算。
    该函数对应原文中的公式 (16),以及算法1中的 function k = dgk(x1, x2, sigma)
    :param sigma: 高斯核带宽
    :param x: 余弦子窗口图像块
    :param y: 空或者模板图像块
    :return: 响应图
    """
    # 计算图像块 x 的傅里叶变换
    xf = pylab.fft2(x)  # x in Fourier domain
    # 把图像块 x 拉平
    x_flat = x.flatten()
    # 计算 x 的2范数平方
    xx = pylab.dot(x_flat.transpose(), x_flat)  # squared norm of x

    if y is not None:
        # 一半情况, x 和 y 是不同的,计算 y 的傅里叶变化和2范数平方
        yf = pylab.fft2(y)
        y_flat = y.flatten()
        yy = pylab.dot(y_flat.transpose(), y_flat)
    else:
        # x 的自相关,避免重复计算
        yf = xf
        yy = xx

    # 傅里叶域的互相关计算,逐元素相乘
    xyf = pylab.multiply(xf, pylab.conj(yf))

    # 转化为频率域
    xyf_ifft = pylab.ifft2(xyf)
    # 对频率域里的矩阵块进行滚动平移,分别沿 row 和 col 轴
    row_shift, col_shift = pylab.floor(pylab.array(x.shape) / 2).astype(int)
    xy_complex = pylab.roll(xyf_ifft, row_shift, axis=0)
    xy_complex = pylab.roll(xy_complex, col_shift, axis=1)
    xy = pylab.real(xy_complex)

    # 计算高斯核响应图
    scaling = -1 / (sigma**2)
    xx_yy = xx + yy
    xx_yy_2xy = xx_yy - 2 * xy

    return pylab.exp(scaling * pylab.maximum(0, xx_yy_2xy / x.size))
Exemple #8
0
def myfft_gc_skew(x, M=1000):
    """
    x : GC_skew vector (list)
    param N: length of the GC skew vector
    param M: length of the template
    param A: amplitude between positive and negative GC skew vector

    """

    N = len(x)
    template = get_template(M) + [0] * (N - M)
    template /= pylab.norm(template)

    c = abs(np.fft.ifft(np.fft.fft(x) * pylab.conj(np.fft.fft(template)))**
            2) / pylab.norm(x) / pylab.norm(template)

    # shift the SNR vector by the template length so that the peak is at the END of the template
    c = np.roll(c, M // 2)

    return x, template, c * 2. / N
Exemple #9
0
def detrend(data,detrend_Kernel_Length = 10,sampling_Frequency = 100000,channels = 8):
    from pylab import fft, ifft, sin , cos,log,plot,show,conj,legend
    import random

    n=len(data[0])
    detrend_fft_Length = (2**((log(detrend_Kernel_Length * sampling_Frequency)/log(2)))) 

    ma = [1.0]*sampling_Frequency
    ma.extend([0.0]*(detrend_fft_Length - sampling_Frequency))
    mafft = fft(ma)
    trend = [0.0]*n
    
    for nch in range(channels):
        count = 0
        while count + detrend_fft_Length <= len(data[nch]):
            temp = data[nch][count:count+int(detrend_fft_Length)]
            y = fft(temp)
            z = ifft( conj(mafft)*y)
            for cc in xrange(count,count+(int(detrend_fft_Length)-sampling_Frequency)):
                trend[cc] = z[cc-count].real / sampling_Frequency 
            count = count+(int(detrend_fft_Length)-sampling_Frequency)     
        for cc in xrange(len(trend)):
            data[nch][cc] = data[nch][cc] - trend[cc]
Exemple #10
0
def myfft(N=100000, M=1000, A=0.05, dispersion=0.01):
    """

    param N: length of the GC skew vector
    param M: length of the template
    param A: amplitude between positive and negative GC skew vector

    """
    N = int(N / 2)
    x1 = [A + (random.random() - 0.5) * dispersion for i in range(N)]
    x2 = [-A + (random.random() - 0.5) * dispersion for i in range(N)]
    x = x1 + x2
    x = x[int(N / 4):] + x[0:int(N / 4)]

    template = get_template(M) + [0] * (N * 2 - M)
    template /= norm(template)

    c = abs(fft.ifft(fft.fft(x) * conj(fft.fft(template)))**
            2) / norm(x) / norm(template)

    # shift the SNR vector by the template length so that the peak is at the END of the template
    c = np.roll(c, M // 2)

    return x, template, c * 2. / N
Exemple #11
0
	def __init__(self,model,lam,nIn=1.,nOut=1.,thetaIn=0,pol='Ey',interpolationType='materialBased',
					nInName="top interface",nOutName="bottom interface"):
		M = model
		self.M = M
		self.lam = lam
		self.nIn  = nIn
		self.nOut = nOut
		self.thetaIn = thetaIn
		self.pol = pol
		self.interpolationType = interpolationType
		self.materials = [self.nIn,self.nOut]
		self.materialNames = [nInName,nOutName]

		if self.nIn.imag != 0:
			warnings.warn("Input refractive index HAS to be real! Expect wrong results.",Warning)
		if self.nOut.imag != 0:
			pass
			#warnings.warn("Warning, it is not thorougly tested if a complex epsOut works",Warning)
			#Seems to work.. but I guess transmission parameters make little meaning

		#####################
		##Derived parameters
		#####################
		self.k0    = 2*pi/self.lam			#Wave number			
		self.freq = self.k0/(2*pi)			#Frequency

		self.epsIn = self.nIn**2
		self.epsOut = self.nOut**2

		self.kIn   = self.nIn*self.k0		#Wavenumber for top material
		self.kOut  = self.nOut*self.k0	#Wavenumber for bottom material

		wavefrac = self.lam/(M.elmsize*max(self.nIn,self.nOut))
		if wavefrac < 10:
			warnings.warn("You are using less than 10 elm per wavelength (may be okay"+
								"for highly absorptive/reflective materials",Warning)
		if wavefrac < 6:
			warnings.warn("SERIOUSLY?? You are using less than 6 elm per wavelength (may be okay"+
								"for highly absorptive/reflective materials",Warning)

		self.kInx = self.kIn*sin(self.thetaIn)		#Inc wave vector component
		self.kInz = self.kIn*cos(self.thetaIn)		# -||-

		##################################
		#Precalculated sizes for FE solver
		##################################
		self.alpha0 = self.kInx
		self.alpha = pl.zeros(M.NM)
		#print "Det ser ud til at chi er defineret forkert i Diaz vs Dossou (den skal ikke konjugeres)"
		self.chiIn = pl.zeros(M.NM,dtype='complex')
		self.chiOut = pl.zeros(M.NM,dtype='complex')
		for m in range(-M.Nm,M.Nm+1):
			idx = m+M.Nm
			self.alpha[idx] = self.alpha0+2*pi*m/M.lx

			self.chiIn[idx] = pl.conj(sqrt(0.j+self.kIn**2-self.alpha[idx]**2))
			self.chiOut[idx] = pl.conj(sqrt(0.j+self.kOut**2-self.alpha[idx]**2))

		self.thetaModesIn  = pl.arcsin(0.j+self.alpha/self.kIn)
		self.thetaModesOut = pl.arcsin(0.j+self.alpha/self.kOut)
		self.propModesIn = abs(self.thetaModesIn.imag) < 1e-8	#Slice of incoming propagating modes
		self.propModesOut = abs(self.thetaModesOut.imag) < 1e-8	#Slice of outgoing propagating modes	
import pylab as pl

# plot the magnitude of the amplification factor for CTCS for k Dx = pi/2

# a range of values of c
c = pl.linspace(-1.5,1.5,31)

# two roots of the amplification factor (1j = sqrt(-1) = complex(0,1))
A1 = -1j*c + pl.sqrt(complex(1,0)-c**2)
A2 = -1j*c - pl.sqrt(complex(1,0)-c**2)

# the magnitude squared of each of the amplification factors
mag2A1 = A1*pl.conj(A1)
mag2A2 = A2*pl.conj(A2)

# plot the squared magnitudes
pl.ion()
pl.clf()
font = {'size'   : 24}
pl.rc('font', **font)
pl.grid(True, which='both')
pl.plot(c, mag2A1, 'b', label='positive root')
pl.plot(c, mag2A2, 'r', label='negative root')
pl.legend(loc='best')
pl.xlabel('Courant number, $c$')
pl.ylabel('$||A||^2$')
pl.savefig('CTCSA.pdf')

Exemple #13
0
def TFE(x,y):
    from pylab import fft,zeros,arange,cos,pi,conj,log,plot,show
    #we just need cross-spectral Pxy  
    ## Fill in defaults for arguments that aren't specified
    
    
    Fs = 2
    overlap = .5
    seg_size =2 ** int( log((len(x)/(1-overlap))**.5)*1/log(2) +.5)
    
    #hanning window is H = .5*(1 - cos(2*pi*(0:n-1)'/(n-1)))
    #lets compute this infunctions
    overlap = seg_size*.5
    
    #window = [.5*(1 - cos(2*pi*(n)/(seg_size-1))) for n in xrange(seg_size)]
    window = [0.54 - 0.46 * cos( (2*pi/seg_size) * k) for k in range(0,seg_size)]

    step = seg_size - overlap
    nfft = max(256, len(window))    
    Pxx = [complex(0,0)]*(64)
    Pxy = [complex(0,0)]*(64)
    
    
    avg = sum(x)/len(x)
    x = [xx-avg for xx in x]
    avg = sum(y)/len(y)
    y = [yy-avg for yy in y]


    
    
    
  ## Average the slices
    l=64
    offset = arange(0,len(x)-overlap,step)
    N = len(offset)
    for i in xrange(0,N):
        
        A=x[int(offset[i]):int(offset[i]+seg_size)]
        print seg_size
        A=[A[j]*window[j] for j in xrange(seg_size)]
        #A.extend([0.0]*(len(window)-nfft))
        
        A = fft(A,l)
        
        for j in xrange(seg_size):
            temp = A[j]*conj(A[j])
            Pxx[j]=Pxx[j].real+temp.real
        
        
        B=y[int(offset[i]):int(offset[i]+seg_size)]
        B=[B[j]*window[j] for j in xrange(seg_size)]
        #B.extend([0.0]*(len(window)-nfft))
        B = fft(B,l)
        
        #P = [A[i]*conj(B[i]) for i in xrange(len(A))]
        
        for j in range(seg_size):
            temp = A[j]*conj(B[j])
            Pxy[j]=complex(Pxy[j].real+ temp.real ,Pxy[j].imag-temp.imag)
        #print sum(Pxy)
        
    #plot(Pxy)
    #plot(Pxx)
    #show()
        #print Pxx
        #t = 0
        #for t in range(len(Pxx)):
        #    t= t+int(Pxx[t] == 0.0)
        #    
        #if t:print t,offset[i],offset[i]+step,i
    P = [Pxy[j]/Pxx[j] for j in xrange(len(Pxx))] ;
    P = P[0:nfft/2]
    #print nfft,Fs
    f = arange(0.0,2.0,float(Fs)/(nfft/4))
    return f,P
Exemple #14
0
     avgwf3.MakeSimilarTo(rawwf)
 if wf4count == 0 and channel == OPPI4ChannelNumber:
     avgwf4.MakeSimilarTo(rawwf)
 if goodOPPI3Waveform:
     rawwf.MakeSimilarTo(avgwf3)
     avgwf3 += rawwf
     # Sample baseline from baseline window
     a3base = numpy.ndarray(
         int(baselineAverageTime / sampling_period), dtype="float", buffer=rawwf.GetData()
     )
     a3base = a3base / a3base.max()
     if wf3count == 0:
         a3bFFT = pylab.rfft(a3base)
     else:
         a3bFFT += pylab.rfft(a3base)
         print a3bFFT * pylab.conj(a3bFFT)
         raw_input("Press Enter to continue")
         pylab.subplot(2, 1, 1)
         X3 = numpy.arange(0, numpy.size(a3bFFT))
         pylab.plot(X3, sampling_period * sampling_period * a3bFFT * pylab.conj(a3bFFT), label="OPPI3")
         pylab.subplot(2, 1, 2)
         pylab.psd(a3base, Fs=100e6)
         fig.canvas.draw()
         # pylab.plot(X3,a3bFFT,label='OPPI3')
         # print "OPPI3 ", eventTime , avgwf3.GetLength(), a3.size , a3
     wf3count += 1
 if goodOPPI4Waveform:
     rawwf.MakeSimilarTo(avgwf4)
     avgwf4 += rawwf
     # Sample baseline from baseline window
     a4base = numpy.ndarray(