Esempio n. 1
0
    def analyze(self):
        l,data = self.inp.read()
        #print l
        if l:
            i = 0
            for x in self.indizes:
                try:
                    self.wave[i] = audioop.getsample(data,2,i)/32767.0
                    self.sample[i] = audioop.getsample(data,2,i)
                except audioop.error:
                    break
                i = i+1

            self.freq[self.indizes] = 0
            self.bands[[0,1,2]] = 0
            fftw3.execute(self.plan)
            ilc = self.nfreq * (self.low_cut_f/ (self.rate*0.5)) - 1
            ihc = self.nfreq * (self.high_cut_f/ (self.rate*0.5)) - 1
            ilc = max(ilc, 1)
            nlow = int(ilc)
            nmid = int(ihc - ilc)
            nhigh = int(self.nfreq - nmid)

            #print nlow, nmid, nhigh

            for i in range(self.nfreq):
                self.spect[i] = math.sqrt(self.freq[i].real*self.freq[i].real + self.freq[i].imag*self.freq[i].imag) * math.log1p(i+2)
                self.spect_flux[i] = self.spect[i] - self.spect_old[i]
                self.spect_old[i] = self.spect[i]
                self.spect_flux[i] = max(self.spect_flux[i], 0.0)
                if((i < ilc) and (i < ihc)):
                    self.bands[0] += self.spect_flux[i]/nlow
                elif((i<ihc) and (i < self.nfreq)):
                    self.bands[1] += self.spect_flux[i]/nmid
                else:
                    self.bands[2] += self.spect_flux[i]/nhigh

            for i in [0,1,2]:
                temp = max(self.bands[i], 0,0)
                old_temp = self.bands_avg[i]
                self.bands[i] = max((temp - old_temp - self.amp_thres), 0,0)
                if(self.nframe > 1):
                    if(self.bands[i] == 0): self.bands_avg[i] = temp*0.15 + self.bands_avg[i]*0.85
                    if(self.bands[i] > self.bands_s[i]):
                        self.bands_s[i] = self.bands[i]
                    else:
                        self.bands_s[i] = self.bands_s[i] * 0.15 + self.bands[i]*0.85
                else:
                    self.bands_avg[i] = temp
                    self.bands_s[i] = self.bands[i]

            self.nframe = self.nframe + 1
Esempio n. 2
0
    1. / N * fn[None, :, None]) * np.sinc(1. / N * fnc[None, None, :])
if rank == 0:
    t0 = time.time()
    ##################################Wdeltag########################################
    print '=' * 80
    print 'starting cal wdengx wdengy'
    deltagw1 = np.empty((N, N, N / 2 + 1), dtype=np.complex128)
    deltagw2 = np.empty((N, N, N / 2 + 1), dtype=np.complex128)
    deltax1 = np.empty_like(deltax, dtype=np.float64)
    deltax2 = np.empty_like(deltax, dtype=np.float64)
    deltak = np.empty((N, N, N / 2 + 1), dtype=np.complex128)
    fft = fftw.Plan(inarray=deltax,
                    outarray=deltak,
                    direction='forward',
                    nthreads=nthreads)
    fftw.execute(fft)
    fftw.destroy_plan(fft)
    k[0, 0, 0] = 10**-4 / Kf
comm.Scatter(deltak, recvdata_k1, root=0)  #deltak  smoothed log
W = wk(k * Kf)
if rank == 0:
    W[0, 0, 0] = 1
deltak1 = recvdata_k1 * W * 1j * Kf * (mpi_fn[rank][:, None, None] +
                                       np.zeros_like(fn)[None, :, None] +
                                       np.zeros_like(fnc)[None, None, :])
deltak2 = recvdata_k1 * W * 1j * Kf * (
    np.zeros_like(mpi_fn[rank])[:, None, None] + fn[None, :, None] +
    np.zeros_like(fnc)[None, None, :])
comm.Gather(deltak1, deltagw1, root=0)
comm.Gather(deltak2, deltagw2, root=0)
if rank == 0:
Esempio n. 3
0
    import time
    Pk0=np.empty((N,N,N/2+1),dtype=np.float64)
    deltax=np.linspace(0,N,N**3).reshape(N,N,N)
    change=np.array(Tide.LoadData(Input),dtype=np.float64)
    deltax[:]=change[:]
    deltax=np.array(deltax,dtype=np.float64)
    del change
    sum=deltax.sum()
    deltax*=(N**3/sum)   #for halo, the data is n/nbar.
###################################smooth#######################################
    print '='*80
    print 'smoothing...'
    t0=time.time()
    deltak=np.empty((N,N,N/2+1),dtype=np.complex128)
    fft=fftw.Plan(inarray=deltax,outarray=deltak,direction='forward',nthreads=nthreads)
    fftw.execute(fft)
    fftw.destroy_plan(fft)
    smooth_k=np.empty((N,N,N/2+1),dtype=np.complex128)
k=(mpi_fn[rank][:,None,None]**2.+fn[None,:,None]**2.+fnc[None,None,:]**2)**(1./2.)
window_k= np.sinc(1./N*mpi_fn[rank][:,None,None])*np.sinc(1./N*fn[None,:,None])*np.sinc(1./N*fnc[None,None,:])
comm.Scatter(deltak,recvdata_k1,root=0) #deltak
sum=comm.bcast(sum,root=0) #deltak
senddata_k1=recvdata_k1*np.exp(-0.5*Kf*Kf*k*k*Sigma**2)/window_k      #smooth_k
Ph=L**3/N**6*np.abs(senddata_k1)**2
Wiener=Ph/(Ph+(L**3)/sum)   #wiener filter
senddata_k1*=Wiener
Pk_halo=np.abs(recvdata_k1/window_k)**2
Pk_halo*=(L**3/N**6)
Pk_halo=np.array(Pk_halo,dtype=np.float64)
comm.Gather(senddata_k1,smooth_k,root=0)
comm.Gather(Pk_halo,Pk0,root=0)
Esempio n. 4
0
File: rsd.py Progetto: POFK/Tide
if rank==0:
    import time
    print '='*80
    print 'displacement'
    t0=time.time()
    smooth_x=Tide.LoadDataOfhdf5(Outfile+'0.000den00_s1.25.hdf5')
    halo=np.linspace(0,N,N**3).reshape(N,N,N)
    change=np.array(Tide.LoadData(Input),dtype=np.float64)
    halo[:]=change[:]
    halo=np.array(halo,dtype=np.float64)
    del change
    smooth_k=np.empty((N,N,N/2+1),dtype=np.complex128)
    s_k=np.empty((N,N,N/2+1),dtype=np.complex128)
    fft=fftw.Plan(inarray=smooth_x,outarray=smooth_k,direction='forward',nthreads=nthreads)
    fftw.execute(fft)
    fftw.destroy_plan(fft)

comm.Scatter(smooth_k,recvdata_k1,root=0) #deltak
#comm.Scatter(smooth_x,mpi_halo,root=0) #deltak##############################
comm.Scatter(halo,mpi_halo,root=0) #deltak##############################
k_mag=Kf*(mpi_fn[rank][:,None,None]**2.+fn[None,:,None]**2.+fnc[None,None,:]**2)**(1./2.)
if rank==0:
    k_mag[0,0,0]=1
kp=Kf*(np.zeros_like(mpi_fn[rank])[:,None,None]**2.+np.zeros_like(fn)[None,:,None]**2.+fnc[None,None,:]**2)**(1./2.)
mu=kp/k_mag
senddata_k1=-1j/bias/k_mag*recvdata_k1/(1+beta*mu**2)
senddata_k1*=mu
comm.Gather(senddata_k1,s_k,root=0)
if rank==0:
    s_x=np.empty((N,N,N),dtype=np.float64)
Esempio n. 5
0
factor=0.308**0.6
beta=factor/bias
################################################################################

if rank==0:
    import time
    print '='*80
    print 'displacement'
    t0=time.time()
    smooth_x=Tide.LoadDataOfhdf5(Outfile+'0.000den00_s1.25.hdf5')
    halo=Tide.LoadData(Input)
    halo=np.array(halo,dtype=np.float64)
    smooth_k=np.empty((N,N,N/2+1),dtype=np.complex128)
    s_k=np.empty((N,N,N/2+1),dtype=np.complex128)
    fft=fftw.Plan(inarray=smooth_x,outarray=smooth_k,direction='forward',nthreads=nthreads)
    fftw.execute(fft)
    fftw.destroy_plan(fft)

comm.Scatter(smooth_k,recvdata_k1,root=0) #deltak
comm.Scatter(halo,mpi_halo,root=0) #deltak
k_mag=(mpi_fn[rank][:,None,None]**2.+fn[None,:,None]**2.+fnc[None,None,:]**2)**(1./2.)
kp=(np.zeros_like(mpi_fn[rank])[:,None,None]**2.+np.zeros_like(fn)[None,:,None]**2.+fnc[None,None,:]**2)**(1./2.)
mu=kp/k_mag
senddata_k1=-1j/bias/k_mag*recvdata_k1/(1+beta*mu**2)
comm.Gather(senddata_k1,s_k,root=0)

if rank==0:
    s_x=np.empty((N,N,N),dtype=np.float64)
    ifft=fftw.Plan(inarray=s_k,outarray=s_x,direction='backward',nthreads=nthreads)
    fftw.execute(ifft)
    fftw.destroy_plan(ifft)
Esempio n. 6
0
	def GetRadioFFTbox(self):
		params = self.params
		resultf = params['hr'][0]
		if len(params['last']) != 0:
			resultf = resultf + params['last'][0]
		resultf = resultf + '-' + params['hr'][1]
		if len(params['last']) != 0:
			resultf = resultf + params['last'][1]

		# Make parent directory and write parameter file.
		kiyopy.utils.mkparents(params['output_root'])
		parse_ini.write_params(params, 
			params['output_root']+'params.ini',prefix='pk_' )
		in_root = params['input_root']
		out_root = params['output_root']
		mid = params['mid']
		all_out_fname_list = []
		all_in_fname_list = []
		OmegaHI = params['OmegaHI']
		Omegam = params['Omegam']
		OmegaL = params['OmegaL']
		fkpp = params['FKPpk']
		FKPweight = params['FKPweight']
		
		#### Process ####
		pol_str = params['polarizations'][0]
		hr_str = params['hr'][0]
		end = pol_str
		if len(params['last']) != 0:
			end = end + params['last'][0]
		box_fname = in_root + 'fftbox_' + hr_str + mid[0] + end + '.npy'
		box = np.load(box_fname)

		nbox_fname = in_root + 'fftbox_' + hr_str + mid[1] + end + '.npy'
		nbox = np.load(nbox_fname)

		#Using map in different day 
		hr_str = params['hr'][1]
		end = pol_str
		if len(params['last']) != 0:
			end = end + params['last'][1]
		box_fname = in_root + 'fftbox_' + hr_str + mid[0] + end + '.npy'
		box2 = np.load(box_fname)

		nbox_fname = in_root + 'fftbox_' + hr_str + mid[1] + end + '.npy'
		nbox2 = np.load(nbox_fname)

		normal = (nbox**2).flatten().sum()
		normal2 = (nbox2**2).flatten().sum()
		normal = sqrt(normal)*sqrt(normal2)
		#print normal
		box = box*nbox
		box2 = box2*nbox2
		#box = nbox*nbox
		#box2 = nbox2*nbox2

		V = params['boxunit']**3

		print "PowerMaker: FFTing "
		inputa = np.zeros(params['boxshape'], dtype=complex)
		outputa = np.zeros(params['boxshape'], dtype=complex)
		fft = FFTW.Plan(inputa,outputa, direction='forward', flags=['measure'])
		inputa.imag = 0.
		inputa.real = box
		FFTW.execute(fft)

		#print outputa[10][10]

		inputb = np.zeros(params['boxshape'], dtype=complex)
		outputb = np.zeros(params['boxshape'], dtype=complex)
		fft = FFTW.Plan(inputb,outputb, direction='forward', flags=['measure'])
		inputb.imag = 0.
		inputb.real = box2
		FFTW.execute(fft)
		
		#print outputb[10][10]

		fftbox = (outputa.__mul__(outputb.conjugate())).real

		#fftbox = (outputa*(outputa.conjugate())).real
		fftbox = fftbox*V/normal #/2./pi/pi/pi
		#boxN = params['boxshape'][0]*params['boxshape'][1]*params['boxshape'][2]
		#fftbox = fftbox*V/boxN #/2./pi/pi/pi

		return fftbox
Esempio n. 7
0
    def GetRadioFFTbox(self):
        params = self.params
        resultf = params["hr"][0]
        if len(params["last"]) != 0:
            resultf = resultf + params["last"][0]
        resultf = resultf + "-" + params["hr"][1]
        if len(params["last"]) != 0:
            resultf = resultf + params["last"][1]

            # Make parent directory and write parameter file.
        kiyopy.utils.mkparents(params["output_root"])
        parse_ini.write_params(params, params["output_root"] + "params.ini", prefix="pk_")
        in_root = params["input_root"]
        out_root = params["output_root"]
        mid = params["mid"]
        all_out_fname_list = []
        all_in_fname_list = []
        OmegaHI = params["OmegaHI"]
        Omegam = params["Omegam"]
        OmegaL = params["OmegaL"]
        fkpp = params["FKPpk"]
        FKPweight = params["FKPweight"]

        #### Process ####
        pol_str = params["polarizations"][0]
        hr_str = params["hr"][0]
        end = pol_str
        if len(params["last"]) != 0:
            end = end + params["last"][0]
        imap_fname = in_root + hr_str + mid[0] + end + ".npy"
        imap = algebra.load(imap_fname)
        imap = algebra.make_vect(imap)
        if imap.axes != ("freq", "ra", "dec"):
            raise ce.DataError("AXES ERROR!")

        box = np.load(imap_fname)

        nmap_fname = in_root + hr_str + mid[1] + end + ".npy"
        try:
            nmap = algebra.load(nmap_fname)
            nmap = algebra.make_vect(nmap)

            bad = nmap < 1.0e-5 * nmap.flatten().max()
            nmap[bad] = 0.0
            non0 = nmap.nonzero()
            # imap[non0] = imap[non0]/nmap[non0]
        except IOError:
            print "NO Noise File :: Set Noise to One"
            nmap = algebra.info_array(sp.ones(imap.shape))
            nmap.axes = imap.axes
            nmap = algebra.make_vect(nmap)
        if FKPweight:
            for i in range(nmap.shape[0]):
                # nmap[i] = nmap[i]/(1.+nmap[i]*fkpp)
                nmap[i] = 1.0 / (1.0 + nmap[i] * fkpp)

                # Using map in different day
        hr_str = params["hr"][1]
        end = pol_str
        if len(params["last"]) != 0:
            end = end + params["last"][1]
        imap_fname = in_root + hr_str + mid[0] + end + ".npy"
        imap2 = algebra.load(imap_fname)
        imap2 = algebra.make_vect(imap2)
        if imap2.axes != ("freq", "ra", "dec"):
            raise ce.DataError("AXES ERROR!")

        box2 = np.load(imap_fname)

        nmap_fname = in_root + hr_str + mid[1] + end + ".npy"
        try:
            nmap2 = algebra.load(nmap_fname)
            nmap2 = algebra.make_vect(nmap2)

            bad = nmap2 < 1.0e-5 * nmap2.flatten().max()
            nmap2[bad] = 0.0
            non0 = nmap2.nonzero()
            # imap2[non0] = imap2[non0]/nmap2[non0]
        except IOError:
            print "NO Noise File :: Set Noise to One"
            nmap2 = algebra.info_array(sp.ones(imap2.shape))
            nmap2.axes = imap2.axes
            nmap2 = algebra.make_vect(nmap2)
        if FKPweight:
            for i in range(nmap.shape[0]):
                # nmap2[i] = nmap2[i]/(1.+nmap2[i]*fkpp)
                nmap2[i] = 1.0 / (1.0 + nmap2[i] * fkpp)

                # print imap2.flatten().min()
                # print dmap2.flatten().min()
                # print nmap2.flatten().sum()

        mapshape = np.array(imap.shape)
        # print imap.shape

        r = self.discrete(self.fq2r(imap.get_axis("freq")))
        ra = self.discrete(imap.get_axis("ra")) * deg2rad
        de = self.discrete(imap.get_axis("dec")) * deg2rad
        ra0 = ra[int(ra.shape[0] / 2)]
        ra = ra - ra0
        dr = r.ptp() / r.shape[0]
        dra = ra.ptp() / ra.shape[0]
        dde = de.ptp() / de.shape[0]
        disc_n = params["discrete"]

        # print r.min(), r.max()
        # print self.xyz(ra.min(), de.min(), r.min())
        # print self.xyz(ra.max(), de.min(), r.min())
        # print self.xyz(ra.min(), de.max(), r.min())
        # print self.xyz(ra.max(), de.max(), r.min())
        # print self.xyz(ra.min(), de.min(), r.max())
        # print self.xyz(ra.max(), de.min(), r.max())
        # print self.xyz(ra.min(), de.max(), r.max())
        # print self.xyz(ra.max(), de.max(), r.max())

        # return 0

        mapinf = [dr, dra, dde, disc_n]
        mapinf = np.array(mapinf)

        # box = algebra.info_array(sp.zeros(params['boxshape']))
        # box.axes = ('x','y','z')
        # box = algebra.make_vect(box)
        # boxshape = np.array(box.shape)

        # box2 = algebra.info_array(sp.zeros(params['boxshape']))
        # box2.axes = ('x','y','z')
        # box2 = algebra.make_vect(box2)

        xrange0 = params["Xrange"][0]
        yrange0 = params["Yrange"][0]
        zrange0 = params["Zrange"][0]
        boxunit = params["boxunit"]
        shapex = params["boxshape"][2]
        shapera = ra.shape[0]
        V = params["boxunit"] ** 3

        boxinf = [xrange0, yrange0, zrange0, boxunit]
        boxinf = np.array(boxinf)

        print "PowerMaker: Filling the BOX"
        # MakePower.Filling(imap, imap2, box, box2, r, ra, de, boxinf, mapinf)
        # MakePower.Filling(dmap, dmap2, box, box2, r, ra, de, boxinf, mapinf)

        # normalize
        # nbox = algebra.info_array(sp.zeros(params['boxshape']))
        # nbox.axes = ('x','y','z')
        # nbox = algebra.make_vect(nbox)
        # nbox2 = algebra.info_array(sp.zeros(params['boxshape']))
        # nbox2.axes = ('x','y','z')
        # nbox2 = algebra.make_vect(nbox2)

        ##non0 = nmap.nonzero()
        ##nmap[non0] = np.sqrt(1./nmap[non0])
        ##non0 = nmap2.nonzero()
        ##nmap2[non0] = np.sqrt(1./nmap2[non0])

        # MakePower.Filling(nmap, nmap2, nbox, nbox2, r, ra, de, boxinf, mapinf)

        nbox = algebra.info_array(sp.ones(params["boxshape"]))
        nbox.axes = ("x", "y", "z")
        nbox = algebra.make_vect(nbox)
        nbox2 = algebra.info_array(sp.ones(params["boxshape"]))
        nbox2.axes = ("x", "y", "z")
        nbox2 = algebra.make_vect(nbox2)

        if params["saveweight"]:
            sp.save(out_root + "Weight_" + resultf, nbox)
            sp.save(out_root + "Weight2_" + resultf, nbox2)
            print "\t::Weight Saved "

        normal = (nbox ** 2).flatten().sum()
        normal2 = (nbox2 ** 2).flatten().sum()
        normal = sqrt(normal) * sqrt(normal2)
        # print normal
        box = box * nbox
        box2 = box2 * nbox2

        print "PowerMaker: FFTing "
        inputa = np.zeros(params["boxshape"], dtype=complex)
        inputa.real = box
        outputa = np.zeros(params["boxshape"], dtype=complex)
        fft = FFTW.Plan(inputa, outputa, direction="forward", flags=["measure"])
        FFTW.execute(fft)
        # box = outputa.real**2 + outputa.imag**2
        # inputb = np.zeros(params['boxshape'], dtype=complex)
        # inputb.real = box2
        # outputb = np.zeros(params['boxshape'], dtype=complex)
        # fft = FFTW.Plan(inputb,outputb, direction='forward', flags=['measure'])
        # FFTW.execute(fft)
        fftbox = (outputa * (outputa.conjugate())).real
        fftbox = fftbox * V / normal  # /2./pi/pi/pi
        # boxN = params['boxshape'][0]*params['boxshape'][1]*params['boxshape'][2]
        # fftbox = 2*fftbox*V/boxN #/2./pi/pi/pi

        return fftbox
Esempio n. 8
0
	def GetWindowFunctionData(self):
		params = self.params
		boxshape = params['boxshape']
		boxunit = params['boxunit']
		resultf = params['hr'][0]
		if len(params['last']) != 0:
			resultf = resultf + params['last'][0]
		resultf = resultf + '-' + params['hr'][1]
		if len(params['last']) != 0:
			resultf = resultf + params['last'][1]
		
		FKPweight = params['FKPweight']
		in_root = params['input_root']
		out_root = params['output_root']
		mid = params['mid']
		fkpp = params['FKPpk']

		# Make parent directory and write parameter file.
		kiyopy.utils.mkparents(params['output_root'])
		parse_ini.write_params(params, 
			params['output_root']+'params.ini',prefix='wd_' )
		all_out_fname_list = []
		all_in_fname_list = []
		
		#### Process ####
		pol_str = params['polarizations'][0]

		hr_str = params['hr'][0]
		end = pol_str
		if len(params['last']) != 0:
			end = end + params['last'][0]
		imap_fname = in_root + hr_str + mid[0] + end + '.npy'
		imap = algebra.load(imap_fname)
		imap = algebra.make_vect(imap)
		if imap.axes != ('freq', 'ra', 'dec') :
			raise ce.DataError('AXES ERROR!')
		nmap_fname = in_root + hr_str + mid[1] + end + '.npy'
		try:
			nmap = algebra.load(nmap_fname)
			nmap = algebra.make_vect(nmap)

			bad = nmap<1.e-5*nmap.flatten().max()
			nmap[bad] = 0.
			non0 = nmap.nonzero()
			#imap[non0] = imap[non0]/nmap[non0]
		except IOError:
			print 'NO Noise File :: Set Noise to One'
			nmap = algebra.info_array(sp.ones(imap.shape))
			nmap.axes = imap.axes
			nmap = algebra.make_vect(nmap)
		nmap.info = imap.info
		if FKPweight:
			for i in range(nmap.shape[0]):
				nmap[i] = 1./(1.+nmap[i]*fkpp)
				#nmap[i] = nmap[i]/(1.+nmap[i]*fkpp)


		hr_str = params['hr'][1]
		end = pol_str
		if len(params['last']) != 0:
			end = end + params['last'][1]
		nmap_fname = in_root + hr_str + mid[1] + end + '.npy'
		try:
			nmap2 = algebra.load(nmap_fname)
			nmap2 = algebra.make_vect(nmap2)
	
			bad = nmap2<1.e-5*nmap2.flatten().max()
			nmap2[bad] = 0.
			non0 = nmap2.nonzero()
		except IOError:
			print 'NO Noise File :: Set Noise to One'
			nmap2 = algebra.info_array(sp.ones(imap.shape))
			nmap2.axes = imap.axes
			nmap2 = algebra.make_vect(nmap2)
		if FKPweight:
			for i in range(nmap.shape[0]):
				nmap2[i] = 1./(1.+nmap2[i]*fkpp)
				#nmap2[i] = nmap2[i]/(1.+nmap2[i]*fkpp)


		weight, weight2 = self.fill(nmap, nmap2)

		#MakePower.Filling(nmap, weight, r, ra, de, boxinf, mapinf)
		#MakePower.Filling(nmap2, weight2, r, ra, de, boxinf, mapinf)
		#weight_fname = in_root + 'Weight_' + resultf + '.npy'
		#weight = algebra.load(weight_fname)
		#weight = algebra.make_vect(weight)

		#weight_fname = in_root + 'Weight2_' + resultf + '.npy'
		#weight2 = algebra.load(weight_fname)
		#weight2 = algebra.make_vect(weight2)
		
		normal = (weight**2).flatten().sum()
		normal2 = (weight2**2).flatten().sum()
		normal = sqrt(normal)*sqrt(normal2)


		print "WindowFunctionMaker: FFTing "
		inputa = np.zeros(params['boxshape'], dtype=complex)
		outputa = np.zeros(params['boxshape'], dtype=complex)
		fft = FFTW.Plan(inputa,outputa, direction='forward', flags=['measure'])
		inputa.imag = 0.
		inputa.real = weight
		FFTW.execute(fft)

		#inputa = np.zeros(params['boxshape'], dtype=complex)
		outputb = np.zeros(params['boxshape'], dtype=complex)
		fft = FFTW.Plan(inputa,outputb, direction='forward', flags=['measure'])
		inputa.imag = 0.
		inputa.real = weight2
		FFTW.execute(fft)

		V = params['boxunit']**3

		fftbox = (outputa*(outputb.conjugate())).real
		fftbox = fftbox*V/normal

		WindowF = np.zeros(40)
		k = np.zeros(40)
		WindowF2 = np.zeros(shape=(10, 10))
		k2 = np.zeros(shape=(2, 10))
		MakePower.Make(fftbox, WindowF, k, WindowF2, k2)
		kunit = 2.*pi/(params['boxunit'])
		k = k*kunit
		k2 = k2*kunit
		#WindowF = WindowF#/V
		#WindowF2 = WindowF2#/V

		return WindowF, k
Esempio n. 9
0
import fftw3
import numpy as np
from scipy.constants import pi
from scipy import signal
import matplotlib.pyplot as plt
T_start = -10  #Beginning Time
T_end = 10  #Ending time
T = T_end - T_start  #Total time
num = 1000  #Number of samples
inputa = np.zeros(num, dtype=complex)
outputa = np.zeros(num, dtype=complex)
timea = np.linspace(T_start, T_end, num)  #Time domain
sa = np.linspace(0, T, num)  #Samples
freqa = 2 * pi * sa / T  #Frequency domain
c = 0
mean = 5
stdev = 0.010
'''
for i in list:
    #elt=np.exp(-(i-mean)**2/(2*stdev**2))
    elt=np.sin(i)
    inputa[c]+=elt  
    c+=1
'''
inputa = 2 * pi * np.sin(timea)
fft = fftw3.Plan(inputa, outputa, direction='forward', flags=['measure'])
#ifft=fftw3.Plan(outputa, inputa, direction='backward', flags=['measure'])

fftw3.execute(fft)
plt.plot(freqa, abs(outputa))
plt.show()
Esempio n. 10
0
	def GetFFTbox(self):

		params = self.params
		in_root = params['input_root']
		out_root = params['output_root']
		filename = params['filename']
		selection = params['selection']
		OmegaHI = params['OmegaHI']
		Omegam = params['Omegam']
		OmegaL = params['OmegaL']
		FKPweight = params['FKPweight']

		imap_fname = in_root + filename + '.npy'
		imap = algebra.load(imap_fname)
		imap = algebra.make_vect(imap)
		if imap.axes != ('freq', 'ra', 'dec') :
			raise ce.DataError('AXES ERROR!')

		nmap_fname = in_root + selection + '.npy'
		try:
			nmap = algebra.load(nmap_fname)
			nmap = algebra.make_vect(nmap)

			bad = nmap<1.e-5*nmap.flatten().max()
			nmap[bad] = 0.
			non0 = nmap.nonzero()
			#imap[non0] = imap[non0]/nmap[non0]
		except IOError:
			print 'NO Noise File :: Set Noise to One'
			nmap = algebra.info_array(sp.ones(imap.shape))
			nmap.axes = imap.axes
			nmap = algebra.make_vect(nmap)
		if FKPweight:
			for i in range(nmap.shape[0]):
				nmap[i] = nmap[i]/(1.+nmap[i]*1.e4)

		#Using map in different day 
		imap2 = imap.copy()
		if imap2.axes != ('freq', 'ra', 'dec') :
			raise ce.DataError('AXES ERROR!')

		nmap2 = nmap.copy()

		mapshape = np.array(imap.shape)

		r  = self.discrete(self.fq2r(imap.get_axis('freq')))
		ra = self.discrete(imap.get_axis('ra'))*deg2rad
		de = self.discrete(imap.get_axis('dec'))*deg2rad
		ra0= ra[int(ra.shape[0]/2)]
		ra = ra - ra0
		dr = r.ptp()/r.shape[0]
		dra= ra.ptp()/ra.shape[0]
		dde= de.ptp()/de.shape[0]
		disc_n = params['discrete']


		mapinf = [dr, dra, dde, disc_n]
		mapinf = np.array(mapinf)

		box = algebra.info_array(sp.zeros(params['boxshape']))
		box.axes = ('x','y','z')
		box = algebra.make_vect(box)

		box2 = algebra.info_array(sp.zeros(params['boxshape']))
		box2.axes = ('x','y','z')
		box2 = algebra.make_vect(box2)
		
		boxshape = np.array(box.shape)

		xrange0 = params['Xrange'][0]
		yrange0 = params['Yrange'][0]
		zrange0 = params['Zrange'][0]
		boxunit = params['boxunit']
		shapex = params['boxshape'][2]
		shapera = ra.shape[0]
		V = params['boxunit']**3

		boxinf = [xrange0, yrange0, zrange0, boxunit]
		boxinf = np.array(boxinf)

		MakePower.Filling(imap, imap2, box, box2, r, ra, de, boxinf, mapinf)
		#MakePower.Filling(dmap, dmap2, box, box2, r, ra, de, boxinf, mapinf)

		# normalize 
		nbox = algebra.info_array(sp.zeros(params['boxshape']))
		nbox.axes = ('x','y','z')
		nbox = algebra.make_vect(nbox)
		nbox2 = algebra.info_array(sp.zeros(params['boxshape']))
		nbox2.axes = ('x','y','z')
		nbox2 = algebra.make_vect(nbox2)

		#non0 = nmap.nonzero()
		#nmap[non0] = np.sqrt(1./nmap[non0])
		#non0 = nmap2.nonzero()
		#nmap2[non0] = np.sqrt(1./nmap2[non0])

		MakePower.Filling(nmap, nmap2, nbox, nbox2, r, ra, de, boxinf, mapinf)
		#if params['saveweight']:
		#	sp.save(out_root+'Weight_'+resultf, nbox)
		#	sp.save(out_root+'Weight2_'+resultf, nbox2)
		#	print '\t::Weight Saved '

		normal = (nbox**2).flatten().sum()
		normal2 = (nbox2**2).flatten().sum()
		normal = sqrt(normal)*sqrt(normal2)
		box = box*nbox
		box2 = box2*nbox2

		inputa = np.zeros(params['boxshape'], dtype=complex)
		inputa.real = box
		outputa = np.zeros(params['boxshape'], dtype=complex)
		fft = FFTW.Plan(inputa,outputa, direction='forward', flags=['measure'])
		FFTW.execute(fft)
		box = outputa.real**2 + outputa.imag**2
		inputa = np.zeros(params['boxshape'], dtype=complex)
		inputa.real = box2
		outputa = np.zeros(params['boxshape'], dtype=complex)
		fft = FFTW.Plan(inputa,outputa, direction='forward', flags=['measure'])
		FFTW.execute(fft)
		box2 = outputa.real**2 + outputa.imag**2
		fftbox = box.__pow__(0.5)*box2.__pow__(0.5)
		fftbox = fftbox*V*V/normal/V #/2./pi/pi/pi
		
		return fftbox
Esempio n. 11
0
import fftw3
import numpy as np
from scipy.constants import pi
from scipy import signal
import matplotlib.pyplot as plt
T_start = -10     #Beginning Time
T_end = 10      #Ending time
T=T_end-T_start    #Total time
num = 1000       #Number of samples   
inputa=np.zeros(num,dtype=complex)
outputa=np.zeros(num,dtype=complex)
timea =np.linspace(T_start,T_end,num)   #Time domain
sa=np.linspace(0,T,num) #Samples
freqa = 2*pi*sa/T  #Frequency domain
c=0
mean=5
stdev=0.010
'''
for i in list:
    #elt=np.exp(-(i-mean)**2/(2*stdev**2))
    elt=np.sin(i)
    inputa[c]+=elt  
    c+=1
'''
inputa = 2*pi*np.sin(timea)
fft=fftw3.Plan(inputa, outputa, direction='forward', flags=['measure'])
#ifft=fftw3.Plan(outputa, inputa, direction='backward', flags=['measure'])

fftw3.execute(fft)
plt.plot(freqa,abs(outputa))
plt.show()
Esempio n. 12
0
	def execute(self, nprocesses=1):
		params = self.params
		resultf = params['hr'][0]
		if len(params['last']) != 0:
			resultf = resultf + params['last'][0]
		resultf = resultf + '-' + params['hr'][1]
		if len(params['last']) != 0:
			resultf = resultf + params['last'][1]

		# Make parent directory and write parameter file.
		kiyopy.utils.mkparents(params['output_root'])
		parse_ini.write_params(params, params['output_root']+'params.ini',prefix='pk_' )
		in_root = params['input_root']
		out_root = params['output_root']
		mid = params['mid']
		all_out_fname_list = []
		all_in_fname_list = []
		
		#### Process ####
		pol_str = params['polarizations'][0]
		hr_str = params['hr'][0]
		end = pol_str
		if len(params['last']) != 0:
			end = end + params['last'][0]
		imap_fname = in_root + hr_str + mid[0] + end + '.npy'
		imap = algebra.load(imap_fname)
		imap = algebra.make_vect(imap)
		if imap.axes != ('freq', 'ra', 'dec') :
			raise ce.DataError('AXES ERROR!')

		nmap_fname = in_root + hr_str + mid[1] + end + '.npy'
		try:
			nmap = algebra.load(nmap_fname)
			nmap = algebra.make_vect(nmap)

			bad = nmap<1.e-5*nmap.flatten().max()
			nmap[bad] = 0.
			#non0 = nmap.nonzero()
			#imap[non0] = imap[non0]/nmap[non0]
		except IOError:
			print 'NO Noise File :: Set Noise to One'
			nmap = algebra.info_array(sp.ones(imap.shape))
			nmap.axes = imap.axes
			nmap = algebra.make_vect(nmap)



		##  Using map in different day 
		hr_str = params['hr'][1]
		end = pol_str
		if len(params['last']) != 0:
			end = end + params['last'][1]
		imap_fname = in_root + hr_str + mid[0] + end + '.npy'
		imap2 = algebra.load(imap_fname)
		imap2 = algebra.make_vect(imap2)
		if imap2.axes != ('freq', 'ra', 'dec') :
			raise ce.DataError('AXES ERROR!')

		nmap_fname = in_root + hr_str + mid[1] + end + '.npy'
		try:
			nmap2 = algebra.load(nmap_fname)
			nmap2 = algebra.make_vect(nmap2)
	
			bad = nmap2<1.e-5*nmap2.flatten().max()
			nmap2[bad] = 0.
			#non0 = nmap2.nonzero()
			#imap2[non0] = imap2[non0]/nmap2[non0]
		except IOError:
			print 'NO Noise File :: Set Noise to One'
			nmap2 = algebra.info_array(sp.ones(imap2.shape))
			nmap2.axes = imap2.axes
			nmap2 = algebra.make_vect(nmap2)

		## FKP ##
		fkp = algebra.info_array(sp.ones(imap.shape))
		fkp.axes = imap.axes
		fkp = algebra.make_vect(fkp)

		fkp2 = algebra.info_array(sp.ones(imap.shape))
		fkp2.axes = imap.axes
		fkp2 = algebra.make_vect(fkp2)

		OmegaHI = params['OmegaHI']
		Omegam = params['Omegam']
		OmegaL = params['OmegaL']
		freq = imap.get_axis('freq')
		z = 1.4e9/freq -1.
		a3 = (1+z)**(-3)
		Tb = 0.3e-3 * (OmegaHI/1.e-3) * ((Omegam + a3*OmegaL)/0.29)**(-0.5)\
			* ((1.+z)/2.5)**0.5

		for i in range(fkp.shape[0]):
			fkp[i] = fkp[i]*Tb[i]/(1.+Tb[i]*1.e4)
			fkp2[i] = fkp2[i]*Tb[i]/(1.+Tb[i]*1.e4)
		
			

		#print imap2.flatten().min()
		#print dmap2.flatten().min()
		#print nmap2.flatten().sum()

		mapshape = np.array(imap.shape)
		#print imap.shape

		r  = self.discrete(self.fq2r(imap.get_axis('freq')))
		ra = self.discrete(imap.get_axis('ra'))*deg2rad
		de = self.discrete(imap.get_axis('dec'))*deg2rad
		ra0= ra[int(ra.shape[0]/2)]
		ra = ra - ra0
		dr = r.ptp()/r.shape[0]
		dra= ra.ptp()/ra.shape[0]
		dde= de.ptp()/de.shape[0]
		disc_n = params['discrete']

		#print r.min(), r.max()
		#print self.xyz(ra.min(), de.min(), r.min())
		#print self.xyz(ra.max(), de.min(), r.min())
		#print self.xyz(ra.min(), de.max(), r.min())
		#print self.xyz(ra.max(), de.max(), r.min())
		#print self.xyz(ra.min(), de.min(), r.max())
		#print self.xyz(ra.max(), de.min(), r.max())
		#print self.xyz(ra.min(), de.max(), r.max())
		#print self.xyz(ra.max(), de.max(), r.max())

		#return 0

		mapinf = [dr, dra, dde, disc_n]
		mapinf = np.array(mapinf)

		box = algebra.info_array(sp.zeros(params['boxshape']))
		box.axes = ('x','y','z')
		box = algebra.make_vect(box)
		boxshape = np.array(box.shape)

		box2 = algebra.info_array(sp.zeros(params['boxshape']))
		box2.axes = ('x','y','z')
		box2 = algebra.make_vect(box2)
		
		xrange0 = params['Xrange'][0]
		yrange0 = params['Yrange'][0]
		zrange0 = params['Zrange'][0]
		boxunit = params['boxunit']
		shapex = params['boxshape'][2]
		shapera = ra.shape[0]

		boxinf = [xrange0, yrange0, zrange0, boxunit]
		boxinf = np.array(boxinf)

		print "PowerMaker: Filling the BOX"
		MakePower.Filling(imap, imap2, box, box2, r, ra, de, boxinf, mapinf)
		#MakePower.Filling(dmap, dmap2, box, box2, r, ra, de, boxinf, mapinf)

		# normalize 
		nbox = algebra.info_array(sp.zeros(params['boxshape']))
		nbox.axes = ('x','y','z')
		nbox = algebra.make_vect(nbox)
		nbox2 = algebra.info_array(sp.zeros(params['boxshape']))
		nbox2.axes = ('x','y','z')
		nbox2 = algebra.make_vect(nbox2)

		#non0 = nmap.nonzero()
		#nmap[non0] = np.sqrt(1./nmap[non0])
		#non0 = nmap2.nonzero()
		#nmap2[non0] = np.sqrt(1./nmap2[non0])

		MakePower.Filling(nmap, nmap2, nbox, nbox2, r, ra, de, boxinf, mapinf)
		if params['saveweight']:
			sp.save(out_root+'Weight_'+resultf, nbox)
			sp.save(out_root+'Weight2_'+resultf, nbox2)
			print '\t::Weight Saved'

		## FKP box ##
		fkpbox = algebra.info_array(sp.zeros(params['boxshape']))
		fkpbox.axes = ('x','y','z')
		fkpbox = algebra.make_vect(fkpbox)
		fkpbox2 = algebra.info_array(sp.zeros(params['boxshape']))
		fkpbox2.axes = ('x','y','z')
		fkpbox2 = algebra.make_vect(fkpbox2)
		MakePower.Filling(fkp, fkp2, fkpbox, fkpbox2, r, ra, de, boxinf, mapinf)
		fkpnormal = (fkpbox**2).flatten().sum()

		#nbox = np.ones(params['boxshape'])
		#nbox2 = np.ones(params['boxshape'])

		#bad = nbox<1.e-5*nbox.flatten().max()
		#nbox[bad] = 0.
		#non0 = nbox.nonzero()
		#nbox[non0] = 1./nbox[non0]
		normal = (nbox**2).flatten().sum()
		#bad = nbox2<1.e-5*nbox2.flatten().max()
		#nbox2[bad] = 0.
		#non0 = nbox2.nonzero()
		#nbox2[non0] = 1./nbox2[non0]
		normal2 = (nbox2**2).flatten().sum()
		normal = sqrt(normal)*sqrt(normal2)
		#print normal

		#box = box*nbox
		#box2 = box2*nbox2

		box = box*fkpbox
		box2 = box2*fkpbox2

		print "PowerMaker: FFTing "
		inputa = np.zeros(params['boxshape'], dtype=complex)
		inputa.real = box.copy()
		outputa = np.zeros(params['boxshape'], dtype=complex)
		fft = FFTW.Plan(inputa,outputa, direction='forward', flags=['measure'])
		FFTW.execute(fft)
		box = outputa.real**2 + outputa.imag**2
		inputa = np.zeros(params['boxshape'], dtype=complex)
		inputa.real = box2.copy()
		outputa = np.zeros(params['boxshape'], dtype=complex)
		fft = FFTW.Plan(inputa,outputa, direction='forward', flags=['measure'])
		FFTW.execute(fft)
		box2 = outputa.real**2 + outputa.imag**2
		fftbox = box.__pow__(0.5)*box2.__pow__(0.5)

		V = params['boxunit']**3

		#fftbox = fftbox*V*V/normal #/2./pi/pi/pi

		fftbox = fftbox*V*V/fkpnormal #/2./pi/pi/pi
      
		#boxN = params['boxshape'][0]*params['boxshape'][1]*params['boxshape'][2]
		#fftbox = fftbox*V*V/boxN #/2./pi/pi/pi

		PK = np.zeros(40)
		k = np.zeros(40)
		PK2 = np.zeros(shape=(10, 10))
		k2 = np.zeros(shape=(2, 10))
		MakePower.Make(fftbox, PK, k, PK2, k2)
		kunit = 2.*pi/(boxunit)
		k = k*kunit
		k2 = k2*kunit
		PK = PK/V
		PK2 = PK2/V
		#PK = PK/(V*boxN)
		#PK2 = PK2/V/boxN

		non0 = PK.nonzero()
		#print PK2
		#print PK
		#print k[non0]
		#return 0
		sp.save(out_root+'fkpPK_'+resultf, PK)
		sp.save(out_root+'fkpPK2_'+resultf, PK2)
		sp.save(out_root+'fkpk_'+resultf, k)

		if self.plot==True:
			print PK[non0]
			plt.figure(figsize=(8,8))
			#print k
			#print PK
			plt.subplot('211')
			plt.scatter(k.take(non0), PK.take(non0))
			plt.loglog()
			plt.ylim(ymin=1.e-8)	
			plt.xlim(xmin=k.min(), xmax=k.max())
			plt.title('Power Spectrum')
			plt.xlabel('$k$')
			plt.ylabel('$P(k) (Kelvin^{2}(h^{-1}Mpc)^3)$')

			PK = PK*k*k*k/2./pi/pi
			#print PK
			plt.subplot('212')
			plt.scatter(k.take(non0), PK.take(non0))
			plt.loglog()
			plt.ylim(ymin=1.e-16)	
			plt.xlim(xmin=k.min(), xmax=k.max())
			plt.xlabel('$k (h Mpc^{-1})$')
			plt.ylabel('$\Delta^2 (Kelvin^{2})$')
			#plt.show()
			plt.savefig(out_root+'fkppower_'+resultf+'.eps', format='eps')

			PK2 = np.log10(PK2)
			plt.figure(figsize=(6,6))
			extent = (k2[0][0], k2[0][-1], k2[1][0], k2[1][-1])
			plt.imshow(PK2, origin='lower', extent = extent, interpolation='nearest')
			plt.xlabel('$k vertical (h Mpc^{-1})$')
			plt.ylabel('$k parallel (h Mpc^{-1})$')
			cb = plt.colorbar()
			cb.set_label('$lg(P^{2D}_{k_pk_v}) (Kelvin^2(h^{-1}Mpc)^3)$')
			plt.loglog()
			plt.savefig(out_root+'fkppower2_'+resultf+'.eps', format='eps')

			plt.show()
			#print 'Finished @_@ '
		return PK