Beispiel #1
0
def shift_back_pulse(x,phi,neq,mesh,dx):
	
	L=mesh[-1]-mesh[0]+dx
	u=scipy.reshape(x,(neq,scipy.shape(mesh)[0]))
	uhat=1./len(mesh)*scipy.fftpack.rfft(u)
	u_shifted=scipy.outerproduct(uhat[:,0],\
		scipy.ones(scipy.shape(mesh),\
				scipy.float64))
	if len(mesh)%2==0:
		u_shifted=u_shifted+scipy.outerproduct(uhat[:,-1],\
			(-1.)**scipy.arange(len(mesh)))
	for k in range(1,(len(mesh)-1)/2+1):
	  u_shifted=u_shifted+\
	    2*scipy.outerproduct(\
		    uhat[:,2*k-1],scipy.cos(\
		2*k*scipy.pi/L*(mesh-mesh[0]+phi)))-\
	    2*scipy.outerproduct(\
		    uhat[:,2*k],scipy.sin(\
		2*k*scipy.pi/L*(mesh-mesh[0]+phi)))
	x_shifted=scipy.reshape(u_shifted,\
		(neq*scipy.shape(mesh)[0],))
	return x_shifted
Beispiel #2
0
def kron(a,b):
    """
    kron(a,b) is the Kronecker tensor product of a and b.
    The result is a large matrix formed by taking all possible
    products between the elements of a and those of b.   For
    example, if a is 2 by 3, then KRON(a,b) is
 
       [ a[1,1]*b  a[1,2]*b  a[1,3]*b
         a[2,1]*b  a[2,2]*b  a[2,3]*b ]

    Author: Nils Wagner
    """
    if not a.iscontiguous():
        a = scipy.reshape(a, a.shape)
    if not b.iscontiguous():
        b = scipy.reshape(b, b.shape)
    o = scipy.outerproduct(a,b)
    o.shape = a.shape + b.shape
    return scipy.concatenate(scipy.concatenate(o, axis=1), axis=1)
Beispiel #3
0
    def _setTex(self, texName):
	"""
	Users shouldn't use this method
	"""
	self._texName = texName
	res = self.texRes
	#create some helper variables
	onePeriodY = scipy.outerproduct(scipy.arange(0,2*scipy.pi,2*scipy.pi/res), scipy.ones((1,res)))#equivalent to matlab meshgrid
	onePeriodX = scipy.outerproduct(scipy.ones((1,res)), scipy.arange(0,2*scipy.pi,2*scipy.pi/res))
	if type(texName) == scipy.ArrayType:
	    #handle a numpy array
	    intensity = texName.astype(scipy.Float)
	    wasLum = True
	    #is it 1D?
	    if texName.shape[0]==1:
		self._tex1D=True
		res=im.shape[1]
	    elif len(texName.shape)==1 or texName.shape[1]==1:
		self._tex1D=True
		res=texName.shape[0]
	    else:
		self._tex1D=False
		if texName.shape[0]!=texName.shape[1]: raise StandardError, "numpy array for texture was not square"
		res=texName.shape[0]
	elif texName in [None,"none", "None"]:
	    res=4 #4x4 (2x2 is SUPPOSED to be fine but generates wierd colours!)
	    intensity = scipy.ones(res,scipy.Float)
	    wasLum = True
	    self._tex1D=True

	if wasLum and self._tex1D:
	    #for a luminance image, scale RGB channels according to color
	    data = scipy.ones((res,3),scipy.Float)#initialise data array as a float
	    data[:,0] = intensity.flat*self.rgb[0] + self.rgbPedestal[0]#R
	    data[:,1] = intensity.flat*self.rgb[1] + self.rgbPedestal[1]#G
	    data[:,2] = intensity.flat*self.rgb[2] + self.rgbPedestal[2]#B
	    data = misc.float_uint8(self.contrast*data)#data range -1:1 -> 0:255
	    texture = data.tostring()#serialise

	elif wasLum:
	    #for a luminance image, scale RGB channels according to color
	    data = scipy.ones((res,res,3),scipy.Float)#initialise data array as a float
	    data[:,:,0] = intensity*self.rgb[0]	 + self.rgbPedestal[0]#R
	    data[:,:,1] = intensity*self.rgb[1]	 + self.rgbPedestal[1]#G
	    data[:,:,2] = intensity*self.rgb[2]	 + self.rgbPedestal[2]#B
	    data = misc.float_uint8(self.contrast*data)#data range -1:1 -> 0:255
	    texture = data.tostring()#serialise

	#bind the texture in openGL
	if self._tex1D:
	    GL.glEnable(GL.GL_TEXTURE_1D)
	    GL.glBindTexture(GL.GL_TEXTURE_1D, self.texID)#bind that name to the target
	    GL.glTexImage1D(GL.GL_TEXTURE_1D,#target
		0,				#mipmap level
		GL.GL_RGB,	#internal format
		res,			#width
		0,				#border
		GL.GL_RGB, #target format
		GL.GL_UNSIGNED_BYTE, #target data type
		texture)		#the data
	    GL.glTexParameteri(GL.GL_TEXTURE_1D,GL.GL_TEXTURE_WRAP_S,GL.GL_REPEAT)	#makes the texture map wrap (this is actually default anyway)
	    #interpolate with NEAREST NEIGHBOUR. Important if using bits++ because GL_LINEAR
	    #sometimes extrapolates to pixel vals outside range
	    GL.glTexParameteri(GL.GL_TEXTURE_1D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_NEAREST)
	    GL.glTexParameteri(GL.GL_TEXTURE_1D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_NEAREST)
	else:
	    GL.glEnable(GL.GL_TEXTURE_2D)
	    GL.glBindTexture(GL.GL_TEXTURE_2D, self.texID)#bind that name to the target
	    GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB,
		res,res, 0,
		GL.GL_RGB, GL.GL_UNSIGNED_BYTE, texture)
	    GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S,GL.GL_REPEAT)	#makes the texture map wrap (this is actually default anyway)
	    #important if using bits++ because GL_LINEAR
	    #sometimes extrapolates to pixel vals outside range
	    GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_NEAREST) #linear smoothing if texture is stretched?
	    GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_NEAREST) #but nearest pixel value if it's compressed?
	GL.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE)#?? do we need this - think not!