def conv2d(smaller, larger): """convolve a pair of 2d numpy matrices Uses fourier transform method, so faster if larger matrix has dimensions of size 2**n Actually right now the matrices must be the same size (will sort out padding issues another day!) """ smallerFFT = numpy.fft2(smaller) largerFFT = numpy.fft2(larger) invFFT = numpy.ifft(smallerFFT*largerFFT) return invFFT.real
def getimage(self,z): ''' :param z: the Zernike vector in microns, starting from Z=2 (tip) z[0] is seeing in arcseconds :return: ''' #COMMON imagedata, uampl, filter2, seeing fact = 2.*np.pi/self.alambda nzer = len(z) phase = np.zeros_like(self.zgrid[0]) # empty array for phase for j in range(1, nzer): phase += fact*z[j]*ztools.zernike_estim(j+1,self.zgrid) # # log.debug('GETIMAGE: %s %s'%(phase[0],phase[-1])) # exit(0) uampl = np.zeros((self.ngrid*2,self.ngrid*2),dtype=np.complex) #uampl = np.complex(tmp, tmp) self.uampl = uampl uampl[self.inside] += np.cos(phase) #, uampl[self.inside] += (np.sin(phase)*np.complex(0,1)) #uampl[np.bitwise_not(self.inside)] = 0. self.seeing = z[0] #--------- compute the image ---------------------- # imh = np.abs(ztools.shift(np.fft.ifft2(ztools.shift(uampl,self.ngrid+self.fovpix/2,self.ngrid+self.fovpix/2)),self.ngrid+self.fovpix/2,self.ngrid+self.fovpix/2))**2. imh = np.abs(ztools.shift(np.fft.ifft2(ztools.shift(uampl,self.ngrid,self.ngrid)),self.ngrid,self.ngrid))**2. if (self.sflag > 0): # exact seeing blur filter2 = np.exp(-2.*np.pi**2*(self.seeing/2.35/self.asperpix/2/self.ngrid)**2*self.r**2) # unbinned image imh = np.abs(np.fft2(ztools.shift(np.fft2(imh),self.ngrid,self.ngrid)*filter2)) impix = ztools.rebin(imh,(self.fovpix,self.fovpix)) # rebinning into CCD pixels else: rr = ztools.shift(ztools.dist(self.fovpix),self.fovpix/2,self.fovpix/2) filter2 = np.exp(-2.*np.pi**2*(self.seeing/2.35/self.ccdpix/self.fovpix)**2*rr**2) # binned image impix = ztools.rebin(imh,[self.fovpix,self.fovpix]) # rebinning into CCD pixels impix = np.abs(np.fft.fft2(ztools.shift(np.fft.fft2(impix),self.fovpix/2,self.fovpix/2)*filter2)) # Seeing blur self.filter2 = filter2 return impix/np.sum(impix)
def __init__(self,cambfile='',N=500,L=1.,seed=626): kPk = numpy.loadtxt(cambfile) k,Pk = zip(*kPk) kPkf = scipy.interpolate.interp1d(k,Pk,bounds_error=False,fill_value=0.) kx = map(abs,numpy.fft.fftfreq(N)) ky = map(abs,numpy,fft.fftfreq(N)) rnd = TRandom3(seed) delta_R = [ [ rnd.Gaus(0,1) for kx_ in kx ] for ky_ in ky ] delta_F = numpy.fft2(delta_R) A_F = [ [ kPkf(math.sqrt(kx_**2+ky_**2)) for kx_ in kx ] for ky_ in ky ] for i_ in xrange(N): for j_ in xrange(N): delta_F[i_][j_] *= A_F[i_][j_]/sum(A_F[0]) delta_M = numpy.fft.ifft2(delta_F) self.N = N self.L = L self.k = k self.Pk = Pk self.delta_M = delta_M
def newimage(self, a, jzer): ''' a is the amplitude change of aberration (micron), Jzer is the Zernike number (1=seeing, 4=focus etc.) :param a: :param jzer: :return: ''' #COMMON imagedata, uampl, filter2, seeing newampl = np.array(self.uampl, copy=True) if (jzer > 1): # Change Zernike coefficient newphase = 2. * np.pi / self.alambda * a * ztools.zernike_estim( jzer, self.zgrid) tmp = np.zeros_like(newphase, dtype=np.complex) tmp += np.cos(newphase) tmp += np.sin(newphase) * np.complex(0., 1.) newampl[self.inside] *= tmp filter = self.filter2 else: # new seeing newseeing = self.seeing + a if (self.sflag > 0): filter = np.exp( -2. * np.pi**2 * (newseeing / 2.35 / self.asperpix / 2 / self.ngrid)**2 * self.r**2) # unbinned image else: rr = ztools.shift(ztools.dist(self.fovpix), self.fovpix / 2, self.fovpix / 2) filter = np.exp( -2. * np.pi**2 * (newseeing / 2.35 / self.ccdpix / self.fovpix)**2 * rr**2) # binned image #--------- compute the image ---------------------- imh = np.abs( ztools.shift( np.fft.ifft2(ztools.shift(newampl, self.ngrid, self.ngrid)), self.ngrid, self.ngrid))**2 # imh = np.abs(ztools.shift(np.fft.ifft2(ztools.shift(newampl,self.ngrid+self.fovpix/2,self.ngrid+self.fovpix/2)),self.ngrid+self.fovpix/2,self.ngrid+self.fovpix/2))**2. if (self.sflag > 0): # exact seing blur imh = np.abs( np.fft2( ztools.shift(np.fft.fft2(imh), self.ngrid, self.ngrid) * filter)) impix = ztools.rebin( imh, [self.fovpix, self.fovpix]) # rebinning into CCD pixels else: impix = ztools.rebin( imh, [self.fovpix, self.fovpix]) # rebinning into CCD pixels impix = np.abs( np.fft.fft2( ztools.shift(np.fft.fft2(impix), self.fovpix / 2, self.fovpix / 2) * filter)) # Seeing blur return impix / np.sum(impix)
def scale_image_2d(image, factor): """Scales up the image by the scaling factor. No cropping is done. Input: image factor""" size_x = image.shape[0] size_y = image.shape[1] center_x = size_x/2 center_y = size_y/2 window_x = int(size_x/factor) window_y = int(size_y/factor) image_ft = _numpy.fft2(image[center_x-window_x/2:center_x+window_x/2, center_y-window_y/2:center_y+window_y/2]) image_scaled = abs(_numpy.fft.ifftn(_numpy.fft.fftshift(image_ft), [size_x, size_y])) return image_scaled
def HT_y(y, fft_BT, sf, sz): [ch, n] = y.shape s0 = sf / 2 if ch == 1: z = zeros(sz) z[s0:sf:end, s0:sf:end] = reshape(y, floor(sz / sf)) z = np.real(np.ifft2(np.fft2(z) * fft_BT)) z = z[:] else: z = np.zeros([ch, n * sf**2]) t = np.zeros(sz) for i in range(0, ch): t[::8, ::8] = np.reshape(y[i, :], (np.asarray(sz, dtype=int) / sf).astype(np.int)) Htz = np.real(np.fft.ifft2(np.fft.fft2(t) * fft_BT)) z[i, :] = np.reshape(Htz, Htz.shape[0] * Htz.shape[1]) return z
def newimage(self, a, jzer): ''' a is the amplitude change of aberration (micron), Jzer is the Zernike number (1=seeing, 4=focus etc.) :param a: :param jzer: :return: ''' #COMMON imagedata, uampl, filter2, seeing newampl = np.array(self.uampl,copy=True) if (jzer > 1): # Change Zernike coefficient newphase = 2.*np.pi/self.alambda*a*ztools.zernike_estim(jzer,self.zgrid) tmp = np.zeros_like(newphase,dtype=np.complex) tmp += np.cos(newphase) tmp += np.sin(newphase)*np.complex(0.,1.) newampl[self.inside] *= tmp filter = self.filter2 else: # new seeing newseeing = self.seeing + a if (self.sflag > 0): filter = np.exp(-2.*np.pi**2*(newseeing/2.35/self.asperpix/2/self.ngrid)**2*self.r**2) # unbinned image else: rr = ztools.shift(ztools.dist(self.fovpix),self.fovpix/2,self.fovpix/2) filter = np.exp(-2.*np.pi**2*(newseeing/2.35/self.ccdpix/self.fovpix)**2*rr**2) # binned image #--------- compute the image ---------------------- imh = np.abs(ztools.shift(np.fft.ifft2(ztools.shift(newampl,self.ngrid,self.ngrid)),self.ngrid,self.ngrid))**2 # imh = np.abs(ztools.shift(np.fft.ifft2(ztools.shift(newampl,self.ngrid+self.fovpix/2,self.ngrid+self.fovpix/2)),self.ngrid+self.fovpix/2,self.ngrid+self.fovpix/2))**2. if (self.sflag > 0): # exact seing blur imh = np.abs(np.fft2(ztools.shift(np.fft.fft2(imh),self.ngrid,self.ngrid)*filter)) impix = ztools.rebin(imh,[self.fovpix,self.fovpix]) # rebinning into CCD pixels else: impix = ztools.rebin(imh,[self.fovpix,self.fovpix]) # rebinning into CCD pixels impix = np.abs(np.fft.fft2(ztools.shift(np.fft.fft2(impix),self.fovpix/2,self.fovpix/2)*filter)) # Seeing blur return impix/np.sum(impix)