pred.keys())[0]]['pressure']['function_values'][0, mics[0] - 1, :, 3]) / 2 L = np.shape(pred[list(pred.keys())[0]]['pressure']['function_values'])[2] fs_pred = L / pred[list(pred.keys())[0]]['pressure']['function_values'][0, 0, -1, 0] df = ((fs_pred**-1) * L)**-1 N = ((fs_pred**-1 * df)**-1) Nfft = np.floor(L / N) pred_spec = list( map( lambda i: fun.msPSD(np.squeeze(pred[list(pred.keys())[0]]['pressure'][ 'function_values'][:, :, :, i]).transpose(), fs_pred, df=df, win=False, ovr=0, save_fig=False, plot=False), np.arange(1, 4))) f_pred = pred_spec[0][0] Xm_avg_pred = np.array([pred_spec[i][1] for i in range(len(pred_spec))]).transpose() Gxx_avg_pred = np.array([pred_spec[i][-1] for i in range(len(pred_spec))]).transpose() # f_pred,Gxx_pred,spl_pred = PSD(pred[list(pred.keys())[0]]['pressure']['function_values'][:,:,-1]) f, Xm_inph_pred, Sxx_inph_pred, Gxx_inph_pred, Gxx_avg_inph_pred = fun.msPSD( np.squeeze(xn_inph_pred), fs_pred, df=df, win=False,
OASPL_pred = 10 * np.log10(np.mean((pred[list(pred.keys())[0]]['pressure']['function_values'][:, :, :, 1:] - np.expand_dims(np.mean(pred[list(pred.keys())[0]]['pressure']['function_values'][:, :, :, 1:], axis = 2), axis = 2)) ** 2, axis=2) / 20e-6 ** 2) #%% p_inph = (pred[list(pred.keys())[0]]['pressure']['function_values'][0, mics[-1] - 1, :, 3] + pred[list(pred.keys())[0]]['pressure']['function_values'][0, mics[0] - 1, :, 3]) / 2 p_outph = (pred[list(pred.keys())[0]]['pressure']['function_values'][0, mics[-1] - 1, :, 3] - pred[list(pred.keys())[0]]['pressure']['function_values'][0, mics[0] - 1, :, 3]) / 2 #%% Nb = 2 L = np.shape(pred[list(pred.keys())[0]]['pressure']['function_values'])[2] fs_pred = L / pred[list(pred.keys())[0]]['pressure']['function_values'][0, 0, -1, 0] df = ((fs_pred**-1)*L)**-1 N = ((fs_pred ** -1 * df)**-1) Nfft = np.floor(L/N) pred_spec = list(map(lambda i: fun.msPSD(np.squeeze(pred[list(pred.keys())[0]]['pressure']['function_values'][:, :, :, i]).transpose(), fs_pred, df = df, win = False, ovr = 0,save_fig = False, plot = False),np.arange(1,4))) f_pred = pred_spec[0][0] Gxx_avg_pred = np.array([pred_spec[i][-1] for i in range(len(pred_spec))]).transpose() f, Xm_inph, Sxx, Gxx, Gxx_avg_inph = fun.msPSD(np.squeeze(p_inph).transpose(), fs_pred, df = df, win = False, ovr = 0, save_fig = False, plot = False) f, Xm, Sxx, Gxx, Gxx_avg_inph_load = fun.msPSD(np.squeeze(p_inph-pred[list(pred.keys())[0]]['pressure']['function_values'][:, :, :, 1]).transpose(), fs_pred, df = df, win = False, ovr = 0, save_fig = False, plot = False) f, Xm_outph, Sxx, Gxx, Gxx_avg_outph = fun.msPSD(np.squeeze(pred[list(pred.keys())[0]]['pressure']['function_values'][:, :, :, 3] - p_inph).transpose(), fs_pred, df = df, win = False, ovr = 0, save_fig = False, plot = False) #%% c = list(mcolors.TABLEAU_COLORS.keys())[:10] #%% Plots predicted pressure time series # Initializes figure with the number of subplots equal to the number of mics specified in the "mics" list fig,ax = plt.subplots(len(mics),1,figsize = (8,6)) # Adds a space in between the subplots and at the bottom for the subplot titles and legend, respectfully. plt.subplots_adjust(hspace = 0.35,bottom = 0.15)
# f1 = lambda a: fun.msPSD(a['Acoustic Data'][:].transpose()/(a['Sensitivities'][:]*1e-3), fs = a['Sampling Rate'][()], df = 5, win= True, ovr = 0.5, plot = False,save_fig=False,f_lim =[10,10e3],levels = [0, 80]) dir ='/Users/danielweitsman/Desktop/Masters_Research/NASA_Coax_2021/Runs/' caseName = ['BG6','Run151'] leglab = '' mic =5 df = 5 start_t = 8 end_t = 15 fig,ax = plt.subplots(1,1,figsize = (6.4,4.5)) plt.subplots_adjust(bottom = 0.15) for i,case in enumerate(caseName): with h5py.File(os.path.join(dir,caseName[i], 'acs_data.h5'), 'r') as dat_file: data = (dat_file['Acoustic Data'][:].transpose()/(dat_file['Sensitivities'][:]*1e-3))[int(start_t*dat_file['Sampling Rate'][()]):int(end_t*dat_file['Sampling Rate'][()])] f,Gxx,Gxx_avg = fun.msPSD(data[:,int(mic-1)], dat_file['Sampling Rate'][()], df=df, ovr=0.5,plot = False,save_fig = False) ax.plot(f,10*np.log10(Gxx_avg*df/20e-6**2),label = case) ax.set_xscale('log') ax.axis([10,20e3,0,80]) ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('$SPL, \: dB\: (re:\: 20 \: \mu Pa)$') ax.grid('on') if isinstance(leglab, list): ax.legend(leglab) else: ax.legend() ax.set_title('Mic: ' + str(mic))
fs_acs = dat_file['Sampling Rate'][()] fs_ttl = round((np.mean(np.diff(dat_file['Time (s)'])))**-1) #%% # generates time vectors for the tac and acoustic data t = np.arange(len(ttl)) / fs_ttl t_acs = np.arange(len(acs_data)) / fs_acs #%% LE_ind1, lim_ind1, rpm_nom1, u_rpm1 = fun.rpm_eval(ttl,fs_ttl,start_t,end_t) ind = list(map(lambda x: bisect(t_acs,x),t[LE_ind1[lim_ind1[0]:lim_ind1[1]]])) f,fs1,spl,u_low, u_high, Xn_avg,Xm_avg,Xn_avg_filt,Xn_bb = fun.harm_extract(acs_data, ind, fs_acs, 0, harm_filt,filt_shaft_harm=True,Nb = 2) f_bb,Xm_bb,Sxx_bb,Gxx_bb,Gxx_avg_bb = fun.msPSD(Xn_bb, fs = fs1, df = df, save_fig = False, plot = False) # construct time vector for the averaged rpm waveform t_rev = (np.arange(len(Xn_avg))*(rpm_nom1/60)**-1/len(Xn_avg)) #%% # Initializes figure with the number of subplots equal to the number of mics specified in the "mics" list fig,ax = plt.subplots(len(mics),1,figsize = (8,6)) # Adds a space in between the subplots and at the bottom for the subplot titles and legend, respectfully. plt.subplots_adjust(hspace = 0.35,bottom = 0.15) # Loops through each mic for i,m in enumerate(mics): # Computes the mean-square PSD spectra for each mic f_ms,Xm_ms,Sxx_ms,Gxx_ms,Gxx_avg_ms = fun.msPSD(acs_data[int(fs_acs * start_t):int(fs_acs * end_t),m-1], fs_acs, df=df, ovr=0.75,plot = False,save_fig = False) # Plots the resulting spectra in dB
bbox_to_anchor=(.25, 0.9)) #%% Overlays the predicted and measured spectra # Initializes figure with the number of subplots equal to the number of mics specified in the "mics" list fig, ax = plt.subplots(len(mics), 1, figsize=(8, 6)) # Adds a space in between the subplots and at the bottom for the subplot titles and legend, respectfully. plt.subplots_adjust(hspace=0.35, bottom=0.15) # Loops through each mic for i, m in enumerate(mics): # Computes the mean-square PSD spectra for each mic f_exp, Xm_exp, Sxx_exp, Gxx_exp, Gxx_avg_exp = fun.msPSD( exp[int(fs * start_t):int(fs * end_t), m - 1], fs, df=df_exp, ovr=0.75, plot=False, save_fig=False) # Plots the resulting spectra in dB if len(mics) > 1: ax[i].plot(f_exp, 10 * np.log10(Gxx_avg_exp * df_exp / 20e-6**2), linestyle=linestyle[1]) ax[i].stem(f, spl[m - 1, :, -1], linefmt=f'C{1}{linestyle[0]}', markerfmt=f'C{1}o', basefmt=f'C{1}') # ax[i].plot(np.squeeze(pred[list(pred.keys())[0]]['octaveFilterSP/spl_octFilt_spectrum']['function_values'][:, m - 1, :, 0]),np.squeeze(pred[list(pred.keys())[0]]['BPM_total_dB'][:, m - 1, :]))
import pyPostAcsFun as fun import numpy as np #%% mic = 9 # prefix to path containing cases dir = '//Users/danielweitsman/Box/Jan21Test/dan_thesis/runs/trim_compare/e2/' # functions to apply to all cases contained in the folder specified as "dir" f1 = lambda a, b: fun.msPSD(a['Acoustic Data'][:].transpose() / (a['Sensitivities'][:] * 1e-3), fs=a['Sampling Rate'][()], df=5, win=True, ovr=0.5, save_path=b, save_fig=True, plot=True, f_lim=[10, 10e3], levels=[0, 100]) f2 = lambda a, b: fun.tseries((a['Acoustic Data (mV)'][:].transpose() / (a['Sensitivities'][:] * 1e-3)), fs=a['Sampling Rate'][()], t_lim=[0, 10], levels=[-0.25, 0.25], save_path=b, save_fig=True) f3 = lambda a, b: fun.spectrogram((a['Acoustic Data'][mic, :].transpose() / (a['Sensitivities'][mic] * 1e-3)), fs=a['Sampling Rate'][()], df=10, ovr=0.5, win=True,
Q_filt = lfilter(b, a, dat_file['Motor2 Torque (Nm)']) Q_avg[i] = np.mean( dat_file['Motor2 Torque (Nm)'][int(t_min * fs):int(t_max * fs)]) # b, a = butter(4, 2000 / (fs / 2), 'lp') a_filt = lfilter(b, a, dat_file['Acceleration_Ch2(m_s2)']) # single sided spectral density of loads df = .25 f, Gxx, Gxx_avg = fun.msPSD( dat_file['Motor2 Thrust (Nm)'][int(t_min * fs):int(t_max * fs)], fs=fs, df=df, win=False, ovr=0, f_lim=[0, 5e3], levels=[0, 100], save_fig=False, save_path='', plot=False) #%% fig, ax = plt.subplots(1, 1, figsize=(6.4, 4.5)) plt.subplots_adjust(bottom=0.15) ax.set_title(case + ': ' + str(round(rpm_avg[i], 1)) + ' rpm') ax.plot(f, 10 * np.log10(Gxx_avg * df)) # ax.set_xscale('log') ax.set_xlim([f[0], 40]) ax.set_xlabel('Frequency (Hz)') ax.set_ylabel('$Thrust \ PSD, \ dB \ (re: \ 1 \ N^2/Hz)$')