def proc_file(file): fobj = bu.hsDat(file, load=True) vperp = fobj.dat[:, 0] elec3 = fobj.dat[:, 1] * tabor_mon_fac inds = np.abs(full_freqs - fspin) < 200.0 elec3_fft = np.fft.rfft(elec3) true_fspin = full_freqs[np.argmax(np.abs(elec3_fft))] pm_freq = fobj.pm_freq amp, phase_mod = bu.demod(vperp, true_fspin, fsamp, plot=plot_demod, \ filt=True, bandwidth=bandwidth, \ notch_freqs=notch_freqs, notch_qs=notch_qs, \ tukey=True, tukey_alpha=5.0e-4, \ detrend=detrend, detrend_order=1, harmind=2.0) drive_amp, drive_phase_mod = \ bu.demod(elec3, true_fspin, fsamp, plot=plot_demod, \ filt=True, bandwidth=bandwidth, \ notch_freqs=notch_freqs, notch_qs=notch_qs, \ tukey=True, tukey_alpha=5.0e-4, \ detrend=detrend, detrend_order=1, harmind=1.0) phase_mod_fft = np.fft.rfft(phase_mod)[out_inds] * fac drive_phase_mod_fft = np.fft.rfft(drive_phase_mod)[out_inds] * fac return (phase_mod_fft, drive_phase_mod_fft, pm_freq)
def proc_file(file): fobj = bu.hsDat(file, load=True) vperp = fobj.dat[:, 0] elec3 = fobj.dat[:, 1] phi_dg = fobj.attribs['phi_dg'] inds = np.abs(full_freqs - fspin) < 200.0 elec3_fft = np.fft.rfft(elec3) true_fspin = full_freqs[np.argmax(np.abs(elec3_fft) * inds)] print(true_fspin) # true_fspin = np.average(full_freqs[inds], weights=np.abs(elec3_fft)[inds]) true_fspin = 25000.1 # try: amp, phase_mod = bu.demod(vperp, true_fspin, fsamp, plot=plot_demod, \ filt=True, bandwidth=bandwidth, \ notch_freqs=notch_freqs, notch_qs=notch_qs, \ tukey=True, tukey_alpha=5.0e-4, \ detrend=detrend, detrend_order=1, harmind=2.0, \ force_2pi_wrap=force_2pi_wrap) # except: # phase_mod = 1e-3 * np.random.randn(len(vperp)) phase_mod_fft = np.fft.rfft(phase_mod)[out_inds] * fac drive_fft = elec3_fft[drive_out_inds] * fac * tabor_mon_fac return (phase_mod_fft, drive_fft, phi_dg)
def proc_file(file): fobj = bu.hsDat(file, load=True) vperp = fobj.dat[:,0] elec3 = fobj.dat[:,1]*tabor_mon_fac inds = np.abs(full_freqs - fspin) < 200.0 elec3_fft = np.fft.rfft(elec3)*fac true_fspin = np.average(full_freqs[inds], weights=np.abs(elec3_fft)[inds]) amp, phase_mod = bu.demod(vperp, true_fspin, fsamp, plot=plot_demod, \ filt=True, bandwidth=bandwidth, tukey=True, tukey_alpha=5.0e-4, \ detrend=True, detrend_order=1, harmind=2.0) phase_mod_fft = np.fft.rfft(phase_mod)[out_inds] * fac freqs = full_freqs[out_inds] drive_fft = elec3_fft[drive_out_inds] * fac drive_freqs = full_freqs[drive_out_inds] upper_ind = np.argmin(np.abs(freqs - 200.0)) ### Fit a power law to the sideband ASD, ignoring the DC bin popt, pcov = optimize.curve_fit(line, np.log(freqs[1:upper_ind]), \ np.log(np.abs(phase_mod_fft[1:upper_ind])), \ maxfev=10000, p0=[0.0, 0.0]) ### Remove the power law from the data if np.abs(popt[0]) > 0.5: phase_mod_fft *= 1.0 / (np.exp(popt[1]) * freqs**popt[0]) ### Find the peaks phase_mod_peaks = \ bu.find_fft_peaks(freqs, phase_mod_fft, window=window, \ lower_delta_fac=lower_delta_fac, \ delta_fac=delta_fac, \ exclude_df=exclude_df) drive_peaks = \ bu.find_fft_peaks(drive_freqs, drive_fft, window=window, \ lower_delta_fac=10.0, delta_fac=10.0, \ exclude_df=exclude_df) ### Put the power law back in to the peak amplitudes so they can ### be plotted over the original data with the plot_pdet() functions if len(phase_mod_peaks): if np.abs(popt[0]) > 0.5: phase_mod_peaks[:,1] *= (np.exp(popt[1]) * phase_mod_peaks[:,0]**popt[0]) if plot_peaks: bu.plot_pdet([phase_mod_peaks, []], freqs, np.abs(phase_mod_fft), \ loglog=True, show=False) bu.plot_pdet([drive_peaks, []], drive_freqs, np.abs(drive_fft), \ loglog=True, show=True) return (phase_mod_peaks, drive_peaks)
def proc_file(file): fobj = bu.hsDat(file, load=True) vperp = fobj.dat[:, 0] elec3 = fobj.dat[:, 1] elec3_filt = signal.filtfilt(b2, a2, elec3) if plot_raw_dat: fac = bu.fft_norm(nsamp, fsamp) plt.plot(time_vec[:10000], elec3[:10000]) plt.figure() plt.plot(time_vec[:10000], vperp[:10000]) plt.figure() plt.loglog(freqs, fac * np.abs(np.fft.rfft(vperp))) plt.figure() plt.loglog(freqs, fac * np.abs(np.fft.rfft(elec3))) plt.loglog(freqs, fac * np.abs(np.fft.rfft(elec3_filt))) plt.show() input() inds = np.abs(freqs - fspin) < 200.0 elec3_fft = np.fft.rfft(elec3) true_fspin = freqs[np.argmax(np.abs(elec3_fft))] amp, phase_mod = bu.demod(vperp, true_fspin, fsamp, plot=plot_demod, \ filt=True, bandwidth=bandwidth, \ notch_freqs=notch_freqs, notch_qs=notch_qs, \ tukey=True, tukey_alpha=5.0e-4, \ detrend=detrend, detrend_order=1, harmind=2.0, \ force_2pi_wrap=force_2pi_wrap) phase_mod_filt = signal.filtfilt(b3, a3, phase_mod) #phase_mod_filt = phase_mod amp_asd = np.abs(np.fft.rfft(amp)) phase_asd = np.abs(np.fft.rfft(phase_mod)) phase_asd_filt = np.abs(np.fft.rfft(phase_mod_filt)) # popt_n, pcov_n = opti.curve_fit(gauss, freqs[notch_fit_inds], \ # phase_asd_filt[notch_fit_inds], \ # p0=[10000, notch_init, 2, 0]) if apply_notch: notch = freqs[np.argmax(phase_asd_filt * notch_fit_inds)] notch_digital = (2.0 / fsamp) * (notch) for i in range(notch_nharm): bn, an = signal.iirnotch(notch_digital * (i + 1), notch_q) phase_mod_filt = signal.lfilter(bn, an, phase_mod_filt) phase_asd_filt_2 = np.abs(np.fft.rfft(phase_mod_filt)) if correct_noise_color: phase_asd = phase_asd * freqs**noise_color_power phase_asd_filt = phase_asd_filt * freqs**noise_color_power phase_asd_filt_2 = phase_asd_filt_2 * freqs**noise_color_power if plot_phase: plt.plot(freqs, phase_mod) plt.xlabel('Frequency [Hz]') plt.ylabel('Phase [rad]') plt.tight_layout() plt.figure() plt.loglog(freqs, phase_asd) plt.loglog(freqs, phase_asd_filt) plt.loglog(freqs, phase_asd_filt_2) plt.xlabel('Frequency [Hz]') plt.ylabel('Phase ASD [arb]') plt.tight_layout() plt.show() input() freq_mask = (freqs > allowed_freqs[0]) * (freqs < allowed_freqs[1]) max_ind = np.argmax(phase_asd_filt_2 * freq_mask) max_freq = freqs[max_ind] p0 = [10000, max_freq, 5.0, 0] try: popt, pcov = opti.curve_fit(lorentzian, freqs[max_ind-30:max_ind+30], \ phase_asd_filt_2[max_ind-30:max_ind+30], p0=p0, \ maxfev=10000) fit_max = popt[1] fit_std = np.abs(popt[2]) except: print('bad fit...') fit_max = max_freq fit_std = 5.0 * (freqs[1] - freqs[0]) popt = p0 if plot_sideband_fit: plot_freqs = np.linspace(freqs[max_ind - 30], freqs[max_ind + 30], 100) plt.loglog(freqs, phase_asd_filt_2) plt.loglog(plot_freqs, lorentzian(plot_freqs, *popt)) print(fit_max, fit_std) plt.show() input() # if fit_max < 10: # return # if len(wobble_freq): # if (np.abs(fit_max - wobble_freq[-1]) / wobble_freq[-1]) > 0.1: # # plt.loglog(freqs, phase_asd) # # plt.loglog(freqs, phase_asd_filt) # # plt.loglog(freqs, phase_asd_filt_2) # # plt.show() # return elec3_filt_fft = np.fft.rfft(elec3_filt) fit_ind = 100000 short_freqs = np.fft.rfftfreq(fit_ind, d=1.0 / fsamp) zeros = np.zeros(fit_ind) voltage = np.array([zeros, zeros, zeros, elec3_filt[:fit_ind], \ zeros, zeros, zeros, zeros]) efield = bu.trap_efield(voltage * tabor_mon_fac, only_x=True) #efield_mag = np.linalg.norm(efield, axis=0) efield_asd = bu.fft_norm(fit_ind, fsamp) * np.abs( np.fft.rfft(efield[0])) # max_ind = np.argmax(np.abs(elec3_filt_fft)) short_max_ind = np.argmax(efield_asd) # freq_guess = freqs[max_ind] # phase_guess = np.mean(np.angle(elec3_filt_fft[max_ind-2:max_ind+2])) # amp_guess = np.sqrt(2) * np.std(efield[0]) # p0 = [amp_guess, freq_guess, phase_guess, 0] # popt_l, pcov_l = opti.curve_fit(lorentzian, short_freqs[short_max_ind-100:short_max_ind+100], \ # efield_asd[short_max_ind-100:short_max_ind+100], \ # p0=[amp_guess, freq_guess, 100, 0], maxfev=10000) # start_sine = time.time() # popt, pcov = opti.curve_fit(sine, time_vec[:fit_ind], efield[0], \ # sigma=0.01*efield[0], p0=p0) # print popt[0], popt_l[0] * (fsamp / fit_ind) #print popt[0], np.sqrt(pcov[0,0]) # amp_fit = efield_asd[short_max_ind] * np.sqrt(2.0 * fsamp / fit_ind) amp_fit = np.sqrt(2) * np.std(efield[0]) # plt.plot(efield[0]) # plt.show() err_val = np.mean(np.array([efield_asd[short_max_ind-10:short_max_ind], \ efield_asd[short_max_ind+1:short_max_ind+11]]).flatten()) amp_err = np.sqrt( (err_val * fsamp / fit_ind)**2) # + (0.01*amp_fit)**2) # print amp_fit, amp_err # stop_sine = time.time() # print "Field sampling: ", stop_sine - start_sine return [2.0 * amp_fit, np.sqrt(2) * amp_err, fit_max, fit_std]
return A * (x**pow) all_data = [] for meas in itertools.product(gases, inds): gas, ind = meas paths, save_paths = path_dict[gas][ind] for pathind, path in enumerate(paths): if load: continue files, lengths = bu.find_all_fnames(path, sort_time=True) if invert_order: files = files[::-1] fobj = bu.hsDat(files[0], load=True) nsamp = fobj.nsamp fsamp = fobj.fsamp time_vec = np.arange(nsamp) * (1.0 / fsamp) freqs = np.fft.rfftfreq(nsamp, 1.0 / fsamp) upper1 = (2.0 / fsamp) * (2.0 * fspin + 0.5 * bandwidth) lower1 = (2.0 / fsamp) * (2.0 * fspin - 0.5 * bandwidth) upper2 = (2.0 / fsamp) * (fspin + 0.25 * bandwidth) lower2 = (2.0 / fsamp) * (fspin - 0.25 * bandwidth) notch_fit_inds = np.abs(freqs - notch_init) < notch_range
xlim = (0.5, 5000) ylim = (3e-4, 5e0) date = re.search(r"\d{8,}", dir_name)[0] files, _ = bu.find_all_fnames(dir_name, ext='.h5', sort_time=False, skip_subdirectories=False) files = files[file_inds[0]:file_inds[1]:file_step] nfiles = len(files) Ibead = bu.get_Ibead(date=date) fobj = bu.hsDat(files[0], load=False, load_attribs=True) nsamp = fobj.nsamp fsamp = fobj.fsamp fac = bu.fft_norm(nsamp, fsamp) time_vec = np.arange(nsamp) * (1.0 / fsamp) full_freqs = np.fft.rfftfreq(nsamp, 1.0 / fsamp) out_inds = (full_freqs > output_band[0]) * (full_freqs < output_band[1]) out_freqs = full_freqs[out_inds] times = [] for file in files: fobj = bu.hsDat(file, load=False, load_attribs=True) times.append(fobj.time)
def plot_many_spectra(files, data_axes=[0,1,2], colormap='jet', \ sort='time', plot_freqs=(0.0,1000000.0), labels=[]): '''Loops over a list of file names, loads each file, then plots the amplitude spectral density of any number of data channels INPUTS: files, list of files names to extract data data_axes, list of pos_data axes to plot OUTPUTS: none, plots stuff ''' dfig, daxarr = plt.subplots(len(data_axes),sharex=True,sharey=False, \ figsize=(8,8)) if len(data_axes) == 1: daxarr = [daxarr] colors = bu.get_color_map(len(files), cmap=colormap) #colors = ['C0', 'C1', 'C2'] if track_feature: times = [] feature_locs = [] old_per = 0 print("Processing %i files..." % len(files)) for fil_ind, fil in enumerate(files): print(fil) color = colors[fil_ind] # Display percent completion bu.progress_bar(fil_ind, len(files)) # Load data obj = bu.hsDat(fil, load=True) #plt.figure() #plt.plot(df.pos_data[0]) #plt.show() fsamp = obj.attribs['fsamp'] nsamp = obj.attribs['nsamp'] t = obj.attribs['time'] freqs = np.fft.rfftfreq(nsamp, d=1.0 / fsamp) if waterfall: fac = waterfall_fac**fil_ind else: fac = 1.0 if not fullNFFT: NFFT = userNFFT else: NFFT = nsamp for axind, ax in enumerate(data_axes): # if fullNFFT: # NFFT = len(df.pos_data[ax]) # else: # NFFT = userNFFT # asd = np.abs(np.fft.rfft(obj.dat[:,axind])) psd, freqs = mlab.psd(obj.dat[:,axind], Fs=obj.attribs['fsamp'], \ NFFT=NFFT, window=window) asd = np.sqrt(psd) plot_inds = (freqs > plot_freqs[0]) * (freqs < plot_freqs[1]) if len(labels): daxarr[axind].loglog(freqs[plot_inds], asd[plot_inds]*fac, \ label=labels[fil_ind], color=colors[fil_ind]) else: daxarr[axind].loglog(freqs[plot_inds], asd[plot_inds]*fac, \ color=colors[fil_ind]) daxarr[axind].set_ylabel('$\sqrt{\mathrm{PSD}}$') if ax == data_axes[-1]: daxarr[axind].set_xlabel('Frequency [Hz]') if len(axes_labels): for labelind, label in enumerate(axes_labels): daxarr[labelind].set_title(label) if len(labels): daxarr[0].legend(fontsize=10) if len(xlim): daxarr[0].set_xlim(xlim[0], xlim[1]) if len(ylim): daxarr[0].set_ylim(ylim[0], ylim[1]) plt.tight_layout() if savefig: dfig.savefig(fig_savename) plt.show()
def proc_file(file): fobj = bu.hsDat(file, load=True) vperp = fobj.dat[:, 0] elec3 = fobj.dat[:, 1] try: phi_dg = fobj.attribs['phi_dg'] except: phi_dg = 0.0 inds = np.abs(full_freqs - fspin) < 200.0 cut = int(0.1 * fsamp) zeros = np.zeros_like(elec3[:cut]) voltages = [ zeros, zeros, zeros, elec3[:cut], -1.0 * elec3[:cut], zeros, zeros, zeros ] efield = bu.trap_efield(voltages, only_x=True)[0] drive_amp, drive_phase = bu.get_sine_amp_phase(efield) elec3_fft = np.fft.rfft(elec3) true_fspin = np.average(full_freqs[inds], weights=np.abs(elec3_fft)[inds]) carrier_amp, carrier_phase_mod = \ bu.demod(vperp, true_fspin, fsamp, plot=plot_carrier_demod, \ filt=True, bandwidth=bandwidth, notch_freqs=notch_freqs, notch_qs=notch_qs, \ tukey=True, tukey_alpha=5.0e-4, \ detrend=True, detrend_order=1, harmind=2.0) # b1, a1 = signal.butter(3, np.array(libration_filt_band)*2.0/fsamp, btype='bandpass') sos = signal.butter(3, libration_filt_band, btype='bandpass', fs=fsamp, output='sos') # carrier_phase_mod_filt = signal.filtfilt(b1, a1, carrier_phase_mod) carrier_phase_mod_filt = signal.sosfiltfilt(sos, carrier_phase_mod) if len(libration_filt_band): libration_inds = (full_freqs > libration_filt_band[0]) \ * (full_freqs < libration_filt_band[1]) else: libration_inds = np.abs(full_freqs - libration_guess) < 0.5 * libration_bandwidth phase_mod_fft = np.fft.rfft(carrier_phase_mod) * fac lib_fit_x = full_freqs[libration_inds] lib_fit_y = np.abs(phase_mod_fft[libration_inds]) try: try: peaks = bu.find_fft_peaks(lib_fit_x, lib_fit_y, delta_fac=5.0, window=50) ind = np.argmax(peaks[:, 1]) except: peaks = bu.find_fft_peaks(lib_fit_x, lib_fit_y, delta_fac=3.0, window=100) ind = np.argmax(peaks[:, 1]) true_libration_freq = peaks[ind, 0] except: true_libration_freq = lib_fit_x[np.argmax(lib_fit_y)] libration_amp, libration_phase = \ bu.demod(carrier_phase_mod, true_libration_freq, fsamp, \ plot=plot_libration_demod, filt=True, \ filt_band=libration_filt_band, \ bandwidth=libration_bandwidth, \ tukey=False, tukey_alpha=5.0e-4, \ detrend=False, detrend_order=1.0, harmind=1.0) libration_ds, time_vec_ds = \ signal.resample(carrier_phase_mod_filt, t=time_vec, num=out_nsamp) libration_amp_ds, time_vec_ds = \ signal.resample(libration_amp, t=time_vec, num=out_nsamp) libration_ds = libration_ds[out_cut:int(-1 * out_cut)] libration_amp_ds = libration_amp_ds[out_cut:int(-1 * out_cut)] time_vec_ds = time_vec_ds[out_cut:int(-1 * out_cut)] if plot_downsample: plt.plot(time_vec, carrier_phase_mod_filt, color='C0', label='Original') plt.plot(time_vec_ds, libration_ds, color='C0', ls='--', label='Downsampled') plt.plot(time_vec, libration_amp, color='C1') #, label='Original') plt.plot(time_vec_ds, libration_amp_ds, color='C1', ls='--') #, label='Downsampled') plt.legend() plt.show() input() return (time_vec_ds, libration_ds, libration_amp_ds, \ true_libration_freq, phi_dg, drive_amp)