def Trms_vs_fq(fqs, jy_spec, umag150=20., B=.008, cen_fqs=None, ntaps=3, window='kaiser3', bm_poly=DEFAULT_BEAM_POLY, bm_fqs=None): if bm_fqs is None: bm_fqs = fqs dfq = fqs[1] - fqs[0] dCH = int(n.around(B / dfq)) if cen_fqs is None: cen_fqs = n.arange(fqs[0]+dCH*dfq*ntaps/2, fqs[-1]-dCH*dfq, dCH*dfq) Tspec = jy_spec * jy2T(bm_fqs, bm_poly=bm_poly) Trms, ks = {}, {} for fq0 in cen_fqs: z = f2z(fq0) umag_fq0 = umag150 * (fq0 / .150) k_pr = dk_du(z) * umag_fq0 ch0 = n.argmin(n.abs(fqs-fq0)) ch1,ch2 = ch0-dCH/2, ch0+dCH/2 _fqs = fqs[ch1:ch2] etas = f2eta(_fqs) k_pl = dk_deta(z) * etas _ks = n.sqrt(k_pr**2 + k_pl**2) V = Tspec[ch1-ntaps/2*(ch2-ch1):ch2+(ntaps-1)/2*(ch2-ch1)] if ntaps <= 1: w = a.dsp.gen_window(V.size, window=window) _Trms = n.fft.ifft(V*w) else: _Trms = pfb.pfb(V, taps=ntaps, window=window, fft=n.fft.ifft) # Trms has both the primary beam and bandwidth divided out, matching Trms in Parsons et al. (2012). Trms[fq0], ks[fq0] = _Trms, (_ks, k_pl, k_pr) return Trms, ks
#plt.ion() ntap = 4 nchan = 1025 nslice = 1024 * 32 nn = 2 * (nchan - 1) x = np.random.randn(nslice + ntap - 1, nn) xx = np.ravel(x) #CUDA expects the window function as an input, so calculate it here. dwin = pfb.sinc_hamming(ntap, nn) win = np.asarray(dwin, dtype='float32') #get the PFB of dxpfb = pfb.pfb(xx, nchan, ntap, pfb.sinc_hamming) #cuda version is fp32, so cast double to single xpfb = np.asarray(dxpfb, dtype='complex64') out = np.empty([nslice, nn], dtype='float32') filt = 0 #you might want this to be ~0.1 if you're using quantized data #set to 0 for no Wiener filtering at all. The proper value #depends on how many bits you use in quantization #do a few loops for timing since the first iteration is slow for i in range(10): t1 = time.time() cuipfb(out.ctypes.data, xpfb.ctypes.data, win.ctypes.data, nchan, nslice, ntap, filt) t2 = time.time() print('took ', t2 - t1, ' seconds to do ipfb, rate of ',
noise = np.random.randint(-2, 2, N) ytot = y1 + y2 + y3 + noise data = np.array([x, ytot]) window1 = generate_win_coeffs(mTaps, pBranches, window_fn="hamming") window2 = generate_win_coeffs(mTaps, int(pBranches / pBsubdiv), window_fn="hamming") naiveFFT(data, sampRate) singleWindowPFB(data, window1, mTaps, sampRate) multiWindowPFB(data, window2, mTaps, int(pBranches / pBsubdiv), sampRate) # This next portion of code compares the CHIME PFB implementation to a naive FFT frec = np.fft.rfftfreq(pBranches) * sampRate Nfreqs = len(frec[frec > 0]) chimePFB = pfb.pfb(ytot, Nfreqs, mTaps) plt.figure() plt.title("Chime PFB output") plt.plot(frec[frec > 0], (2.0 * np.abs(chimePFB[0] / pBranches))) plt.figure() N = len(data[0]) yFTraw = np.fft.fft(data[1]) yFT = 2.0 * np.abs(yFTraw) / N freqs = np.fft.fftfreq(len(yFT)) * sampRate mask = freqs > 0 plt.title("Naive FFT output") plt.plot(freqs[mask], yFT[mask]) plt.xlabel('Frequency Bins') plt.ylabel('Magnitude')
def performInv_Rechan_AC_CC(fileID, fileNum, reChan=1025, originalNumChans=2048, originalSampRate=2 * (125e6), saveFigs=False, trim=True, showRTS=False, performAutoCor=False, performCrossCor=False, doAllSats=False, zpadCoeff=0, newSubLen=500, useChunksOfRTS=True, returnRTS=True, returnFFT=False, freq_ranges=[], freq_range_types=[]): start = t.time() # Extracting PFB data from the files (both polarizations and the channels used) pfb_raw1, pfb_raw2 = read_4bit.read_4bit_new(fileID) rows, nchan = pfb_raw1.shape print('\nRaw data dims in file #' + str(fileNum) + ' = (' + str(rows) + ',' + str(nchan) + ')') # Calculate the effective time per sample in the recovered timestream dt = originalNumChans / originalSampRate / nchan # Performing the inverse PFB on both polarizations. Assume nTaps=4 rts_pol1 = pfb.inverse_pfb(pfb_raw1, 4) rts_pol2 = pfb.inverse_pfb(pfb_raw2, 4) print("Inverted PFB shape in file #" + str(fileNum) + " = " + str(rts_pol1.shape)) # Ravel the output of the inverse PFB (make it one big timestream) and remove edges due to high residuals rts_pol1 = rts_pol1.ravel()[1000:-1000] rts_pol2 = rts_pol2.ravel()[1000:-1000] lenRTS = len(rts_pol1) print("Recovered timestream length (removed edges) in file #" + str(fileNum) + " = " + str(lenRTS)) #print("Effective 'dt' in recovered timestream is " + str(int(dt*10**9)) + " nanoseconds") print("Total time of recovered timestream (removed edges) in file #" + str(fileNum) + " = " + str(round(lenRTS * dt, 3)) + "s") nanosecsPerSamp = dt * 10**9 if (returnFFT or performCrossCor): # Do rechannelization using FFT fft1 = np.fft.rfft(rts_pol1[1000:-1000]) fft2 = np.fft.rfft(rts_pol2[1000:-1000]) print("Rechannelized FFT (max channels) shape = " + str(fft1.shape)) if (performAutoCor): # Do rechannelization using PFB pfb_rechan1 = pfb.pfb(rts_pol1[1000:-1000], reChan) pfb_rechan2 = pfb.pfb(rts_pol2[1000:-1000], reChan) print("Rechannelized PFB (" + str(reChan) + " channels) shape = " + str(pfb_rechan1.shape)) # These parameters control plot label size plt.rc('font', size=14) # controls default text sizes plt.rc('axes', titlesize=30) plt.rc('axes', labelsize=25) plt.rc('xtick', labelsize=18) plt.rc('ytick', labelsize=18) plt.rc('legend', fontsize=18) if performAutoCor: # Performing auto-correlation on the original raw PFB data to see what it looks like pfb_raw1_AC = np.mean(np.abs(pfb_raw1)**2, axis=0) pfb_raw2_AC = np.mean(np.abs(pfb_raw2)**2, axis=0) # Performing auto-correlation Power on the recovered and rechannelized signals. Option to trim unwanted spikes or not pfb_rechan1_AC_PSD = np.mean(np.abs(pfb_rechan1)**2, axis=0) pfb_rechan2_AC_PSD = np.mean(np.abs(pfb_rechan2)**2, axis=0) if trim: # Get rid of artificial spikes pfb_rechan1_AC_PSD = getRidOfSpikes(pfb_rechan1_AC_PSD, 0.2, 6, doPlot=False) pfb_rechan2_AC_PSD = getRidOfSpikes(pfb_rechan2_AC_PSD, 0.2, 30, doPlot=False) plt.figure() plt.plot(pfb_raw1_AC, c='b', label='polar_1') plt.plot(pfb_raw2_AC, c='r', label='polar_2') plt.title("Auto-Correlation of Raw PFB Data for Both Polarizations") plt.xlabel("Frequency Channels") plt.ylabel("Power Spectrum Density") plt.legend() if saveFigs: plt.savefig(r"dataFromEarlyFeb\Figs\f" + str(fileNum) + "_rawPFB_26chan_AC.png") if showRTS: plt.figure() plt.subplot(1, 3, 1) plt.plot(np.arange(1000), rts_pol1[:1000]) plt.subplot(1, 3, 2) plt.plot(np.arange(int(lenRTS / 2) - 500, int(lenRTS / 2) + 500), rts_pol1[int(lenRTS / 2) - 500:int(lenRTS / 2) + 500]) plt.title("Recovered Timestream") plt.xlabel("Timestep = " + str(int(nanosecsPerSamp)) + " ns") plt.subplot(1, 3, 3) plt.plot(np.arange(lenRTS - 1000, lenRTS), rts_pol1[-1000:]) plt.ticklabel_format(useOffset=False) if saveFigs: plt.savefig(r"dataFromEarlyFeb\Figs\f" + str(fileNum) + "_recoveredTimestream.png") plt.figure() plt.plot(pfb_rechan1_AC_PSD, c='b', label='polar_1') plt.plot(pfb_rechan2_AC_PSD, c='r', label='polar_2') plt.xlabel("Frequency Channels") plt.ylabel("Power Spectrum Density") plt.title("Rechannelized (" + str(reChan) + " channels) Auto-Cor For Both Polarizations") plt.legend() if saveFigs: plt.savefig(r"dataFromEarlyFeb\Figs\f" + str(fileNum) + "_" + str(reChan) + "chan_AutoCor_TimeSqrAv.png") if performCrossCor: if len(freq_ranges) == 0: """ Freq ranges for satellites seen by eye using rechannelized auto-cor graph Good Signals: - [75,214] out of 1025 - [350,490] out of 1025 - [738,900] out of 1025 Ok Signals: - [12,75] out of 1025 - [215,340] out of 1025 - [490,615] out of 1025 What are those: - [620,740] out of 1025 `""" freq_ranges_goodSats = np.array([[75, 214], [350, 490], [738, 900]]) if doAllSats: freq_ranges_okSats = np.array([[12, 75], [215, 340], [490, 615]]) freq_ranges_poorSats = np.array([[620, 740]]) freq_ranges = [ freq_ranges_goodSats, freq_ranges_okSats, freq_ranges_poorSats ] else: freq_ranges = [freq_ranges_goodSats] freq_range_types = ['Good', 'Ok', 'Poor'] performCC_analysisOnSats(fft1, fft2, freq_ranges[0], reChan, newSubLen=newSubLen, useChunksOfRTS=useChunksOfRTS, dt=nanosecsPerSamp, dt_units='ns', satDesignation=freq_range_types[0], zpadCoeff=zpadCoeff) if doAllSats: for i in range(1, len(freq_ranges)): performCC_analysisOnSats(fft1, fft2, freq_ranges[i], reChan, newSubLen=newSubLen, useChunksOfRTS=useChunksOfRTS, dt=nanosecsPerSamp, dt_units='ns', satDesignation=freq_range_types[i], zpadCoeff=zpadCoeff) end = t.time() print("Total execution time in 'performInv_Rechan_AC_CC' was " + str(round(end - start, 3)) + " seconds") if returnRTS: return rts_pol1, rts_pol2 elif returnFFT: return fft1, fft2 else: return pfb_rechan1, pfb_rechan2