def get_amp_data(data_path): p = re.compile(r'amp(\d+).csv') cond = pd.read_csv(filepath_or_buffer=os.path.join(data_path, 'cond.txt'), sep=",", header=None) sample_rate = cond.at[0, 1] mV_per_Pa = cond.at[2, 1] dt = 1.0 / sample_rate results_sound = np.zeros(256) c = 0 for filepath in glob.glob(os.path.join(data_path, '*')): m = p.match(filepath.split(os.path.sep)[-1]) if m is None: continue df = pd.read_csv(filepath_or_buffer=filepath, sep=",") sound = df[' A Max [mV]'] duty = int(m.group(1)) results_sound[duty] = get_40kHz_amp(sound, dt) / mV_per_Pa / np.sqrt(2) c += 1 print_progress(c, 256) print() print(f'max [Pa]: {results_sound.max()}') return normalized(results_sound)
def get_input_data(data_path): p = re.compile(r'input(\d+).csv') cond = pd.read_csv(filepath_or_buffer=os.path.join(data_path, 'cond.txt'), sep=",", header=None) sample_rate = cond.at[0, 1] dt = 1.0 / sample_rate results_input = np.zeros(256) c = 0 for filepath in glob.glob(os.path.join(data_path, '*')): m = p.match(filepath.split(os.path.sep)[-1]) if m is None: continue df = pd.read_csv(filepath_or_buffer=filepath, sep=",") input_sig = df[' A Max [mV]'] duty = int(m.group(1)) results_input[duty] = get_40kHz_amp(input_sig, dt) c += 1 print_progress(c, 256) print() return normalized(results_input)
def get_amp_data(data_path, total): cond = pd.read_csv(filepath_or_buffer=os.path.join(data_path, 'cond.txt'), sep=",", header=None) sample_rate = cond.at[0, 1] mV_per_Pa = cond.at[2, 1] dt = 1.0 / sample_rate p = re.compile( r'x([+-]?\d+\.?\d+?)y([+-]?\d+\.?\d+?)z([+-]?\d+\.?\d+?).csv') results = pd.DataFrame(columns=['amp']) c = 0 for dev_dir in glob.glob(os.path.join(data_path, '*')): for tr_dir in glob.glob(os.path.join(dev_dir, '*')): for filepath in glob.glob(os.path.join(tr_dir, '*')): m = p.match(filepath.split(os.path.sep)[-1]) if m is None: continue z = int(float(m.group(3))) if z != 200: continue df = pd.read_csv(filepath_or_buffer=filepath, sep=",") sound = df[' A Max [mV]'] results.at[c, 'amp'] = get_40kHz_amp( sound, dt) / mV_per_Pa / np.sqrt(2) c += 1 print_progress(c, total) print() results.to_csv('individual_amp.csv')
def calc(data_path): cond = pd.read_csv(filepath_or_buffer=os.path.join(data_path, 'cond.txt'), sep=",", header=None) sample_rate = cond.at[0, 1] mV_per_Pa = cond.at[2, 1] dt = 1.0 / sample_rate p = re.compile(r'x([+-]?\d+\.?\d+?)y([+-]?\d+\.?\d+?)z([+-]?\d+\.?\d+?).csv') x_axis = [] y_axis = [] total = 0 for filepath in glob.glob(os.path.join(data_path, '*')): m = p.match(filepath.split(os.path.sep)[-1]) if m is None: continue x = float(m.group(1)) y = float(m.group(2)) x_axis.append(x) y_axis.append(y) total += 1 x_axis = sorted(set(x_axis)) y_axis = sorted(set(y_axis)) rms = pd.DataFrame(index=y_axis, columns=x_axis) c = 0 for filepath in glob.glob(os.path.join(data_path, '*')): m = p.match(filepath.split(os.path.sep)[-1]) if m is None: continue x = float(m.group(1)) y = float(m.group(2)) df = pd.read_csv(filepath_or_buffer=filepath, sep=",") sound = df[' A Max [mV]'] rms.at[y, x] = get_40kHz_amp(sound, dt) / mV_per_Pa / np.sqrt(2) c += 1 print_progress(c, total) print() rms.to_csv('xy.csv')
def get_phase_data(data_path, total): p = re.compile(r'dev(\d+)') cond = pd.read_csv(filepath_or_buffer=os.path.join(data_path, 'cond.txt'), sep=",", header=None) phases = np.array([]) print_progress(len(phases), total) for dev_dir in glob.glob(os.path.join(data_path, '*')): m = p.match(dev_dir.split(os.path.sep)[-1]) if m is None: continue phases = np.concatenate( [phases, process_phase_data_dev(dev_dir, cond)]) print_progress(len(phases), total) print() results = pd.DataFrame(columns=['phase']) results['phase'] = phases results.to_csv('individual_phase.csv')
def get_amp_data(data_path): p = re.compile(r'duty(\d+).csv') cond = pd.read_csv(filepath_or_buffer=os.path.join(data_path, 'cond.txt'), sep=",", header=None) sample_rate = cond.at[0, 1] mV_per_Pa = cond.at[2, 1] dt = 1.0 / sample_rate duties = [] total = 0 for filepath in glob.glob(os.path.join(data_path, '*')): m = p.match(filepath.split(os.path.sep)[-1]) if m is None: continue duty = int(m.group(1)) duties.append(duty) total += 1 duties = np.array(sorted(duties)) results_sound = pd.DataFrame(index=duties, columns=['rms']) c = 0 for filepath in glob.glob(os.path.join(data_path, '*')): m = p.match(filepath.split(os.path.sep)[-1]) if m is None: continue df = pd.read_csv(filepath_or_buffer=filepath, sep=",") sound = df[' A Max [mV]'] duty = int(m.group(1)) results_sound.at[duty, 'rms'] = get_40kHz_amp(sound, dt) / mV_per_Pa / np.sqrt(2) c += 1 print_progress(c, total) print() return results_sound
def phase(data_path): p = re.compile(r'phase(\d+).csv') cond = pd.read_csv(filepath_or_buffer=os.path.join(data_path, 'cond.txt'), sep=",", header=None) sample_rate = cond.at[0, 1] dt = 1.0 / sample_rate period = 25e-6 df = pd.read_csv(filepath_or_buffer=os.path.join(data_path, 'phase0.csv'), sep=",") sig_base = df[' A Max [mV]'] sig_base = sig_base - sig_base.mean() results = np.zeros(256) c = 0 for filepath in glob.glob(os.path.join(data_path, '*')): m = p.match(filepath.split(os.path.sep)[-1]) if m is None: continue df = pd.read_csv(filepath_or_buffer=filepath, sep=",") sig = df[' A Max [mV]'] sig = sig - sig.mean() corr = np.correlate(sig, sig_base, "full") delay = corr.argmax() - (len(sig) - 1) phases_delay = delay * dt / period * 2 * math.pi if phases_delay < 0: phases_delay += 2 * math.pi phases_delay %= 2 * math.pi phase_value = int(m.group(1)) results[phase_value] = phases_delay c += 1 print_progress(c, 256) print() x = np.linspace(0, 255, 256) fig = plt.figure(figsize=(6, 6), dpi=DPI) ax = fig.add_subplot(111) ax.plot(x, results, marker='.', zorder=2, markersize=4, linestyle='None', label='measured') ax.plot(x, x / 256.0 * 2 * math.pi, zorder=1, linewidth=1, label='theoretical') plt.ylabel('Phase [rad]', fontname='Arial', fontsize=18) plt.xlabel(r'$S$ [-]', fontname='Arial', fontsize=18) plt.yticks([0, math.pi, 2 * math.pi], [r'$0$', r'$\pi$', r'$2\pi$'], fontname='Arial', fontsize=16) plt.xticks([0, 127, 255], ['0', '127', '255'], fontname='Arial', fontsize=16) plt.legend(bbox_to_anchor=(1, 0), loc='lower right', borderaxespad=1, fontsize=16, frameon=False, markerscale=2) # delete right up frame ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left() ax.set_xlim(0, 255) ax.set_ylim(0, 2 * math.pi) plt.tight_layout() plt.savefig(os.path.join('plot', 'measured_phase' + ext), bbox_inches='tight', pad_inches=0)
def calc(): NUM_TRANS_X = 18 * 3 NUM_TRANS_Y = 14 * 3 TRANS_SIZE = 10.16 FREQUENCY = 40e3 TEMPERATURE = 287.6843037730883 Z_DIR = np.array([0., 0., 1.]) # sound source direction R = 8.5 Z = 500.0 array_center = np.array([ TRANS_SIZE * (NUM_TRANS_X - 1) / 2, TRANS_SIZE * (NUM_TRANS_Y - 1) / 2, Z ]) # Observe properties, units are mm X_RANGE = (array_center[0], array_center[0] + 2 * R) # Initialize calculator calculator = CpuCalculator() # initialize position, direction, amplitude and phase of each sound source system = UniformSystem(TEMPERATURE) for y in range(NUM_TRANS_Y): for x in range(NUM_TRANS_X): pos = np.array([TRANS_SIZE * x, TRANS_SIZE * y, 0.]) source = T4010A1(pos, Z_DIR, 1.0, 0.0, FREQUENCY) system.add_wave_source(source) # Generate observe area, units are mm observe_area = GridAreaBuilder()\ .x_range(X_RANGE)\ .y_at(array_center[1])\ .z_at(Z)\ .resolution(RESOLUTION)\ .generate() field = PressureFieldBuffer() foci_x = np.array([x * RESOLUTION for x in range(85 + 1)]) phase_div = range(2, 256) df = pd.DataFrame(columns=['x'].extend(phase_div)) df['x'] = foci_x c = 0 total = len(foci_x) * len(phase_div) for i in phase_div: N = 85 + 1 results = np.zeros(N) d = 0 for focus_x in foci_x: focal_pos = array_center + np.array([focus_x, 0, 0]) Optimizer.focus(system, focal_pos) to_digital(system, i) result = calculator.calculate(system, observe_area, field) max_index = np.argmax(result) results[d] = max_index d += 1 c += 1 print_progress(c, total) df[i] = results df.to_csv('pos_vs_argmax.csv')